blob: b5e8ff07fdf8533b644f96d29f9c32ffc1d7a46c [file] [log] [blame]
Kevin Cernekeed05be172017-06-17 17:40:21 -07001// 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 Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_DNS_DNS_PROTOCOL_H_
6#define PATCHPANEL_DNS_DNS_PROTOCOL_H_
Kevin Cernekeed05be172017-06-17 17:40:21 -07007
8#include <stdint.h>
9
Garrick Evans3388a032020-03-24 11:25:55 +090010#include "patchpanel/dns/net_export.h"
Kevin Cernekeed05be172017-06-17 17:40:21 -070011
12namespace net {
13
14namespace dns_protocol {
15
16static const uint16_t kDefaultPort = 53;
17static 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.
78struct 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
89static const uint8_t kLabelMask = 0xc0;
90static const uint8_t kLabelPointer = 0xc0;
91static const uint8_t kLabelDirect = 0x0;
92static 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.
97static 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.
102static 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).
106static 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
110static const int kMaxMulticastSize = 9000;
111
112// DNS class types.
113//
114// https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2
115static 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
120static const uint16_t kTypeA = 1;
121static const uint16_t kTypeCNAME = 5;
122static const uint16_t kTypePTR = 12;
123static const uint16_t kTypeTXT = 16;
124static const uint16_t kTypeAAAA = 28;
125static const uint16_t kTypeSRV = 33;
126static 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
131static const uint8_t kRcodeNOERROR = 0;
132static const uint8_t kRcodeFORMERR = 1;
133static const uint8_t kRcodeSERVFAIL = 2;
134static const uint8_t kRcodeNXDOMAIN = 3;
135static const uint8_t kRcodeNOTIMP = 4;
136static 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
141static const uint16_t kFlagResponse = 0x8000;
142static const uint16_t kFlagRD = 0x100; // Recursion Desired - query flag.
143static const uint16_t kFlagTC = 0x200; // Truncated - server flag.
144
145} // namespace dns_protocol
146
147} // namespace net
148
Garrick Evans3388a032020-03-24 11:25:55 +0900149#endif // PATCHPANEL_DNS_DNS_PROTOCOL_H_