-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMulti.php
More file actions
451 lines (399 loc) · 15.7 KB
/
Copy pathMulti.php
File metadata and controls
451 lines (399 loc) · 15.7 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
<?php
declare(strict_types=1);
namespace Darsyn\IP\Version;
use Darsyn\IP\Exception;
use Darsyn\IP\IpInterface;
use Darsyn\IP\Strategy\CanonicalEmbeddingInterface;
use Darsyn\IP\Strategy\EmbeddingStrategyInterface;
use Darsyn\IP\Strategy\Mapped as MappedEmbeddingStrategy;
use Darsyn\IP\Util\Binary;
use Darsyn\IP\Util\MbString;
/**
* Multi-version IP Address
*
* IP is an immutable value object that provides several notations of the same
* IP value, including some helper functions for broadcast and network
* addresses, and whether its within the range of another IP address according
* to a CIDR (subnet mask), etc.
* Although it deals with both IPv4 and IPv6 notations, it makes no distinction
* between the two protocol formats as it converts both of them to a 16-byte
* binary sequence for easy mathematical operations and consistency (for
* example, storing both IPv4 and IPv6 addresses' binary sequences in a
* fixed-length database column).
*
* @author Zan Baldwin <hello@zanbaldwin.com>
* @link https://github.com/darsyn/ip
* @copyright 2015 Zan Baldwin
* @license MIT/X11 <http://j.mp/mit-license>
*/
class Multi extends IPv6 implements MultiVersionInterface
{
/** @var \Darsyn\IP\Strategy\EmbeddingStrategyInterface|null $defaultEmbeddingStrategy */
private static $defaultEmbeddingStrategy;
/** @var \Darsyn\IP\Strategy\EmbeddingStrategyInterface $embeddingStrategy */
private $embeddingStrategy;
/** @var bool $embedded */
private $embedded;
public static function setDefaultEmbeddingStrategy(EmbeddingStrategyInterface $strategy): void
{
self::$defaultEmbeddingStrategy = $strategy;
}
/**
* Get the default embedding strategy set. Default to the IPv4-mapped IPv6
* embedding strategy if the user has not set one globally.
*/
private static function getDefaultEmbeddingStrategy(): EmbeddingStrategyInterface
{
return self::$defaultEmbeddingStrategy ?: new MappedEmbeddingStrategy();
}
/** Graceful degradation for a user-defined embedding strategy that doesn't implement the CanonicalEmbeddingInterface. */
private static function packIntoCanonical(EmbeddingStrategyInterface $strategy, string $binary): string
{
if ($strategy instanceof CanonicalEmbeddingInterface) {
return $strategy->packIntoCanonical($binary);
}
/** @phpstan-ignore method.deprecated */
return $strategy->pack($binary);
}
/** Graceful degradation for a user-defined embedding strategy that doesn't implement the CanonicalEmbeddingInterface. */
private static function packIntoNonCanonical(EmbeddingStrategyInterface $strategy, string $ipv6, string $ipv4): string
{
if ($strategy instanceof CanonicalEmbeddingInterface) {
return $strategy->packIntoNonCanonical($ipv6, $ipv4);
}
/** @phpstan-ignore method.deprecated */
return $strategy->pack($ipv4);
}
/** @deprecated Use fromProtocol() or fromBinary() instead. */
public static function factory(string $ip, ?EmbeddingStrategyInterface $strategy = null): self
{
// We need a strategy to pack version 4 addresses.
$strategy = $strategy ?: self::getDefaultEmbeddingStrategy();
try {
// Convert from protocol notation to binary sequence.
$binary = self::getProtocolFormatter()->pton($ip);
// If the IP address is a binary sequence of 4 bytes, then pack it into
// a 16 byte IPv6 binary sequence according to the embedding strategy.
if (4 === MbString::getLength($binary)) {
$binary = self::packIntoCanonical($strategy, $binary);
}
} catch (Exception\IpException $e) {
throw new Exception\InvalidIpAddressException($ip, $e);
}
return new static($binary, $strategy);
}
public static function fromProtocol(string $ip, ?EmbeddingStrategyInterface $strategy = null)
{
$strategy = $strategy ?: self::getDefaultEmbeddingStrategy();
try {
$binary = self::getProtocolFormatter()->pton($ip);
} catch (Exception\IpException $e) {
throw new Exception\InvalidIpAddressException($ip, $e);
}
// (see rant in IPv4::fromProtocol).
if ($binary === $ip) {
throw new Exception\InvalidIpAddressException($ip);
}
$length = MbString::getLength($binary);
if (4 === $length) {
$binary = self::packIntoCanonical($strategy, $binary);
} elseif (16 !== $length) {
throw new Exception\InvalidBinaryException($binary);
}
return new static($binary, $strategy);
}
public static function tryFromProtocol(string $ip, ?EmbeddingStrategyInterface $strategy = null)
{
try {
return static::fromProtocol($ip, $strategy);
} catch (Exception\InvalidIpAddressException $e) {
return null;
}
}
public static function fromBinary(string $binary, ?EmbeddingStrategyInterface $strategy = null)
{
$strategy = $strategy ?: self::getDefaultEmbeddingStrategy();
$length = MbString::getLength($binary);
if (4 === $length) {
$binary = self::packIntoCanonical($strategy, $binary);
} elseif (16 !== $length) {
throw new Exception\InvalidBinaryException($binary);
}
return new static($binary, $strategy);
}
public static function tryFromBinary(string $binary, ?EmbeddingStrategyInterface $strategy = null)
{
try {
return static::fromBinary($binary, $strategy);
} catch (Exception\InvalidIpAddressException $e) {
return null;
}
}
public static function fromHex(string $hex, ?EmbeddingStrategyInterface $strategy = null)
{
try {
$binary = Binary::fromHex($hex);
} catch (\InvalidArgumentException $e) {
throw new Exception\InvalidIpAddressException($hex, $e);
}
return static::fromBinary($binary, $strategy);
}
public static function tryFromHex(string $hex, ?EmbeddingStrategyInterface $strategy = null)
{
try {
return static::fromHex($hex, $strategy);
} catch (Exception\InvalidIpAddressException $e) {
return null;
}
}
public static function isValid(string $ip, ?EmbeddingStrategyInterface $strategy = null): bool
{
return null !== static::tryFromProtocol($ip, $strategy);
}
protected function __construct(string $ip, ?EmbeddingStrategyInterface $strategy = null)
{
// Fallback to default in case this instance was created from static in
// the abstract IP class.
$this->embeddingStrategy = $strategy ?: self::getDefaultEmbeddingStrategy();
parent::__construct($ip);
}
public function getProtocolAppropriateAddress(/* ?ProtocolFormatterInterface $formatter = null */): string
{
// If binary string contains an embedded IPv4 address, then extract it.
$ip = $this->isEmbedded()
? $this->getShortBinary()
: $this->getBinary();
// Render the IP address in the correct notation according to its
// protocol (based on how long the binary string is).
return self::resolveProtocolFormatter(\func_get_args())->ntop($ip);
}
/**
* @throws \Darsyn\IP\Exception\WrongVersionException
* @throws \Darsyn\IP\Exception\IpException
*/
public function getDotAddress(/* ?ProtocolFormatterInterface $formatter = null */): string
{
// Resolve the per-call formatter argument before the version check so a
// deprecated (non-formatter) argument is flagged regardless of embedded state.
$formatter = self::resolveProtocolFormatter(\func_get_args());
if ($this->isEmbedded()) {
try {
return $formatter->ntop($this->getShortBinary());
} catch (Exception\Formatter\FormatException $e) {
throw new Exception\IpException('An unknown error occurred internally.', 0, $e);
}
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
public function getOctets(): array
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->getOctets()
: parent::getOctets();
}
public function getSegments(): array
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::getSegments();
}
public function getVersion(): int
{
return $this->isEmbedded() ? 4 : 6;
}
public function getNetworkIp(int $cidr): self
{
try {
if ($this->isVersion4WithAppropriateCidr($cidr)) {
$v4 = (new IPv4($this->getShortBinary()))->getNetworkIp($cidr)->getBinary();
return new static(
self::packIntoCanonical($this->embeddingStrategy, $v4),
clone $this->embeddingStrategy
);
}
} catch (Exception\IpException $e) {
}
return new static(parent::getNetworkIp($cidr)->getBinary(), clone $this->embeddingStrategy);
}
public function getBroadcastIp(int $cidr): self
{
try {
if ($this->isVersion4WithAppropriateCidr($cidr)) {
$v4 = (new IPv4($this->getShortBinary()))->getBroadcastIp($cidr)->getBinary();
return new static(
self::packIntoCanonical($this->embeddingStrategy, $v4),
clone $this->embeddingStrategy
);
}
} catch (Exception\IpException $e) {
}
return new static(parent::getBroadcastIp($cidr)->getBinary(), clone $this->embeddingStrategy);
}
public function offset(int $offset): self
{
if ($this->isEmbedded()) {
$v4 = (new IPv4($this->getShortBinary()))->offset($offset)->getBinary();
return new static(
self::packIntoNonCanonical($this->embeddingStrategy, $this->getBinary(), $v4),
clone $this->embeddingStrategy
);
}
return new static(parent::offset($offset)->getBinary(), clone $this->embeddingStrategy);
}
public function inRange(IpInterface $ip, int $cidr): bool
{
try {
if ($this->isVersion4WithAppropriateCidr($cidr) && $this->isVersion4CompatibleWithCurrentStrategy($ip)) {
$ours = $this->getShortBinary();
$theirs = $this->embeddingStrategy->extract($ip->getBinary());
return (new IPv4($ours))->inRange(new IPv4($theirs), $cidr);
}
} catch (Exception\IpException $e) {
// If an exception was thrown, the two IP addresses were incompatible
// and should not have been checked as IPv4 addresses, fallback to
// performing the operation as IPv6 addresses.
}
return parent::inRange($ip, $cidr);
}
public function getCommonCidr(IpInterface $ip): int
{
try {
if ($this->isVersion4CompatibleWithCurrentStrategy($ip)) {
$ours = $this->getShortBinary();
$theirs = $this->embeddingStrategy->extract($ip->getBinary());
return (new IPv4($ours))->getCommonCidr(new IPv4($theirs));
}
} catch (Exception\IpException $e) {
// If an exception was thrown, the two IP addresses were incompatible
// and should not have been checked as IPv4 addresses, fallback to
// performing the operation as IPv6 addresses.
}
return parent::getCommonCidr($ip);
}
public function isEmbedded(): bool
{
if (null === $this->embedded) {
$this->embedded = $this->embeddingStrategy->isEmbedded($this->getBinary());
}
return $this->embedded;
}
public function isLinkLocal(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isLinkLocal()
: parent::isLinkLocal();
}
public function isLoopback(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isLoopback()
: parent::isLoopback();
}
public function isMulticast(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isMulticast()
: parent::isMulticast();
}
public function isPrivateUse(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isPrivateUse()
: parent::isPrivateUse();
}
public function isUnspecified(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isUnspecified()
: parent::isUnspecified();
}
public function isBenchmarking(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isBenchmarking()
: parent::isBenchmarking();
}
public function isDocumentation(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isDocumentation()
: parent::isDocumentation();
}
/** @deprecated Use isGloballyReachable() instead. */
public function isPublicUse(): bool
{
return $this->isGloballyReachable();
}
public function isGloballyReachable(): bool
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isGloballyReachable()
: parent::isGloballyReachable();
}
public function isUniqueLocal(): bool
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::isUniqueLocal();
}
public function isUnicast(): bool
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::isUnicast();
}
public function isUnicastGlobal(): bool
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::isUnicastGlobal();
}
public function isBroadcast(): bool
{
if ($this->isEmbedded()) {
return (new IPv4($this->getShortBinary()))->isBroadcast();
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
public function isShared(): bool
{
if ($this->isEmbedded()) {
return (new IPv4($this->getShortBinary()))->isShared();
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
public function isFutureReserved(): bool
{
if ($this->isEmbedded()) {
return (new IPv4($this->getShortBinary()))->isFutureReserved();
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
/** @throws \Darsyn\IP\Exception\Strategy\ExtractionException */
private function getShortBinary(): string
{
return $this->embeddingStrategy->extract($this->getBinary());
}
/** Can the supplied CIDR and current version be considered as an IPv4 operation? */
private function isVersion4WithAppropriateCidr(int $cidr): bool
{
return $cidr <= 32 && $this->isVersion4();
}
/** Can the supplied and current IP be considered as an IPv4 operation? */
private function isVersion4CompatibleWithCurrentStrategy(IpInterface $ip): bool
{
return $this->isVersion4() && $ip->isVersion4() && $this->embeddingStrategy->isEmbedded($ip->getBinary());
}
public function toString(): string
{
return $this->getProtocolAppropriateAddress();
}
public function __toString(): string
{
return $this->toString();
}
}