cache: reduce allocation of cache records to one chunk of memory, using calloc_a
[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
43 static struct uloop_timeout cache_gc;
44 struct avl_tree records, entries, hosts;
45 static struct blob_buf b;
46
47 static struct kvlist types;
48
49 static void
50 cache_record_free(struct cache_record *r, int rem)
51 {
52         DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
53         if (rem)
54                 avl_delete(&records, &r->avl);
55         free(r);
56 }
57
58 static void
59 cache_entry_free(struct cache_entry *s)
60 {
61         DBG(2, "%s\n", s->entry);
62         avl_delete(&entries, &s->avl);
63         free(s);
64 }
65
66 static int
67 cache_is_expired(time_t t, uint32_t ttl)
68 {
69         if (time(NULL) - t >= ttl)
70                 return 1;
71
72         return 0;
73 }
74
75 static void
76 cache_gc_timer(struct uloop_timeout *timeout)
77 {
78         struct cache_record *r, *p;
79         struct cache_entry *s, *t;
80
81         avl_for_each_element_safe(&records, r, avl, p)
82                 if (cache_is_expired(r->time, r->ttl))
83                         cache_record_free(r, 1);
84
85         avl_for_each_element_safe(&entries, s, avl, t) {
86                 if (!s->host)
87                         continue;
88                 if (cache_is_expired(s->time, s->ttl))
89                         cache_entry_free(s);
90         }
91
92         uloop_timeout_set(timeout, 10000);
93 }
94
95 static void
96 cache_load_services(void)
97 {
98         struct blob_attr *cur;
99         int rem;
100
101         blob_buf_init(&b, 0);
102
103         if (!blobmsg_add_json_from_file(&b, "/lib/mdns/service-types"))
104                 return;
105
106         blob_for_each_attr(cur, b.head, rem)
107                 kvlist_set(&types, blobmsg_name(cur), blobmsg_get_string(cur));
108 }
109
110 char*
111 cache_lookup_name(const char *key)
112 {
113         return kvlist_get(&types, key);
114 }
115
116 int
117 cache_init(void)
118 {
119         kvlist_init(&types, kvlist_strlen);
120         avl_init(&entries, avl_strcmp, true, NULL);
121         avl_init(&records, avl_strcmp, true, NULL);
122
123         cache_gc.cb = cache_gc_timer;
124         uloop_timeout_set(&cache_gc, 10000);
125         cache_load_services();
126
127         return 0;
128 }
129
130 void cache_cleanup(void)
131 {
132         struct cache_record *r, *p;
133         struct cache_entry *s, *t;
134
135         avl_for_each_element_safe(&records, r, avl, p)
136                 cache_record_free(r, 1);
137
138         avl_for_each_element_safe(&entries, s, avl, t)
139                 cache_entry_free(s);
140 }
141
142 void
143 cache_scan(void)
144 {
145         struct cache_entry *s;
146
147         avl_for_each_element(&entries, s, avl)
148                 dns_send_question(&listener, s->entry, TYPE_PTR);
149 }
150
151 static struct cache_entry*
152 cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
153 {
154         struct cache_entry *s;
155         char *entry_buf;
156         char *host_buf;
157         char *type;
158
159         s = avl_find_element(&entries, entry, s, avl);
160         if (s)
161                 return s;
162
163         s = calloc_a(sizeof(*s),
164                 &entry_buf, strlen(entry) + 1,
165                 &host_buf, hlen ? hlen + 1 : 0);
166
167         s->avl.key = s->entry = strcpy(entry_buf, entry);
168         s->time = time(NULL);
169         s->ttl = ttl;
170
171         if (hlen)
172                 s->host = strncpy(host_buf, s->entry, hlen);
173
174         type = strstr(entry_buf, "._");
175         if (type)
176                 type++;
177         if (type)
178                 s->avl.key = type;
179         avl_insert(&entries, &s->avl);
180
181         if (!hlen)
182                 dns_send_question(u, entry, TYPE_PTR);
183
184         return s;
185 }
186
187 static struct cache_record*
188 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
189 {
190         struct cache_record *l = avl_find_element(&records, record, l, avl);
191
192         if (!l)
193                 return NULL;
194
195         while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
196                 struct cache_record *r = l;
197
198                 l = avl_next_element(l, avl);
199                 if (r->type != type)
200                         continue;
201
202                 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
203                         return r;
204
205                 if (r->port != port)
206                         continue;
207
208                 if (r->rdlength != rdlength)
209                         continue;
210
211                 if (!!r->rdata != !!rdata)
212                         continue;
213
214                 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
215                         continue;
216
217                 return r;
218         }
219
220         return NULL;
221 }
222
223 int
224 cache_host_is_known(char *record)
225 {
226         struct cache_record *l = avl_find_element(&records, record, l, avl);
227
228         if (!l)
229                 return 0;
230
231         while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
232                 struct cache_record *r = l;
233
234                 l = avl_next_element(l, avl);
235                 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
236                         continue;
237                 return 1;
238         }
239
240         return 0;
241 }
242
243 void
244 cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
245 {
246         struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
247         struct cache_record *r;
248         int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
249         char *p = NULL;
250         char *name_buf;
251         void *rdata_ptr, *txt_ptr;
252
253         if (!(a->class & CLASS_IN))
254                 return;
255
256         nlen = strlen(name);
257
258         switch (a->type) {
259         case TYPE_PTR:
260                 if (a->rdlength < 2)
261                         return;
262
263                 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
264                         perror("process_answer/dn_expand");
265                         return;
266                 }
267
268                 DBG(1, "A -> %s %s %s\n", dns_type_string(a->type), name, rdata_buffer);
269
270                 rdlength = strlen(rdata_buffer);
271
272                 if (!strcmp(C_DNS_SD, name)) {
273                         cache_entry(u, rdata_buffer, 0, a->ttl);
274                         return;
275                 }
276
277                 if ((rdlength < nlen) && (rdlength - nlen - 1 > 0))
278                         return;
279
280                 cache_entry(u, rdata_buffer, rdlength - nlen - 1, a->ttl);
281                 return;
282
283         case TYPE_SRV:
284                 if (a->rdlength < 8)
285                         return;
286
287                 port = be16_to_cpu(dsd->port);
288                 break;
289
290         case TYPE_TXT:
291                 rdlength = a->rdlength;
292                 if (rdlength <= 2)
293                         return;
294
295                 memcpy(rdata_buffer, &rdata[1], rdlength);
296                 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
297                 tlen = rdlength + 1;
298                 p = &rdata_buffer[*rdata];
299
300                 do {
301                         uint8_t v = *p;
302
303                         *p = '\0';
304                         if (v)
305                                 p += v + 1;
306                 } while (*p);
307                 break;
308
309         case TYPE_A:
310                 cache_entry(u, name, strlen(name), a->ttl);
311                 if (a->rdlength != 4)
312                         return;
313                 dlen = 4;
314                 break;
315
316         case TYPE_AAAA:
317                 cache_entry(u, name, strlen(name), a->ttl);
318                 if (a->rdlength != 16)
319                         return;
320                 dlen = 16;
321                 break;
322
323         default:
324                 return;
325         }
326
327         r = cache_record_find(name, a->type, port, dlen, rdata);
328         if (r) {
329                 if (!a->ttl) {
330                         cache_record_free(r, 1);
331                         DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
332                 } else {
333                         r->ttl = a->ttl;
334                         DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
335                 }
336                 return;
337         }
338
339         if (!a->ttl)
340                 return;
341
342         r = calloc_a(sizeof(*r),
343                 &name_buf, strlen(name) + 1,
344                 &txt_ptr, tlen,
345                 &rdata_ptr, dlen);
346
347         r->avl.key = r->record = strcpy(name_buf, name);
348         r->type = a->type;
349         r->ttl = a->ttl;
350         r->port = port;
351         r->rdlength = dlen;
352         r->time = time(NULL);
353
354         if (tlen)
355                 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
356
357         if (dlen)
358                 r->rdata = memcpy(rdata_ptr, rdata, dlen);
359
360         if (avl_insert(&records, &r->avl))
361                 free(r);
362         else
363                 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
364 }