]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
added timeouts for sockets (todo: handling of timeout and make users able to set...
authorJelte Jansen <jeltejan@NLnetLabs.nl>
Mon, 14 Feb 2005 12:20:14 +0000 (12:20 +0000)
committerJelte Jansen <jeltejan@NLnetLabs.nl>
Mon, 14 Feb 2005 12:20:14 +0000 (12:20 +0000)
lintfixes

ldns/net.h
net.c
run-test5.c
run-test8.c
str2host.c

index ddc12f9ef9a4e1e8b34bd4fed25d69e7fbf47ad8..d82253175dcae5c0d78f08fb204fbb8b20182d34 100644 (file)
 
 #include <sys/socket.h>
 
+#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 d873775b5a36f3d77557c0a78edf949586739f07..4d4b3fcc35c16d038568fc49cc9ba3d670d2896a 100644 (file)
--- 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;
-}
index 13c4b447dea47b24c4a682bcb010c7e21a89bea0..d3e4c0ba3ae51d4ae46b5926a13d2440a8b2528d 100644 (file)
@@ -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) 
index 512ef6bbc35c2a5a367ecc69b5223d135c3e03d3..fff76687a5cf514e1f595a8cfa6f66042fb2927f 100644 (file)
@@ -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;
 }
index d79ec2046eeda61f0cafed112f087750356694cb..10966050af372be439636b34861bf6968f7c9c14 100644 (file)
@@ -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];