Fix code freeing cached non-A(AAA) records too early
[project/mdnsd.git] / cache.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 #define _GNU_SOURCE
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 #include <fcntl.h>
19 #include <time.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <arpa/nameser.h>
28 #include <resolv.h>
29 #include <time.h>
30
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/kvlist.h>
36 #include <libubus.h>
37
38 #include "cache.h"
39 #include "util.h"
40 #include "dns.h"
41 #include "interface.h"
42
43 static struct uloop_timeout cache_gc;
44 struct avl_tree services;
45 AVL_TREE(records, avl_strcmp, true, NULL);
46
47 static void
48 cache_record_free(struct cache_record *r)
49 {
50 DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
51 avl_delete(&records, &r->avl);
52 free(r);
53 }
54
55 static void
56 cache_service_free(struct cache_service *s)
57 {
58 DBG(2, "%s\n", s->entry);
59 avl_delete(&services, &s->avl);
60 free(s);
61 }
62
63 static int
64 cache_is_expired(time_t t, uint32_t ttl, int frac)
65 {
66 if (monotonic_time() - t >= ttl * frac / 100)
67 return 1;
68
69 return 0;
70 }
71
72 static void
73 cache_gc_timer(struct uloop_timeout *timeout)
74 {
75 struct cache_record *r, *p;
76 struct cache_service *s, *t;
77
78 avl_for_each_element_safe(&records, r, avl, p) {
79 if (!cache_is_expired(r->time, r->ttl, r->refresh))
80 continue;
81 /* Records other than A(AAA) are handled as services */
82 if (r->type != TYPE_A && r->type != TYPE_AAAA) {
83 if (cache_is_expired(r->time, r->ttl, 100))
84 cache_record_free(r);
85 continue;
86 }
87 if (r->refresh >= 100) {
88 cache_record_free(r);
89 continue;
90 }
91 r->refresh += 50;
92 dns_send_question(r->iface, r->record, r->type, 0);
93 }
94
95 avl_for_each_element_safe(&services, s, avl, t) {
96 if (!s->host)
97 continue;
98 if (!cache_is_expired(s->time, s->ttl, s->refresh))
99 continue;
100 if (s->refresh >= 100) {
101 cache_service_free(s);
102 continue;
103 }
104 s->refresh += 50;
105 dns_send_question(s->iface, s->entry, TYPE_PTR, 0);
106 }
107
108 uloop_timeout_set(timeout, 10000);
109 }
110
111 int
112 cache_init(void)
113 {
114 avl_init(&services, avl_strcmp, true, NULL);
115
116 cache_gc.cb = cache_gc_timer;
117 uloop_timeout_set(&cache_gc, 10000);
118
119 return 0;
120 }
121
122 void cache_cleanup(struct interface *iface)
123 {
124 struct cache_record *r, *p;
125 struct cache_service *s, *t;
126
127 avl_for_each_element_safe(&services, s, avl, t)
128 if (!iface || iface == s->iface)
129 cache_service_free(s);
130
131 avl_for_each_element_safe(&records, r, avl, p)
132 if (!iface || iface == r->iface)
133 cache_record_free(r);
134 }
135
136 void
137 cache_update(void)
138 {
139 struct interface *iface;
140 struct cache_service *s;
141
142 vlist_for_each_element(&interfaces, iface, node)
143 avl_for_each_element(&services, s, avl)
144 dns_send_question(iface, s->entry, TYPE_PTR, 0);
145 }
146
147 static struct cache_service*
148 cache_service(struct interface *iface, char *entry, int hlen, int ttl)
149 {
150 struct cache_service *s, *t;
151 char *entry_buf;
152 char *host_buf;
153 char *type;
154
155 avl_for_each_element_safe(&services, s, avl, t)
156 if (!strcmp(s->entry, entry)) {
157 s->refresh = 50;
158 s->time = monotonic_time();
159 s->ttl = ttl;
160 return s;
161 }
162
163 s = calloc_a(sizeof(*s),
164 &entry_buf, strlen(entry) + 1,
165 &host_buf, hlen ? hlen + 1 : 0);
166
167 s->avl.key = s->entry = strcpy(entry_buf, entry);
168 s->time = monotonic_time();
169 s->ttl = ttl;
170 s->iface = iface;
171 s->refresh = 50;
172
173 if (hlen)
174 s->host = strncpy(host_buf, s->entry, hlen);
175
176 type = strstr(entry_buf, "._");
177 if (type)
178 type++;
179 if (type)
180 s->avl.key = type;
181 avl_insert(&services, &s->avl);
182
183 if (!hlen)
184 dns_send_question(iface, entry, TYPE_PTR, iface->multicast);
185
186 return s;
187 }
188
189 static struct cache_record*
190 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
191 {
192 struct cache_record *l = avl_find_element(&records, record, l, avl);
193
194 if (!l)
195 return NULL;
196
197 while (l && l->record && !strcmp(l->record, record)) {
198 struct cache_record *r = l;
199
200 l = avl_next_element(l, avl);
201 if (r->type != type)
202 continue;
203
204 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
205 return r;
206
207 if (r->port != port)
208 continue;
209
210 if (r->rdlength != rdlength)
211 continue;
212
213 if (!!r->rdata != !!rdata)
214 continue;
215
216 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
217 continue;
218
219 return r;
220 }
221
222 return NULL;
223 }
224
225 int
226 cache_host_is_known(char *record)
227 {
228 struct cache_record *l = avl_find_element(&records, record, l, avl);
229
230 if (!l)
231 return 0;
232
233 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
234 struct cache_record *r = l;
235
236 l = avl_next_element(l, avl);
237 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
238 continue;
239 return 1;
240 }
241
242 return 0;
243 }
244
245 void
246 cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata, int flush)
247 {
248 struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
249 struct cache_record *r;
250 int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
251 char *p = NULL;
252 char *name_buf;
253 void *rdata_ptr, *txt_ptr;
254 int host_len = 0;
255 static char *rdata_buffer = (char *) mdns_buf;
256 time_t now = monotonic_time();
257
258 nlen = strlen(name);
259
260 switch (a->type) {
261 case TYPE_PTR:
262 if (a->rdlength < 2)
263 return;
264
265 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
266 perror("process_answer/dn_expand");
267 return;
268 }
269
270 DBG(1, "A -> %s %s %s ttl:%d\n", dns_type_string(a->type), name, rdata_buffer, a->ttl);
271
272 rdlength = strlen(rdata_buffer);
273
274 if (strcmp(C_DNS_SD, name) != 0 &&
275 nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
276 host_len = rdlength - nlen - 1;
277
278 if (name[0] == '_')
279 cache_service(iface, rdata_buffer, host_len, a->ttl);
280
281 dlen = strlen(rdata_buffer) + 1;
282 rdata = (uint8_t*)rdata_buffer;
283 break;
284
285 case TYPE_SRV:
286 if (a->rdlength < 8)
287 return;
288
289 port = be16_to_cpu(dsd->port);
290 memcpy(rdata_buffer, dsd, sizeof(*dsd));
291 if (dn_expand(base, base + blen, (const uint8_t*)&dsd[1],
292 &rdata_buffer[sizeof(*dsd)], MAX_DATA_LEN - sizeof(*dsd)) < 0) {
293 perror("process_answer/dn_expand");
294 return;
295 }
296 dlen = sizeof(*dsd) + strlen(&rdata_buffer[sizeof(*dsd)]) + 1;
297 rdata = (uint8_t*)rdata_buffer;
298 break;
299
300 case TYPE_TXT:
301 rdlength = a->rdlength;
302 if (rdlength <= 2)
303 return;
304
305 memcpy(rdata_buffer, &rdata[1], rdlength);
306 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
307 tlen = rdlength + 1;
308 p = &rdata_buffer[*rdata];
309
310 do {
311 uint8_t v = *p;
312
313 *p = '\0';
314 if (v && p + v < &rdata_buffer[rdlength])
315 p += v + 1;
316 } while (*p);
317 break;
318
319 case TYPE_A:
320 if (a->rdlength != 4)
321 return;
322 dlen = 4;
323 break;
324
325 case TYPE_AAAA:
326 if (a->rdlength != 16)
327 return;
328 dlen = 16;
329 break;
330
331 default:
332 return;
333 }
334
335 r = cache_record_find(name, a->type, port, dlen, rdata);
336 if (r) {
337 if (!a->ttl) {
338 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
339 r->time = now + 1 - r->ttl;
340 r->refresh = 100;
341 } else {
342 r->ttl = a->ttl;
343 r->time = now;
344 r->refresh = 50;
345 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
346 }
347 return;
348 }
349
350 if (!a->ttl)
351 return;
352
353 r = calloc_a(sizeof(*r),
354 &name_buf, strlen(name) + 1,
355 &txt_ptr, tlen,
356 &rdata_ptr, dlen);
357
358 r->avl.key = r->record = strcpy(name_buf, name);
359 r->type = a->type;
360 r->ttl = a->ttl;
361 r->port = port;
362 r->rdlength = dlen;
363 r->time = now;
364 r->iface = iface;
365 r->refresh = 50;
366
367 if (tlen)
368 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
369
370 if (dlen)
371 r->rdata = memcpy(rdata_ptr, rdata, dlen);
372
373 if (avl_insert(&records, &r->avl))
374 free(r);
375 else
376 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
377 }
378
379 void
380 cache_dump_records(struct blob_buf *buf, const char *name)
381 {
382 struct cache_record *r, *last, *next;
383 const char *txt;
384 char buffer[INET6_ADDRSTRLEN];
385
386 last = avl_last_element(&records, last, avl);
387 for (r = avl_find_element(&records, name, r, avl); r; r = next) {
388 switch (r->type) {
389 case TYPE_TXT:
390 if (r->txt && strlen(r->txt)) {
391 txt = r->txt;
392 do {
393 blobmsg_add_string(buf, "txt", txt);
394 txt = &txt[strlen(txt) + 1];
395 } while (*txt);
396 }
397 break;
398
399 case TYPE_SRV:
400 if (r->port)
401 blobmsg_add_u32(buf, "port", r->port);
402 break;
403
404 case TYPE_A:
405 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
406 blobmsg_add_string(buf, "ipv4", buffer);
407 break;
408
409 case TYPE_AAAA:
410 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
411 blobmsg_add_string(buf, "ipv6", buffer);
412 break;
413 }
414
415 if (r == last)
416 break;
417
418 next = avl_next_element(r, avl);
419 if (strcmp(r->record, next->record) != 0)
420 break;
421 }
422 }
423
424 void
425 cache_dump_recursive(struct blob_buf *b, const char *name, uint16_t type, struct interface *iface)
426 {
427 time_t now = monotonic_time();
428 for (struct cache_record *r = avl_find_ge_element(&records, name, r, avl);
429 r && !strcmp(r->record, name);
430 r = !avl_is_last(&records, &r->avl) ? avl_next_element(r, avl) : NULL) {
431 int32_t ttl = r->ttl - (now - r->time);
432 if (ttl <= 0 || (iface && iface->ifindex != r->iface->ifindex) ||
433 (type != TYPE_ANY && type != r->type))
434 continue;
435
436 const char *txt;
437 char buf[INET6_ADDRSTRLEN];
438 void *k = blobmsg_open_table(b, NULL), *l;
439 const struct dns_srv_data *dsd = (const struct dns_srv_data*)r->rdata;
440
441 blobmsg_add_string(b, "name", r->record);
442 blobmsg_add_string(b, "type", dns_type_string(r->type));
443 blobmsg_add_u32(b, "ttl", ttl);
444
445 switch (r->type) {
446 case TYPE_TXT:
447 if ((txt = r->txt) && strlen(txt)) {
448 l = blobmsg_open_array(b, "data");
449 do {
450 blobmsg_add_string(b, NULL, txt);
451 txt = &txt[strlen(txt) + 1];
452 } while (*txt);
453 blobmsg_close_array(b, l);
454 }
455 break;
456
457 case TYPE_SRV:
458 if (r->rdlength > sizeof(*dsd)) {
459 blobmsg_add_u32(b, "priority", be16_to_cpu(dsd->priority));
460 blobmsg_add_u32(b, "weight", be16_to_cpu(dsd->weight));
461 blobmsg_add_u32(b, "port", be16_to_cpu(dsd->port));
462 blobmsg_add_string(b, "target", (const char*)&dsd[1]);
463 }
464 break;
465
466 case TYPE_PTR:
467 if (r->rdlength > 0)
468 blobmsg_add_string(b, "target", (const char*)r->rdata);
469 break;
470
471 case TYPE_A:
472 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buf, sizeof(buf)))
473 blobmsg_add_string(b, "target", buf);
474 break;
475
476 case TYPE_AAAA:
477 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buf, sizeof(buf)))
478 blobmsg_add_string(b, "target", buf);
479 break;
480 }
481
482 blobmsg_close_table(b, k);
483
484
485 if (r->type == TYPE_PTR) {
486 cache_dump_recursive(b, (const char*)r->rdata, TYPE_SRV, iface);
487 cache_dump_recursive(b, (const char*)r->rdata, TYPE_TXT, iface);
488 }
489
490 if (r->type == TYPE_SRV) {
491 cache_dump_recursive(b, (const char*)&dsd[1], TYPE_A, iface);
492 cache_dump_recursive(b, (const char*)&dsd[1], TYPE_AAAA, iface);
493 }
494 }
495 }