rdataclass.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. """DNS Rdata Classes.
  16. @var _by_text: The rdata class textual name to value mapping
  17. @type _by_text: dict
  18. @var _by_value: The rdata class value to textual name mapping
  19. @type _by_value: dict
  20. @var _metaclasses: If an rdataclass is a metaclass, there will be a mapping
  21. whose key is the rdatatype value and whose value is True in this dictionary.
  22. @type _metaclasses: dict"""
  23. import re
  24. import dns.exception
  25. RESERVED0 = 0
  26. IN = 1
  27. CH = 3
  28. HS = 4
  29. NONE = 254
  30. ANY = 255
  31. _by_text = {
  32. 'RESERVED0': RESERVED0,
  33. 'IN': IN,
  34. 'CH': CH,
  35. 'HS': HS,
  36. 'NONE': NONE,
  37. 'ANY': ANY
  38. }
  39. # We construct the inverse mapping programmatically to ensure that we
  40. # cannot make any mistakes (e.g. omissions, cut-and-paste errors) that
  41. # would cause the mapping not to be true inverse.
  42. _by_value = dict((y, x) for x, y in _by_text.items())
  43. # Now that we've built the inverse map, we can add class aliases to
  44. # the _by_text mapping.
  45. _by_text.update({
  46. 'INTERNET': IN,
  47. 'CHAOS': CH,
  48. 'HESIOD': HS
  49. })
  50. _metaclasses = {
  51. NONE: True,
  52. ANY: True
  53. }
  54. _unknown_class_pattern = re.compile('CLASS([0-9]+)$', re.I)
  55. class UnknownRdataclass(dns.exception.DNSException):
  56. """A DNS class is unknown."""
  57. def from_text(text):
  58. """Convert text into a DNS rdata class value.
  59. @param text: the text
  60. @type text: string
  61. @rtype: int
  62. @raises dns.rdataclass.UnknownRdataclass: the class is unknown
  63. @raises ValueError: the rdata class value is not >= 0 and <= 65535
  64. """
  65. value = _by_text.get(text.upper())
  66. if value is None:
  67. match = _unknown_class_pattern.match(text)
  68. if match is None:
  69. raise UnknownRdataclass
  70. value = int(match.group(1))
  71. if value < 0 or value > 65535:
  72. raise ValueError("class must be between >= 0 and <= 65535")
  73. return value
  74. def to_text(value):
  75. """Convert a DNS rdata class to text.
  76. @param value: the rdata class value
  77. @type value: int
  78. @rtype: string
  79. @raises ValueError: the rdata class value is not >= 0 and <= 65535
  80. """
  81. if value < 0 or value > 65535:
  82. raise ValueError("class must be between >= 0 and <= 65535")
  83. text = _by_value.get(value)
  84. if text is None:
  85. text = 'CLASS' + repr(value)
  86. return text
  87. def is_metaclass(rdclass):
  88. """True if the class is a metaclass.
  89. @param rdclass: the rdata class
  90. @type rdclass: int
  91. @rtype: bool"""
  92. if rdclass in _metaclasses:
  93. return True
  94. return False