]> granicus.if.org Git - libevent/commitdiff
Replace magic numbers with consts for evdns_base_resolv_conf_parse() errors
authorDaniel Kempenich <dan.kempenich@gmail.com>
Wed, 18 Jan 2023 05:02:56 +0000 (23:02 -0600)
committerAzat Khuzhin <azat@libevent.org>
Fri, 27 Jan 2023 07:47:01 +0000 (08:47 +0100)
evdns.c
include/event2/dns.h
test/regress_dns.c

diff --git a/evdns.c b/evdns.c
index cd8e038a9e616bf9d6d32c7859ac1c661b57715f..52e7ee629c869b17f64cd4d37fa96ba0ce28e4ac 100644 (file)
--- a/evdns.c
+++ b/evdns.c
@@ -4471,12 +4471,13 @@ resolv_conf_parse_line(struct evdns_base *base, char *const start, int flags) {
 
 /* exported function */
 /* returns: */
-/*   0 no errors */
-/*   1 failed to open file */
-/*   2 failed to stat file */
-/*   3 file too large */
-/*   4 out of memory */
-/*   5 short read from file */
+/*   EVDNS_ERROR_NONE (0) no errors */
+/*   EVDNS_ERROR_FAILED_TO_OPEN_FILE (1) failed to open file */
+/*   EVDNS_ERROR_FAILED_TO_STAT_FILE (2) failed to stat file */
+/*   EVDNS_ERROR_FILE_TOO_LARGE (3) file too large */
+/*   EVDNS_ERROR_OUT_OF_MEMORY (4) out of memory */
+/*   EVDNS_ERROR_SHORT_READ_FROM_FILE (5) short read from file */
+/*   EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED (6) no nameservers configured */
 int
 evdns_base_resolv_conf_parse(struct evdns_base *base, int flags, const char *const filename) {
        int res;
@@ -4516,7 +4517,7 @@ evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char
        size_t n;
        char *resolv;
        char *start;
-       int err = 0;
+       int err = EVDNS_ERROR_NONE;
        int add_default;
 
        log(EVDNS_LOG_DEBUG, "Parsing resolv.conf file %s", filename);
@@ -4534,16 +4535,16 @@ evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char
 
        if (!filename) {
                evdns_resolv_set_defaults(base, flags);
-               return 1;
+               return EVDNS_ERROR_FAILED_TO_OPEN_FILE;
        }
 
        if ((err = evutil_read_file_(filename, &resolv, &n, 0)) < 0) {
                if (err == -1) {
                        /* No file. */
                        evdns_resolv_set_defaults(base, flags);
-                       return 1;
+                       return EVDNS_ERROR_FAILED_TO_OPEN_FILE;
                } else {
-                       return 2;
+                       return EVDNS_ERROR_FAILED_TO_STAT_FILE;
                }
        }
 
@@ -4563,7 +4564,7 @@ evdns_base_resolv_conf_parse_impl(struct evdns_base *base, int flags, const char
        if (!base->server_head && add_default) {
                /* no nameservers were configured. */
                evdns_base_nameserver_ip_add(base, "127.0.0.1");
-               err = 6;
+               err = EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED;
        }
        if (flags & DNS_OPTION_SEARCH && (!base->global_search_state || base->global_search_state->num_domains == 0)) {
                search_set_from_hostname(base);
index be61e7838fea0043a6bae00d025dda9b6555cdf3..17a00cff2aec3308c77518f2dca282155e91e172 100644 (file)
@@ -261,6 +261,21 @@ struct event_base;
  * @see DNS_OPTION_NAMESERVERS_NO_DEFAULT */
 #define EVDNS_BASE_NAMESERVERS_NO_DEFAULT 0x10000
 
+/* No errors */
+#define EVDNS_ERROR_NONE 0
+/* Failed to open file */
+#define EVDNS_ERROR_FAILED_TO_OPEN_FILE 1
+/* Failed to stat file */
+#define EVDNS_ERROR_FAILED_TO_STAT_FILE 2
+/* File too large */
+#define EVDNS_ERROR_FILE_TOO_LARGE 3
+/* Out of memory */
+#define EVDNS_ERROR_OUT_OF_MEMORY 4
+/* Short read from file */
+#define EVDNS_ERROR_SHORT_READ_FROM_FILE 5
+/* No nameservers configured */
+#define EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED 6
+
 /**
   Initialize the asynchronous DNS library.
 
@@ -511,9 +526,13 @@ int evdns_base_set_option(struct evdns_base *base, const char *option, const cha
   The following directives are not parsed from the file: sortlist, rotate,
   no-check-names, inet6, debug.
 
-  If this function encounters an error, the possible return values are: 1 =
-  failed to open file, 2 = failed to stat file, 3 = file too large, 4 = out of
-  memory, 5 = short read from file, 6 = no nameservers listed in the file
+  If this function encounters an error, the possible return values are:
+   EVDNS_ERROR_FAILED_TO_OPEN_FILE (1) - failed to open file
+   EVDNS_ERROR_FAILED_TO_STAT_FILE (2) - failed to stat file
+   EVDNS_ERROR_FILE_TOO_LARGE (3) - file too large
+   EVDNS_ERROR_OUT_OF_MEMORY (4) - out of memory
+   EVDNS_ERROR_SHORT_READ_FROM_FILE (5) - short read from file
+   EVDNS_ERROR_NO_NAMESERVERS_CONFIGURED (6) - no nameservers configured.
 
   @param base the evdns_base to which to apply this operation
   @param flags any of DNS_OPTION_NAMESERVERS|DNS_OPTION_SEARCH|DNS_OPTION_MISC|
index 2f251c72786a76120af5e57678d14a4a674e8d7c..4e247b343533db5eca9c1a0446249c9d06794e05 100644 (file)
@@ -1203,12 +1203,14 @@ dns_nameservers_no_default_test(void *arg)
         * EVDNS_BASE_INITIALIZE_NAMESERVERS|EVDNS_BASE_NAMESERVERS_NO_DEFAULT
         * because we cannot mock "/etc/resolv.conf" (yet). */
 
-       evdns_base_resolv_conf_parse(dns,
+       ok = evdns_base_resolv_conf_parse(dns,
                DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT, RESOLV_FILE);
+       tt_int_op(ok, ==, EVDNS_ERROR_FAILED_TO_OPEN_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);
+       ok = evdns_base_resolv_conf_parse(dns, DNS_OPTIONS_ALL, RESOLV_FILE);
+       tt_int_op(ok, ==, EVDNS_ERROR_FAILED_TO_OPEN_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);