1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Rdata; |
15
|
|
|
|
16
|
|
|
use PhpIP\IPBlock; |
17
|
|
|
use PhpIP\IPv4Block; |
18
|
|
|
use PhpIP\IPv6Block; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @see https://tools.ietf.org/html/rfc3123 |
22
|
|
|
*/ |
23
|
|
|
class APL implements RdataInterface |
24
|
|
|
{ |
25
|
|
|
use RdataTrait; |
26
|
|
|
|
27
|
|
|
const TYPE = 'APL'; |
28
|
|
|
const TYPE_CODE = 42; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var IPBlock[] |
32
|
|
|
*/ |
33
|
|
|
private $includedAddressRanges = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var IPBlock[] |
37
|
|
|
*/ |
38
|
|
|
private $excludedAddressRanges = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param IPBlock $ipBlock |
42
|
|
|
* @param bool $included True if the resource exists within the range, False if the resource |
43
|
|
|
* is not within the range. I.E. the negation. |
44
|
|
|
*/ |
45
|
9 |
|
public function addAddressRange(IPBlock $ipBlock, $included = true): void |
46
|
|
|
{ |
47
|
9 |
|
if ($included) { |
48
|
9 |
|
$this->includedAddressRanges[] = $ipBlock; |
49
|
|
|
} else { |
50
|
9 |
|
$this->excludedAddressRanges[] = $ipBlock; |
51
|
|
|
} |
52
|
9 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return IPBlock[] |
56
|
|
|
*/ |
57
|
4 |
|
public function getIncludedAddressRanges(): array |
58
|
|
|
{ |
59
|
4 |
|
return $this->includedAddressRanges; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return IPBlock[] |
64
|
|
|
*/ |
65
|
4 |
|
public function getExcludedAddressRanges(): array |
66
|
|
|
{ |
67
|
4 |
|
return $this->excludedAddressRanges; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
public function toText(): string |
74
|
|
|
{ |
75
|
2 |
|
$string = ''; |
76
|
2 |
|
foreach ($this->includedAddressRanges as $ipBlock) { |
77
|
2 |
|
$string .= (4 === $ipBlock->getVersion()) ? '1:' : '2:'; |
78
|
2 |
|
$string .= (string) $ipBlock.' '; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
foreach ($this->excludedAddressRanges as $ipBlock) { |
82
|
2 |
|
$string .= (4 === $ipBlock->getVersion()) ? '!1:' : '!2:'; |
83
|
2 |
|
$string .= (string) $ipBlock.' '; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return rtrim($string, ' '); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function toWire(): string |
93
|
|
|
{ |
94
|
1 |
|
$encoded = ''; |
95
|
|
|
|
96
|
1 |
|
foreach ($this->includedAddressRanges as $ipBlock) { |
97
|
1 |
|
$encoded .= pack('nCC', |
98
|
1 |
|
(4 === $ipBlock->getVersion()) ? 1 : 2, |
99
|
1 |
|
$ipBlock->getPrefix(), |
100
|
1 |
|
$ipBlock->getGivenIp()::NB_BYTES |
101
|
1 |
|
).inet_pton((string) $ipBlock->getGivenIp()); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
foreach ($this->excludedAddressRanges as $ipBlock) { |
105
|
1 |
|
$encoded .= pack('nCCC*', |
106
|
1 |
|
(4 === $ipBlock->getVersion()) ? 1 : 2, |
107
|
1 |
|
$ipBlock->getPrefix(), |
108
|
1 |
|
$ipBlock->getGivenIp()::NB_BYTES | 0b10000000 |
109
|
1 |
|
).inet_pton((string) $ipBlock->getGivenIp()); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $encoded; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
* |
118
|
|
|
* @throws \Exception |
119
|
|
|
*/ |
120
|
7 |
|
public static function fromText(string $text): RdataInterface |
121
|
|
|
{ |
122
|
7 |
|
$iterator = new \ArrayIterator(explode(' ', $text)); |
123
|
7 |
|
$apl = new self(); |
124
|
|
|
|
125
|
7 |
|
while ($iterator->valid()) { |
126
|
7 |
|
$matches = []; |
127
|
7 |
|
if (1 !== preg_match('/^(?<negate>!)?[1-2]:(?<block>.+)$/i', $iterator->current(), $matches)) { |
128
|
2 |
|
throw new \Exception(sprintf('"%s" is not a valid IP range.', $iterator->current())); |
129
|
|
|
} |
130
|
|
|
|
131
|
5 |
|
$ipBlock = IPBlock::create($matches['block']); |
132
|
5 |
|
$apl->addAddressRange($ipBlock, '!' !== $matches['negate']); |
133
|
5 |
|
$iterator->next(); |
134
|
|
|
} |
135
|
|
|
|
136
|
5 |
|
return $apl; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
1 |
|
public static function fromWire(string $rdata): RdataInterface |
143
|
|
|
{ |
144
|
1 |
|
$apl = new self(); |
145
|
1 |
|
$rdLength = strlen($rdata); |
146
|
1 |
|
$offset = 0; |
147
|
|
|
|
148
|
1 |
|
while ($offset < $rdLength) { |
149
|
1 |
|
$apItem = unpack('nfamily/Cprefix/Clength', $rdata, $offset); |
150
|
1 |
|
$isExcluded = (bool) ($apItem['length'] & 0b10000000); |
151
|
1 |
|
$len = $apItem['length'] & 0b01111111; |
152
|
1 |
|
$version = (1 === $apItem['family']) ? 4 : 6; |
153
|
1 |
|
$offset += 4; |
154
|
1 |
|
$address = substr($rdata, $offset, $len); |
155
|
1 |
|
$address = inet_ntop($address); |
156
|
1 |
|
$offset += $len; |
157
|
|
|
|
158
|
1 |
|
$ipBlock = (4 === $version) ? new IPv4Block($address, $apItem['prefix']) : new IPv6Block($address, $apItem['prefix']); |
159
|
1 |
|
$apl->addAddressRange($ipBlock, !$isExcluded); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $apl; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|