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 entries
;
45 static 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_entry_free(struct cache_entry
*s
)
58 DBG(2, "%s\n", s
->entry
);
59 avl_delete(&entries
, &s
->avl
);
64 cache_is_expired(time_t t
, uint32_t ttl
)
66 if (time(NULL
) - t
>= ttl
)
73 cache_gc_timer(struct uloop_timeout
*timeout
)
75 struct cache_record
*r
, *p
;
76 struct cache_entry
*s
, *t
;
78 avl_for_each_element_safe(&records
, r
, avl
, p
)
79 if (cache_is_expired(r
->time
, r
->ttl
))
82 avl_for_each_element_safe(&entries
, s
, avl
, t
) {
85 if (cache_is_expired(s
->time
, s
->ttl
))
89 uloop_timeout_set(timeout
, 10000);
95 avl_init(&entries
, 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 interface
*iface
;
119 struct cache_entry
*s
;
121 vlist_for_each_element(&interfaces
, iface
, node
)
122 avl_for_each_element(&entries
, s
, avl
)
123 dns_send_question(iface
, s
->entry
, TYPE_PTR
);
126 static struct cache_entry
*
127 cache_entry(struct interface
*iface
, char *entry
, int hlen
, int ttl
)
129 struct cache_entry
*s
, *t
;
134 avl_for_each_element_safe(&entries
, s
, avl
, t
)
135 if (!strcmp(s
->entry
, entry
))
138 s
= calloc_a(sizeof(*s
),
139 &entry_buf
, strlen(entry
) + 1,
140 &host_buf
, hlen
? hlen
+ 1 : 0);
142 s
->avl
.key
= s
->entry
= strcpy(entry_buf
, entry
);
143 s
->time
= time(NULL
);
147 s
->host
= strncpy(host_buf
, s
->entry
, hlen
);
149 type
= strstr(entry_buf
, "._");
154 avl_insert(&entries
, &s
->avl
);
157 dns_send_question(iface
, entry
, TYPE_PTR
);
162 static struct cache_record
*
163 cache_record_find(char *record
, int type
, int port
, int rdlength
, uint8_t *rdata
)
165 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
170 while (l
&& l
->record
&& !strcmp(l
->record
, record
)) {
171 struct cache_record
*r
= l
;
173 l
= avl_next_element(l
, avl
);
177 if (r
->type
== TYPE_TXT
|| (r
->type
== TYPE_SRV
))
183 if (r
->rdlength
!= rdlength
)
186 if (!!r
->rdata
!= !!rdata
)
189 if (!r
->rdata
|| !rdata
|| memcmp(r
->rdata
, rdata
, rdlength
))
199 cache_host_is_known(char *record
)
201 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
206 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
207 struct cache_record
*r
= l
;
209 l
= avl_next_element(l
, avl
);
210 if ((r
->type
!= TYPE_A
) && (r
->type
!= TYPE_AAAA
))
219 cache_answer(struct interface
*iface
, uint8_t *base
, int blen
, char *name
, struct dns_answer
*a
, uint8_t *rdata
)
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
;
226 void *rdata_ptr
, *txt_ptr
;
228 static char *rdata_buffer
= (char *) mdns_buf
;
230 if (!(a
->class & CLASS_IN
))
240 if (dn_expand(base
, base
+ blen
, rdata
, rdata_buffer
, MAX_DATA_LEN
) < 0) {
241 perror("process_answer/dn_expand");
245 DBG(1, "A -> %s %s %s\n", dns_type_string(a
->type
), name
, rdata_buffer
);
247 rdlength
= strlen(rdata_buffer
);
249 if (strcmp(C_DNS_SD
, name
) != 0 &&
250 nlen
+ 1 < rdlength
&& !strcmp(rdata_buffer
+ rdlength
- nlen
, name
))
251 host_len
= rdlength
- nlen
- 1;
253 cache_entry(iface
, rdata_buffer
, host_len
, a
->ttl
);
260 port
= be16_to_cpu(dsd
->port
);
264 rdlength
= a
->rdlength
;
268 memcpy(rdata_buffer
, &rdata
[1], rdlength
);
269 rdata_buffer
[rdlength
] = rdata_buffer
[rdlength
+ 1] = '\0';
271 p
= &rdata_buffer
[*rdata
];
277 if (v
&& p
+ v
< &rdata_buffer
[rdlength
])
283 cache_entry(iface
, name
, strlen(name
), a
->ttl
);
284 if (a
->rdlength
!= 4)
290 cache_entry(iface
, name
, strlen(name
), a
->ttl
);
291 if (a
->rdlength
!= 16)
300 r
= cache_record_find(name
, a
->type
, port
, dlen
, rdata
);
303 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
304 cache_record_free(r
);
307 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
315 r
= calloc_a(sizeof(*r
),
316 &name_buf
, strlen(name
) + 1,
320 r
->avl
.key
= r
->record
= strcpy(name_buf
, name
);
325 r
->time
= time(NULL
);
328 r
->txt
= memcpy(txt_ptr
, rdata_buffer
, tlen
);
331 r
->rdata
= memcpy(rdata_ptr
, rdata
, dlen
);
333 if (avl_insert(&records
, &r
->avl
))
336 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
340 cache_dump_records(struct blob_buf
*buf
, const char *name
)
342 struct cache_record
*r
, *last
, *next
;
344 char buffer
[INET6_ADDRSTRLEN
];
346 last
= avl_last_element(&records
, last
, avl
);
347 for (r
= avl_find_element(&records
, name
, r
, avl
); r
; r
= next
) {
350 if (r
->txt
&& strlen(r
->txt
)) {
353 blobmsg_add_string(buf
, "txt", txt
);
354 txt
= &txt
[strlen(txt
) + 1];
361 blobmsg_add_u32(buf
, "port", r
->port
);
365 if ((r
->rdlength
== 4) && inet_ntop(AF_INET
, r
->rdata
, buffer
, INET6_ADDRSTRLEN
))
366 blobmsg_add_string(buf
, "ipv4", buffer
);
370 if ((r
->rdlength
== 16) && inet_ntop(AF_INET6
, r
->rdata
, buffer
, INET6_ADDRSTRLEN
))
371 blobmsg_add_string(buf
, "ipv6", buffer
);
378 next
= avl_next_element(r
, avl
);
379 if (strcmp(r
->record
, next
->record
) != 0)