1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of PHP DNS Server. |
5
|
|
|
* |
6
|
|
|
* (c) Yif Swery <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace yswery\DNS; |
13
|
|
|
|
14
|
|
|
class RdataEncoder |
15
|
|
|
{ |
16
|
|
|
private static $methodMap = [ |
17
|
|
|
RecordTypeEnum::TYPE_A => 'a', |
18
|
|
|
RecordTypeEnum::TYPE_AAAA => 'a', |
19
|
|
|
RecordTypeEnum::TYPE_CNAME => 'cname', |
20
|
|
|
RecordTypeEnum::TYPE_DNAME => 'cname', |
21
|
|
|
RecordTypeEnum::TYPE_NS => 'cname', |
22
|
|
|
RecordTypeEnum::TYPE_PTR => 'cname', |
23
|
|
|
RecordTypeEnum::TYPE_SOA => 'soa', |
24
|
|
|
RecordTypeEnum::TYPE_MX => 'mx', |
25
|
|
|
RecordTypeEnum::TYPE_TXT => 'txt', |
26
|
|
|
RecordTypeEnum::TYPE_SRV => 'srv', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param int $type |
31
|
|
|
* @param string|array $rdata |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
* |
35
|
|
|
* @throws UnsupportedTypeException|\InvalidArgumentException |
36
|
|
|
*/ |
37
|
11 |
|
public static function encodeRdata(int $type, $rdata): string |
38
|
|
|
{ |
39
|
11 |
|
if (!array_key_exists($type, self::$methodMap)) { |
40
|
1 |
|
throw new UnsupportedTypeException(sprintf('Record type "%s" is not a supported type.', RecordTypeEnum::getName($type))); |
41
|
|
|
} |
42
|
|
|
|
43
|
10 |
|
return call_user_func(['self', self::$methodMap[$type]], $rdata); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Used for A and AAAA records. |
48
|
|
|
* |
49
|
|
|
* @param string $rdata |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
9 |
|
public static function a(string $rdata): string |
54
|
|
|
{ |
55
|
9 |
|
if (!filter_var($rdata, FILTER_VALIDATE_IP)) { |
56
|
2 |
|
throw new \InvalidArgumentException(sprintf('The IP address "%s" is invalid.', $rdata)); |
57
|
|
|
} |
58
|
|
|
|
59
|
7 |
|
return inet_pton($rdata); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Used for CNAME, DNAME, NS, and PTR records. |
64
|
|
|
* |
65
|
|
|
* @param string $rdata |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
2 |
|
public static function cname(string $rdata): string |
70
|
|
|
{ |
71
|
2 |
|
return Encoder::encodeDomainName($rdata); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Exclusively for SOA records. |
76
|
|
|
* |
77
|
|
|
* @param array $rdata |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
1 |
|
public static function soa(array $rdata): string |
82
|
|
|
{ |
83
|
|
|
return |
84
|
1 |
|
Encoder::encodeDomainName($rdata['mname']). |
85
|
1 |
|
Encoder::encodeDomainName($rdata['rname']). |
86
|
1 |
|
pack( |
87
|
1 |
|
'NNNNN', |
88
|
1 |
|
$rdata['serial'], |
89
|
1 |
|
$rdata['refresh'], |
90
|
1 |
|
$rdata['retry'], |
91
|
1 |
|
$rdata['expire'], |
92
|
1 |
|
$rdata['minimum'] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Exclusively for MX records. |
98
|
|
|
* |
99
|
|
|
* @param array $rdata |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
3 |
|
public static function mx(array $rdata): string |
104
|
|
|
{ |
105
|
3 |
|
return pack('n', (int) $rdata['preference']).Encoder::encodeDomainName($rdata['exchange']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Exclusively for TXT records. |
110
|
|
|
* |
111
|
|
|
* @param string $rdata |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
public static function txt(string $rdata): string |
116
|
|
|
{ |
117
|
1 |
|
$rdata = substr($rdata, 0, 255); |
118
|
|
|
|
119
|
1 |
|
return chr(strlen($rdata)).$rdata; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Exclusively for SRV records. |
124
|
|
|
* |
125
|
|
|
* @param array $rdata |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
2 |
|
public static function srv(array $rdata): string |
130
|
|
|
{ |
131
|
2 |
|
return pack('nnn', (int) $rdata['priority'], (int) $rdata['weight'], (int) $rdata['port']). |
132
|
2 |
|
Encoder::encodeDomainName($rdata['target']); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|