Commit | Line | Data |
---|---|---|
6cdaf0d2 | 1 | #!/usr/bin/php |
85bedcf1 TM |
2 | <?php |
3 | ||
4 | /** | |
6cdaf0d2 TM |
5 | * (c) 2020 - Tomas Mudrunka <harvie@github> |
6 | * | |
85bedcf1 TM |
7 | * Given IPv6 address and prefix creates array of * wildcard suffixed |
8 | * addresses to be used for as reverse DNS records | |
9 | */ | |
10 | function ipv6_prefix2wildcards($address, $prefix) { | |
11 | ||
12 | //Parse ip and prefix | |
13 | echo("IPv6 prefix: ".$address."/".$prefix."\n"); | |
14 | $addr = inet_pton ($address); | |
15 | $pref = $prefix; | |
16 | $a = gmp_import($addr); | |
17 | ||
18 | //Separate maskable octets | |
19 | $pref_hex = ceil($pref / 4)*4; //Prefix rounded up to full octets | |
20 | $pref_rem = $pref_hex - $pref; //Remainder of prefix | |
21 | echo("Rounding prefix: ".$pref_hex."-".$pref_rem."\n"); | |
22 | ||
23 | $a = gmp_div_q($a, gmp_pow(2,128-$prefix)); //Truncate to just prefix | |
24 | $a = gmp_mul($a, gmp_pow(2,$pref_rem)); //Fill remaining bits in last wildcard octet with zeros | |
25 | ||
26 | //Generate wildcards | |
27 | $wildcards = []; | |
28 | for($i = 0; $i < (2**$pref_rem) ; $i++) { | |
29 | //echo("ADDED: ".$i." = ".decbin($i)."\n"); | |
30 | $ap = gmp_or($a, gmp_init($i)); | |
31 | $wildcards[] = $ap; | |
32 | } | |
33 | ||
34 | return $wildcards; | |
35 | } | |
36 | ||
37 | if(isset($argv[2])) { | |
38 | $wcs = ipv6_prefix2wildcards($argv[1],$argv[2]); | |
39 | } else { | |
40 | $wcs = ipv6_prefix2wildcards('2001:1:3::10', 67); | |
41 | } | |
42 | ||
43 | echo("\n"); | |
44 | foreach($wcs as $w) { | |
45 | //echo(gmp_strval($w, 2)."* :-)\n"); | |
46 | echo(gmp_strval($w, 16)."*\n"); | |
47 | } |