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