2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/types.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>
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>
41 #include "interface.h"
43 static struct uloop_timeout cache_gc;
44 struct avl_tree services;
45 AVL_TREE(records, avl_strcmp, true, NULL);
48 cache_record_free(struct cache_record *r)
50 DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
51 avl_delete(&records, &r->avl);
56 cache_service_free(struct cache_service *s)
58 DBG(2, "%s\n", s->entry);
59 avl_delete(&services, &s->avl);
64 cache_is_expired(time_t t, uint32_t ttl, int frac)
66 if (monotonic_time() - t >= ttl * frac / 100)
73 cache_gc_timer(struct uloop_timeout *timeout)
75 struct cache_record *r, *p;
76 struct cache_service *s, *t;
78 avl_for_each_element_safe(&records, r, avl, p) {
79 if (!cache_is_expired(r->time, r->ttl, r->refresh))
81 /* Records other than A(AAA) are handled as services */
82 if (r->type != TYPE_A && r->type != TYPE_AAAA) {
83 if (cache_is_expired(r->time, r->ttl, 100))
87 if (r->refresh >= 100) {
92 dns_send_question(r->iface, (struct sockaddr *)&r->from, r->record, r->type, 0);
95 avl_for_each_element_safe(&services, s, avl, t) {
98 if (!cache_is_expired(s->time, s->ttl, s->refresh))
100 if (s->refresh >= 100) {
101 cache_service_free(s);
105 dns_send_question(s->iface, NULL, s->entry, TYPE_PTR, 0);
108 uloop_timeout_set(timeout, 10000);
114 avl_init(&services, avl_strcmp, true, NULL);
116 cache_gc.cb = cache_gc_timer;
117 uloop_timeout_set(&cache_gc, 10000);
122 void cache_cleanup(struct interface *iface)
124 struct cache_record *r, *p;
125 struct cache_service *s, *t;
127 avl_for_each_element_safe(&services, s, avl, t)
128 if (!iface || iface == s->iface)
129 cache_service_free(s);
131 avl_for_each_element_safe(&records, r, avl, p)
132 if (!iface || iface == r->iface)
133 cache_record_free(r);
139 struct interface *iface;
140 struct cache_service *s;
142 vlist_for_each_element(&interfaces, iface, node)
143 avl_for_each_element(&services, s, avl)
144 dns_send_question(iface, NULL, s->entry, TYPE_PTR, 0);
147 static struct cache_service*
148 cache_service(struct interface *iface, char *entry, int hlen, int ttl)
150 struct cache_service *s, *t;
155 avl_for_each_element_safe(&services, s, avl, t)
156 if (!strcmp(s->entry, entry)) {
158 s->time = monotonic_time();
163 s = calloc_a(sizeof(*s),
164 &entry_buf, strlen(entry) + 1,
165 &host_buf, hlen ? hlen + 1 : 0);
167 s->avl.key = s->entry = strcpy(entry_buf, entry);
168 s->time = monotonic_time();
174 s->host = strncpy(host_buf, s->entry, hlen);
176 type = strstr(entry_buf, "._");
181 avl_insert(&services, &s->avl);
184 dns_send_question(iface, NULL, entry, TYPE_PTR, iface->multicast);
189 static struct cache_record*
190 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
192 struct cache_record *l = avl_find_element(&records, record, l, avl);
197 while (l && l->record && !strcmp(l->record, record)) {
198 struct cache_record *r = l;
200 l = avl_next_element(l, avl);
204 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
210 if (r->rdlength != rdlength)
213 if (!!r->rdata != !!rdata)
216 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
226 cache_host_is_known(char *record)
228 struct cache_record *l = avl_find_element(&records, record, l, avl);
233 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
234 struct cache_record *r = l;
236 l = avl_next_element(l, avl);
237 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
245 void cache_answer(struct interface *iface, struct sockaddr *from, uint8_t *base,
246 int blen, char *name, struct dns_answer *a, uint8_t *rdata,
249 struct dns_srv_data *dsd = (struct dns_srv_data *) rdata;
250 struct cache_record *r;
251 int port = 0, dlen = 0, tlen = 0, nlen, rdlength;
254 void *rdata_ptr, *txt_ptr;
256 static char *rdata_buffer = (char *) mdns_buf;
257 time_t now = monotonic_time();
266 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
267 perror("process_answer/dn_expand");
271 DBG(1, "A -> %s %s %s ttl:%d\n", dns_type_string(a->type), name, rdata_buffer, a->ttl);
273 rdlength = strlen(rdata_buffer);
275 if (strcmp(C_DNS_SD, name) != 0 &&
276 nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
277 host_len = rdlength - nlen - 1;
280 cache_service(iface, rdata_buffer, host_len, a->ttl);
282 dlen = strlen(rdata_buffer) + 1;
283 rdata = (uint8_t*)rdata_buffer;
290 port = be16_to_cpu(dsd->port);
291 memcpy(rdata_buffer, dsd, sizeof(*dsd));
292 if (dn_expand(base, base + blen, (const uint8_t*)&dsd[1],
293 &rdata_buffer[sizeof(*dsd)], MAX_DATA_LEN - sizeof(*dsd)) < 0) {
294 perror("process_answer/dn_expand");
297 dlen = sizeof(*dsd) + strlen(&rdata_buffer[sizeof(*dsd)]) + 1;
298 rdata = (uint8_t*)rdata_buffer;
302 rdlength = a->rdlength;
306 memcpy(rdata_buffer, &rdata[1], rdlength);
307 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
309 p = &rdata_buffer[*rdata];
315 if (v && p + v < &rdata_buffer[rdlength])
321 if (a->rdlength != 4)
327 if (a->rdlength != 16)
336 r = cache_record_find(name, a->type, port, dlen, rdata);
339 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
340 r->time = now + 1 - r->ttl;
346 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
354 r = calloc_a(sizeof(*r),
355 &name_buf, strlen(name) + 1,
359 r->avl.key = r->record = strcpy(name_buf, name);
367 memcpy(&r->from, from, sizeof(struct sockaddr_in6));
369 memcpy(&r->from, from, sizeof(struct sockaddr_in));
373 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
376 r->rdata = memcpy(rdata_ptr, rdata, dlen);
378 if (avl_insert(&records, &r->avl))
381 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
385 cache_dump_records(struct blob_buf *buf, const char *name)
387 struct cache_record *r, *last, *next;
389 char buffer[INET6_ADDRSTRLEN];
391 last = avl_last_element(&records, last, avl);
392 for (r = avl_find_element(&records, name, r, avl); r; r = next) {
395 if (r->txt && strlen(r->txt)) {
398 blobmsg_add_string(buf, "txt", txt);
399 txt = &txt[strlen(txt) + 1];
406 blobmsg_add_u32(buf, "port", r->port);
410 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
411 blobmsg_add_string(buf, "ipv4", buffer);
415 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
416 blobmsg_add_string(buf, "ipv6", buffer);
423 next = avl_next_element(r, avl);
424 if (strcmp(r->record, next->record) != 0)
430 cache_dump_recursive(struct blob_buf *b, const char *name, uint16_t type, struct interface *iface)
432 time_t now = monotonic_time();
433 for (struct cache_record *r = avl_find_ge_element(&records, name, r, avl);
434 r && !strcmp(r->record, name);
435 r = !avl_is_last(&records, &r->avl) ? avl_next_element(r, avl) : NULL) {
436 int32_t ttl = r->ttl - (now - r->time);
437 if (ttl <= 0 || (iface && iface->ifindex != r->iface->ifindex) ||
438 (type != TYPE_ANY && type != r->type))
442 char buf[INET6_ADDRSTRLEN];
443 void *k = blobmsg_open_table(b, NULL), *l;
444 const struct dns_srv_data *dsd = (const struct dns_srv_data*)r->rdata;
446 blobmsg_add_string(b, "name", r->record);
447 blobmsg_add_string(b, "type", dns_type_string(r->type));
448 blobmsg_add_u32(b, "ttl", ttl);
452 if ((txt = r->txt) && strlen(txt)) {
453 l = blobmsg_open_array(b, "data");
455 blobmsg_add_string(b, NULL, txt);
456 txt = &txt[strlen(txt) + 1];
458 blobmsg_close_array(b, l);
463 if (r->rdlength > sizeof(*dsd)) {
464 blobmsg_add_u32(b, "priority", be16_to_cpu(dsd->priority));
465 blobmsg_add_u32(b, "weight", be16_to_cpu(dsd->weight));
466 blobmsg_add_u32(b, "port", be16_to_cpu(dsd->port));
467 blobmsg_add_string(b, "target", (const char*)&dsd[1]);
473 blobmsg_add_string(b, "target", (const char*)r->rdata);
477 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buf, sizeof(buf)))
478 blobmsg_add_string(b, "target", buf);
482 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buf, sizeof(buf)))
483 blobmsg_add_string(b, "target", buf);
487 blobmsg_close_table(b, k);
490 if (r->type == TYPE_PTR) {
491 cache_dump_recursive(b, (const char*)r->rdata, TYPE_SRV, iface);
492 cache_dump_recursive(b, (const char*)r->rdata, TYPE_TXT, iface);
495 if (r->type == TYPE_SRV) {
496 cache_dump_recursive(b, (const char*)&dsd[1], TYPE_A, iface);
497 cache_dump_recursive(b, (const char*)&dsd[1], TYPE_AAAA, iface);