Debian Codegraph @

dns/rdatatype.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 # Copyright (C) 2001-2017 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 """DNS Rdata Types."""
19
20 from typing import Dict
21
22 import dns.enum
23 import dns.exception
24
25
26 class RdataType(dns.enum.IntEnum):
27 """DNS Rdata Type"""
28
29 TYPE0 = 0
30 NONE = 0
31 A = 1
32 NS = 2
33 MD = 3
34 MF = 4
35 CNAME = 5
36 SOA = 6
37 MB = 7
38 MG = 8
39 MR = 9
40 NULL = 10
41 WKS = 11
42 PTR = 12
43 HINFO = 13
44 MINFO = 14
45 MX = 15
46 TXT = 16
47 RP = 17
48 AFSDB = 18
49 X25 = 19
50 ISDN = 20
51 RT = 21
52 NSAP = 22
53 NSAP_PTR = 23
54 SIG = 24
55 KEY = 25
56 PX = 26
57 GPOS = 27
58 AAAA = 28
59 LOC = 29
60 NXT = 30
61 SRV = 33
62 NAPTR = 35
63 KX = 36
64 CERT = 37
65 A6 = 38
66 DNAME = 39
67 OPT = 41
68 APL = 42
69 DS = 43
70 SSHFP = 44
71 IPSECKEY = 45
72 RRSIG = 46
73 NSEC = 47
74 DNSKEY = 48
75 DHCID = 49
76 NSEC3 = 50
77 NSEC3PARAM = 51
78 TLSA = 52
79 SMIMEA = 53
80 HIP = 55
81 NINFO = 56
82 CDS = 59
83 CDNSKEY = 60
84 OPENPGPKEY = 61
85 CSYNC = 62
86 ZONEMD = 63
87 SVCB = 64
88 HTTPS = 65
89 DSYNC = 66
90 SPF = 99
91 UNSPEC = 103
92 NID = 104
93 L32 = 105
94 L64 = 106
95 LP = 107
96 EUI48 = 108
97 EUI64 = 109
98 TKEY = 249
99 TSIG = 250
100 IXFR = 251
101 AXFR = 252
102 MAILB = 253
103 MAILA = 254
104 ANY = 255
105 URI = 256
106 CAA = 257
107 AVC = 258
108 AMTRELAY = 260
109 RESINFO = 261
110 WALLET = 262
111 TA = 32768
112 DLV = 32769
113
114 @classmethod
115 def _maximum(cls):
116 return 65535
117
118 @classmethod
119 def _short_name(cls):
120 return "type"
121
122 @classmethod
123 def _prefix(cls):
124 return "TYPE"
125
126 @classmethod
127 def _extra_from_text(cls, text):
128 if text.find("-") >= 0:
129 try:
130 return cls[text.replace("-", "_")]
131 except KeyError: # pragma: no cover
132 pass
133 return _registered_by_text.get(text)
134
135 @classmethod
136 def _extra_to_text(cls, value, current_text):
137 if current_text is None:
138 return _registered_by_value.get(value)
139 if current_text.find("_") >= 0:
140 return current_text.replace("_", "-")
141 return current_text
142
143 @classmethod
144 def _unknown_exception_class(cls):
145 return UnknownRdatatype
146
147
148 _registered_by_text: Dict[str, RdataType] = {}
149 _registered_by_value: Dict[RdataType, str] = {}
150
151 _metatypes = {RdataType.OPT}
152
153 _singletons = {
154 RdataType.SOA,
155 RdataType.NXT,
156 RdataType.DNAME,
157 RdataType.NSEC,
158 RdataType.CNAME,
159 }
160
161
162 class UnknownRdatatype(dns.exception.DNSException):
163 """DNS resource record type is unknown."""
164
165
166 def from_text(text: str) -> RdataType:
167 """Convert text into a DNS rdata type value.
168
169 The input text can be a defined DNS RR type mnemonic or
170 instance of the DNS generic type syntax.
171
172 For example, "NS" and "TYPE2" will both result in a value of 2.
173
174 Raises ``dns.rdatatype.UnknownRdatatype`` if the type is unknown.
175
176 Raises ``ValueError`` if the rdata type value is not >= 0 and <= 65535.
177
178 Returns a ``dns.rdatatype.RdataType``.
179 """
180
181 return RdataType.from_text(text)
182
183
184 def to_text(value: RdataType) -> str:
185 """Convert a DNS rdata type value to text.
186
187 If the value has a known mnemonic, it will be used, otherwise the
188 DNS generic type syntax will be used.
189
190 Raises ``ValueError`` if the rdata type value is not >= 0 and <= 65535.
191
192 Returns a ``str``.
193 """
194
195 return RdataType.to_text(value)
196
197
198 def is_metatype(rdtype: RdataType) -> bool:
199 """True if the specified type is a metatype.
200
201 *rdtype* is a ``dns.rdatatype.RdataType``.
202
203 The currently defined metatypes are TKEY, TSIG, IXFR, AXFR, MAILA,
204 MAILB, ANY, and OPT.
205
206 Returns a ``bool``.
207 """
208
209 return (256 > rdtype >= 128) or rdtype in _metatypes
210
211
212 def is_singleton(rdtype: RdataType) -> bool:
213 """Is the specified type a singleton type?
214
215 Singleton types can only have a single rdata in an rdataset, or a single
216 RR in an RRset.
217
218 The currently defined singleton types are CNAME, DNAME, NSEC, NXT, and
219 SOA.
220
221 *rdtype* is an ``int``.
222
223 Returns a ``bool``.
224 """
225
226 if rdtype in _singletons:
227 return True
228 return False
229
230
231 # pylint: disable=redefined-outer-name
232 def register_type(
233 rdtype: RdataType, rdtype_text: str, is_singleton: bool = False
234 ) -> None:
235 """Dynamically register an rdatatype.
236
237 *rdtype*, a ``dns.rdatatype.RdataType``, the rdatatype to register.
238
239 *rdtype_text*, a ``str``, the textual form of the rdatatype.
240
241 *is_singleton*, a ``bool``, indicating if the type is a singleton (i.e.
242 RRsets of the type can have only one member.)
243 """
244
245 _registered_by_text[rdtype_text] = rdtype
246 _registered_by_value[rdtype] = rdtype_text
247 if is_singleton:
248 _singletons.add(rdtype)
249
250
251 ### BEGIN generated RdataType constants
252
253 TYPE0 = RdataType.TYPE0
254 NONE = RdataType.NONE
255 A = RdataType.A
256 NS = RdataType.NS
257 MD = RdataType.MD
258 MF = RdataType.MF
259 CNAME = RdataType.CNAME
260 SOA = RdataType.SOA
261 MB = RdataType.MB
262 MG = RdataType.MG
263 MR = RdataType.MR
264 NULL = RdataType.NULL
265 WKS = RdataType.WKS
266 PTR = RdataType.PTR
267 HINFO = RdataType.HINFO
268 MINFO = RdataType.MINFO
269 MX = RdataType.MX
270 TXT = RdataType.TXT
271 RP = RdataType.RP
272 AFSDB = RdataType.AFSDB
273 X25 = RdataType.X25
274 ISDN = RdataType.ISDN
275 RT = RdataType.RT
276 NSAP = RdataType.NSAP
277 NSAP_PTR = RdataType.NSAP_PTR
278 SIG = RdataType.SIG
279 KEY = RdataType.KEY
280 PX = RdataType.PX
281 GPOS = RdataType.GPOS
282 AAAA = RdataType.AAAA
283 LOC = RdataType.LOC
284 NXT = RdataType.NXT
285 SRV = RdataType.SRV
286 NAPTR = RdataType.NAPTR
287 KX = RdataType.KX
288 CERT = RdataType.CERT
289 A6 = RdataType.A6
290 DNAME = RdataType.DNAME
291 OPT = RdataType.OPT
292 APL = RdataType.APL
293 DS = RdataType.DS
294 SSHFP = RdataType.SSHFP
295 IPSECKEY = RdataType.IPSECKEY
296 RRSIG = RdataType.RRSIG
297 NSEC = RdataType.NSEC
298 DNSKEY = RdataType.DNSKEY
299 DHCID = RdataType.DHCID
300 NSEC3 = RdataType.NSEC3
301 NSEC3PARAM = RdataType.NSEC3PARAM
302 TLSA = RdataType.TLSA
303 SMIMEA = RdataType.SMIMEA
304 HIP = RdataType.HIP
305 NINFO = RdataType.NINFO
306 CDS = RdataType.CDS
307 CDNSKEY = RdataType.CDNSKEY
308 OPENPGPKEY = RdataType.OPENPGPKEY
309 CSYNC = RdataType.CSYNC
310 ZONEMD = RdataType.ZONEMD
311 SVCB = RdataType.SVCB
312 HTTPS = RdataType.HTTPS
313 DSYNC = RdataType.DSYNC
314 SPF = RdataType.SPF
315 UNSPEC = RdataType.UNSPEC
316 NID = RdataType.NID
317 L32 = RdataType.L32
318 L64 = RdataType.L64
319 LP = RdataType.LP
320 EUI48 = RdataType.EUI48
321 EUI64 = RdataType.EUI64
322 TKEY = RdataType.TKEY
323 TSIG = RdataType.TSIG
324 IXFR = RdataType.IXFR
325 AXFR = RdataType.AXFR
326 MAILB = RdataType.MAILB
327 MAILA = RdataType.MAILA
328 ANY = RdataType.ANY
329 URI = RdataType.URI
330 CAA = RdataType.CAA
331 AVC = RdataType.AVC
332 AMTRELAY = RdataType.AMTRELAY
333 RESINFO = RdataType.RESINFO
334 WALLET = RdataType.WALLET
335 TA = RdataType.TA
336 DLV = RdataType.DLV
337
338 ### END generated RdataType constants