Source code for constants

"""
The constants module defines the various DNS constants, and also
provides some utility functions for dealing with these.
"""

# The DNS QR header flag can be either 0 (query) or 1 (response).
QR_QUERY = 0     #: QR flag for queries
QR_RESPONSE = 1  #: QR flag for responses

# Possible DNS operation code values are listed on the IANA DNS
# parameters page:
# https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5
OP_QUERY = 0    #: OpCode for queries
OP_IQUERY = 1   #: OpCode for inverse queries
OP_STATUS = 2   #: OpCode for status queries
# 3 is unassigned
OP_NOTIFY = 4   #: OpCode for NOTIFY messages
OP_UPDATE = 5   #: OpCode for UPDATE messages
OP_DSO = 6      #: OpCode for DNS Stateful Operation (DSO) messages

#: Handy dictionary for converting numeric OPCODE into a symbolic name.
OPCODE_NAMES = {
    0: "QUERY",
    1: "IQUERY",
    2: "STATUS",
    # 3 is unassigned
    4: "NOTIFY",
    5: "UPDATE",
    6: "DSO",
}

# Possible DNS result code values are listed on the IANA DNS
# parameters page:
# https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6
# Note that we do not include the extended RCODE values here, which
# are returned in the EDNS OPT record, if present.
RCODE_NOERROR = 0   #: No Error RCODE
RCODE_FORMERR = 1   #: Format Error RCODE
RCODE_SERVFAIL = 2  #: Server Failure RCODE
RCODE_NXDOMAIN = 3  #: Non-Existent Domain RCODE
RCODE_NOTIMP = 4    #: Not Implemented RCODE
RCODE_REFUSED = 5   #: Query Refused RCODE
RCODE_YXDOMAIN = 6  #: Name Exists when it should not RCODE
RCODE_YXRRSET = 7   #: RR Set Exists when it should not RCODE
RCODE_NXRRSET = 8   #: RR Set that should exist does not RCODE
RCODE_NOTAUTH = 9   #: Server Not Authoritative for zone / Not Authorized RCODE
RCODE_NOTZONE = 10  #: Name not contained in zone RCODE
RCODE_DSOTYPENI = 11  #: DSO-TYPE Not Implemented RCODE

#: Handy dictionary for converting numeric RCODE into a symbolic name.
RCODE_NAMES = {
    0: "NoError",
    1: "FormErr",
    2: "ServFail",
    3: "NXDomain",
    4: "NotImp",
    5: "Refused",
    6: "YXDomain",
    7: "YXRRSet",
    8: "NXRRSet",
    9: "NotAuth",
    10: "NotZone",
    11: "DSOTYPENI",
}

# Resource record (RR) types are documented on the IANA DNS parameters
# page:
# https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
RTYPE_A = 1          #: a host address RTYPE
RTYPE_NS = 2         #: an authoritative name server RTYPE
RTYPE_MD = 3         #: a mail destination RTYPE
RTYPE_MF = 4         #: a mail forwarder RTYPE
RTYPE_CNAME = 5      #: the canonical name for an alias RTYPE
RTYPE_SOA = 6        #: marks the start of a zone of authority RTYPE
RTYPE_MB = 7         #: a mailbox domain name RTYPE
RTYPE_MG = 8         #: a mail group member RTYPE
RTYPE_MR = 9         #: a mail rename domain name RTYPE
RTYPE_NULL = 10      #: a null RR RTYPE
RTYPE_WKS = 11       #: a well known service description RTYPE
RTYPE_PTR = 12       #: a domain name pointer RTYPE
RTYPE_HINFO = 13     #: host information RTYPE
RTYPE_MINFO = 14     #: mailbox or mail list information RTYPE
RTYPE_MX = 15        #: mail exchange RTYPE
RTYPE_TXT = 16       #: text strings RTYPE
RTYPE_RP = 17        #: for Responsible Person RTYPE
RTYPE_AFSDB = 18     #: for AFS Data Base location RTYPE
RTYPE_X25 = 19       #: for X.25 PSDN address RTYPE
RTYPE_ISDN = 20      #: for ISDN address RTYPE
RTYPE_RT = 21        #: for Route Through RTYPE
RTYPE_NSAP = 22      #: for NSAP address, NSAP style A record RTYPE
RTYPE_NSAP_PTR = 23  #: for domain name pointer, NSAP style RTYPE
RTYPE_SIG = 24       #: for security signature RTYPE
RTYPE_KEY = 25       #: for security key RTYPE
RTYPE_PX = 26        #: X.400 mail mapping information RTYPE
RTYPE_GPOS = 27      #: Geographical Position RTYPE
RTYPE_AAAA = 28      #: IP6 Address RTYPE

