blob: 757eaaf28d9a90b0d8cd9a3fb7bcc1d3bf87ed06 [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
33#define DNS_MAX_UDP_MESSAGE 4096
34
35/* DNS error messages */
36#define DNS_TOO_LONG_FQDN "hostname too long"
37#define DNS_LABEL_TOO_LONG "one label too long"
38#define DNS_INVALID_CHARACTER "found an invalid character"
39
40/* dns query class */
41#define DNS_RCLASS_IN 1 /* internet class */
42
43/* dns record types (non exhaustive list) */
44#define DNS_RTYPE_A 1 /* IPv4 address */
45#define DNS_RTYPE_CNAME 5 /* canonical name */
46#define DNS_RTYPE_AAAA 28 /* IPv6 address */
47#define DNS_RTYPE_ANY 255 /* all records */
48
49/* dns rcode values */
50#define DNS_RCODE_NO_ERROR 0 /* no error */
51#define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */
52#define DNS_RCODE_REFUSED 5 /* query refused */
53
Baptiste Assmann042d0a12015-09-02 21:52:37 +020054/* dns flags masks */
55#define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */
56#define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */
57
Thierry Fournierac88cfe2016-02-17 22:05:30 +010058/* max number of network preference entries are avalaible from the
59 * configuration file.
60 */
61#define SRV_MAX_PREF_NET 5
62
Baptiste Assmann325137d2015-04-13 23:40:55 +020063/* DNS request or response header structure */
64struct dns_header {
65 unsigned short id:16; /* identifier */
66 unsigned char rd :1; /* recursion desired 0: no, 1: yes */
67 unsigned char tc :1; /* truncation 0:no, 1: yes */
68 unsigned char aa :1; /* authoritative answer 0: no, 1: yes */
69 unsigned char opcode :4; /* operation code */
70 unsigned char qr :1; /* query/response 0: query, 1: response */
71 unsigned char rcode :4; /* response code */
72 unsigned char z :1; /* no used */
73 unsigned char ad :1; /* authentic data */
74 unsigned char cd :1; /* checking disabled */
75 unsigned char ra :1; /* recursion available 0: no, 1: yes */
76 unsigned short qdcount :16; /* question count */
77 unsigned short ancount :16; /* answer count */
78 unsigned short nscount :16; /* authority count */
79 unsigned short arcount :16; /* additional count */
80};
81
82/* short structure to describe a DNS question */
83struct dns_question {
84 unsigned short qtype; /* question type */
85 unsigned short qclass; /* query class */
86};
87
88/*
89 * resolvers section and parameters. It is linked to the name servers
90 * servers points to it.
91 * current resolution are stored in a FIFO list.
92 */
93struct dns_resolvers {
94 struct list list; /* resolvers list */
95 char *id; /* resolvers unique identifier */
96 struct {
97 const char *file; /* file where the section appears */
98 int line; /* line where the section appears */
99 } conf; /* config information */
100 struct list nameserver_list; /* dns server list */
101 int count_nameservers; /* total number of nameservers in a resolvers section */
102 int resolve_retries; /* number of retries before giving up */
103 struct { /* time to: */
104 int retry; /* wait for a response before retrying */
105 } timeout;
106 struct { /* time to hold current data when */
107 int valid; /* a response is valid */
108 } hold;
109 struct task *t; /* timeout management */
110 struct list curr_resolution; /* current running resolutions */
111 struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */
112 /* used by each nameserver, but stored in resolvers since there must */
113 /* be a unique relation between an eb_root and an eb_node (resolution) */
114};
115
116/*
117 * structure describing a name server used during name resolution.
118 * A name server belongs to a resolvers section.
119 */
120struct dns_nameserver {
121 struct list list; /* nameserver chained list */
122 char *id; /* nameserver unique identifier */
123 struct {
124 const char *file; /* file where the section appears */
125 int line; /* line where the section appears */
126 } conf; /* config information */
127 struct dns_resolvers *resolvers;
128 struct dgram_conn *dgram; /* transport layer */
129 struct sockaddr_storage addr; /* IP address */
130 struct { /* numbers relted to this name server: */
131 long int sent; /* - queries sent */
132 long int valid; /* - valid response */
133 long int update; /* - valid response used to update server's IP */
134 long int cname; /* - CNAME response requiring new resolution */
135 long int cname_error; /* - error when resolving CNAMEs */
136 long int any_err; /* - void response (usually because ANY qtype) */
137 long int nx; /* - NX response */
138 long int timeout; /* - queries which reached timeout */
139 long int refused; /* - queries refused */
140 long int other; /* - other type of response */
141 long int invalid; /* - malformed DNS response */
142 long int too_big; /* - too big response */
143 long int outdated; /* - outdated response (server slower than the other ones) */
Baptiste Assmann6cdea932015-09-02 21:56:05 +0200144 long int truncated; /* - truncated response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200145 } counters;
146};
147
Thierry Fournierada34842016-02-17 21:25:09 +0100148struct dns_options {
149 int family_prio; /* which IP family should the resolver use when both are returned */
Thierry Fournierac88cfe2016-02-17 22:05:30 +0100150 struct {
151 int family;
152 union {
153 struct in_addr in4;
154 struct in6_addr in6;
155 } addr;
156 union {
157 struct in_addr in4;
158 struct in6_addr in6;
159 } mask;
160 } pref_net[SRV_MAX_PREF_NET];
161 int pref_net_nb; /* The number of registered prefered networks. */
Thierry Fournierada34842016-02-17 21:25:09 +0100162};
163
Baptiste Assmann325137d2015-04-13 23:40:55 +0200164/*
165 * resolution structure associated to single server and used to manage name resolution for
166 * this server.
167 * The only link between the resolution and a nameserver is through the query_id.
168 */
169struct dns_resolution {
170 struct list list; /* resolution list */
171 struct dns_resolvers *resolvers; /* resolvers section associated to this resolution */
172 void *requester; /* owner of this name resolution */
173 int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *, unsigned char *, int);
174 /* requester callback for valid response */
175 int (*requester_error_cb)(struct dns_resolution *, int);
176 /* requester callback, for error management */
177 char *hostname_dn; /* server hostname in domain name label format */
178 int hostname_dn_len; /* server domain name label len */
Thierry Fournierada34842016-02-17 21:25:09 +0100179 struct dns_options *opts; /* IP selection options inherited from the configuration file. */
Baptiste Assmann189363e2015-09-03 10:55:20 +0200180 unsigned int last_resolution; /* time of the lastest valid resolution */
181 unsigned int last_sent_packet; /* time of the latest DNS packet sent */
182 unsigned int last_status_change; /* time of the latest DNS resolution status change */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200183 int query_id; /* DNS query ID dedicated for this resolution */
184 struct eb32_node qid; /* ebtree query id */
Andrew Hayworthe6a4a322015-10-19 22:29:51 +0000185 int query_type;
186 /* 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 +0200187 int status; /* status of the resolution being processed RSLV_STATUS_* */
188 int step; /* */
189 int try; /* current resolution try */
190 int try_cname; /* number of CNAME requests sent */
191 int nb_responses; /* count number of responses received */
192};
193
194/* last resolution status code */
195enum {
196 RSLV_STATUS_NONE = 0, /* no resolution occured yet */
197 RSLV_STATUS_VALID, /* no error */
198 RSLV_STATUS_INVALID, /* invalid responses */
199 RSLV_STATUS_ERROR, /* error */
200 RSLV_STATUS_NX, /* NXDOMAIN */
201 RSLV_STATUS_REFUSED, /* server refused our query */
202 RSLV_STATUS_TIMEOUT, /* no response from DNS servers */
203 RSLV_STATUS_OTHER, /* other errors */
204};
205
206/* current resolution step */
207enum {
208 RSLV_STEP_NONE = 0, /* nothing happening currently */
209 RSLV_STEP_RUNNING, /* resolution is running */
210};
211
212/* return codes after analyzing a DNS response */
213enum {
214 DNS_RESP_VALID = 0, /* valid response */
215 DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */
216 DNS_RESP_ERROR, /* DNS error code */
217 DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */
218 DNS_RESP_REFUSED, /* DNS server refused to answer */
219 DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */
220 DNS_RESP_WRONG_NAME, /* response does not match query name */
221 DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */
222 DNS_RESP_TIMEOUT, /* DNS server has not answered in time */
Baptiste Assmann0df5d962015-09-02 21:58:32 +0200223 DNS_RESP_TRUNCATED, /* DNS response is truncated */
Baptiste Assmann96972bc2015-09-09 00:46:58 +0200224 DNS_RESP_NO_EXPECTED_RECORD, /* No expected records were found in the response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200225};
226
227/* return codes after searching an IP in a DNS response buffer, using a family preference */
228enum {
229 DNS_UPD_NO = 1, /* provided IP was found and preference is matched
230 * OR provided IP found and preference is not matched, but no IP
231 * matching preference was found */
232 DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found
233 * OR provided IP found and preference is not match and an IP
234 * matching preference was found */
235 DNS_UPD_CNAME, /* CNAME without any IP provided in the response */
236 DNS_UPD_NAME_ERROR, /* name in the response did not match the query */
Baptiste Assmann0453a1d2015-09-09 00:51:08 +0200237 DNS_UPD_NO_IP_FOUND, /* no IP could be found in the response */
Baptiste Assmann325137d2015-04-13 23:40:55 +0200238};
239
240#endif /* _TYPES_DNS_H */