rcode.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Result Codes."""
  17. import dns.enum
  18. import dns.exception
  19. class Rcode(dns.enum.IntEnum):
  20. #: No error
  21. NOERROR = 0
  22. #: Format error
  23. FORMERR = 1
  24. #: Server failure
  25. SERVFAIL = 2
  26. #: Name does not exist ("Name Error" in RFC 1025 terminology).
  27. NXDOMAIN = 3
  28. #: Not implemented
  29. NOTIMP = 4
  30. #: Refused
  31. REFUSED = 5
  32. #: Name exists.
  33. YXDOMAIN = 6
  34. #: RRset exists.
  35. YXRRSET = 7
  36. #: RRset does not exist.
  37. NXRRSET = 8
  38. #: Not authoritative.
  39. NOTAUTH = 9
  40. #: Name not in zone.
  41. NOTZONE = 10
  42. #: DSO-TYPE Not Implemented
  43. DSOTYPENI = 11
  44. #: Bad EDNS version.
  45. BADVERS = 16
  46. #: TSIG Signature Failure
  47. BADSIG = 16
  48. #: Key not recognized.
  49. BADKEY = 17
  50. #: Signature out of time window.
  51. BADTIME = 18
  52. #: Bad TKEY Mode.
  53. BADMODE = 19
  54. #: Duplicate key name.
  55. BADNAME = 20
  56. #: Algorithm not supported.
  57. BADALG = 21
  58. #: Bad Truncation
  59. BADTRUNC = 22
  60. #: Bad/missing Server Cookie
  61. BADCOOKIE = 23
  62. @classmethod
  63. def _maximum(cls):
  64. return 4095
  65. @classmethod
  66. def _unknown_exception_class(cls):
  67. return UnknownRcode
  68. class UnknownRcode(dns.exception.DNSException):
  69. """A DNS rcode is unknown."""
  70. def from_text(text):
  71. """Convert text into an rcode.
  72. *text*, a ``str``, the textual rcode or an integer in textual form.
  73. Raises ``dns.rcode.UnknownRcode`` if the rcode mnemonic is unknown.
  74. Returns an ``int``.
  75. """
  76. return Rcode.from_text(text)
  77. def from_flags(flags, ednsflags):
  78. """Return the rcode value encoded by flags and ednsflags.
  79. *flags*, an ``int``, the DNS flags field.
  80. *ednsflags*, an ``int``, the EDNS flags field.
  81. Raises ``ValueError`` if rcode is < 0 or > 4095
  82. Returns an ``int``.
  83. """
  84. value = (flags & 0x000f) | ((ednsflags >> 20) & 0xff0)
  85. return value
  86. def to_flags(value):
  87. """Return a (flags, ednsflags) tuple which encodes the rcode.
  88. *value*, an ``int``, the rcode.
  89. Raises ``ValueError`` if rcode is < 0 or > 4095.
  90. Returns an ``(int, int)`` tuple.
  91. """
  92. if value < 0 or value > 4095:
  93. raise ValueError('rcode must be >= 0 and <= 4095')
  94. v = value & 0xf
  95. ev = (value & 0xff0) << 20
  96. return (v, ev)
  97. def to_text(value, tsig=False):
  98. """Convert rcode into text.
  99. *value*, an ``int``, the rcode.
  100. Raises ``ValueError`` if rcode is < 0 or > 4095.
  101. Returns a ``str``.
  102. """
  103. if tsig and value == Rcode.BADVERS:
  104. return 'BADSIG'
  105. return Rcode.to_text(value)
  106. ### BEGIN generated Rcode constants
  107. NOERROR = Rcode.NOERROR
  108. FORMERR = Rcode.FORMERR
  109. SERVFAIL = Rcode.SERVFAIL
  110. NXDOMAIN = Rcode.NXDOMAIN
  111. NOTIMP = Rcode.NOTIMP
  112. REFUSED = Rcode.REFUSED
  113. YXDOMAIN = Rcode.YXDOMAIN
  114. YXRRSET = Rcode.YXRRSET
  115. NXRRSET = Rcode.NXRRSET
  116. NOTAUTH = Rcode.NOTAUTH
  117. NOTZONE = Rcode.NOTZONE
  118. DSOTYPENI = Rcode.DSOTYPENI
  119. BADVERS = Rcode.BADVERS
  120. BADSIG = Rcode.BADSIG
  121. BADKEY = Rcode.BADKEY
  122. BADTIME = Rcode.BADTIME
  123. BADMODE = Rcode.BADMODE
  124. BADNAME = Rcode.BADNAME
  125. BADALG = Rcode.BADALG
  126. BADTRUNC = Rcode.BADTRUNC
  127. BADCOOKIE = Rcode.BADCOOKIE
  128. ### END generated Rcode constants