Subversion Repositories vgwhois

Rev

Rev 5 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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