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\Tokens; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@link https://tools.ietf.org/html/rfc7477}. |
20
|
|
|
*/ |
21
|
|
|
class CSYNC implements RdataInterface |
22
|
|
|
{ |
23
|
|
|
use RdataTrait; |
24
|
|
|
|
25
|
|
|
const TYPE = 'CSYNC'; |
26
|
|
|
const TYPE_CODE = 62; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $soaSerial; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $flags; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $types = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $type |
45
|
|
|
*/ |
46
|
6 |
|
public function addType(string $type): void |
47
|
|
|
{ |
48
|
6 |
|
$this->types[] = $type; |
49
|
6 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Clears the types from the RDATA. |
53
|
|
|
*/ |
54
|
1 |
|
public function clearTypes(): void |
55
|
|
|
{ |
56
|
1 |
|
$this->types = []; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
2 |
|
public function getTypes(): array |
63
|
|
|
{ |
64
|
2 |
|
return $this->types; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
1 |
|
public function getSoaSerial(): int |
71
|
|
|
{ |
72
|
1 |
|
return $this->soaSerial; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $soaSerial |
77
|
|
|
*/ |
78
|
5 |
|
public function setSoaSerial(int $soaSerial): void |
79
|
|
|
{ |
80
|
5 |
|
$this->soaSerial = $soaSerial; |
81
|
5 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
1 |
|
public function getFlags(): int |
87
|
|
|
{ |
88
|
1 |
|
return $this->flags; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int $flags |
93
|
|
|
*/ |
94
|
5 |
|
public function setFlags(int $flags): void |
95
|
|
|
{ |
96
|
5 |
|
$this->flags = $flags; |
97
|
5 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function toText(): string |
103
|
|
|
{ |
104
|
1 |
|
return sprintf('%d %d %s', $this->soaSerial, $this->flags, implode(Tokens::SPACE, $this->types)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
* |
110
|
|
|
* @throws UnsupportedTypeException |
111
|
|
|
*/ |
112
|
1 |
|
public function toWire(): string |
113
|
|
|
{ |
114
|
1 |
|
return pack('Nn', $this->soaSerial, $this->flags).NSEC::renderBitmap($this->types); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
|
public static function fromText(string $text): RdataInterface |
121
|
|
|
{ |
122
|
1 |
|
$rdata = explode(Tokens::SPACE, $text); |
123
|
1 |
|
$csync = new static(); |
124
|
1 |
|
$csync->setSoaSerial((int) array_shift($rdata)); |
125
|
1 |
|
$csync->setFlags((int) array_shift($rdata)); |
126
|
1 |
|
array_map([$csync, 'addType'], $rdata); |
127
|
|
|
|
128
|
1 |
|
return $csync; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
* |
134
|
|
|
* @throws UnsupportedTypeException |
135
|
|
|
*/ |
136
|
1 |
|
public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface |
137
|
|
|
{ |
138
|
1 |
|
$integers = unpack('Nserial/nflags', $rdata, $offset); |
139
|
1 |
|
$offset += 6; |
140
|
1 |
|
$types = NSEC::parseBitmap($rdata, $offset); |
141
|
|
|
|
142
|
1 |
|
$csync = new static(); |
143
|
1 |
|
$csync->setSoaSerial((int) $integers['serial']); |
144
|
1 |
|
$csync->setFlags((int) $integers['flags']); |
145
|
1 |
|
array_map([$csync, 'addType'], $types); |
146
|
|
|
|
147
|
1 |
|
return $csync; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|