Package dns :: Module flags
[hide private]
[frames] | no frames]

Source Code for Module dns.flags

  1  # Copyright (C) 2001-2007, 2009-2011 Nominum, Inc. 
  2  # 
  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. 
  7  # 
  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. 
 15   
 16  """DNS Message Flags.""" 
 17   
 18  # Standard DNS flags 
 19   
 20  QR = 0x8000 
 21  AA = 0x0400 
 22  TC = 0x0200 
 23  RD = 0x0100 
 24  RA = 0x0080 
 25  AD = 0x0020 
 26  CD = 0x0010 
 27   
 28  # EDNS flags 
 29   
 30  DO = 0x8000 
 31   
 32  _by_text = { 
 33      'QR': QR, 
 34      'AA': AA, 
 35      'TC': TC, 
 36      'RD': RD, 
 37      'RA': RA, 
 38      'AD': AD, 
 39      'CD': CD 
 40  } 
 41   
 42  _edns_by_text = { 
 43      'DO': DO 
 44  } 
 45   
 46   
 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. 
 50   
 51  _by_value = dict((y, x) for x, y in _by_text.items()) 
 52   
 53  _edns_by_value = dict((y, x) for x, y in _edns_by_text.items()) 
 54   
 55   
56 -def _order_flags(table):
57 order = list(table.items()) 58 order.sort() 59 order.reverse() 60 return order
61 62 _flags_order = _order_flags(_by_value) 63 64 _edns_flags_order = _order_flags(_edns_by_value) 65 66
67 -def _from_text(text, table):
68 flags = 0 69 tokens = text.split() 70 for t in tokens: 71 flags = flags | table[t.upper()] 72 return flags
73 74
75 -def _to_text(flags, table, order):
76 text_flags = [] 77 for k, v in order: 78 if flags & k != 0: 79 text_flags.append(v) 80 return ' '.join(text_flags)
81 82
83 -def from_text(text):
84 """Convert a space-separated list of flag text values into a flags 85 value. 86 @rtype: int""" 87 88 return _from_text(text, _by_text)
89 90
91 -def to_text(flags):
92 """Convert a flags value into a space-separated list of flag text 93 values. 94 @rtype: string""" 95 96 return _to_text(flags, _by_value, _flags_order)
97 98
99 -def edns_from_text(text):
100 """Convert a space-separated list of EDNS flag text values into a EDNS 101 flags value. 102 @rtype: int""" 103 104 return _from_text(text, _edns_by_text)
105 106
107 -def edns_to_text(flags):
108 """Convert an EDNS flags value into a space-separated list of EDNS flag 109 text values. 110 @rtype: string""" 111 112 return _to_text(flags, _edns_by_value, _edns_flags_order)
113