1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """DNS Result Codes."""
19
20 import dns.exception
21 from ._compat import long
22
23
24 NOERROR = 0
25
26 FORMERR = 1
27
28 SERVFAIL = 2
29
30 NXDOMAIN = 3
31
32 NOTIMP = 4
33
34 REFUSED = 5
35
36 YXDOMAIN = 6
37
38 YXRRSET = 7
39
40 NXRRSET = 8
41
42 NOTAUTH = 9
43
44 NOTZONE = 10
45
46 BADVERS = 16
47
48 _by_text = {
49 'NOERROR': NOERROR,
50 'FORMERR': FORMERR,
51 'SERVFAIL': SERVFAIL,
52 'NXDOMAIN': NXDOMAIN,
53 'NOTIMP': NOTIMP,
54 'REFUSED': REFUSED,
55 'YXDOMAIN': YXDOMAIN,
56 'YXRRSET': YXRRSET,
57 'NXRRSET': NXRRSET,
58 'NOTAUTH': NOTAUTH,
59 'NOTZONE': NOTZONE,
60 'BADVERS': BADVERS
61 }
62
63
64
65
66
67 _by_value = {y: x for x, y in _by_text.items()}
68
69
71 """A DNS rcode is unknown."""
72
73
75 """Convert text into an rcode.
76
77 *text*, a ``text``, the textual rcode or an integer in textual form.
78
79 Raises ``dns.rcode.UnknownRcode`` if the rcode mnemonic is unknown.
80
81 Returns an ``int``.
82 """
83
84 if text.isdigit():
85 v = int(text)
86 if v >= 0 and v <= 4095:
87 return v
88 v = _by_text.get(text.upper())
89 if v is None:
90 raise UnknownRcode
91 return v
92
93
95 """Return the rcode value encoded by flags and ednsflags.
96
97 *flags*, an ``int``, the DNS flags field.
98
99 *ednsflags*, an ``int``, the EDNS flags field.
100
101 Raises ``ValueError`` if rcode is < 0 or > 4095
102
103 Returns an ``int``.
104 """
105
106 value = (flags & 0x000f) | ((ednsflags >> 20) & 0xff0)
107 if value < 0 or value > 4095:
108 raise ValueError('rcode must be >= 0 and <= 4095')
109 return value
110
111
113 """Return a (flags, ednsflags) tuple which encodes the rcode.
114
115 *value*, an ``int``, the rcode.
116
117 Raises ``ValueError`` if rcode is < 0 or > 4095.
118
119 Returns an ``(int, int)`` tuple.
120 """
121
122 if value < 0 or value > 4095:
123 raise ValueError('rcode must be >= 0 and <= 4095')
124 v = value & 0xf
125 ev = long(value & 0xff0) << 20
126 return (v, ev)
127
128
130 """Convert rcode into text.
131
132 *value*, and ``int``, the rcode.
133
134 Raises ``ValueError`` if rcode is < 0 or > 4095.
135
136 Returns a ``text``.
137 """
138
139 if value < 0 or value > 4095:
140 raise ValueError('rcode must be >= 0 and <= 4095')
141 text = _by_value.get(value)
142 if text is None:
143 text = str(value)
144 return text
145