-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPv6Calculator.php
353 lines (303 loc) · 9.25 KB
/
IPv6Calculator.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* Created by PhpStorm.
* User: adriar
* Date: 7/2/15
* Time: 1:38 PM
*/
class IPv6Calculator
{
const ADDRESS_EMPTY = 128;
const ADDRESS_INVALID = 129;
const ADDRESS_NOT_STRING = 130;
const ADDRESS_LONG_TOO = 131;
const SUBNET_UNDEFINED = 256;
const ADDRESS_NOT_SEGMENTED = 132;
const ADDRESS_SEGMENTS_COUNT = 133;
const QUERY_EMPTY = 64;
const QUERY_INVALID = 65;
const QUERY_TOO_LONG = 66;
const PREFIX_NOT_NUMERIC = 197;
const PREFIX_EMPTY = 196;
const PREFIX_RANGE = 198;
const PREFIX_INVALID = 199;
protected $errors = array();
protected $segments = array();
protected $ip;
protected $prefix;
protected $network_range = array();
/**
* @param $query
* @return bool|int
* @throws Exception
*/
public function calc($query)
{
$this->reset();
if (!$this->isValidQuery($query)) {
return static::QUERY_INVALID;
}
if (!$this->isValidAddress()) {
return $this->errors[] = static::ADDRESS_INVALID;
}
if (!$this->isValidPrefix()) {
return $this->errors[] = static::PREFIX_INVALID;
}
return $this->calcSubnet();
}
/**
* Reset all properties for this class
*
* @return $this
*/
public function reset() {
$this->errors = array();
$this->segments = array();
$this->ip = null;
$this->prefix = null;
$this->network_range = array();
return $this;
}
/**
* Returns an string with the logged error messages
*
* @return string
* @throws Exception
*/
public function getErrorMessages()
{
$msg = '';
foreach ($this->errors as $code) {
$msg .= $this->getErrorMessage($code) . PHP_EOL;
}
return $msg;
}
/**
* Returns an array with the calculated NetworkRange
* @return array
*/
public function getNetworkRange() {
return $this->network_range;
}
/**
* Returns the error message string based on error ID
*
* @param $id
* @return mixed
* @throws Exception
*/
protected function getErrorMessage($id)
{
$messages = array(
static::QUERY_EMPTY => 'Query string is empty',
static::QUERY_INVALID => 'Query string is invalid',
static::QUERY_TOO_LONG => 'Query string is too long',
static::ADDRESS_INVALID => 'IP address is invalid',
static::ADDRESS_EMPTY => 'IP address value is empty',
static::ADDRESS_NOT_STRING => 'IP address value is not a string',
static::ADDRESS_LONG_TOO => 'IP address string is too long',
static::ADDRESS_NOT_SEGMENTED => 'IP address has not segments',
static::ADDRESS_SEGMENTS_COUNT => 'IP address has too many segments',
static::PREFIX_EMPTY => 'Prefix is empty',
static::PREFIX_NOT_NUMERIC => 'Prefix is not numeric',
static::PREFIX_RANGE => 'Prefix is not a valid range',
static::PREFIX_INVALID => 'Prefix is invalid'
);
if (isset($messages[$id])) {
return $messages[$id];
}
throw new \Exception('Error messages is not defined');
}
/**
* Validates basic format for the submitted query string and sets IP and Prefix property values
*
* @param $string
* @return bool
*/
protected function isValidQuery($string)
{
if (empty($string)) {
$this->errors[] = static::QUERY_EMPTY;
return false;
} elseif (strlen($string) > (4 * 8) + 11) {
$this->errors[] = static::QUERY_TOO_LONG;
return false;
}
$list = explode('/', $string);
if (count($list) !== 2) {
$this->errors[] = static::QUERY_INVALID;
return false;
}
$this->ip = $list[0];
$this->prefix = $list[1];
return true;
}
/**
* Validates the IP property value if it is a valid IPv6 string
*
* @return bool
* @throws Exception
*/
protected function isValidAddress()
{
$string = $this->ip;
if (empty($string)) {
$this->errors[] = static::ADDRESS_EMPTY;
return false;
} elseif (!is_string($string)) {
$this->errors[] = static::ADDRESS_NOT_STRING;
return false;
} elseif (strlen($string) > ((4 * 8) + 7)) {
$this->errors[] = static::ADDRESS_LONG_TOO;
return false;
}
if ( $this->validateAddressFormat($string) ) {
$this->ip = implode(':', $this->segments);
return true;
}
return false;
}
/**
* Validates the IP address format and splits in segments into groups
*
* @param $address
* @return bool
* @throws Exception
*/
protected function validateAddressFormat($address)
{
$segments = $this->splitAddressString($address);
if ($segments === false) {
return false;
} elseif (!is_array($segments)) {
throw new \Exception('Split address did not return an valid format');
}
$this->segments = $segments;
return true;
}
/**
* Validates the Prefix property value if it is a valid CIDR notation
*
* @return bool
*/
protected function isValidPrefix()
{
if (empty($this->prefix)) {
$this->errors[] = static::PREFIX_EMPTY;
return false;
} elseif (!is_numeric($this->prefix)) {
$this->errors[] = static::PREFIX_NOT_NUMERIC;
return false;
} elseif ($this->prefix < 1 || $this->prefix > 128) {
$this->errors[] = static::PREFIX_RANGE;
return false;
}
return true;
}
/**
* Calculates the network range based on the IP and Prefix
*
* @return bool
* @throws Exception
*/
protected function calcSubnet() {
if ( !is_numeric($this->prefix) ) {
throw new \Exception('Prefix is not available!');
}
$group = (int) floor($this->prefix / 16);
$mod = $this->prefix % 16;
$segment = $this->segments[$group];
$seg_bin = str_pad(base_convert($segment, 16, 2), 16, '0', STR_PAD_LEFT);
$seg_sub = substr($seg_bin, 0, $mod);
$hex_min = base_convert(str_pad($seg_sub, 16, '0', STR_PAD_RIGHT), 2, 16);
$hex_max = base_convert(str_pad($seg_sub, 16, '1', STR_PAD_RIGHT), 2, 16);
$segments_min = $segments_max = $this->segments;
$segments_min[$group] = str_pad($hex_min, 4, '0', STR_PAD_LEFT);
$segments_max[$group] = str_pad($hex_max, 4, '0', STR_PAD_LEFT);
for ($i = ($group+1); $i < 8; $i++) {
$segments_min[$i] = '0000';
$segments_max[$i] = 'ffff';
}
$this->network_range['min'] = implode(':', $segments_min);
$this->network_range['max'] = implode(':', $segments_max);
$this->network_range['hosts'] = number_format(pow(2, (128-$this->prefix)), 0);
return true;
}
/**
* Splits the Address String in 8 network groups
* @param $string
* @return array|bool
* @throws Exception
*/
protected function splitAddressString($string)
{
$segments = explode(':', $string);
if (empty($segments)) {
$this->errors[] = static::ADDRESS_NOT_SEGMENTED;
return false;
} elseif (count($segments) > 8) {
$this->errors[] = static::ADDRESS_SEGMENTS_COUNT;
return false;
}
$this->normalizeSegments($segments);
return $segments;
}
/**
* Normalizes given array into 8 complete groups with full 4 char hex string
*
* @param $segments
* @return mixed
*/
protected function normalizeSegments(&$segments)
{
$this->expandSegments($segments);
foreach ($segments as $i => $segment) {
if (strlen($segment) !== 4) {
$segments[$i] = str_pad($segment, 4, '0', STR_PAD_LEFT);
}
}
return $segments;
}
/**
* Expands a given array and segments into 8 groups
*
* @param $segments
* @return array|bool
* @throws Exception
*/
protected function expandSegments(&$segments)
{
$t = 8;
if (($c = count($segments)) === $t) {
return true;
}
$diff = $t - $c;
$newSegments = array();
foreach ($segments as $i => $segment) {
if (empty($segment)) {
for ($i = 0; $i <= $diff; $i++) {
$newSegments[] = '0000';
}
continue;
}
$newSegments[] = $segment;
}
if (count($newSegments) !== 8) {
throw new \Exception('Expanding segments failed for unknown reason');
}
$segments = $newSegments;
unset($newSegments);
return $segments;
}
}
// lets run this thing
$calc = new IPv6Calculator();
if ( isset($_REQUEST['cidr']) ) {
if ($calc->calc($_REQUEST['cidr']) === true) {
var_dump($calc->getNetworkRange());
} else {
echo $calc->getErrorMessages();
}
} else {
echo "No Request received";
}