Skip to content

Commit f88e807

Browse files
committed
Use an initdb-time test to see if the local version of getaddrinfo()
chokes on IPv6 addresses, and comment out the IPv6 entry in the default pg_hba.conf if so. Per Andrew Dunstan.
1 parent d0096a4 commit f88e807

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

src/bin/initdb/initdb.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1994, Regents of the University of California
4343
* Portions taken from FreeBSD.
4444
*
45-
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.94 2005/08/02 15:16:27 tgl Exp $
45+
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.95 2005/08/22 16:27:36 tgl Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -54,13 +54,13 @@
5454
#include <unistd.h>
5555
#include <locale.h>
5656
#include <signal.h>
57-
#include <errno.h>
5857
#ifdef HAVE_LANGINFO_H
5958
#include <langinfo.h>
6059
#endif
6160

6261
#include "libpq/pqsignal.h"
6362
#include "mb/pg_wchar.h"
63+
#include "getaddrinfo.h"
6464
#include "getopt_long.h"
6565

6666
#ifndef HAVE_INT_OPTRESET
@@ -1210,11 +1210,42 @@ setup_config(void)
12101210
conflines = replace_token(conflines,"@remove-line-for-nolocal@","");
12111211
#endif
12121212

1213-
#ifndef HAVE_IPV6
1213+
#if defined(HAVE_IPV6) && defined(HAVE_STRUCT_ADDRINFO) && defined(HAVE_GETADDRINFO)
1214+
/*
1215+
* Probe to see if there is really any platform support for IPv6, and
1216+
* comment out the relevant pg_hba line if not. This avoids runtime
1217+
* warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1218+
* useful on Windows, where executables built on a machine with IPv6
1219+
* may have to run on a machine without.
1220+
*
1221+
* We don't bother with testing if we aren't using the system version
1222+
* of getaddrinfo, since we know our own version doesn't do IPv6.
1223+
*/
1224+
{
1225+
struct addrinfo *gai_result;
1226+
struct addrinfo hints;
1227+
1228+
/* for best results, this code should match parse_hba() */
1229+
hints.ai_flags = AI_NUMERICHOST;
1230+
hints.ai_family = PF_UNSPEC;
1231+
hints.ai_socktype = 0;
1232+
hints.ai_protocol = 0;
1233+
hints.ai_addrlen = 0;
1234+
hints.ai_canonname = NULL;
1235+
hints.ai_addr = NULL;
1236+
hints.ai_next = NULL;
1237+
1238+
if (getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1239+
conflines = replace_token(conflines,
1240+
"host all all ::1",
1241+
"#host all all ::1");
1242+
}
1243+
#else /* !HAVE_IPV6 etc */
1244+
/* If we didn't compile IPV6 support at all, always comment it out */
12141245
conflines = replace_token(conflines,
12151246
"host all all ::1",
12161247
"#host all all ::1");
1217-
#endif
1248+
#endif /* HAVE_IPV6 etc */
12181249

12191250
/* Replace default authentication methods */
12201251
conflines = replace_token(conflines,

0 commit comments

Comments
 (0)