dnspython  1.16.0
About: dnspython is a DNS toolkit (for Python 2.x) that supports almost all record types.
  Fossies Dox: dnspython-1.16.0.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

rcode.py
Go to the documentation of this file.
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2 
3 # Copyright (C) 2001-2017 Nominum, Inc.
4 #
5 # Permission to use, copy, modify, and distribute this software and its
6 # documentation for any purpose with or without fee is hereby granted,
7 # provided that the above copyright notice and this permission notice
8 # appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18 """DNS Result Codes."""
19 
20 import dns.exception
21 from ._compat import long
22 
23 #: No error
24 NOERROR = 0
25 #: Form error
26 FORMERR = 1
27 #: Server failure
28 SERVFAIL = 2
29 #: Name does not exist ("Name Error" in RFC 1025 terminology).
30 NXDOMAIN = 3
31 #: Not implemented
32 NOTIMP = 4
33 #: Refused
34 REFUSED = 5
35 #: Name exists.
36 YXDOMAIN = 6
37 #: RRset exists.
38 YXRRSET = 7
39 #: RRset does not exist.
40 NXRRSET = 8
41 #: Not authoritative.
42 NOTAUTH = 9
43 #: Name not in zone.
44 NOTZONE = 10
45 #: Bad EDNS version.
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 # We construct the inverse mapping programmatically to ensure that we
64 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
65 # would cause the mapping not to be a true inverse.
66 
67 _by_value = {y: x for x, y in _by_text.items()}
68 
69 
71  """A DNS rcode is unknown."""
72 
73 
74 def from_text(text):
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 
94 def from_flags(flags, ednsflags):
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 
112 def to_flags(value):
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 
129 def to_text(value):
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
dns.rcode.from_text
def from_text(text)
Definition: rcode.py:74
dns.exception.DNSException
Definition: exception.py:24
dns.rcode.to_text
def to_text(value)
Definition: rcode.py:129
dns.rcode.from_flags
def from_flags(flags, ednsflags)
Definition: rcode.py:94
dns.rcode.UnknownRcode
Definition: rcode.py:70
dns.rcode.to_flags
def to_flags(value)
Definition: rcode.py:112
dns._compat.long
long
Definition: _compat.py:10
dns.exception
Definition: exception.py:1