1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-26 12:21:12 +03:00

When reporting the server as not responding, if the hostname was

supplied, also print the IP address.  This allows IPv4 and IPv6 failures
to be distinguished.  Also useful when a hostname resolves to multiple
IP addresses.

Also, remove use of inet_ntoa() and use our own inet_net_ntop() in all
places, including in libpq, because it is thread-safe.
This commit is contained in:
Bruce Momjian
2010-11-24 17:04:19 -05:00
parent 725d52d0c2
commit ba11258ccb
9 changed files with 313 additions and 250 deletions

View File

@ -960,9 +960,28 @@ connectFailureMessage(PGconn *conn, int errorno)
else
#endif /* HAVE_UNIX_SOCKETS */
{
char host_addr[NI_MAXHOST];
bool display_host_addr;
struct sockaddr_in *host_addr_struct = (struct sockaddr_in *)
&conn->raddr.addr;
/*
* Optionally display the network address with the hostname.
* This is useful to distinguish between IPv4 and IPv6 connections.
*/
if (conn->pghostaddr != NULL)
strlcpy(host_addr, conn->pghostaddr, NI_MAXHOST);
else if (inet_net_ntop(conn->addr_cur->ai_family, &host_addr_struct->sin_addr,
host_addr_struct->sin_family == AF_INET ? 32 : 128,
host_addr, sizeof(host_addr)) == NULL)
strcpy(host_addr, "???");
display_host_addr = !conn->pghostaddr &&
strcmp(conn->pghost, host_addr) != 0;
appendPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not connect to server: %s\n"
"\tIs the server running on host \"%s\" and accepting\n"
"\tIs the server running on host \"%s\" %s%s%sand accepting\n"
"\tTCP/IP connections on port %s?\n"),
SOCK_STRERROR(errorno, sebuf, sizeof(sebuf)),
conn->pghostaddr
@ -970,6 +989,10 @@ connectFailureMessage(PGconn *conn, int errorno)
: (conn->pghost
? conn->pghost
: "???"),
/* display the IP address only if not already output */
display_host_addr ? "(" : "",
display_host_addr ? host_addr : "",
display_host_addr ? ") " : "",
conn->pgport);
}
}