]> granicus.if.org Git - libevent/commitdiff
feat: add `evdns_base_get_nameserver_fd` method
authorYongsheng Xu <chuxdesign@hotmail.com>
Fri, 10 Dec 2021 03:05:13 +0000 (11:05 +0800)
committerYongsheng Xu <chuxdesign@hotmail.com>
Fri, 10 Dec 2021 03:05:13 +0000 (11:05 +0800)
To get underlying udp socket fd.

evdns.c
include/event2/dns.h
test/regress_dns.c

diff --git a/evdns.c b/evdns.c
index 7e54f241897436a0d860d41201c3e998dc13369b..ee06fdda68bdc18d1f1f06c725f0b7adf4ec23e7 100644 (file)
--- a/evdns.c
+++ b/evdns.c
@@ -3435,6 +3435,27 @@ done:
        return result;
 }
 
+int
+evdns_base_get_nameserver_fd(struct evdns_base *base, int idx)
+{
+       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;
+       result = server->socket;
+done:
+       EVDNS_UNLOCK(base);
+       return result;
+}
+
+
 /* remove from the queue */
 static void
 evdns_request_remove(struct request *req, struct request **head)
index 4a9f42f4137d6db105dd3d33065d7c5e4b9c14c4..be61e7838fea0043a6bae00d025dda9b6555cdf3 100644 (file)
@@ -800,6 +800,18 @@ EVENT2_EXPORT_SYMBOL
 int evdns_base_get_nameserver_addr(struct evdns_base *base, int idx,
     struct sockaddr *sa, ev_socklen_t len);
 
+/**
+   Retrieve the fd 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.
+
+   @return the fd value.  On failure, returns
+     -1 if idx is greater than the number of configured nameservers
+ */
+EVENT2_EXPORT_SYMBOL
+int evdns_base_get_nameserver_fd(struct evdns_base *base, int idx);
+
 #ifdef __cplusplus
 }
 #endif
index fbce619a0dcfafea1fdaac0de4bcb587df359e85..2c4973aedf9260078e9406744877e93f89cf49a8 100644 (file)
@@ -1190,6 +1190,7 @@ dns_nameservers_no_default_test(void *arg)
        dns = evdns_base_new(base, 0);
        tt_assert(dns);
        tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, -1);
+       tt_int_op(evdns_base_get_nameserver_fd(dns, 0), ==, -1);
 
        /* We cannot test
         * EVDNS_BASE_INITIALIZE_NAMESERVERS|EVDNS_BASE_NAMESERVERS_NO_DEFAULT
@@ -1198,9 +1199,11 @@ dns_nameservers_no_default_test(void *arg)
        evdns_base_resolv_conf_parse(dns,
                DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT, RESOLV_FILE);
        tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, -1);
+       tt_int_op(evdns_base_get_nameserver_fd(dns, 0), ==, -1);
 
        evdns_base_resolv_conf_parse(dns, DNS_OPTIONS_ALL, RESOLV_FILE);
        tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, sizeof(struct sockaddr));
+       tt_int_op(evdns_base_get_nameserver_fd(dns, 0), !=, -1);
 
 end:
        if (dns)