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