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