1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_DNS_PUBLIC_DNS_PROTOCOL_H_ 6 #define NET_DNS_PUBLIC_DNS_PROTOCOL_H_ 7 8 #include <stdint.h> 9 10 #include "net/base/net_export.h" 11 12 namespace net { 13 14 // General constants and structs defined by the DNS and MDNS protocols. 15 // 16 // Direct interaction with DNS and MDNS, as well as parsing DNS and MDNS 17 // messages, should generally only be done within network stack code. 18 // Network-stack-external code should interact indirectly through network 19 // service APIs, e.g. NetworkContext::ResolveHost(). But these constants may 20 // still be useful for other minor purposes. 21 namespace dns_protocol { 22 23 static const uint16_t kDefaultPort = 53; 24 // RFC 5353. 25 static const uint16_t kDefaultPortMulticast = 5353; 26 27 // https://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml#multicast-addresses-1 28 static const char kMdnsMulticastGroupIPv4[] = "224.0.0.251"; 29 // https://www.iana.org/assignments/ipv6-multicast-addresses/ipv6-multicast-addresses.xhtml#link-local 30 static const char kMdnsMulticastGroupIPv6[] = "FF02::FB"; 31 32 // DNS packet consists of a header followed by questions and/or answers. 33 // For the meaning of specific fields, please see RFC 1035 and 2535 34 35 // Header format. 36 // 1 1 1 1 1 1 37 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 38 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 39 // | ID | 40 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 41 // |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | 42 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 43 // | QDCOUNT | 44 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 45 // | ANCOUNT | 46 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 47 // | NSCOUNT | 48 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 49 // | ARCOUNT | 50 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 51 52 // Question format. 53 // 1 1 1 1 1 1 54 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 55 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 56 // | | 57 // / QNAME / 58 // / / 59 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 60 // | QTYPE | 61 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 62 // | QCLASS | 63 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 64 65 // Answer format. 66 // 1 1 1 1 1 1 67 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 68 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 69 // | | 70 // / / 71 // / NAME / 72 // | | 73 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 74 // | TYPE | 75 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 76 // | CLASS | 77 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 78 // | TTL | 79 // | | 80 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 81 // | RDLENGTH | 82 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| 83 // / RDATA / 84 // / / 85 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 86 87 #pragma pack(push) 88 #pragma pack(1) 89 90 // On-the-wire header. All uint16_t are in network order. 91 struct NET_EXPORT Header { 92 uint16_t id = 0; 93 uint16_t flags = 0; 94 uint16_t qdcount = 0; 95 uint16_t ancount = 0; 96 uint16_t nscount = 0; 97 uint16_t arcount = 0; 98 }; 99 100 #pragma pack(pop) 101 102 static const uint8_t kLabelMask = 0xc0; 103 static const uint8_t kLabelPointer = 0xc0; 104 static const uint8_t kLabelDirect = 0x0; 105 static const uint16_t kOffsetMask = 0x3fff; 106 107 // In MDns the most significant bit of the rrclass is designated as the 108 // "cache-flush bit", as described in http://www.rfc-editor.org/rfc/rfc6762.txt 109 // section 10.2. 110 static const uint16_t kMDnsClassMask = 0x7FFF; 111 112 // RFC 1035, section 3.1: To simplify implementations, the total length of 113 // a domain name in wire form (i.e., label octets and label length octets) is 114 // restricted to 255 octets or less. 115 // 116 // Note that RFC 1035 is ambiguous over whether or not this limit includes the 117 // final zero-length terminating label, but RFC 6762 unambiguously uses the 118 // more permissive interpretation of not including the terminating label against 119 // the limit for mDNS and argues in RFC 6762 Appendix C that that is the correct 120 // interpretation for unicast DNS. To avoid overcomplicating logic, Chrome 121 // universally uses the more permissive RFC 6762 interpretation for all parsing. 122 static const int kMaxNameLength = 255; 123 124 // The maximum number of ASCII characters allowed in a domain in dotted form, 125 // derived from `kMaxNameLength` above by subtracting one from the count to 126 // correspond to the first byte, which is not available to encode characters and 127 // does not correspond to a dot after conversion. 128 static const uint16_t kMaxCharNameLength = 254; 129 130 // RFC 1035, section 2.3.4: labels 63 octets or less. 131 // Section 3.1: Each label is represented as a one octet length field followed 132 // by that number of octets. 133 const int kMaxLabelLength = 63; 134 135 // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 136 // bytes (not counting the IP nor UDP headers). 137 static const int kMaxUDPSize = 512; 138 139 // RFC 6762, section 17: Messages over the local link are restricted by the 140 // medium's MTU, and must be under 9000 bytes 141 static const int kMaxMulticastSize = 9000; 142 143 // RFC 1035, Section 4.1.3. 144 // TYPE (2 bytes) + CLASS (2 bytes) + TTL (4 bytes) + RDLENGTH (2 bytes) 145 static const int kResourceRecordSizeInBytesWithoutNameAndRData = 10; 146 147 // DNS class types. 148 // 149 // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2 150 static const uint16_t kClassIN = 1; 151 // RFC 6762, Section 10.2. 152 // 153 // For resource records sent through mDNS, the top bit of the class field in a 154 // resource record is repurposed to the cache-flush bit. This bit should only be 155 // used in mDNS transactions. 156 static const uint16_t kFlagCacheFlush = 0x8000; 157 158 // DNS resource record types. 159 // 160 // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4 161 static const uint16_t kTypeA = 1; 162 static const uint16_t kTypeCNAME = 5; 163 static const uint16_t kTypeSOA = 6; 164 static const uint16_t kTypePTR = 12; 165 static const uint16_t kTypeTXT = 16; 166 static const uint16_t kTypeAAAA = 28; 167 static const uint16_t kTypeSRV = 33; 168 static const uint16_t kTypeOPT = 41; 169 static const uint16_t kTypeNSEC = 47; 170 static const uint16_t kTypeHttps = 65; 171 static const uint16_t kTypeANY = 255; 172 173 // DNS reply codes (RCODEs). 174 // 175 // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 176 static const uint8_t kRcodeNOERROR = 0; 177 static const uint8_t kRcodeFORMERR = 1; 178 static const uint8_t kRcodeSERVFAIL = 2; 179 static const uint8_t kRcodeNXDOMAIN = 3; 180 static const uint8_t kRcodeNOTIMP = 4; 181 static const uint8_t kRcodeREFUSED = 5; 182 183 // DNS EDNS(0) option codes (OPT) 184 // 185 // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-11 186 static constexpr uint16_t kEdnsPadding = 12; 187 static constexpr uint16_t kEdnsExtendedDnsError = 15; 188 189 // DNS header flags. 190 // 191 // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-12 192 static const uint16_t kFlagResponse = 0x8000; 193 static const uint16_t kFlagAA = 0x400; // Authoritative Answer - response flag. 194 static const uint16_t kFlagRD = 0x100; // Recursion Desired - query flag. 195 static const uint16_t kFlagTC = 0x200; // Truncated - server flag. 196 197 // SVCB/HTTPS ServiceParamKey 198 // 199 // IANA registration pending. Values from draft-ietf-dnsop-svcb-https-08. 200 static constexpr uint16_t kHttpsServiceParamKeyMandatory = 0; 201 static constexpr uint16_t kHttpsServiceParamKeyAlpn = 1; 202 static constexpr uint16_t kHttpsServiceParamKeyNoDefaultAlpn = 2; 203 static constexpr uint16_t kHttpsServiceParamKeyPort = 3; 204 static constexpr uint16_t kHttpsServiceParamKeyIpv4Hint = 4; 205 static constexpr uint16_t kHttpsServiceParamKeyEchConfig = 5; 206 static constexpr uint16_t kHttpsServiceParamKeyIpv6Hint = 6; 207 208 // draft-ietf-dnsop-svcb-https-08#section-9 209 inline constexpr char kHttpsServiceDefaultAlpn[] = "http/1.1"; 210 211 } // namespace dns_protocol 212 213 } // namespace net 214 215 #endif // NET_DNS_PUBLIC_DNS_PROTOCOL_H_ 216