Spravne zarovnani delek ipv6 adres reverzu
[mirrors/Programs.git] / php / ipv6wildcard.php
CommitLineData
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 */
10function ipv6_prefix2wildcards($address, $prefix) {
e9c37a44
TM
11 //Special case of /0 prefix
12 if($prefix == 0) return ['*'];
85bedcf1
TM
13
14 //Parse ip and prefix
15 echo("IPv6 prefix: ".$address."/".$prefix."\n");
16 $addr = inet_pton ($address);
17 $pref = $prefix;
18 $a = gmp_import($addr);
19
20 //Separate maskable octets
e9c37a44
TM
21 $pref_hex = ceil($pref / 4); //Number of prefix octets
22 $pref_bin = $pref_hex*4; //Number of prefix octets in bits
23 $pref_rem = $pref_bin - $pref; //Remainder of prefix
24 echo("Rounding prefix: ".$pref_bin."-".$pref_rem."\n");
85bedcf1
TM
25
26 $a = gmp_div_q($a, gmp_pow(2,128-$prefix)); //Truncate to just prefix
27 $a = gmp_mul($a, gmp_pow(2,$pref_rem)); //Fill remaining bits in last wildcard octet with zeros
28
29 //Generate wildcards
30 $wildcards = [];
31 for($i = 0; $i < (2**$pref_rem) ; $i++) {
32 //echo("ADDED: ".$i." = ".decbin($i)."\n");
33 $ap = gmp_or($a, gmp_init($i));
e9c37a44
TM
34 $w = gmp_strval($ap, 16);
35 $w = str_repeat('0', $pref_hex-strlen($w)).$w;
36 if(strlen($w) < 32) $w.='*';
37 $wildcards[] = $w;
85bedcf1
TM
38 }
39
40 return $wildcards;
41}
42
e9c37a44
TM
43function ipv6_string2rev($str) {
44 return implode('.',str_split(strrev($str)));
45}
46
85bedcf1
TM
47if(isset($argv[2])) {
48 $wcs = ipv6_prefix2wildcards($argv[1],$argv[2]);
49} else {
50 $wcs = ipv6_prefix2wildcards('2001:1:3::10', 67);
51}
52
53echo("\n");
54foreach($wcs as $w) {
e9c37a44
TM
55 echo($w."\n");
56}
57
58echo("\n");
59foreach($wcs as $w) {
60 echo(ipv6_string2rev($w)."\n");
85bedcf1 61}
This page took 0.196686 seconds and 4 git commands to generate.