]> granicus.if.org Git - libevent/commitdiff
New function to get address for nameserver.
authorNick Mathewson <nickm@torproject.org>
Mon, 2 Feb 2015 18:57:22 +0000 (13:57 -0500)
committerNick Mathewson <nickm@torproject.org>
Mon, 2 Feb 2015 18:57:22 +0000 (13:57 -0500)
evdns.c
include/event2/dns.h
test/regress_dns.c

diff --git a/evdns.c b/evdns.c
index 615cd880b6af71656293488a2d85e844ae0d0f6e..afc8c63c327c96e4d1a504cb17175b657c493fdb 100644 (file)
--- a/evdns.c
+++ b/evdns.c
@@ -2652,6 +2652,34 @@ evdns_base_nameserver_sockaddr_add(struct evdns_base *base,
        return res;
 }
 
+int
+evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
+    struct sockaddr *sa, ev_socklen_t len)
+{
+       int result = -1;
+       int i;
+       struct nameserver *server;
+       EVDNS_LOCK(base);
+       server = base->server_head;
+       for (i = 0; i < idx && server; ++i, server = server->next) {
+               if (server->next == base->server_head)
+                       goto done;
+       }
+       if (! server)
+               goto done;
+
+       if (server->addrlen > len) {
+               result = (int) server->addrlen;
+               goto done;
+       }
+
+       memcpy(sa, &server->address, server->addrlen);
+       result = (int) server->addrlen;
+done:
+       EVDNS_UNLOCK(base);
+       return result;
+}
+
 /* remove from the queue */
 static void
 evdns_request_remove(struct request *req, struct request **head)
index f052af68581d3e482cf0051685969c21771eb9a4..17cd86a2ec2df91f515b056619e67a1ca2fca700 100644 (file)
@@ -694,6 +694,22 @@ struct evdns_getaddrinfo_request *evdns_getaddrinfo(
 EVENT2_EXPORT_SYMBOL
 void evdns_getaddrinfo_cancel(struct evdns_getaddrinfo_request *req);
 
+/**
+   Retrieve the address of the 'idx'th configured nameserver.
+
+   @param base The evdns_base to examine.
+   @param idx The index of the nameserver to get the address of.
+   @param sa A location to receive the server's address.
+   @param len The number of bytes available at sa.
+
+   @return the number of bytes written into sa on success.  On failure, returns
+     -1 if idx is greater than the number of configured nameservers, or a
+     value greater than 'len' if len was not high enough.
+ */
+EVENT2_EXPORT_SYMBOL
+int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
+    struct sockaddr *sa, ev_socklen_t len);
+
 #ifdef __cplusplus
 }
 #endif
index e03c143ca8d9d885846640820fc90db3d886e983..9f4b7e2e865cf0b0cfa32242a81438c328e49eb5 100644 (file)
@@ -406,6 +406,22 @@ dns_server(void)
         * the only nameserver. */
        evdns_base_nameserver_sockaddr_add(base, (struct sockaddr*)&ss, slen, 0);
        tt_int_op(evdns_base_count_nameservers(base), ==, 1);
+       {
+               struct sockaddr_storage ss2;
+               int slen2;
+
+               memset(&ss2, 0, sizeof(ss2));
+
+               slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, 3);
+               tt_int_op(slen2, ==, slen);
+               tt_int_op(ss2.ss_family, ==, 0);
+               slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, sizeof(ss2));
+               tt_int_op(slen2, ==, slen);
+               tt_mem_op(&ss2, ==, &ss, slen);
+
+               slen2 = evdns_base_get_nameserver_addr(base, 1, (struct sockaddr *)&ss2, sizeof(ss2));
+               tt_int_op(-1, ==, slen2);
+       }
 
        /* Send some queries. */
        evdns_base_resolve_ipv4(base, "zz.example.com", DNS_QUERY_NO_SEARCH,