static struct uloop_timeout cache_gc;
struct avl_tree services;
-AVL_TREE(records, avl_strcmp, true, NULL);
+
+static int avl_strcasecmp(const void *k1, const void *k2, void *ptr)
+{
+ return strcasecmp(k1, k2);
+}
+
+AVL_TREE(records, avl_strcasecmp, true, NULL);
static void
cache_record_free(struct cache_record *r)
int
cache_init(void)
{
- avl_init(&services, avl_strcmp, true, NULL);
+ avl_init(&services, avl_strcasecmp, true, NULL);
cache_gc.cb = cache_gc_timer;
uloop_timeout_set(&cache_gc, 10000);
cache_update(void)
{
struct interface *iface;
- struct cache_service *s;
- vlist_for_each_element(&interfaces, iface, node)
- avl_for_each_element(&services, s, avl)
- dns_send_question(iface, NULL, s->entry, TYPE_PTR, 0);
+ vlist_for_each_element(&interfaces, iface, node) {
+ dns_send_question(iface, NULL, C_DNS_SD, TYPE_ANY, 0);
+ dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0);
+ }
}
static struct cache_service*
avl_insert(&services, &s->avl);
if (!hlen)
- dns_send_question(iface, NULL, entry, TYPE_PTR, iface->multicast);
+ dns_send_question(iface, NULL, entry, TYPE_PTR, interface_multicast(iface));
return s;
}
{
struct cache_record *l = avl_find_element(&records, record, l, avl);
- if (!l)
- return NULL;
-
- while (l && l->record && !strcmp(l->record, record)) {
+ while (l && !strcmp(l->record, record)) {
struct cache_record *r = l;
- l = avl_next_element(l, avl);
+ l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
if (r->type != type)
continue;
{
struct cache_record *l = avl_find_element(&records, record, l, avl);
- if (!l)
- return 0;
-
- while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
+ while (l && !strcmp(l->record, record)) {
struct cache_record *r = l;
- l = avl_next_element(l, avl);
+ l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
continue;
return 1;
if (rdlength <= 2)
return;
- memcpy(rdata_buffer, &rdata[1], rdlength);
+ memcpy(rdata_buffer, &rdata[1], rdlength-1);
rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
tlen = rdlength + 1;
p = &rdata_buffer[*rdata];
r->rdlength = dlen;
r->time = now;
r->iface = iface;
- if (iface->v6)
+ if (interface_ipv6(iface))
memcpy(&r->from, from, sizeof(struct sockaddr_in6));
else
memcpy(&r->from, from, sizeof(struct sockaddr_in));
}
void
-cache_dump_records(struct blob_buf *buf, const char *name)
+cache_dump_records(struct blob_buf *buf, const char *name, int array,
+ const char **hostname)
{
struct cache_record *r, *last, *next;
const char *txt;
char buffer[INET6_ADDRSTRLEN];
+ void *c = NULL;
last = avl_last_element(&records, last, avl);
+ for (r = avl_find_element(&records, name, r, avl); r; r = next) {
+ switch (r->type) {
+ case TYPE_A:
+ if (!c && array)
+ c = blobmsg_open_array(buf, "ipv4");
+ if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
+ blobmsg_add_string(buf, "ipv4", buffer);
+ break;
+ }
+
+ if (r == last)
+ break;
+
+ next = avl_next_element(r, avl);
+ if (strcmp(r->record, next->record) != 0)
+ break;
+ }
+
+ if (c) {
+ blobmsg_close_array(buf, c);
+ c = NULL;
+ }
+
+ for (r = avl_find_element(&records, name, r, avl); r; r = next) {
+ switch (r->type) {
+ case TYPE_AAAA:
+ if (!c && array)
+ c = blobmsg_open_array(buf, "ipv6");
+ if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
+ blobmsg_add_string(buf, "ipv6", buffer);
+ break;
+ }
+
+ if (r == last)
+ break;
+
+ next = avl_next_element(r, avl);
+ if (strcmp(r->record, next->record) != 0)
+ break;
+ }
+
+ if (c) {
+ blobmsg_close_array(buf, c);
+ c = NULL;
+ }
+
for (r = avl_find_element(&records, name, r, avl); r; r = next) {
switch (r->type) {
case TYPE_TXT:
if (r->txt && strlen(r->txt)) {
+ if (array)
+ c = blobmsg_open_array(buf, "txt");
+
txt = r->txt;
do {
blobmsg_add_string(buf, "txt", txt);
txt = &txt[strlen(txt) + 1];
} while (*txt);
+ if (array)
+ blobmsg_close_array(buf, c);
}
break;
case TYPE_SRV:
+ if (r->rdata) {
+ blobmsg_add_string(buf, "host", (char *)r->rdata + sizeof(struct dns_srv_data));
+ if (hostname)
+ *hostname = (char *)r->rdata + sizeof(struct dns_srv_data);
+ }
if (r->port)
blobmsg_add_u32(buf, "port", r->port);
break;
-
- case TYPE_A:
- if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
- blobmsg_add_string(buf, "ipv4", buffer);
- break;
-
- case TYPE_AAAA:
- if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
- blobmsg_add_string(buf, "ipv6", buffer);
- break;
}
if (r == last)