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