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/nameser.h>
16 #include <sys/socket.h>
24 #include <libubox/vlist.h>
25 #include <libubox/uloop.h>
26 #include <libubox/avl-cmp.h>
27 #include <libubox/blobmsg_json.h>
33 #include "interface.h"
44 struct vlist_node node;
56 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
57 [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
58 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
59 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
63 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
64 struct vlist_node *node_old);
66 static struct blob_buf b;
67 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
68 char *sdudp = "_services._dns-sd._udp.local";
69 char *sdtcp = "_services._dns-sd._tcp.local";
70 static int service_init_announce;
73 service_name(const char *domain)
75 static char buffer[256];
77 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
83 service_add_ptr(const char *host, int ttl)
85 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
90 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
94 service_add_srv(struct service *s, int ttl)
96 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
97 int len = sizeof(*sd);
99 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
100 if (len <= sizeof(*sd))
103 sd->port = cpu_to_be16(s->port);
104 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
107 #define TOUT_LOOKUP 60
110 service_timeout(struct service *s)
112 time_t t = monotonic_time();
114 if (t - s->t <= TOUT_LOOKUP)
121 service_reply_single(struct interface *iface, struct service *s, int ttl, int force)
123 const char *host = service_name(s->service);
124 char *service = strstr(host, "._");
125 time_t t = service_timeout(s);
128 if (!force && (!s->active || !service || !t))
136 service_add_ptr(service_name(s->service), ttl);
137 dns_send_answer(iface, service);
140 service_add_srv(s, ttl);
141 if (s->txt && s->txt_len)
142 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
143 dns_send_answer(iface, host);
147 service_reply(struct interface *iface, const char *match, int ttl)
151 vlist_for_each_element(&services, s, node) {
152 if (!match || !strcmp(s->service, match))
153 service_reply_single(iface, s, ttl, 0);
158 service_announce_services(struct interface *iface, int tcp, int ttl)
162 vlist_for_each_element(&services, s, node) {
163 if (!strstr(s->service, "._tcp") && tcp)
165 if (!strstr(s->service, "._udp") && !tcp)
170 service_add_ptr(s->service, ttl);
172 dns_send_answer(iface, sdtcp);
174 dns_send_answer(iface, sdudp);
176 service_reply_single(iface, s, ttl, 0);
181 service_announce(struct interface *iface, int ttl)
183 service_announce_services(iface, 0, ttl);
184 service_announce_services(iface, 1, ttl);
188 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
189 struct vlist_node *node_old)
191 struct interface *iface;
195 s = container_of(node_new, struct service, node);
196 if (service_init_announce)
197 vlist_for_each_element(&interfaces, iface, node) {
199 service_reply_single(iface, s, announce_ttl, 1);
204 s = container_of(node_old, struct service, node);
205 if (!node_new && service_init_announce)
206 vlist_for_each_element(&interfaces, iface, node)
207 service_reply_single(iface, s, 0, 1);
212 service_load_blob(struct blob_attr *b)
214 struct blob_attr *txt, *_tb[__SERVICE_MAX];
216 char *d_service, *d_id;
221 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
222 _tb, blobmsg_data(b), blobmsg_data_len(b));
223 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
226 if (_tb[SERVICE_TXT])
227 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
228 txt_len += 1 + strlen(blobmsg_get_string(txt));
230 s = calloc_a(sizeof(*s),
231 &d_id, strlen(blobmsg_name(b)) + 1,
232 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
237 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
238 s->id = strcpy(d_id, blobmsg_name(b));
239 s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
242 s->txt_len = txt_len;
245 if (_tb[SERVICE_TXT])
246 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
247 int len = strlen(blobmsg_get_string(txt));
254 memcpy(d_txt, blobmsg_get_string(txt), len);
258 vlist_add(&services, &s->node, s->id);
262 service_load(char *path)
264 struct blob_attr *cur;
268 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
271 for (i = 0; i < gl.gl_pathc; i++) {
272 blob_buf_init(&b, 0);
273 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
274 blob_for_each_attr(cur, b.head, rem)
275 service_load_blob(cur);
281 service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
283 struct blob_attr *cur;
288 vlist_update(&services);
289 service_load("/tmp/run/mdns/*");
291 blob_for_each_attr(cur, msg, rem) {
292 struct blob_attr *cur2;
295 blobmsg_for_each_attr(cur2, cur, rem2) {
296 struct blob_attr *cur3;
299 if (strcmp(blobmsg_name(cur2), "instances"))
302 blobmsg_for_each_attr(cur3, cur2, rem3) {
303 struct blob_attr *cur4;
307 blobmsg_for_each_attr(cur4, cur3, rem4) {
308 const char *name = blobmsg_name(cur4);
310 if (!strcmp(name, "running")) {
311 running = blobmsg_get_bool(cur4);
312 } else if (running && !strcmp(name, "data")) {
313 struct blob_attr *cur5;
316 blobmsg_for_each_attr(cur5, cur4, rem5) {
317 struct blob_attr *cur6;
320 if (strcmp(blobmsg_name(cur5), "mdns"))
323 blobmsg_for_each_attr(cur6, cur5, rem6)
324 service_load_blob(cur6);
332 vlist_flush(&services);
336 service_init(int announce)
340 service_init_announce = announce;
341 ubus_service_list(service_init_cb);
345 service_cleanup(void)
347 vlist_flush(&services);