edns.py 4.3 KB

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