cache: use proper avl lookup for entries
[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_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
163 {
164 struct cache_entry *s;
165 char *type;
166
167 s = avl_find_element(&entries, entry, s, avl);
168 if (s)
169 return s;
170
171 s = malloc(sizeof(struct cache_entry));
172 memset(s, 0, sizeof(struct cache_entry));
173 s->avl.key = s->entry = strdup(entry);
174 s->time = time(NULL);
175 s->ttl = ttl;
176
177 if (hlen)
178 s->host = strndup(s->entry, hlen);
179 type = strstr(s->entry, "._");
180 if (type)
181 type++;
182 if (type)
183 s->avl.key = type;
184 avl_insert(&entries, &s->avl);
185
186 if (!hlen)
187 dns_send_question(u, entry, TYPE_PTR);
188
189 return s;
190 }
191
192 static struct cache_record*
193 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
194 {
195 struct cache_record *l = avl_find_element(&records, record, l, avl);
196
197 if (!l)
198 return NULL;
199
200 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
201 struct cache_record *r = l;
202
203 l = avl_next_element(l, avl);
204 if (r->type != type)
205 continue;
206
207 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
208 return r;
209
210 if (r->port != port)
211 continue;
212
213 if (r->rdlength != rdlength)
214 continue;
215
216 if (!!r->rdata != !!rdata)
217 continue;
218
219 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
220 continue;
221
222 return r;
223 }
224
225 return NULL;
226 }
227
228 int
229 cache_host_is_known(char *record)
230 {
231 struct cache_record *l = avl_find_element(&records, record, l, avl);
232
233 if (!l)
234 return 0;
235
236 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
237 struct cache_record *r = l;
238
239 l = avl_next_element(l, avl);
240 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
241 continue;
242 return 1;
243 }
244
245 return 0;
246 }
247
248 void
249 cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
250 {
251 struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
252 struct cache_record *r;
253 int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
254 char *p = NULL;
255
256 if (!(a->class & CLASS_IN))
257 return;
258
259 nlen = strlen(name);
260
261 switch (a->type) {
262 case TYPE_PTR:
263 if (a->rdlength < 2)
264 return;
265
266 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
267 perror("process_answer/dn_expand");
268 return;
269 }
270
271 DBG(1, "A -> %s %s %s\n", dns_type_string(a->type), name, rdata_buffer);
272
273 rdlength = strlen(rdata_buffer);
274
275 if (!strcmp(C_DNS_SD, name)) {
276 cache_entry(u, rdata_buffer, 0, a->ttl);
277 return;
278 }
279
280 if ((rdlength < nlen) && (rdlength - nlen - 1 > 0))
281 return;
282
283 cache_entry(u, rdata_buffer, rdlength - nlen - 1, a->ttl);
284 return;
285
286 case TYPE_SRV:
287 if (a->rdlength < 8)
288 return;
289
290 port = be16_to_cpu(dsd->port);
291 break;
292
293 case TYPE_TXT:
294 rdlength = a->rdlength;
295 if (rdlength <= 2)
296 return;
297
298 memcpy(rdata_buffer, &rdata[1], rdlength);
299 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
300 tlen = rdlength + 1;
301 p = &rdata_buffer[*rdata];
302
303 do {
304 uint8_t v = *p;
305
306 *p = '\0';
307 if (v)
308 p += v + 1;
309 } while (*p);
310 break;
311
312 case TYPE_A:
313 cache_entry(u, name, strlen(name), a->ttl);
314 if (a->rdlength != 4)
315 return;
316 dlen = 4;
317 break;
318
319 case TYPE_AAAA:
320 cache_entry(u, name, strlen(name), a->ttl);
321 if (a->rdlength != 16)
322 return;
323 dlen = 16;
324 break;
325
326 default:
327 return;
328 }
329
330 r = cache_record_find(name, a->type, port, dlen, rdata);
331 if (r) {
332 if (!a->ttl) {
333 cache_record_free(r, 1);
334 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
335 } else {
336 r->ttl = a->ttl;
337 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
338 }
339 return;
340 }
341
342 if (!a->ttl)
343 return;
344
345 r = malloc(sizeof(struct cache_record));
346 memset(r, 0, sizeof(struct cache_record));
347 r->avl.key = r->record = strdup(name);
348 r->type = a->type;
349 r->ttl = a->ttl;
350 r->port = port;
351 r->rdlength = dlen;
352 r->time = time(NULL);
353
354 if (tlen) {
355 r->txt = malloc(tlen);
356 if (r->txt)
357 memcpy(r->txt, rdata_buffer, tlen);
358 }
359
360 if (dlen) {
361 r->rdata = malloc(dlen);
362 if (!r->rdata) {
363 cache_record_free(r, 0);
364 return;
365 }
366 memcpy(r->rdata, rdata, dlen);
367 }
368
369 if (avl_insert(&records, &r->avl))
370 cache_record_free(r, 0);
371 else
372 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
373 }