Thread: LibPQ doesn't say host=* translates to localhost

LibPQ doesn't say host=* translates to localhost

From
"David G. Johnston"
Date:
The documentation [0] doesn't say this should work and general networking knowledge leads me to believe it wouldn't [1].

PGHOST='*' psql postgres
psql (17beta1)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "davidj" on host "*" (address "127.0.0.1") at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)

David J.
[1]
ping *
ping: src: Temporary failure in name resolution

ping localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.026 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.031 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.028 ms

Re: LibPQ doesn't say host=* translates to localhost

From
Tom Lane
Date:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> The documentation [0] doesn't say this should work and general networking
> knowledge leads me to believe it wouldn't [1].

> PGHOST='*' psql postgres
> psql (17beta1)

Seems to be an undocumented glibc-ism.  That works for me on RHEL8,
but not on macOS:

$ psql -h '*'
psql: error: could not translate host name "*" to address: nodename nor servname provided, or not known

The POSIX spec for getaddrinfo(3) doesn't suggest it should
work, either.

            regards, tom lane



Re: LibPQ doesn't say host=* translates to localhost

From
Francisco Olarte
Date:
On Thu, 27 Jun 2024 at 21:57, David G. Johnston
<david.g.johnston@gmail.com> wrote:
> The documentation [0] doesn't say this should work and general networking knowledge leads me to believe it wouldn't
[1].
>
> PGHOST='*' psql postgres
> psql (17beta1)
> SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
> Type "help" for help.
>
> postgres=# \conninfo
> You are connected to database "postgres" as user "davidj" on host "*" (address "127.0.0.1") at port "5432".
> SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
>
> David J.
>
> [0] https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
>
> [1]
> ping *
> ping: src: Temporary failure in name resolution

Bad quoting, PGHOST=¡*', single quoted, but ping *, unquouted, with an
error message which hints at <<echo ping *>> giving back <<ping src
....>>

Bug may be real, but ping '*' could give some better data for debugging.

Francisco Olarte.



Re: LibPQ doesn't say host=* translates to localhost

From
"David G. Johnston"
Date:
On Fri, Jun 28, 2024 at 12:48 AM Francisco Olarte <folarte@peoplecall.com> wrote:
On Thu, 27 Jun 2024 at 21:57, David G. Johnston
<david.g.johnston@gmail.com> wrote:
> The documentation [0] doesn't say this should work and general networking knowledge leads me to believe it wouldn't [1].
>
> PGHOST='*' psql postgres
> psql (17beta1)
> SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
> Type "help" for help.
>
> postgres=# \conninfo
> You are connected to database "postgres" as user "davidj" on host "*" (address "127.0.0.1") at port "5432".
> SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off, ALPN: postgresql)
>
> David J.
>
> [0] https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
>
> [1]
> ping *
> ping: src: Temporary failure in name resolution

Bad quoting, PGHOST=¡*', single quoted, but ping *, unquouted, with an
error message which hints at <<echo ping *>> giving back <<ping src
....>>

Bug may be real, but ping '*' could give some better data for debugging.


Good catch.

ping '*'
ping: *: Name or service not known

This is Ubuntu.

I'm fine with not documenting this if we are delegating to some underlying library that makes the behavior platform-specific (though maybe document that...even though it now seems obvious to me in retrospect).  I just thought since ping didn't work we were doing something in between to avoid the issue.

David J.

Re: LibPQ doesn't say host=* translates to localhost

From
Tom Lane
Date:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> Good catch.

> ping '*'
> ping: *: Name or service not known

> This is Ubuntu.

Yeah, I see the same on a couple different Red Hat versions.  Very
interesting, since it implies that ping is using something other than
getaddrinfo(3) to resolve the hostname.  I'm not quite interested
enough to go find out what, though.

            regards, tom lane



Re: LibPQ doesn't say host=* translates to localhost

From
Francisco Olarte
Date:
On Fri, 28 Jun 2024 at 18:56, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "David G. Johnston" <david.g.johnston@gmail.com> writes:
> > ping '*'
> > ping: *: Name or service not known
> > This is Ubuntu.
> Yeah, I see the same on a couple different Red Hat versions.  Very
> interesting, since it implies that ping is using something other than
> getaddrinfo(3) to resolve the hostname.  I'm not quite interested
> enough to go find out what, though.

Same in Debian, as expected.

But it picked my curiosity, so I did an ltrace:

$ sudo ltrace ping '*' |& fgrep addrinfo
getaddrinfo("*", nil, 0x7ffde84578c0, 0x7ffde84578a8) = -2

And did this little perl experiment....
>>> addrinfo.pl
use strict;
use Socket qw(:addrinfo);

sub U($) { $_[0] // 'undef'; }
sub M($) {
    my $h = shift;
    join ';', map { "$_=>".U($h->{$_}) }  sort keys %$h;
}

sub pai {
    my ($host, $service, $hints)=@_;
    printf "gai(%s,%s,{%s})\n",
    $host, U $service, M $hints;
    my ($err, @addr) = getaddrinfo($host, $service, $hints);
    printf "  err=%s\n", $err//'UNDEF';
    while(my ($i, $v)=each @addr) {
    printf "  a[%d]={%s},addr=%s\n",
        $i, M $v,
        unpack("h*",$v->{addr});
    }
}


pai('*',undef,{});
pai('*',undef,{family=>Socket::AF_INET});
pai('*',5432, {family=>Socket::AF_INET});
<<<
Which gives:
>>>
$ perl -w addrinfo.pl
gai(*,undef,{})
  err=Name or service not known
gai(*,undef,{family=>2})
  err=Name or service not known
gai(*,5432,{family=>2})
  err=
  a[0]={addr=>8;canonname=>undef;family=>2;protocol=>6;socktype=>1},addr=20005183f70000100000000000000000
  a[1]={addr=>8;canonname=>undef;family=>2;protocol=>17;socktype=>2},addr=20005183f70000100000000000000000
  a[2]={addr=>8;canonname=>undef;family=>2;protocol=>0;socktype=>3},addr=20005183f70000100000000000000000
<<<
I am not sure how to follow, or if perl uses glibc, and have to go
AFK, and I am not sure if it relates, but getaddrinfo may be doing
strange things.

Francisco Olarte.