Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hVMO4-0004Zc-Qx for pgsql-hackers@arkaria.postgresql.org; Mon, 27 May 2019 20:38:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hVMO2-0003S0-D4 for pgsql-hackers@arkaria.postgresql.org; Mon, 27 May 2019 20:38:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hVMO1-0003Rt-Tl for pgsql-hackers@lists.postgresql.org; Mon, 27 May 2019 20:38:26 +0000 Received: from mail-ot1-x341.google.com ([2607:f8b0:4864:20::341]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hVMNy-000838-P3 for pgsql-hackers@lists.postgresql.org; Mon, 27 May 2019 20:38:25 +0000 Received: by mail-ot1-x341.google.com with SMTP id u11so15782667otq.7 for ; Mon, 27 May 2019 13:38:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Gmv9koEBku3qxhcxqIaWMYtN2Nzg+lYG97NWBWBcphE=; b=gn18PZDjg7G16Fc78WpofX2gw449xN7Y+eKFikyYqNRpfpUhRq+a8aG1/U1/A0Nhr6 wzBDIy4krqYy9GJj9EnTK7unXwsZa27X9lnT3u6xCRDSR46dFgWWpkwohlA7T3Lfd5OJ Ks0uSM/ktNQjPecdrGH3RyFi+4tyEbHmx9B34= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=Gmv9koEBku3qxhcxqIaWMYtN2Nzg+lYG97NWBWBcphE=; b=XyQkpDRTjaPh0jSzpUm4z6SB5rjKB2IeRJpDda2C4QO/wUJh758fGQefG5mrFhe7RW 81rQqWQVkbJtGI+yDXmYQu8KS+9vwIrQ8h8bQbN/h4bnt80zdquPwNL0tE7tyNgKxzY2 Xyx0LPMEJ+Yf+2TvF+PZZdqwtng1/YKGb21FAxK4TaxShPtp4elSqAKCpPCBi5NU4QGj Ast3hkSx+wGVYzADQJ0QRz7LKZfRxVbTZlZt7TOQeBpwcaum4xVCdGEnlnuQFkWSoR9J Ki0lTtmSg6RWdLCGU7N0DFciU0mQN4TpTK50EsXa1AOfHuwWI0lTczdSSDAMqZr/vyAE sAGw== X-Gm-Message-State: APjAAAXsVMGPjCYo5ATHXyNJQLHs7PjTj/93K7hsIv9gWaE6QOvn6Mdc rfTqz55gJfvM3rphQfQ3XeJcqg== X-Google-Smtp-Source: APXvYqyOfPK1l4LYzgUAgNpRlpsLdwg/sOUYX3xBS4UK+35UNeihY278jdWe0v8x6Xj5v0inCh1FRg== X-Received: by 2002:a9d:7e88:: with SMTP id m8mr40454344otp.358.1558989500465; Mon, 27 May 2019 13:38:20 -0700 (PDT) Received: from gust.leadboat.com ([172.56.6.125]) by smtp.gmail.com with ESMTPSA id b25sm3994815otq.65.2019.05.27.13.38.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 27 May 2019 13:38:19 -0700 (PDT) Date: Mon, 27 May 2019 16:37:53 -0400 From: Noah Misch To: Alvaro Herrera Cc: Fabien COELHO , PostgreSQL Developers , Arthur Zakirov , Robert Haas Subject: Re: fix psql \conninfo & \connect when using hostaddr Message-ID: <20190527203713.GA58392@gust.leadboat.com> References: <20181119155315.vsrgj2droozqgfqo@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20181119155315.vsrgj2droozqgfqo@alvherre.pgsql> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Mon, Nov 19, 2018 at 12:53:15PM -0300, Alvaro Herrera wrote: > commit 6e5f8d4 > Commit: Alvaro Herrera > CommitDate: Mon Nov 19 14:34:12 2018 -0300 > > psql: Show IP address in \conninfo > Discussion: https://postgr.es/m/alpine.DEB.2.21.1810261532380.27686@lancre > https://postgr.es/m/alpine.DEB.2.21.1808201323020.13832@lancre > --- a/src/bin/psql/command.c > +++ b/src/bin/psql/command.c > @@ -2894,12 +2911,27 @@ do_connect(enum trivalue reuse_previous_specification, > } > > /* grab missing values from the old connection */ > - if (!user && reuse_previous) > - user = PQuser(o_conn); > - if (!host && reuse_previous) > - host = PQhost(o_conn); > - if (!port && reuse_previous) > - port = PQport(o_conn); > + if (reuse_previous) > + { > + if (!user) > + user = PQuser(o_conn); > + if (host && strcmp(host, PQhost(o_conn)) == 0) > + { > + /* > + * if we are targetting the same host, reuse its hostaddr for > + * consistency > + */ > + hostaddr = PQhostaddr(o_conn); > + } > + if (!host) > + { > + host = PQhost(o_conn); > + /* also set hostaddr for consistency */ > + hostaddr = PQhostaddr(o_conn); > + } > + if (!port) > + port = PQport(o_conn); > + } > > /* > * Any change in the parameters read above makes us discard the password. The "hostaddr = PQhostaddr(o_conn)" branches make \connect use the same IP address as the existing connection. I like that when the existing connection uses a hostaddr= parameter, but I doubt it's the right thing otherwise. If the server IP changes, \connect should find the server at its new IP. If the server has multiple IPs, \connect should have the opportunity to try them all, just like the original connection attempt could have. Other than that, the \connect behavior change makes sense to me. However, nothing updated \connect documentation. (Even the commit log message said nothing about \connect.) > @@ -3071,14 +3108,27 @@ do_connect(enum trivalue reuse_previous_specification, > /* If the host is an absolute path, the connection is via socket */ This comment is no longer correct. Copy the other "via socket" comment. > --- a/src/interfaces/libpq/fe-connect.c > +++ b/src/interfaces/libpq/fe-connect.c > @@ -1471,6 +1471,39 @@ connectNoDelay(PGconn *conn) > return 1; > } > > +/* ---------- > + * Write currently connected IP address into host_addr (of len host_addr_len). > + * If unable to, set it to the empty string. > + * ---------- > + */ > +static void > +getHostaddr(PGconn *conn, char *host_addr, int host_addr_len) > +{ > + struct sockaddr_storage *addr = &conn->raddr.addr; > + > + if (conn->connhost[conn->whichhost].type == CHT_HOST_ADDRESS) > + strlcpy(host_addr, conn->connhost[conn->whichhost].hostaddr, host_addr_len); I recommend removing this branch; inet_net_ntop() would work fine in the CHT_HOST_ADDRESS case, and that has the advantage of putting the address in standard form. Currently, hostaddr in nonstandard form stays that way (trailing space, in this example): [nm@gust 8:1 2019-05-23T13:21:54 postgresql 0]$ psql -X "hostaddr='127.0.7.1 '" -c '\conninfo' You are connected to database "test" as user "nm" on host "127.0.7.1 " at port "5433". > @@ -6173,6 +6202,25 @@ PQhost(const PGconn *conn) > } > > char * > +PQhostaddr(const PGconn *conn) > +{ > + if (!conn) > + return NULL; > + > + if (conn->connhost != NULL) > + { > + if (conn->connhost[conn->whichhost].hostaddr != NULL && > + conn->connhost[conn->whichhost].hostaddr[0] != '\0') > + return conn->connhost[conn->whichhost].hostaddr; > + > + if (conn->connip != NULL) > + return conn->connip; > + } > + > + return ""; > +} Similar to my comment on getHostaddr(), why ever use hostaddr? connip should always be equivalent to hostaddr when hostaddr is set. (connip may be NULL if a malloc failed, but if we really cared, we'd fail the connection attempt when that happens. If connip differs in any other material way, I'd want the user to see connip.)