-
Notifications
You must be signed in to change notification settings - Fork 1
/
libIp6Addr.php
151 lines (126 loc) · 4.24 KB
/
libIp6Addr.php
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Expand ipv6 address
* @author From Internet
*/
function ip6Expand($ip){
$hex = unpack("H*hex", inet_pton($ip));
$ip = substr(preg_replace("/([A-f0-9]{4})/", "$1:", $hex['hex']), 0, -1);
return $ip;
}
/**
* Pack ipv6 address
* @author Johnson Liu
* @version v1.0
*/
function ip6Compress($ip) {
return inet_ntop(inet_pton($ip));
}
/**
* Get ipv6 prefix
* @author Johnson Liu
* @version v1.0
*/
function ip6GetPrefix($ip, $cidr) {
$prefix = '';
$ip_expanded = ip6Expand($ip);
$ip_grouped = explode(':', $ip_expanded);
$ip_grouped_save_num = $cidr/4;
for ( $i=0; $i<intval($cidr/16); $i++ ) {
$prefix = $prefix . $ip_grouped[$i];
$prefix = $prefix . ':';
}
// if need to spilt
if ( $cidr%16 ) {
for ( $i=0; $i<intval($cidr%16/4); $i++ ) {
$prefix = $prefix . substr($ip_grouped[$cidr/16], $i, 1);
}
// if need to cut
if ( $cidr%16%4 ) {
$current_num = substr($ip_grouped[$cidr/16], $cidr%16/4, 1);
$current_num = hexdec($current_num);
if ( $cidr%16%4 == 3 ) $current_num = $current_num & 0x0E;
else if ( $cidr%16%4 == 2 ) $current_num = $current_num & 0x0C;
else if ( $cidr%16%4 == 1 ) $current_num = $current_num & 0x08;
$current_num = dechex($current_num);
$prefix = $prefix . $current_num;
}
if (strlen($prefix)<35) $prefix = $prefix . '::';
} else {
if (strlen($prefix)<36) $prefix = $prefix . ':';
else $prefix = substr($prefix, 0, strlen($prefix)-1);
}
return ip6Compress($prefix);
}
/**
* Get ipv6 suffix
* @author Johnson Liu
* @version v1.0
*/
function ip6GetSuffix(string $ip, int $cidr) {
$suffix = '';
$ip_expanded = ip6Expand($ip);
$ip_grouped = explode(':', $ip_expanded);
$cidr = 128-$cidr;
for ( $i=7; $i>7-intval($cidr/16); $i-- ) {
$suffix = $ip_grouped[$i] . ':' . $suffix;
}
$suffix = substr($suffix, 0, strlen($suffix)-1);
// if need to split
if ( $cidr%16 ) {
$suffix = ':' . $suffix;
for ( $i=3; $i>3-intval($cidr%16/4); $i-- ) {
$suffix = substr($ip_grouped[7-intval($cidr/16)], $i, 1) . $suffix;
}
// if need to cut
if ( $cidr%16%4 ) {
$current_num = substr($ip_grouped[7-intval($cidr/16)], 3-$cidr%16/4, 1);
$current_num = hexdec($current_num);
if ( $cidr%16%4 == 3 ) $current_num = $current_num & 0x07;
else if ( $cidr%16%4 == 2 ) $current_num = $current_num & 0x03;
else if ( $cidr%16%4 == 1 ) $current_num = $current_num & 0x01;
$current_num = dechex($current_num);
$suffix = $current_num . $suffix;
}
if (strlen($suffix)<35) $suffix = '::' . $suffix;
} else {
if (strlen($suffix)<36) $suffix = '::' . $suffix;
}
return ip6Compress($suffix);
}
/**
* Merge ipv6 prefix and suffix into full address.
* @author Johnson Liu
* @version v2.1
*/
function ip6Merge(string $prefix, string $suffix, int $cidr = 64) {
if (!$cidr) $cidr = 64;
$addr = '';
$addr_grouped = [];
$prefix_expanded = ip6Expand($prefix);
$suffix_expanded = ip6Expand($suffix);
$prefix_grouped = explode(':', $prefix_expanded);
$suffix_grouped = explode(':', $suffix_expanded);
for ( $i=0; $i<intval($cidr/16); $i++ ) {
$addr_grouped[$i] = $prefix_grouped[$i];
}
for ( $i=intval($cidr/16); $i<8; $i++ ) {
$addr_grouped[$i] = $suffix_grouped[$i];
}
// if need to split
if ( $cidr%16 ) {
$prefix_temp = hexdec( $prefix_grouped[intval($cidr/16)] );
$suffix_temp = hexdec( $suffix_grouped[intval($cidr/16)] );
$cidr_temp = $cidr%16;
$prefix_temp = $prefix_temp & bindec( str_repeat('1', $cidr_temp) . str_repeat('0', 16-$cidr_temp) );
$suffix_temp = $suffix_temp & bindec( str_repeat('0', $cidr_temp) . str_repeat('1', 16-$cidr_temp) );
$addr_grouped[intval($cidr/16)] = dechex( $prefix_temp | $suffix_temp );
}
// pack grouped address
for ( $i=0; $i<8; $i++ ) {
$addr = $addr . $addr_grouped[$i] . ':';
}
$addr = substr($addr, 0, strlen($addr)-1);
return ip6Compress($addr);
}
?>