-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion.php
More file actions
244 lines (222 loc) · 6.26 KB
/
Copy pathQuestion.php
File metadata and controls
244 lines (222 loc) · 6.26 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
<?php
declare(strict_types=1);
namespace ArrayAccess\DnsRecord\Packet;
use ArrayAccess\DnsRecord\Exceptions\EmptyArgumentException;
use ArrayAccess\DnsRecord\Interfaces\Packet\PacketQuestionInterface;
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordClassInterface;
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordQTypeDefinitionInterface;
use ArrayAccess\DnsRecord\Interfaces\ResourceRecord\ResourceRecordTypeInterface;
use ArrayAccess\DnsRecord\Utils\Addresses;
use ArrayAccess\DnsRecord\Utils\Buffer;
use ArrayAccess\DnsRecord\Utils\Lookup;
use ReflectionClass;
use Throwable;
use function preg_match;
use function serialize;
use function strtolower;
use function trim;
use function unserialize;
/**
* Question format
*
* 1 1 1 1 1 1
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | |
* / QNAME /
* / /
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | QTYPE |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* | QCLASS |
* +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
*
* @link https://datatracker.ietf.org/doc/html/rfc1035#section-4.1.2
*/
class Question implements PacketQuestionInterface
{
/**
* @see PacketQuestionInterface::getName()
* @var string
*/
private string $name;
/**
* @see PacketQuestionInterface::getMessage()
* @var string
*/
private string $message;
/**
* @see PacketQuestionInterface::getType()
*/
private ?ResourceRecordQTypeDefinitionInterface $type;
/**
* @see PacketQuestionInterface::getClass()
* @var ?ResourceRecordClassInterface
*/
private ?ResourceRecordClassInterface $class;
/**
* @inheritdoc
*/
public function __construct(
string $name,
string|Int|ResourceRecordTypeInterface|ResourceRecordQTypeDefinitionInterface $type,
string|int|ResourceRecordClassInterface $class,
bool $disableInit = false
) {
if (!$disableInit) {
/**
* @uses self::init()
*/
$this->init($name, $type, $class);
}
}
private function initType(
string|Int|ResourceRecordTypeInterface|ResourceRecordQTypeDefinitionInterface $type,
): void {
$this->type ??= Lookup::resourceType($type);
}
private function initClass(string|Int|ResourceRecordClassInterface $type): void
{
if (isset($this->class)) {
return;
}
$this->class ??= Lookup::resourceClass($type);
}
private function initName(string $name, bool $internal = false): void
{
$name = trim($name);
if (!$internal && $name === '') {
throw new EmptyArgumentException(
'Argument QNAME could not be empty or whitespace only'
);
}
$name = strtolower($name);
if ($this->getType()?->getName() === 'PTR'
&& !preg_match('~\.(in-addr|ip6)\.arpa$~', $name)
) {
$name = Addresses::reverseIp($name)??$name;
}
$this->name = $name;
}
/**
* Init constructor
*
* @param string $name
* @param string|Int|ResourceRecordTypeInterface|ResourceRecordQTypeDefinitionInterface $type
* @param string|int|ResourceRecordClassInterface $class
* @return void
*/
private function init(
string $name,
string|Int|ResourceRecordTypeInterface|ResourceRecordQTypeDefinitionInterface $type,
string|int|ResourceRecordClassInterface $class
): void {
$this->initType($type);
$this->initClass($class);
$this->initName($name);
}
/**
* @inheritdoc
*/
public function getName(): string
{
return $this->name;
}
/**
* @inheritdoc
*/
public function getType(): ?ResourceRecordQTypeDefinitionInterface
{
return $this->type;
}
/**
* @inheritdoc
*/
public function getClass(): ?ResourceRecordClassInterface
{
return $this->class;
}
/**
* @inheritdoc
*/
public function getMessage(): string
{
return $this->message ??= Buffer::createQuestionMessage($this);
}
/**
* @inheritdoc
*/
public function serialize(): string
{
return serialize($this->__serialize());
}
/**
* @inheritdoc
*/
public function unserialize(string $data): void
{
$this->__unserialize(unserialize($data));
}
/**
* Magic method for serializing
*
* @return array{
* name: string,
* type: string,
* class: string
* }
*/
public function __serialize(): array
{
return [
'name' => $this->name,
'type' => $this->getType()?->getName()??'',
'class' => $this->getClass()?->getName()??'',
];
}
/**
* @param string $name
* @param int $type
* @param int $class
* @return Question
* @noinspection PhpDocMissingThrowsInspection
*/
public static function fromFilteredResponse(
string $name,
int $type,
int $class
): self {
/** @noinspection PhpUnhandledExceptionInspection */
$object = (new ReflectionClass(__CLASS__))
->newInstanceWithoutConstructor();
/**
* @var Question $object
*/
try {
$object->initType($type);
} catch (Throwable) {
$object->type = null;
}
try {
$object->initClass($class);
} catch (Throwable) {
$object->class = null;
}
$object->initName($name, true);
return $object;
}
/**
* Magic method for unserialize
*
* @param array{
* name: string,
* type: string,
* class: string
* } $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->init($data['name'], $data['type'], $data['class']);
}
}