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
)
52 DBG(2, "%s %s\n", dns_type_string(r
->type
), r
->record
);
53 avl_delete(&records
, &r
->avl
);
58 cache_entry_free(struct cache_entry
*s
)
60 DBG(2, "%s\n", s
->entry
);
61 avl_delete(&entries
, &s
->avl
);
66 cache_is_expired(time_t t
, uint32_t ttl
)
68 if (time(NULL
) - t
>= ttl
)
75 cache_gc_timer(struct uloop_timeout
*timeout
)
77 struct cache_record
*r
, *p
;
78 struct cache_entry
*s
, *t
;
80 avl_for_each_element_safe(&records
, r
, avl
, p
)
81 if (cache_is_expired(r
->time
, r
->ttl
))
84 avl_for_each_element_safe(&entries
, s
, avl
, t
) {
87 if (cache_is_expired(s
->time
, s
->ttl
))
91 uloop_timeout_set(timeout
, 10000);
95 cache_load_services(void)
97 struct blob_attr
*cur
;
100 blob_buf_init(&b
, 0);
102 if (!blobmsg_add_json_from_file(&b
, "/lib/mdns/service-types"))
105 blob_for_each_attr(cur
, b
.head
, rem
)
106 kvlist_set(&types
, blobmsg_name(cur
), blobmsg_get_string(cur
));
110 cache_lookup_name(const char *key
)
112 return kvlist_get(&types
, key
);
118 kvlist_init(&types
, kvlist_strlen
);
119 avl_init(&entries
, avl_strcmp
, true, NULL
);
120 avl_init(&records
, avl_strcmp
, true, NULL
);
122 cache_gc
.cb
= cache_gc_timer
;
123 uloop_timeout_set(&cache_gc
, 10000);
124 cache_load_services();
129 void cache_cleanup(void)
131 struct cache_record
*r
, *p
;
132 struct cache_entry
*s
, *t
;
134 avl_for_each_element_safe(&records
, r
, avl
, p
)
135 cache_record_free(r
);
137 avl_for_each_element_safe(&entries
, s
, avl
, t
)
144 struct cache_entry
*s
;
146 avl_for_each_element(&entries
, s
, avl
)
147 dns_send_question(&listener
, s
->entry
, TYPE_PTR
);
150 static struct cache_entry
*
151 cache_entry(struct uloop_fd
*u
, char *entry
, int hlen
, int ttl
)
153 struct cache_entry
*s
;
158 s
= avl_find_element(&entries
, entry
, s
, avl
);
162 s
= calloc_a(sizeof(*s
),
163 &entry_buf
, strlen(entry
) + 1,
164 &host_buf
, hlen
? hlen
+ 1 : 0);
166 s
->avl
.key
= s
->entry
= strcpy(entry_buf
, entry
);
167 s
->time
= time(NULL
);
171 s
->host
= strncpy(host_buf
, s
->entry
, hlen
);
173 type
= strstr(entry_buf
, "._");
178 avl_insert(&entries
, &s
->avl
);
181 dns_send_question(u
, entry
, TYPE_PTR
);
186 static struct cache_record
*
187 cache_record_find(char *record
, int type
, int port
, int rdlength
, uint8_t *rdata
)
189 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
194 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
195 struct cache_record
*r
= l
;
197 l
= avl_next_element(l
, avl
);
201 if (r
->type
== TYPE_TXT
|| (r
->type
== TYPE_SRV
))
207 if (r
->rdlength
!= rdlength
)
210 if (!!r
->rdata
!= !!rdata
)
213 if (!r
->rdata
|| !rdata
|| memcmp(r
->rdata
, rdata
, rdlength
))
223 cache_host_is_known(char *record
)
225 struct cache_record
*l
= avl_find_element(&records
, record
, l
, avl
);
230 while (l
&& !avl_is_last(&records
, &l
->avl
) && !strcmp(l
->record
, record
)) {
231 struct cache_record
*r
= l
;
233 l
= avl_next_element(l
, avl
);
234 if ((r
->type
!= TYPE_A
) && (r
->type
!= TYPE_AAAA
))
243 cache_answer(struct uloop_fd
*u
, uint8_t *base
, int blen
, char *name
, struct dns_answer
*a
, uint8_t *rdata
)
245 struct dns_srv_data
*dsd
= (struct dns_srv_data
*) rdata
;
246 struct cache_record
*r
;
247 int port
= 0, dlen
= 0, tlen
= 0, nlen
, rdlength
;
250 void *rdata_ptr
, *txt_ptr
;
252 if (!(a
->class & CLASS_IN
))
262 if (dn_expand(base
, base
+ blen
, rdata
, rdata_buffer
, MAX_DATA_LEN
) < 0) {
263 perror("process_answer/dn_expand");
267 DBG(1, "A -> %s %s %s\n", dns_type_string(a
->type
), name
, rdata_buffer
);
269 rdlength
= strlen(rdata_buffer
);
271 if (!strcmp(C_DNS_SD
, name
)) {
272 cache_entry(u
, rdata_buffer
, 0, a
->ttl
);
276 if ((rdlength
< nlen
) && (rdlength
- nlen
- 1 > 0))
279 cache_entry(u
, rdata_buffer
, rdlength
- nlen
- 1, a
->ttl
);
286 port
= be16_to_cpu(dsd
->port
);
290 rdlength
= a
->rdlength
;
294 memcpy(rdata_buffer
, &rdata
[1], rdlength
);
295 rdata_buffer
[rdlength
] = rdata_buffer
[rdlength
+ 1] = '\0';
297 p
= &rdata_buffer
[*rdata
];
309 cache_entry(u
, name
, strlen(name
), a
->ttl
);
310 if (a
->rdlength
!= 4)
316 cache_entry(u
, name
, strlen(name
), a
->ttl
);
317 if (a
->rdlength
!= 16)
326 r
= cache_record_find(name
, a
->type
, port
, dlen
, rdata
);
329 cache_record_free(r
);
330 DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
333 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);
341 r
= calloc_a(sizeof(*r
),
342 &name_buf
, strlen(name
) + 1,
346 r
->avl
.key
= r
->record
= strcpy(name_buf
, name
);
351 r
->time
= time(NULL
);
354 r
->txt
= memcpy(txt_ptr
, rdata_buffer
, tlen
);
357 r
->rdata
= memcpy(rdata_ptr
, rdata
, dlen
);
359 if (avl_insert(&records
, &r
->avl
))
362 DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r
->type
), r
->record
, r
->ttl
);