1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """DNS Rdata Types.
17
18 @var _by_text: The rdata type textual name to value mapping
19 @type _by_text: dict
20 @var _by_value: The rdata type value to textual name mapping
21 @type _by_value: dict
22 @var _metatypes: If an rdatatype is a metatype, there will be a mapping
23 whose key is the rdatatype value and whose value is True in this dictionary.
24 @type _metatypes: dict
25 @var _singletons: If an rdatatype is a singleton, there will be a mapping
26 whose key is the rdatatype value and whose value is True in this dictionary.
27 @type _singletons: dict"""
28
29 import re
30
31 import dns.exception
32
33 NONE = 0
34 A = 1
35 NS = 2
36 MD = 3
37 MF = 4
38 CNAME = 5
39 SOA = 6
40 MB = 7
41 MG = 8
42 MR = 9
43 NULL = 10
44 WKS = 11
45 PTR = 12
46 HINFO = 13
47 MINFO = 14
48 MX = 15
49 TXT = 16
50 RP = 17
51 AFSDB = 18
52 X25 = 19
53 ISDN = 20
54 RT = 21
55 NSAP = 22
56 NSAP_PTR = 23
57 SIG = 24
58 KEY = 25
59 PX = 26
60 GPOS = 27
61 AAAA = 28
62 LOC = 29
63 NXT = 30
64 SRV = 33
65 NAPTR = 35
66 KX = 36
67 CERT = 37
68 A6 = 38
69 DNAME = 39
70 OPT = 41
71 APL = 42
72 DS = 43
73 SSHFP = 44
74 IPSECKEY = 45
75 RRSIG = 46
76 NSEC = 47
77 DNSKEY = 48
78 DHCID = 49
79 NSEC3 = 50
80 NSEC3PARAM = 51
81 TLSA = 52
82 HIP = 55
83 CDS = 59
84 CDNSKEY = 60
85 CSYNC = 62
86 SPF = 99
87 UNSPEC = 103
88 EUI48 = 108
89 EUI64 = 109
90 TKEY = 249
91 TSIG = 250
92 IXFR = 251
93 AXFR = 252
94 MAILB = 253
95 MAILA = 254
96 ANY = 255
97 URI = 256
98 CAA = 257
99 AVC = 258
100 TA = 32768
101 DLV = 32769
102
103 _by_text = {
104 'NONE': NONE,
105 'A': A,
106 'NS': NS,
107 'MD': MD,
108 'MF': MF,
109 'CNAME': CNAME,
110 'SOA': SOA,
111 'MB': MB,
112 'MG': MG,
113 'MR': MR,
114 'NULL': NULL,
115 'WKS': WKS,
116 'PTR': PTR,
117 'HINFO': HINFO,
118 'MINFO': MINFO,
119 'MX': MX,
120 'TXT': TXT,
121 'RP': RP,
122 'AFSDB': AFSDB,
123 'X25': X25,
124 'ISDN': ISDN,
125 'RT': RT,
126 'NSAP': NSAP,
127 'NSAP-PTR': NSAP_PTR,
128 'SIG': SIG,
129 'KEY': KEY,
130 'PX': PX,
131 'GPOS': GPOS,
132 'AAAA': AAAA,
133 'LOC': LOC,
134 'NXT': NXT,
135 'SRV': SRV,
136 'NAPTR': NAPTR,
137 'KX': KX,
138 'CERT': CERT,
139 'A6': A6,
140 'DNAME': DNAME,
141 'OPT': OPT,
142 'APL': APL,
143 'DS': DS,
144 'SSHFP': SSHFP,
145 'IPSECKEY': IPSECKEY,
146 'RRSIG': RRSIG,
147 'NSEC': NSEC,
148 'DNSKEY': DNSKEY,
149 'DHCID': DHCID,
150 'NSEC3': NSEC3,
151 'NSEC3PARAM': NSEC3PARAM,
152 'TLSA': TLSA,
153 'HIP': HIP,
154 'CDS': CDS,
155 'CDNSKEY': CDNSKEY,
156 'CSYNC': CSYNC,
157 'SPF': SPF,
158 'UNSPEC': UNSPEC,
159 'EUI48': EUI48,
160 'EUI64': EUI64,
161 'TKEY': TKEY,
162 'TSIG': TSIG,
163 'IXFR': IXFR,
164 'AXFR': AXFR,
165 'MAILB': MAILB,
166 'MAILA': MAILA,
167 'ANY': ANY,
168 'URI': URI,
169 'CAA': CAA,
170 'AVC': AVC,
171 'TA': TA,
172 'DLV': DLV,
173 }
174
175
176
177
178
179 _by_value = dict((y, x) for x, y in _by_text.items())
180
181
182 _metatypes = {
183 OPT: True
184 }
185
186 _singletons = {
187 SOA: True,
188 NXT: True,
189 DNAME: True,
190 NSEC: True,
191
192 }
193
194 _unknown_type_pattern = re.compile('TYPE([0-9]+)$', re.I)
195
196
198
199 """DNS resource record type is unknown."""
200
201
202 -def from_text(text):
203 """Convert text into a DNS rdata type value.
204 @param text: the text
205 @type text: string
206 @raises dns.rdatatype.UnknownRdatatype: the type is unknown
207 @raises ValueError: the rdata type value is not >= 0 and <= 65535
208 @rtype: int"""
209
210 value = _by_text.get(text.upper())
211 if value is None:
212 match = _unknown_type_pattern.match(text)
213 if match is None:
214 raise UnknownRdatatype
215 value = int(match.group(1))
216 if value < 0 or value > 65535:
217 raise ValueError("type must be between >= 0 and <= 65535")
218 return value
219
220
222 """Convert a DNS rdata type to text.
223 @param value: the rdata type value
224 @type value: int
225 @raises ValueError: the rdata type value is not >= 0 and <= 65535
226 @rtype: string"""
227
228 if value < 0 or value > 65535:
229 raise ValueError("type must be between >= 0 and <= 65535")
230 text = _by_value.get(value)
231 if text is None:
232 text = 'TYPE' + repr(value)
233 return text
234
235
245
246
248 """True if the type is a singleton.
249 @param rdtype: the type
250 @type rdtype: int
251 @rtype: bool"""
252
253 if rdtype in _singletons:
254 return True
255 return False
256