From: Miek Gieben Date: Wed, 10 Aug 2005 11:42:59 +0000 (+0000) Subject: add udp counterpart for tcp X-Git-Tag: release-1.0.0~325 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f94e9f8b0ab70ca8ca594cd393f05aba0b0fd12c;p=thirdparty%2Fldns.git add udp counterpart for tcp --- diff --git a/ldns/net.h b/ldns/net.h index 4741571f..2b9d1a6c 100644 --- a/ldns/net.h +++ b/ldns/net.h @@ -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 b6d4a2e9..f975dc3c 100644 --- 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; } +