From 8addc7e881daaed28df892575d501da752d3bf34 Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Mon, 26 Mar 2018 17:38:21 +1100
Subject: [PATCH] PQhost to return active connected host and hostaddr details

Earlier PQhost doesn't return the connected host details
when the connection type is CHT_HOST_ADDRESS instead it
returns the provided all connection host parameter values
or the default host details, this can lead to confusion.

It is better to provide the active host or hostaddr details of
the connected server host irrespective of the connection type.
when both host and hostaddr are specified in the connection
string, active host parameter value is returned.

PQhost function returns NULL when the input conn is NULL,
otherwise returns an empty string when the input conn structure
cannot be evaluated.

Similarly PQPort is also changed to return the active connection
port or NULL or an empty string.
---
 doc/src/sgml/libpq.sgml           | 24 ++++++++++++++++++++++--
 src/interfaces/libpq/fe-connect.c | 25 +++++++++++++------------
 2 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 1fd5dd9fca..e206b5d08d 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1692,7 +1692,7 @@ char *PQpass(const PGconn *conn);
 
      <listitem>
       <para>
-       Returns the server host name of the connection.
+       Returns the server host name of the active connection.
        This can be a host name, an IP address, or a directory path if the
        connection is via Unix socket.  (The path case can be distinguished
        because it will always be an absolute path, beginning
@@ -1701,6 +1701,20 @@ char *PQpass(const PGconn *conn);
 char *PQhost(const PGconn *conn);
 </synopsis>
       </para>
+
+      <para>
+       The <function>PQhost</function> function returns NULL when the
+       input conn parameter is NULL or an empty string if conn cannot be evaluated.
+       Applications of this function must carefully evaluate the return value.
+      </para>
+
+      <note>
+       <para>
+        when both <literal>host</literal> and <literal>hostaddr</literal>
+        parameters are specified in the connection string, the connection
+        <literal>host</literal> parameter is returned.
+       </para>
+      </note>
      </listitem>
     </varlistentry>
 
@@ -1714,12 +1728,18 @@ char *PQhost(const PGconn *conn);
 
      <listitem>
       <para>
-       Returns the port of the connection.
+       Returns the port of the active connection.
 
 <synopsis>
 char *PQport(const PGconn *conn);
 </synopsis>
       </para>
+
+      <para>
+       The <function>PQport</function> function returns NULL when the input
+       conn parameter is NULL or an empty string if conn cannot be evaluated.
+       Applications of this function must carefully evaluate the return value.
+      </para>
      </listitem>
     </varlistentry>
 
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 39c19998c2..33cf844bb3 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6017,19 +6017,18 @@ PQhost(const PGconn *conn)
 {
 	if (!conn)
 		return NULL;
-	if (conn->connhost != NULL &&
-		conn->connhost[conn->whichhost].type != CHT_HOST_ADDRESS)
-		return conn->connhost[conn->whichhost].host;
-	else if (conn->pghost != NULL && conn->pghost[0] != '\0')
-		return conn->pghost;
-	else
+
+	if (conn->connhost != NULL)
 	{
-#ifdef HAVE_UNIX_SOCKETS
-		return DEFAULT_PGSOCKET_DIR;
-#else
-		return DefaultHost;
-#endif
+		if (conn->connhost[conn->whichhost].host != NULL &&
+			conn->connhost[conn->whichhost].host[0] != '\0')
+			return conn->connhost[conn->whichhost].host;
+		else if ((conn->connhost[conn->whichhost].hostaddr != NULL) &&
+				(conn->connhost[conn->whichhost].hostaddr[0] != '\0'))
+			return conn->connhost[conn->whichhost].hostaddr;
 	}
+
+	return "";
 }
 
 char *
@@ -6037,9 +6036,11 @@ PQport(const PGconn *conn)
 {
 	if (!conn)
 		return NULL;
+
 	if (conn->connhost != NULL)
 		return conn->connhost[conn->whichhost].port;
-	return conn->pgport;
+
+	return "";
 }
 
 char *
-- 
2.16.1.windows.4

