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.
14 #include <sys/types.h>
15 #include <arpa/inet.h>
20 #include <libubox/avl.h>
21 #include <libubox/uloop.h>
26 static struct ubus_auto_conn conn
;
27 static struct blob_buf b
;
30 mdns_reload(struct ubus_context
*ctx
, struct ubus_object
*obj
,
31 struct ubus_request_data
*req
, const char *method
,
32 struct blob_attr
*msg
)
38 mdns_scan(struct ubus_context
*ctx
, struct ubus_object
*obj
,
39 struct ubus_request_data
*req
, const char *method
,
40 struct blob_attr
*msg
)
47 mdns_add_records(const char *name
)
49 struct cache_record
*r
, *q
= avl_find_element(&records
, name
, r
, avl
);
51 char buffer
[MAX_NAME_LEN
];
60 if (r
->txt
&& strlen(r
->txt
)) {
63 blobmsg_add_string(&b
, "txt", txt
);
64 txt
= &txt
[strlen(txt
) + 1];
71 blobmsg_add_u32(&b
, "port", r
->port
);
75 if ((r
->rdlength
== 4) && inet_ntop(AF_INET
, r
->rdata
, buffer
, INET6_ADDRSTRLEN
))
76 blobmsg_add_string(&b
, "ipv4", buffer
);
80 if ((r
->rdlength
== 16) && inet_ntop(AF_INET6
, r
->rdata
, buffer
, INET6_ADDRSTRLEN
))
81 blobmsg_add_string(&b
, "ipv6", buffer
);
84 q
= avl_next_element(r
, avl
);
85 } while (q
&& !strcmp(r
->record
, q
->record
));
89 mdns_browse(struct ubus_context
*ctx
, struct ubus_object
*obj
,
90 struct ubus_request_data
*req
, const char *method
,
91 struct blob_attr
*msg
)
93 struct cache_entry
*s
, *q
;
94 char buffer
[MAX_NAME_LEN
];
98 avl_for_each_element(&entries
, s
, avl
) {
100 if (*((char *) s
->avl
.key
) != '_')
102 snprintf(buffer
, MAX_NAME_LEN
, s
->avl
.key
);
103 local
= strstr(buffer
, ".local");
106 if (!strcmp(buffer
, "_tcp") || !strcmp(buffer
, "_udp"))
110 char *type
= cache_lookup_name(buffer
);
111 c1
= blobmsg_open_table(&b
, buffer
);
113 blobmsg_add_string(&b
, ".desc", type
);
115 snprintf(buffer
, MAX_NAME_LEN
, s
->entry
);
116 local
= strstr(buffer
, "._");
119 c2
= blobmsg_open_table(&b
, buffer
);
120 strncat(buffer
, ".local", MAX_NAME_LEN
);
121 mdns_add_records(buffer
);
122 mdns_add_records(s
->entry
);
123 blobmsg_close_table(&b
, c2
);
124 q
= avl_next_element(s
, avl
);
125 if (!q
|| avl_is_last(&entries
, &s
->avl
) || strcmp(s
->avl
.key
, q
->avl
.key
)) {
126 blobmsg_close_table(&b
, c1
);
130 ubus_send_reply(ctx
, req
, b
.head
);
132 return UBUS_STATUS_OK
;
136 mdns_hosts(struct ubus_context
*ctx
, struct ubus_object
*obj
,
137 struct ubus_request_data
*req
, const char *method
,
138 struct blob_attr
*msg
)
140 struct cache_entry
*s
;
141 char buffer
[MAX_NAME_LEN
];
144 blob_buf_init(&b
, 0);
145 avl_for_each_element(&entries
, s
, avl
) {
147 if (*((char *) s
->avl
.key
) == '_')
149 snprintf(buffer
, MAX_NAME_LEN
, s
->entry
);
150 local
= strstr(buffer
, "._");
153 c
= blobmsg_open_table(&b
, buffer
);
154 strncat(buffer
, ".local", MAX_NAME_LEN
);
155 mdns_add_records(buffer
);
156 mdns_add_records(s
->entry
);
157 blobmsg_close_table(&b
, c
);
159 ubus_send_reply(ctx
, req
, b
.head
);
161 return UBUS_STATUS_OK
;
164 static const struct ubus_method mdns_methods
[] = {
165 UBUS_METHOD_NOARG("scan", mdns_scan
),
166 UBUS_METHOD_NOARG("browse", mdns_browse
),
167 UBUS_METHOD_NOARG("hosts", mdns_hosts
),
168 UBUS_METHOD_NOARG("reload", mdns_reload
),
171 static struct ubus_object_type mdns_object_type
=
172 UBUS_OBJECT_TYPE("mdns", mdns_methods
);
174 static struct ubus_object mdns_object
= {
176 .type
= &mdns_object_type
,
177 .methods
= mdns_methods
,
178 .n_methods
= ARRAY_SIZE(mdns_methods
),
182 ubus_connect_handler(struct ubus_context
*ctx
)
186 ret
= ubus_add_object(ctx
, &mdns_object
);
188 fprintf(stderr
, "Failed to add object: %s\n", ubus_strerror(ret
));
194 conn
.cb
= ubus_connect_handler
;
195 ubus_auto_connect(&conn
);