mirror of
https://github.com/moparisthebest/curl
synced 2024-08-13 17:03:50 -04:00
Support a few more socket options, and refactor the option setting a bit. (Patch from the Google tree.)
This commit is contained in:
parent
6ce589c3ee
commit
08a70d117c
@ -98,6 +98,8 @@ extern "C" {
|
|||||||
#define ARES_OPT_LOOKUPS (1 << 8)
|
#define ARES_OPT_LOOKUPS (1 << 8)
|
||||||
#define ARES_OPT_SOCK_STATE_CB (1 << 9)
|
#define ARES_OPT_SOCK_STATE_CB (1 << 9)
|
||||||
#define ARES_OPT_SORTLIST (1 << 10)
|
#define ARES_OPT_SORTLIST (1 << 10)
|
||||||
|
#define ARES_OPT_SOCK_SNDBUF (1 << 11)
|
||||||
|
#define ARES_OPT_SOCK_RCVBUF (1 << 12)
|
||||||
|
|
||||||
/* Nameinfo flag values */
|
/* Nameinfo flag values */
|
||||||
#define ARES_NI_NOFQDN (1 << 0)
|
#define ARES_NI_NOFQDN (1 << 0)
|
||||||
@ -177,6 +179,8 @@ struct ares_options {
|
|||||||
int ndots;
|
int ndots;
|
||||||
unsigned short udp_port;
|
unsigned short udp_port;
|
||||||
unsigned short tcp_port;
|
unsigned short tcp_port;
|
||||||
|
int socket_send_buffer_size;
|
||||||
|
int socket_receive_buffer_size;
|
||||||
struct in_addr *servers;
|
struct in_addr *servers;
|
||||||
int nservers;
|
int nservers;
|
||||||
char **domains;
|
char **domains;
|
||||||
|
@ -133,6 +133,8 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
|
|||||||
channel->ndots = -1;
|
channel->ndots = -1;
|
||||||
channel->udp_port = -1;
|
channel->udp_port = -1;
|
||||||
channel->tcp_port = -1;
|
channel->tcp_port = -1;
|
||||||
|
channel->socket_send_buffer_size = -1;
|
||||||
|
channel->socket_receive_buffer_size = -1;
|
||||||
channel->nservers = -1;
|
channel->nservers = -1;
|
||||||
channel->ndomains = -1;
|
channel->ndomains = -1;
|
||||||
channel->nsort = -1;
|
channel->nsort = -1;
|
||||||
@ -320,6 +322,12 @@ static int init_by_options(ares_channel channel,
|
|||||||
channel->sock_state_cb = options->sock_state_cb;
|
channel->sock_state_cb = options->sock_state_cb;
|
||||||
channel->sock_state_cb_data = options->sock_state_cb_data;
|
channel->sock_state_cb_data = options->sock_state_cb_data;
|
||||||
}
|
}
|
||||||
|
if ((optmask & ARES_OPT_SOCK_SNDBUF)
|
||||||
|
&& channel->socket_send_buffer_size == -1)
|
||||||
|
channel->socket_send_buffer_size = options->socket_send_buffer_size;
|
||||||
|
if ((optmask & ARES_OPT_SOCK_RCVBUF)
|
||||||
|
&& channel->socket_receive_buffer_size == -1)
|
||||||
|
channel->socket_receive_buffer_size = options->socket_receive_buffer_size;
|
||||||
|
|
||||||
/* Copy the servers, if given. */
|
/* Copy the servers, if given. */
|
||||||
if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
|
if ((optmask & ARES_OPT_SERVERS) && channel->nservers == -1)
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
#ifdef HAVE_SYS_UIO_H
|
#ifdef HAVE_SYS_UIO_H
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <netinet/tcp.h> /* for TCP_NODELAY */
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <arpa/nameser.h>
|
#include <arpa/nameser.h>
|
||||||
@ -732,9 +733,36 @@ static int nonblock(ares_socket_t sockfd, /* operate on this */
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int configure_socket(int s, ares_channel channel)
|
||||||
|
{
|
||||||
|
nonblock(s, TRUE);
|
||||||
|
|
||||||
|
#ifdef FD_CLOEXEC
|
||||||
|
/* Configure the socket fd as close-on-exec. */
|
||||||
|
if (fcntl(s, F_SETFD, FD_CLOEXEC) == -1)
|
||||||
|
return -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Set the socket's send and receive buffer sizes. */
|
||||||
|
if ((channel->socket_send_buffer_size > 0) &&
|
||||||
|
setsockopt(s, SOL_SOCKET, SO_SNDBUF,
|
||||||
|
&channel->socket_send_buffer_size,
|
||||||
|
sizeof(channel->socket_send_buffer_size)) == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if ((channel->socket_receive_buffer_size > 0) &&
|
||||||
|
setsockopt(s, SOL_SOCKET, SO_RCVBUF,
|
||||||
|
&channel->socket_receive_buffer_size,
|
||||||
|
sizeof(channel->socket_receive_buffer_size)) == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int open_tcp_socket(ares_channel channel, struct server_state *server)
|
static int open_tcp_socket(ares_channel channel, struct server_state *server)
|
||||||
{
|
{
|
||||||
ares_socket_t s;
|
ares_socket_t s;
|
||||||
|
int opt;
|
||||||
struct sockaddr_in sockin;
|
struct sockaddr_in sockin;
|
||||||
|
|
||||||
/* Acquire a socket. */
|
/* Acquire a socket. */
|
||||||
@ -742,8 +770,25 @@ static int open_tcp_socket(ares_channel channel, struct server_state *server)
|
|||||||
if (s == ARES_SOCKET_BAD)
|
if (s == ARES_SOCKET_BAD)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Set the socket non-blocking. */
|
/* Configure it. */
|
||||||
nonblock(s, TRUE);
|
if (configure_socket(s, channel) < 0)
|
||||||
|
{
|
||||||
|
close(s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Disable the Nagle algorithm (only relevant for TCP sockets, and thus not in
|
||||||
|
* configure_socket). In general, in DNS lookups we're pretty much interested
|
||||||
|
* in firing off a single request and then waiting for a reply, so batching
|
||||||
|
* isn't very interesting in general.
|
||||||
|
*/
|
||||||
|
opt = 1;
|
||||||
|
if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof(opt)) == -1)
|
||||||
|
{
|
||||||
|
close(s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Connect to the server. */
|
/* Connect to the server. */
|
||||||
memset(&sockin, 0, sizeof(sockin));
|
memset(&sockin, 0, sizeof(sockin));
|
||||||
@ -777,7 +822,11 @@ static int open_udp_socket(ares_channel channel, struct server_state *server)
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* Set the socket non-blocking. */
|
/* Set the socket non-blocking. */
|
||||||
nonblock(s, TRUE);
|
if (configure_socket(s, channel) < 0)
|
||||||
|
{
|
||||||
|
close(s);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/* Connect to the server. */
|
/* Connect to the server. */
|
||||||
memset(&sockin, 0, sizeof(sockin));
|
memset(&sockin, 0, sizeof(sockin));
|
||||||
|
Loading…
Reference in New Issue
Block a user