]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
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 975f9d4825386239b385a093e3dd6540291c462c..57b7a285269cec4c87336cbdd27a9d3fd114d096 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 4de00bf0e0df137a2812f1c5e62674e220d16f55..facdfed84d8fb9013611f9dbeb814adf0ae6aca1 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 4634b55f74b20302b9444d05fb4f1c1c22d75da3..7cbddef31de2ff1f8a2fa9a4c4a3fb3773507d43 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);