From 09d2780137252896e5ba347ae621cf8134c66b88 Mon Sep 17 00:00:00 2001
From: Jacob Champion <champion.p@gmail.com>
Date: Sun, 17 May 2026 22:04:40 -0700
Subject: [PATCH 1/3] WIP: Switch to getaddrinfo_a where available

...in synchronous mode only. In other words, this uses the new
async-capable API but without any asynchronous functionality.

TODO:
- autoconf support
---
 meson.build     |  3 +++
 src/common/ip.c | 26 ++++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/meson.build b/meson.build
index d88a7a70308..db19257d5ab 100644
--- a/meson.build
+++ b/meson.build
@@ -3167,6 +3167,8 @@ dl_dep = cc.find_library('dl', required: false)
 
 util_dep = cc.find_library('util', required: false)
 
+anl_dep = cc.find_library('anl', required: false)
+
 getopt_dep = cc.find_library('getopt', required: false)
 gnugetopt_dep = cc.find_library('gnugetopt', required: false)
 # Check if we want to replace getopt/getopt_long even if provided by the system
@@ -3208,6 +3210,7 @@ func_checks = [
   ['elf_aux_info'],
   ['explicit_bzero'],
   ['explicit_memset'],
+  ['getaddrinfo_a', {'dependencies': [anl_dep]}],
   ['getauxval'],
   ['getifaddrs'],
   ['getopt', {'dependencies': [getopt_dep, gnugetopt_dep], 'skip': always_replace_getopt}],
diff --git a/src/common/ip.c b/src/common/ip.c
index bdcb52b8141..a5ac7293da3 100644
--- a/src/common/ip.c
+++ b/src/common/ip.c
@@ -65,8 +65,30 @@ pg_getaddrinfo_all(const char *hostname, const char *servname,
 		return getaddrinfo_unix(servname, hintp, result);
 
 	/* NULL has special meaning to getaddrinfo(). */
-	rc = getaddrinfo((!hostname || hostname[0] == '\0') ? NULL : hostname,
-					 servname, hintp, result);
+	hostname = (!hostname || hostname[0] == '\0') ? NULL : hostname;
+
+#if defined(HAVE_GETADDRINFO_A)
+	{
+		struct gaicb cb = {
+			.ar_name = hostname,
+			.ar_service = servname,
+			.ar_request = hintp,
+		};
+		struct gaicb *cb_list[] = {&cb};
+
+		rc = getaddrinfo_a(GAI_WAIT, cb_list, lengthof(cb_list), NULL);
+		if (rc)
+			return rc;			/* TODO: it may be useful to fall back? */
+
+		rc = gai_error(&cb);
+		if (rc)
+			return rc;
+
+		*result = cb.ar_result;
+	}
+#else
+	rc = getaddrinfo(hostname, servname, hintp, result);
+#endif
 
 	return rc;
 }
-- 
2.43.0

