MEDIUM: resolvers: create a "default" resolvers section at startup
authorWilliam Lallemand <wlallemand@haproxy.org>
Thu, 5 May 2022 17:02:59 +0000 (19:02 +0200)
committerWilliam Lallemand <wlallemand@haproxy.org>
Fri, 6 May 2022 15:02:15 +0000 (17:02 +0200)
Try to create a "default" resolvers section at startup, but does not
display any error nor warning. This section is initialized using the
/etc/resolv.conf of the system.

This is opportunistic and with no guarantee that it will work (but it should
on most systems).

This is useful for the httpclient as it allows to use the DNS resolver
without any configuration in most of the cases.

The function is called from the httpclient_pre_check() function to
ensure than we tried to create the section before trying to initiate the
httpclient. But it is also called from the resolvers.c to ensure the
section is created when the httpclient init was disabled.

include/haproxy/resolvers.h
src/http_client.c
src/resolvers.c

index 975f9d4..57b7a28 100644 (file)
@@ -61,5 +61,6 @@ int stats_dump_resolvers(struct conn_stream *cs,
 void resolv_stats_clear_counters(int clrall, struct list *stat_modules);
 int resolv_allocate_counters(struct list *stat_modules);
 int dns_dgram_init(struct dns_nameserver *ns, struct sockaddr_storage *sk);
+int resolvers_create_default();
 
 #endif // _HAPROXY_RESOLVER_H
index 4de00bf..facdfed 100644 (file)
@@ -1074,6 +1074,9 @@ static int httpclient_resolve_init()
        memprintf(&do_resolve, "do-resolve(txn.hc_ip,%s%s%s)", resolvers_id, resolvers_prefer ? "," : "", resolvers_prefer ? resolvers_prefer : "");
        http_rules[1][0] = do_resolve;
 
+       /* Try to create the default resolvers section */
+       resolvers_create_default();
+
        /* if the resolver does not exist and no hard_error was set, simply ignore resolving */
        if (!find_resolvers_by_id(resolvers_id) && !hard_error_resolvers) {
                free(do_resolve);
index 4634b55..7cbddef 100644 (file)
@@ -3628,6 +3628,25 @@ out:
        free(warnmsg);
        return err_code;
 }
+
+/* try to create a "default" resolvers section which uses "/etc/resolv.conf"
+ *
+ * This function is opportunistic and does not try to display errors or warnings.
+ */
+int resolvers_create_default()
+{
+       int err_code = 0;
+
+       if (find_resolvers_by_id("default"))
+               return 0;
+
+       err_code |= resolvers_new(&curr_resolvers, "default", "<internal>", 0);
+       if (!(err_code & ERR_CODE))
+               err_code |= parse_resolve_conf(NULL, NULL);
+
+       return 0;
+}
+
 int cfg_post_parse_resolvers()
 {
        int err_code = 0;
@@ -3658,3 +3677,4 @@ int cfg_post_parse_resolvers()
 REGISTER_CONFIG_SECTION("resolvers",      cfg_parse_resolvers, cfg_post_parse_resolvers);
 REGISTER_POST_DEINIT(resolvers_deinit);
 REGISTER_CONFIG_POSTPARSER("dns runtime resolver", resolvers_finalize_config);
+REGISTER_PRE_CHECK(resolvers_create_default);