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