opcode.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Opcodes."""
  17. from typing import Type
  18. import dns.enum
  19. import dns.exception
  20. class Opcode(dns.enum.IntEnum):
  21. #: Query
  22. QUERY = 0
  23. #: Inverse Query (historical)
  24. IQUERY = 1
  25. #: Server Status (unspecified and unimplemented anywhere)
  26. STATUS = 2
  27. #: Notify
  28. NOTIFY = 4
  29. #: Dynamic Update
  30. UPDATE = 5
  31. @classmethod
  32. def _maximum(cls):
  33. return 15
  34. @classmethod
  35. def _unknown_exception_class(cls) -> Type[Exception]:
  36. return UnknownOpcode
  37. class UnknownOpcode(dns.exception.DNSException):
  38. """An DNS opcode is unknown."""
  39. def from_text(text: str) -> Opcode:
  40. """Convert text into an opcode.
  41. *text*, a ``str``, the textual opcode
  42. Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown.
  43. Returns an ``int``.
  44. """
  45. return Opcode.from_text(text)
  46. def from_flags(flags: int) -> Opcode:
  47. """Extract an opcode from DNS message flags.
  48. *flags*, an ``int``, the DNS flags.
  49. Returns an ``int``.
  50. """
  51. return Opcode((flags & 0x7800) >> 11)
  52. def to_flags(value: Opcode) -> int:
  53. """Convert an opcode to a value suitable for ORing into DNS message
  54. flags.
  55. *value*, an ``int``, the DNS opcode value.
  56. Returns an ``int``.
  57. """
  58. return (value << 11) & 0x7800
  59. def to_text(value: Opcode) -> str:
  60. """Convert an opcode to text.
  61. *value*, an ``int`` the opcode value,
  62. Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown.
  63. Returns a ``str``.
  64. """
  65. return Opcode.to_text(value)
  66. def is_update(flags: int) -> bool:
  67. """Is the opcode in flags UPDATE?
  68. *flags*, an ``int``, the DNS message flags.
  69. Returns a ``bool``.
  70. """
  71. return from_flags(flags) == Opcode.UPDATE
  72. ### BEGIN generated Opcode constants
  73. QUERY = Opcode.QUERY
  74. IQUERY = Opcode.IQUERY
  75. STATUS = Opcode.STATUS
  76. NOTIFY = Opcode.NOTIFY
  77. UPDATE = Opcode.UPDATE
  78. ### END generated Opcode constants