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