Skip to content

Commit be3b666

Browse files
committed
Clean up IPv4 vs IPv6 bogosity in connectFailureMessage().
Newly added code was supposing that "struct sockaddr_in" applies to IPv6.
1 parent 3840bc0 commit be3b666

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -989,27 +989,41 @@ connectFailureMessage(PGconn *conn, int errorno)
989989
{
990990
char host_addr[NI_MAXHOST];
991991
bool display_host_addr;
992-
struct sockaddr_in *host_addr_struct = (struct sockaddr_in *)
993-
&conn->raddr.addr;
992+
struct sockaddr_storage *addr = &conn->raddr.addr;
994993

995994
/*
996995
* Optionally display the network address with the hostname.
997996
* This is useful to distinguish between IPv4 and IPv6 connections.
998997
*/
999998
if (conn->pghostaddr != NULL)
1000999
strlcpy(host_addr, conn->pghostaddr, NI_MAXHOST);
1001-
else if (inet_net_ntop(host_addr_struct->sin_family,
1002-
&host_addr_struct->sin_addr.s_addr,
1003-
host_addr_struct->sin_family == AF_INET ? 32 : 128,
1004-
host_addr, sizeof(host_addr)) == NULL)
1000+
else if (addr->ss_family == AF_INET)
1001+
{
1002+
if (inet_net_ntop(AF_INET,
1003+
&((struct sockaddr_in *) addr)->sin_addr.s_addr,
1004+
32,
1005+
host_addr, sizeof(host_addr)) == NULL)
1006+
strcpy(host_addr, "???");
1007+
}
1008+
#ifdef HAVE_IPV6
1009+
else if (addr->ss_family == AF_INET6)
1010+
{
1011+
if (inet_net_ntop(AF_INET6,
1012+
&((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr,
1013+
128,
1014+
host_addr, sizeof(host_addr)) == NULL)
1015+
strcpy(host_addr, "???");
1016+
}
1017+
#endif
1018+
else
10051019
strcpy(host_addr, "???");
10061020

1007-
display_host_addr = !conn->pghostaddr &&
1008-
strcmp(conn->pghost, host_addr) != 0;
1009-
1021+
display_host_addr = (conn->pghostaddr == NULL) &&
1022+
(strcmp(conn->pghost, host_addr) != 0);
1023+
10101024
appendPQExpBuffer(&conn->errorMessage,
10111025
libpq_gettext("could not connect to server: %s\n"
1012-
"\tIs the server running on host \"%s\" %s%s%sand accepting\n"
1026+
"\tIs the server running on host \"%s\"%s%s%s and accepting\n"
10131027
"\tTCP/IP connections on port %s?\n"),
10141028
SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)),
10151029
conn->pghostaddr
@@ -1018,9 +1032,9 @@ connectFailureMessage(PGconn *conn, int errorno)
10181032
? conn->pghost
10191033
: "???"),
10201034
/* display the IP address only if not already output */
1021-
display_host_addr ? "(" : "",
1035+
display_host_addr ? " (" : "",
10221036
display_host_addr ? host_addr : "",
1023-
display_host_addr ? ") " : "",
1037+
display_host_addr ? ")" : "",
10241038
conn->pgport);
10251039
}
10261040
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy