select on FD > 1024 get this patch to ldns ppl

This commit is contained in:
Jeff Lenk 2011-03-25 11:31:12 -05:00
parent 579927b53a
commit 710fc7a775
3 changed files with 67 additions and 75 deletions

View File

@ -148,6 +148,16 @@ extern ldns_lookup_table ldns_opcodes[];
/** EDNS flags */ /** EDNS flags */
extern ldns_lookup_table ldns_edns_flags[]; extern ldns_lookup_table ldns_edns_flags[];
#ifdef USE_WINSOCK
#define SOCK_INVALID INVALID_SOCKET
#define close_socket(_s) if (_s > SOCK_INVALID) {closesocket(_s); _s = SOCK_INVALID;}
#else
#define SOCK_INVALID -1
#define close_socket(_s) if (_s > SOCK_INVALID) {close(_s); _s = SOCK_INVALID;}
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -261,12 +261,18 @@ ldns_sock_block(int sockfd)
#endif #endif
} }
#ifndef WIN32
#include <poll.h>
#endif
/** wait for a socket to become ready */ /** wait for a socket to become ready */
static int static int
ldns_sock_wait(int sockfd, struct timeval timeout, int write) ldns_sock_wait(int sockfd, struct timeval timeout, int write)
{ {
fd_set fds;
int ret; int ret;
#ifdef WIN32
fd_set fds;
#ifndef S_SPLINT_S #ifndef S_SPLINT_S
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(FD_SET_T sockfd, &fds); FD_SET(FD_SET_T sockfd, &fds);
@ -275,6 +281,26 @@ ldns_sock_wait(int sockfd, struct timeval timeout, int write)
ret = select(sockfd+1, NULL, &fds, NULL, &timeout); ret = select(sockfd+1, NULL, &fds, NULL, &timeout);
else else
ret = select(sockfd+1, &fds, NULL, NULL, &timeout); ret = select(sockfd+1, &fds, NULL, NULL, &timeout);
#else
struct pollfd pfds[2];
int x = 0;
memset(&pfds[0], 0, sizeof(pfds[0]) * 2);
pfds[0].fd = sockfd;
pfds[0].events = POLLIN|POLLERR;
if (write) {
pfds[0].events |= POLLOUT;
}
ret = poll(pfds, 1, timeout.tv_sec * 1000 + timeout.tv_usec / 1000);
#endif
if(ret == 0) if(ret == 0)
/* timeout expired */ /* timeout expired */
return 0; return 0;
@ -299,11 +325,7 @@ ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage
/* wait for an response*/ /* wait for an response*/
if(!ldns_sock_wait(sockfd, timeout, 0)) { if(!ldns_sock_wait(sockfd, timeout, 0)) {
#ifndef USE_WINSOCK close_socket(sockfd);
close(sockfd);
#else
closesocket(sockfd);
#endif
return LDNS_STATUS_NETWORK_ERR; return LDNS_STATUS_NETWORK_ERR;
} }
@ -313,11 +335,7 @@ ldns_udp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage
ldns_sock_nonblock(sockfd); ldns_sock_nonblock(sockfd);
answer = ldns_udp_read_wire(sockfd, answer_size, NULL, NULL); answer = ldns_udp_read_wire(sockfd, answer_size, NULL, NULL);
#ifndef USE_WINSOCK close_socket(sockfd);
close(sockfd);
#else
closesocket(sockfd);
#endif
if (*answer_size == 0) { if (*answer_size == 0) {
/* oops */ /* oops */
@ -353,9 +371,10 @@ ldns_udp_connect(const struct sockaddr_storage *to, struct timeval ATTR_UNUSED(t
if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM, if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM,
IPPROTO_UDP)) IPPROTO_UDP))
== -1) { == SOCK_INVALID) {
return 0; return 0;
} }
return sockfd; return sockfd;
} }
@ -366,20 +385,20 @@ ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen,
int sockfd; int sockfd;
if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM, if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM,
IPPROTO_TCP)) == -1) { IPPROTO_TCP)) == SOCK_INVALID) {
return 0; return 0;
} }
/* perform nonblocking connect, to be able to wait with select() */ /* perform nonblocking connect, to be able to wait with select() */
ldns_sock_nonblock(sockfd); ldns_sock_nonblock(sockfd);
if (connect(sockfd, (struct sockaddr*)to, tolen) == -1) { if (connect(sockfd, (struct sockaddr*)to, tolen) == SOCK_INVALID) {
#ifndef USE_WINSOCK #ifndef USE_WINSOCK
#ifdef EINPROGRESS #ifdef EINPROGRESS
if(errno != EINPROGRESS) { if(errno != EINPROGRESS) {
#else #else
if(1) { if(1) {
#endif #endif
close(sockfd); close_socket(sockfd);
return 0; return 0;
} }
#else /* USE_WINSOCK */ #else /* USE_WINSOCK */
@ -398,11 +417,7 @@ ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen,
socklen_t len = (socklen_t)sizeof(error); socklen_t len = (socklen_t)sizeof(error);
if(!ldns_sock_wait(sockfd, timeout, 1)) { if(!ldns_sock_wait(sockfd, timeout, 1)) {
#ifndef USE_WINSOCK close_socket(sockfd);
close(sockfd);
#else
closesocket(sockfd);
#endif
return 0; return 0;
} }
@ -421,7 +436,7 @@ ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen,
continue; /* try again */ continue; /* try again */
#endif #endif
else if(error != 0) { else if(error != 0) {
close(sockfd); close_socket(sockfd);
/* error in errno for our user */ /* error in errno for our user */
errno = error; errno = error;
return 0; return 0;
@ -645,11 +660,7 @@ ldns_tcp_send(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storag
} }
answer = ldns_tcp_read_wire_timeout(sockfd, answer_size, timeout); answer = ldns_tcp_read_wire_timeout(sockfd, answer_size, timeout);
#ifndef USE_WINSOCK close_socket(sockfd);
close(sockfd);
#else
closesocket(sockfd);
#endif
if (*answer_size == 0) { if (*answer_size == 0) {
/* oops */ /* oops */
@ -782,7 +793,7 @@ ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class)
* @hostname is used */ * @hostname is used */
for (ns_i = 0; for (ns_i = 0;
ns_i < ldns_resolver_nameserver_count(resolver) && ns_i < ldns_resolver_nameserver_count(resolver) &&
resolver->_socket == 0; resolver->_socket == SOCK_INVALID;
ns_i++) { ns_i++) {
ns = ldns_rdf2native_sockaddr_storage( ns = ldns_rdf2native_sockaddr_storage(
resolver->_nameservers[ns_i], resolver->_nameservers[ns_i],
@ -792,7 +803,7 @@ ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class)
ldns_resolver_timeout(resolver)); ldns_resolver_timeout(resolver));
} }
if (resolver->_socket == 0) { if (resolver->_socket == SOCK_INVALID) {
ldns_pkt_free(query); ldns_pkt_free(query);
LDNS_FREE(ns); LDNS_FREE(ns);
return LDNS_STATUS_NETWORK_ERR; return LDNS_STATUS_NETWORK_ERR;
@ -807,13 +818,7 @@ ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class)
if (status != LDNS_STATUS_OK) { if (status != LDNS_STATUS_OK) {
/* RoRi: to prevent problems on subsequent calls to ldns_axfr_start /* RoRi: to prevent problems on subsequent calls to ldns_axfr_start
we have to close the socket here! */ we have to close the socket here! */
#ifndef USE_WINSOCK close_socket(resolver->_socket);
close(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
return LDNS_STATUS_CRYPTO_TSIG_ERR; return LDNS_STATUS_CRYPTO_TSIG_ERR;
} }
} }
@ -826,12 +831,8 @@ ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class)
if(!query_wire) { if(!query_wire) {
ldns_pkt_free(query); ldns_pkt_free(query);
LDNS_FREE(ns); LDNS_FREE(ns);
#ifndef USE_WINSOCK
close(resolver->_socket); close_socket(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
return LDNS_STATUS_MEM_ERR; return LDNS_STATUS_MEM_ERR;
} }
@ -843,12 +844,8 @@ ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class)
/* RoRi: to prevent problems on subsequent calls to ldns_axfr_start /* RoRi: to prevent problems on subsequent calls to ldns_axfr_start
we have to close the socket here! */ we have to close the socket here! */
#ifndef USE_WINSOCK
close(resolver->_socket); close_socket(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
return status; return status;
} }
@ -862,12 +859,8 @@ ldns_axfr_start(ldns_resolver *resolver, ldns_rdf *domain, ldns_rr_class class)
/* RoRi: to prevent problems on subsequent calls to ldns_axfr_start /* RoRi: to prevent problems on subsequent calls to ldns_axfr_start
we have to close the socket here! */ we have to close the socket here! */
#ifndef USE_WINSOCK
close(resolver->_socket); close_socket(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
return LDNS_STATUS_NETWORK_ERR; return LDNS_STATUS_NETWORK_ERR;
} }

View File

@ -627,9 +627,7 @@ ldns_resolver_new(void)
r->_timeout.tv_sec = LDNS_DEFAULT_TIMEOUT_SEC; r->_timeout.tv_sec = LDNS_DEFAULT_TIMEOUT_SEC;
r->_timeout.tv_usec = LDNS_DEFAULT_TIMEOUT_USEC; r->_timeout.tv_usec = LDNS_DEFAULT_TIMEOUT_USEC;
/* TODO: fd=0 is actually a valid socket (stdin), r->_socket = -1;
replace with -1 */
r->_socket = 0;
r->_axfr_soa_count = 0; r->_axfr_soa_count = 0;
r->_axfr_i = 0; r->_axfr_i = 0;
r->_cur_axfr_pkt = NULL; r->_cur_axfr_pkt = NULL;
@ -903,6 +901,8 @@ ldns_resolver_deep_free(ldns_resolver *res)
size_t i; size_t i;
if (res) { if (res) {
close_socket(res->_socket);
if (res->_searchlist) { if (res->_searchlist) {
for (i = 0; i < ldns_resolver_searchlist_count(res); i++) { for (i = 0; i < ldns_resolver_searchlist_count(res); i++) {
ldns_rdf_deep_free(res->_searchlist[i]); ldns_rdf_deep_free(res->_searchlist[i]);
@ -1198,7 +1198,7 @@ ldns_axfr_next(ldns_resolver *resolver)
ldns_status status; ldns_status status;
/* check if start() has been called */ /* check if start() has been called */
if (!resolver || resolver->_socket == 0) { if (!resolver || resolver->_socket == -1) {
return NULL; return NULL;
} }
@ -1215,12 +1215,9 @@ ldns_axfr_next(ldns_resolver *resolver)
if (ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_SOA) { if (ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_SOA) {
resolver->_axfr_soa_count++; resolver->_axfr_soa_count++;
if (resolver->_axfr_soa_count >= 2) { if (resolver->_axfr_soa_count >= 2) {
#ifndef USE_WINSOCK
close(resolver->_socket); close_socket(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
ldns_pkt_free(resolver->_cur_axfr_pkt); ldns_pkt_free(resolver->_cur_axfr_pkt);
resolver->_cur_axfr_pkt = NULL; resolver->_cur_axfr_pkt = NULL;
} }
@ -1243,12 +1240,8 @@ ldns_axfr_next(ldns_resolver *resolver)
/* RoRi: we must now also close the socket, otherwise subsequent uses of the /* RoRi: we must now also close the socket, otherwise subsequent uses of the
same resolver structure will fail because the link is still open or same resolver structure will fail because the link is still open or
in an undefined state */ in an undefined state */
#ifndef USE_WINSOCK
close(resolver->_socket); close_socket(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
return NULL; return NULL;
} else if (ldns_pkt_get_rcode(resolver->_cur_axfr_pkt) != 0) { } else if (ldns_pkt_get_rcode(resolver->_cur_axfr_pkt) != 0) {
@ -1258,12 +1251,8 @@ ldns_axfr_next(ldns_resolver *resolver)
/* RoRi: we must now also close the socket, otherwise subsequent uses of the /* RoRi: we must now also close the socket, otherwise subsequent uses of the
same resolver structure will fail because the link is still open or same resolver structure will fail because the link is still open or
in an undefined state */ in an undefined state */
#ifndef USE_WINSOCK
close(resolver->_socket); close_socket(resolver->_socket);
#else
closesocket(resolver->_socket);
#endif
resolver->_socket = 0;
return NULL; return NULL;
} else { } else {