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 <netinet/in.h>
16 #include <arpa/nameser.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
29 #include <libubox/vlist.h>
30 #include <libubox/uloop.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/blobmsg_json.h>
45 struct vlist_node node;
57 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
58 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
59 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
62 static const struct uci_blob_param_list service_attr_list = {
63 .n_params = __SERVICE_MAX,
64 .params = service_policy,
68 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
69 struct vlist_node *node_old);
71 static struct blob_buf b;
72 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
73 char *hostname = NULL;
74 static char *sdudp = "_services._dns-sd._udp.local";
75 static char *sdtcp = "_services._dns-sd._tcp.local";
78 service_name(const char *domain)
80 static char buffer[256];
82 snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
88 service_send_ptr(struct uloop_fd *u, struct service *s)
90 unsigned char buffer[MAX_NAME_LEN];
91 const char *host = service_name(s->service);
92 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
97 dns_add_answer(TYPE_PTR, buffer, len);
101 service_send_ptr_c(struct uloop_fd *u, const char *host)
103 unsigned char buffer[MAX_NAME_LEN];
104 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
109 dns_add_answer(TYPE_PTR, buffer, len);
113 service_send_a(struct uloop_fd *u)
115 unsigned char buffer[MAX_NAME_LEN];
116 char *host = service_name("local");
117 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
120 if (!inet_aton(iface_ip, &in)) {
121 fprintf(stderr, "%s is not valid\n", iface_ip);
128 dns_add_answer(TYPE_A, (uint8_t *) &in.s_addr, 4);
132 service_send_srv(struct uloop_fd *u, struct service *s)
134 unsigned char buffer[MAX_NAME_LEN];
135 struct dns_srv_data *sd;
136 char *host = service_name("local");
137 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
142 sd = calloc(1, len + sizeof(struct dns_srv_data));
146 sd->port = cpu_to_be16(s->port);
147 memcpy(&sd[1], buffer, len);
148 host = service_name(s->service);
149 dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
153 #define TOUT_LOOKUP 60
156 service_timeout(struct service *s)
158 time_t t = time(NULL);
160 if (t - s->t <= TOUT_LOOKUP)
169 service_reply_a(struct uloop_fd *u, int type)
176 dns_send_answer(u, service_name("local"));
180 service_reply(struct uloop_fd *u, const char *match)
184 vlist_for_each_element(&services, s, node) {
185 char *host = service_name(s->service);
186 char *service = strstr(host, "._");
188 if (!s->active || !service || !service_timeout(s))
193 if (match && strcmp(match, s->service))
197 service_send_ptr(u, s);
198 dns_send_answer(u, service);
201 service_send_srv(u, s);
202 if (s->txt && s->txt_len)
203 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
204 dns_send_answer(u, host);
212 dns_send_answer(u, service_name("local"));
216 service_announce_services(struct uloop_fd *u, const char *service)
221 if (!strcmp(service, sdudp))
223 else if (strcmp(service, sdtcp))
226 vlist_for_each_element(&services, s, node) {
227 if (!strstr(s->service, "._tcp") && tcp)
229 if (!strstr(s->service, "._udp") && !tcp)
233 service_send_ptr_c(u, s->service);
235 dns_send_answer(u, sdtcp);
237 dns_send_answer(u, sdudp);
238 service_reply(u, s->service);
243 service_announce(struct uloop_fd *u)
245 service_announce_services(u, sdudp);
246 service_announce_services(u, sdtcp);
250 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
251 struct vlist_node *node_old)
258 s = container_of(node_old, struct service, node);
263 service_load(char *path)
265 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
269 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
272 for (i = 0; i < gl.gl_pathc; i++) {
273 blob_buf_init(&b, 0);
275 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
277 blob_for_each_attr(cur, b.head, rem) {
279 char *d_service, *d_daemon;
284 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
285 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
286 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
289 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
290 txt_len += 1 + strlen(blobmsg_get_string(txt));
292 s = calloc_a(sizeof(*s),
293 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
294 &d_service, strlen(blobmsg_name(cur)) + 1,
299 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
300 s->service = strcpy(d_service, blobmsg_name(cur));
301 s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
304 s->txt_len = txt_len;
307 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
308 int len = strlen(blobmsg_get_string(txt));
315 memcpy(d_txt, blobmsg_get_string(txt), len);
319 vlist_add(&services, &s->node, s->service);
328 hostname = get_hostname();
330 vlist_update(&services);
331 service_load("/tmp/run/mdnsd/*");
332 vlist_flush(&services);
336 service_cleanup(void)
338 vlist_flush(&services);