From f162a15a6d723f8c94d9daa6236149e1f39b0d9a Mon Sep 17 00:00:00 2001
From: Thomas Munro <tmunro@postgresql.org>
Date: Thu, 18 Aug 2022 11:55:10 +1200
Subject: [PATCH] Remove configure probe for sockaddr_in6 and require AF_INET6.

SUSv3 <netinet/in.h> defines struct sockaddr_in6, and all targeted Unix
systems have it.  Windows has it in <ws2ipdef.h>.  Remove the configure
probe, the macro and a small amount of dead code.

Also remove a mention of IPv6-less builds from the documentation, since
there aren't any.

This is similar to commits f5580882 and 077bf2f2 for Unix sockets.
Even though AF_INET6 is an "optional" component of SUSv3, there are no
known modern operating system without it, and it seems even less likely
to be omitted from future systems than AF_UNIX.

Discussion: https://postgr.es/m/CA+hUKGKErNfhmvb_H0UprEmp4LPzGN06yR2_0tYikjzB-2ECMw@mail.gmail.com
---
 configure                           | 10 ----------
 configure.ac                        |  6 ------
 doc/src/sgml/client-auth.sgml       |  2 --
 src/backend/libpq/auth.c            | 21 ---------------------
 src/backend/libpq/hba.c             |  5 -----
 src/backend/libpq/ifaddr.c          | 18 +-----------------
 src/backend/libpq/pqcomm.c          |  2 --
 src/backend/utils/adt/network.c     | 10 ----------
 src/backend/utils/adt/pgstatfuncs.c | 11 ++---------
 src/bin/initdb/initdb.c             | 10 ----------
 src/include/pg_config.h.in          |  3 ---
 src/include/utils/inet.h            |  9 ++++-----
 src/interfaces/libpq/fe-connect.c   |  2 --
 src/port/inet_net_ntop.c            |  5 ++---
 src/tools/ifaddrs/test_ifaddrs.c    |  2 --
 src/tools/msvc/Solution.pm          |  1 -
 16 files changed, 9 insertions(+), 108 deletions(-)

diff --git a/configure b/configure
index b7fd6c5f4e..c275330114 100755
--- a/configure
+++ b/configure
@@ -16279,16 +16279,6 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
-ac_fn_c_check_type "$LINENO" "struct sockaddr_in6" "ac_cv_type_struct_sockaddr_in6" "$ac_includes_default
-#include <netinet/in.h>
-"
-if test "x$ac_cv_type_struct_sockaddr_in6" = xyes; then :
-
-$as_echo "#define HAVE_IPV6 1" >>confdefs.h
-
-fi
-
-
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for PS_STRINGS" >&5
 $as_echo_n "checking for PS_STRINGS... " >&6; }
 if ${pgac_cv_var_PS_STRINGS+:} false; then :
diff --git a/configure.ac b/configure.ac
index e5740f4fb5..a97a48a508 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1801,12 +1801,6 @@ AC_CHECK_DECLS([pwritev], [], [AC_LIBOBJ(pwritev)], [#include <sys/uio.h>])
 # This is probably only present on macOS, but may as well check always
 AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])
 
