7c7a8fba52858cc573100f9d1ff8f6713722cbce
[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 <arpa/nameser.h>
28 #include <resolv.h>
29 #include <time.h>
30
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/kvlist.h>
36 #include <libubus.h>
37
38 #include "cache.h"
39 #include "util.h"
40 #include "dns.h"
41 #include "interface.h"
42
43 static struct uloop_timeout cache_gc;
44 struct avl_tree services;
45 static AVL_TREE(records, avl_strcmp, true, NULL);
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_service_free(struct cache_service *s)
57 {
58 DBG(2, "%s\n", s->entry);
59 avl_delete(&services, &s->avl);
60 free(s);
61 }
62
63 static int
64 cache_is_expired(time_t t, uint32_t ttl, int frac)
65 {
66 if (monotonic_time() - t >= ttl * frac / 100)
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_service *s, *t;
77
78 avl_for_each_element_safe(&records, r, avl, p)
79 if (cache_is_expired(r->time, r->ttl, 100))
80 cache_record_free(r);
81
82 avl_for_each_element_safe(&services, s, avl, t) {
83 if (!s->host)
84 continue;
85 if (!cache_is_expired(s->time, s->ttl, s->refresh))
86 continue;
87 if (s->refresh >= 100) {
88 cache_service_free(s);
89 continue;
90 }
91 s->refresh += 50;
92 dns_send_question(s->iface, s->entry, TYPE_PTR, 1);
93 }
94
95 uloop_timeout_set(timeout, 10000);
96 }
97
98 int
99 cache_init(void)
100 {
101 avl_init(&services, avl_strcmp, true, NULL);
102
103 cache_gc.cb = cache_gc_timer;
104 uloop_timeout_set(&cache_gc, 10000);
105
106 return 0;
107 }
108
109 void cache_cleanup(struct interface *iface)
110 {
111 struct cache_record *r, *p;
112 struct cache_service *s, *t;
113
114 avl_for_each_element_safe(&services, s, avl, t)
115 if (!iface || iface == s->iface)
116 cache_service_free(s);
117
118 avl_for_each_element_safe(&records, r, avl, p)
119 if (!iface || iface == r->iface)
120 cache_record_free(r);
121 }
122
123 void
124 cache_scan(void)
125 {
126 struct interface *iface;
127 struct cache_service *s;
128
129 vlist_for_each_element(&interfaces, iface, node)
130 avl_for_each_element(&services, s, avl)
131 dns_send_question(iface, s->entry, TYPE_PTR, 1);
132 }
133
134 static struct cache_service*
135 cache_service(struct interface *iface, char *entry, int hlen, int ttl)
136 {
137 struct cache_service *s, *t;
138 char *entry_buf;
139 char *host_buf;
140 char *type;
141
142 avl_for_each_element_safe(&services, s, avl, t)
143 if (!strcmp(s->entry, entry)) {
144 s->refresh = 50;
145 s->time = monotonic_time();
146 return s;
147 }
148
149 s = calloc_a(sizeof(*s),
150 &entry_buf, strlen(entry) + 1,
151 &host_buf, hlen ? hlen + 1 : 0);
152
153 s->avl.key = s->entry = strcpy(entry_buf, entry);
154 s->time = monotonic_time();
155 s->ttl = ttl;
156 s->iface = iface;
157 s->refresh = 50;
158
159 if (hlen)
160 s->host = strncpy(host_buf, s->entry, hlen);
161
162 type = strstr(entry_buf, "._");
163 if (type)
164 type++;
165 if (type)
166 s->avl.key = type;
167 avl_insert(&services, &s->avl);
168
169 if (!hlen)
170 dns_send_question(iface, entry, TYPE_PTR, !iface->multicast);
171
172 return s;
173 }
174
175 static struct cache_record*
176 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
177 {
178 struct cache_record *l = avl_find_element(&records, record, l, avl);
179
180 if (!l)
181 return NULL;
182
183 while (l && l->record && !strcmp(l->record, record)) {
184 struct cache_record *r = l;
185
186 l = avl_next_element(l, avl);
187 if (r->type != type)
188 continue;
189
190 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
191 return r;
192
193 if (r->port != port)
194 continue;
195
196 if (r->rdlength != rdlength)
197 continue;
198
199 if (!!r->rdata != !!rdata)
200 continue;
201
202 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
203 continue;
204
205 return r;
206 }
207
208 return NULL;
209 }
210
211 int
212 cache_host_is_known(char *record)
213 {
214 struct cache_record *l = avl_find_element(&records, record, l, avl);
215
216 if (!l)
217 return 0;
218
219 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
220 struct cache_record *r = l;
221
222 l = avl_next_element(l, avl);
223 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
224 continue;
225 return 1;
226 }
227
228 return 0;
229 }
230
231 void
232 cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata, int flush)
233 {
234 struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
235 struct cache_record *r;
236 int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
237 char *p = NULL;
238 char *name_buf;
239 void *rdata_ptr, *txt_ptr;
240 int host_len = 0;
241 static char *rdata_buffer = (char *) mdns_buf;
242
243 nlen = strlen(name);
244
245 switch (a->type) {
246 case TYPE_PTR:
247 if (a->rdlength < 2)
248 return;
249
250 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
251 perror("process_answer/dn_expand");
252 return;
253 }
254
255 DBG(1, "A -> %s %s %s ttl:%d\n", dns_type_string(a->type), name, rdata_buffer, a->ttl);
256
257 rdlength = strlen(rdata_buffer);
258
259 if (strcmp(C_DNS_SD, name) != 0 &&
260 nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
261 host_len = rdlength - nlen - 1;
262
263 cache_service(iface, rdata_buffer, host_len, a->ttl);
264 return;
265
266 case TYPE_SRV:
267 if (a->rdlength < 8)
268 return;
269
270 port = be16_to_cpu(dsd->port);
271 memcpy(rdata_buffer, dsd, sizeof(*dsd));
272 if (dn_expand(base, base + blen, (const uint8_t*)&dsd[1],
273 &rdata_buffer[sizeof(*dsd)], MAX_DATA_LEN - sizeof(*dsd)) < 0) {
274 perror("process_answer/dn_expand");
275 return;
276 }
277 dlen = sizeof(*dsd) + strlen(&rdata_buffer[sizeof(*dsd)]) + 1;
278 rdata = (uint8_t*)rdata_buffer;
279 break;
280
281 case TYPE_TXT:
282 rdlength = a->rdlength;
283 if (rdlength <= 2)
284 return;
285
286 memcpy(rdata_buffer, &rdata[1], rdlength);
287 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
288 tlen = rdlength + 1;
289 p = &rdata_buffer[*rdata];
290
291 do {
292 uint8_t v = *p;
293
294 *p = '\0';
295 if (v && p + v < &rdata_buffer[rdlength])
296 p += v + 1;
297 } while (*p);
298 break;
299
300 case TYPE_A:
301 cache_service(iface, name, strlen(name), a->ttl);
302 if (a->rdlength != 4)
303 return;
304 dlen = 4;
305 break;
306
307 case TYPE_AAAA:
308 cache_service(iface, name, strlen(name), a->ttl);
309 if (a->rdlength != 16)
310 return;
311 dlen = 16;
312 break;
313
314 default:
315 return;
316 }
317
318 r = cache_record_find(name, a->type, port, dlen, rdata);
319 if (r) {
320 if (!a->ttl) {
321 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
322 r->time = monotonic_time() + 1 - r->ttl;
323 } else {
324 r->ttl = a->ttl;
325 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
326 }
327 return;
328 }
329
330 if (!a->ttl)
331 return;
332
333 r = calloc_a(sizeof(*r),
334 &name_buf, strlen(name) + 1,
335 &txt_ptr, tlen,
336 &rdata_ptr, dlen);
337
338 r->avl.key = r->record = strcpy(name_buf, name);
339 r->type = a->type;
340 r->ttl = a->ttl;
341 r->port = port;
342 r->rdlength = dlen;
343 r->time = monotonic_time();
344 r->iface = iface;
345
346 if (tlen)
347 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
348
349 if (dlen)
350 r->rdata = memcpy(rdata_ptr, rdata, dlen);
351
352 if (avl_insert(&records, &r->avl))
353 free(r);
354 else
355 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
356 }
357
358 void
359 cache_dump_records(struct blob_buf *buf, const char *name)
360 {
361 struct cache_record *r, *last, *next;
362 const char *txt;
363 char buffer[INET6_ADDRSTRLEN];
364
365 last = avl_last_element(&records, last, avl);
366 for (r = avl_find_element(&records, name, r, avl); r; r = next) {
367 switch (r->type) {
368 case TYPE_TXT:
369 if (r->txt && strlen(r->txt)) {
370 txt = r->txt;
371 do {
372 blobmsg_add_string(buf, "txt", txt);
373 txt = &txt[strlen(txt) + 1];
374 } while (*txt);
375 }
376 break;
377
378 case TYPE_SRV:
379 if (r->port)
380 blobmsg_add_u32(buf, "port", r->port);
381 break;
382
383 case TYPE_A:
384 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
385 blobmsg_add_string(buf, "ipv4", buffer);
386 break;
387
388 case TYPE_AAAA:
389 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
390 blobmsg_add_string(buf, "ipv6", buffer);
391 break;
392 }
393
394 if (r == last)
395 break;
396
397 next = avl_next_element(r, avl);
398 if (strcmp(r->record, next->record) != 0)
399 break;
400 }
401 }