From 9f80f4835a55a1cbffcda5d23a617917f3286c14 Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Fri, 24 Jan 2014 02:32:39 +0900 Subject: [PATCH] Add libpq function PQhostaddr(). There was a bug in the psql's meta command \conninfo. When the IP address was specified in the hostaddr and psql used it to create a connection (i.e., psql -d "hostaddr=xxx"), \conninfo could not display that address. This is because \conninfo got the connection information only from PQhost() which could not return hostaddr. This patch adds PQhostaddr(), and changes \conninfo so that it can display not only the host name that PQhost() returns but also the IP address which PQhostaddr() returns. The bug has existed since 9.1 where \conninfo was introduced. But it's too late to add new libpq function into the released versions, so no backpatch. --- doc/src/sgml/libpq.sgml | 18 ++++++++++++++++++ src/bin/psql/command.c | 2 +- src/interfaces/libpq/exports.txt | 1 + src/interfaces/libpq/fe-connect.c | 8 ++++++++ src/interfaces/libpq/libpq-fe.h | 1 + 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml index 3ab06a1a1b..33641ade60 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1464,6 +1464,24 @@ char *PQhost(const PGconn *conn); + + + PQhostaddr + + PQhostaddr + + + + + + Returns the server numeric IP address of the connection. + +char *PQhostaddr(const PGconn *conn); + + + + + PQport diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 49f389071a..f498cdfee7 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -300,7 +300,7 @@ exec_command(const char *cmd, else if (strcmp(cmd, "conninfo") == 0) { char *db = PQdb(pset.db); - char *host = PQhost(pset.db); + char *host = (PQhostaddr(pset.db) != NULL) ? PQhostaddr(pset.db) : PQhost(pset.db); if (db == NULL) printf(_("You are currently not connected to a database.\n")); diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt index 93da50df31..cbb6e36c11 100644 --- a/src/interfaces/libpq/exports.txt +++ b/src/interfaces/libpq/exports.txt @@ -165,3 +165,4 @@ lo_lseek64 162 lo_tell64 163 lo_truncate64 164 PQconninfo 165 +PQhostaddr 166 diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index 92cdd2cbe3..35672a747b 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -5200,6 +5200,14 @@ PQhost(const PGconn *conn) } } +char * +PQhostaddr(const PGconn *conn) +{ + if (!conn) + return NULL; + return conn->pghostaddr; +} + char * PQport(const PGconn *conn) { diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 24b2f98acc..856bdff006 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -301,6 +301,7 @@ extern char *PQdb(const PGconn *conn); extern char *PQuser(const PGconn *conn); extern char *PQpass(const PGconn *conn); extern char *PQhost(const PGconn *conn); +extern char *PQhostaddr(const PGconn *conn); extern char *PQport(const PGconn *conn); extern char *PQtty(const PGconn *conn); extern char *PQoptions(const PGconn *conn); -- 2.40.0