1 # Copyright (C) 2001-2007, 2009-2011 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 @var _by_text: The rdata type textual name to value mapping
20 @var _by_value: The rdata type value to textual name mapping
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"""
118 'NSAP-PTR' : NSAP_PTR,
136 'IPSECKEY' : IPSECKEY,
142 'NSEC3PARAM' : NSEC3PARAM,
157 # We construct the inverse mapping programmatically to ensure that we
158 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
159 # would cause the mapping not to be true inverse.
161 _by_value = dict([(y, x) for x, y in _by_text.iteritems()])
173 # CNAME is technically a singleton, but we allow multiple CNAMEs.
176 _unknown_type_pattern = re.compile('TYPE([0-9]+)$', re.I);
178 class UnknownRdatatype(dns.exception.DNSException):
179 """Raised if a type is unknown."""
183 """Convert text into a DNS rdata type value.
184 @param text: the text
186 @raises dns.rdatatype.UnknownRdatatype: the type is unknown
187 @raises ValueError: the rdata type value is not >= 0 and <= 65535
190 value = _by_text.get(text.upper())
192 match = _unknown_type_pattern.match(text)
194 raise UnknownRdatatype
195 value = int(match.group(1))
196 if value < 0 or value > 65535:
197 raise ValueError("type must be between >= 0 and <= 65535")
201 """Convert a DNS rdata type to text.
202 @param value: the rdata type value
204 @raises ValueError: the rdata type value is not >= 0 and <= 65535
207 if value < 0 or value > 65535:
208 raise ValueError("type must be between >= 0 and <= 65535")
209 text = _by_value.get(value)
211 text = 'TYPE' + `value`
214 def is_metatype(rdtype):
215 """True if the type is a metatype.
216 @param rdtype: the type
220 if rdtype >= TKEY and rdtype <= ANY or _metatypes.has_key(rdtype):
224 def is_singleton(rdtype):
225 """True if the type is a singleton.
226 @param rdtype: the type
230 if _singletons.has_key(rdtype):