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; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Rdata\RdataInterface; |
17
|
|
|
use Badcow\DNS\Rdata\RdataTrait; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
class ResourceRecord |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var int|null |
24
|
|
|
*/ |
25
|
|
|
private $classId = 1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var RdataInterface|null |
29
|
|
|
*/ |
30
|
|
|
private $rdata; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int|null |
34
|
|
|
*/ |
35
|
|
|
private $ttl; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $name; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string|null |
44
|
|
|
*/ |
45
|
|
|
private $comment; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $name |
49
|
|
|
* @param RdataInterface $rdata |
50
|
|
|
* @param int $ttl |
51
|
|
|
* @param string $class |
52
|
|
|
* @param string $comment |
53
|
|
|
* |
54
|
|
|
* @return ResourceRecord |
55
|
|
|
*/ |
56
|
16 |
|
public static function create(string $name, RdataInterface $rdata, int $ttl = null, string $class = Classes::INTERNET, string $comment = null): ResourceRecord |
57
|
|
|
{ |
58
|
16 |
|
$rr = new self(); |
59
|
16 |
|
$rr->setName($name); |
60
|
16 |
|
$rr->setRdata($rdata); |
61
|
16 |
|
$rr->setTtl($ttl); |
62
|
16 |
|
$rr->setClass($class); |
63
|
16 |
|
$rr->setComment($comment); |
64
|
|
|
|
65
|
16 |
|
return $rr; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the class for the resource record |
70
|
|
|
* Usually one of IN, HS, or CH. |
71
|
|
|
* |
72
|
|
|
* @param string $class |
73
|
|
|
* |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
*/ |
76
|
39 |
|
public function setClass(?string $class): void |
77
|
|
|
{ |
78
|
39 |
|
if (null !== $class && !Classes::isValid($class)) { |
79
|
1 |
|
throw new InvalidArgumentException(sprintf('No such class as "%s"', $class)); |
80
|
|
|
} |
81
|
|
|
|
82
|
39 |
|
if (null === $class) { |
83
|
1 |
|
$this->classId = null; |
84
|
|
|
|
85
|
1 |
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
39 |
|
$this->classId = Classes::getClassId($class); |
89
|
39 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set the name for the resource record. |
93
|
|
|
* Eg. "subdomain.example.com.". |
94
|
|
|
* |
95
|
|
|
* @param string $name |
96
|
|
|
*/ |
97
|
40 |
|
public function setName(string $name): void |
98
|
|
|
{ |
99
|
40 |
|
$this->name = $name; |
100
|
40 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param RdataInterface $rdata |
104
|
|
|
*/ |
105
|
37 |
|
public function setRdata(?RdataInterface $rdata): void |
106
|
|
|
{ |
107
|
37 |
|
$this->rdata = $rdata; |
108
|
37 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
32 |
|
public function getClass(): ?string |
114
|
|
|
{ |
115
|
32 |
|
if (null === $this->classId) { |
116
|
1 |
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
31 |
|
return Classes::getClassName($this->classId); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function setClassId(int $classId): void |
123
|
|
|
{ |
124
|
|
|
$this->classId = $classId; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return int|null |
129
|
|
|
*/ |
130
|
2 |
|
public function getClassId(): ?int |
131
|
|
|
{ |
132
|
2 |
|
return $this->classId; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set the time to live. |
137
|
|
|
* |
138
|
|
|
* @param int $ttl |
139
|
|
|
*/ |
140
|
40 |
|
public function setTtl(?int $ttl): void |
141
|
|
|
{ |
142
|
40 |
|
$this->ttl = $ttl; |
143
|
40 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
31 |
|
public function getName(): ?string |
149
|
|
|
{ |
150
|
31 |
|
return $this->name; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
26 |
|
public function getType(): ?string |
157
|
|
|
{ |
158
|
26 |
|
if (null === $this->rdata) { |
159
|
2 |
|
return null; |
160
|
|
|
} |
161
|
|
|
|
162
|
25 |
|
return $this->rdata->getType(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return RdataInterface |
167
|
|
|
*/ |
168
|
26 |
|
public function getRdata(): ?RdataInterface |
169
|
|
|
{ |
170
|
26 |
|
return $this->rdata; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
28 |
|
public function getTtl(): ?int |
177
|
|
|
{ |
178
|
28 |
|
return $this->ttl; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set a comment for the record. |
183
|
|
|
* |
184
|
|
|
* @param string $comment |
185
|
|
|
*/ |
186
|
39 |
|
public function setComment(?string $comment): void |
187
|
|
|
{ |
188
|
39 |
|
$this->comment = $comment; |
189
|
39 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get the record's comment. |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
10 |
|
public function getComment(): ?string |
197
|
|
|
{ |
198
|
10 |
|
return $this->comment; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return string |
203
|
|
|
* |
204
|
|
|
* @throws UnsetValueException|InvalidArgumentException |
205
|
|
|
*/ |
206
|
1 |
|
public function toWire(): string |
207
|
|
|
{ |
208
|
1 |
|
if (null === $this->name) { |
209
|
|
|
throw new UnsetValueException('ResourceRecord name has not been set.'); |
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
if (null === $this->rdata) { |
213
|
|
|
throw new UnsetValueException('ResourceRecord rdata has not been set.'); |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
if (null === $this->classId) { |
217
|
|
|
throw new UnsetValueException('ResourceRecord class has not been set.'); |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
if (null === $this->ttl) { |
221
|
|
|
throw new UnsetValueException('ResourceRecord TTL has not been set.'); |
222
|
|
|
} |
223
|
|
|
|
224
|
1 |
|
if (!Validator::fullyQualifiedDomainName($this->name)) { |
225
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not a fully qualified domain name.', $this->name)); |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
$rdata = $this->rdata->toWire(); |
229
|
|
|
|
230
|
1 |
|
$encoded = RdataTrait::encodeName($this->name); |
231
|
1 |
|
$encoded .= pack('nnNn', |
232
|
1 |
|
$this->rdata->getTypeCode(), |
233
|
1 |
|
$this->classId, |
234
|
1 |
|
$this->ttl, |
235
|
1 |
|
strlen($rdata) |
236
|
|
|
); |
237
|
1 |
|
$encoded .= $rdata; |
238
|
|
|
|
239
|
1 |
|
return $encoded; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|