add good bye messages
[project/mdnsd.git] / dns.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #include <fcntl.h>
18 #include <time.h>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <arpa/nameser.h>
26 #include <resolv.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <libubox/uloop.h>
31 #include <libubox/usock.h>
32 #include <libubox/utils.h>
33
34 #include "announce.h"
35 #include "util.h"
36 #include "dns.h"
37 #include "cache.h"
38 #include "service.h"
39 #include "interface.h"
40
41 static char name_buffer[MAX_NAME_LEN + 1];
42 static struct blob_buf ans_buf;
43
44 const char*
45 dns_type_string(uint16_t type)
46 {
47         static const struct {
48                 uint16_t type;
49                 char str[5];
50         } type_str[] = {
51                 { TYPE_A, "A" },
52                 { TYPE_AAAA, "AAAA" },
53                 { TYPE_PTR, "PTR" },
54                 { TYPE_TXT, "TXT" },
55                 { TYPE_SRV, "SRV" },
56                 { TYPE_ANY, "ANY" },
57         };
58         int i;
59
60         for (i = 0; i < ARRAY_SIZE(type_str); i++) {
61                 if (type == type_str[i].type)
62                         return type_str[i].str;
63         }
64
65         return "N/A";
66 }
67
68 void
69 dns_send_question(struct interface *iface, const char *question, int type, int unicast)
70 {
71         static struct dns_header h;
72         static struct dns_question q;
73         static struct iovec iov[] = {
74                 {
75                         .iov_base = &h,
76                         .iov_len = sizeof(h),
77                 },
78                 {
79                         .iov_base = name_buffer,
80                 },
81                 {
82                         .iov_base = &q,
83                         .iov_len = sizeof(q),
84                 }
85         };
86         int len;
87
88         h.questions = cpu_to_be16(1);
89         q.class = cpu_to_be16(((unicast) ? (CLASS_UNICAST) : (0))  | 1);
90         q.type = cpu_to_be16(type);
91
92         len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
93         if (len < 1)
94                 return;
95
96         iov[1].iov_len = len;
97
98         DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
99         if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
100                 perror("failed to send question :");
101 }
102
103
104 struct dns_reply {
105         int type;
106         struct dns_answer a;
107         uint16_t rdlength;
108         uint8_t *rdata;
109         char *buffer;
110 };
111
112 static int dns_answer_cnt;
113
114 void
115 dns_init_answer(void)
116 {
117         dns_answer_cnt = 0;
118         blob_buf_init(&ans_buf, 0);
119 }
120
121 void
122 dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl)
123 {
124         struct blob_attr *attr;
125         struct dns_answer *a;
126
127         attr = blob_new(&ans_buf, 0, sizeof(*a) + rdlength);
128         a = blob_data(attr);
129         a->type = cpu_to_be16(type);
130         a->class = cpu_to_be16(1);
131         a->ttl = cpu_to_be32(ttl);
132         a->rdlength = cpu_to_be16(rdlength);
133         memcpy(a + 1, rdata, rdlength);
134
135         dns_answer_cnt++;
136 }
137
138 void
139 dns_send_answer(struct interface *iface, const char *answer)
140 {
141         uint8_t buffer[256];
142         struct blob_attr *attr;
143         struct dns_header h = { 0 };
144         struct iovec *iov;
145         int answer_len, rem;
146         int n_iov = 0;
147
148         if (!dns_answer_cnt)
149                 return;
150
151         h.answers = cpu_to_be16(dns_answer_cnt);
152         h.flags = cpu_to_be16(0x8400);
153
154         iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 2) + 1));
155
156         iov[n_iov].iov_base = &h;
157         iov[n_iov].iov_len = sizeof(struct dns_header);
158         n_iov++;
159
160         answer_len = dn_comp(answer, buffer, sizeof(buffer), NULL, NULL);
161         if (answer_len < 1)
162                 return;
163
164         blob_for_each_attr(attr, ans_buf.head, rem) {
165                 struct dns_answer *a = blob_data(attr);
166
167                 iov[n_iov].iov_base = buffer;
168                 iov[n_iov].iov_len = answer_len;
169                 n_iov++;
170
171                 iov[n_iov].iov_base = blob_data(attr);
172                 iov[n_iov].iov_len = blob_len(attr);
173                 n_iov++;
174
175                 DBG(1, "A <- %s %s\n", dns_type_string(be16_to_cpu(a->type)), answer);
176         }
177
178         if (interface_send_packet(iface, iov, n_iov) < 0)
179                 fprintf(stderr, "failed to send question\n");
180 }
181
182 static int
183 scan_name(const uint8_t *buffer, int len)
184 {
185         int offset = 0;
186
187         while (len && (*buffer != '\0')) {
188                 int l = *buffer;
189
190                 if (IS_COMPRESSED(l))
191                         return offset + 2;
192
193                 len -= l + 1;
194                 offset += l + 1;
195                 buffer += l + 1;
196         }
197
198         if (!len || !offset || (*buffer != '\0'))
199                 return -1;
200
201         return offset + 1;
202 }
203
204 static struct dns_header*
205 dns_consume_header(uint8_t **data, int *len)
206 {
207         struct dns_header *h = (struct dns_header *) *data;
208         uint16_t *swap = (uint16_t *) h;
209         int endianess = 6;
210
211         if (*len < sizeof(struct dns_header))
212                 return NULL;
213
214         while (endianess--) {
215                 *swap = be16_to_cpu(*swap);
216                 swap++;
217         }
218
219         *len -= sizeof(struct dns_header);
220         *data += sizeof(struct dns_header);
221
222         return h;
223 }
224
225 static struct dns_question*
226 dns_consume_question(uint8_t **data, int *len)
227 {
228         struct dns_question *q = (struct dns_question *) *data;
229         uint16_t *swap = (uint16_t *) q;
230         int endianess = 2;
231
232         if (*len < sizeof(struct dns_question))
233                 return NULL;
234
235         while (endianess--) {
236                 *swap = be16_to_cpu(*swap);
237                 swap++;
238         }
239
240         *len -= sizeof(struct dns_question);
241         *data += sizeof(struct dns_question);
242
243         return q;
244 }
245
246 static struct dns_answer*
247 dns_consume_answer(uint8_t **data, int *len)
248 {
249         struct dns_answer *a = (struct dns_answer *) *data;
250
251         if (*len < sizeof(struct dns_answer))
252                 return NULL;
253
254         a->type = be16_to_cpu(a->type);
255         a->class = be16_to_cpu(a->class);
256         a->ttl = be32_to_cpu(a->ttl);
257         a->rdlength = be16_to_cpu(a->rdlength);
258
259         *len -= sizeof(struct dns_answer);
260         *data += sizeof(struct dns_answer);
261
262         return a;
263 }
264
265 static char *
266 dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
267 {
268         int nlen = scan_name(*data, *len);
269
270         if (nlen < 1)
271                 return NULL;
272
273         if (dn_expand(base, base + blen, *data, name_buffer, MAX_NAME_LEN) < 0) {
274                 perror("dns_consume_name/dn_expand");
275                 return NULL;
276         }
277
278         *len -= nlen;
279         *data += nlen;
280
281         return name_buffer;
282 }
283
284 static int
285 parse_answer(struct interface *iface, uint8_t *buffer, int len, uint8_t **b, int *rlen, int cache)
286 {
287         char *name = dns_consume_name(buffer, len, b, rlen);
288         struct dns_answer *a;
289         uint8_t *rdata;
290
291         if (!name) {
292                 fprintf(stderr, "dropping: bad question\n");
293                 return -1;
294         }
295
296         a = dns_consume_answer(b, rlen);
297         if (!a) {
298                 fprintf(stderr, "dropping: bad question\n");
299                 return -1;
300         }
301
302         rdata = *b;
303         if (a->rdlength > *rlen) {
304                 fprintf(stderr, "dropping: bad question\n");
305                 return -1;
306         }
307
308         *rlen -= a->rdlength;
309         *b += a->rdlength;
310
311         if (cache)
312                 cache_answer(iface, buffer, len, name, a, rdata);
313
314         return 0;
315 }
316
317 static void
318 parse_question(struct interface *iface, char *name, struct dns_question *q)
319 {
320         char *host;
321
322         if ((q->class & CLASS_UNICAST) && iface->multicast)
323                 iface = iface->peer;
324
325         DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
326
327         switch (q->type) {
328         case TYPE_ANY:
329                 if (!strcmp(name, mdns_hostname_local))
330                         service_reply(iface, NULL, announce_ttl);
331                 break;
332
333         case TYPE_PTR:
334                 service_announce_services(iface, name, announce_ttl);
335                 service_reply(iface, name, announce_ttl);
336                 break;
337
338         case TYPE_AAAA:
339         case TYPE_A:
340                 host = strstr(name, ".local");
341                 if (host)
342                         *host = '\0';
343                 if (!strcmp(mdns_hostname, name))
344                         service_reply_a(iface, announce_ttl);
345                 break;
346         };
347 }
348
349 void
350 dns_handle_packet(struct interface *iface, uint8_t *buffer, int len, int unicast)
351 {
352         struct dns_header *h;
353         uint8_t *b = buffer;
354         int rlen = len;
355
356         h = dns_consume_header(&b, &rlen);
357         if (!h) {
358                 fprintf(stderr, "dropping: bad header\n");
359                 return;
360         }
361
362         while (h->questions-- > 0) {
363                 char *name = dns_consume_name(buffer, len, &b, &rlen);
364                 struct dns_question *q;
365
366                 if (!name) {
367                         fprintf(stderr, "dropping: bad name\n");
368                         return;
369                 }
370
371                 q = dns_consume_question(&b, &rlen);
372                 if (!q) {
373                         fprintf(stderr, "dropping: bad question\n");
374                         return;
375                 }
376
377                 if (!(h->flags & FLAG_RESPONSE))
378                         parse_question(iface, name, q);
379         }
380
381         if (!(h->flags & FLAG_RESPONSE))
382                 return;
383
384         while (h->answers-- > 0)
385                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
386                         return;
387
388         while (h->authority-- > 0)
389                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
390                         return;
391
392         while (h->additional-- > 0)
393                 if (parse_answer(iface, buffer, len, &b, &rlen, 1))
394                         return;
395
396 }