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