Package dns :: Module rdatatype
[hide private]
[frames] | no frames]

Source Code for Module dns.rdatatype

  1  # Copyright (C) 2001-2007, 2009-2011 Nominum, Inc. 
  2  # 
  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. 
  7  # 
  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. 
 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  TA = 32768 
100  DLV = 32769 
101   
102  _by_text = { 
103      'NONE': NONE, 
104      'A': A, 
105      'NS': NS, 
106      'MD': MD, 
107      'MF': MF, 
108      'CNAME': CNAME, 
109      'SOA': SOA, 
110      'MB': MB, 
111      'MG': MG, 
112      'MR': MR, 
113      'NULL': NULL, 
114      'WKS': WKS, 
115      'PTR': PTR, 
116      'HINFO': HINFO, 
117      'MINFO': MINFO, 
118      'MX': MX, 
119      'TXT': TXT, 
120      'RP': RP, 
121      'AFSDB': AFSDB, 
122      'X25': X25, 
123      'ISDN': ISDN, 
124      'RT': RT, 
125      'NSAP': NSAP, 
126      'NSAP-PTR': NSAP_PTR, 
127      'SIG': SIG, 
128      'KEY': KEY, 
129      'PX': PX, 
130      'GPOS': GPOS, 
131      'AAAA': AAAA, 
132      'LOC': LOC, 
133      'NXT': NXT, 
134      'SRV': SRV, 
135      'NAPTR': NAPTR, 
136      'KX': KX, 
137      'CERT': CERT, 
138      'A6': A6, 
139      'DNAME': DNAME, 
140      'OPT': OPT, 
141      'APL': APL, 
142      'DS': DS, 
143      'SSHFP': SSHFP, 
144      'IPSECKEY': IPSECKEY, 
145      'RRSIG': RRSIG, 
146      'NSEC': NSEC, 
147      'DNSKEY': DNSKEY, 
148      'DHCID': DHCID, 
149      'NSEC3': NSEC3, 
150      'NSEC3PARAM': NSEC3PARAM, 
151      'TLSA': TLSA, 
152      'HIP': HIP, 
153      'CDS': CDS, 
154      'CDNSKEY': CDNSKEY, 
155      'CSYNC': CSYNC, 
156      'SPF': SPF, 
157      'UNSPEC': UNSPEC, 
158      'EUI48': EUI48, 
159      'EUI64': EUI64, 
160      'TKEY': TKEY, 
161      'TSIG': TSIG, 
162      'IXFR': IXFR, 
163      'AXFR': AXFR, 
164      'MAILB': MAILB, 
165      'MAILA': MAILA, 
166      'ANY': ANY, 
167      'URI': URI, 
168      'CAA': CAA, 
169      'TA': TA, 
170      'DLV': DLV, 
171  } 
172   
173  # We construct the inverse mapping programmatically to ensure that we 
174  # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that 
175  # would cause the mapping not to be true inverse. 
176   
177  _by_value = dict((y, x) for x, y in _by_text.items()) 
178   
179   
180  _metatypes = { 
181      OPT: True 
182  } 
183   
184  _singletons = { 
185      SOA: True, 
186      NXT: True, 
187      DNAME: True, 
188      NSEC: True, 
189      # CNAME is technically a singleton, but we allow multiple CNAMEs. 
190  } 
191   
192  _unknown_type_pattern = re.compile('TYPE([0-9]+)$', re.I) 
193   
194   
195 -class UnknownRdatatype(dns.exception.DNSException):
196 197 """DNS resource record type is unknown."""
198 199
200 -def from_text(text):
201 """Convert text into a DNS rdata type value. 202 @param text: the text 203 @type text: string 204 @raises dns.rdatatype.UnknownRdatatype: the type is unknown 205 @raises ValueError: the rdata type value is not >= 0 and <= 65535 206 @rtype: int""" 207 208 value = _by_text.get(text.upper()) 209 if value is None: 210 match = _unknown_type_pattern.match(text) 211 if match is None: 212 raise UnknownRdatatype 213 value = int(match.group(1)) 214 if value < 0 or value > 65535: 215 raise ValueError("type must be between >= 0 and <= 65535") 216 return value
217 218
219 -def to_text(value):
220 """Convert a DNS rdata type to text. 221 @param value: the rdata type value 222 @type value: int 223 @raises ValueError: the rdata type value is not >= 0 and <= 65535 224 @rtype: string""" 225 226 if value < 0 or value > 65535: 227 raise ValueError("type must be between >= 0 and <= 65535") 228 text = _by_value.get(value) 229 if text is None: 230 text = 'TYPE' + repr(value) 231 return text
232 233
234 -def is_metatype(rdtype):
235 """True if the type is a metatype. 236 @param rdtype: the type 237 @type rdtype: int 238 @rtype: bool""" 239 240 if rdtype >= TKEY and rdtype <= ANY or rdtype in _metatypes: 241 return True 242 return False
243 244
245 -def is_singleton(rdtype):
246 """True if the type is a singleton. 247 @param rdtype: the type 248 @type rdtype: int 249 @rtype: bool""" 250 251 if rdtype in _singletons: 252 return True 253 return False
254