-
Notifications
You must be signed in to change notification settings - Fork 0
/
toTree.php
118 lines (95 loc) · 2.67 KB
/
toTree.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
<?php
abstract class Node
{
public $prefix;
public $mask;
public function isParentOf($node): bool
{
return ($node->prefix & $this->mask) === $this->prefix;
}
}
class Branch extends Node
{
public $left;
public $right;
public static function commonParent(Node $child1, Node $child2): self
{
$node = new self();
$node->mask = 0x0;
while (($child1->prefix & $node->mask) === ($child2->prefix & $node->mask)) {
$node->mask = ($node->mask >> 1) | 0x80000000;
}
$node->mask = $node->mask << 1;
$node->prefix = $child1->prefix & $node->mask;
$node->left = $child1;
$node->right = $child2;
return $node;
}
}
class Leaf extends Node
{
public $type;
public static function fromCidr(string $cidr, string $type): self
{
$leaf = new self();
$leaf->type = $type;
$parts = explode('/', $cidr);
$leaf->prefix = ip2long($parts[0]);
$shift = 32 - (int)$parts[1];
$leaf->mask = (0xffffffff >> $shift) << $shift;
return $leaf;
}
}
$tree = null;
function add_country(string $code) {
global $tree;
$handle = fopen("ips/ipv4/" . $code, "r");
if (!$handle) {
// err
}
while (($line = fgets($handle)) !== false) {
if ($line{0} == '#') {
continue;
}
$leaf = Leaf::fromCidr($line, strtoupper($code));
if (!$tree) {
$tree = $leaf;
continue;
}
if (!$tree->isParentOf($leaf)) {
$tree = Branch::commonParent($tree, $leaf);
continue;
}
$node = $tree;
while (1) {
if (is_a($node, Leaf::class)) {
break;
} elseif ($node->left->isParentOf($leaf)) {
$node = $node->left;
} elseif ($node->right->isParentOf($leaf)) {
$node = $node->right;
} else {
break;
}
}
$newLeft = Branch::commonParent($node->left, $leaf);
$newRight = Branch::commonParent($node->right, $leaf);
if ($newLeft->prefix > $newRight->prefix) {
$node->left = $newLeft;
} else {
$node->right = $newRight;
}
}
fclose($handle);
}
function ptree(Node $node): string {
$str = pack("N", $node->prefix) . pack("N", $node->mask);
if (is_a($node, Leaf::class)) {
return $str . "\xff\xff" . $node->type;
}
$data = ptree($node->left) . ptree($node->right);
return $str . pack("N", strlen($data)) . $data;
}
add_country('ru');
add_country('by');
file_put_contents("trees/ipv4.bin", ptree($tree));