9c2f062590dbb4fa0a103b0d5be13aa931e76085
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
;
45 static struct blob_buf b
;
47 static struct kvlist types
;
50 cache_record_free(struct cache_record
*r
, int rem
)
52 DBG(2, "%s %s\n", dns_type_string(r
->type
), r
->record
);
54 avl_delete(&records
, &r
->avl
);
65 cache_entry_free(struct cache_entry
*s
)
67 DBG(2, "%s\n", s
->entry
);
68 avl_delete(&entries
, &s
->avl
);
77 cache_is_expired(time_t t
, uint32_t ttl
)
79 if (time(NULL
) - t
>= ttl
)
86 cache_gc_timer(struct uloop_timeout
*timeout
)
88 struct cache_record
*r
, *p
;
89 struct cache_entry
*s
, *t
;
91 avl_for_each_element_safe(&records
, r
, avl
, p
)
92 if (cache_is_expired(r
->time
, r
->ttl
))
93 cache_record_free(r
, 1);
95 avl_for_each_element_safe(&entries
, s
, avl
, t
) {
98 if (cache_is_expired(s
->time
, s
->ttl
))
102 uloop_timeout_set(timeout
, 10000);
106 cache_load_services(void)
108 struct blob_attr
*cur
;
111 blob_buf_init(&b
, 0);
113 if (!blobmsg_add_json_from_file(&b
, "/lib/mdns/service-types"))
116 blob_for_each_attr(cur
, b
.head
, rem
)
117 kvlist_set(&types
, blobmsg_name(cur
), blobmsg_get_string(cur
));
121 cache_lookup_name(const char *key
)
123 return kvlist_get(&types
, key
);
129 kvlist_init(&types
, kvlist_strlen
);
130 avl_init(&entries
, avl_strcmp
, true, NULL
);
131 avl_init(&records
, avl_strcmp
, true, NULL
);
133 cache_gc
.cb
= cache_gc_timer
;
134 uloop_timeout_set(&cache_gc
, 10000);
135 cache_load_services();
140 void cache_cleanup(void)
142 struct cache_record
*r
, *p
;
143 struct cache_entry
*s
, *t
;
145 avl_for_each_element_safe(&records
, r
, avl
, p
)
146 cache_record_free(r
, 1);
148 avl_for_each_element_safe(&entries
, s
, avl
, t
)
155 struct cache_entry
*s
;
157 avl_for_each_element(&entries
, s
, avl
)
158 dns_send_question(&listener
, s
->entry
, TYPE_PTR
);
161 static struct cache_entry
*
162 cache_find_entry(char *entry
)
164 struct cache_entry
*s
;
166 avl_for_each_element(&entries
, s
, avl
)
167 if (!strcmp(s
->entry
, entry
))
172 static struct cache_entry
*
173 cache_entry(struct uloop_fd
*u
, char *entry
, int hlen
, int ttl
)
175 struct cache_entry
*s
;
178 s
= cache_find_entry(entry
);
182 s
= malloc(sizeof(struct cache_entry
));
183 memset(s
, 0, sizeof(struct cache_entry
));
184 s
->avl
.key
= s
->entry
= strdup(entry
);
185 s
->time
= time(NULL
);
189 s
->host
= strndup(s
->entry
, hlen
);
190 type
= strstr(s
->entry
, "._");
195 avl_insert(&entries
, &s
->avl
);
198 dns_send_question(u
, entry
, TYPE_PTR
);
203 static struct cache_record
*
204 cache_record_find(char *record
, int type
, int port
, int rdlength
, uint8_t *rdata
)
206 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
211 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
212 struct cache_record
*r
= l
;
214 l
= avl_next_element(l
, avl
);
218 if (r
->type
== TYPE_TXT
|| (r
->type
== TYPE_SRV
))
224 if (r
->rdlength
!= rdlength
)
227 if (!!r
->rdata
!= !!rdata
)
230 if (!r
->rdata
|| !rdata
|| memcmp(r
->rdata
, rdata
, rdlength
))
240 cache_host_is_known(char *record
)
242 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
247 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
248 struct cache_record
*r
= l
;
250 l
= avl_next_element(l
, avl
);
251 if ((r
->type
!= TYPE_A
) && (r
->type
!= TYPE_AAAA
))
260 cache_answer(struct uloop_fd
*u
, uint8_t *base
, int blen
, char *name
, struct dns_answer
*a
, uint8_t *rdata
)
262 struct dns_srv_data
*dsd
= (struct dns_srv_data
*) rdata
;
263 struct cache_record
*r
;
264 int port
= 0, dlen
= 0, tlen
= 0, nlen
, rdlength
;
267 if (!(a
->class & CLASS_IN
))
277 if (dn_expand(base
, base
+ blen
, rdata
, rdata_buffer
, MAX_DATA_LEN
) < 0) {
278 perror("process_answer/dn_expand");
282 DBG(1, "A -> %s %s %s\n", dns_type_string(a
->type
), name
, rdata_buffer
);
284 rdlength
= strlen(rdata_buffer
);
286 if (!strcmp(C_DNS_SD
, name
)) {
287 cache_entry(u
, rdata_buffer
, 0, a
->ttl
);
291 if ((rdlength
< nlen
) && (rdlength
- nlen
- 1 > 0))
294 cache_entry(u
, rdata_buffer
, rdlength
- nlen
- 1, a
->ttl
);
301 port
= be16_to_cpu(dsd
->port
);
305 rdlength
= a
->rdlength
;
309 memcpy(rdata_buffer
, &rdata
[1], rdlength
);
310 rdata_buffer
[rdlength
] = rdata_buffer
[rdlength
+ 1] = '\0';
312 p
= &rdata_buffer
[*rdata
];
324 cache_entry(u
, name
, strlen(name
), a
->ttl
);
325 if (a
->rdlength
!= 4)
331 cache_entry(u
, name
, strlen(name
), a
->ttl
);
332 if (a
->rdlength
!= 16)
341 r
= cache_record_find(name
, a
->type
, port
, dlen
, rdata
);
344 cache_record_free(r
, 1);
345 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
348 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
356 r
= malloc(sizeof(struct cache_record
));
357 memset(r
, 0, sizeof(struct cache_record
));
358 r
->avl
.key
= r
->record
= strdup(name
);
363 r
->time
= time(NULL
);
366 r
->txt
= malloc(tlen
);
368 memcpy(r
->txt
, rdata_buffer
, tlen
);
372 r
->rdata
= malloc(dlen
);
374 cache_record_free(r
, 0);
377 memcpy(r
->rdata
, rdata
, dlen
);
380 if (avl_insert(&records
, &r
->avl
))
381 cache_record_free(r
, 0);
383 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);