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