1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49
50
51
52 _by_value = dict([(y, x) for x, y in _by_text.iteritems()])
53
54
56 """A DNS rcode is unknown."""
57
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
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
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
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