|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thruster\Component\Dns; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Parser |
|
7
|
|
|
* |
|
8
|
|
|
* @package Thruster\Component\Dns |
|
9
|
|
|
* @author Aurimas Niekis <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Parser |
|
12
|
|
|
{ |
|
13
|
|
|
public function parseChunk($data, Message $message) |
|
14
|
|
|
{ |
|
15
|
|
|
$message->data .= $data; |
|
16
|
|
|
|
|
17
|
|
|
if (null === $message->header->get('id')) { |
|
18
|
|
|
if (null === $this->parseHeader($message)) { |
|
19
|
|
|
return; |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
if ($message->header->get('qdCount') != count($message->questions)) { |
|
24
|
|
|
if (null === $this->parseQuestion($message)) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if ($message->header->get('anCount') != count($message->answers)) { |
|
30
|
|
|
if (null === $this->parseAnswer($message)) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
return $message; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function parseHeader(Message $message) : Message |
|
39
|
|
|
{ |
|
40
|
|
|
if (strlen($message->data) < 12) { |
|
41
|
|
|
return; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$header = substr($message->data, 0, 12); |
|
45
|
|
|
$message->consumed += 12; |
|
46
|
|
|
|
|
47
|
|
|
list($id, $fields, $qdCount, $anCount, $nsCount, $arCount) = array_values(unpack('n*', $header)); |
|
48
|
|
|
|
|
49
|
|
|
$rcode = $fields & bindec('1111'); |
|
50
|
|
|
$z = ($fields >> 4) & bindec('111'); |
|
51
|
|
|
$ra = ($fields >> 7) & 1; |
|
52
|
|
|
$rd = ($fields >> 8) & 1; |
|
53
|
|
|
$tc = ($fields >> 9) & 1; |
|
54
|
|
|
$aa = ($fields >> 10) & 1; |
|
55
|
|
|
$opcode = ($fields >> 11) & bindec('1111'); |
|
56
|
|
|
$qr = ($fields >> 15) & 1; |
|
57
|
|
|
|
|
58
|
|
|
$vars = [ |
|
59
|
|
|
'id' => $id, |
|
60
|
|
|
'qdCount' => $qdCount, |
|
61
|
|
|
'anCount' => $anCount, |
|
62
|
|
|
'nsCount' => $nsCount, |
|
63
|
|
|
'arCount' => $arCount, |
|
64
|
|
|
'qr' => $qr, |
|
65
|
|
|
'opcode' => $opcode, |
|
66
|
|
|
'aa' => $aa, |
|
67
|
|
|
'tc' => $tc, |
|
68
|
|
|
'rd' => $rd, |
|
69
|
|
|
'ra' => $ra, |
|
70
|
|
|
'z' => $z, |
|
71
|
|
|
'rcode' => $rcode, |
|
72
|
|
|
]; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($vars as $name => $value) { |
|
75
|
|
|
$message->header->set($name, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $message; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function parseQuestion(Message $message) |
|
82
|
|
|
{ |
|
83
|
|
|
if (strlen($message->data) < 2) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$consumed = $message->consumed; |
|
88
|
|
|
|
|
89
|
|
|
list($labels, $consumed) = $this->readLabels($message->data, $consumed); |
|
90
|
|
|
|
|
91
|
|
|
if (null === $labels) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (strlen($message->data) - $consumed < 4) { |
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
list($type, $class) = array_values(unpack('n*', substr($message->data, $consumed, 4))); |
|
100
|
|
|
$consumed += 4; |
|
101
|
|
|
|
|
102
|
|
|
$message->consumed = $consumed; |
|
103
|
|
|
|
|
104
|
|
|
$message->questions[] = [ |
|
105
|
|
|
'name' => implode('.', $labels), |
|
106
|
|
|
'type' => $type, |
|
107
|
|
|
'class' => $class, |
|
108
|
|
|
]; |
|
109
|
|
|
|
|
110
|
|
|
if ($message->header->get('qdCount') != count($message->questions)) { |
|
111
|
|
|
return $this->parseQuestion($message); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $message; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function parseAnswer(Message $message) : Message |
|
118
|
|
|
{ |
|
119
|
|
|
if (strlen($message->data) < 2) { |
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$consumed = $message->consumed; |
|
124
|
|
|
|
|
125
|
|
|
list($labels, $consumed) = $this->readLabels($message->data, $consumed); |
|
126
|
|
|
|
|
127
|
|
|
if (null === $labels) { |
|
128
|
|
|
return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (strlen($message->data) - $consumed < 10) { |
|
132
|
|
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
list($type, $class) = array_values(unpack('n*', substr($message->data, $consumed, 4))); |
|
136
|
|
|
$consumed += 4; |
|
137
|
|
|
|
|
138
|
|
|
list($ttl) = array_values(unpack('N', substr($message->data, $consumed, 4))); |
|
139
|
|
|
$consumed += 4; |
|
140
|
|
|
|
|
141
|
|
|
list($rdLength) = array_values(unpack('n', substr($message->data, $consumed, 2))); |
|
142
|
|
|
$consumed += 2; |
|
143
|
|
|
|
|
144
|
|
|
$rdata = null; |
|
145
|
|
|
|
|
146
|
|
|
if (Message::TYPE_A === $type) { |
|
147
|
|
|
$ip = substr($message->data, $consumed, $rdLength); |
|
148
|
|
|
$consumed += $rdLength; |
|
149
|
|
|
|
|
150
|
|
|
$rdata = inet_ntop($ip); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (Message::TYPE_CNAME === $type) { |
|
154
|
|
|
list($bodyLabels, $consumed) = $this->readLabels($message->data, $consumed); |
|
155
|
|
|
|
|
156
|
|
|
$rdata = implode('.', $bodyLabels); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$message->consumed = $consumed; |
|
160
|
|
|
|
|
161
|
|
|
$name = implode('.', $labels); |
|
162
|
|
|
$ttl = $this->signedLongToUnsignedLong($ttl); |
|
163
|
|
|
$record = new Record($name, $type, $class, $ttl, $rdata); |
|
164
|
|
|
|
|
165
|
|
|
$message->answers[] = $record; |
|
166
|
|
|
|
|
167
|
|
|
if ($message->header->get('anCount') != count($message->answers)) { |
|
168
|
|
|
return $this->parseAnswer($message); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $message; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
private function readLabels($data, $consumed) |
|
175
|
|
|
{ |
|
176
|
|
|
$labels = []; |
|
177
|
|
|
|
|
178
|
|
|
while (true) { |
|
179
|
|
|
if ($this->isEndOfLabels($data, $consumed)) { |
|
180
|
|
|
$consumed += 1; |
|
181
|
|
|
break; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
if ($this->isCompressedLabel($data, $consumed)) { |
|
185
|
|
|
list($newLabels, $consumed) = $this->getCompressedLabel($data, $consumed); |
|
186
|
|
|
$labels = array_merge($labels, $newLabels); |
|
187
|
|
|
break; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$length = ord(substr($data, $consumed, 1)); |
|
191
|
|
|
$consumed += 1; |
|
192
|
|
|
|
|
193
|
|
|
if (strlen($data) - $consumed < $length) { |
|
194
|
|
|
return [null, null]; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$labels[] = substr($data, $consumed, $length); |
|
198
|
|
|
$consumed += $length; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return [$labels, $consumed]; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function isEndOfLabels($data, $consumed) : bool |
|
205
|
|
|
{ |
|
206
|
|
|
$length = ord(substr($data, $consumed, 1)); |
|
207
|
|
|
|
|
208
|
|
|
return 0 === $length; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function getCompressedLabel($data, $consumed) : array |
|
212
|
|
|
{ |
|
213
|
|
|
list($nameOffset, $consumed) = $this->getCompressedLabelOffset($data, $consumed); |
|
214
|
|
|
list($labels) = $this->readLabels($data, $nameOffset); |
|
215
|
|
|
|
|
216
|
|
|
return [$labels, $consumed]; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
View Code Duplication |
public function isCompressedLabel($data, $consumed) : bool |
|
|
|
|
|
|
220
|
|
|
{ |
|
221
|
|
|
$mask = 0xc000; // 1100000000000000 |
|
222
|
|
|
list($peek) = array_values(unpack('n', substr($data, $consumed, 2))); |
|
223
|
|
|
|
|
224
|
|
|
return (bool)($peek & $mask); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
View Code Duplication |
public function getCompressedLabelOffset($data, $consumed) : array |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
$mask = 0x3fff; // 0011111111111111 |
|
230
|
|
|
list($peek) = array_values(unpack('n', substr($data, $consumed, 2))); |
|
231
|
|
|
|
|
232
|
|
|
return [$peek & $mask, $consumed + 2]; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function signedLongToUnsignedLong($i) : int |
|
236
|
|
|
{ |
|
237
|
|
|
return $i & 0x80000000 ? $i - 0xffffffff : $i; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.