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-2023 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 2.2.4
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 {
87 
88 
89 /**
90  * @brief DNS return codes
91  **/
92 
93 typedef enum
94 {
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 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
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 A 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  uint8_t rdata[4]; //10-13
223 
224 
225 /**
226  * @brief AAAA resource record format
227  **/
228 
229 typedef __start_packed struct
230 {
231  uint16_t rtype; //0-1
232  uint16_t rclass; //2-3
233  uint32_t ttl; //4-7
234  uint16_t rdlength; //8-9
235  uint8_t rdata[16]; //10-25
237 
238 
239 /**
240  * @brief SRV resource record format
241  **/
242 
243 typedef __start_packed struct
244 {
245  uint16_t rtype; //0-1
246  uint16_t rclass; //2-3
247  uint32_t ttl; //4-7
248  uint16_t rdlength; //8-9
249  uint16_t priority; //10-11
250  uint16_t weight; //12-13
251  uint16_t port; //14-15
252  uint8_t target[]; //16
254 
255 
256 //CodeWarrior or Win32 compiler?
257 #if defined(__CWCC__) || defined(_WIN32)
258  #pragma pack(pop)
259 #endif
260 
261 //DNS related functions
262 size_t dnsEncodeName(const char_t *src, uint8_t *dest);
263 
264 size_t dnsParseName(const DnsHeader *message,
265  size_t length, size_t pos, char_t *dest, uint_t level);
266 
268  size_t pos, const char_t *name, uint_t level);
269 
270 int_t dnsCompareEncodedName(const DnsHeader *message1, size_t length1, size_t pos1,
271  const DnsHeader *message2, size_t length2, size_t pos2, uint_t level);
272 
273 void dnsGenerateIpv4ReverseName(Ipv4Addr ipv4Addr, char_t *buffer);
274 void dnsGenerateIpv6ReverseName(const Ipv6Addr *ipv6Addr, char_t *buffer);
275 
276 //C++ guard
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif
@ DNS_RR_TYPE_WKS
Well known service description.
Definition: dns_common.h:127
uint8_t length
Definition: coap_common.h:193
uint8_t opcode
Definition: dns_common.h:172
__start_packed struct @2 DnsResourceRecord
Resource record format.
@ 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:49
uint16_t weight
Definition: dns_common.h:250
@ DNS_OPCODE_QUERY
Definition: dns_common.h:81
@ DNS_RCODE_SERVER_FAILURE
Definition: dns_common.h:97
void dnsGenerateIpv6ReverseName(const Ipv6Addr *ipv6Addr, char_t *buffer)
Generate domain name for reverse DNS lookup (IPv6)
Definition: dns_common.c:490
@ 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
DnsOpcode
DNS opcodes.
Definition: dns_common.h:80
@ DNS_RCODE_FORMAT_ERROR
Definition: dns_common.h:96
@ DNS_RR_TYPE_MINFO
Mailbox or mail list information.
Definition: dns_common.h:130
__start_packed struct @0 DnsHeader
DNS message header.
@ 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
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:268
@ DNS_RCODE_QUERY_REFUSED
Definition: dns_common.h:100
uint16_t rdlength
Definition: dns_common.h:206
uint8_t level
Definition: tls.h:1800
@ DNS_RR_TYPE_AXFR
Transfer of an entire zone.
Definition: dns_common.h:140
__start_packed struct @4 DnsIpv6AddrResourceRecord
AAAA resource record format.
@ 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:133
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:342
uint16_t qclass
Definition: dns_common.h:193
uint8_t questions[]
Definition: dns_common.h:182
uint8_t rdata[]
Definition: dns_common.h:207
@ 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
__start_packed struct @5 DnsSrvResourceRecord
SRV resource record format.
@ 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:243
uint8_t z
Definition: dns_common.h:175
@ DNS_RCODE_NAME_ERROR
Definition: dns_common.h:98
uint16_t port
Definition: dns_common.h:251
char char_t
Definition: compiler_port.h:48
uint16_t nscount
Definition: dns_common.h:180
DnsResourceRecordClass
DNS resource record classes.
Definition: dns_common.h:109
__start_packed struct @1 DnsQuestion
Question format.
@ 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:59
uint8_t message[]
Definition: chap.h:152
DnsResourceRecordType
DNS resource record types.
Definition: dns_common.h:122
uint16_t id
Definition: dns_common.h:158
uint16_t arcount
Definition: dns_common.h:181
uint8_t target[]
Definition: dns_common.h:252
@ 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
void dnsGenerateIpv4ReverseName(Ipv4Addr ipv4Addr, char_t *buffer)
Generate domain name for reverse DNS lookup (IPv4)
Definition: dns_common.c:471
@ 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
@ 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:94
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:50
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:249
__start_packed struct @0 Ipv6Addr
IPv6 network address.
@ DNS_RR_TYPE_NAPTR
Naming authority pointer.
Definition: dns_common.h:136
uint16_t qtype
Definition: dns_common.h:192
__start_packed struct @3 DnsIpv4AddrResourceRecord
A resource record format.
@ DNS_RR_TYPE_HINFO
Host information.
Definition: dns_common.h:129