]> granicus.if.org Git - libevent/commitdiff
Allow evdns_base_new to succeed with no nameservers configured
authorDaniel Kempenich <dan.kempenich@gmail.com>
Fri, 27 Jan 2023 07:44:41 +0000 (08:44 +0100)
committerAzat Khuzhin <azat@libevent.org>
Fri, 27 Jan 2023 07:50:29 +0000 (08:50 +0100)
If resolv.conf has no nameservers, evdns_base_new can still succeed with
the default of using the name server from localhost matching the man
page documentation for resolv.conf.

evdns.c
test/regress_dns.c
test/regress_main.c

diff --git a/evdns.c b/evdns.c
index 52e7ee629c869b17f64cd4d37fa96ba0ce28e4ac..05e515c0a3e613b571a4884cd389175accfb3717 100644 (file)
--- a/evdns.c
+++ b/evdns.c
@@ -4892,7 +4892,7 @@ evdns_base_new(struct event_base *event_base, int flags)
 #else
                r = evdns_base_resolv_conf_parse(base, opts, "/etc/resolv.conf");
 #endif
-               if (r) {
+               if (r && (EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED != r)) {
                        evdns_base_free_and_unlock(base, 0);
                        return NULL;
                }
index 4e247b343533db5eca9c1a0446249c9d06794e05..e28ad52bb43e3c65bf009721f584c374e2be009a 100644 (file)
@@ -42,6 +42,9 @@
 #include <sys/queue.h>
 #ifndef _WIN32
 #include <sys/socket.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <signal.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -1218,6 +1221,39 @@ end:
        if (dns)
                evdns_base_free(dns, 0);
 }
+
+static void
+dns_nameservers_no_nameservers_configured_test(void *arg)
+{
+       struct basic_test_data *data = arg;
+       struct event_base *base = data->base;
+       struct evdns_base *dns = NULL;
+       int fd = -1;
+       char *tmpfilename = NULL;
+       const char filecontents[] = "# tmp empty resolv.conf\n";
+       const size_t filecontentssize = sizeof(filecontents);
+       int ok;
+       
+       fd = regress_make_tmpfile(filecontents, filecontentssize, &tmpfilename);
+       if (fd < 0)
+               tt_skip();
+
+       dns = evdns_base_new(base, 0);
+       tt_assert(dns);
+
+       ok = evdns_base_resolv_conf_parse(dns, DNS_OPTIONS_ALL, tmpfilename);
+       tt_int_op(ok, ==, EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED);
+
+end:
+       if (fd != -1)
+               close(fd);
+       if (dns)
+               evdns_base_free(dns, 0);
+       if (tmpfilename) {
+               unlink(tmpfilename);
+               free(tmpfilename);
+       }
+}
 #endif
 
 /* === Test for bufferevent_socket_connect_hostname */
@@ -3007,6 +3043,8 @@ struct testcase_t dns_testcases[] = {
 #ifndef _WIN32
        { "nameservers_no_default", dns_nameservers_no_default_test,
          TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
+       { "no_nameservers_configured", dns_nameservers_no_nameservers_configured_test,
+         TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
 #endif
 
        { "getaddrinfo_async", test_getaddrinfo_async,
index 489c74b733bd0ac9b05201506661175e8321b298..a504d6ce93a1d4000799516b29aff09cc361a862 100644 (file)
@@ -144,11 +144,11 @@ regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
                return (-1);
        if (write(fd, data, datalen) != (int)datalen) {
                close(fd);
+               unlink(tmpfilename);
                return (-1);
        }
        lseek(fd, 0, SEEK_SET);
-       /* remove it from the file system */
-       unlink(tmpfilename);
+       *filename_out = strdup(tmpfilename);
        return (fd);
 #else
        /* XXXX actually delete the file later */