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

Source Code for Module dns.edns

  1  # Copyright (C) 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  """EDNS Options""" 
 17   
 18  NSID = 3 
19 20 21 -class Option(object):
22 23 """Base class for all EDNS option types. 24 """ 25
26 - def __init__(self, otype):
27 """Initialize an option. 28 @param otype: The rdata type 29 @type otype: int 30 """ 31 self.otype = otype
32
33 - def to_wire(self, file):
34 """Convert an option to wire format. 35 """ 36 raise NotImplementedError
37 38 @classmethod
39 - def from_wire(cls, otype, wire, current, olen):
40 """Build an EDNS option object from wire format 41 42 @param otype: The option type 43 @type otype: int 44 @param wire: The wire-format message 45 @type wire: string 46 @param current: The offset in wire of the beginning of the rdata. 47 @type current: int 48 @param olen: The length of the wire-format option data 49 @type olen: int 50 @rtype: dns.edns.Option instance""" 51 raise NotImplementedError
52
53 - def _cmp(self, other):
54 """Compare an EDNS option with another option of the same type. 55 Return < 0 if self < other, 0 if self == other, 56 and > 0 if self > other. 57 """ 58 raise NotImplementedError
59
60 - def __eq__(self, other):
61 if not isinstance(other, Option): 62 return False 63 if self.otype != other.otype: 64 return False 65 return self._cmp(other) == 0
66
67 - def __ne__(self, other):
68 if not isinstance(other, Option): 69 return False 70 if self.otype != other.otype: 71 return False 72 return self._cmp(other) != 0
73
74 - def __lt__(self, other):
75 if not isinstance(other, Option) or \ 76 self.otype != other.otype: 77 return NotImplemented 78 return self._cmp(other) < 0
79
80 - def __le__(self, other):
81 if not isinstance(other, Option) or \ 82 self.otype != other.otype: 83 return NotImplemented 84 return self._cmp(other) <= 0
85
86 - def __ge__(self, other):
87 if not isinstance(other, Option) or \ 88 self.otype != other.otype: 89 return NotImplemented 90 return self._cmp(other) >= 0
91
92 - def __gt__(self, other):
93 if not isinstance(other, Option) or \ 94 self.otype != other.otype: 95 return NotImplemented 96 return self._cmp(other) > 0
97
98 99 -class GenericOption(Option):
100 101 """Generate Rdata Class 102 103 This class is used for EDNS option types for which we have no better 104 implementation. 105 """ 106
107 - def __init__(self, otype, data):
108 super(GenericOption, self).__init__(otype) 109 self.data = data
110
111 - def to_wire(self, file):
112 file.write(self.data)
113 114 @classmethod
115 - def from_wire(cls, otype, wire, current, olen):
116 return cls(otype, wire[current: current + olen])
117
118 - def _cmp(self, other):
119 if self.data == other.data: 120 return 0 121 if self.data > other.data: 122 return 1 123 return -1
124 125 _type_to_class = { 126 }
127 128 129 -def get_option_class(otype):
130 cls = _type_to_class.get(otype) 131 if cls is None: 132 cls = GenericOption 133 return cls
134
135 136 -def option_from_wire(otype, wire, current, olen):
137 """Build an EDNS option object from wire format 138 139 @param otype: The option type 140 @type otype: int 141 @param wire: The wire-format message 142 @type wire: string 143 @param current: The offset in wire of the beginning of the rdata. 144 @type current: int 145 @param olen: The length of the wire-format option data 146 @type olen: int 147 @rtype: dns.edns.Option instance""" 148 149 cls = get_option_class(otype) 150 return cls.from_wire(otype, wire, current, olen)
151