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