On Tue, Nov 25, 2014 at 09:53:10PM +0900, Fujii Masao wrote:
> On Tue, Nov 25, 2014 at 12:37 PM, Noah Misch <n...@leadboat.com> wrote:
> > On Wed, Jan 22, 2014 at 11:48:26PM +0900, Fujii Masao wrote:
> >> (3) PQhost() cannot return the hostaddr.
> >
> >> We can fix the problem (3) by changing PQhost() so that it also
> >> returns the hostaddr. But this change might break the existing
> >> application using PQhost(). So, I added new libpq function PQhostaddr()
> >> which returns the hostaddr, and changed \conninfo so that it reports
> >> correct connection information by using both PQhost() and PQhostaddr().
> >
> >> +     <varlistentry id="libpq-pqhostaddr">
> >> +      <term>
> >> +       <function>PQhostaddr</function>
> >> +       <indexterm>
> >> +        <primary>PQhostaddr</primary>
> >> +       </indexterm>
> >> +      </term>
> >> +
> >> +      <listitem>
> >> +       <para>
> >> +        Returns the server numeric IP address or host name of the 
> >> connection.
> >> + <synopsis>
> >> + char *PQhostaddr(const PGconn *conn);
> >> + </synopsis>
> >> +       </para>
> >> +      </listitem>
> >> +     </varlistentry>
> >
> > From reading this documentation, I assumed this function would return a
> > non-empty value for every TCP connection.  After all, every TCP connection 
> > has
> > a peer IP address.  In fact, PQhostaddr() returns the raw value of the
> > "hostaddr" connection parameter, whether from a libpq function argument or
> > from the PGHOSTADDR environment variable.  (If the parameter and environment
> > variable are absent, it returns NULL.  Adding "hostaddr=" to the connection
> > string makes it return the empty string.)  A caller wanting the specific raw
> > value of a parameter could already use PQconninfo().  I suspect this new
> > function will confuse more than help.  What do you think of reverting it and
> > having \conninfo use PQconninfo() to discover any "hostaddr" value?
> 
> Sounds reasonable to me. Are you planning to implement and commit the patch?

Sure.  I'll first issue "git revert 9f80f48", then apply the attached patch.
Since libpq ignores a hostaddr parameter equal to the empty string, this
implementation does likewise.  Apart from that, I anticipate behavior
identical to today's code.
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index b518fb1..0c60746 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -302,13 +302,32 @@ exec_command(const char *cmd,
        else if (strcmp(cmd, "conninfo") == 0)
        {
                char       *db = PQdb(pset.db);
-               char       *host = PQhost(pset.db);
 
                if (db == NULL)
                        printf(_("You are currently not connected to a 
database.\n"));
                else
                {
-                       if (host == NULL)
+                       char       *host;
+                       PQconninfoOption *connOptions;
+                       PQconninfoOption *option;
+
+                       /* Report "hostaddr" if libpq used it, otherwise 
PQhost(). */
+                       host = PQhost(pset.db);
+                       connOptions = PQconninfo(pset.db);
+                       if (connOptions == NULL)
+                       {
+                               fprintf(stderr, _("out of memory\n"));
+                               exit(EXIT_FAILURE);
+                       }
+                       for (option = connOptions; option && option->keyword; 
option++)
+                               if (strcmp(option->keyword, "hostaddr") == 0)
+                               {
+                                       if (option->val != NULL && 
option->val[0] != '\0')
+                                               host = option->val;
+                                       break;
+                               }
+
+                       if (host == NULL)       /* can't happen */
                                host = DEFAULT_PGSOCKET_DIR;
                        /* If the host is an absolute path, the connection is 
via socket */
                        if (is_absolute_path(host))
@@ -318,6 +337,8 @@ exec_command(const char *cmd,
                                printf(_("You are connected to database \"%s\" 
as user \"%s\" on host \"%s\" at port \"%s\".\n"),
                                           db, PQuser(pset.db), host, 
PQport(pset.db));
                        printSSLInfo();
+
+                       PQconninfoFree(connOptions);
                }
        }
 
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to