-
Notifications
You must be signed in to change notification settings - Fork 2
/
cidr-to-ip.php
165 lines (122 loc) · 3.19 KB
/
cidr-to-ip.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/php
<?php
// Preset PHP settings
error_reporting(E_ALL);
ini_set('display_errors', 0);
date_default_timezone_set('UTC');
// Define root directory
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', __DIR__ . DS);
if (!isset($argv)) {
die('ERROR: Please run this script in command line.');
}
if (!isset($argv[1])) {
die('ERROR: Please provide the absolute path to input CSV file.');
}
if (!file_exists($argv[1])) {
die('ERROR: The input CSV file is not found.');
}
if (!isset($argv[2])) {
die('ERROR: Please provide the absolute path to output CSV file.');
}
if (!is_writable(dirname($argv[2]))) {
die('ERROR: The output directory is not writable.');
}
$file = fopen($argv[1], 'r');
if (!$file) {
die('ERROR: Failed to read the input CSV.');
}
@file_put_contents($argv[2], '');
while (!feof($file)) {
$data = fgetcsv($file);
// CIDR
if (strpos($data[0], '/') !== false) {
list($ip, $cidr) = explode('/', $data[0]);
// IPv4
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$rows = cidrToRange($data[0]);
unset($data[0]);
@file_put_contents($argv[2], '"' . implode('","', array_merge($rows, $data)) . "\"\n", FILE_APPEND);
continue;
}
// IPv6
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
}
continue;
}
// IPv4 Range
if (filter_var($data[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) && filter_var($data[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$rows = rangeToCIDR($data[0], $data[1]);
unset($data[0]);
unset($data[1]);
foreach ($rows as $row) {
@file_put_contents($argv[2], '"' . implode('","', array_merge([$row], $data)) . "\"\n", FILE_APPEND);
}
continue;
}
// IPv6 Range
if (filter_var($data[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && filter_var($data[1], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
}
}
fclose($file);
function cidrToRange($cidr)
{
list($ip, $prefix) = explode('/', $cidr);
$ipStart = long2ip((ip2long($ip)) & ((-1 << (32 - (int) $prefix))));
$total = 1 << (32 - $prefix);
$ipStartLong = sprintf('%u', ip2long($ipStart));
$ipEndLong = $ipStartLong + $total - 1;
if ($ipEndLong > 4294967295) {
$ipEndLong = 4294967295;
}
$ipLast = long2ip($ipEndLong);
return [$ipStart, $ipLast, $total];
}
function iMask($s)
{
return base_convert((pow(2, 32) - pow(2, (32 - $s))), 10, 16);
}
function iMaxBlock($ibase, $tbit)
{
while ($tbit > 0) {
$im = hexdec(iMask($tbit - 1));
$imand = $ibase & $im;
if ($imand != $ibase) {
break;
}
--$tbit;
}
return $tbit;
}
function rangeToCIDR($ipStart, $ipEnd)
{
$s = explode('.', $ipStart);
$start = '';
$dot = '';
while (list($key, $val) = each($s)) {
$start = sprintf('%s%s%d', $start, $dot, $val);
$dot = '.';
}
$end = '';
$dot = '';
$e = explode('.', $ipEnd);
while (list($key, $val) = each($e)) {
$end = sprintf('%s%s%d', $end, $dot, $val);
$dot = '.';
}
$start = ip2long($start);
$end = ip2long($end);
$result = [];
while ($end >= $start) {
$maxSize = iMaxBlock($start, 32);
$x = log($end - $start + 1) / log(2);
$maxDiff = floor(32 - floor($x));
$ip = long2ip($start);
if ($maxSize < $maxDiff) {
$maxSize = $maxDiff;
}
array_push($result, "$ip/$maxSize");
$start += pow(2, (32 - $maxSize));
}
return $result;
}