Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/dns/opcode.py: 90%
31 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-02 06:07 +0000
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-02 06:07 +0000
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
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.
18"""DNS Opcodes."""
20import dns.enum
21import dns.exception
24class Opcode(dns.enum.IntEnum):
25 #: Query
26 QUERY = 0
27 #: Inverse Query (historical)
28 IQUERY = 1
29 #: Server Status (unspecified and unimplemented anywhere)
30 STATUS = 2
31 #: Notify
32 NOTIFY = 4
33 #: Dynamic Update
34 UPDATE = 5
36 @classmethod
37 def _maximum(cls):
38 return 15
40 @classmethod
41 def _unknown_exception_class(cls):
42 return UnknownOpcode
45class UnknownOpcode(dns.exception.DNSException):
46 """An DNS opcode is unknown."""
49def from_text(text: str) -> Opcode:
50 """Convert text into an opcode.
52 *text*, a ``str``, the textual opcode
54 Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown.
56 Returns an ``int``.
57 """
59 return Opcode.from_text(text)
62def from_flags(flags: int) -> Opcode:
63 """Extract an opcode from DNS message flags.
65 *flags*, an ``int``, the DNS flags.
67 Returns an ``int``.
68 """
70 return Opcode((flags & 0x7800) >> 11)
73def to_flags(value: Opcode) -> int:
74 """Convert an opcode to a value suitable for ORing into DNS message
75 flags.
77 *value*, an ``int``, the DNS opcode value.
79 Returns an ``int``.
80 """
82 return (value << 11) & 0x7800
85def to_text(value: Opcode) -> str:
86 """Convert an opcode to text.
88 *value*, an ``int`` the opcode value,
90 Raises ``dns.opcode.UnknownOpcode`` if the opcode is unknown.
92 Returns a ``str``.
93 """
95 return Opcode.to_text(value)
98def is_update(flags: int) -> bool:
99 """Is the opcode in flags UPDATE?
101 *flags*, an ``int``, the DNS message flags.
103 Returns a ``bool``.
104 """
106 return from_flags(flags) == Opcode.UPDATE
109### BEGIN generated Opcode constants
111QUERY = Opcode.QUERY
112IQUERY = Opcode.IQUERY
113STATUS = Opcode.STATUS
114NOTIFY = Opcode.NOTIFY
115UPDATE = Opcode.UPDATE
117### END generated Opcode constants