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\Message; |
17
|
|
|
use Badcow\DNS\Parser\TimeFormat; |
18
|
|
|
use Badcow\DNS\Parser\Tokens; |
19
|
|
|
use InvalidArgumentException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @see https://tools.ietf.org/html/rfc1035#section-3.3.13 |
23
|
|
|
*/ |
24
|
|
|
class SOA implements RdataInterface |
25
|
|
|
{ |
26
|
|
|
use RdataTrait; |
27
|
|
|
|
28
|
|
|
const TYPE = 'SOA'; |
29
|
|
|
const TYPE_CODE = 6; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The <domain-name> of the name server that was the |
33
|
|
|
* original or primary source of data for this zone. |
34
|
|
|
* |
35
|
|
|
* @var string|null |
36
|
|
|
*/ |
37
|
|
|
private $mname; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A <domain-name> which specifies the mailbox of the |
41
|
|
|
* person responsible for this zone. |
42
|
|
|
* |
43
|
|
|
* @var string|null |
44
|
|
|
*/ |
45
|
|
|
private $rname; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The unsigned 32 bit version number of the original copy |
49
|
|
|
* of the zone. |
50
|
|
|
* |
51
|
|
|
* @var int|null |
52
|
|
|
*/ |
53
|
|
|
private $serial; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* A 32 bit time interval before the zone should be |
57
|
|
|
* refreshed. |
58
|
|
|
* |
59
|
|
|
* @var int|null |
60
|
|
|
*/ |
61
|
|
|
private $refresh; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* A 32 bit time interval that should elapse before a |
65
|
|
|
* failed refresh should be retried. |
66
|
|
|
* |
67
|
|
|
* @var int|null |
68
|
|
|
*/ |
69
|
|
|
private $retry; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* A 32 bit time value that specifies the upper limit on |
73
|
|
|
* the time interval that can elapse before the zone is no |
74
|
|
|
* longer authoritative. |
75
|
|
|
* |
76
|
|
|
* @var int|null |
77
|
|
|
*/ |
78
|
|
|
private $expire; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* The unsigned 32 bit minimum TTL field that should be |
82
|
|
|
* exported with any RR from this zone. |
83
|
|
|
* |
84
|
|
|
* @var int|null |
85
|
|
|
*/ |
86
|
|
|
private $minimum; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param int $expire |
90
|
|
|
*/ |
91
|
28 |
|
public function setExpire(int $expire): void |
92
|
|
|
{ |
93
|
28 |
|
$this->expire = $expire; |
94
|
28 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
6 |
|
public function getExpire(): ?int |
100
|
|
|
{ |
101
|
6 |
|
return $this->expire; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param int $minimum |
106
|
|
|
*/ |
107
|
28 |
|
public function setMinimum(int $minimum): void |
108
|
|
|
{ |
109
|
28 |
|
$this->minimum = $minimum; |
110
|
28 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return int |
114
|
|
|
*/ |
115
|
6 |
|
public function getMinimum(): ?int |
116
|
|
|
{ |
117
|
6 |
|
return $this->minimum; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $mname |
122
|
|
|
*/ |
123
|
28 |
|
public function setMname(string $mname): void |
124
|
|
|
{ |
125
|
28 |
|
$this->mname = $mname; |
126
|
28 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
5 |
|
public function getMname(): ?string |
132
|
|
|
{ |
133
|
5 |
|
return $this->mname; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param int $refresh |
138
|
|
|
*/ |
139
|
28 |
|
public function setRefresh(int $refresh): void |
140
|
|
|
{ |
141
|
28 |
|
$this->refresh = $refresh; |
142
|
28 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
6 |
|
public function getRefresh(): ?int |
148
|
|
|
{ |
149
|
6 |
|
return $this->refresh; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param int $retry |
154
|
|
|
*/ |
155
|
28 |
|
public function setRetry(int $retry): void |
156
|
|
|
{ |
157
|
28 |
|
$this->retry = (int) $retry; |
158
|
28 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return int |
162
|
|
|
*/ |
163
|
6 |
|
public function getRetry(): ?int |
164
|
|
|
{ |
165
|
6 |
|
return $this->retry; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param string $rname |
170
|
|
|
*/ |
171
|
28 |
|
public function setRname(string $rname): void |
172
|
|
|
{ |
173
|
28 |
|
$this->rname = $rname; |
174
|
28 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
5 |
|
public function getRname(): ?string |
180
|
|
|
{ |
181
|
5 |
|
return $this->rname; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param int $serial |
186
|
|
|
*/ |
187
|
28 |
|
public function setSerial(int $serial): void |
188
|
|
|
{ |
189
|
28 |
|
$this->serial = $serial; |
190
|
28 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
5 |
|
public function getSerial(): ?int |
196
|
|
|
{ |
197
|
5 |
|
return $this->serial; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
8 |
|
public function toText(): string |
204
|
|
|
{ |
205
|
8 |
|
if (!isset($this->mname) || |
206
|
8 |
|
!isset($this->rname) || |
207
|
8 |
|
!isset($this->serial) || |
208
|
8 |
|
!isset($this->refresh) || |
209
|
8 |
|
!isset($this->retry) || |
210
|
8 |
|
!isset($this->expire) || |
211
|
8 |
|
!isset($this->minimum)) { |
212
|
|
|
throw new InvalidArgumentException('All parameters of SOA must be set.'); |
213
|
|
|
} |
214
|
|
|
|
215
|
8 |
|
return sprintf( |
216
|
8 |
|
'%s %s %s %s %s %s %s', |
217
|
8 |
|
$this->mname, |
218
|
8 |
|
$this->rname, |
219
|
8 |
|
$this->serial, |
220
|
8 |
|
$this->refresh, |
221
|
8 |
|
$this->retry, |
222
|
8 |
|
$this->expire, |
223
|
8 |
|
$this->minimum |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* {@inheritdoc} |
229
|
|
|
*/ |
230
|
1 |
|
public function toWire(): string |
231
|
|
|
{ |
232
|
1 |
|
if (!isset($this->mname) || |
233
|
1 |
|
!isset($this->rname) || |
234
|
1 |
|
!isset($this->serial) || |
235
|
1 |
|
!isset($this->refresh) || |
236
|
1 |
|
!isset($this->retry) || |
237
|
1 |
|
!isset($this->expire) || |
238
|
1 |
|
!isset($this->minimum)) { |
239
|
|
|
throw new InvalidArgumentException('All parameters of SOA must be set.'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return |
243
|
1 |
|
Message::encodeName($this->mname). |
|
|
|
|
244
|
1 |
|
Message::encodeName($this->rname). |
245
|
|
|
pack( |
246
|
1 |
|
'NNNNN', |
247
|
1 |
|
$this->serial, |
248
|
1 |
|
$this->refresh, |
249
|
1 |
|
$this->retry, |
250
|
1 |
|
$this->expire, |
251
|
1 |
|
$this->minimum |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* {@inheritdoc} |
257
|
|
|
*/ |
258
|
12 |
|
public function fromText(string $text): void |
259
|
|
|
{ |
260
|
12 |
|
$rdata = explode(Tokens::SPACE, $text); |
261
|
|
|
|
262
|
12 |
|
$this->setMname($rdata[0]); |
263
|
12 |
|
$this->setRname($rdata[1]); |
264
|
12 |
|
$this->setSerial((int) $rdata[2]); |
265
|
12 |
|
$this->setRefresh(TimeFormat::toSeconds($rdata[3])); |
266
|
12 |
|
$this->setRetry(TimeFormat::toSeconds($rdata[4])); |
267
|
12 |
|
$this->setExpire(TimeFormat::toSeconds($rdata[5])); |
268
|
12 |
|
$this->setMinimum(TimeFormat::toSeconds($rdata[6])); |
269
|
12 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* {@inheritdoc} |
273
|
|
|
*/ |
274
|
1 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
275
|
|
|
{ |
276
|
1 |
|
$this->setMname(Message::decodeName($rdata, $offset)); |
277
|
1 |
|
$this->setRname(Message::decodeName($rdata, $offset)); |
278
|
1 |
|
$parameters = unpack('Nserial/Nrefresh/Nretry/Nexpire/Nminimum', $rdata, $offset); |
279
|
|
|
|
280
|
1 |
|
$this->setSerial((int) $parameters['serial']); |
281
|
1 |
|
$this->setRefresh(TimeFormat::toSeconds($parameters['refresh'])); |
282
|
1 |
|
$this->setRetry(TimeFormat::toSeconds($parameters['retry'])); |
283
|
1 |
|
$this->setExpire(TimeFormat::toSeconds($parameters['expire'])); |
284
|
1 |
|
$this->setMinimum(TimeFormat::toSeconds($parameters['minimum'])); |
285
|
|
|
|
286
|
1 |
|
$offset += 20; |
287
|
1 |
|
} |
288
|
|
|
} |
289
|
|
|
|