-AC_CHECK_TYPE([struct sockaddr_in6],
-        [AC_DEFINE(HAVE_IPV6, 1, [Define to 1 if you have support for IPv6.])],
-        [],
-[$ac_includes_default
-#include <netinet/in.h>])
-
 AC_CACHE_CHECK([for PS_STRINGS], [pgac_cv_var_PS_STRINGS],
 [AC_LINK_IFELSE([AC_LANG_PROGRAM(
 [#include <machine/vmparam.h>
diff --git a/doc/src/sgml/client-auth.sgml b/doc/src/sgml/client-auth.sgml
index 433759928b..c6f1b70fd3 100644
--- a/doc/src/sgml/client-auth.sgml
+++ b/doc/src/sgml/client-auth.sgml
@@ -305,8 +305,6 @@ hostnogssenc  <replaceable>database</replaceable>  <replaceable>user</replaceabl
        An entry given in IPv4 format will match only IPv4 connections,
        and an entry given in IPv6 format will match only IPv6 connections,
        even if the represented address is in the IPv4-in-IPv6 range.
-       Note that entries in IPv6 format will be rejected if the system's
-       C library does not have support for IPv6 addresses.
       </para>
 
       <para>
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index 1545ff9f16..71677b69d8 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -3014,13 +3014,8 @@ PerformRadiusTransaction(const char *server, const char *secret, const char *por
 	int			packetlength;
 	pgsocket	sock;
 
-#ifdef HAVE_IPV6
 	struct sockaddr_in6 localaddr;
 	struct sockaddr_in6 remoteaddr;
-#else
-	struct sockaddr_in localaddr;
-	struct sockaddr_in remoteaddr;
-#endif
 	struct addrinfo hint;
 	struct addrinfo *serveraddrs;
 	int			port;
@@ -3130,18 +3125,12 @@ PerformRadiusTransaction(const char *server, const char *secret, const char *por
 	}
 
 	memset(&localaddr, 0, sizeof(localaddr));
-#ifdef HAVE_IPV6
 	localaddr.sin6_family = serveraddrs[0].ai_family;
 	localaddr.sin6_addr = in6addr_any;
 	if (localaddr.sin6_family == AF_INET6)
 		addrsize = sizeof(struct sockaddr_in6);
 	else
 		addrsize = sizeof(struct sockaddr_in);
-#else
-	localaddr.sin_family = serveraddrs[0].ai_family;
-	localaddr.sin_addr.s_addr = INADDR_ANY;
-	addrsize = sizeof(struct sockaddr_in);
-#endif
 
 	if (bind(sock, (struct sockaddr *) &localaddr, addrsize))
 	{
@@ -3244,21 +3233,11 @@ PerformRadiusTransaction(const char *server, const char *secret, const char *por
 			return STATUS_ERROR;
 		}
 
-#ifdef HAVE_IPV6
 		if (remoteaddr.sin6_port != pg_hton16(port))
-#else
-		if (remoteaddr.sin_port != pg_hton16(port))
-#endif
 		{
-#ifdef HAVE_IPV6
 			ereport(LOG,
 					(errmsg("RADIUS response from %s was sent from incorrect port: %d",
 							server, pg_ntoh16(remoteaddr.sin6_port))));
-#else
-			ereport(LOG,
-					(errmsg("RADIUS response from %s was sent from incorrect port: %d",
-							server, pg_ntoh16(remoteaddr.sin_port))));
-#endif
 			continue;
 		}
 
diff --git a/src/backend/libpq/hba.c b/src/backend/libpq/hba.c
index 1447588c4a..8f2b8c6b03 100644
--- a/src/backend/libpq/hba.c
+++ b/src/backend/libpq/hba.c
@@ -645,8 +645,6 @@ ipv4eq(struct sockaddr_in *a, struct sockaddr_in *b)
 	return (a->sin_addr.s_addr == b->sin_addr.s_addr);
 }
 
-#ifdef HAVE_IPV6
-
 static bool
 ipv6eq(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
 {
@@ -658,7 +656,6 @@ ipv6eq(struct sockaddr_in6 *a, struct sockaddr_in6 *b)
 
 	return true;
 }
-#endif							/* HAVE_IPV6 */
 
 /*
  * Check whether host name matches pattern.
@@ -747,7 +744,6 @@ check_hostname(hbaPort *port, const char *hostname)
 					break;
 				}
 			}
-#ifdef HAVE_IPV6
 			else if (gai->ai_addr->sa_family == AF_INET6)
 			{
 				if (ipv6eq((struct sockaddr_in6 *) gai->ai_addr,
@@ -757,7 +753,6 @@ check_hostname(hbaPort *port, const char *hostname)
 					break;
 				}
 			}
-#endif
 		}
 	}
 
diff --git a/src/backend/libpq/ifaddr.c b/src/backend/libpq/ifaddr.c
index de35fbba45..ff9dadfb37 100644
--- a/src/backend/libpq/ifaddr.c
+++ b/src/backend/libpq/ifaddr.c
@@ -34,11 +34,9 @@ static int	range_sockaddr_AF_INET(const struct sockaddr_in *addr,
 								   const struct sockaddr_in *netaddr,
 								   const struct sockaddr_in *netmask);
 
-#ifdef HAVE_IPV6
 static int	range_sockaddr_AF_INET6(const struct sockaddr_in6 *addr,
 									const struct sockaddr_in6 *netaddr,
 									const struct sockaddr_in6 *netmask);
-#endif
 
 
 /*
@@ -56,12 +54,10 @@ pg_range_sockaddr(const struct sockaddr_storage *addr,
 		return range_sockaddr_AF_INET((const struct sockaddr_in *) addr,
 									  (const struct sockaddr_in *) netaddr,
 									  (const struct sockaddr_in *) netmask);
-#ifdef HAVE_IPV6
 	else if (addr->ss_family == AF_INET6)
 		return range_sockaddr_AF_INET6((const struct sockaddr_in6 *) addr,
 									   (const struct sockaddr_in6 *) netaddr,
 									   (const struct sockaddr_in6 *) netmask);
-#endif
 	else
 		return 0;
 }
@@ -78,9 +74,6 @@ range_sockaddr_AF_INET(const struct sockaddr_in *addr,
 		return 0;
 }
 
-
-#ifdef HAVE_IPV6
-
 static int
 range_sockaddr_AF_INET6(const struct sockaddr_in6 *addr,
 						const struct sockaddr_in6 *netaddr,
@@ -97,7 +90,6 @@ range_sockaddr_AF_INET6(const struct sockaddr_in6 *addr,
 
 	return 1;
 }
-#endif							/* HAVE_IPV6 */
 
 /*
  *	pg_sockaddr_cidr_mask - make a network mask of the appropriate family
@@ -147,7 +139,6 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
 				break;
 			}
 
-#ifdef HAVE_IPV6
 		case AF_INET6:
 			{
 				struct sockaddr_in6 mask6;
@@ -172,7 +163,7 @@ pg_sockaddr_cidr_mask(struct sockaddr_storage *mask, char *numbits, int family)
 				memcpy(mask, &mask6, sizeof(mask6));
 				break;
 			}
-#endif
+
 		default:
 			return -1;
 	}
@@ -207,13 +198,11 @@ run_ifaddr_callback(PgIfAddrCallback callback, void *cb_data,
 			if (((struct sockaddr_in *) mask)->sin_addr.s_addr == INADDR_ANY)
 				mask = NULL;
 		}
-#ifdef HAVE_IPV6
 		else if (mask->sa_family == AF_INET6)
 		{
 			if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6 *) mask)->sin6_addr))
 				mask = NULL;
 		}
-#endif
 	}
 
 	/* If mask is invalid, generate our own fully-set mask */
@@ -437,10 +426,7 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
 {
 	struct sockaddr_in addr;
 	struct sockaddr_storage mask;
-
-#ifdef HAVE_IPV6
 	struct sockaddr_in6 addr6;
-#endif
 
 	/* addr 127.0.0.1/8 */
 	memset(&addr, 0, sizeof(addr));
@@ -452,7 +438,6 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
 						(struct sockaddr *) &addr,
 						(struct sockaddr *) &mask);
 
-#ifdef HAVE_IPV6
 	/* addr ::1/128 */
 	memset(&addr6, 0, sizeof(addr6));
 	addr6.sin6_family = AF_INET6;
@@ -462,7 +447,6 @@ pg_foreach_ifaddr(PgIfAddrCallback callback, void *cb_data)
 	run_ifaddr_callback(callback, cb_data,
 						(struct sockaddr *) &addr6,
 						(struct sockaddr *) &mask);
-#endif
 
 	return 0;
 }
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 8ff3be611d..d2f18dfe50 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -413,11 +413,9 @@ StreamServerPort(int family, const char *hostName, unsigned short portNumber,
 			case AF_INET:
 				familyDesc = _("IPv4");
 				break;
-#ifdef HAVE_IPV6
 			case AF_INET6:
 				familyDesc = _("IPv6");
 				break;
-#endif
 			case AF_UNIX:
 				familyDesc = _("Unix");
 				break;
diff --git a/src/backend/utils/adt/network.c b/src/backend/utils/adt/network.c
index ea1c7390d0..6d580ea78f 100644
--- a/src/backend/utils/adt/network.c
+++ b/src/backend/utils/adt/network.c
@@ -1725,9 +1725,7 @@ inet_client_addr(PG_FUNCTION_ARGS)
 	switch (port->raddr.addr.ss_family)
 	{
 		case AF_INET:
-#ifdef HAVE_IPV6
 		case AF_INET6:
-#endif
 			break;
 		default:
 			PG_RETURN_NULL();
@@ -1764,9 +1762,7 @@ inet_client_port(PG_FUNCTION_ARGS)
 	switch (port->raddr.addr.ss_family)
 	{
 		case AF_INET:
-#ifdef HAVE_IPV6
 		case AF_INET6:
-#endif
 			break;
 		default:
 			PG_RETURN_NULL();
@@ -1801,9 +1797,7 @@ inet_server_addr(PG_FUNCTION_ARGS)
 	switch (port->laddr.addr.ss_family)
 	{
 		case AF_INET:
-#ifdef HAVE_IPV6
 		case AF_INET6:
-#endif
 			break;
 		default:
 			PG_RETURN_NULL();
@@ -1840,9 +1834,7 @@ inet_server_port(PG_FUNCTION_ARGS)
 	switch (port->laddr.addr.ss_family)
 	{
 		case AF_INET:
-#ifdef HAVE_IPV6
 		case AF_INET6:
-#endif
 			break;
 		default:
 			PG_RETURN_NULL();
@@ -2102,7 +2094,6 @@ inetmi(PG_FUNCTION_ARGS)
 void
 clean_ipv6_addr(int addr_family, char *addr)
 {
-#ifdef HAVE_IPV6
 	if (addr_family == AF_INET6)
 	{
 		char	   *pct = strchr(addr, '%');
@@ -2110,5 +2101,4 @@ clean_ipv6_addr(int addr_family, char *addr)
 		if (pct)
 			*pct = '\0';
 	}
-#endif
 }
diff --git a/src/backend/utils/adt/pgstatfuncs.c b/src/backend/utils/adt/pgstatfuncs.c
index d9e2a79382..4cca30aae7 100644
--- a/src/backend/utils/adt/pgstatfuncs.c
+++ b/src/backend/utils/adt/pgstatfuncs.c
@@ -735,11 +735,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
 			}
 			else
 			{
-				if (beentry->st_clientaddr.addr.ss_family == AF_INET
-#ifdef HAVE_IPV6
-					|| beentry->st_clientaddr.addr.ss_family == AF_INET6
-#endif
-					)
+				if (beentry->st_clientaddr.addr.ss_family == AF_INET ||
+					beentry->st_clientaddr.addr.ss_family == AF_INET6)
 				{
 					char		remote_host[NI_MAXHOST];
 					char		remote_port[NI_MAXSERV];
@@ -1105,9 +1102,7 @@ pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
 	switch (beentry->st_clientaddr.addr.ss_family)
 	{
 		case AF_INET:
-#ifdef HAVE_IPV6
 		case AF_INET6:
-#endif
 			break;
 		default:
 			PG_RETURN_NULL();
@@ -1152,9 +1147,7 @@ pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
 	switch (beentry->st_clientaddr.addr.ss_family)
 	{
 		case AF_INET:
-#ifdef HAVE_IPV6
 		case AF_INET6:
-#endif
 			break;
 		case AF_UNIX:
 			PG_RETURN_INT32(-1);
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index a779153c74..29c28b7315 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -1176,7 +1176,6 @@ setup_config(void)
 
 	conflines = replace_token(conflines, "@remove-line-for-nolocal@", "");
 
-#ifdef HAVE_IPV6
 
 	/*
 	 * Probe to see if there is really any platform support for IPv6, and
@@ -1218,15 +1217,6 @@ setup_config(void)
 									  "#host    replication     all             ::1");
 		}
 	}
-#else							/* !HAVE_IPV6 */
-	/* If we didn't compile IPV6 support at all, always comment it out */
-	conflines = replace_token(conflines,
-							  "host    all             all             ::1",
-							  "#host    all             all             ::1");
-	conflines = replace_token(conflines,
-							  "host    replication     all             ::1",
-							  "#host    replication     all             ::1");
-#endif							/* HAVE_IPV6 */
 
 	/* Replace default authentication methods */
 	conflines = replace_token(conflines,
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index b8fe2201f4..112760e302 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -241,9 +241,6 @@
 /* Define to 1 if you have the global variable 'int timezone'. */
 #undef HAVE_INT_TIMEZONE
 
-/* Define to 1 if you have support for IPv6. */
-#undef HAVE_IPV6
-
 /* Define to 1 if __builtin_constant_p(x) implies "i"(x) acceptance. */
 #undef HAVE_I_CONSTRAINT__BUILTIN_CONSTANT_P
 
diff --git a/src/include/utils/inet.h b/src/include/utils/inet.h
index 3073c0307e..578c44ab40 100644
--- a/src/include/utils/inet.h
+++ b/src/include/utils/inet.h
@@ -30,11 +30,10 @@ typedef struct
 /*
  * We use these values for the "family" field.
  *
- * Referencing all of the non-AF_INET types to AF_INET lets us work on
- * machines which may not have the appropriate address family (like
- * inet6 addresses when AF_INET6 isn't present) but doesn't cause a
- * dump/reload requirement.  Pre-7.4 databases used AF_INET for the family
- * type on disk.
+ * We started when operating system support for IPv6 was not ubiquitous.
+ * Since we couldn't rely on AF_INET6 being defined yet, we used AF_INET + 1.
+ * We continue to do that so that on-disk data originating on such systems
+ * survives pg_upgrade.
  */
 #define PGSQL_AF_INET	(AF_INET + 0)
 #define PGSQL_AF_INET6	(AF_INET + 1)
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 8cefef20d1..917b19e0e9 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1652,7 +1652,6 @@ getHostaddr(PGconn *conn, char *host_addr, int host_addr_len)
 							 host_addr, host_addr_len) == NULL)
 			host_addr[0] = '\0';
 	}
-#ifdef HAVE_IPV6
 	else if (addr->ss_family == AF_INET6)
 	{
 		if (pg_inet_net_ntop(AF_INET6,
@@ -1661,7 +1660,6 @@ getHostaddr(PGconn *conn, char *host_addr, int host_addr_len)
 							 host_addr, host_addr_len) == NULL)
 			host_addr[0] = '\0';
 	}
-#endif
 	else
 		host_addr[0] = '\0';
 }
diff --git a/src/port/inet_net_ntop.c b/src/port/inet_net_ntop.c
index b8ad69c390..500c66dbf1 100644
--- a/src/port/inet_net_ntop.c
+++ b/src/port/inet_net_ntop.c
@@ -80,15 +80,14 @@ pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
 	 * We need to cover both the address family constants used by the PG inet
 	 * type (PGSQL_AF_INET and PGSQL_AF_INET6) and those used by the system
 	 * libraries (AF_INET and AF_INET6).  We can safely assume PGSQL_AF_INET
-	 * == AF_INET, but the INET6 constants are very likely to be different. If
-	 * AF_INET6 isn't defined, silently ignore it.
+	 * == AF_INET, but the INET6 constants are very likely to be different.
 	 */
 	switch (af)
 	{
 		case PGSQL_AF_INET:
 			return (inet_net_ntop_ipv4(src, bits, dst, size));
 		case PGSQL_AF_INET6:
-#if defined(AF_INET6) && AF_INET6 != PGSQL_AF_INET6
+#if AF_INET6 != PGSQL_AF_INET6
 		case AF_INET6:
 #endif
 			return (inet_net_ntop_ipv6(src, bits, dst, size));
diff --git a/src/tools/ifaddrs/test_ifaddrs.c b/src/tools/ifaddrs/test_ifaddrs.c
index b8dbb84945..b9a1b7b5e8 100644
--- a/src/tools/ifaddrs/test_ifaddrs.c
+++ b/src/tools/ifaddrs/test_ifaddrs.c
@@ -26,11 +26,9 @@ print_addr(struct sockaddr *addr)
 		case AF_INET:
 			len = sizeof(struct sockaddr_in);
 			break;
-#ifdef HAVE_IPV6
 		case AF_INET6:
 			len = sizeof(struct sockaddr_in6);
 			break;
-#endif
 		default:
 			len = sizeof(struct sockaddr_storage);
 			break;
diff --git a/src/tools/msvc/Solution.pm b/src/tools/msvc/Solution.pm
index 55b30090f6..e20702824e 100644
--- a/src/tools/msvc/Solution.pm
+++ b/src/tools/msvc/Solution.pm
@@ -277,7 +277,6 @@ sub GenerateFiles
 		HAVE_INTTYPES_H                             => undef,
 		HAVE_INT_OPTERR                             => undef,
 		HAVE_INT_OPTRESET                           => undef,
-		HAVE_IPV6                                   => 1,
 		HAVE_I_CONSTRAINT__BUILTIN_CONSTANT_P       => undef,
 		HAVE_KQUEUE                                 => undef,
 		HAVE_LANGINFO_H                             => undef,
-- 
2.35.1

