simplify memory management for dns answers: use a blob_buf to cache entries
[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 <asm/byteorder.h>
28 #include <arpa/nameser.h>
29 #include <resolv.h>
30 #include <time.h>
31
32 #include <libubox/usock.h>
33 #include <libubox/uloop.h>
34 #include <libubox/avl-cmp.h>
35 #include <libubox/blobmsg_json.h>
36 #include <libubox/kvlist.h>
37 #include <libubus.h>
38
39 #include "cache.h"
40 #include "util.h"
41 #include "dns.h"
42 #include "interface.h"
43
44 static struct uloop_timeout cache_gc;
45 struct avl_tree entries;
46 static AVL_TREE(records, avl_strcmp, true, NULL);
47
48 static void
49 cache_record_free(struct cache_record *r)
50 {
51         DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
52         avl_delete(&records, &r->avl);
53         free(r);
54 }
55
56 static void
57 cache_entry_free(struct cache_entry *s)
58 {
59         DBG(2, "%s\n", s->entry);
60         avl_delete(&entries, &s->avl);
61         free(s);
62 }
63
64 static int
65 cache_is_expired(time_t t, uint32_t ttl)
66 {
67         if (time(NULL) - t >= ttl)
68                 return 1;
69
70         return 0;
71 }
72
73 static void
74 cache_gc_timer(struct uloop_timeout *timeout)
75 {
76         struct cache_record *r, *p;
77         struct cache_entry *s, *t;
78
79         avl_for_each_element_safe(&records, r, avl, p)
80                 if (cache_is_expired(r->time, r->ttl))
81                         cache_record_free(r);
82
83         avl_for_each_element_safe(&entries, s, avl, t) {
84                 if (!s->host)
85                         continue;
86                 if (cache_is_expired(s->time, s->ttl))
87                         cache_entry_free(s);
88         }
89
90         uloop_timeout_set(timeout, 10000);
91 }
92
93 int
94 cache_init(void)
95 {
96         avl_init(&entries, avl_strcmp, true, NULL);
97
98         cache_gc.cb = cache_gc_timer;
99         uloop_timeout_set(&cache_gc, 10000);
100
101         return 0;
102 }
103
104 void cache_cleanup(void)
105 {
106         struct cache_record *r, *p;
107         struct cache_entry *s, *t;
108
109         avl_for_each_element_safe(&records, r, avl, p)
110                 cache_record_free(r);
111
112         avl_for_each_element_safe(&entries, s, avl, t)
113                 cache_entry_free(s);
114 }
115
116 void
117 cache_scan(void)
118 {
119         struct interface *iface;
120         struct cache_entry *s;
121
122         vlist_for_each_element(&interfaces, iface, node)
123                 avl_for_each_element(&entries, s, avl)
124                         dns_send_question(iface, s->entry, TYPE_PTR);
125 }
126
127 static struct cache_entry*
128 cache_entry(struct interface *iface, char *entry, int hlen, int ttl)
129 {
130         struct cache_entry *s;
131         char *entry_buf;
132         char *host_buf;
133         char *type;
134
135         s = avl_find_element(&entries, entry, s, avl);
136         if (s)
137                 return s;
138
139         s = calloc_a(sizeof(*s),
140                 &entry_buf, strlen(entry) + 1,
141                 &host_buf, hlen ? hlen + 1 : 0);
142
143         s->avl.key = s->entry = strcpy(entry_buf, entry);
144         s->time = time(NULL);
145         s->ttl = ttl;
146
147         if (hlen)
148                 s->host = strncpy(host_buf, s->entry, hlen);
149
150         type = strstr(entry_buf, "._");
151         if (type)
152                 type++;
153         if (type)
154                 s->avl.key = type;
155         avl_insert(&entries, &s->avl);
156
157         if (!hlen)
158                 dns_send_question(iface, entry, TYPE_PTR);
159
160         return s;
161 }
162
163 static struct cache_record*
164 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
165 {
166         struct cache_record *l = avl_find_element(&records, record, l, avl);
167
168         if (!l)
169                 return NULL;
170
171         while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
172                 struct cache_record *r = l;
173
174                 l = avl_next_element(l, avl);
175                 if (r->type != type)
176                         continue;
177
178                 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
179                         return r;
180
181                 if (r->port != port)
182                         continue;
183
184                 if (r->rdlength != rdlength)
185                         continue;
186
187                 if (!!r->rdata != !!rdata)
188                         continue;
189
190                 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
191                         continue;
192
193                 return r;
194         }
195
196         return NULL;
197 }
198
199 int
200 cache_host_is_known(char *record)
201 {
202         struct cache_record *l = avl_find_element(&records, record, l, avl);
203
204         if (!l)
205                 return 0;
206
207         while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
208                 struct cache_record *r = l;
209
210                 l = avl_next_element(l, avl);
211                 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
212                         continue;
213                 return 1;
214         }
215
216         return 0;
217 }
218
219 void
220 cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
221 {
222         struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
223         struct cache_record *r;
224         int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
225         char *p = NULL;
226         char *name_buf;
227         void *rdata_ptr, *txt_ptr;
228         int host_len = 0;
229         static char *rdata_buffer = (char *) mdns_buf;
230
231         if (!(a->class & CLASS_IN))
232                 return;
233
234         nlen = strlen(name);
235
236         switch (a->type) {
237         case TYPE_PTR:
238                 if (a->rdlength < 2)
239                         return;
240
241                 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
242                         perror("process_answer/dn_expand");
243                         return;
244                 }
245
246                 DBG(1, "A -> %s %s %s\n", dns_type_string(a->type), name, rdata_buffer);
247
248                 rdlength = strlen(rdata_buffer);
249
250                 if (strcmp(C_DNS_SD, name) != 0 &&
251                     nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
252                         host_len = rdlength - nlen - 1;
253
254                 cache_entry(iface, rdata_buffer, host_len, a->ttl);
255                 return;
256
257         case TYPE_SRV:
258                 if (a->rdlength < 8)
259                         return;
260
261                 port = be16_to_cpu(dsd->port);
262                 break;
263
264         case TYPE_TXT:
265                 rdlength = a->rdlength;
266                 if (rdlength <= 2)
267                         return;
268
269                 memcpy(rdata_buffer, &rdata[1], rdlength);
270                 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
271                 tlen = rdlength + 1;
272                 p = &rdata_buffer[*rdata];
273
274                 do {
275                         uint8_t v = *p;
276
277                         *p = '\0';
278                         if (v)
279                                 p += v + 1;
280                 } while (*p);
281                 break;
282
283         case TYPE_A:
284                 cache_entry(iface, name, strlen(name), a->ttl);
285                 if (a->rdlength != 4)
286                         return;
287                 dlen = 4;
288                 break;
289
290         case TYPE_AAAA:
291                 cache_entry(iface, name, strlen(name), a->ttl);
292                 if (a->rdlength != 16)
293                         return;
294                 dlen = 16;
295                 break;
296
297         default:
298                 return;
299         }
300
301         r = cache_record_find(name, a->type, port, dlen, rdata);
302         if (r) {
303                 if (!a->ttl) {
304                         cache_record_free(r);
305                         DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
306                 } else {
307                         r->ttl = a->ttl;
308                         DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
309                 }
310                 return;
311         }
312
313         if (!a->ttl)
314                 return;
315
316         r = calloc_a(sizeof(*r),
317                 &name_buf, strlen(name) + 1,
318                 &txt_ptr, tlen,
319                 &rdata_ptr, dlen);
320
321         r->avl.key = r->record = strcpy(name_buf, name);
322         r->type = a->type;
323         r->ttl = a->ttl;
324         r->port = port;
325         r->rdlength = dlen;
326         r->time = time(NULL);
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, *q = avl_find_element(&records, name, r, avl);
344         const char *txt;
345         char buffer[INET6_ADDRSTRLEN];
346
347         if (!q)
348                 return;
349
350         do {
351                 r = q;
352                 switch (r->type) {
353                 case TYPE_TXT:
354                         if (r->txt && strlen(r->txt)) {
355                                 txt = r->txt;
356                                 do {
357                                         blobmsg_add_string(buf, "txt", txt);
358                                         txt = &txt[strlen(txt) + 1];
359                                 } while (*txt);
360                         }
361                         break;
362
363                 case TYPE_SRV:
364                         if (r->port)
365                                 blobmsg_add_u32(buf, "port", r->port);
366                         break;
367
368                 case TYPE_A:
369                         if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
370                                 blobmsg_add_string(buf, "ipv4", buffer);
371                         break;
372
373                 case TYPE_AAAA:
374                         if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
375                                 blobmsg_add_string(buf, "ipv6", buffer);
376                         break;
377                 }
378                 q = avl_next_element(r, avl);
379         } while (q && !strcmp(r->record, q->record));
380 }