-
Notifications
You must be signed in to change notification settings - Fork 21
/
Multi.php
374 lines (337 loc) · 11.5 KB
/
Multi.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
namespace Darsyn\IP\Version;
use Darsyn\IP\Exception;
use Darsyn\IP\IpInterface;
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). in the same column in a database).
*
* @author Zan Baldwin <[email protected]>
* @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;
/**
* {@inheritDoc}
*/
public static function setDefaultEmbeddingStrategy(EmbeddingStrategyInterface $strategy)
{
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.
*
* @return \Darsyn\IP\Strategy\EmbeddingStrategyInterface
*/
private static function getDefaultEmbeddingStrategy()
{
return self::$defaultEmbeddingStrategy ?: new MappedEmbeddingStrategy;
}
/**
* {@inheritDoc}
* @param \Darsyn\IP\Strategy\EmbeddingStrategyInterface $strategy
*/
public static function factory($ip, EmbeddingStrategyInterface $strategy = null)
{
// 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 (MbString::getLength($binary) === 4) {
$binary = $strategy->pack($binary);
}
} catch (Exception\IpException $e) {
throw new Exception\InvalidIpAddressException($ip, $e);
}
return new static($binary, $strategy);
}
/**
* {@inheritDoc}
* @param \Darsyn\IP\Strategy\EmbeddingStrategyInterface|null $strategy
*/
protected function __construct($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);
}
/** {@inheritDoc} */
public function getProtocolAppropriateAddress()
{
// 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::getProtocolFormatter()->ntop($ip);
}
/**
* @throws \Darsyn\IP\Exception\WrongVersionException
* @throws \Darsyn\IP\Exception\IpException
* @return string
*/
public function getDotAddress()
{
if ($this->isEmbedded()) {
try {
return self::getProtocolFormatter()->ntop($this->getShortBinary());
} catch (Exception\Formatter\FormatException $e) {
throw new Exception\IpException('An unknown error occured internally.', 0, $e);
}
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
/** {@inheritDoc} */
public function getVersion()
{
return $this->isEmbedded() ? 4 : 6;
}
/** {@inheritDoc} */
public function getNetworkIp($cidr)
{
try {
if ($this->isVersion4WithAppropriateCidr($cidr)) {
$v4 = (new IPv4($this->getShortBinary()))->getNetworkIp($cidr)->getBinary();
return new static(
$this->embeddingStrategy->pack($v4),
clone $this->embeddingStrategy
);
}
} catch (Exception\IpException $e) {
}
return new static(parent::getNetworkIp($cidr)->getBinary(), clone $this->embeddingStrategy);
}
/** {@inheritDoc} */
public function getBroadcastIp($cidr)
{
try {
if ($this->isVersion4WithAppropriateCidr($cidr)) {
$v4 = (new IPv4($this->getShortBinary()))->getBroadcastIp($cidr)->getBinary();
return new static(
$this->embeddingStrategy->pack($v4),
clone $this->embeddingStrategy
);
}
} catch (Exception\IpException $e) {
}
return new static(parent::getBroadcastIp($cidr)->getBinary(), clone $this->embeddingStrategy);
}
/** {@inheritDoc} */
public function inRange(IpInterface $ip, $cidr)
{
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);
}
/** {@inheritDoc} */
public function getCommonCidr(IpInterface $ip)
{
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);
}
/** {@inheritDoc} */
public function isEmbedded()
{
if (null === $this->embedded) {
$this->embedded = $this->embeddingStrategy->isEmbedded($this->getBinary());
}
return $this->embedded;
}
/** {@inheritDoc} */
public function isLinkLocal()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isLinkLocal()
: parent::isLinkLocal();
}
/** {@inheritDoc} */
public function isLoopback()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isLoopback()
: parent::isLoopback();
}
/** * {@inheritDoc} */
public function isMulticast()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isMulticast()
: parent::isMulticast();
}
/** {@inheritDoc} */
public function isPrivateUse()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isPrivateUse()
: parent::isPrivateUse();
}
/** {@inheritDoc} */
public function isUnspecified()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isUnspecified()
: parent::isUnspecified();
}
/** {@inheritDoc} */
public function isBenchmarking()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isBenchmarking()
: parent::isBenchmarking();
}
/** {@inheritDoc} */
public function isDocumentation()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isDocumentation()
: parent::isDocumentation();
}
/** {@inheritDoc} */
public function isPublicUse()
{
return $this->isEmbedded()
? (new IPv4($this->getShortBinary()))->isPublicUse()
: parent::isPublicUse();
}
/**
* @inheritDoc
*/
public function isUniqueLocal()
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::isUniqueLocal();
}
/**
* @inheritDoc
*/
public function isUnicast()
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::isUnicast();
}
/**
* @inheritDoc
*/
public function isUnicastGlobal()
{
if ($this->isEmbedded()) {
throw new Exception\WrongVersionException(6, 4, (string) $this);
}
return parent::isUnicastGlobal();
}
/**
* {@inheritDoc}
*/
public function isBroadcast()
{
if ($this->isEmbedded()) {
return (new IPv4($this->getShortBinary()))->isBroadcast();
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
/**
* {@inheritDoc}
*/
public function isShared()
{
if ($this->isEmbedded()) {
return (new IPv4($this->getShortBinary()))->isShared();
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
/**
* {@inheritDoc}
*/
public function isFutureReserved()
{
if ($this->isEmbedded()) {
return (new IPv4($this->getShortBinary()))->isFutureReserved();
}
throw new Exception\WrongVersionException(4, 6, (string) $this);
}
/**
* @throws \Darsyn\IP\Exception\Strategy\ExtractionException
* @return string
*/
private function getShortBinary()
{
return $this->embeddingStrategy->extract($this->getBinary());
}
/**
* Can the supplied CIDR and current version be considered as an IPv4 operation?
*
* @param int $cidr
* @return bool
*/
private function isVersion4WithAppropriateCidr($cidr)
{
return \is_int($cidr) && $cidr <= 32 && $this->isVersion4();
}
/**
* Can the supplied and current IP be considered as an IPv4 operation?
*
* @param \Darsyn\IP\IpInterface $ip
* @return bool
*/
private function isVersion4CompatibleWithCurrentStrategy(IpInterface $ip)
{
return $this->isVersion4() && $ip->isVersion4() && $this->embeddingStrategy->isEmbedded($ip->getBinary());
}
/**
* {@inheritDoc}
*/
public function __toString()
{
return $this->getProtocolAppropriateAddress();
}
}