Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 5 | #ifndef PATCHPANEL_DNS_DNS_PROTOCOL_H_ |
| 6 | #define PATCHPANEL_DNS_DNS_PROTOCOL_H_ |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 10 | #include "patchpanel/dns/net_export.h" |
Kevin Cernekee | d05be17 | 2017-06-17 17:40:21 -0700 | [diff] [blame] | 11 | |
| 12 | namespace net { |
| 13 | |
| 14 | namespace dns_protocol { |
| 15 | |
| 16 | static const uint16_t kDefaultPort = 53; |
| 17 | static const uint16_t kDefaultPortMulticast = 5353; |
| 18 | |
| 19 | // DNS packet consists of a header followed by questions and/or answers. |
| 20 | // For the meaning of specific fields, please see RFC 1035 and 2535 |
| 21 | |
| 22 | // Header format. |
| 23 | // 1 1 1 1 1 1 |
| 24 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 25 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 26 | // | ID | |
| 27 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 28 | // |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | |
| 29 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 30 | // | QDCOUNT | |
| 31 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 32 | // | ANCOUNT | |
| 33 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 34 | // | NSCOUNT | |
| 35 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 36 | // | ARCOUNT | |
| 37 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 38 | |
| 39 | // Question format. |
| 40 | // 1 1 1 1 1 1 |
| 41 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 |
| 42 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 43 | // | | |
| 44 | // / QNAME / |
| 45 | // / / |
| 46 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 47 | // | QTYPE | |
| 48 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 49 | // | QCLASS | |
| 50 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 51 | |
| 52 | // Answer 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 | // / / |
| 58 | // / NAME / |
| 59 | // | | |
| 60 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 61 | // | TYPE | |
| 62 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 63 | // | CLASS | |
| 64 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 65 | // | TTL | |
| 66 | // | | |
| 67 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 68 | // | RDLENGTH | |
| 69 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| |
| 70 | // / RDATA / |
| 71 | // / / |
| 72 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |
| 73 | |
| 74 | #pragma pack(push) |
| 75 | #pragma pack(1) |
| 76 | |
| 77 | // On-the-wire header. All uint16_t are in network order. |
| 78 | struct NET_EXPORT Header { |
| 79 | uint16_t id; |
| 80 | uint16_t flags; |
| 81 | uint16_t qdcount; |
| 82 | uint16_t ancount; |
| 83 | uint16_t nscount; |
| 84 | uint16_t arcount; |
| 85 | }; |
| 86 | |
| 87 | #pragma pack(pop) |
| 88 | |
| 89 | static const uint8_t kLabelMask = 0xc0; |
| 90 | static const uint8_t kLabelPointer = 0xc0; |
| 91 | static const uint8_t kLabelDirect = 0x0; |
| 92 | static const uint16_t kOffsetMask = 0x3fff; |
| 93 | |
| 94 | // In MDns the most significant bit of the rrclass is designated as the |
| 95 | // "cache-flush bit", as described in http://www.rfc-editor.org/rfc/rfc6762.txt |
| 96 | // section 10.2. |
| 97 | static const uint16_t kMDnsClassMask = 0x7FFF; |
| 98 | |
| 99 | // RFC 1035, section 3.1: To simplify implementations, the total length of |
| 100 | // a domain name (i.e., label octets and label length octets) is restricted |
| 101 | // to 255 octets or less. |
| 102 | static const int kMaxNameLength = 255; |
| 103 | |
| 104 | // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 |
| 105 | // bytes (not counting the IP nor UDP headers). |
| 106 | static const int kMaxUDPSize = 512; |
| 107 | |
| 108 | // RFC 6762, section 17: Messages over the local link are restricted by the |
| 109 | // medium's MTU, and must be under 9000 bytes |
| 110 | static const int kMaxMulticastSize = 9000; |
| 111 | |
| 112 | // DNS class types. |
| 113 | // |
| 114 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2 |
| 115 | static const uint16_t kClassIN = 1; |
| 116 | |
| 117 | // DNS resource record types. |
| 118 | // |
| 119 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4 |
| 120 | static const uint16_t kTypeA = 1; |
| 121 | static const uint16_t kTypeCNAME = 5; |
| 122 | static const uint16_t kTypePTR = 12; |
| 123 | static const uint16_t kTypeTXT = 16; |
| 124 | static const uint16_t kTypeAAAA = 28; |
| 125 | static const uint16_t kTypeSRV = 33; |
| 126 | static const uint16_t kTypeNSEC = 47; |
| 127 | |
| 128 | // DNS reply codes (RCODEs). |
| 129 | // |
| 130 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 |
| 131 | static const uint8_t kRcodeNOERROR = 0; |
| 132 | static const uint8_t kRcodeFORMERR = 1; |
| 133 | static const uint8_t kRcodeSERVFAIL = 2; |
| 134 | static const uint8_t kRcodeNXDOMAIN = 3; |
| 135 | static const uint8_t kRcodeNOTIMP = 4; |
| 136 | static const uint8_t kRcodeREFUSED = 5; |
| 137 | |
| 138 | // DNS header flags. |
| 139 | // |
| 140 | // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-12 |
| 141 | static const uint16_t kFlagResponse = 0x8000; |
| 142 | static const uint16_t kFlagRD = 0x100; // Recursion Desired - query flag. |
| 143 | static const uint16_t kFlagTC = 0x200; // Truncated - server flag. |
| 144 | |
| 145 | } // namespace dns_protocol |
| 146 | |
| 147 | } // namespace net |
| 148 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 149 | #endif // PATCHPANEL_DNS_DNS_PROTOCOL_H_ |