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