| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2001-2017 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
"""DNS Opcodes.""" |
| 19 |
|
| 20 |
from typing import Type |
| 21 |
|
| 22 |
import dns.enum |
| 23 |
import dns.exception |
| 24 |
|
| 25 |
|
| 26 |
class Opcode(dns.enum.IntEnum): |
| 27 |
#: Query |
| 28 |
QUERY = 0 |
| 29 |
#: Inverse Query (historical) |
| 30 |
IQUERY = 1 |
| 31 |
#: Server Status (unspecified and unimplemented anywhere) |
| 32 |
STATUS = 2 |
| 33 |
#: Notify |
| 34 |
NOTIFY = 4 |
| 35 |
#: Dynamic Update |
| 36 |
UPDATE = 5 |
| 37 |
|
| 38 |
@classmethod |
| 39 |
def _maximum(cls): |
| 40 |
return 15 |
| 41 |
|
| 42 |
@classmethod |
| 43 |
def _unknown_exception_class(cls) -> Type[Exception]: |
| 44 |
return UnknownOpcode |
| 45 |
|
| 46 |
|
| 47 |
class UnknownOpcode(dns.exception.DNSException): |
| 48 |
"""An DNS opcode is unknown.""" |
| 49 |
|
| 50 |
|
| 51 |
def from_text(text: str) -> Opcode: |
| 52 |
"""Convert text into an opcode. |
| 53 |
|
| 54 |
*text*, a ``str``, the textual opcode |
| 55 |
|
| 56 |
Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown. |
| 57 |
|
| 58 |
Returns an ``int``. |
| 59 |
""" |
| 60 |
|
| 61 |
return Opcode.from_text(text) |
| 62 |
|
| 63 |
|
| 64 |
def from_flags(flags: int) -> Opcode: |
| 65 |
"""Extract an opcode from DNS message flags. |
| 66 |
|
| 67 |
*flags*, an ``int``, the DNS flags. |
| 68 |
|
| 69 |
Returns an ``int``. |
| 70 |
""" |
| 71 |
|
| 72 |
return Opcode((flags & 0x7800) >> 11) |
| 73 |
|
| 74 |
|
| 75 |
def to_flags(value: Opcode) -> int: |
| 76 |
"""Convert an opcode to a value suitable for ORing into DNS message |
| 77 |
flags. |
| 78 |
|
| 79 |
*value*, an ``int``, the DNS opcode value. |
| 80 |
|
| 81 |
Returns an ``int``. |
| 82 |
""" |
| 83 |
|
| 84 |
return (value << 11) & 0x7800 |
| 85 |
|
| 86 |
|
| 87 |
def to_text(value: Opcode) -> str: |
| 88 |
"""Convert an opcode to text. |
| 89 |
|
| 90 |
*value*, an ``int`` the opcode value, |
| 91 |
|
| 92 |
Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown. |
| 93 |
|
| 94 |
Returns a ``str``. |
| 95 |
""" |
| 96 |
|
| 97 |
return Opcode.to_text(value) |
| 98 |
|
| 99 |
|
| 100 |
def is_update(flags: int) -> bool: |
| 101 |
"""Is the opcode in flags UPDATE? |
| 102 |
|
| 103 |
*flags*, an ``int``, the DNS message flags. |
| 104 |
|
| 105 |
Returns a ``bool``. |
| 106 |
""" |
| 107 |
|
| 108 |
return from_flags(flags) == Opcode.UPDATE |
| 109 |
|
| 110 |
|
| 111 |
### BEGIN generated Opcode constants |
| 112 |
|
| 113 |
QUERY = Opcode.QUERY |
| 114 |
IQUERY = Opcode.IQUERY |
| 115 |
STATUS = Opcode.STATUS |
| 116 |
NOTIFY = Opcode.NOTIFY |
| 117 |
UPDATE = Opcode.UPDATE |
| 118 |
|
| 119 |
### END generated Opcode constants |