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