be26fb6e1b4494d52f7d453bc01d27993fefde78
[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         int host_len = 0;
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) != 0 &&
273                     nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
274                         host_len = rdlength - nlen - 1;
275
276                 cache_entry(u, rdata_buffer, host_len, a->ttl);
277                 return;
278
279         case TYPE_SRV:
280                 if (a->rdlength < 8)
281                         return;
282
283                 port = be16_to_cpu(dsd->port);
284                 break;
285
286         case TYPE_TXT:
287                 rdlength = a->rdlength;
288                 if (rdlength <= 2)
289                         return;
290
291                 memcpy(rdata_buffer, &rdata[1], rdlength);
292                 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
293                 tlen = rdlength + 1;
294                 p = &rdata_buffer[*rdata];
295
296                 do {
297                         uint8_t v = *p;
298
299                         *p = '\0';
300                         if (v)
301                                 p += v + 1;
302                 } while (*p);
303                 break;
304
305         case TYPE_A:
306                 cache_entry(u, name, strlen(name), a->ttl);
307                 if (a->rdlength != 4)
308                         return;
309                 dlen = 4;
310                 break;
311
312         case TYPE_AAAA:
313                 cache_entry(u, name, strlen(name), a->ttl);
314                 if (a->rdlength != 16)
315                         return;
316                 dlen = 16;
317                 break;
318
319         default:
320                 return;
321         }
322
323         r = cache_record_find(name, a->type, port, dlen, rdata);
324         if (r) {
325                 if (!a->ttl) {
326                         cache_record_free(r);
327                         DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
328                 } else {
329                         r->ttl = a->ttl;
330                         DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
331                 }
332                 return;
333         }
334
335         if (!a->ttl)
336                 return;
337
338         r = calloc_a(sizeof(*r),
339                 &name_buf, strlen(name) + 1,
340                 &txt_ptr, tlen,
341                 &rdata_ptr, dlen);
342
343         r->avl.key = r->record = strcpy(name_buf, name);
344         r->type = a->type;
345         r->ttl = a->ttl;
346         r->port = port;
347         r->rdlength = dlen;
348         r->time = time(NULL);
349
350         if (tlen)
351                 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
352
353         if (dlen)
354                 r->rdata = memcpy(rdata_ptr, rdata, dlen);
355
356         if (avl_insert(&records, &r->avl))
357                 free(r);
358         else
359                 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
360 }