dns_common.h
Go to the documentation of this file.
1 /**
2  * @file dns_common.h
3  * @brief Common DNS routines
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _DNS_COMMON_H
32 #define _DNS_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //Maximum recursion limit when parsing domain names
38 #ifndef DNS_NAME_MAX_RECURSION
39  #define DNS_NAME_MAX_RECURSION 4
40 #elif (DNS_NAME_MAX_RECURSION < 1 || DNS_NAME_MAX_RECURSION > 8)
41  #error DNS_NAME_MAX_RECURSION parameter is not valid
42 #endif
43 
44 //Maximum size of DNS messages
45 #define DNS_MESSAGE_MAX_SIZE 512
46 //Maximum size of names
47 #define DNS_NAME_MAX_SIZE 255
48 //Maximum size of labels
49 #define DNS_LABEL_MAX_SIZE 63
50 
51 //Maximum length of reverse DNS names (IPv4)
52 #define DNS_MAX_IPV4_REVERSE_NAME_LEN 15
53 //Maximum length of reverse DNS names (IPv6)
54 #define DNS_MAX_IPV6_REVERSE_NAME_LEN 63
55 
56 //DNS port number
57 #define DNS_PORT 53
58 
59 //Label compression tag
60 #define DNS_COMPRESSION_TAG 0xC0
61 
62 //Macro definition
63 #define DNS_GET_QUESTION(message, offset) (DnsQuestion *) ((uint8_t *) (message) + (offset))
64 #define DNS_GET_RESOURCE_RECORD(message, offset) (DnsResourceRecord *) ((uint8_t *) (message) + (offset))
65 
66 #define DNS_SET_NSEC_BITMAP(bitmap, type) bitmap[(type) / 8] |= 0x80 >> ((type) % 8)
67 #define DNS_CLR_NSEC_BITMAP(bitmap, type) bitmap[(type) / 8] &= ~(0x80 >> ((type) % 8))
68 
69 //C++ guard
70 #ifdef __cplusplus
71 extern "C" {
72 #endif
73 
74 
75 /**
76  * @brief DNS opcodes
77  **/
78 
79 typedef enum
80 {
86 } DnsOpcode;
87 
88 
89 /**
90  * @brief DNS return codes
91  **/
92 
93 typedef enum
94 {
101 } DnsReturnCode;
102 
103 
104 /**
105  * @brief DNS resource record classes
106  **/
107 
108 typedef enum
109 {
110  DNS_RR_CLASS_IN = 1, ///<Internet
111  DNS_RR_CLASS_CH = 3, ///<Chaos
112  DNS_RR_CLASS_HS = 4, ///<Hesiod
113  DNS_RR_CLASS_ANY = 255 ///<Any class
115 
116 
117 /**
118  * @brief DNS resource record types
119  **/
120 
121 typedef enum
122 {
123  DNS_RR_TYPE_A = 1, ///<Host address
124  DNS_RR_TYPE_NS = 2, ///<Authoritative name server
125  DNS_RR_TYPE_CNAME = 5, ///<Canonical name for an alias
126  DNS_RR_TYPE_SOA = 6, ///<Start of a zone of authority
127  DNS_RR_TYPE_WKS = 11, ///<Well known service description
128  DNS_RR_TYPE_PTR = 12, ///<Domain name pointer
129  DNS_RR_TYPE_HINFO = 13, ///<Host information
130  DNS_RR_TYPE_MINFO = 14, ///<Mailbox or mail list information
131  DNS_RR_TYPE_MX = 15, ///<Mail exchange
132  DNS_RR_TYPE_TXT = 16, ///<Text strings
133  DNS_RR_TYPE_AAAA = 28, ///<IPv6 address
134  DNS_RR_TYPE_NB = 32, ///<NetBIOS name service
135  DNS_RR_TYPE_SRV = 33, ///<Server selection
136  DNS_RR_TYPE_NAPTR = 35, ///<Naming authority pointer
137  DNS_RR_TYPE_NSEC = 47, ///<NSEC record
138  DNS_RR_TYPE_EUI48 = 108, ///<EUI-48 address
139  DNS_RR_TYPE_EUI64 = 109, ///<EUI-64 address
140  DNS_RR_TYPE_AXFR = 252, ///<Transfer of an entire zone
141  DNS_RR_TYPE_ANY = 255, ///<A request for all records
142  DNS_RR_TYPE_URI = 256 ///<Uniform resource identifier
144 
145 
146 //CodeWarrior or Win32 compiler?
147 #if defined(__CWCC__) || defined(_WIN32)
148  #pragma pack(push, 1)
149 #endif
150 
151 
152 /**
153  * @brief DNS message header
154  **/
155 
156 typedef __start_packed struct
157 {
158  uint16_t id; //0-1
159 #ifdef _CPU_BIG_ENDIAN
160  uint8_t qr : 1; //2
161  uint8_t opcode : 4;
162  uint8_t aa : 1;
163  uint8_t tc : 1;
164  uint8_t rd : 1;
165  uint8_t ra : 1; //3
166  uint8_t z : 3;
167  uint8_t rcode : 4;
168 #else
169  uint8_t rd : 1; //2
170  uint8_t tc : 1;
171  uint8_t aa : 1;
172  uint8_t opcode : 4;
173  uint8_t qr : 1;
174  uint8_t rcode : 4; //3
175  uint8_t z : 3;
176  uint8_t ra : 1;
177 #endif
178  uint16_t qdcount; //4-5
179  uint16_t ancount; //6-7
180  uint16_t nscount; //8-9
181  uint16_t arcount; //10-11
182  uint8_t questions[]; //12
184 
185 
186 /**
187  * @brief Question format
188  **/
189 
190 typedef __start_packed struct
191 {
192  uint16_t qtype;
193  uint16_t qclass;
195 
196 
197 /**
198  * @brief Resource record format
199  **/
200 
201 typedef __start_packed struct
202 {
203  uint16_t rtype; //0-1
204  uint16_t rclass; //2-3
205  uint32_t ttl; //4-7
206  uint16_t rdlength; //8-9
207  uint8_t rdata[]; //10
209 
210 
211 /**
212  * @brief SRV resource record format
213  **/
214 
215 typedef __start_packed struct
216 {
217  uint16_t rtype; //0-1
218  uint16_t rclass; //2-3
219  uint32_t ttl; //4-7
220  uint16_t rdlength; //8-9
221  uint16_t priority; //10-11
222  uint16_t weight; //12-13
223  uint16_t port; //14-15
224  uint8_t target[]; //16
226 
227 
228 //CodeWarrior or Win32 compiler?
229 #if defined(__CWCC__) || defined(_WIN32)
230  #pragma pack(pop)
231 #endif
232 
233 
234 //DNS related functions
235 size_t dnsEncodeName(const char_t *src, uint8_t *dest);
236 
237 size_t dnsParseName(const DnsHeader *message,
238  size_t length, size_t pos, char_t *dest, uint_t level);
239 
241  size_t pos, const char_t *name, uint_t level);
242 
243 int_t dnsCompareEncodedName(const DnsHeader *message1, size_t length1, size_t pos1,
244  const DnsHeader *message2, size_t length2, size_t pos2, uint_t level);
245 
246 //C++ guard
247 #ifdef __cplusplus
248 }
249 #endif
250 
251 #endif
@ DNS_RR_TYPE_WKS
Well known service description.
Definition: dns_common.h:127
uint8_t length
Definition: dtls_misc.h:149
uint8_t opcode
Definition: dns_common.h:172
@ DNS_RR_CLASS_ANY
Any class.
Definition: dns_common.h:113
uint8_t tc
Definition: dns_common.h:170
signed int int_t
Definition: compiler_port.h:44
uint16_t weight
Definition: dns_common.h:222
@ DNS_OPCODE_QUERY
Definition: dns_common.h:81
@ DNS_RCODE_SERVER_FAILURE
Definition: dns_common.h:97
@ DNS_RR_TYPE_SOA
Start of a zone of authority.
Definition: dns_common.h:126
@ DNS_RR_CLASS_CH
Chaos.
Definition: dns_common.h:111
__start_packed struct @146 DnsHeader
DNS message header.
DnsOpcode
DNS opcodes.
Definition: dns_common.h:79
@ DNS_RCODE_FORMAT_ERROR
Definition: dns_common.h:96
@ DNS_RR_TYPE_MINFO
Mailbox or mail list information.
Definition: dns_common.h:130
@ DNS_RR_TYPE_NSEC
NSEC record.
Definition: dns_common.h:137
@ DNS_RR_TYPE_EUI64
EUI-64 address.
Definition: dns_common.h:139
uint8_t aa
Definition: dns_common.h:171
uint16_t rclass
Definition: dns_common.h:204
@ DNS_RR_CLASS_IN
Internet.
Definition: dns_common.h:110
char_t name[]
@ DNS_OPCODE_STATUS
Definition: dns_common.h:83
uint8_t rcode
Definition: dns_common.h:174
uint16_t rtype
Definition: dns_common.h:203
uint16_t qdcount
Definition: dns_common.h:178
@ DNS_RCODE_QUERY_REFUSED
Definition: dns_common.h:100
uint16_t rdlength
Definition: dns_common.h:206
__start_packed struct @148 DnsResourceRecord
Resource record format.
uint8_t level
Definition: tls.h:1702
@ DNS_RR_TYPE_AXFR
Transfer of an entire zone.
Definition: dns_common.h:140
@ DNS_RR_TYPE_ANY
A request for all records.
Definition: dns_common.h:141
@ DNS_RR_TYPE_NB
NetBIOS name service.
Definition: dns_common.h:134
size_t dnsParseName(const DnsHeader *message, size_t length, size_t pos, char_t *dest, uint_t level)
Decode a domain name that uses the DNS name encoding.
Definition: dns_common.c:130
int_t dnsCompareEncodedName(const DnsHeader *message1, size_t length1, size_t pos1, const DnsHeader *message2, size_t length2, size_t pos2, uint_t level)
Compare domain names encoded with DNS notation.
Definition: dns_common.c:339
uint8_t rdata[]
Definition: dns_common.h:207
uint16_t qclass
Definition: dns_common.h:193
uint8_t questions[]
Definition: dns_common.h:182
@ DNS_RR_TYPE_A
Host address.
Definition: dns_common.h:123
uint8_t qr
Definition: dns_common.h:173
__start_packed struct _Ipv4Header __end_packed
@ DNS_RR_TYPE_PTR
Domain name pointer.
Definition: dns_common.h:128
@ DNS_OPCODE_NOTIFY
Definition: dns_common.h:84
uint16_t ancount
Definition: dns_common.h:179
@ DNS_OPCODE_UPDATE
Definition: dns_common.h:85
uint8_t ra
Definition: dns_common.h:176
@ DNS_RR_TYPE_NS
Authoritative name server.
Definition: dns_common.h:124
int_t dnsCompareName(const DnsHeader *message, size_t length, size_t pos, const char_t *name, uint_t level)
Compare domain names.
Definition: dns_common.c:240
uint8_t z
Definition: dns_common.h:175
@ DNS_RCODE_NAME_ERROR
Definition: dns_common.h:98
uint16_t port
Definition: dns_common.h:223
char char_t
Definition: compiler_port.h:43
uint16_t nscount
Definition: dns_common.h:180
DnsResourceRecordClass
DNS resource record classes.
Definition: dns_common.h:108
@ DNS_RR_TYPE_TXT
Text strings.
Definition: dns_common.h:132
size_t dnsEncodeName(const char_t *src, uint8_t *dest)
Encode a domain name using the DNS name notation.
Definition: dns_common.c:56
uint8_t message[]
Definition: chap.h:152
DnsResourceRecordType
DNS resource record types.
Definition: dns_common.h:121
uint16_t id
Definition: dns_common.h:158
uint16_t arcount
Definition: dns_common.h:181
uint8_t target[]
Definition: dns_common.h:224
@ DNS_RR_TYPE_MX
Mail exchange.
Definition: dns_common.h:131
@ DNS_RR_TYPE_CNAME
Canonical name for an alias.
Definition: dns_common.h:125
@ DNS_RR_CLASS_HS
Hesiod.
Definition: dns_common.h:112
uint8_t rd
Definition: dns_common.h:169
@ DNS_RR_TYPE_EUI48
EUI-48 address.
Definition: dns_common.h:138
__start_packed struct @147 DnsQuestion
Question format.
@ DNS_RR_TYPE_SRV
Server selection.
Definition: dns_common.h:135
@ DNS_RCODE_NOT_IMPLEMENTED
Definition: dns_common.h:99
DnsReturnCode
DNS return codes.
Definition: dns_common.h:93
uint32_t ttl
Definition: dns_common.h:205
@ DNS_OPCODE_INVERSE_QUERY
Definition: dns_common.h:82
unsigned int uint_t
Definition: compiler_port.h:45
TCP/IP stack core.
@ DNS_RCODE_NO_ERROR
Definition: dns_common.h:95
@ DNS_RR_TYPE_URI
Uniform resource identifier.
Definition: dns_common.h:142
@ DNS_RR_TYPE_AAAA
IPv6 address.
Definition: dns_common.h:133
uint16_t priority
Definition: dns_common.h:221
@ DNS_RR_TYPE_NAPTR
Naming authority pointer.
Definition: dns_common.h:136
uint16_t qtype
Definition: dns_common.h:192
@ DNS_RR_TYPE_HINFO
Host information.
Definition: dns_common.h:129
__start_packed struct @149 DnsSrvResourceRecord
SRV resource record format.