-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfunctions.php
260 lines (207 loc) · 7.5 KB
/
functions.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
<?php declare(strict_types = 1);
namespace DaveRandom\LibDNS;
use DaveRandom\LibDNS\Protocol\DecodingContext;
use DaveRandom\LibDNS\Protocol\EncodingContext;
use DaveRandom\Network\DomainName;
use DaveRandom\Network\IPAddress;
use DaveRandom\Network\IPv4Address;
use DaveRandom\Network\IPv6Address;
const UINT16_MIN = 0;
const UINT16_MAX = 0xffff;
const UINT16_MASK = 0xffff;
const UINT32_MIN = \PHP_INT_SIZE === 8 ? 0 : \PHP_INT_MIN;
const UINT32_MAX = \PHP_INT_SIZE === 8 ? 0xffffffff : \PHP_INT_MAX;
const UINT32_MASK = (0x7fffffff << 1) | 0x01;
function normalize_name(string $label): string
{
\trigger_error(
'DaveRandom\LibDNS\normalize_name() is deprecated, please use DaveRandom\Network\normalize_dns_name() instead',
E_USER_DEPRECATED
);
return \DaveRandom\Network\normalize_dns_name($label);
}
function encode_domain_name(EncodingContext $ctx, DomainName $name, bool $neverCompress = false)
{
$labels = $name->getLabels();
$result = '';
// If compression is disabled, just encode the whole name literally
if ($neverCompress || !$ctx->isCompressionEnabled()) {
foreach ($labels as $label) {
$result .= \chr(\strlen($label)) . $label;
}
goto done;
}
// Loop the labels until there are none left
do {
$part = \implode('.', $labels);
// If the remainder of the name exists in the registry, encode a pointer to it and return
if ($ctx->hasIndexForLabel($part)) {
$ctx->appendData($result . \pack('n', 0b1100000000000000 | $ctx->getLabelIndex($part)));
return;
}
// Add the remainder of the name to the registry as long as the offset is small enough to fit in a reference
if ($ctx->getOffset() <= 0x3fff) {
$ctx->setLabelIndexAtCurrentOffset($part);
}
// Encode the current label literally
$label = \array_shift($labels);
$length = \strlen($label);
$ctx->appendData(\chr($length) . $label);
} while (!empty($labels));
done:
$ctx->appendData("{$result}\x00");
}
function decode_domain_name(DecodingContext $ctx): DomainName
{
$startOffset = $ctx->getOffset();
$result = [];
$literalLabels = [];
// Read the label length byte from the buffer
while (0 !== $length = \ord($ctx->getData(1))) {
switch ($length & 0b11000000) {
// If the first two bits are set, the label is a pointer to somewhere earlier in the packet
case 0b11000000: {
$offset = (($length & 0b00111111) << 8) | \ord($ctx->getData(1));
if (!$ctx->hasLabelsAtOffset($offset)) {
throw new \UnexpectedValueException(\sprintf(
'Decode error: Invalid compression pointer reference 0x%X in domain name at position 0x%X',
$offset,
$startOffset
));
}
$result = $ctx->getLabelsAtOffset($offset);
break 2;
}
// If the first two bits are clear, the label is $length bytes long
case 0: {
$offset = $ctx->getOffset();
if ($offset + $length > $ctx->getDataLength()) {
throw new \UnexpectedValueException(\sprintf(
'Decode error: Incomplete label in domain name at position 0x%X',
$startOffset
));
}
$literalLabels[] = [$offset - 1, $ctx->getData($length)];
break;
}
// Handling of any other combination is not specified
default: throw new \UnexpectedValueException(\sprintf(
'Decode error: Invalid label type 0x%X in domain name at position 0x%X',
$length & 0b11000000,
$startOffset
));
}
if (!$ctx->hasData(1)) {
throw new \UnexpectedValueException(\sprintf(
'Decode error: Incomplete domain name at position 0x%X',
$startOffset
));
}
}
// Store decoded label indexes for later compression lookups
while (list($offset, $label) = \array_pop($literalLabels)) {
\array_unshift($result, $label);
$ctx->setLabelsAtOffset($offset, $result);
}
return new DomainName($result, false);
}
function encode_ipv4address(EncodingContext $ctx, IPv4Address $address)
{
$ctx->appendData(\pack(
'C4',
$address->getOctet1(),
$address->getOctet2(),
$address->getOctet3(),
$address->getOctet4()
));
}
function decode_ipv4address(DecodingContext $ctx): IPv4Address
{
$octets = $ctx->unpack('C4', 4);
return new IPv4Address($octets[1], $octets[2], $octets[3], $octets[4]);
}
function encode_ipv6address(EncodingContext $ctx, IPv6Address $address)
{
$ctx->appendData(\pack(
'n8',
$address->getHextet1(),
$address->getHextet2(),
$address->getHextet3(),
$address->getHextet4(),
$address->getHextet5(),
$address->getHextet6(),
$address->getHextet7(),
$address->getHextet8()
));
}
function decode_ipv6address(DecodingContext $ctx): IPv6Address
{
$hextets = $ctx->unpack('n8', 16);
return new IPv6Address(
$hextets[1], $hextets[2], $hextets[3], $hextets[4], $hextets[5], $hextets[6], $hextets[7], $hextets[8]
);
}
function encode_character_string(EncodingContext $ctx, string $string)
{
$length = \strlen($string);
if ($length > 255) {
throw new \InvalidArgumentException(
"Maximum length of character-string is 255 bytes (got {$length} bytes)"
);
}
$ctx->appendData(\chr(\strlen($string)) . $string);
}
function decode_character_string(DecodingContext $ctx): string
{
$length = \ord($ctx->getData(1));
return $ctx->unpack("a{$length}", $length)[1];
}
function validate_nibble(string $description, int $value): int
{
if (($value & 0x0f) !== $value) {
throw new \InvalidArgumentException("{$description} must be in the range 0 - 15");
}
return $value;
}
function validate_byte(string $description, int $value): int
{
if (($value & 0xff) !== $value) {
throw new \InvalidArgumentException("{$description} must be in the range 0 - 255");
}
return $value;
}
function validate_uint16(string $description, int $value): int
{
if (($value & 0xffff) !== $value) {
throw new \InvalidArgumentException("{$description} must be in the range 0 - 65535");
}
return $value;
}
function validate_uint32(string $description, int $value): int
{
if (($value & UINT32_MASK) !== $value) {
throw new \InvalidArgumentException("{$description} must be in the range " . UINT32_MIN . " - " . UINT32_MAX);
}
return $value;
}
function ipaddress_to_ptr_name(IPAddress $address): DomainName
{
if ($address instanceof IPv4Address) {
return new DomainName([
$address->getOctet4(), $address->getOctet3(), $address->getOctet2(), $address->getOctet1(),
'in-addr', 'arpa'
]);
}
if (!$address instanceof IPv6Address) {
throw new \InvalidArgumentException('Unknown IP address type: ' . \get_class($address));
}
$labels = [];
$bin = $address->toBinary();
for ($i = 15; $i >= 0; $i--) {
$byte = \ord($bin[$i]);
\array_push($labels, \dechex($byte & 0x0f), \dechex(($byte & 0xf0) >> 4));
}
$labels[] = 'ip6';
$labels[] = 'arpa';
return new DomainName($labels);
}