Rev 46 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * IPv4 functions for PHP |
||
46 | daniel-mar | 5 | * Copyright 2012-2022 Daniel Marschall, ViaThinkSoft |
51 | daniel-mar | 6 | * Version 2022-09-22 |
2 | daniel-mar | 7 | * |
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
21 | // TODO: oop, exceptions? |
||
22 | |||
23 | // Very small self-test: |
||
24 | /* |
||
25 | function ipv4_selftest() { |
||
26 | $iv_b = ipv4_complete('1.2'); |
||
27 | $iv_m = 20; |
||
28 | $r = ipv4_cidr2range($iv_b, $iv_m); |
||
29 | echo "$iv_b/$iv_m => $r[0] - $r[1]\n"; |
||
30 | |||
31 | $rev = ipv4_range2cidr($r[0], $r[1]); |
||
32 | $rev = implode("\n", $rev); |
||
33 | echo "$r[0] - $r[1] => $rev ["; |
||
34 | $ok = $rev == "$iv_b/$iv_m"; |
||
35 | echo $ok ? 'OK' : 'Mismatch'; |
||
36 | echo "]\n"; |
||
37 | echo "In-CIDR-Test: "; |
||
38 | echo ipv4_in_cidr("$iv_b/$iv_m", "$iv_b/$iv_m") ? 'OK' : 'Fail'; |
||
39 | echo "\n"; |
||
40 | } |
||
41 | ipv4_selftest(); |
||
42 | */ |
||
43 | |||
44 | function ipv4_cidr2range($baseip_or_cidr, $subnet='') { |
||
45 | # (C) 2012 ViaThinkSoft |
||
46 | # Version 1.1 |
||
47 | # This function converts an CIDR notation <baseip>/<subnet> into an IPv4 address block array($low_ip, $high_ip) |
||
48 | |||
49 | if (strpos($baseip_or_cidr, '/') !== false) { |
||
50 | $tmp = explode('/', $baseip_or_cidr, 2); |
||
51 | $baseip_or_cidr = $tmp[0]; |
||
52 | $subnet = $tmp[1]; |
||
53 | unset($tmp); |
||
54 | } |
||
55 | |||
56 | if (($subnet < 0) || ($subnet > 32)) return false; |
||
57 | |||
58 | $maxint32 = 0xFFFFFFFF; |
||
59 | $netmask = $maxint32 << (32-$subnet); |
||
60 | $netmask = $netmask & $maxint32; // crop to 32 bits |
||
61 | $wildcard = $maxint32 ^ $netmask; // ~$netmask; |
||
62 | |||
63 | $x = ipv4_incomplete_ip2long($baseip_or_cidr) & $netmask; |
||
64 | $nums = $wildcard; |
||
65 | $low = long2ip($x); |
||
66 | $high = long2ip($x + $nums); |
||
67 | |||
68 | return array($low, $high); |
||
69 | } |
||
70 | |||
71 | function ipv4_range2cidr($baseip, $topip, $shortening=false) { |
||
72 | # (C) 2012 ViaThinkSoft |
||
73 | # Version 1.0 |
||
74 | # This function converts an IPv4 address block into valid CIDR blocks (There may be multiple blocks!) |
||
75 | |||
76 | $out = array(); |
||
77 | if (ipv4_cmp($baseip, $topip) > 0) return false; |
||
78 | while (ipv4_incomplete_ip2long($baseip)-1 != ipv4_incomplete_ip2long($topip)) { |
||
79 | $i = -1; |
||
80 | do { |
||
81 | $i++; |
||
82 | $range = ipv4_cidr2range($baseip, $i); |
||
83 | $l = $range[0]; |
||
84 | $t = $range[1]; |
||
85 | } while ((ipv4_cmp($l, $baseip) != 0) || (ipv4_cmp($t, $topip) > 0)); |
||
86 | |||
87 | # Shortening: Stroke ".0" at the end |
||
88 | if ($shortening) $baseip = ipv4_shortening($baseip); |
||
89 | |||
90 | $out[] = "$baseip/$i"; |
||
91 | $baseip = ipv4_add($t, 1); |
||
92 | } |
||
93 | return $out; |
||
94 | } |
||
95 | |||
96 | function ipv4_shortening($ip) { |
||
97 | # (C) 2012 ViaThinkSoft |
||
98 | # Version 1.0 |
||
99 | |||
100 | return preg_replace("|(\\.0{1,3}){0,3}\$|ismU", '', $ip); |
||
101 | } |
||
102 | |||
103 | function ipv4_add($baseip, $num) { |
||
104 | # (C) 2012 ViaThinkSoft |
||
105 | # Version 1.0 |
||
106 | |||
107 | return long2ip(ipv4_incomplete_ip2long($baseip) + $num); |
||
108 | } |
||
109 | |||
110 | function ipv4_sub($baseip, $num) { |
||
111 | # (C) 2012 ViaThinkSoft |
||
112 | # Version 1.0 |
||
113 | |||
114 | return long2ip(ipv4_incomplete_ip2long($baseip) - $num); |
||
115 | } |
||
116 | |||
117 | function ipv4_cmp($a, $b) { |
||
118 | # (C) 2012 ViaThinkSoft |
||
119 | # Version 1.0 |
||
120 | |||
121 | $a = ipv4_incomplete_ip2long($a); |
||
122 | $b = ipv4_incomplete_ip2long($b); |
||
123 | |||
124 | if ($a == $b) return 0; |
||
125 | if ($a < $b) return -1; |
||
126 | if ($a > $b) return 1; |
||
127 | } |
||
128 | |||
129 | function ipv4_in_cidr($haystack, $needle) { |
||
130 | # (C) 2012 ViaThinkSoft |
||
131 | # Version 1.1 |
||
132 | |||
133 | $x = explode('/', $haystack); |
||
134 | $ha = ipv4_cidr2range($x[0], $x[1]); |
||
135 | |||
136 | $x = explode('/', $needle); |
||
137 | if (!isset($x[1])) $x[1] = '32'; // single IP |
||
138 | $ne = ipv4_cidr2range($x[0], $x[1]); |
||
139 | |||
140 | $ha_low = ipv4_incomplete_ip2long($ha[0]); |
||
141 | $ha_hig = ipv4_incomplete_ip2long($ha[1]); |
||
142 | $ne_low = ipv4_incomplete_ip2long($ne[0]); |
||
143 | $ne_hig = ipv4_incomplete_ip2long($ne[1]); |
||
144 | |||
145 | # HA: low[ ]high |
||
146 | # NE: low[ ]high |
||
147 | |||
148 | return ($ne_low >= $ha_low) && ($ne_hig <= $ha_hig); |
||
149 | } |
||
150 | |||
151 | function ipv4_complete($short_form) { |
||
152 | # (C) 2012 ViaThinkSoft |
||
153 | # Version 1.0 |
||
154 | |||
155 | $short_form = trim($short_form); |
||
156 | if ($short_form == '') return '0.0.0.0'; |
||
157 | $c = substr_count($short_form, '.'); |
||
158 | if ($c > 3) return false; |
||
159 | if ($c == 3) return $short_form; |
||
160 | $c = substr_count($short_form, '.'); |
||
161 | $short_form .= str_repeat('.0', 3-$c); |
||
162 | return $short_form; |
||
163 | } |
||
164 | |||
165 | function ipv4_incomplete_ip2long($ip) { |
||
166 | # (C) 2012-2014 ViaThinkSoft |
||
167 | # Version 1.2 |
||
168 | |||
169 | # return sprintf('%u', ip2long(ipv4_complete($ip))); |
||
170 | return sprintf('%u', ip2long(ipv4_normalize($ip))); |
||
171 | } |
||
172 | |||
173 | // IMPORTANT! $cmp_ary[x]=y MUST HAVE x<=y ! |
||
174 | function ipv4_merge_address_blocks($data, $debug = false, $shortening = false) { |
||
175 | # (C) 2012-2013 ViaThinkSoft |
||
176 | # Version 2.2 |
||
177 | |||
178 | if ($debug !== false) $STARTZEIT = time(); |
||
179 | |||
180 | // 1. Convert IPs to numbers |
||
181 | |||
182 | $cmp_ary = array(); |
||
183 | foreach ($data as $a => &$b) { |
||
184 | $a = ipv4_incomplete_ip2long($a); |
||
185 | $b = ipv4_incomplete_ip2long($b); |
||
186 | |||
187 | $cmp_ary[$a] = $b; |
||
188 | unset($a); |
||
189 | unset($b); |
||
190 | } |
||
191 | |||
192 | // 2. Sort array |
||
193 | |||
194 | ksort($cmp_ary); |
||
195 | |||
196 | // 3. Merge the blocks in an intelligent way (and remove redundant blocks) |
||
197 | |||
198 | # Merge overlapping blocks |
||
199 | # [ ] |
||
200 | # [ ] -> [ ] |
||
201 | |||
202 | # Merge neighbor blocks |
||
203 | # [ ][ ] -> [ ] |
||
204 | |||
205 | # Remove redundant blocks |
||
206 | # [ ] -> [ ] |
||
207 | # [ ] |
||
208 | |||
209 | $merge_count = 0; |
||
210 | $redundant_deleted_count = 0; |
||
211 | $round_count = 0; |
||
212 | do { |
||
213 | if ($debug !== false) { |
||
214 | $LAUFZEIT = time() - $STARTZEIT; |
||
215 | echo $debug."Merging... $round_count rounds; merged $merge_count blocks; deleted $redundant_deleted_count redundant blocks; time: $LAUFZEIT seconds\r"; |
||
216 | } |
||
217 | |||
218 | $round_count++; |
||
219 | |||
220 | $clean = true; |
||
221 | |||
222 | foreach ($cmp_ary as $a => &$b) { |
||
223 | foreach ($cmp_ary as $x => &$y) { |
||
224 | // x in range [a+1..b+1] ? |
||
225 | if ($x<=$a) continue; |
||
226 | if ($x>$b+1) break; |
||
227 | |||
228 | // Merge |
||
229 | $clean = false; |
||
230 | if ($y>$b) { |
||
231 | $merge_count++; |
||
232 | $b = $y; |
||
233 | unset($cmp_ary[$x]); |
||
234 | } else { |
||
235 | $redundant_deleted_count++; |
||
236 | unset($cmp_ary[$x]); |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | } while (!$clean); |
||
241 | |||
242 | if ($debug !== false) { |
||
243 | $LAUFZEIT = time() - $STARTZEIT; |
||
244 | echo $debug."Merge completed. $round_count rounds; merged $merge_count blocks; deleted $redundant_deleted_count redundant blocks; time: $LAUFZEIT seconds\n"; |
||
245 | } |
||
246 | |||
247 | // 4. Convert back to IPs |
||
248 | |||
249 | $out_ary = array(); |
||
250 | foreach ($cmp_ary as $a => &$b) { |
||
251 | $a = long2ip($a); |
||
252 | $b = long2ip($b); |
||
253 | if ($shortening) { |
||
254 | $a = ipv4_shortening($a); |
||
255 | $b = ipv4_shortening($b); |
||
256 | } |
||
257 | $out_ary[$a] = $b; |
||
258 | } |
||
259 | |||
260 | return $out_ary; |
||
261 | } |
||
262 | |||
263 | function ipv4_merge_arrays($data_a, $data_b) { |
||
264 | # (C) 2012 ViaThinkSoft |
||
265 | # Version 1.2 |
||
266 | |||
267 | $normalized_data_a = array(); |
||
268 | foreach ($data_a as $from => &$to) { |
||
269 | $normalized_data_a[ipv4_normalize($from)] = ipv4_normalize($to); |
||
270 | } |
||
271 | |||
272 | $normalized_data_b = array(); |
||
273 | foreach ($data_b as $from => &$to) { |
||
274 | $normalized_data_b[ipv4_normalize($from)] = ipv4_normalize($to); |
||
275 | } |
||
276 | |||
277 | $data = array(); |
||
278 | |||
279 | foreach ($normalized_data_a as $from => &$to) { |
||
280 | if (isset($normalized_data_b[$from])) { |
||
281 | $data[$from] = ipv4_max($to, $normalized_data_b[$from]); |
||
282 | } else { |
||
283 | $data[$from] = $to; |
||
284 | } |
||
285 | } |
||
286 | |||
287 | foreach ($normalized_data_b as $from => &$to) { |
||
288 | if (!isset($normalized_data_a[$from])) { |
||
289 | $data[$from] = $to; |
||
290 | } |
||
291 | } |
||
292 | |||
293 | return $data; |
||
294 | } |
||
295 | |||
296 | function ipv4_valid($ip) { |
||
297 | # (C) 2012 ViaThinkSoft |
||
298 | # Version 1.0 |
||
299 | |||
300 | # return ipv4_incomplete_ip2long($ip) !== false; |
||
301 | return ip2long($ip) !== false; |
||
302 | } |
||
303 | |||
304 | function ipv4_normalize($ip) { |
||
305 | # (C) 2012-2013 ViaThinkSoft |
||
306 | # Version 1.1.1 |
||
307 | |||
308 | # Example: |
||
309 | # 100.010.001.000 -> 100.10.1.0 |
||
310 | |||
311 | $ip = ipv4_complete($ip); |
||
312 | if (!$ip) return false; |
||
313 | |||
314 | # ip2long buggy: 001.0.0.0 is not accepted |
||
315 | ## $cry = explode('.', $ip); |
||
316 | ## $cry[0] = preg_replace('@^0+@', '', $cry[0]); if ($cry[0] == '') $cry[0] = '0'; |
||
317 | ## $cry[1] = preg_replace('@^0+@', '', $cry[1]); if ($cry[1] == '') $cry[1] = '0'; |
||
318 | ## $cry[2] = preg_replace('@^0+@', '', $cry[2]); if ($cry[2] == '') $cry[2] = '0'; |
||
319 | ## $cry[3] = preg_replace('@^0+@', '', $cry[3]); if ($cry[3] == '') $cry[3] = '0'; |
||
320 | ## $ip = implode('.', $cry); |
||
321 | ## return $ip; |
||
322 | |||
323 | return preg_replace('@^0{0,2}([0-9]{1,3})\.0{0,2}([0-9]{1,3})\.0{0,2}([0-9]{1,3})\.0{0,2}([0-9]{1,3})$@', '\\1.\\2.\\3.\\4', $ip); |
||
324 | } |
||
325 | |||
326 | function ipv4_expand($ip) { |
||
327 | # (C) 2012 ViaThinkSoft |
||
328 | # Version 1.0 |
||
329 | |||
330 | # Example: |
||
331 | # 100.10.1.0 -> 100.010.001.000 |
||
332 | |||
333 | $ip = ipv4_complete($ip); |
||
334 | if (!$ip) return false; |
||
335 | |||
336 | $cry = explode('.', $ip); |
||
337 | $cry[0] = str_pad($cry[0], 3, '0', STR_PAD_LEFT); |
||
338 | $cry[1] = str_pad($cry[1], 3, '0', STR_PAD_LEFT); |
||
339 | $cry[2] = str_pad($cry[2], 3, '0', STR_PAD_LEFT); |
||
340 | $cry[3] = str_pad($cry[3], 3, '0', STR_PAD_LEFT); |
||
341 | return implode('.', $cry); |
||
342 | } |
||
343 | |||
344 | function ipv4_min($ip_a, $ip_b) { |
||
345 | # (C) 2012 ViaThinkSoft |
||
346 | # Version 1.0 |
||
347 | |||
348 | if (ipv4_cmp($ip_a, $ip_b) == -1) { |
||
349 | return $ip_a; |
||
350 | } else { |
||
351 | return $ip_b; |
||
352 | } |
||
353 | } |
||
354 | |||
355 | function ipv4_max($ip_a, $ip_b) { |
||
356 | # (C) 2012 ViaThinkSoft |
||
357 | # Version 1.0 |
||
358 | |||
359 | if (ipv4_cmp($ip_a, $ip_b) == 1) { |
||
360 | return $ip_a; |
||
361 | } else { |
||
362 | return $ip_b; |
||
363 | } |
||
364 | } |
||
365 | |||
366 | function ipv4_ipcount($data) { |
||
367 | # (C) 2012 ViaThinkSoft |
||
368 | # Version 1.0 |
||
369 | |||
370 | $cnt = 0; |
||
371 | |||
372 | foreach ($data as $from => &$to) { |
||
373 | $cnt += ipv4_incomplete_ip2long($to) - ipv4_incomplete_ip2long($from); |
||
374 | } |
||
375 | |||
376 | return $cnt; |
||
377 | } |
||
378 | |||
379 | function ipv4_read_file($file) { |
||
380 | # (C) 2012 ViaThinkSoft |
||
381 | # Version 1.0 |
||
382 | |||
383 | $data = array(); |
||
384 | |||
385 | $lines = file($file); |
||
386 | foreach ($lines as &$line) { |
||
387 | $rng = ipv4_line2range($line); |
||
388 | $data[$rng[0]] = $rng[1]; |
||
389 | } |
||
390 | |||
391 | return $data; |
||
392 | } |
||
393 | |||
394 | function ipv4_line2range($line) { |
||
395 | # (C) 2012 ViaThinkSoft |
||
396 | # Version 1.0 |
||
397 | |||
398 | $line = trim($line); |
||
399 | |||
400 | if (strpos($line, '/') !== false) { |
||
401 | $rng = ipv4_cidr2range($line); |
||
402 | } else { |
||
403 | $rng = explode('-', $line); |
||
51 | daniel-mar | 404 | $rng[0] = ipv4_normalize(trim($rng[0])); |
405 | $rng[1] = isset($rng[1]) ? ipv4_normalize(trim($rng[1])) : $rng[0]; |
||
2 | daniel-mar | 406 | } |
407 | |||
408 | return $rng; |
||
409 | } |
||
410 | |||
411 | # --- New 16,12,12 |
||
412 | |||
413 | define('IPV4_BITS', 32); |
||
414 | |||
415 | function ipv4_distance($ipOrCIDR_Searchterm, $ipOrCIDR_Candidate) { |
||
416 | $ary = ipv4_cidr_split($ipOrCIDR_Searchterm); |
||
417 | $ip = $ary[0]; |
||
418 | |||
419 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
||
420 | return false; |
||
421 | } |
||
422 | |||
423 | $ary = ipv4_cidr_split($ipOrCIDR_Candidate); |
||
424 | $ip = $ary[0]; |
||
425 | $cidr_bits = $ary[1]; |
||
426 | if ($cidr_bits > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
427 | if (!is_numeric($cidr_bits)) return false; |
||
428 | |||
429 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false) { |
||
430 | return false; |
||
431 | } |
||
432 | |||
433 | $x = ipv4_trackdown($ipOrCIDR_Searchterm); |
||
434 | |||
435 | if (ipv4_in_cidr($x[0], $ip.'/'.$cidr_bits)) { |
||
436 | $ary = ipv4_cidr_split($x[0]); |
||
437 | $cidr_bits2 = $ary[1]; |
||
438 | if ($cidr_bits2 > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
439 | return $cidr_bits2-$cidr_bits; |
||
440 | } |
||
441 | |||
442 | $i = 0; |
||
443 | $max = false; |
||
444 | foreach ($x as &$y) { |
||
445 | if (ipv4_in_cidr($ip.'/'.$cidr_bits, $y)) { |
||
446 | $max = $i; |
||
447 | } |
||
448 | $i++; |
||
449 | } |
||
450 | |||
451 | return $max; |
||
452 | } |
||
453 | |||
454 | function ipv4_cidr_split($ipOrCIDR) { |
||
455 | $ary = explode('/', $ipOrCIDR, 2); |
||
456 | $cidr_bits = isset($ary[1]) ? $ary[1] : IPV4_BITS; |
||
457 | if ($cidr_bits > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
458 | if (!is_numeric($cidr_bits)) return false; |
||
459 | $ip = $ary[0]; |
||
460 | return array($ip, $cidr_bits); |
||
461 | } |
||
462 | |||
463 | function ipv4_equals($ipOrCIDRA, $ipOrCIDRB) { |
||
464 | return ipv4_normalize_range($ipOrCIDRA) == ipv4_normalize_range($ipOrCIDRB); |
||
465 | } |
||
466 | |||
467 | function ipv4_cidr_min_ip($ipOrCIDR) { |
||
468 | $ary = ipv4_cidr_split($ipOrCIDR); |
||
469 | $ipOrCIDR = $ary[0]; |
||
470 | $cidr_bits = $ary[1]; |
||
471 | if ($cidr_bits > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
472 | if (!is_numeric($cidr_bits)) return false; |
||
473 | |||
474 | $m = ip2bin($ipOrCIDR); |
||
475 | $m = substr($m, 0, $cidr_bits) . str_repeat('0', IPV4_BITS-$cidr_bits); |
||
476 | |||
477 | return bin2ip($m); |
||
478 | } |
||
479 | |||
480 | function ipv4_cidr_max_ip($ipOrCIDR) { |
||
481 | $ary = ipv4_cidr_split($ipOrCIDR); |
||
482 | $ipOrCIDR = $ary[0]; |
||
483 | $cidr_bits = $ary[1]; |
||
484 | if ($cidr_bits > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
485 | if (!is_numeric($cidr_bits)) return false; |
||
486 | |||
487 | $m = ip2bin($ipOrCIDR); |
||
488 | $m = substr($m, 0, $cidr_bits) . str_repeat('1', IPV4_BITS-$cidr_bits); |
||
489 | |||
490 | return bin2ip($m); |
||
491 | } |
||
492 | |||
493 | function ipv4_normalize_range($ipOrCIDR) { |
||
494 | $ary = ipv4_cidr_split($ipOrCIDR); |
||
495 | $ipOrCIDR = $ary[0]; |
||
496 | $cidr_bits = $ary[1]; |
||
497 | if ($cidr_bits > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
498 | if (!is_numeric($cidr_bits)) return false; |
||
499 | |||
500 | $m = ip2bin($ipOrCIDR); |
||
501 | $m = substr($m, 0, $cidr_bits) . str_repeat('0', IPV4_BITS-$cidr_bits); |
||
502 | |||
503 | return bin2ip($m) . '/' . $cidr_bits; |
||
504 | } |
||
505 | |||
506 | function ipv4_trackdown($ipOrCIDR) { |
||
507 | $ary = ipv4_cidr_split($ipOrCIDR); |
||
508 | $ipOrCIDR = $ary[0]; |
||
509 | $cidr_bits = $ary[1]; |
||
510 | if ($cidr_bits > IPV4_BITS) return false; // throw new Exception('CIDR bits > '.IPV4_BITS); |
||
511 | if (!is_numeric($cidr_bits)) return false; |
||
512 | |||
513 | $out = array(); |
||
514 | $m = ip2bin($ipOrCIDR); |
||
515 | |||
516 | for ($i=$cidr_bits; $i>=0; $i--) { |
||
517 | $m = substr($m, 0, $i) . str_repeat('0', IPV4_BITS-$i); |
||
518 | $out[] = bin2ip($m) . '/' . $i; |
||
519 | } |
||
520 | |||
521 | return $out; |
||
522 | } |
||
523 | |||
524 | # --- |
||
525 | |||
526 | if (!function_exists('ip2bin')) { |
||
527 | function ip2bin($ip) { |
||
528 | # Source: http://php.net/manual/en/function.ip2long.php#104163 |
||
529 | # modified by VTS |
||
530 | |||
531 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) { |
||
532 | $iplong = ip2long($ip); |
||
533 | assert($iplong !== false); |
||
534 | return base_convert((string)$iplong, 10, 2); |
||
535 | } |
||
536 | if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { |
||
537 | return false; |
||
538 | } |
||
539 | if (($ip_n = inet_pton($ip)) === false) { |
||
540 | return false; |
||
541 | } |
||
542 | $bits = 15; // 16 x 8 bit = 128bit (ipv6) |
||
543 | $ipbin = ''; # added by vts to avoid warning |
||
544 | while ($bits >= 0) { |
||
545 | $bin = sprintf('%08b', (ord($ip_n[$bits]))); |
||
546 | $ipbin = $bin.$ipbin; |
||
547 | $bits--; |
||
548 | } |
||
549 | return $ipbin; |
||
550 | } |
||
551 | } |
||
552 | |||
553 | if (!function_exists('bin2ip')) { |
||
554 | function bin2ip($bin) { |
||
555 | # Source: http://php.net/manual/en/function.ip2long.php#104163 |
||
556 | # modified by VTS |
||
557 | |||
558 | if (strlen($bin) <= 32) { // 32bits (ipv4) |
||
559 | $iplong = base_convert($bin, 2, 10); |
||
560 | return long2ip(intval($iplong)); |
||
561 | } |
||
562 | if (strlen($bin) != 128) { |
||
563 | return false; |
||
564 | } |
||
46 | daniel-mar | 565 | //$bin = str_pad($bin, 128, '0', STR_PAD_LEFT); |
2 | daniel-mar | 566 | $bits = 0; |
567 | $ipv6 = ''; # added by vts to avoid warning |
||
568 | while ($bits <= 7) { |
||
569 | $bin_part = substr($bin,($bits*16),16); |
||
570 | $ipv6 .= dechex(bindec($bin_part)) . ':'; |
||
571 | $bits++; |
||
572 | } |
||
573 | return inet_ntop(inet_pton(substr($ipv6, 0, -1))); |
||
574 | } |
||
575 | } |
||
576 | |||
577 | # --- TEST |
||
578 | |||
579 | /* |
||
580 | assert(ipv4_normalize('100.010.001.000') == '100.10.1.0'); |
||
581 | assert(ipv4_normalize('100.010.01.000') == '100.10.1.0'); |
||
582 | assert(ipv4_normalize('100.10.001.000') == '100.10.1.0'); |
||
583 | assert(ipv4_normalize('1.010.001.000') == '1.10.1.0'); |
||
584 | assert(ipv4_normalize('1.10.001.000') == '1.10.1.0'); |
||
585 | |||
586 | assert(ipv4_distance('192.168.0.0/16', '192.168.64.0/18') == -2); |
||
587 | assert(ipv4_distance('192.168.0.0/17', '192.168.64.0/18') == -1); |
||
588 | assert(ipv4_distance('192.168.64.0/18', '192.168.64.0/18') == 0); |
||
589 | assert(ipv4_distance('192.168.64.0/19', '192.168.64.0/18') == 1); |
||
590 | assert(ipv4_distance('192.168.64.0/20', '192.168.64.0/18') == 2); |
||
591 | |||
592 | assert(ipv4_distance('192.168.69.202/31', '192.168.69.200/31') === false); |
||
593 | assert(ipv4_distance('192.168.69.201/32', '192.168.69.200/32') === false); |
||
594 | assert(ipv4_distance('192.168.69.201', '192.168.69.200') === false); |
||
595 | */ |
||
596 | |||
597 | /* |
||
598 | $test = '192.168.69.123'; |
||
599 | $x = ipv4_trackdown($test); |
||
600 | foreach ($x as &$cidr) { |
||
601 | $min = ipv4_cidr_min_ip($cidr); |
||
602 | $max = ipv4_cidr_max_ip($cidr); |
||
603 | echo "$cidr ($min - $max)\n"; |
||
604 | } |
||
605 | */ |
||
606 | |||
607 | |||
608 | |||
609 | |||
610 | function ipv4_sort($ary) { |
||
611 | $f = array(); |
||
612 | foreach ($ary as $c) { |
||
613 | $a = explode('/', $c); |
||
614 | $ip = $a[0]; |
||
615 | $bits = isset($a[1]) ? $a[1] : 32; |
||
616 | |||
617 | $d = ip2bin($ip); |
||
618 | |||
619 | # ord('*') must be smaller than ord('0') |
||
620 | $d = substr($d, 0, $bits).str_repeat('*', 32-$bits); |
||
621 | |||
622 | $f[$d] = $c; |
||
623 | } |
||
624 | |||
625 | return $f; |
||
626 | } |
||
627 | |||
628 | function ipv4_make_tree($ary) { |
||
629 | $ary = ipv4_sort($ary); |
||
630 | |||
631 | if (count($ary) == 0) return array(); |
||
632 | |||
633 | $sub_begin = ''; |
||
634 | $sub_begin_ip = ''; |
||
635 | foreach ($ary as $n => $d) { |
||
636 | $sub_begin = substr($n, 0, strpos($n, '*')); |
||
637 | $sub_begin_ip = $d; |
||
638 | unset($ary[$n]); |
||
639 | break; |
||
640 | } |
||
641 | |||
642 | $sub = array(); |
||
643 | $nonsub = array(); |
||
644 | foreach ($ary as $n => $d) { |
||
645 | if (substr($n, 0, strlen($sub_begin)) == $sub_begin) { |
||
646 | $sub[$n] = $d; |
||
647 | } else { |
||
648 | $nonsub[$n] = $d; |
||
649 | } |
||
650 | } |
||
651 | |||
652 | $out = array(); |
||
653 | $out[$sub_begin_ip] = ipv4_make_tree($sub); |
||
654 | |||
655 | $a = ipv4_make_tree($nonsub); |
||
656 | |||
657 | $out = array_merge($out, $a); |
||
658 | |||
659 | return $out; |
||
660 | } |
||
661 |