blob: c86eb4dfef314903f3dd93e2fb6a3ce5316f8af2 [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 Assmann325137d2015-04-13 23:40:55 +020085/* DNS request or response header structure */
86struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +020087 uint16_t id;
88 uint16_t flags;
89 uint16_t qdcount;
90 uint16_t ancount;
91 uint16_t nscount;
92 uint16_t arcount;
93} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +020094
95/* short structure to describe a DNS question */
Baptiste Assmann83b0a172016-09-05 19:09:49 +020096/* NOTE: big endian structure */
Baptiste Assmann325137d2015-04-13 23:40:55 +020097struct dns_question {
98 unsigned short qtype; /* question type */
99 unsigned short qclass; /* query class */
100};
101
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200102/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200103struct dns_query_item {
104 struct list list;
105 char name[DNS_MAX_NAME_SIZE]; /* query name */
106 unsigned short type; /* question type */
107 unsigned short class; /* query class */
108};
109
Baptiste Assmann83b0a172016-09-05 19:09:49 +0200110/* NOTE: big endian structure */
Baptiste Assmann5748f732015-07-21 15:36:24 +0200111struct dns_answer_item {
112 struct list list;
113 char *name; /* answer name
114 * For SRV type, name also includes service
115 * and protocol value */
116 int16_t type; /* question type */
117 int16_t class; /* query class */
118 int32_t ttl; /* response TTL */
119 int16_t priority; /* SRV type priority */
120 int16_t weight; /* SRV type weight */
121 int16_t port; /* SRV type port */
122 int16_t data_len; /* number of bytes in target below */
123 struct sockaddr address; /* IPv4 or IPv6, network format */
124 char *target; /* Response data: SRV or CNAME type target */
125};
126
127struct dns_response_packet {
128 struct dns_header header;
129 struct list query_list;
130 struct list answer_list;
131 /* authority and additional_information ignored for now */
132};
133
Baptiste Assmann325137d2015-04-13 23:40:55 +0200134/*
135 * resolvers section and parameters. It is linked to the name servers
136 * servers points to it.
137 * current resolution are stored in a FIFO list.
138 */
139struct dns_resolvers {
140 struct list list; /* resolvers list */
141 char *id; /* resolvers unique identifier */
142 struct {
143 const char *file; /* file where the section appears */
144 int line; /* line where the section appears */
145 } conf; /* config information */
146 struct list nameserver_list; /* dns server list */
147 int count_nameservers; /* total number of nameservers in a resolvers section */
148 int resolve_retries; /* number of retries before giving up */
149 struct { /* time to: */
150 int retry; /* wait for a response before retrying */
151 } timeout;
152 struct { /* time to hold current data when */
153 int valid; /* a response is valid */
Baptiste Assmann987e16d2016-11-02 22:23:31 +0100154 int nx; /* a response doesn't exist */
155 int timeout; /* no answer was delivered */
156 int refused; /* dns server refused to answer */
157 int other; /* other dns response errors */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200158 } hold;
159 struct task *t; /* timeout management */
160 struct list curr_resolution; /* current running resolutions */
161 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */
162 /* used by each nameserver, but stored in resolvers since there must */
163 /* be a unique relation between an eb_root and an eb_node (resolution) */
164};
165
166/*
167 * structure describing a name server used during name resolution.
168 * A name server belongs to a resolvers section.
169 */
170struct dns_nameserver {
171 struct list list; /* nameserver chained list */
172 char *id; /* nameserver unique identifier */
173 struct {
174 const char *file; /* file where the section appears */
175 int line; /* line where the section appears */
176 } conf; /* config information */
177 struct dns_resolvers *resolvers;
178 struct dgram_conn *dgram; /* transport layer */
179 struct sockaddr_storage addr; /* IP address */
180 struct { /* numbers relted to this name server: */
181 long int sent; /* - queries sent */
182 long int valid; /* - valid response */
183 long int update; /* - valid response used to update server's IP */
184 long int cname; /* - CNAME response requiring new resolution */
185 long int cname_error; /* - error when resolving CNAMEs */
186 long int any_err; /* - void response (usually because ANY qtype) */
187 long int nx; /* - NX response */
188 long int timeout; /* - queries which reached timeout */
189 long int refused; /* - queries refused */
190 long int other; /* - other type of response */
191 long int invalid; /* - malformed DNS response */
192 long int too_big; /* - too big response */
193 long int outdated; /* - outdated response (server slower than the other ones) */
Baptiste Assmann6cdea932015-09-02 21:56:05 +0200194 long int truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200195 } counters;
196};
197
Thierry Fournierada34842016-02-17 21:25:09 +0100198struct dns_options {
199 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100200 struct {
201 int family;
202 union {
203 struct in_addr in4;
204 struct in6_addr in6;
205 } addr;
206 union {
207 struct in_addr in4;
208 struct in6_addr in6;
209 } mask;
210 } pref_net[SRV_MAX_PREF_NET];
211 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100212};
213
Baptiste Assmann325137d2015-04-13 23:40:55 +0200214/*
215 * resolution structure associated to single server and used to manage name resolution for
216 * this server.
217 * The only link between the resolution and a nameserver is through the query_id.
218 */
219struct dns_resolution {
220 struct list list; /* resolution list */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200221 void *requester; /* owner of this name resolution */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200222 int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *, struct dns_response_packet *);
Baptiste Assmann325137d2015-04-13 23:40:55 +0200223 /* requester callback for valid response */
224 int (*requester_error_cb)(struct dns_resolution *, int);
225 /* requester callback, for error management */
226 char *hostname_dn; /* server hostname in domain name label format */
227 int hostname_dn_len; /* server domain name label len */
Baptiste Assmann189363e2015-09-03 10:55:20 +0200228 unsigned int last_resolution; /* time of the lastest valid resolution */
229 unsigned int last_sent_packet; /* time of the latest DNS packet sent */
230 unsigned int last_status_change; /* time of the latest DNS resolution status change */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200231 int query_id; /* DNS query ID dedicated for this resolution */
232 struct eb32_node qid; /* ebtree query id */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000233 int query_type;
234 /* 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 +0200235 int status; /* status of the resolution being processed RSLV_STATUS_* */
236 int step; /* */
237 int try; /* current resolution try */
238 int try_cname; /* number of CNAME requests sent */
239 int nb_responses; /* count number of responses received */
240};
241
242/* last resolution status code */
243enum {
244 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
245 RSLV_STATUS_VALID, /* no error */
246 RSLV_STATUS_INVALID, /* invalid responses */
247 RSLV_STATUS_ERROR, /* error */
248 RSLV_STATUS_NX, /* NXDOMAIN */
249 RSLV_STATUS_REFUSED, /* server refused our query */
250 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
251 RSLV_STATUS_OTHER, /* other errors */
252};
253
254/* current resolution step */
255enum {
256 RSLV_STEP_NONE = 0, /* nothing happening currently */
257 RSLV_STEP_RUNNING, /* resolution is running */
258};
259
260/* return codes after analyzing a DNS response */
261enum {
262 DNS_RESP_VALID = 0, /* valid response */
263 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
264 DNS_RESP_ERROR, /* DNS error code */
265 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
266 DNS_RESP_REFUSED, /* DNS server refused to answer */
267 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
268 DNS_RESP_WRONG_NAME, /* response does not match query name */
269 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
270 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200271 DNS_RESP_TRUNCATED, /* DNS response is truncated */
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200272 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
Baptiste Assmannc1ce5f32016-05-14 11:26:22 +0200273 DNS_RESP_QUERY_COUNT_ERROR, /* we did not get the expected number of queries in the response */
Baptiste Assmann42746372017-05-03 12:12:02 +0200274 DNS_RESP_INTERNAL, /* internal resolver error */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200275};
276
277/* return codes after searching an IP in a DNS response buffer, using a family preference */
278enum {
279 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
280 * OR provided IP found and preference is not matched, but no IP
281 * matching preference was found */
282 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
283 * OR provided IP found and preference is not match and an IP
284 * matching preference was found */
285 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
286 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200287 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200288};
289
290#endif /* _TYPES_DNS_H */