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  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  # We construct the inverse mapping programmatically to ensure that we 
176  # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that 
177  # would cause the mapping not to be true inverse. 
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      # CNAME is technically a singleton, but we allow multiple CNAMEs. 
192  } 
193   
194  _unknown_type_pattern = re.compile('TYPE([0-9]+)$', re.I) 
195   
196   
197 -class UnknownRdatatype(dns.exception.DNSException):
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
221 -def to_text(value):
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
236 -def is_metatype(rdtype):
237 """True if the type is a metatype. 238 @param rdtype: the type 239 @type rdtype: int 240 @rtype: bool""" 241 242 if rdtype >= TKEY and rdtype <= ANY or rdtype in _metatypes: 243 return True 244 return False
245 246
247 -def is_singleton(rdtype):
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