blob: 12c11552f209f68d45225cf55363a29c78af0d80 [file] [log] [blame]
Baptiste Assmann325137d2015-04-13 23:40:55 +02001/*
2 * include/types/dns.h
3 * This file provides structures and types for DNS.
4 *
5 * Copyright (C) 2014 Baptiste Assmann <bedis9@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _TYPES_DNS_H
23#define _TYPES_DNS_H
24
25/*DNS maximum values */
26/*
27 * Maximum issued from RFC:
28 * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4
29 * RFC 2671: http://tools.ietf.org/html/rfc2671
30 */
31#define DNS_MAX_LABEL_SIZE 63
32#define DNS_MAX_NAME_SIZE 255
Baptiste Assmannd20bbaf2016-03-26 15:09:48 +010033#define DNS_MAX_UDP_MESSAGE 512
Baptiste Assmann325137d2015-04-13 23:40:55 +020034
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010035/* DNS minimun record size: 1 char + 1 NULL + type + class */
36#define DNS_MIN_RECORD_SIZE ( 1 + 1 + 2 + 2 )
37
Baptiste Assmannd0aa6d22017-04-03 14:40:20 +020038/* DNS smallest fqdn 'a.gl' size */
39# define DNS_SMALLEST_FQDN_SIZE 4
40
Baptiste Assmann4ec076f2015-12-09 14:02:01 +010041/* maximum number of query records in a DNS response
42 * For now, we allow only one */
43#define DNS_MAX_QUERY_RECORDS 1
44
45/* maximum number of answer record in a DNS response */
46#define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE)
47
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +020048/* size of dns_buffer used to store responses from the buffer
49 * dns_buffer is used to store data collected from records found in a response.
50 * Before using it, caller will always check that there is at least DNS_MAX_NAME_SIZE bytes
51 * available */
52#define DNS_ANALYZE_BUFFER_SIZE DNS_MAX_UDP_MESSAGE + DNS_MAX_NAME_SIZE
53
Baptiste Assmann325137d2015-04-13 23:40:55 +020054/* DNS error messages */
55#define DNS_TOO_LONG_FQDN "hostname too long"
56#define DNS_LABEL_TOO_LONG "one label too long"
57#define DNS_INVALID_CHARACTER "found an invalid character"
58
59/* dns query class */
60#define DNS_RCLASS_IN 1 /* internet class */
61
62/* dns record types (non exhaustive list) */
63#define DNS_RTYPE_A 1 /* IPv4 address */
64#define DNS_RTYPE_CNAME 5 /* canonical name */
65#define DNS_RTYPE_AAAA 28 /* IPv6 address */
66#define DNS_RTYPE_ANY 255 /* all records */
67
68/* dns rcode values */
69#define DNS_RCODE_NO_ERROR 0 /* no error */
70#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
71#define DNS_RCODE_REFUSED 5 /* query refused */
72
Baptiste Assmann042d0a12015-09-02 21:52:37 +020073/* dns flags masks */
74#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
75#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
76
Thierry Fournierac88cfe2016-02-17 22:05:30 +010077/* max number of network preference entries are avalaible from the
78 * configuration file.
79 */
80#define SRV_MAX_PREF_NET 5
81
Baptiste Assmanned97c952015-07-21 15:34:51 +020082/* DNS header size */
83#define DNS_HEADER_SIZE sizeof(struct dns_header)
84
Baptiste Assmann201c07f2017-05-22 15:17:15 +020085/* DNS resolution pool size, per resolvers section */
86#define DNS_DEFAULT_RESOLUTION_POOL_SIZE 64
87
Baptiste Assmann325137d2015-04-13 23:40:55 +020088/* DNS request or response header structure */
89struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +020090 uint16_t id;
91 uint16_t flags;
92 uint16_t qdcount;
93 uint16_t ancount;
94 uint16_t nscount;
95 uint16_t arcount;
96} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +020097
98/* short structure to describe a DNS question */
Baptiste Assmann83b0a172016-09-05 19:09:49 +020099/* NOTE: big endian structure */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200100struct dns_question {
101 unsigned short qtype; /* question type */
102 unsigned short qclass; /* query class */
103};
104
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200105/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200106struct dns_query_item {
107 struct list list;
108 char name[DNS_MAX_NAME_SIZE]; /* query name */
109 unsigned short type; /* question type */
110 unsigned short class; /* query class */
111};
112
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200113/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200114struct dns_answer_item {
115 struct list list;
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200116 char name[DNS_MAX_NAME_SIZE]; /* answer name
Baptiste Assmann5748f732015-07-21 15:36:24 +0200117 * For SRV type, name also includes service
118 * and protocol value */
119 int16_t type; /* question type */
120 int16_t class; /* query class */
121 int32_t ttl; /* response TTL */
122 int16_t priority; /* SRV type priority */
123 int16_t weight; /* SRV type weight */
124 int16_t port; /* SRV type port */
125 int16_t data_len; /* number of bytes in target below */
126 struct sockaddr address; /* IPv4 or IPv6, network format */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200127 char target[DNS_MAX_NAME_SIZE]; /* Response data: SRV or CNAME type target */
128 time_t last_seen; /* When was the answer was last seen */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200129};
130
131struct dns_response_packet {
132 struct dns_header header;
133 struct list query_list;
134 struct list answer_list;
135 /* authority and additional_information ignored for now */
136};
137
Baptiste Assmann325137d2015-04-13 23:40:55 +0200138/*
139 * resolvers section and parameters. It is linked to the name servers
140 * servers points to it.
141 * current resolution are stored in a FIFO list.
142 */
143struct dns_resolvers {
144 struct list list; /* resolvers list */
145 char *id; /* resolvers unique identifier */
146 struct {
147 const char *file; /* file where the section appears */
148 int line; /* line where the section appears */
149 } conf; /* config information */
150 struct list nameserver_list; /* dns server list */
151 int count_nameservers; /* total number of nameservers in a resolvers section */
152 int resolve_retries; /* number of retries before giving up */
153 struct { /* time to: */
154 int retry; /* wait for a response before retrying */
155 } timeout;
156 struct { /* time to hold current data when */
157 int valid; /* a response is valid */
Baptiste Assmann987e16d2016-11-02 22:23:31 +0100158 int nx; /* a response doesn't exist */
159 int timeout; /* no answer was delivered */
160 int refused; /* dns server refused to answer */
161 int other; /* other dns response errors */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200162 int obsolete; /* an answer hasn't been seen */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200163 } hold;
164 struct task *t; /* timeout management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200165 int resolution_pool_size; /* size of the resolution pool associated to this resolvers section */
166 struct {
167 struct list pool; /* resolution pool dedicated to this resolvers section */
168 struct list wait; /* resolutions managed to this resolvers section */
169 struct list curr; /* current running resolutions */
170 } resolution;
Baptiste Assmann325137d2015-04-13 23:40:55 +0200171 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */
172 /* used by each nameserver, but stored in resolvers since there must */
173 /* be a unique relation between an eb_root and an eb_node (resolution) */
174};
175
176/*
177 * structure describing a name server used during name resolution.
178 * A name server belongs to a resolvers section.
179 */
180struct dns_nameserver {
181 struct list list; /* nameserver chained list */
182 char *id; /* nameserver unique identifier */
183 struct {
184 const char *file; /* file where the section appears */
185 int line; /* line where the section appears */
186 } conf; /* config information */
187 struct dns_resolvers *resolvers;
188 struct dgram_conn *dgram; /* transport layer */
189 struct sockaddr_storage addr; /* IP address */
190 struct { /* numbers relted to this name server: */
191 long int sent; /* - queries sent */
192 long int valid; /* - valid response */
193 long int update; /* - valid response used to update server's IP */
194 long int cname; /* - CNAME response requiring new resolution */
195 long int cname_error; /* - error when resolving CNAMEs */
196 long int any_err; /* - void response (usually because ANY qtype) */
197 long int nx; /* - NX response */
198 long int timeout; /* - queries which reached timeout */
199 long int refused; /* - queries refused */
200 long int other; /* - other type of response */
201 long int invalid; /* - malformed DNS response */
202 long int too_big; /* - too big response */
203 long int outdated; /* - outdated response (server slower than the other ones) */
Baptiste Assmann6cdea932015-09-02 21:56:05 +0200204 long int truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200205 } counters;
206};
207
Thierry Fournierada34842016-02-17 21:25:09 +0100208struct dns_options {
209 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100210 struct {
211 int family;
212 union {
213 struct in_addr in4;
214 struct in6_addr in6;
215 } addr;
216 union {
217 struct in_addr in4;
218 struct in6_addr in6;
219 } mask;
220 } pref_net[SRV_MAX_PREF_NET];
221 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100222};
223
Baptiste Assmann325137d2015-04-13 23:40:55 +0200224/*
225 * resolution structure associated to single server and used to manage name resolution for
226 * this server.
227 * The only link between the resolution and a nameserver is through the query_id.
228 */
229struct dns_resolution {
230 struct list list; /* resolution list */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200231 struct {
232 struct list wait; /* list of standby requesters for this resolution */
233 struct list curr; /* list of requesters currently active on this resolution */
234 } requester;
Baptiste Assmann729c9012017-05-22 15:13:10 +0200235 int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200236 /* requester callback for valid response */
237 int (*requester_error_cb)(struct dns_resolution *, int);
238 /* requester callback, for error management */
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200239 int uuid; /* unique id (used for debugging purpose) */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200240 char *hostname_dn; /* server hostname in domain name label format */
241 int hostname_dn_len; /* server domain name label len */
Baptiste Assmann189363e2015-09-03 10:55:20 +0200242 unsigned int last_resolution; /* time of the lastest valid resolution */
243 unsigned int last_sent_packet; /* time of the latest DNS packet sent */
244 unsigned int last_status_change; /* time of the latest DNS resolution status change */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200245 int query_id; /* DNS query ID dedicated for this resolution */
246 struct eb32_node qid; /* ebtree query id */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000247 int query_type;
248 /* query type to send. By default DNS_RTYPE_A or DNS_RTYPE_AAAA depending on resolver_family_priority */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200249 int status; /* status of the resolution being processed RSLV_STATUS_* */
250 int step; /* */
251 int try; /* current resolution try */
252 int try_cname; /* number of CNAME requests sent */
253 int nb_responses; /* count number of responses received */
Baptiste Assmannfa4a6632017-05-04 09:05:00 +0200254 unsigned long long revision; /* updated for each update */
Baptiste Assmann729c9012017-05-22 15:13:10 +0200255 struct dns_response_packet response; /* structure hosting the DNS response */
256 struct dns_query_item response_query_records[DNS_MAX_QUERY_RECORDS]; /* <response> query records */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200257};
258
Baptiste Assmann201c07f2017-05-22 15:17:15 +0200259/*
260 * structure used to describe the owner of a DNS resolution.
261 */
262struct dns_requester {
263 struct list list; /* requester list */
264 enum obj_type *requester; /* pointer to the requester */
265 int prefered_query_type; /* prefered query type */
266 int (*requester_cb)(struct dns_requester *, struct dns_nameserver *);
267 /* requester callback for valid response */
268 int (*requester_error_cb)(struct dns_requester *, int);
269 /* requester callback, for error management */
270};
271
Baptiste Assmann325137d2015-04-13 23:40:55 +0200272/* last resolution status code */
273enum {
274 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
275 RSLV_STATUS_VALID, /* no error */
276 RSLV_STATUS_INVALID, /* invalid responses */
277 RSLV_STATUS_ERROR, /* error */
278 RSLV_STATUS_NX, /* NXDOMAIN */
279 RSLV_STATUS_REFUSED, /* server refused our query */
280 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
281 RSLV_STATUS_OTHER, /* other errors */
282};
283
284/* current resolution step */
285enum {
286 RSLV_STEP_NONE = 0, /* nothing happening currently */
287 RSLV_STEP_RUNNING, /* resolution is running */
288};
289
290/* return codes after analyzing a DNS response */
291enum {
292 DNS_RESP_VALID = 0, /* valid response */
293 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
294 DNS_RESP_ERROR, /* DNS error code */
295 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
296 DNS_RESP_REFUSED, /* DNS server refused to answer */
297 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
298 DNS_RESP_WRONG_NAME, /* response does not match query name */
299 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
300 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200301 DNS_RESP_TRUNCATED, /* DNS response is truncated */
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200302 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200303 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
Baptiste Assmann42746372017-05-03 12:12:02 +0200304 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200305};
306
307/* return codes after searching an IP in a DNS response buffer, using a family preference */
308enum {
309 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
310 * OR provided IP found and preference is not matched, but no IP
311 * matching preference was found */
312 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
313 * OR provided IP found and preference is not match and an IP
314 * matching preference was found */
315 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
316 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200317 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
Olivier Houcharda8c6db82017-07-06 18:46:47 +0200318 DNS_UPD_OBSOLETE_IP, /* The server IP was obsolete, and no other IP was found */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200319};
320
321#endif /* _TYPES_DNS_H */