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