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