Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9f80f48

Browse files
committed
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.
1 parent d5bc6ce commit 9f80f48

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

doc/src/sgml/libpq.sgml

+18
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,24 @@ char *PQhost(const PGconn *conn);
14641464
</listitem>
14651465
</varlistentry>
14661466

1467+
<varlistentry id="libpq-pqhostaddr">
1468+
<term>
1469+
<function>PQhostaddr</function>
1470+
<indexterm>
1471+
<primary>PQhostaddr</primary>
1472+
</indexterm>
1473+
</term>
1474+
1475+
<listitem>
1476+
<para>
1477+
Returns the server numeric IP address of the connection.
1478+
<synopsis>
1479+
char *PQhostaddr(const PGconn *conn);
1480+
</synopsis>
1481+
</para>
1482+
</listitem>
1483+
</varlistentry>
1484+
14671485
<varlistentry id="libpq-pqport">
14681486
<term>
14691487
<function>PQport</function>

src/bin/psql/command.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ exec_command(const char *cmd,
300300
else if (strcmp(cmd, "conninfo") == 0)
301301
{
302302
char *db = PQdb(pset.db);
303-
char *host = PQhost(pset.db);
303+
char *host = (PQhostaddr(pset.db) != NULL) ? PQhostaddr(pset.db) : PQhost(pset.db);
304304

305305
if (db == NULL)
306306
printf(_("You are currently not connected to a database.\n"));

src/interfaces/libpq/exports.txt

+1
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,4 @@ lo_lseek64 162
165165
lo_tell64 163
166166
lo_truncate64 164
167167
PQconninfo 165
168+
PQhostaddr 166

src/interfaces/libpq/fe-connect.c

+8
Original file line numberDiff line numberDiff line change
@@ -5200,6 +5200,14 @@ PQhost(const PGconn *conn)
52005200
}
52015201
}
52025202

5203+
char *
5204+
PQhostaddr(const PGconn *conn)
5205+
{
5206+
if (!conn)
5207+
return NULL;
5208+
return conn->pghostaddr;
5209+
}
5210+
52035211
char *
52045212
PQport(const PGconn *conn)
52055213
{

src/interfaces/libpq/libpq-fe.h

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ extern char *PQdb(const PGconn *conn);
301301
extern char *PQuser(const PGconn *conn);
302302
extern char *PQpass(const PGconn *conn);
303303
extern char *PQhost(const PGconn *conn);
304+
extern char *PQhostaddr(const PGconn *conn);
304305
extern char *PQport(const PGconn *conn);
305306
extern char *PQtty(const PGconn *conn);
306307
extern char *PQoptions(const PGconn *conn);

0 commit comments

Comments
 (0)