From: Jelte Jansen Date: Mon, 14 Feb 2005 12:20:14 +0000 (+0000) Subject: added timeouts for sockets (todo: handling of timeout and make users able to set... X-Git-Tag: release-0.50~440 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e8d46da35c35ad7eb0ee00622ef9bd86a8a3503e;p=thirdparty%2Fldns.git added timeouts for sockets (todo: handling of timeout and make users able to set timeout?) lintfixes --- diff --git a/ldns/net.h b/ldns/net.h index ddc12f9e..d8225317 100644 --- a/ldns/net.h +++ b/ldns/net.h @@ -19,6 +19,10 @@ #include +#define LDNS_DEFAULT_TIMEOUT_SEC 5 +#define LDNS_DEFAULT_TIMEOUT_USEC 0 + + /* prototypes */ ldns_pkt * ldns_send_udp(ldns_buffer *, const struct sockaddr_storage *, socklen_t); ldns_pkt * ldns_send_tcp(ldns_buffer *, const struct sockaddr_storage *, socklen_t); diff --git a/net.c b/net.c index d873775b..4d4b3fcc 100644 --- a/net.c +++ b/net.c @@ -126,7 +126,11 @@ ldns_send_udp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to struct timeval tv_s; struct timeval tv_e; - + struct timeval timeout; + + timeout.tv_sec = LDNS_DEFAULT_TIMEOUT_SEC; + timeout.tv_usec = LDNS_DEFAULT_TIMEOUT_USEC; + gettimeofday(&tv_s, NULL); if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP)) == -1) { @@ -134,6 +138,13 @@ ldns_send_udp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to return NULL; } + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, + (socklen_t) sizeof(timeout))) { + perror("setsockopt"); + close(sockfd); + return NULL; + } + bytes = sendto(sockfd, ldns_buffer_begin(qbin), ldns_buffer_position(qbin), 0, (struct sockaddr *)to, tolen); @@ -163,6 +174,9 @@ ldns_send_udp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to close(sockfd); if (bytes == -1) { + if (errno == EAGAIN) { + fprintf(stderr, "socket timeout\n"); + } printf("received too little\n"); FREE(answer); return NULL; @@ -209,6 +223,11 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to struct timeval tv_s; struct timeval tv_e; + struct timeval timeout; + + timeout.tv_sec = LDNS_DEFAULT_TIMEOUT_SEC; + timeout.tv_usec = LDNS_DEFAULT_TIMEOUT_USEC; + gettimeofday(&tv_s, NULL); if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_STREAM, IPPROTO_TCP)) == -1) { @@ -216,6 +235,13 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to return NULL; } + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, + (socklen_t) sizeof(timeout))) { + perror("setsockopt"); + close(sockfd); + return NULL; + } + if (connect(sockfd, (struct sockaddr*)to, tolen) == -1) { close(sockfd); perror("could not bind socket"); @@ -259,6 +285,9 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to while (total_bytes < 2) { bytes = recv(sockfd, answer, MAX_PACKET_SIZE, 0); if (bytes == -1) { + if (errno == EAGAIN) { + fprintf(stderr, "socket timeout\n"); + } perror("error receiving tcp packet"); FREE(answer); return NULL; @@ -271,9 +300,12 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to /* if we did not receive the whole packet in one tcp packet, we must recv() on */ - while (total_bytes < answer_size + 2) { - bytes = recv(sockfd, answer+total_bytes, MAX_PACKET_SIZE-total_bytes, 0); + while (total_bytes < (ssize_t) (answer_size + 2)) { + bytes = recv(sockfd, answer+total_bytes, (size_t) (MAX_PACKET_SIZE-total_bytes), 0); if (bytes == -1) { + if (errno == EAGAIN) { + fprintf(stderr, "socket timeout\n"); + } perror("error receiving tcp packet"); FREE(answer); return NULL; @@ -301,52 +333,3 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to } } -/* - * Read SIZE bytes from the socket into BUF. Keep reading unless an - * error occurs (except for EINTR) or EOF is reached. - */ -static bool -read_socket(int s, void *buf, size_t size) -{ - char *data = buf; - size_t total_count = 0; - - while (total_count < size) { - ssize_t count = read(s, data + total_count, size - - total_count); - if (count == -1) { - if (errno != EINTR) { - return false; - } else { - continue; - } - } - total_count += count; - } - return true; -} - -/* - * Write the complete buffer to the socket, irrespective of short - * writes or interrupts. - */ -static bool -write_socket(int s, const void *buf, size_t size) -{ - const char *data = buf; - size_t total_count = 0; - - while (total_count < size) { - ssize_t count = write(s, data + total_count, size - - total_count); - if (count == -1) { - if (errno != EINTR) { - return false; - } else { - continue; - } - } - total_count += count; - } - return true; -} diff --git a/run-test5.c b/run-test5.c index 13c4b447..d3e4c0ba 100644 --- a/run-test5.c +++ b/run-test5.c @@ -19,7 +19,7 @@ main() printf("test 5\n"); packet = ldns_pkt_query_new_frm_str("www.kanariepiet.com", LDNS_RR_TYPE_A, - LDNS_RR_CLASS_IN, LDNS_AD | LDNS_AA); + LDNS_RR_CLASS_IN, (uint16_t) (LDNS_AD | LDNS_AA)); printf("Packet:\n"); if (packet) diff --git a/run-test8.c b/run-test8.c index 512ef6bb..fff76687 100644 --- a/run-test8.c +++ b/run-test8.c @@ -18,7 +18,7 @@ print_usage(char *file) exit(0); } -void +int main(int argc, char **argv) { ldns_resolver *res; @@ -40,17 +40,17 @@ main(int argc, char **argv) /* init */ res = ldns_resolver_new(); if (!res) - return; + return -1; nameserver = ldns_rdf_new_frm_str(server_ip, LDNS_RDF_TYPE_A); if (!nameserver) { printf("Bad server ip\n"); - return; + return -1; } if (ldns_resolver_push_nameserver(res, nameserver) != LDNS_STATUS_OK) { printf("error push nameserver\n"); - return; + return -1; } /* HACK */ ldns_resolver_set_configured(res, 1); @@ -59,13 +59,15 @@ main(int argc, char **argv) qname = ldns_rdf_new_frm_str(name, LDNS_RDF_TYPE_DNAME); if (!qname) { printf("error making qname\n"); - return; + return -1; } pkt = ldns_resolver_send(res, qname, ldns_rr_get_type_by_name(type), 0, LDNS_RD); if (!pkt) { printf("error pkt sending\n"); - return; + return -1; } ldns_pkt_print(stdout, pkt); + + return 0; } diff --git a/str2host.c b/str2host.c index d79ec204..10966050 100644 --- a/str2host.c +++ b/str2host.c @@ -152,7 +152,6 @@ ldns_str2rdf_dname(ldns_rdf **d, const uint8_t* str) size_t len; size_t octet_len; ldns_status stat; - int i = 0; uint8_t *s,*p,*q; uint8_t buf_str[MAXDOMAINLEN + 1];