chromium/net/dns/public/dns_protocol.h

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DNS_PUBLIC_DNS_PROTOCOL_H_
#define NET_DNS_PUBLIC_DNS_PROTOCOL_H_

#include <stdint.h>

#include "net/base/net_export.h"

namespace net {

// General constants and structs defined by the DNS and MDNS protocols.
//
// Direct interaction with DNS and MDNS, as well as parsing DNS and MDNS
// messages, should generally only be done within network stack code.
// Network-stack-external code should interact indirectly through network
// service APIs, e.g. NetworkContext::ResolveHost(). But these constants may
// still be useful for other minor purposes.
namespace dns_protocol {

static const uint16_t kDefaultPort =;
// RFC 5353.
static const uint16_t kDefaultPortMulticast =;

// https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml#multicast-addresses-1
static const char kMdnsMulticastGroupIPv4[] =;
// https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#link-local
static const char kMdnsMulticastGroupIPv6[] =;

// DNS packet consists of a header followed by questions and/or answers.
// For the meaning of specific fields, please see RFC 1035 and 2535

// Header format.
//                                  1  1  1  1  1  1
//    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                      ID                       |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |QR|   Opcode  |AA|TC|RD|RA| Z|AD|CD|   RCODE   |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                    QDCOUNT                    |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                    ANCOUNT                    |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                    NSCOUNT                    |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                    ARCOUNT                    |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

// Question format.
//                                  1  1  1  1  1  1
//    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                                               |
//  /                     QNAME                     /
//  /                                               /
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                     QTYPE                     |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                     QCLASS                    |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

// Answer format.
//                                  1  1  1  1  1  1
//    0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                                               |
//  /                                               /
//  /                      NAME                     /
//  |                                               |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                      TYPE                     |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                     CLASS                     |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                      TTL                      |
//  |                                               |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//  |                   RDLENGTH                    |
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
//  /                     RDATA                     /
//  /                                               /
//  +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

#pragma pack(push)
#pragma pack(1)

// On-the-wire header. All uint16_t are in network order.
struct NET_EXPORT Header {};

#pragma pack(pop)

static const uint8_t kLabelMask =;
static const uint8_t kLabelPointer =;
static const uint8_t kLabelDirect =;
static const uint16_t kOffsetMask =;

// In MDns the most significant bit of the rrclass is designated as the
// "cache-flush bit", as described in http://www.rfc-editor.org/rfc/rfc6762.txt
// section 10.2.
static const uint16_t kMDnsClassMask =;

// RFC 1035, section 3.1: To simplify implementations, the total length of
// a domain name in wire form (i.e., label octets and label length octets) is
// restricted to 255 octets or less.
//
// Note that RFC 1035 is ambiguous over whether or not this limit includes the
// final zero-length terminating label, but RFC 6762 unambiguously uses the
// more permissive interpretation of not including the terminating label against
// the limit for mDNS and argues in RFC 6762 Appendix C that that is the correct
// interpretation for unicast DNS. To avoid overcomplicating logic, Chrome
// universally uses the more permissive RFC 6762 interpretation for all parsing.
static const int kMaxNameLength =;

// The maximum number of ASCII characters allowed in a domain in dotted form,
// derived from `kMaxNameLength` above by subtracting one from the count to
// correspond to the first byte, which is not available to encode characters and
// does not correspond to a dot after conversion.
static const uint16_t kMaxCharNameLength =;

// RFC 1035, section 2.3.4: labels 63 octets or less.
// Section 3.1: Each label is represented as a one octet length field followed
// by that number of octets.
const int kMaxLabelLength =;

// RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512
// bytes (not counting the IP nor UDP headers).
static const int kMaxUDPSize =;

// RFC 6762, section 17: Messages over the local link are restricted by the
// medium's MTU, and must be under 9000 bytes
static const int kMaxMulticastSize =;

// RFC 1035, Section 4.1.3.
// TYPE (2 bytes) + CLASS (2 bytes) + TTL (4 bytes) + RDLENGTH (2 bytes)
static const int kResourceRecordSizeInBytesWithoutNameAndRData =;

// DNS class types.
//
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2
static const uint16_t kClassIN =;
// RFC 6762, Section 10.2.
//
// For resource records sent through mDNS, the top bit of the class field in a
// resource record is repurposed to the cache-flush bit. This bit should only be
// used in mDNS transactions.
static const uint16_t kFlagCacheFlush =;

// DNS resource record types.
//
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
static const uint16_t kTypeA =;
static const uint16_t kTypeCNAME =;
static const uint16_t kTypeSOA =;
static const uint16_t kTypePTR =;
static const uint16_t kTypeTXT =;
static const uint16_t kTypeAAAA =;
static const uint16_t kTypeSRV =;
static const uint16_t kTypeOPT =;
static const uint16_t kTypeNSEC =;
static const uint16_t kTypeHttps =;
static const uint16_t kTypeANY =;

// DNS reply codes (RCODEs).
//
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6
static const uint8_t kRcodeNOERROR =;
static const uint8_t kRcodeFORMERR =;
static const uint8_t kRcodeSERVFAIL =;
static const uint8_t kRcodeNXDOMAIN =;
static const uint8_t kRcodeNOTIMP =;
static const uint8_t kRcodeREFUSED =;

// DNS EDNS(0) option codes (OPT)
//
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-11
static constexpr uint16_t kEdnsPadding =;
static constexpr uint16_t kEdnsExtendedDnsError =;

// DNS header flags.
//
// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-12
static const uint16_t kFlagResponse =;
static const uint16_t kFlagAA =;  // Authoritative Answer - response flag.
static const uint16_t kFlagRD =;  // Recursion Desired - query flag.
static const uint16_t kFlagTC =;  // Truncated - server flag.

// SVCB/HTTPS ServiceParamKey
//
// IANA registration pending. Values from draft-ietf-dnsop-svcb-https-08.
static constexpr uint16_t kHttpsServiceParamKeyMandatory =;
static constexpr uint16_t kHttpsServiceParamKeyAlpn =;
static constexpr uint16_t kHttpsServiceParamKeyNoDefaultAlpn =;
static constexpr uint16_t kHttpsServiceParamKeyPort =;
static constexpr uint16_t kHttpsServiceParamKeyIpv4Hint =;
static constexpr uint16_t kHttpsServiceParamKeyEchConfig =;
static constexpr uint16_t kHttpsServiceParamKeyIpv6Hint =;

// draft-ietf-dnsop-svcb-https-08#section-9
inline constexpr char kHttpsServiceDefaultAlpn[] =;

}  // namespace dns_protocol

}  // namespace net

#endif  // NET_DNS_PUBLIC_DNS_PROTOCOL_H_