e4479226609b0b2ac69b92ec0f3755af03270ef1
[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)
65 {
66         if (time(NULL) - t >= ttl)
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))
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))
86                         cache_service_free(s);
87         }
88
89         uloop_timeout_set(timeout, 10000);
90 }
91
92 int
93 cache_init(void)
94 {
95         avl_init(&services, avl_strcmp, true, NULL);
96
97         cache_gc.cb = cache_gc_timer;
98         uloop_timeout_set(&cache_gc, 10000);
99
100         return 0;
101 }
102
103 void cache_cleanup(struct interface *iface)
104 {
105         struct cache_record *r, *p;
106         struct cache_service *s, *t;
107
108         avl_for_each_element_safe(&services, s, avl, t)
109                 if (!iface || iface == s->iface)
110                         cache_service_free(s);
111
112         avl_for_each_element_safe(&records, r, avl, p)
113                 if (!iface || iface == r->iface)
114                         cache_record_free(r);
115 }
116
117 void
118 cache_scan(void)
119 {
120         struct interface *iface;
121         struct cache_service *s;
122
123         vlist_for_each_element(&interfaces, iface, node)
124                 avl_for_each_element(&services, s, avl)
125                         dns_send_question(iface, s->entry, TYPE_PTR, 1);
126 }
127
128 static struct cache_service*
129 cache_service(struct interface *iface, char *entry, int hlen, int ttl)
130 {
131         struct cache_service *s, *t;
132         char *entry_buf;
133         char *host_buf;
134         char *type;
135
136         avl_for_each_element_safe(&services, s, avl, t)
137                 if (!strcmp(s->entry, entry))
138                         return s;
139
140         s = calloc_a(sizeof(*s),
141                 &entry_buf, strlen(entry) + 1,
142                 &host_buf, hlen ? hlen + 1 : 0);
143
144         s->avl.key = s->entry = strcpy(entry_buf, entry);
145         s->time = time(NULL);
146         s->ttl = ttl;
147         s->iface = iface;
148
149         if (hlen)
150                 s->host = strncpy(host_buf, s->entry, hlen);
151
152         type = strstr(entry_buf, "._");
153         if (type)
154                 type++;
155         if (type)
156                 s->avl.key = type;
157         avl_insert(&services, &s->avl);
158
159         if (!hlen)
160                 dns_send_question(iface, entry, TYPE_PTR, !iface->multicast);
161
162         return s;
163 }
164
165 static struct cache_record*
166 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
167 {
168         struct cache_record *l = avl_find_element(&records, record, l, avl);
169
170         if (!l)
171                 return NULL;
172
173         while (l && l->record && !strcmp(l->record, record)) {
174                 struct cache_record *r = l;
175
176                 l = avl_next_element(l, avl);
177                 if (r->type != type)
178                         continue;
179
180                 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
181                         return r;
182
183                 if (r->port != port)
184                         continue;
185
186                 if (r->rdlength != rdlength)
187                         continue;
188
189                 if (!!r->rdata != !!rdata)
190                         continue;
191
192                 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
193                         continue;
194
195                 return r;
196         }
197
198         return NULL;
199 }
200
201 int
202 cache_host_is_known(char *record)
203 {
204         struct cache_record *l = avl_find_element(&records, record, l, avl);
205
206         if (!l)
207                 return 0;
208
209         while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
210                 struct cache_record *r = l;
211
212                 l = avl_next_element(l, avl);
213                 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
214                         continue;
215                 return 1;
216         }
217
218         return 0;
219 }
220
221 void
222 cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata, int flush)
223 {
224         struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
225         struct cache_record *r;
226         int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
227         char *p = NULL;
228         char *name_buf;
229         void *rdata_ptr, *txt_ptr;
230         int host_len = 0;
231         static char *rdata_buffer = (char *) mdns_buf;
232
233         nlen = strlen(name);
234
235         switch (a->type) {
236         case TYPE_PTR:
237                 if (a->rdlength < 2)
238                         return;
239
240                 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
241                         perror("process_answer/dn_expand");
242                         return;
243                 }
244
245                 DBG(1, "A -> %s %s %s ttl:%d\n", dns_type_string(a->type), name, rdata_buffer, a->ttl);
246
247                 rdlength = strlen(rdata_buffer);
248
249                 if (strcmp(C_DNS_SD, name) != 0 &&
250                     nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
251                         host_len = rdlength - nlen - 1;
252
253                 cache_service(iface, rdata_buffer, host_len, a->ttl);
254                 return;
255
256         case TYPE_SRV:
257                 if (a->rdlength < 8)
258                         return;
259
260                 port = be16_to_cpu(dsd->port);
261                 break;
262
263         case TYPE_TXT:
264                 rdlength = a->rdlength;
265                 if (rdlength <= 2)
266                         return;
267
268                 memcpy(rdata_buffer, &rdata[1], rdlength);
269                 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
270                 tlen = rdlength + 1;
271                 p = &rdata_buffer[*rdata];
272
273                 do {
274                         uint8_t v = *p;
275
276                         *p = '\0';
277                         if (v && p + v < &rdata_buffer[rdlength])
278                                 p += v + 1;
279                 } while (*p);
280                 break;
281
282         case TYPE_A:
283                 cache_service(iface, name, strlen(name), a->ttl);
284                 if (a->rdlength != 4)
285                         return;
286                 dlen = 4;
287                 break;
288
289         case TYPE_AAAA:
290                 cache_service(iface, name, strlen(name), a->ttl);
291                 if (a->rdlength != 16)
292                         return;
293                 dlen = 16;
294                 break;
295
296         default:
297                 return;
298         }
299
300         r = cache_record_find(name, a->type, port, dlen, rdata);
301         if (r) {
302                 if (!a->ttl) {
303                         DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
304                         r->time = time(0) + 1 - r->ttl;
305                 } else {
306                         r->ttl = a->ttl;
307                         DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
308                 }
309                 return;
310         }
311
312         if (!a->ttl)
313                 return;
314
315         r = calloc_a(sizeof(*r),
316                 &name_buf, strlen(name) + 1,
317                 &txt_ptr, tlen,
318                 &rdata_ptr, dlen);
319
320         r->avl.key = r->record = strcpy(name_buf, name);
321         r->type = a->type;
322         r->ttl = a->ttl;
323         r->port = port;
324         r->rdlength = dlen;
325         r->time = time(NULL);
326         r->iface = iface;
327
328         if (tlen)
329                 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
330
331         if (dlen)
332                 r->rdata = memcpy(rdata_ptr, rdata, dlen);
333
334         if (avl_insert(&records, &r->avl))
335                 free(r);
336         else
337                 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
338 }
339
340 void
341 cache_dump_records(struct blob_buf *buf, const char *name)
342 {
343         struct cache_record *r, *last, *next;
344         const char *txt;
345         char buffer[INET6_ADDRSTRLEN];
346
347         last = avl_last_element(&records, last, avl);
348         for (r = avl_find_element(&records, name, r, avl); r; r = next) {
349                 switch (r->type) {
350                 case TYPE_TXT:
351                         if (r->txt && strlen(r->txt)) {
352                                 txt = r->txt;
353                                 do {
354                                         blobmsg_add_string(buf, "txt", txt);
355                                         txt = &txt[strlen(txt) + 1];
356                                 } while (*txt);
357                         }
358                         break;
359
360                 case TYPE_SRV:
361                         if (r->port)
362                                 blobmsg_add_u32(buf, "port", r->port);
363                         break;
364
365                 case TYPE_A:
366                         if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
367                                 blobmsg_add_string(buf, "ipv4", buffer);
368                         break;
369
370                 case TYPE_AAAA:
371                         if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
372                                 blobmsg_add_string(buf, "ipv6", buffer);
373                         break;
374                 }
375
376                 if (r == last)
377                         break;
378
379                 next = avl_next_element(r, avl);
380                 if (strcmp(r->record, next->record) != 0)
381                         break;
382         }
383 }