Hi

Please find the patch attached. It is related to my comment
https://github.com/haproxy/haproxy/issues/508#issuecomment-2812291338.

Best regards

-- 
Tim Düsterhus
Senior Software Engineer
Tideways GmbH
Königswinterer Str. 116
53227 Bonn
https://tideways.com/imprint
Sitz der Gesellschaft: Bonn
Geschäftsführer: Benjamin Außenhofer (geb. Eberlei)
Registergericht: Amtsgericht Bonn, HRB 22127
From 38b3a437ce2df5380dfc16b84082acb119bb68c5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= <t...@tideways-gmbh.com>
Date: Thu, 17 Apr 2025 12:15:50 +0200
Subject: [PATCH] MINOR: server: Use an explicitly given address family as the
 default value for `resolve-prefer`

For a configuration:

    server-template example 5 i...@example.com:443 resolvers system

the server's IP addresses would previously be initialized to IPv4 addresses, as
requested by the `ipv4@` prefix. However since the builtin default value of
`resolve-prefer` is `ipv6`, the runtime resolver would immediately update the
IP addresses to the IPv6 addresses, violating the users expectations:

    [WARNING]  (158574) : bk_example/example1: IP changed from '96.7.128.198' to '2600:1406:bc00:53::b81e:94ce' by 'system/127.0.0.53'.
    [WARNING]  (158574) : bk_example/example2: IP changed from '96.7.128.175' to '2600:1408:ec00:36::1736:7f31' by 'DNS cache'.
    [WARNING]  (158574) : bk_example/example3: IP changed from '96.7.128.175' to '2600:1406:bc00:53::b81e:94c8' by 'DNS cache'.
    [WARNING]  (158574) : bk_example/example4: IP changed from '23.192.228.80' to '2600:1406:3a00:21::173e:2e65' by 'DNS cache'.
    [WARNING]  (158574) : bk_example/example5: IP changed from '23.215.0.136' to '2600:1408:ec00:36::1736:7f24' by 'DNS cache'.

Improve this by using the server's explicitly specified address family as the
default value for `resolve-prefer` instead of the value specified in the
`default-server` option (or the builtin default of IPv6). If for some reason it
is desired to initialize servers with IPv4 addresses and dynamically resolve
them to IPv6 addresses, the `resolve-prefer` option can still be given on the
`server` line itself and will work as expected.

This patch should be safe for backporting to the recent stable branches.

See GitHub Issue #508
---
 src/server.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/server.c b/src/server.c
index a94fef190..56b7c106d 100644
--- a/src/server.c
+++ b/src/server.c
@@ -2914,7 +2914,20 @@ void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl
 
 	if (src->resolvers_id != NULL)
 		srv->resolvers_id = strdup(src->resolvers_id);
-	srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
+
+	switch (srv->addr.ss_family) {
+	case AF_INET:
+	case AF_INET6:
+		/* Use an explicitly specified address family as the server's
+		 * default value for the "resolve-prefer" setting.
+		 */
+		srv->resolv_opts.family_prio = srv->addr.ss_family;
+		break;
+	default:
+		/* And use the "default-server" settings otherwise. */
+		srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
+	}
+
 	srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
 	srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
 	if (srv->resolv_opts.family_prio == AF_UNSPEC)
-- 
2.43.0

Reply via email to