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