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/avl.h>
30 #include <libubox/uloop.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/blobmsg_json.h>
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,
67 static struct blob_buf b;
68 static struct avl_tree services;
69 char *hostname = NULL;
70 static char *sdudp = "_services._dns-sd._udp.local";
71 static char *sdtcp = "_services._dns-sd._tcp.local";
74 service_name(const char *domain)
76 static char buffer[256];
78 snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
84 service_send_ptr(struct uloop_fd *u, struct service *s)
86 unsigned char buffer[MAX_NAME_LEN];
87 const char *host = service_name(s->service);
88 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
93 dns_add_answer(TYPE_PTR, buffer, len);
97 service_send_ptr_c(struct uloop_fd *u, const char *host)
99 unsigned char buffer[MAX_NAME_LEN];
100 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
105 dns_add_answer(TYPE_PTR, buffer, len);
109 service_send_a(struct uloop_fd *u)
111 unsigned char buffer[MAX_NAME_LEN];
112 char *host = service_name("local");
113 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
116 if (!inet_aton(iface_ip, &in)) {
117 fprintf(stderr, "%s is not valid\n", iface_ip);
124 dns_add_answer(TYPE_A, (uint8_t *) &in.s_addr, 4);
128 service_send_srv(struct uloop_fd *u, struct service *s)
130 unsigned char buffer[MAX_NAME_LEN];
131 struct dns_srv_data *sd;
132 char *host = service_name("local");
133 int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
138 sd = calloc(1, len + sizeof(struct dns_srv_data));
142 sd->port = cpu_to_be16(s->port);
143 memcpy(&sd[1], buffer, len);
144 host = service_name(s->service);
145 dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
149 #define TOUT_LOOKUP 60
152 service_timeout(struct service *s)
154 time_t t = time(NULL);
156 if (t - s->t <= TOUT_LOOKUP)
165 service_reply_a(struct uloop_fd *u, int type)
172 dns_send_answer(u, service_name("local"));
176 service_reply(struct uloop_fd *u, const char *match)
180 avl_for_each_element(&services, s, avl) {
181 char *host = service_name(s->service);
182 char *service = strstr(host, "._");
184 if (!s->active || !service || !service_timeout(s))
189 if (match && strcmp(match, s->service))
193 service_send_ptr(u, s);
194 dns_send_answer(u, service);
197 service_send_srv(u, s);
198 if (s->txt && s->txt_len)
199 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
200 dns_send_answer(u, host);
208 dns_send_answer(u, service_name("local"));
212 service_announce_services(struct uloop_fd *u, const char *service)
217 if (!strcmp(service, sdudp))
219 else if (strcmp(service, sdtcp))
222 avl_for_each_element(&services, s, avl) {
223 if (!strstr(s->service, "._tcp") && tcp)
225 if (!strstr(s->service, "._udp") && !tcp)
229 service_send_ptr_c(u, s->service);
231 dns_send_answer(u, sdtcp);
233 dns_send_answer(u, sdudp);
234 service_reply(u, s->service);
239 service_announce(struct uloop_fd *u)
241 service_announce_services(u, sdudp);
242 service_announce_services(u, sdtcp);
246 service_load(char *path)
248 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
252 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
255 for (i = 0; i < gl.gl_pathc; i++) {
256 blob_buf_init(&b, 0);
258 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
260 blob_for_each_attr(cur, b.head, rem) {
262 char *d_service, *d_daemon;
267 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
268 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
269 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
272 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
273 txt_len += 1 + strlen(blobmsg_get_string(txt));
275 s = calloc_a(sizeof(*s),
276 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
277 &d_service, strlen(blobmsg_name(cur)) + 1,
282 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
283 s->service = strcpy(d_service, blobmsg_name(cur));
284 s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
285 s->avl.key = s->service;
288 s->txt_len = txt_len;
290 avl_insert(&services, &s->avl);
295 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
296 int len = strlen(blobmsg_get_string(txt));
303 memcpy(d_txt, blobmsg_get_string(txt), len);
314 hostname = get_hostname();
315 avl_init(&services, avl_strcmp, true, NULL);
316 service_load("/tmp/run/mdnsd/*");
320 service_cleanup(void)