]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
add udp counterpart for tcp
authorMiek Gieben <miekg@NLnetLabs.nl>
Wed, 10 Aug 2005 11:42:59 +0000 (11:42 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Wed, 10 Aug 2005 11:42:59 +0000 (11:42 +0000)
ldns/net.h
net.c

index 4741571faf08ba82d1b01ffb5781639784c37d03..2b9d1a6c3edd521a37708e6d21bf98747c572292 100644 (file)
@@ -60,9 +60,21 @@ ldns_status ldns_send(ldns_pkt **pkt, ldns_resolver *r, ldns_pkt *query_pkt);
 
 /**
  * Create a tcp socket to the specified address
+ * \param[in] to ip and family
+ * \param[in] tolen length of to
+ * \param[in] timeout timeout for the socket
+ * \return a socket descriptor
  */
 int ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout);
 
+/**
+ * Create a udp socket to the specified address
+ * \param[in] to ip and family
+ * \param[in] timeout timeout for the socket
+ * \return a socket descriptor
+ */
+int ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout);
+
 /**
  * send a query via tcp to a server. Don;t want for the answer
  *
diff --git a/net.c b/net.c
index b6d4a2e92e83f1db60050a36cf639dbc5b83d374..f975dc3cc58042ae6051ae379ce6b894d9fd1212 100644 (file)
--- a/net.c
+++ b/net.c
@@ -260,6 +260,24 @@ ldns_send_udp(uint8_t **result, ldns_buffer *qbin, const struct sockaddr_storage
        return LDNS_STATUS_OK;
 }
 
+int
+ldns_udp_connect(const struct sockaddr_storage *to, struct timeval timeout)
+{
+       int sockfd;
+       
+       if ((sockfd = socket((int)((struct sockaddr*)to)->sa_family, SOCK_DGRAM, IPPROTO_UDP)) 
+                       == -1) {
+                return 0;
+        }
+       if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
+                               (socklen_t) sizeof(timeout))) {
+               perror("setsockopt");
+               close(sockfd);
+               return 0;
+        }
+       return sockfd;
+}
+
 int
 ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct timeval timeout)
 {
@@ -282,7 +300,6 @@ ldns_tcp_connect(const struct sockaddr_storage *to, socklen_t tolen, struct time
                perror("could not bind socket");
                return 0;
        }
-
        return sockfd;
 }
 
@@ -435,3 +452,4 @@ ldns_send_tcp(ldns_buffer *qbin, const struct sockaddr_storage *to, socklen_t to
        
        return answer;
 }
+