A postgresql inet to ip6.arpa snippet

Olipro olipro at 8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa
Wed Jul 27 11:56:30 CEST 2011


On Tuesday 26 Jul 2011 01:10:47 Martin Millnert wrote:
> 128-bit friends,
> 
> I just took the time to come up with a IMHO quite clean way to create
> ip6.arpa strings from inet types in PostgreSQL and thought I'd share (if
> nothing else so for the Internet archives, where I came up empty with a
> similar solution previously):
> 
> Prerequisites: a string reverse() function. Either, wait for/install 9.2
> where it seems to be included, or simply install for example this one:
> http://archives.postgresql.org/pgsql-sql/2005-02/msg00334.php
> 
> Then:
> sysdb=# SELECT
> reverse(regexp_replace(substring(encode(inet_send('2a02:9a0:100:2::101'::in
> et), 'hex') from 9), '(.)', E'.\\1', 'g')); reverse
> ------------------------------------------------------------------
>  1.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.0.0.1.0.0.a.9.0.2.0.a.2.
> (1 row)
> 
> It may not look as much, but for me the dark magic was the inet_send
> function to access the raw data of the inet type. I'm using this myself
> now to generate part of a zone file anyway.
> 
> Cheers,
> Martin
> 
> (If you're not using pgsql already, "you're doing it wrong" ;-) )

The icing on the cake would be to support CIDR for this; i.e. if I specify 
2001:db8::/32 then it becomes 8.b.d.0.1.0.0.2.ip6.arpa


More information about the ipv6-ops mailing list