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 <asm/byteorder.h>
28 #include <arpa/nameser.h>
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>
43 static struct uloop_timeout cache_gc;
44 struct avl_tree records, entries, hosts;
47 cache_record_free(struct cache_record *r)
49 DBG(2, "%s %s\n", dns_type_string(r->type), r->record);
50 avl_delete(&records, &r->avl);
55 cache_entry_free(struct cache_entry *s)
57 DBG(2, "%s\n", s->entry);
58 avl_delete(&entries, &s->avl);
63 cache_is_expired(time_t t, uint32_t ttl)
65 if (time(NULL) - t >= ttl)
72 cache_gc_timer(struct uloop_timeout *timeout)
74 struct cache_record *r, *p;
75 struct cache_entry *s, *t;
77 avl_for_each_element_safe(&records, r, avl, p)
78 if (cache_is_expired(r->time, r->ttl))
81 avl_for_each_element_safe(&entries, s, avl, t) {
84 if (cache_is_expired(s->time, s->ttl))
88 uloop_timeout_set(timeout, 10000);
94 avl_init(&entries, avl_strcmp, true, NULL);
95 avl_init(&records, avl_strcmp, true, NULL);
97 cache_gc.cb = cache_gc_timer;
98 uloop_timeout_set(&cache_gc, 10000);
103 void cache_cleanup(void)
105 struct cache_record *r, *p;
106 struct cache_entry *s, *t;
108 avl_for_each_element_safe(&records, r, avl, p)
109 cache_record_free(r);
111 avl_for_each_element_safe(&entries, s, avl, t)
118 struct cache_entry *s;
120 avl_for_each_element(&entries, s, avl)
121 dns_send_question(&listener, s->entry, TYPE_PTR);
124 static struct cache_entry*
125 cache_entry(struct uloop_fd *u, char *entry, int hlen, int ttl)
127 struct cache_entry *s;
132 s = avl_find_element(&entries, entry, s, avl);
136 s = calloc_a(sizeof(*s),
137 &entry_buf, strlen(entry) + 1,
138 &host_buf, hlen ? hlen + 1 : 0);
140 s->avl.key = s->entry = strcpy(entry_buf, entry);
141 s->time = time(NULL);
145 s->host = strncpy(host_buf, s->entry, hlen);
147 type = strstr(entry_buf, "._");
152 avl_insert(&entries, &s->avl);
155 dns_send_question(u, entry, TYPE_PTR);
160 static struct cache_record*
161 cache_record_find(char *record, int type, int port, int rdlength, uint8_t *rdata)
163 struct cache_record *l = avl_find_element(&records, record, l, avl);
168 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
169 struct cache_record *r = l;
171 l = avl_next_element(l, avl);
175 if (r->type == TYPE_TXT || (r->type == TYPE_SRV))
181 if (r->rdlength != rdlength)
184 if (!!r->rdata != !!rdata)
187 if (!r->rdata || !rdata || memcmp(r->rdata, rdata, rdlength))
197 cache_host_is_known(char *record)
199 struct cache_record *l = avl_find_element(&records, record, l, avl);
204 while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
205 struct cache_record *r = l;
207 l = avl_next_element(l, avl);
208 if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
217 cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns_answer *a, uint8_t *rdata)
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;
224 void *rdata_ptr, *txt_ptr;
227 if (!(a->class & CLASS_IN))
237 if (dn_expand(base, base + blen, rdata, rdata_buffer, MAX_DATA_LEN) < 0) {
238 perror("process_answer/dn_expand");
242 DBG(1, "A -> %s %s %s\n", dns_type_string(a->type), name, rdata_buffer);
244 rdlength = strlen(rdata_buffer);
246 if (strcmp(C_DNS_SD, name) != 0 &&
247 nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
248 host_len = rdlength - nlen - 1;
250 cache_entry(u, rdata_buffer, host_len, a->ttl);
257 port = be16_to_cpu(dsd->port);
261 rdlength = a->rdlength;
265 memcpy(rdata_buffer, &rdata[1], rdlength);
266 rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
268 p = &rdata_buffer[*rdata];
280 cache_entry(u, name, strlen(name), a->ttl);
281 if (a->rdlength != 4)
287 cache_entry(u, name, strlen(name), a->ttl);
288 if (a->rdlength != 16)
297 r = cache_record_find(name, a->type, port, dlen, rdata);
300 cache_record_free(r);
301 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
304 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
312 r = calloc_a(sizeof(*r),
313 &name_buf, strlen(name) + 1,
317 r->avl.key = r->record = strcpy(name_buf, name);
322 r->time = time(NULL);
325 r->txt = memcpy(txt_ptr, rdata_buffer, tlen);
328 r->rdata = memcpy(rdata_ptr, rdata, dlen);
330 if (avl_insert(&records, &r->avl))
333 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);