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.
This commit is contained in:
Fujii Masao 2014-01-24 02:32:39 +09:00
parent d5bc6ce6ac
commit 9f80f4835a
5 changed files with 29 additions and 1 deletions

View File

@ -1464,6 +1464,24 @@ char *PQhost(const PGconn *conn);
</listitem>
</varlistentry>
<varlistentry id="libpq-pqhostaddr">
<term>
<function>PQhostaddr</function>
<indexterm>
<primary>PQhostaddr</primary>
</indexterm>
</term>
<listitem>
<para>
Returns the server numeric IP address of the connection.
<synopsis>
char *PQhostaddr(const PGconn *conn);
</synopsis>
</para>
</listitem>
</varlistentry>
<varlistentry id="libpq-pqport">
<term>
<function>PQport</function>

View File

@ -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"));

View File

@ -165,3 +165,4 @@ lo_lseek64 162
lo_tell64 163
lo_truncate64 164
PQconninfo 165
PQhostaddr 166

View File

@ -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)
{

View File

@ -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);