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