summaryrefslogtreecommitdiff
path: root/src/interfaces/libpq/fe-connect.c
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2010-11-24 17:04:19 -0500
committerBruce Momjian <bruce@momjian.us>2010-11-24 17:04:19 -0500
commitba11258ccbf364d85de48b8b7fd46953ea7bb4f4 (patch)
tree94824222a24f4e98cb2af0e90f845a687eb38dac /src/interfaces/libpq/fe-connect.c
parent725d52d0c27cffe8c99bb78e2b0d2480d5cd702b (diff)
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.
Diffstat (limited to 'src/interfaces/libpq/fe-connect.c')
-rw-r--r--src/interfaces/libpq/fe-connect.c25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 8f318a1a8cc..8011604e5cd 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -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);
}
}