codekingpro/portable-devtools
114k
1# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license2 3# Copyright (C) 2001-2017 Nominum, Inc.4#5# Permission to use, copy, modify, and distribute this software and its6# documentation for any purpose with or without fee is hereby granted,7# provided that the above copyright notice and this permission notice8# appear in all copies.9#10# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT16# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.17 18"""DNS Result Codes."""19 20from typing import Tuple21 22import dns.enum23import dns.exception24 25 26class Rcode(dns.enum.IntEnum):27 #: No error28 NOERROR = 029 #: Format error30 FORMERR = 131 #: Server failure32 SERVFAIL = 233 #: Name does not exist ("Name Error" in RFC 1025 terminology).34 NXDOMAIN = 335 #: Not implemented36 NOTIMP = 437 #: Refused38 REFUSED = 539 #: Name exists.40 YXDOMAIN = 641 #: RRset exists.42 YXRRSET = 743 #: RRset does not exist.44 NXRRSET = 845 #: Not authoritative.46 NOTAUTH = 947 #: Name not in zone.48 NOTZONE = 1049 #: DSO-TYPE Not Implemented50 DSOTYPENI = 1151 #: Bad EDNS version.52 BADVERS = 1653 #: TSIG Signature Failure54 BADSIG = 1655 #: Key not recognized.56 BADKEY = 1757 #: Signature out of time window.58 BADTIME = 1859 #: Bad TKEY Mode.60 BADMODE = 1961 #: Duplicate key name.62 BADNAME = 2063 #: Algorithm not supported.64 BADALG = 2165 #: Bad Truncation66 BADTRUNC = 2267 #: Bad/missing Server Cookie68 BADCOOKIE = 2369 70 @classmethod71 def _maximum(cls):72 return 409573 74 @classmethod75 def _unknown_exception_class(cls):76 return UnknownRcode77 78 79class UnknownRcode(dns.exception.DNSException):80 """A DNS rcode is unknown."""81 82 83def 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 96def 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 > 4095104 105 Returns a ``dns.rcode.Rcode``.106 """107 108 value = (flags & 0x000F) | ((ednsflags >> 20) & 0xFF0)109 return Rcode.make(value)110 111 112def 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 & 0xF125 ev = (value & 0xFF0) << 20126 return (v, ev)127 128 129def 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 constants145 146NOERROR = Rcode.NOERROR147FORMERR = Rcode.FORMERR148SERVFAIL = Rcode.SERVFAIL149NXDOMAIN = Rcode.NXDOMAIN150NOTIMP = Rcode.NOTIMP151REFUSED = Rcode.REFUSED152YXDOMAIN = Rcode.YXDOMAIN153YXRRSET = Rcode.YXRRSET154NXRRSET = Rcode.NXRRSET155NOTAUTH = Rcode.NOTAUTH156NOTZONE = Rcode.NOTZONE157DSOTYPENI = Rcode.DSOTYPENI158BADVERS = Rcode.BADVERS159BADSIG = Rcode.BADSIG160BADKEY = Rcode.BADKEY161BADTIME = Rcode.BADTIME162BADMODE = Rcode.BADMODE163BADNAME = Rcode.BADNAME164BADALG = Rcode.BADALG165BADTRUNC = Rcode.BADTRUNC166BADCOOKIE = Rcode.BADCOOKIE167 168### END generated Rcode constants169 