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\Validator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see https://tools.ietf.org/html/rfc1035#section-3.4.1 |
20
|
|
|
*/ |
21
|
|
|
class A implements RdataInterface |
22
|
|
|
{ |
23
|
|
|
use RdataTrait; |
24
|
|
|
|
25
|
|
|
const TYPE = 'A'; |
26
|
|
|
const TYPE_CODE = 1; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $address; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $address |
35
|
|
|
*/ |
36
|
37 |
|
public function setAddress(string $address): void |
37
|
|
|
{ |
38
|
37 |
|
if (!Validator::ipv4($address)) { |
39
|
1 |
|
throw new \InvalidArgumentException(sprintf('The address "%s" is not a valid IPv4 address.', $address)); |
40
|
|
|
} |
41
|
|
|
|
42
|
37 |
|
$this->address = $address; |
43
|
37 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
15 |
|
public function getAddress(): ?string |
49
|
|
|
{ |
50
|
15 |
|
return $this->address; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
7 |
|
public function toText(): string |
57
|
|
|
{ |
58
|
7 |
|
return $this->address ?? ''; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
* |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
*/ |
66
|
7 |
|
public function toWire(): string |
67
|
|
|
{ |
68
|
7 |
|
if (false === $encoded = @inet_pton($this->address)) { |
69
|
2 |
|
throw new \InvalidArgumentException(sprintf('The IP address "%s" cannot be encoded. Check that it is a valid IP address.', $this->address)); |
70
|
|
|
} |
71
|
|
|
|
72
|
5 |
|
return $encoded; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
10 |
|
public static function fromText(string $text): RdataInterface |
79
|
|
|
{ |
80
|
10 |
|
$a = new static(); |
81
|
10 |
|
$a->setAddress($text); |
82
|
|
|
|
83
|
10 |
|
return $a; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
* |
89
|
|
|
* @throws DecodeException |
90
|
|
|
*/ |
91
|
12 |
|
public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface |
92
|
|
|
{ |
93
|
12 |
|
if (false === $address = @inet_ntop(substr($rdata, $offset, 4))) { |
94
|
1 |
|
throw new DecodeException(static::TYPE, $rdata); |
95
|
|
|
} |
96
|
12 |
|
$offset += 4; |
97
|
|
|
|
98
|
12 |
|
$a = new static(); |
99
|
12 |
|
$a->setAddress($address); |
100
|
|
|
|
101
|
12 |
|
return $a; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|