1 # Copyright (C) 2001-2007, 2009, 2010 Nominum, Inc.
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 """DNS Message Flags."""
47 # We construct the inverse mappings programmatically to ensure that we
48 # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
49 # would cause the mappings not to be true inverses.
51 _by_value
= dict([(y
, x
) for x
, y
in _by_text
.iteritems()])
53 _edns_by_value
= dict([(y
, x
) for x
, y
in _edns_by_text
.iteritems()])
55 def _order_flags(table
):
56 order
= list(table
.iteritems())
61 _flags_order
= _order_flags(_by_value
)
63 _edns_flags_order
= _order_flags(_edns_by_value
)
65 def _from_text(text
, table
):
69 flags
= flags | table
[t
.upper()]
72 def _to_text(flags
, table
, order
):
77 return ' '.join(text_flags
)
80 """Convert a space-separated list of flag text values into a flags
84 return _from_text(text
, _by_text
)
87 """Convert a flags value into a space-separated list of flag text
91 return _to_text(flags
, _by_value
, _flags_order
)
94 def edns_from_text(text
):
95 """Convert a space-separated list of EDNS flag text values into a EDNS
99 return _from_text(text
, _edns_by_text
)
101 def edns_to_text(flags
):
102 """Convert an EDNS flags value into a space-separated list of EDNS flag
106 return _to_text(flags
, _edns_by_value
, _edns_flags_order
)