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)  

flags.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 Message Flags."""
19 
20 # Standard DNS flags
21 
22 #: Query Response
23 QR = 0x8000
24 #: Authoritative Answer
25 AA = 0x0400
26 #: Truncated Response
27 TC = 0x0200
28 #: Recursion Desired
29 RD = 0x0100
30 #: Recursion Available
31 RA = 0x0080
32 #: Authentic Data
33 AD = 0x0020
34 #: Checking Disabled
35 CD = 0x0010
36 
37 # EDNS flags
38 
39 #: DNSSEC answer OK
40 DO = 0x8000
41 
42 _by_text = {
43  'QR': QR,
44  'AA': AA,
45  'TC': TC,
46  'RD': RD,
47  'RA': RA,
48  'AD': AD,
49  'CD': CD
50 }
51 
52 _edns_by_text = {
53  'DO': DO
54 }
55 
56 
57 # We construct the inverse mappings programmatically to ensure that we
58 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
59 # would cause the mappings not to be true inverses.
60 
61 _by_value = {y: x for x, y in _by_text.items()}
62 
63 _edns_by_value = {y: x for x, y in _edns_by_text.items()}
64 
65 
66 def _order_flags(table):
67  order = list(table.items())
68  order.sort()
69  order.reverse()
70  return order
71 
72 _flags_order = _order_flags(_by_value)
73 
74 _edns_flags_order = _order_flags(_edns_by_value)
75 
76 
77 def _from_text(text, table):
78  flags = 0
79  tokens = text.split()
80  for t in tokens:
81  flags = flags | table[t.upper()]
82  return flags
83 
84 
85 def _to_text(flags, table, order):
86  text_flags = []
87  for k, v in order:
88  if flags & k != 0:
89  text_flags.append(v)
90  return ' '.join(text_flags)
91 
92 
93 def from_text(text):
94  """Convert a space-separated list of flag text values into a flags
95  value.
96 
97  Returns an ``int``
98  """
99 
100  return _from_text(text, _by_text)
101 
102 
103 def to_text(flags):
104  """Convert a flags value into a space-separated list of flag text
105  values.
106 
107  Returns a ``text``.
108  """
109 
110  return _to_text(flags, _by_value, _flags_order)
111 
112 
113 def edns_from_text(text):
114  """Convert a space-separated list of EDNS flag text values into a EDNS
115  flags value.
116 
117  Returns an ``int``
118  """
119 
120  return _from_text(text, _edns_by_text)
121 
122 
123 def edns_to_text(flags):
124  """Convert an EDNS flags value into a space-separated list of EDNS flag
125  text values.
126 
127  Returns a ``text``.
128  """
129 
130  return _to_text(flags, _edns_by_value, _edns_flags_order)
dns.flags.from_text
def from_text(text)
Definition: flags.py:93
dns.flags._to_text
def _to_text(flags, table, order)
Definition: flags.py:85
dns.flags.edns_to_text
def edns_to_text(flags)
Definition: flags.py:123
dns.flags._from_text
def _from_text(text, table)
Definition: flags.py:77
dns.flags.edns_from_text
def edns_from_text(text)
Definition: flags.py:113
dns.flags.to_text
def to_text(flags)
Definition: flags.py:103
dns.flags._order_flags
def _order_flags(table)
Definition: flags.py:66