MEDIUM: resolvers: create a "default" resolvers section at startup
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.
diff --git a/include/haproxy/resolvers.h b/include/haproxy/resolvers.h
index 975f9d4..57b7a28 100644
--- a/include/haproxy/resolvers.h
+++ b/include/haproxy/resolvers.h
@@ -61,5 +61,6 @@
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
diff --git a/src/http_client.c b/src/http_client.c
index 4de00bf..facdfed 100644
--- a/src/http_client.c
+++ b/src/http_client.c
@@ -1074,6 +1074,9 @@
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);
diff --git a/src/resolvers.c b/src/resolvers.c
index 4634b55..7cbddef 100644
--- a/src/resolvers.c
+++ b/src/resolvers.c
@@ -3628,6 +3628,25 @@
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 @@
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);