-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathIPBlock.php
More file actions
561 lines (489 loc) · 13.2 KB
/
Copy pathIPBlock.php
File metadata and controls
561 lines (489 loc) · 13.2 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<?php
declare(strict_types=1);
/**
* Licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE file.
*
* @author Rémi Lanvin <remi@cloudconnected.fr>
*
* @see https://github.com/rlanvin/php-ip
*/
namespace PhpIP;
/**
* Base class to manipulate CIDR block (aka "networks").
*/
abstract class IPBlock implements \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* @var IP
*/
protected $given_ip;
/**
* @var IP
*/
protected $first_ip;
/**
* @var IP
*/
protected $last_ip;
/**
* @var int
*/
protected $prefix;
/**
* @var IP
*/
protected $mask;
/**
* @var IP
*/
protected $delta;
/**
* @var string Numeric string
*/
protected $nb_addresses;
/**
* @var string Either "IPv4" or "IPv6"
*/
protected static $ip_class;
/**
* Return netmask.
*
* @return IP
*/
public function getMask(): IP
{
if ($this->mask === null) {
if ($this->prefix == 0) {
$this->mask = new static::$ip_class(0);
} else {
$max_int = gmp_init(static::$ip_class::MAX_INT);
$mask = gmp_shiftl($max_int, static::$ip_class::NB_BITS - $this->prefix);
$mask = gmp_and($mask, $max_int); // truncate to 128 bits only
$this->mask = new static::$ip_class($mask);
}
}
return $this->mask;
}
/**
* Return delta to last IP address.
*
* @return IP
*/
public function getDelta(): IP
{
if ($this->delta === null) {
if ($this->prefix == 0) {
$this->delta = new static::$ip_class(static::$ip_class::MAX_INT);
} else {
$this->delta = new static::$ip_class(gmp_sub(gmp_shiftl(1, static::$ip_class::NB_BITS - $this->prefix), 1));
}
}
return $this->delta;
}
/**
* @param mixed $ip
* @param mixed $prefix
*
* @return IPv4Block|IPv6Block
*/
public static function create($ip, $prefix = ''): IPBlock
{
try {
return new IPv4Block($ip, $prefix);
} catch (\InvalidArgumentException $e) {
// do nothing
}
try {
return new IPv6Block($ip, $prefix);
} catch (\InvalidArgumentException $e) {
// do nothing
}
throw new \InvalidArgumentException("$ip does not appear to be an IPv4 or IPv6 block");
}
/**
* Accepts a CIDR string (e.g. 192.168.0.0/24) or an IP and a prefix as
* two separate parameters.
*
* @param mixed $ip_or_cidr IP or CIDR string
* @param mixed $prefix int (optional) The "slash" part
*/
public function __construct($ip_or_cidr, $prefix = '')
{
$this->given_ip = $ip_or_cidr;
if (is_string($ip_or_cidr) && strpos($ip_or_cidr, '/') !== false) {
list($this->given_ip, $prefix) = explode('/', $ip_or_cidr, 2);
}
if (!$this->given_ip instanceof IP) {
$this->given_ip = new static::$ip_class($this->given_ip);
}
$this->prefix = $this->checkPrefix($prefix);
$this->first_ip = $this->given_ip->bit_and($this->getMask());
$this->last_ip = $this->first_ip->bit_or($this->getDelta());
}
/**
* @return string
*/
public function __toString(): string
{
return (string) $this->first_ip.'/'.$this->prefix;
}
/**
* Returns given IP.
*
* For example 192.168.48.7 for 192.168.48.7/24
*
* @return IP
*/
public function getGivenIp(): IP
{
return $this->given_ip;
}
/**
* Returns the prefix (the slash part).
*
* @return int
*/
public function getPrefix(): int
{
return $this->prefix;
}
/**
* @return int
*/
public function getMaxPrefix(): int
{
return static::$ip_class::NB_BITS;
}
/**
* @return int
*/
public function getVersion(): int
{
return static::$ip_class::IP_VERSION;
}
/**
* @param int $value
*
* @return IPBlock
*/
public function plus(int $value): IPBlock
{
if ($value < 0) {
return $this->minus(-1 * $value);
}
if ($value == 0) {
return clone $this;
}
// check boundaries
try {
$first_ip = $this->first_ip->plus(gmp_mul($value, $this->getNbAddresses()));
return new static(
$first_ip,
$this->prefix
);
} catch (\InvalidArgumentException $e) {
throw new \OutOfBoundsException($e->getMessage());
}
}
/**
* @param int $value
*
* @return IPBlock
*/
public function minus(int $value): IPBlock
{
if ($value < 0) {
return $this->plus(-1 * $value);
}
if ($value == 0) {
return clone $this;
}
// check boundaries
try {
$first_ip = $this->first_ip->minus(gmp_mul($value, $this->getNbAddresses()));
return new static(
$first_ip,
$this->prefix
);
} catch (\InvalidArgumentException $e) {
throw new \OutOfBoundsException($e->getMessage());
}
}
/**
* Returns the first IP address of the block.
*
* @return IP
*/
public function getFirstIp(): IP
{
return $this->first_ip;
}
/**
* Returns the last IP address of the block.
*
* @return IP
*/
public function getLastIp(): IP
{
return $this->last_ip;
}
/**
* Returns the Network IP address of the block (the first address).
*
* @see getFirstIp
*
* @return IP
*/
public function getNetworkAddress(): IP
{
return $this->first_ip;
}
/**
* Returns the Broadcast IP address of the block (the last address).
*
* @see getLastIp
*
* @return IP
*/
public function getBroadcastAddress(): IP
{
return $this->last_ip;
}
/**
* A string representation of the given IP with the mask in prefix notation.
*
* @return string
*/
public function getGivenIpWithPrefixLen(): string
{
return $this->given_ip.'/'.$this->prefix;
}
/**
* A string representation of the given IP with the network as a net mask.
*
* @return string
**/
public function getGivenIpWithNetmask(): string
{
return $this->given_ip.'/'.$this->getMask();
}
/**
* @internal
* Check if the prefix is valid
*
* @param mixed $prefix
*
* @throws \InvalidArgumentException
*
* @return int
*/
protected function checkPrefix($prefix)
{
if ($prefix === '' || $prefix === null || $prefix === false || $prefix < 0 || $prefix > $this->getMaxPrefix()) {
throw new \InvalidArgumentException(sprintf(
"Invalid IPv%s block prefix '%s'",
$this->getVersion(),
$prefix
));
}
return (int) $prefix;
}
/**
* Split the block into smaller blocks.
*
* Returns an iterator, use foreach to loop it and count to get number of subnets.
*
* @param mixed $prefix
*
* @return IPBlockIterator
*/
public function getSubBlocks($prefix): IPBlockIterator
{
$prefix = ltrim($prefix, '/');
$prefix = $this->checkPrefix($prefix);
if ($prefix <= $this->prefix) {
throw new \InvalidArgumentException("Prefix must be smaller than {$this->prefix} ($prefix given)");
}
$first_block = new static($this->first_ip, $prefix);
$number_of_blocks = gmp_pow(2, $prefix - $this->prefix);
return new IPBlockIterator($first_block, $number_of_blocks);
}
/**
* @deprecated since version 2.0 and will be removed in 3.0. Use IPBlock::getSuperBlock() instead.
*
* @param mixed $prefix
*
* @return IPBlock
*/
public function getSuper($prefix): IPBlock
{
@trigger_error('IPBlock::getSuper() is deprecated since version 2.0 and will be removed in 3.0. Use IPBlock::getSuperBlock() instead.', E_USER_DEPRECATED);
return $this->getSuperBlock($prefix);
}
/**
* Return the super block containing the current block.
*
* @param mixed $prefix
*
* @return IPBlock
*/
public function getSuperBlock($prefix): IPBlock
{
$prefix = ltrim($prefix, '/');
$prefix = $this->checkPrefix($prefix);
if ($prefix >= $this->prefix) {
throw new \InvalidArgumentException("Prefix must be bigger than {$this->prefix} ($prefix given)");
}
return new static($this->first_ip, $prefix);
}
/**
* Determine if the current block contains an IP address or block.
*
* @param mixed $ip_or_block
*
* @return bool
*/
public function contains($ip_or_block): bool
{
if ((is_string($ip_or_block) && strpos($ip_or_block, '/') !== false) || $ip_or_block instanceof IPBlock) {
return $this->containsBlock($ip_or_block);
} else {
return $this->containsIP($ip_or_block);
}
}
/**
* Determine if the current block contains an IP address.
*
* @param mixed $ip
*
* @return bool
*/
public function containsIP($ip): bool
{
if (!$ip instanceof IP) {
$ip = IP::create($ip);
}
return ($ip->numeric() >= $this->getFirstIp()->numeric()) && ($ip->numeric() <= $this->getLastIp()->numeric());
}
/**
* Determine if the current block contains another block.
*
* True in this situation:
* $this: first_ip[ ]last_ip
* $block: first_ip[ ]last_ip
*
* @param mixed $block
*
* @return bool
*/
public function containsBlock($block): bool
{
if (!$block instanceof IPBlock) {
$block = new static($block);
}
return $block->getFirstIp()->numeric() >= $this->first_ip->numeric() && $block->getLastIp()->numeric() <= $this->last_ip->numeric();
}
/**
* Determine if the current block is contained in another block.
*
* @param mixed $block
*
* @return bool
*/
public function isIn($block): bool
{
if (!$block instanceof IPBlock) {
$block = new static($block);
}
return $block->containsBlock($this);
}
/**
* Test is the two blocks overlap, i.e. if block1 contains block2, or block2 contains block1.
*
* @param mixed $block
*
* @return bool
*/
public function overlaps($block): bool
{
if (!$block instanceof IPBlock) {
$block = new static($block);
}
return !($block->getFirstIp()->numeric() > $this->last_ip->numeric() || $block->getLastIp()->numeric() < $this->first_ip->numeric());
}
/**
* Return the number of IP addresses in the block.
*
* @return string numeric string (can be huge)
*/
public function getNbAddresses(): string
{
if ($this->nb_addresses === null) {
$this->nb_addresses = gmp_strval(gmp_pow(2, $this->getMaxPrefix() - $this->prefix));
}
return $this->nb_addresses;
}
/**
* Count the number of addresses contained with the address block. May exceed PHP's internal maximum integer.
*
* @return int
*
* @throws \RuntimeException thrown if the number of addresses exceeds PHP_INT_MAX
*/
public function count(): int
{
$network_size = gmp_init($this->getNbAddresses());
if (gmp_cmp($network_size, PHP_INT_MAX) > 0) {
throw new \RuntimeException('The number of addresses is bigger than PHP_INT_MAX, use getNbAddresses() instead');
}
return gmp_intval($network_size);
}
/**
* @return \Generator|IP[]
*/
public function getIterator(): \Generator
{
$position = gmp_init(0);
while (gmp_cmp($position, 0) >= 0 && gmp_cmp($position, $this->getNbAddresses()) < 0) {
yield $this->first_ip->plus(gmp_strval($position));
$position = gmp_add($position, 1);
}
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset): bool
{
return gmp_cmp($offset, 0) >= 0 && gmp_cmp($offset, $this->getNbAddresses()) < 0;
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset): IP
{
if (!$this->offsetExists($offset)) {
throw new \OutOfBoundsException("Offset $offset does not exists");
}
return $this->first_ip->plus($offset);
}
/**
* Method is logically unsupported.
*
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
throw new \LogicException('Setting IP in block is not supported');
}
/**
* Method is logically unsupported.
*
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
throw new \LogicException('Unsetting IP in block is not supported');
}
}