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 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
51
52
53
54 _by_value = dict((y, x) for x, y in _by_text.items())
55
56
58
59 """A DNS rcode is unknown."""
60
61
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
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
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
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