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 1qdwX9-005F1C-Fz for pgsql-general@arkaria.postgresql.org; Wed, 06 Sep 2023 17:41:43 +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 1qdwX6-00C5qU-2t for pgsql-general@arkaria.postgresql.org; Wed, 06 Sep 2023 17:41:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qdwX5-00C5qI-Jp for pgsql-general@lists.postgresql.org; Wed, 06 Sep 2023 17:41:39 +0000 Received: from mout-u-204.mailbox.org ([80.241.59.204]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qdwX1-0036BD-Ke for pgsql-general@lists.postgresql.org; Wed, 06 Sep 2023 17:41:38 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-204.mailbox.org (Postfix) with ESMTPS id 4RgqRs4Z0jz9smk; Wed, 6 Sep 2023 19:41:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ewie.name; s=MBO0001; t=1694022089; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=b2AMqU5ti0TB3BHygUml9B84nnbKP+1gbXswjohhTk0=; b=prQE1NLGZnK6lbM4YFx3hyx3HHDJ81WJ/9VftuQ0ZoDFawJB4WgF+Y+iKVFxmr0bV6238g o1ZCnfnqVmO33u2KF46lFYTNKPMEZSX/thj7McWDUBgC6TGqS6HKv5oB6GUUNggVEDRbDM wyTjL/cL7guP9L1A0sHKbS5IslICVQGfOavh1CyMIDmx1E2GfFiDuPtX9zPE9ho4Ou3Dqh soZU/wFHyHrJCgnt1MpagtDlo0xgkC40zp8lnuWUnvTACdw6TogDRkbDT1Ty0+qBRwTieF nDnF/8sqyoP8cGIwOK61ygumQrI+TJECxjvkzaVS9UH8vNlfxK6fpL279CAYXA== Date: Wed, 6 Sep 2023 19:41:28 +0200 (CEST) From: Erik Wienhold To: =?UTF-8?Q?Albrecht_Dre=C3=9F?= , pgsql-general@lists.postgresql.org Message-ID: <704799538.14934.1694022088295@office.mailbox.org> In-Reply-To: References: Subject: Re: Q: inet operators for IPv4 encapsulated in IPv6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Priority: 3 Importance: Normal X-Rspamd-Queue-Id: 4RgqRs4Z0jz9smk List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 06/09/2023 16:51 CEST Albrecht Dre=C3=9F wrot= e: > I have a question regarding the use of inet operators in environments wit= h > mixed IPv4 and IPv6 notations. > > Consider the example from sect. 9.12. =E2=80=9CNetwork Address Functions = and Operators=E2=80=9D > in the docs which returns properly > > > postgres=3D# select '192.168.1.5'::inet << '192.168.1/24'::inet; > ?column? > ---------- > t > (1 row) > > > However, for an IPv6 encapsulated IPv4 address, the result is > > > postgres=3D# select '::ffff:192.168.1.5'::inet << '192.168.1/24'::inet; > ?column? > ---------- > f > (1 row) > > > Although the representation is different, in reality '192.168.1.5' and > '::ffff:192.168.1.5' designate the same node, so IMHO it would be logical= if > the second statement would also return True. Is there any option to simp= ly > achieve this? I use PostgreSQL v. 15 on a Debian Bookworm box. The docs don't spell it out, but inet operators and functions expect values= of the same inet family. Comparing IPv4 and IPv6 always returns false, e.g. [= 1]. The only hint in the docs that may imply this is [2]: =09"When sorting inet or cidr data types, IPv4 addresses will always sort =09 before IPv6 addresses, including IPv4 addresses encapsulated or mapped =09 to IPv6 addresses, such as ::10.2.3.4 or ::ffff:10.4.3.2." You can write your own function that converts IPv4-mapped IPv6 addresses to IPv4 and then do the comparison: =09postgres=3D# SELECT ltrim(host('::ffff:192.168.1.5'::inet & '::255.255.2= 55.255'::inet), ':')::inet; =09 ltrim =09------------- =09 192.168.1.5 =09(1 row) [1] https://git.postgresql.org/gitweb/?p=3Dpostgresql.git;a=3Dblob;f=3Dsrc/= backend/utils/adt/network.c;h=3Dae11de0ba5007e445017b91acfeff3adc2e3d6af#l9= 23 [2] https://www.postgresql.org/docs/current/datatype-net-types.html -- Erik