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;
57 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
58 [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
59 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
60 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
64 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
65 struct vlist_node *node_old);
67 static struct blob_buf b;
68 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
69 static int service_init_announce;
72 * service_instance_name - construct Service Instance Name as in RFC 6763
74 * RFC 6763 specifies Service Instance Names in the following way:
76 * Service Instance Name = <Instance> . <Service> . <Domain>
78 * @s: service to generate service instance name for
81 service_instance_name(struct service *s)
83 static char buffer[256];
85 snprintf(buffer, sizeof(buffer), "%s.%s", s->instance, s->service);
91 service_add_ptr(const char *host, int ttl)
93 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
98 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
102 service_add_srv(struct service *s, int ttl)
104 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
105 int len = sizeof(*sd);
107 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
108 if (len <= sizeof(*sd))
111 sd->port = cpu_to_be16(s->port);
112 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
115 #define TOUT_LOOKUP 60
118 service_timeout(struct service *s)
120 time_t t = monotonic_time();
122 if (t - s->t <= TOUT_LOOKUP)
129 service_reply_single(struct interface *iface, struct sockaddr *to, struct service *s, int ttl, int force)
131 const char *host = service_instance_name(s);
132 char *service = strstr(host, "._");
133 time_t t = service_timeout(s);
136 if (!force && (!s->active || !service || !t))
144 service_add_ptr(service_instance_name(s), ttl);
145 dns_send_answer(iface, to, service);
148 service_add_srv(s, ttl);
149 if (s->txt && s->txt_len)
150 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
151 dns_send_answer(iface, to, host);
155 service_reply(struct interface *iface, struct sockaddr *to, const char *match, int ttl)
159 vlist_for_each_element(&services, s, node) {
160 if (!match || !strcmp(s->service, match))
161 service_reply_single(iface, to, s, ttl, 0);
166 service_announce_services(struct interface *iface, struct sockaddr *to, int ttl)
170 vlist_for_each_element(&services, s, node) {
174 service_add_ptr(s->service, ttl);
175 dns_send_answer(iface, to, C_DNS_SD);
177 service_reply_single(iface, to, s, ttl, 0);
182 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
183 struct vlist_node *node_old)
185 struct interface *iface;
189 s = container_of(node_new, struct service, node);
190 if (service_init_announce)
191 vlist_for_each_element(&interfaces, iface, node) {
193 service_reply_single(iface, NULL, s, announce_ttl, 1);
198 s = container_of(node_old, struct service, node);
199 if (!node_new && service_init_announce)
200 vlist_for_each_element(&interfaces, iface, node)
201 service_reply_single(iface, NULL, s, 0, 1);
206 service_load_blob(struct blob_attr *b)
208 struct blob_attr *txt, *_tb[__SERVICE_MAX];
210 char *d_service, *d_id;
215 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
216 _tb, blobmsg_data(b), blobmsg_data_len(b));
217 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
220 if (_tb[SERVICE_TXT])
221 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
222 txt_len += 1 + strlen(blobmsg_get_string(txt));
224 s = calloc_a(sizeof(*s),
225 &d_id, strlen(blobmsg_name(b)) + 1,
226 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
231 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
232 s->id = strcpy(d_id, blobmsg_name(b));
233 s->instance = umdns_host_label;
234 s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
237 s->txt_len = txt_len;
240 if (_tb[SERVICE_TXT])
241 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
242 int len = strlen(blobmsg_get_string(txt));
249 memcpy(d_txt, blobmsg_get_string(txt), len);
253 vlist_add(&services, &s->node, s->id);
257 service_load(char *path)
259 struct blob_attr *cur;
263 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
266 for (i = 0; i < gl.gl_pathc; i++) {
267 blob_buf_init(&b, 0);
268 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i])) {
269 blob_for_each_attr(cur, b.head, rem)
270 service_load_blob(cur);
272 fprintf(stderr, "Error reading %s JSON\n", gl.gl_pathv[i]);
279 service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
281 struct blob_attr *cur;
286 vlist_update(&services);
287 service_load("/etc/umdns/*");
289 blob_for_each_attr(cur, msg, rem) {
290 struct blob_attr *cur2;
293 blobmsg_for_each_attr(cur2, cur, rem2) {
294 struct blob_attr *cur3;
297 if (strcmp(blobmsg_name(cur2), "instances"))
300 blobmsg_for_each_attr(cur3, cur2, rem3) {
301 struct blob_attr *cur4;
305 blobmsg_for_each_attr(cur4, cur3, rem4) {
306 const char *name = blobmsg_name(cur4);
308 if (!strcmp(name, "running")) {
309 running = blobmsg_get_bool(cur4);
310 } else if (running && !strcmp(name, "data")) {
311 struct blob_attr *cur5;
314 blobmsg_for_each_attr(cur5, cur4, rem5) {
315 struct blob_attr *cur6;
318 if (strcmp(blobmsg_name(cur5), "mdns"))
321 blobmsg_for_each_attr(cur6, cur5, rem6)
322 service_load_blob(cur6);
330 vlist_flush(&services);
334 service_init(int announce)
338 service_init_announce = announce;
339 ubus_service_list(service_init_cb);
343 service_cleanup(void)
345 vlist_flush(&services);