From 313f56ce2d1b9dfd3483e4f39611baa27852835a Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 14 Jun 2019 18:02:26 -0400 Subject: Tweak libpq's PQhost, PQhostaddr, and psql's \connect Fixes some problems introduced by 6e5f8d489acc: * When reusing conninfo data from the previous connection in \connect, the host address should only be reused if it was specified as hostaddr; if it wasn't, then 'host' is resolved afresh. We were reusing the same IP address, which ignores a possible DNS change as well as any other addresses that the name resolves to than the one that was used in the original connection. * PQhost, PQhostaddr: Don't present user-specified hostaddr when we have an inet_net_ntop-produced equivalent address. The latter has been put in canonical format, which is cleaner (so it produces "127.0.0.1" when given "host=2130706433", for example). * Document the hostaddr-reusing aspect of \connect. * Fix some code comments Author: Fabien Coelho Reported-by: Noah Misch Discussion: https://postgr.es/m/20190527203713.GA58392@gust.leadboat.com --- src/interfaces/libpq/fe-connect.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/interfaces/libpq/fe-connect.c') diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index e58fa6742a4..c800d7921e3 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -1536,9 +1536,7 @@ getHostaddr(PGconn *conn, char *host_addr, int host_addr_len) { struct sockaddr_storage *addr = &conn->raddr.addr; - if (conn->connhost[conn->whichhost].type == CHT_HOST_ADDRESS) - strlcpy(host_addr, conn->connhost[conn->whichhost].hostaddr, host_addr_len); - else if (addr->ss_family == AF_INET) + if (addr->ss_family == AF_INET) { if (inet_net_ntop(AF_INET, &((struct sockaddr_in *) addr)->sin_addr.s_addr, @@ -6463,6 +6461,10 @@ PQhost(const PGconn *conn) if (conn->connhost != NULL) { + /* + * Return the verbatim host value provided by user, or hostaddr in its + * lack. + */ if (conn->connhost[conn->whichhost].host != NULL && conn->connhost[conn->whichhost].host[0] != '\0') return conn->connhost[conn->whichhost].host; @@ -6480,15 +6482,9 @@ PQhostaddr(const PGconn *conn) if (!conn) return NULL; - if (conn->connhost != NULL) - { - if (conn->connhost[conn->whichhost].hostaddr != NULL && - conn->connhost[conn->whichhost].hostaddr[0] != '\0') - return conn->connhost[conn->whichhost].hostaddr; - - if (conn->connip != NULL) - return conn->connip; - } + /* Return the parsed IP address */ + if (conn->connhost != NULL && conn->connip != NULL) + return conn->connip; return ""; } -- cgit v1.2.3