blob: 35d6ae601a606ee07a67f5c77b4b5c3cc5051950 (
plain) (
blame)
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
|
<?php
namespace React\Dns\Protocol;
use React\Dns\Model\Message;
use React\Dns\Model\HeaderBag;
class BinaryDumper
{
public function toBinary(Message $message)
{
$data = '';
$data .= $this->headerToBinary($message->header);
$data .= $this->questionToBinary($message->questions);
return $data;
}
private function headerToBinary(HeaderBag $header)
{
$data = '';
$data .= pack('n', $header->get('id'));
$flags = 0x00;
$flags = ($flags << 1) | $header->get('qr');
$flags = ($flags << 4) | $header->get('opcode');
$flags = ($flags << 1) | $header->get('aa');
$flags = ($flags << 1) | $header->get('tc');
$flags = ($flags << 1) | $header->get('rd');
$flags = ($flags << 1) | $header->get('ra');
$flags = ($flags << 3) | $header->get('z');
$flags = ($flags << 4) | $header->get('rcode');
$data .= pack('n', $flags);
$data .= pack('n', $header->get('qdCount'));
$data .= pack('n', $header->get('anCount'));
$data .= pack('n', $header->get('nsCount'));
$data .= pack('n', $header->get('arCount'));
return $data;
}
private function questionToBinary(array $questions)
{
$data = '';
foreach ($questions as $question) {
$labels = explode('.', $question['name']);
foreach ($labels as $label) {
$data .= chr(strlen($label)).$label;
}
$data .= "\x00";
$data .= pack('n*', $question['type'], $question['class']);
}
return $data;
}
}
|