entry->service
[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)
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_service *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(&services, s, avl, t) {
83 if (!s->host)
84 continue;
85 if (cache_is_expired(s->time, s->ttl))
86 cache_service_free(s);
87 }
88
89 uloop_timeout_set(timeout, 10000);
90 }
91
92 int
93 cache_init(void)
94 {
95 avl_init(&services, 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_service *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(&services, s, avl, t)
112 cache_service_free(s);
113 }
114
115 void
116 cache_scan(void)
117 {
118 struct interface *iface;
119 struct cache_service *s;
120
121 vlist_for_each_element(&interfaces, iface, node)
122 avl_for_each_element(&services, s, avl)
123 dns_send_question(iface, s->entry, TYPE_PTR, 1);
124 }
125
126 static struct cache_service*
127 cache_service(struct interface *iface, char *entry, int hlen, int ttl)
128 {
129 struct cache_service *s, *t;
130 char *entry_buf;
131 char *host_buf;
132 char *type;
133
134 avl_for_each_element_safe(&services, s, avl, t)
135 if (!strcmp(s->entry, entry))
136 return s;
137
138 s = calloc_a(sizeof(*s),
139 &entry_buf, strlen(entry) + 1,
140 &host_buf, hlen ? hlen + 1 : 0);
141
142 s->avl.key = s->entry = strcpy(entry_buf, entry);
143 s->time = time(NULL);
144 s->ttl = ttl;
145
146 if (hlen)
147 s->host = strncpy(host_buf, s->entry, hlen);
148
149 type = strstr(entry_buf, "._");
150 if (type)
151 type++;
152 if (type)
153 s->avl.key = type;
154 avl_insert(&services, &s->avl);
155
156 if (!hlen)
157 dns_send_question(iface, entry, TYPE_PTR, !iface->multicast);
158
159 return s;
160 }
161
162 static struct cache_record*
163 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
164 {
165 struct cache_record *l = avl_find_element(&records, record, l, avl);
166
167 if (!l)
168 return NULL;
169
170 while (l && l->record && !strcmp(l->record, record)) {
171 struct cache_record *r = l;
172
173 l = avl_next_element(l, avl);
174 if (r->type != type)
175 continue;
176
177 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
178 return r;
179
180 if (r->port != port)
181 continue;
182
183 if (r->rdlength != rdlength)
184 continue;
185
186 if (!!r->rdata != !!rdata)
187 continue;
188
189 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
190 continue;
191
192 return r;
193 }
194
195 return NULL;
196 }
197
198 int
199 cache_host_is_known(char *record)
200 {
201 struct cache_record *l = avl_find_element(&records, record, l, avl);
202
203 if (!l)
204 return 0;
205
206 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
207 struct cache_record *r = l;
208
209 l = avl_next_element(l, avl);
210 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
211 continue;
212 return 1;
213 }
214
215 return 0;
216 }
217
218 void
219 cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata, int flush)
220 {
221 struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
222 struct cache_record *r;
223 int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
224 char *p = NULL;
225 char *name_buf;
226 void *rdata_ptr, *txt_ptr;
227 int host_len = 0;
228 static char *rdata_buffer = (char *) mdns_buf;
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 ttl:%d\n", dns_type_string(a->type), name, rdata_buffer, a->ttl);
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_service(iface, 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 && p + v < &rdata_buffer[rdlength])
275 p += v + 1;
276 } while (*p);
277 break;
278
279 case TYPE_A:
280 cache_service(iface, name, strlen(name), a->ttl);
281 if (a->rdlength != 4)
282 return;
283 dlen = 4;
284 break;
285
286 case TYPE_AAAA:
287 cache_service(iface, 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 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
301 r->time = time(0) + 1 - 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 }
335
336 void
337 cache_dump_records(struct blob_buf *buf, const char *name)
338 {
339 struct cache_record *r, *last, *next;
340 const char *txt;
341 char buffer[INET6_ADDRSTRLEN];
342
343 last = avl_last_element(&records, last, avl);
344 for (r = avl_find_element(&records, name, r, avl); r; r = next) {
345 switch (r->type) {
346 case TYPE_TXT:
347 if (r->txt && strlen(r->txt)) {
348 txt = r->txt;
349 do {
350 blobmsg_add_string(buf, "txt", txt);
351 txt = &txt[strlen(txt) + 1];
352 } while (*txt);
353 }
354 break;
355
356 case TYPE_SRV:
357 if (r->port)
358 blobmsg_add_u32(buf, "port", r->port);
359 break;
360
361 case TYPE_A:
362 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
363 blobmsg_add_string(buf, "ipv4", buffer);
364 break;
365
366 case TYPE_AAAA:
367 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
368 blobmsg_add_string(buf, "ipv6", buffer);
369 break;
370 }
371
372 if (r == last)
373 break;
374
375 next = avl_next_element(r, avl);
376 if (strcmp(r->record, next->record) != 0)
377 break;
378 }
379 }