|
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
|
|
|
|
|
15
|
|
|
class RdataEncoder |
|
16
|
|
|
{ |
|
17
|
|
|
private static $methodMap = [ |
|
18
|
|
|
RecordTypeEnum::TYPE_A => 'a', |
|
19
|
|
|
RecordTypeEnum::TYPE_AAAA => 'a', |
|
20
|
|
|
RecordTypeEnum::TYPE_CNAME => 'cname', |
|
21
|
|
|
RecordTypeEnum::TYPE_DNAME => 'cname', |
|
22
|
|
|
RecordTypeEnum::TYPE_NS => 'cname', |
|
23
|
|
|
RecordTypeEnum::TYPE_PTR => 'cname', |
|
24
|
|
|
RecordTypeEnum::TYPE_SOA => 'soa', |
|
25
|
|
|
RecordTypeEnum::TYPE_MX => 'mx', |
|
26
|
|
|
RecordTypeEnum::TYPE_TXT => 'txt', |
|
27
|
|
|
RecordTypeEnum::TYPE_SRV => 'srv', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $type |
|
32
|
|
|
* @param string|array $rdata |
|
33
|
|
|
* |
|
34
|
|
|
* @return string |
|
35
|
|
|
* |
|
36
|
|
|
* @throws UnsupportedTypeException|\InvalidArgumentException |
|
37
|
|
|
*/ |
|
38
|
11 |
|
public static function encodeRdata(int $type, $rdata): string |
|
39
|
|
|
{ |
|
40
|
11 |
|
if (!array_key_exists($type, self::$methodMap)) { |
|
41
|
1 |
|
throw new UnsupportedTypeException( |
|
42
|
1 |
|
sprintf('Record type "%s" is not a supported type.', RecordTypeEnum::getName($type)) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
10 |
|
return call_user_func(['self', self::$methodMap[$type]], $rdata); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Used for A and AAAA records. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $rdata |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
9 |
|
public static function a(string $rdata): string |
|
56
|
|
|
{ |
|
57
|
9 |
|
if (!filter_var($rdata, FILTER_VALIDATE_IP)) { |
|
58
|
2 |
|
throw new \InvalidArgumentException(sprintf('The IP address "%s" is invalid.', $rdata)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
7 |
|
return inet_pton($rdata); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Used for CNAME, DNAME, NS, and PTR records. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $rdata |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public static function cname(string $rdata): string |
|
71
|
|
|
{ |
|
72
|
2 |
|
return Encoder::encodeDomainName($rdata); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Exclusively for SOA records. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $rdata |
|
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
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
3 |
|
public static function mx(array $rdata): string |
|
103
|
|
|
{ |
|
104
|
3 |
|
return pack('n', (int) $rdata['preference']).Encoder::encodeDomainName($rdata['exchange']); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Exclusively for TXT records. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $rdata |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function txt(string $rdata): string |
|
114
|
|
|
{ |
|
115
|
1 |
|
$rdata = substr($rdata, 0, 255); |
|
116
|
|
|
|
|
117
|
1 |
|
return chr(strlen($rdata)).$rdata; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Exclusively for SRV records. |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $rdata |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
2 |
|
public static function srv(array $rdata): string |
|
127
|
|
|
{ |
|
128
|
2 |
|
return pack('nnn', (int) $rdata['priority'], (int) $rdata['weight'], (int) $rdata['port']). |
|
129
|
2 |
|
Encoder::encodeDomainName($rdata['target']); |
|
130
|
|
|
} |
|
131
|
|
|
} |