RTYPE_OPT = 41       #: OPT RTYPE

RTYPE_RRSIG = 46     #: RRSIG RTYPE

RTYPE_TSIG = 250     #: Transaction Signature
RTYPE_IXFR = 251     #: incremental transfer
RTYPE_AXFR = 252     #: transfer of an entire zone
RTYPE_ANY = 255
#: A request for some or all record the server has available RTYPE

# Handy dictionary for converting numeric RR types into a symbolic name.
RTYPE_NAMES = {
    RTYPE_A: "A",
    RTYPE_NS: "NS",
    RTYPE_MD: "MD",
    RTYPE_MF: "MF",
    RTYPE_CNAME: "CNAME",
    RTYPE_SOA: "SOA",
    RTYPE_MB: "MB",
    RTYPE_MG: "MG",
    RTYPE_MR: "MR",
    RTYPE_NULL: "NULL",
    RTYPE_WKS: "WKS",
    RTYPE_PTR: "PTR",
    RTYPE_HINFO: "HINFO",
    RTYPE_MINFO: "MINFO",
    RTYPE_MX: "MX",
    RTYPE_TXT: "TXT",
    RTYPE_RP: "RP",
    RTYPE_AFSDB: "AFSDB",
    RTYPE_X25: "X25",
    RTYPE_ISDN: "ISDN",
    RTYPE_RT: "RT",
    RTYPE_NSAP: "NSAP",
    RTYPE_NSAP_PTR: "NSAP-PTR",
    RTYPE_SIG: "SIG",
    RTYPE_KEY: "KEY",
    RTYPE_PX: "PX",
    RTYPE_GPOS: "GPOS",
    RTYPE_AAAA: "AAAA",

    RTYPE_OPT: "OPT",

    RTYPE_RRSIG: "RRSIG",

    RTYPE_TSIG: "TSIG",
    RTYPE_IXFR: "IXFR",
    RTYPE_AXFR: "AXFR",
    RTYPE_ANY: "ANY",
}


[docs]def rtype_name(rtype: int) -> str: """ Converts a numeric type value to a name. This is the symbolic name if we know it (like `NS` or `AAAA`), otherwise the `RFC 3597 <https://tools.ietf.org/html/rfc3597>`_ version (`TYPE####`). :param rtype: Numeric value of an RTYPE to convert to string. :type rtype: str :return: String version of the RTYPE identifier. :rtype: str """ try: return RTYPE_NAMES[rtype] except KeyError: return f"TYPE{rtype}"
# DNS classes are documented on the IANA DNS parameters page: # https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2 CLASS_IN = 1 #: Internet class # 2 is unassigned CLASS_CH = 3 #: Chaos class CLASS_HS = 4 #: Hesiod class CLASS_NONE = 254 #: "NONE" class CLASS_ANY = 255 #: "ANY" class CLASS_NAMES = { CLASS_IN: "IN", CLASS_CH: "CH", CLASS_HS: "HS", CLASS_NONE: "NONE", CLASS_ANY: "ANY", }
[docs]def class_name(dns_class: int) -> str: """ Converts a numeric class value to a name. This is the symbolic name if we know it (like `IN` or `CH`), otherwise the `RFC 3597 <https://tools.ietf.org/html/rfc3597>`_ version (`CLASS####`). :param dns_class: Numeric value of a class to convert to string. :type dns_class: str :return: String version of the class identifier. :rtype: str """ try: return CLASS_NAMES[dns_class] except KeyError: return f"CLASS{dns_class}"