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