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

# Source Code for Module dns.rcode

      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 Result Codes."""
     17
     18  import dns.exception
     19  from ._compat import long
     20
     21
     22  NOERROR = 0
     23  FORMERR = 1
     24  SERVFAIL = 2
     25  NXDOMAIN = 3
     26  NOTIMP = 4
     27  REFUSED = 5
     28  YXDOMAIN = 6
     29  YXRRSET = 7
     30  NXRRSET = 8
     31  NOTAUTH = 9
     32  NOTZONE = 10
     33  BADVERS = 16
     34
     35  _by_text = {
     36      'NOERROR': NOERROR,
     37      'FORMERR': FORMERR,
     38      'SERVFAIL': SERVFAIL,
     39      'NXDOMAIN': NXDOMAIN,
     40      'NOTIMP': NOTIMP,
     41      'REFUSED': REFUSED,
     42      'YXDOMAIN': YXDOMAIN,
     43      'YXRRSET': YXRRSET,
     44      'NXRRSET': NXRRSET,
     45      'NOTAUTH': NOTAUTH,
     46      'NOTZONE': NOTZONE,
     47      'BADVERS': BADVERS
     48  }
     49
     50  # We construct the inverse mapping programmatically to ensure that we
     51  # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
     52  # would cause the mapping not to be a true inverse.
     53
     54  _by_value = dict((y, x) for x, y in _by_text.items())
     55
     56
     57 -class UnknownRcode(dns.exception.DNSException):
     58
     59      """A DNS rcode is unknown."""
     60
     61
     62 -def from_text(text):
     63      """Convert text into an rcode.
     64
     65      @param text: the textual rcode
     66      @type text: string
     67      @raises UnknownRcode: the rcode is unknown
     68      @rtype: int
     69      """
     70
     71      if text.isdigit():
     72          v = int(text)
     73          if v >= 0 and v <= 4095:
     74              return v
     75      v = _by_text.get(text.upper())
     76      if v is None:
     77          raise UnknownRcode
     78      return v
     79
     80
     81 -def from_flags(flags, ednsflags):
     82      """Return the rcode value encoded by flags and ednsflags.
     83
     84      @param flags: the DNS flags
     85      @type flags: int
     86      @param ednsflags: the EDNS flags
     87      @type ednsflags: int
     88      @raises ValueError: rcode is < 0 or > 4095
     89      @rtype: int
     90      """
     91
     92      value = (flags & 0x000f) | ((ednsflags >> 20) & 0xff0)
     93      if value < 0 or value > 4095:
     94          raise ValueError('rcode must be >= 0 and <= 4095')
     95      return value
     96
     97
     98 -def to_flags(value):
     99      """Return a (flags, ednsflags) tuple which encodes the rcode.
    100
    101      @param value: the rcode
    102      @type value: int
    103      @raises ValueError: rcode is < 0 or > 4095
    104      @rtype: (int, int) tuple
    105      """
    106
    107      if value < 0 or value > 4095:
    108          raise ValueError('rcode must be >= 0 and <= 4095')
    109      v = value & 0xf
    110      ev = long(value & 0xff0) << 20
    111      return (v, ev)
    112
    113
    114 -def to_text(value):
    115      """Convert rcode into text.
    116
    117      @param value: the rcode
    118      @type value: int
    119      @rtype: string
    120      """
    121
    122      text = _by_value.get(value)
    123      if text is None:
    124          text = str(value)
    125      return text
    126