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 -def _order_flags(table):
56 return sorted(table.items(), reverse=True)
57 58 _flags_order = _order_flags(_by_value) 59 60 _edns_flags_order = _order_flags(_edns_by_value) 61
62 -def _from_text(text, table):
63 flags = 0 64 tokens = text.split() 65 for t in tokens: 66 flags = flags | table[t.upper()] 67 return flags
68
69 -def _to_text(flags, table, order):
70 text_flags = [] 71 for k, v in order: 72 if flags & k != 0: 73 text_flags.append(v) 74 return ' '.join(text_flags)
75
76 -def from_text(text):
77 """Convert a space-separated list of flag text values into a flags 78 value. 79 @rtype: int""" 80 81 return _from_text(text, _by_text)
82
83 -def to_text(flags):
84 """Convert a flags value into a space-separated list of flag text 85 values. 86 @rtype: string""" 87 88 return _to_text(flags, _by_value, _flags_order)
89 90
91 -def edns_from_text(text):
92 """Convert a space-separated list of EDNS flag text values into a EDNS 93 flags value. 94 @rtype: int""" 95 96 return _from_text(text, _edns_by_text)
97
98 -def edns_to_text(flags):
99 """Convert an EDNS flags value into a space-separated list of EDNS flag 100 text values. 101 @rtype: string""" 102 103 return _to_text(flags, _edns_by_value, _edns_flags_order)
104