Add libpq function PQhostaddr().
authorFujii Masao <fujii@postgresql.org>
Thu, 23 Jan 2014 17:32:39 +0000 (02:32 +0900)
committerFujii Masao <fujii@postgresql.org>
Thu, 23 Jan 2014 17:32:39 +0000 (02:32 +0900)
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
src/bin/psql/command.c
src/interfaces/libpq/exports.txt
src/interfaces/libpq/fe-connect.c
src/interfaces/libpq/libpq-fe.h

index 3ab06a1a1b75dac8abafc64dac8238a154286498..33641ade60867ecf6813e4c63a4378597a038b0f 100644 (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>
index 49f389071a16d86598248fd9fe3e2eb962bbdd05..f498cdfee7638492faa3d09152165ed895d29c9c 100644 (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"));
index 93da50df31143713f38537fbe667e5d7fe48fc94..cbb6e36c119d8bc178ff42ddfb3c57243c6ec54f 100644 (file)
@@ -165,3 +165,4 @@ lo_lseek64                162
 lo_tell64                 163
 lo_truncate64             164
 PQconninfo                165
+PQhostaddr                166
index 92cdd2cbe3524fc9012ce6fb3ffaf217c6184f7f..35672a747b1f4bae6830e722e481f87e3f8ec62e 100644 (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)
 {
index 24b2f98accaabe213229b65d967b9cb2568b7b52..856bdff006fe7d11726aa28de61893348f9c920f 100644 (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);