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_entry(struct uloop_fd
*u
, char *entry
, int hlen
, int ttl
)
164 struct cache_entry
*s
;
167 s
= avl_find_element(&entries
, entry
, s
, avl
);
171 s
= malloc(sizeof(struct cache_entry
));
172 memset(s
, 0, sizeof(struct cache_entry
));
173 s
->avl
.key
= s
->entry
= strdup(entry
);
174 s
->time
= time(NULL
);
178 s
->host
= strndup(s
->entry
, hlen
);
179 type
= strstr(s
->entry
, "._");
184 avl_insert(&entries
, &s
->avl
);
187 dns_send_question(u
, entry
, TYPE_PTR
);
192 static struct cache_record
*
193 cache_record_find(char *record
, int type
, int port
, int rdlength
, uint8_t *rdata
)
195 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
200 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
201 struct cache_record
*r
= l
;
203 l
= avl_next_element(l
, avl
);
207 if (r
->type
== TYPE_TXT
|| (r
->type
== TYPE_SRV
))
213 if (r
->rdlength
!= rdlength
)
216 if (!!r
->rdata
!= !!rdata
)
219 if (!r
->rdata
|| !rdata
|| memcmp(r
->rdata
, rdata
, rdlength
))
229 cache_host_is_known(char *record
)
231 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
236 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
237 struct cache_record
*r
= l
;
239 l
= avl_next_element(l
, avl
);
240 if ((r
->type
!= TYPE_A
) && (r
->type
!= TYPE_AAAA
))
249 cache_answer(struct uloop_fd
*u
, uint8_t *base
, int blen
, char *name
, struct dns_answer
*a
, uint8_t *rdata
)
251 struct dns_srv_data
*dsd
= (struct dns_srv_data
*) rdata
;
252 struct cache_record
*r
;
253 int port
= 0, dlen
= 0, tlen
= 0, nlen
, rdlength
;
256 if (!(a
->class & CLASS_IN
))
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\n", dns_type_string(a
->type
), name
, rdata_buffer
);
273 rdlength
= strlen(rdata_buffer
);
275 if (!strcmp(C_DNS_SD
, name
)) {
276 cache_entry(u
, rdata_buffer
, 0, a
->ttl
);
280 if ((rdlength
< nlen
) && (rdlength
- nlen
- 1 > 0))
283 cache_entry(u
, rdata_buffer
, rdlength
- nlen
- 1, a
->ttl
);
290 port
= be16_to_cpu(dsd
->port
);
294 rdlength
= a
->rdlength
;
298 memcpy(rdata_buffer
, &rdata
[1], rdlength
);
299 rdata_buffer
[rdlength
] = rdata_buffer
[rdlength
+ 1] = '\0';
301 p
= &rdata_buffer
[*rdata
];
313 cache_entry(u
, name
, strlen(name
), a
->ttl
);
314 if (a
->rdlength
!= 4)
320 cache_entry(u
, name
, strlen(name
), a
->ttl
);
321 if (a
->rdlength
!= 16)
330 r
= cache_record_find(name
, a
->type
, port
, dlen
, rdata
);
333 cache_record_free(r
, 1);
334 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
337 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
345 r
= malloc(sizeof(struct cache_record
));
346 memset(r
, 0, sizeof(struct cache_record
));
347 r
->avl
.key
= r
->record
= strdup(name
);
352 r
->time
= time(NULL
);
355 r
->txt
= malloc(tlen
);
357 memcpy(r
->txt
, rdata_buffer
, tlen
);
361 r
->rdata
= malloc(dlen
);
363 cache_record_free(r
, 0);
366 memcpy(r
->rdata
, rdata
, dlen
);
369 if (avl_insert(&records
, &r
->avl
))
370 cache_record_free(r
, 0);
372 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);