blob: 03ffdc1483c16a317fc5673a57760bcb8256f5f9 [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
38/* maximum number of query records in a DNS response
39 * For now, we allow only one */
40#define DNS_MAX_QUERY_RECORDS 1
41
42/* maximum number of answer record in a DNS response */
43#define DNS_MAX_ANSWER_RECORDS ((DNS_MAX_UDP_MESSAGE - DNS_HEADER_SIZE) / DNS_MIN_RECORD_SIZE)
44
Baptiste Assmann325137d2015-04-13 23:40:55 +020045/* DNS error messages */
46#define DNS_TOO_LONG_FQDN "hostname too long"
47#define DNS_LABEL_TOO_LONG "one label too long"
48#define DNS_INVALID_CHARACTER "found an invalid character"
49
50/* dns query class */
51#define DNS_RCLASS_IN 1 /* internet class */
52
53/* dns record types (non exhaustive list) */
54#define DNS_RTYPE_A 1 /* IPv4 address */
55#define DNS_RTYPE_CNAME 5 /* canonical name */
56#define DNS_RTYPE_AAAA 28 /* IPv6 address */
57#define DNS_RTYPE_ANY 255 /* all records */
58
59/* dns rcode values */
60#define DNS_RCODE_NO_ERROR 0 /* no error */
61#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
62#define DNS_RCODE_REFUSED 5 /* query refused */
63
Baptiste Assmann042d0a12015-09-02 21:52:37 +020064/* dns flags masks */
65#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
66#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
67
Thierry Fournierac88cfe2016-02-17 22:05:30 +010068/* max number of network preference entries are avalaible from the
69 * configuration file.
70 */
71#define SRV_MAX_PREF_NET 5
72
Baptiste Assmanned97c952015-07-21 15:34:51 +020073/* DNS header size */
74#define DNS_HEADER_SIZE sizeof(struct dns_header)
75
Baptiste Assmann325137d2015-04-13 23:40:55 +020076/* DNS request or response header structure */
77struct dns_header {
Nenad Merdanovic8ab79422016-07-13 14:03:43 +020078 uint16_t id;
79 uint16_t flags;
80 uint16_t qdcount;
81 uint16_t ancount;
82 uint16_t nscount;
83 uint16_t arcount;
84} __attribute__ ((packed));
Baptiste Assmann325137d2015-04-13 23:40:55 +020085
86/* short structure to describe a DNS question */
87struct dns_question {
88 unsigned short qtype; /* question type */
89 unsigned short qclass; /* query class */
90};
91
Baptiste Assmann5748f732015-07-21 15:36:24 +020092struct dns_query_item {
93 struct list list;
94 char name[DNS_MAX_NAME_SIZE]; /* query name */
95 unsigned short type; /* question type */
96 unsigned short class; /* query class */
97};
98
99struct dns_answer_item {
100 struct list list;
101 char *name; /* answer name
102 * For SRV type, name also includes service
103 * and protocol value */
104 int16_t type; /* question type */
105 int16_t class; /* query class */
106 int32_t ttl; /* response TTL */
107 int16_t priority; /* SRV type priority */
108 int16_t weight; /* SRV type weight */
109 int16_t port; /* SRV type port */
110 int16_t data_len; /* number of bytes in target below */
111 struct sockaddr address; /* IPv4 or IPv6, network format */
112 char *target; /* Response data: SRV or CNAME type target */
113};
114
115struct dns_response_packet {
116 struct dns_header header;
117 struct list query_list;
118 struct list answer_list;
119 /* authority and additional_information ignored for now */
120};
121
Baptiste Assmann325137d2015-04-13 23:40:55 +0200122/*
123 * resolvers section and parameters. It is linked to the name servers
124 * servers points to it.
125 * current resolution are stored in a FIFO list.
126 */
127struct dns_resolvers {
128 struct list list; /* resolvers list */
129 char *id; /* resolvers unique identifier */
130 struct {
131 const char *file; /* file where the section appears */
132 int line; /* line where the section appears */
133 } conf; /* config information */
134 struct list nameserver_list; /* dns server list */
135 int count_nameservers; /* total number of nameservers in a resolvers section */
136 int resolve_retries; /* number of retries before giving up */
137 struct { /* time to: */
138 int retry; /* wait for a response before retrying */
139 } timeout;
140 struct { /* time to hold current data when */
141 int valid; /* a response is valid */
142 } hold;
143 struct task *t; /* timeout management */
144 struct list curr_resolution; /* current running resolutions */
145 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */
146 /* used by each nameserver, but stored in resolvers since there must */
147 /* be a unique relation between an eb_root and an eb_node (resolution) */
148};
149
150/*
151 * structure describing a name server used during name resolution.
152 * A name server belongs to a resolvers section.
153 */
154struct dns_nameserver {
155 struct list list; /* nameserver chained list */
156 char *id; /* nameserver unique identifier */
157 struct {
158 const char *file; /* file where the section appears */
159 int line; /* line where the section appears */
160 } conf; /* config information */
161 struct dns_resolvers *resolvers;
162 struct dgram_conn *dgram; /* transport layer */
163 struct sockaddr_storage addr; /* IP address */
164 struct { /* numbers relted to this name server: */
165 long int sent; /* - queries sent */
166 long int valid; /* - valid response */
167 long int update; /* - valid response used to update server's IP */
168 long int cname; /* - CNAME response requiring new resolution */
169 long int cname_error; /* - error when resolving CNAMEs */
170 long int any_err; /* - void response (usually because ANY qtype) */
171 long int nx; /* - NX response */
172 long int timeout; /* - queries which reached timeout */
173 long int refused; /* - queries refused */
174 long int other; /* - other type of response */
175 long int invalid; /* - malformed DNS response */
176 long int too_big; /* - too big response */
177 long int outdated; /* - outdated response (server slower than the other ones) */
Baptiste Assmann6cdea932015-09-02 21:56:05 +0200178 long int truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200179 } counters;
180};
181
Thierry Fournierada34842016-02-17 21:25:09 +0100182struct dns_options {
183 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100184 struct {
185 int family;
186 union {
187 struct in_addr in4;
188 struct in6_addr in6;
189 } addr;
190 union {
191 struct in_addr in4;
192 struct in6_addr in6;
193 } mask;
194 } pref_net[SRV_MAX_PREF_NET];
195 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100196};
197
Baptiste Assmann325137d2015-04-13 23:40:55 +0200198/*
199 * resolution structure associated to single server and used to manage name resolution for
200 * this server.
201 * The only link between the resolution and a nameserver is through the query_id.
202 */
203struct dns_resolution {
204 struct list list; /* resolution list */
205 struct dns_resolvers *resolvers; /* resolvers section associated to this resolution */
206 void *requester; /* owner of this name resolution */
207 int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *, unsigned char *, int);
208 /* requester callback for valid response */
209 int (*requester_error_cb)(struct dns_resolution *, int);
210 /* requester callback, for error management */
211 char *hostname_dn; /* server hostname in domain name label format */
212 int hostname_dn_len; /* server domain name label len */
Thierry Fournierada34842016-02-17 21:25:09 +0100213 struct dns_options *opts; /* IP selection options inherited from the configuration file. */
Baptiste Assmann189363e2015-09-03 10:55:20 +0200214 unsigned int last_resolution; /* time of the lastest valid resolution */
215 unsigned int last_sent_packet; /* time of the latest DNS packet sent */
216 unsigned int last_status_change; /* time of the latest DNS resolution status change */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200217 int query_id; /* DNS query ID dedicated for this resolution */
218 struct eb32_node qid; /* ebtree query id */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000219 int query_type;
220 /* 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 +0200221 int status; /* status of the resolution being processed RSLV_STATUS_* */
222 int step; /* */
223 int try; /* current resolution try */
224 int try_cname; /* number of CNAME requests sent */
225 int nb_responses; /* count number of responses received */
226};
227
228/* last resolution status code */
229enum {
230 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
231 RSLV_STATUS_VALID, /* no error */
232 RSLV_STATUS_INVALID, /* invalid responses */
233 RSLV_STATUS_ERROR, /* error */
234 RSLV_STATUS_NX, /* NXDOMAIN */
235 RSLV_STATUS_REFUSED, /* server refused our query */
236 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
237 RSLV_STATUS_OTHER, /* other errors */
238};
239
240/* current resolution step */
241enum {
242 RSLV_STEP_NONE = 0, /* nothing happening currently */
243 RSLV_STEP_RUNNING, /* resolution is running */
244};
245
246/* return codes after analyzing a DNS response */
247enum {
248 DNS_RESP_VALID = 0, /* valid response */
249 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
250 DNS_RESP_ERROR, /* DNS error code */
251 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
252 DNS_RESP_REFUSED, /* DNS server refused to answer */
253 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
254 DNS_RESP_WRONG_NAME, /* response does not match query name */
255 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
256 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200257 DNS_RESP_TRUNCATED, /* DNS response is truncated */
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200258 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200259};
260
261/* return codes after searching an IP in a DNS response buffer, using a family preference */
262enum {
263 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
264 * OR provided IP found and preference is not matched, but no IP
265 * matching preference was found */
266 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
267 * OR provided IP found and preference is not match and an IP
268 * matching preference was found */
269 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
270 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200271 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200272};
273
274#endif /* _TYPES_DNS_H */