1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Rdata; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Parser\ParseException; |
17
|
|
|
use Badcow\DNS\Parser\Tokens; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@link https://tools.ietf.org/html/rfc3597}. |
21
|
|
|
*/ |
22
|
|
|
class UnknownType implements RdataInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $typeCode; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $data; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param int $typeCode |
36
|
|
|
*/ |
37
|
1 |
|
public function setTypeCode(int $typeCode): void |
38
|
|
|
{ |
39
|
1 |
|
$this->typeCode = $typeCode; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
public function getData(): ?string |
46
|
|
|
{ |
47
|
1 |
|
return $this->data; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $data |
52
|
|
|
*/ |
53
|
1 |
|
public function setData(?string $data): void |
54
|
|
|
{ |
55
|
1 |
|
$this->data = $data; |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
public function getType(): string |
59
|
|
|
{ |
60
|
|
|
return 'TYPE'.$this->typeCode; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
public function getTypeCode(): int |
64
|
|
|
{ |
65
|
1 |
|
return $this->typeCode; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function toText(): string |
69
|
|
|
{ |
70
|
|
|
if (null === $this->data) { |
71
|
|
|
return '\# 0'; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return sprintf('\# %d %s', strlen($this->data), bin2hex($this->data)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function toWire(): string |
78
|
|
|
{ |
79
|
|
|
return $this->data ?? ''; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $text |
84
|
|
|
* |
85
|
|
|
* @return UnknownType |
86
|
|
|
* |
87
|
|
|
* @throws ParseException |
88
|
|
|
*/ |
89
|
1 |
|
public static function fromText(string $text): RdataInterface |
90
|
|
|
{ |
91
|
1 |
|
if (1 !== preg_match('/^\\\#\s+(\d+)(\s[a-f0-9\s]+)?$/i', $text, $matches)) { |
92
|
|
|
throw new ParseException('Could not parse rdata of unknown type. Malformed string.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$rdata = new self(); |
96
|
1 |
|
if ('0' === $matches[1]) { |
97
|
1 |
|
return $rdata; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$hexVal = str_replace(Tokens::SPACE, '', $matches[2]); |
101
|
|
|
|
102
|
1 |
|
if (false === $data = hex2bin($hexVal)) { |
103
|
|
|
throw new ParseException(sprintf('Could not parse hexadecimal data "%s".', $hexVal)); |
104
|
|
|
} |
105
|
1 |
|
$rdata->setData($data); |
106
|
|
|
|
107
|
1 |
|
return $rdata; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @return UnknownType |
114
|
|
|
*/ |
115
|
|
|
public static function fromWire(string $rdata): RdataInterface |
116
|
|
|
{ |
117
|
|
|
$_rdata = new self(); |
118
|
|
|
$_rdata->setData($rdata); |
119
|
|
|
|
120
|
|
|
return $_rdata; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|