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
|
<?php
namespace React\Dns\Protocol;
use React\Dns\Model\Message;
use React\Dns\Model\Record;
use InvalidArgumentException;
/**
* DNS protocol parser
*
* Obsolete and uncommon types and classes are not implemented.
*/
class Parser
{
/**
* Parses the given raw binary message into a Message object
*
* @param string $data
* @throws InvalidArgumentException
* @return Message
*/
public function parseMessage($data)
{
$message = new Message();
if ($this->parse($data, $message) !== $message) {
throw new InvalidArgumentException('Unable to parse binary message');
}
return $message;
}
/**
* @deprecated unused, exists for BC only
*/
public function parseChunk($data, Message $message)
{
return $this->parse($data, $message);
}
private function parse($data, Message $message)
{
$message->data .= $data;
if (!$message->header->get('id')) {
if (!$this->parseHeader($message)) {
return;
}
}
if ($message->header->get('qdCount') != count($message->questions)) {
if (!$this->parseQuestion($message)) {
return;
}
}
if ($message->header->get('anCount') != count($message->answers)) {
if (!$this->parseAnswer($message)) {
return;
}
}
return $message;
}
public function parseHeader(Message $message)
{
if (strlen($message->data) < 12) {
return;
}
$header = substr($message->data, 0, 12);
$message->consumed += 12;
list($id, $fields, $qdCount, $anCount, $nsCount, $arCount) = array_values(unpack('n*', $header));
$rcode = $fields & bindec('1111');
$z = ($fields >> 4) & bindec('111');
$ra = ($fields >> 7) & 1;
$rd = ($fields >> 8) & 1;
$tc = ($fields >> 9) & 1;
$aa = ($fields >> 10) & 1;
$opcode = ($fields >> 11) & bindec('1111');
$qr = ($fields >> 15) & 1;
$vars = compact('id', 'qdCount', 'anCount', 'nsCount', 'arCount',
'qr', 'opcode', 'aa', 'tc', 'rd', 'ra', 'z', 'rcode');
foreach ($vars as $name => $value) {
$message->header->set($name, $value);
}
return $message;
}
public function parseQuestion(Message $message)
{
if (strlen($message->data) < 2) {
return;
}
$consumed = $message->consumed;
list($labels, $consumed) = $this->readLabels($message->data, $consumed);
if (null === $labels) {
return;
}
if (strlen($message->data) - $consumed < 4) {
return;
}
list($type, $class) = array_values(unpack('n*', substr($message->data, $consumed, 4)));
$consumed += 4;
$message->consumed = $consumed;
$message->questions[] = array(
'name' => implode('.', $labels),
'type' => $type,
'class' => $class,
);
if ($message->header->get('qdCount') != count($message->questions)) {
return $this->parseQuestion($message);
}
return $message;
}
public function parseAnswer(Message $message)
{
if (strlen($message->data) < 2) {
return;
}
$consumed = $message->consumed;
list($labels, $consumed) = $this->readLabels($message->data, $consumed);
if (null === $labels) {
return;
}
if (strlen($message->data) - $consumed < 10) {
return;
}
list($type, $class) = array_values(unpack('n*', substr($message->data, $consumed, 4)));
$consumed += 4;
list($ttl) = array_values(unpack('N', substr($message->data, $consumed, 4)));
$consumed += 4;
list($rdLength) = array_values(unpack('n', substr($message->data, $consumed, 2)));
$consumed += 2;
$rdata = null;
if (Message::TYPE_A === $type || Message::TYPE_AAAA === $type) {
$ip = substr($message->data, $consumed, $rdLength);
$consumed += $rdLength;
$rdata = inet_ntop($ip);
}
if (Message::TYPE_CNAME === $type || Message::TYPE_PTR === $type) {
list($bodyLabels, $consumed) = $this->readLabels($message->data, $consumed);
$rdata = implode('.', $bodyLabels);
}
$message->consumed = $consumed;
$name = implode('.', $labels);
$ttl = $this->signedLongToUnsignedLong($ttl);
$record = new Record($name, $type, $class, $ttl, $rdata);
$message->answers[] = $record;
if ($message->header->get('anCount') != count($message->answers)) {
return $this->parseAnswer($message);
}
return $message;
}
private function readLabels($data, $consumed)
{
$labels = array();
while (true) {
if ($this->isEndOfLabels($data, $consumed)) {
$consumed += 1;
break;
}
if ($this->isCompressedLabel($data, $consumed)) {
list($newLabels, $consumed) = $this->getCompressedLabel($data, $consumed);
$labels = array_merge($labels, $newLabels);
break;
}
$length = ord(substr($data, $consumed, 1));
$consumed += 1;
if (strlen($data) - $consumed < $length) {
return array(null, null);
}
$labels[] = substr($data, $consumed, $length);
$consumed += $length;
}
return array($labels, $consumed);
}
public function isEndOfLabels($data, $consumed)
{
$length = ord(substr($data, $consumed, 1));
return 0 === $length;
}
public function getCompressedLabel($data, $consumed)
{
list($nameOffset, $consumed) = $this->getCompressedLabelOffset($data, $consumed);
list($labels) = $this->readLabels($data, $nameOffset);
return array($labels, $consumed);
}
public function isCompressedLabel($data, $consumed)
{
$mask = 0xc000; // 1100000000000000
list($peek) = array_values(unpack('n', substr($data, $consumed, 2)));
return (bool) ($peek & $mask);
}
public function getCompressedLabelOffset($data, $consumed)
{
$mask = 0x3fff; // 0011111111111111
list($peek) = array_values(unpack('n', substr($data, $consumed, 2)));
return array($peek & $mask, $consumed + 2);
}
public function signedLongToUnsignedLong($i)
{
return $i & 0x80000000 ? $i - 0xffffffff : $i;
}
}
|