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