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