1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<?php
/*
Mainly copied from djamps's work - https://github.com/djamps/php-ipv6-calculator/blob/master/ipcalc.php
Edited by Jonas Lindstad for The Gathering 2015
Licensed under GPL
*/
// Convert array of short unsigned integers to binary
function _packBytes($array) {
foreach ( $array as $byte ) {
$chars .= pack('C',$byte);
}
return $chars;
}
// Convert binary to array of short integers
function _unpackBytes($string) {
return unpack('C*',$string);
}
// Add array of short unsigned integers
function _addBytes($array1,$array2) {
$result = array();
$carry = 0;
foreach ( array_reverse($array1,true) as $value1 ) {
$value2 = array_pop($array2);
if ( empty($result) ) { $value2++; }
$newValue = $value1 + $value2 + $carry;
if ( $newValue > 255 ) {
$newValue = $newValue - 256;
$carry = 1;
} else {
$carry = 0;
}
array_unshift($result,$newValue);
}
return $result;
}
/* Useful Functions */
function _cdr2Bin ($cdrin,$len=4){
if ( $len > 4 || $cdrin > 32 ) { // Are we ipv6?
return str_pad(str_pad("", $cdrin, "1"), 128, "0");
} else {
return str_pad(str_pad("", $cdrin, "1"), 32, "0");
}
}
function _bin2Cdr ($binin){
return strlen(rtrim($binin,"0"));
}
function _cdr2Char ($cdrin,$len=4){
$hex = _bin2Hex(_cdr2Bin($cdrin,$len));
return _hex2Char($hex);
}
function _char2Cdr ($char){
$bin = _hex2Bin(_char2Hex($char));
return _bin2Cdr($bin);
}
function _hex2Char($hex){
return pack('H*',$hex);
}
function _char2Hex($char){
$hex = unpack('H*',$char);
return array_pop($hex);
}
function _hex2Bin($hex){
$bin='';
for($i=0;$i<strlen($hex);$i++)
$bin.=str_pad(decbin(hexdec($hex{$i})),4,'0',STR_PAD_LEFT);
return $bin;
}
function _bin2Hex($bin){
$hex='';
for($i=strlen($bin)-4;$i>=0;$i-=4)
$hex.=dechex(bindec(substr($bin,$i,4)));
return strrev($hex);
}
/*
Converts a v4/v6 subnet to the first usable IP
*/
function net_to_gw($net){
$maxSubNets = '2048'; // Stop memory leak from invalid input or large ranges
$charHost = inet_pton(strtok($net, '/'));
$charMask = _cdr2Char(strtok('/'),strlen($charHost));
$charHostMask = substr(_cdr2Char(127),-strlen($charHost));
$charNet = $charHost & $charMask; // Supernet network address
$charHostMin = $charNet | ~$charHostMask;
return inet_ntop($charHostMin);
}
/*
$maxSubNets = '2048'; // Stop memory leak from invalid input or large ranges
$superNet = '2a02:ed02:180a::13/64';
if (ereg('/',$superNet)){ //if cidr type mask
$charHost = inet_pton(strtok($superNet, '/'));
$charMask = _cdr2Char(strtok('/'),strlen($charHost));
}
$charHostMask = substr(_cdr2Char(127),-strlen($charHost));
$charNet = $charHost & $charMask; // Supernet network address
$charHostMin = $charNet | ~$charHostMask;
echo 'Første brukbare adresse i ' . $superNet . ': ';
echo inet_ntop($charHostMin);
*/
?>
|