From 6557b564a1597883a074540bddecdc159678e372 Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Sun, 14 Jan 2018 15:59:03 +1100
Subject: [PATCH 1/2] Addition of two new libpq API's

PQhostNoDefault is a new API which is similar like PQhost,
but it doesn't return the default host name.

PQhostaddr is a new API which is similar like PQhost,
but it returns the server host address of the connection or
NULL
---
 doc/src/sgml/libpq.sgml           | 41 ++++++++++++++++++++++++++++++++++++
 src/interfaces/libpq/exports.txt  |  2 ++
 src/interfaces/libpq/fe-connect.c | 44 ++++++++++++++++++++++++++++++++++-----
 src/interfaces/libpq/libpq-fe.h   |  2 ++
 4 files changed, 84 insertions(+), 5 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 02884bae1f..ae6250640a 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1621,6 +1621,47 @@ char *PQhost(const PGconn *conn);
      </listitem>
     </varlistentry>
 
+    <varlistentry id="libpq-pqhostnodefault">
+     <term>
+      <function>PQhostNoDefault</function>
+      <indexterm>
+       <primary>PQhostNoDefault</primary>
+      </indexterm>
+     </term>
+
+     <listitem>
+      <para>
+       Returns the server host name of the connection or NULL if there is
+       no host name associated with the 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 with <literal>/</literal>.)
+<synopsis>
+char *PQhostNoDefault(const PGconn *conn);
+</synopsis>
+      </para>
+     </listitem>
+    </varlistentry>
+
+    <varlistentry id="libpq-pqhostaddr">
+     <term>
+      <function>PQhostaddr</function>
+      <indexterm>
+       <primary>PQhostaddr</primary>
+      </indexterm>
+     </term>
+
+     <listitem>
+      <para>
+       Returns the server host address of the connection.
+
+<synopsis>
+char *PQhostaddr(const PGconn *conn);
+</synopsis>
+      </para>
+     </listitem>
+    </varlistentry>
+
     <varlistentry id="libpq-pqport">
      <term>
       <function>PQport</function>
diff --git a/src/interfaces/libpq/exports.txt b/src/interfaces/libpq/exports.txt
index d6a38d0df8..94581e73b4 100644
--- a/src/interfaces/libpq/exports.txt
+++ b/src/interfaces/libpq/exports.txt
@@ -172,3 +172,5 @@ PQsslAttribute            169
 PQsetErrorContextVisibility 170
 PQresultVerboseErrorMessage 171
 PQencryptPasswordConn     172
+PQhostNoDefault				173
+PQhostaddr					174
\ No newline at end of file
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 77eebb0ba1..174c8f98db 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6015,13 +6015,15 @@ PQpass(const PGconn *conn)
 char *
 PQhost(const PGconn *conn)
 {
+	char *host;
+
 	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;
+
+	host = PQhostNoDefault(conn);
+
+	if (host)
+		return host;
 	else
 	{
 #ifdef HAVE_UNIX_SOCKETS
@@ -6032,6 +6034,38 @@ PQhost(const PGconn *conn)
 	}
 }
 
+char *
+PQhostNoDefault(const PGconn *conn)
+{
+	if (!conn)
+		return NULL;
+	if (conn->connhost != NULL &&
+		conn->connhost[conn->whichhost].host != NULL &&
+		conn->connhost[conn->whichhost].host[0] != '\0')
+		return conn->connhost[conn->whichhost].host;
+	else if (conn->pghost != NULL && conn->pghost[0] != '\0')
+		return conn->pghost;
+	else
+		return NULL;
+}
+
+char *
+PQhostaddr(const PGconn *conn)
+{
+	if (!conn)
+		return NULL;
+	if (conn->connhost != NULL &&
+			conn->connhost[conn->whichhost].hostaddr != NULL &&
+			conn->connhost[conn->whichhost].hostaddr[0] != '\0')
+		return conn->connhost[conn->whichhost].hostaddr;
+	else if (conn->pghostaddr != NULL && conn->pghostaddr[0] != '\0')
+		return conn->pghostaddr;
+	else
+	{
+		return NULL;
+	}
+}
+
 char *
 PQport(const PGconn *conn)
 {
diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h
index ed9c806861..551bcc41c0 100644
--- a/src/interfaces/libpq/libpq-fe.h
+++ b/src/interfaces/libpq/libpq-fe.h
@@ -312,6 +312,8 @@ 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 *PQhostNoDefault(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.15.0.windows.1

