1 # Copyright (C) 2001-2007, 2009-2011 Nominum, Inc.
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.
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.
16 """DNS Result Codes."""
36 'SERVFAIL' : SERVFAIL
,
37 'NXDOMAIN' : NXDOMAIN
,
40 'YXDOMAIN' : YXDOMAIN
,
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.
52 _by_value
= dict([(y
, x
) for x
, y
in _by_text
.iteritems()])
55 class UnknownRcode(dns
.exception
.DNSException
):
56 """Raised if an rcode is unknown."""
60 """Convert text into an rcode.
62 @param text: the texual rcode
64 @raises UnknownRcode: the rcode is unknown
70 if v
>= 0 and v
<= 4095:
72 v
= _by_text
.get(text
.upper())
77 def from_flags(flags
, ednsflags
):
78 """Return the rcode value encoded by flags and ednsflags.
80 @param flags: the DNS flags
82 @param ednsflags: the EDNS flags
84 @raises ValueError: rcode is < 0 or > 4095
88 value
= (flags
& 0x000f) |
((ednsflags
>> 20) & 0xff0)
89 if value
< 0 or value
> 4095:
90 raise ValueError('rcode must be >= 0 and <= 4095')
94 """Return a (flags, ednsflags) tuple which encodes the rcode.
96 @param value: the rcode
98 @raises ValueError: rcode is < 0 or > 4095
99 @rtype: (int, int) tuple
102 if value
< 0 or value
> 4095:
103 raise ValueError('rcode must be >= 0 and <= 4095')
105 ev
= long(value
& 0xff0) << 20
109 """Convert rcode into text.
111 @param value: the rcode
116 text
= _by_value
.get(value
)