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