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>
26 #include <libubox/vlist.h>
27 #include <libubox/uloop.h>
28 #include <libubox/avl-cmp.h>
29 #include <libubox/blobmsg_json.h>
34 #include "interface.h"
43 struct vlist_node node;
55 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
56 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
57 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
60 static const struct uci_blob_param_list service_attr_list = {
61 .n_params = __SERVICE_MAX,
62 .params = service_policy,
66 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
67 struct vlist_node *node_old);
69 static struct blob_buf b;
70 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
71 char *hostname = NULL;
72 static char *sdudp = "_services._dns-sd._udp.local";
73 static char *sdtcp = "_services._dns-sd._tcp.local";
76 service_name(const char *domain)
78 static char buffer[256];
80 snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
86 service_add_ptr(const char *host)
88 unsigned char buffer[MAX_NAME_LEN];
89 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
94 dns_add_answer(TYPE_PTR, buffer, len);
98 service_send_a(struct interface *iface)
100 unsigned char buffer[MAX_NAME_LEN];
101 char *host = service_name("local");
102 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
107 dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
111 service_add_srv(struct service *s)
113 unsigned char buffer[MAX_NAME_LEN];
114 struct dns_srv_data *sd;
115 char *host = service_name("local");
116 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
121 sd = calloc(1, len + sizeof(struct dns_srv_data));
125 sd->port = cpu_to_be16(s->port);
126 memcpy(&sd[1], buffer, len);
127 host = service_name(s->service);
128 dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
132 #define TOUT_LOOKUP 60
135 service_timeout(struct service *s)
137 time_t t = time(NULL);
139 if (t - s->t <= TOUT_LOOKUP)
148 service_reply_a(struct interface *iface, int type)
154 service_send_a(iface);
155 dns_send_answer(iface, service_name("local"));
159 service_reply(struct interface *iface, const char *match)
163 vlist_for_each_element(&services, s, node) {
164 char *host = service_name(s->service);
165 char *service = strstr(host, "._");
167 if (!s->active || !service || !service_timeout(s))
172 if (match && strcmp(match, s->service))
176 service_add_ptr(service_name(s->service));
177 dns_send_answer(iface, service);
181 if (s->txt && s->txt_len)
182 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
183 dns_send_answer(iface, host);
190 service_send_a(iface);
191 dns_send_answer(iface, service_name("local"));
195 service_announce_services(struct interface *iface, const char *service)
200 if (!strcmp(service, sdudp))
202 else if (strcmp(service, sdtcp))
205 vlist_for_each_element(&services, s, node) {
206 if (!strstr(s->service, "._tcp") && tcp)
208 if (!strstr(s->service, "._udp") && !tcp)
212 service_add_ptr(s->service);
214 dns_send_answer(iface, sdtcp);
216 dns_send_answer(iface, sdudp);
217 service_reply(iface, s->service);
222 service_announce(struct interface *iface)
224 service_announce_services(iface, sdudp);
225 service_announce_services(iface, sdtcp);
229 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
230 struct vlist_node *node_old)
237 s = container_of(node_old, struct service, node);
242 service_load(char *path)
244 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
248 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
251 for (i = 0; i < gl.gl_pathc; i++) {
252 blob_buf_init(&b, 0);
254 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
256 blob_for_each_attr(cur, b.head, rem) {
258 char *d_service, *d_daemon;
263 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
264 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
265 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
268 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
269 txt_len += 1 + strlen(blobmsg_get_string(txt));
271 s = calloc_a(sizeof(*s),
272 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
273 &d_service, strlen(blobmsg_name(cur)) + 1,
278 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
279 s->service = strcpy(d_service, blobmsg_name(cur));
280 s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
283 s->txt_len = txt_len;
286 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
287 int len = strlen(blobmsg_get_string(txt));
294 memcpy(d_txt, blobmsg_get_string(txt), len);
298 vlist_add(&services, &s->node, s->service);
307 hostname = get_hostname();
309 vlist_update(&services);
310 service_load("/tmp/run/mdnsd/*");
311 vlist_flush(&services);
315 service_cleanup(void)
317 vlist_flush(&services);