rdataclass.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2001-2017 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """DNS Rdata Classes."""
  17. import dns.enum
  18. import dns.exception
  19. class RdataClass(dns.enum.IntEnum):
  20. """DNS Rdata Class"""
  21. RESERVED0 = 0
  22. IN = 1
  23. INTERNET = IN
  24. CH = 3
  25. CHAOS = CH
  26. HS = 4
  27. HESIOD = HS
  28. NONE = 254
  29. ANY = 255
  30. @classmethod
  31. def _maximum(cls):
  32. return 65535
  33. @classmethod
  34. def _short_name(cls):
  35. return "class"
  36. @classmethod
  37. def _prefix(cls):
  38. return "CLASS"
  39. @classmethod
  40. def _unknown_exception_class(cls):
  41. return UnknownRdataclass
  42. _metaclasses = {RdataClass.NONE, RdataClass.ANY}
  43. class UnknownRdataclass(dns.exception.DNSException):
  44. """A DNS class is unknown."""
  45. def from_text(text):
  46. """Convert text into a DNS rdata class value.
  47. The input text can be a defined DNS RR class mnemonic or
  48. instance of the DNS generic class syntax.
  49. For example, "IN" and "CLASS1" will both result in a value of 1.
  50. Raises ``dns.rdatatype.UnknownRdataclass`` if the class is unknown.
  51. Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
  52. Returns an ``int``.
  53. """
  54. return RdataClass.from_text(text)
  55. def to_text(value):
  56. """Convert a DNS rdata class value to text.
  57. If the value has a known mnemonic, it will be used, otherwise the
  58. DNS generic class syntax will be used.
  59. Raises ``ValueError`` if the rdata class value is not >= 0 and <= 65535.
  60. Returns a ``str``.
  61. """
  62. return RdataClass.to_text(value)
  63. def is_metaclass(rdclass):
  64. """True if the specified class is a metaclass.
  65. The currently defined metaclasses are ANY and NONE.
  66. *rdclass* is an ``int``.
  67. """
  68. if rdclass in _metaclasses:
  69. return True
  70. return False
  71. ### BEGIN generated RdataClass constants
  72. RESERVED0 = RdataClass.RESERVED0
  73. IN = RdataClass.IN
  74. INTERNET = RdataClass.INTERNET
  75. CH = RdataClass.CH
  76. CHAOS = RdataClass.CHAOS
  77. HS = RdataClass.HS
  78. HESIOD = RdataClass.HESIOD
  79. NONE = RdataClass.NONE
  80. ANY = RdataClass.ANY
  81. ### END generated RdataClass constants