Convert IPV6 to nibble format for PTR records

I need to convert the ipv6 address to its nibble format for use when creating ptr entries dynamically. Here is the information I received from Wikipedia:

IPv6 Reverse Resolution

Reverse DNS lookup for IPv6 addresses use the special ip6.arpa domain. An IPv6 address is displayed as a name in this domain as a sequence of pieces in reverse order, represented as hexadecimal digits as subdomains. For example, the domain name of the pointer corresponds to the IPv6 2001 address: db8 :: 567: 89ab ba9.8.7.6.5.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.bd0.1.0.0.2.ip6.arpa .

The only thing I could find regarding nibbles was in the pack function, http://www.php.net/pack . I could not find solutions with examples from the search problem.

Any help is greatly appreciated.

+6
source share
5 answers

Given a suitable modern version of PHP (> = 5.1.0 or 5.3+ on Windows), use the inet_pton function to parse the IPv6 address into a 16-byte array, and then use the standard string operators to cancel it.

 $ip = '2001:db8::567:89ab'; $addr = inet_pton($ip); $unpack = unpack('H*hex', $addr); $hex = $unpack['hex']; $arpa = implode('.', array_reverse(str_split($hex))) . '.ip6.arpa'; 
+10
source

You can use the ipv6calc string ipv6calc (UNIX / Linux) from here .

For instance:

 $ ./ipv6calc --out revnibbles.arpa 2001:db8::1 No input type specified, try autodetection...found type: ipv6addr 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.bd0.1.0.0.2.ip6.arpa. 

You can paste this script to eat your direct zone files and create PTRs .

+3
source

And this is the opposite, based on the large Alnitak code, as a function:

 function ptr_to_ipv6($arpa) { $mainptr = substr($arpa, 0, strlen($arpa)-9); $pieces = array_reverse(explode(".",$mainptr)); $hex = implode("",$pieces); $ipbin = pack('H*', $hex); $ipv6addr = inet_ntop($ipbin); return $ipv6addr; } 
+2
source

I'm doing it:

 function expand($ip){ $hex = unpack("H*hex", inet_pton($ip)); $ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1); return $ip; } function ipv6_reverse_calc($address){ $rev = chop(chunk_split(strrev(str_replace(':', '', expand($address))), 1, '.'), '.'); return "$rev.ip6.arpa"; } 
0
source

Well, from this example you can see that the nibble format is the full ipv6 address (including 0'd fields), the reverse, then separated by characters and separated by periods. So personally, I would just use a string representation and manipulate it as needed.

-1
source

Source: https://habr.com/ru/post/892290/


All Articles