Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u9rhT-007JWc-38 for pgsql-docs@arkaria.postgresql.org; Tue, 29 Apr 2025 20:37:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1u9rhQ-009onJ-Oe for pgsql-docs@arkaria.postgresql.org; Tue, 29 Apr 2025 20:37:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1u9rhQ-009onB-H0 for pgsql-docs@lists.postgresql.org; Tue, 29 Apr 2025 20:37:05 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u9rhP-000EyX-0a for pgsql-docs@lists.postgresql.org; Tue, 29 Apr 2025 20:37:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 53TKb1R31087319; Tue, 29 Apr 2025 16:37:01 -0400 From: Tom Lane To: Nathan Long cc: pgsql-docs@lists.postgresql.org Subject: Re: `inet` docs suggestion and possible bug report In-reply-to: <898176.1745872137@sss.pgh.pa.us> References: <898176.1745872137@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 28 Apr 2025 16:28:57 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1087317.1745959021.1@sss.pgh.pa.us> Date: Tue, 29 Apr 2025 16:37:01 -0400 Message-ID: <1087318.1745959021@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I wrote: > Nathan Long writes: >> At least in the case of `inet`, another reason is for accurate comparison. >> IPv4 and IPv6 both have shorthand textual representations; eg `127.1` = >> `127.1.0.0`. Text storage would consider these unequal. > I'm not sure how much we want to press that point, because AFAICS > the code we use does not have the same abbreviation rules you are > expecting. Notably, it thinks '127.1' means 127.1.0.0. > (We lifted this logic from BIND 20+ years ago, so while it might > not entirely agree with practice elsewhere, it has a respectable > pedigree and I'm hesitant to mess with it.) I spent a little while researching this. BIND stopped including the relevant code at all sometime in the past 10 years, apparently feeling that POSIX standardization means the libc versions of inet_pton() behave sufficiently alike everywhere. You can still find copies of their code at, eg, https://users.isc.org/~each/doxygen/bind9/inet__pton_8c-source.html and there are also versions in the NetBSD source tree and probably elsewhere. As far as I can find, none of these will interpret '127.1' as 127.0.0.1. Some will reject it (which is what the POSIX spec for the function says to do) and some will interpret it as 127.1.0.0. Where 127.1 => 127.0.0.1 seems to come from is inet_addr (in POSIX) and inet_aton (not in POSIX), which are legacy IPv4-only functions. They say (quoting POSIX here): Values specified using IPv4 dotted decimal notation take one of the following forms: a.b.c.d When four parts are specified, each shall be interpreted as a byte of data and assigned, from left to right, to the four bytes of an Internet address. a.b.c When a three-part address is specified, the last part shall be interpreted as a 16-bit quantity and placed in the rightmost two bytes of the network address. This makes the three-part address format convenient for specifying Class B network addresses as "128.net.host". a.b When a two-part address is supplied, the last part shall be interpreted as a 24-bit quantity and placed in the rightmost three bytes of the network address. This makes the two-part address format convenient for specifying Class A network addresses as "net.host". a When only one part is given, the value shall be stored directly in the network address without any byte rearrangement. All numbers supplied as parts in IPv4 dotted decimal notation may be decimal, octal, or hexadecimal. Frankly, I don't think we want to support this. Classful network addresses have gone the way of the dodo. And the fact that it'd be inconsistent with our traditional interpretation for some non-error cases such as '127.1/16'::inet is really problematic. Moreover, the option to allow octal input is a true disaster, not least because there is plenty of code out there that is willing to print IPv4 addresses with zero-padded *decimal* byte values. So at this point I'm very unexcited about touching the behavior of inet_in. Maybe in another universe it would have acted differently, but we have too many years of history with the current behavior. I do take your point about the inet types helping to standardize comparison behavior, but I think we should probably limit the text to talking about IPv6 abbreviations. Maybe like these types offer input error checking and specialized operators and functions (see ). + They also simplify comparisons of inconsistently-written addresses, + such as abbreviated and unabbreviated IPv6 addresses. regards, tom lane