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>
27 #include <libubox/vlist.h>
28 #include <libubox/uloop.h>
29 #include <libubox/avl-cmp.h>
30 #include <libubox/blobmsg_json.h>
35 #include "interface.h"
46 struct vlist_node node;
58 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
59 [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
60 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
61 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
65 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
66 struct vlist_node *node_old);
68 static struct blob_buf b;
69 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
70 static char *sdudp = "_services._dns-sd._udp.local";
71 static char *sdtcp = "_services._dns-sd._tcp.local";
72 static int service_init_announce;
75 service_name(const char *domain)
77 static char buffer[256];
79 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
85 service_add_ptr(const char *host, int ttl)
87 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
92 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
96 service_add_srv(struct service *s, int ttl)
98 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
99 int len = sizeof(*sd);
101 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
102 if (len <= sizeof(*sd))
105 sd->port = cpu_to_be16(s->port);
106 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
109 #define TOUT_LOOKUP 60
112 service_timeout(struct service *s)
114 time_t t = time(NULL);
116 if (t - s->t <= TOUT_LOOKUP)
125 service_reply_a(struct interface *iface, int type, int ttl)
127 struct ifaddrs *ifap, *ifa;
128 struct sockaddr_in *sa;
129 struct sockaddr_in6 *sa6;
134 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
135 if (strcmp(ifa->ifa_name, iface->name))
137 if (ifa->ifa_addr->sa_family==AF_INET) {
138 sa = (struct sockaddr_in *) ifa->ifa_addr;
139 dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
141 if (ifa->ifa_addr->sa_family==AF_INET6) {
142 uint8_t ll_prefix[] = {0xfe, 0x80 };
143 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
144 if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
145 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
148 dns_send_answer(iface, mdns_hostname_local);
154 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
156 const char *host = service_name(s->service);
157 char *service = strstr(host, "._");
159 if (!force && (!s->active || !service || !service_timeout(s)))
164 if (match && strcmp(match, s->service))
168 service_add_ptr(service_name(s->service), ttl);
169 dns_send_answer(iface, service);
172 service_add_srv(s, ttl);
173 if (s->txt && s->txt_len)
174 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
175 dns_send_answer(iface, host);
179 service_reply(struct interface *iface, const char *match, int ttl)
183 vlist_for_each_element(&services, s, node)
184 service_reply_single(iface, s, match, ttl, 0);
189 service_reply_a(iface, TYPE_A, ttl);
193 service_announce_services(struct interface *iface, const char *service)
198 if (!strcmp(service, sdudp))
200 else if (strcmp(service, sdtcp))
203 vlist_for_each_element(&services, s, node) {
204 if (!strstr(s->service, "._tcp") && tcp)
206 if (!strstr(s->service, "._udp") && !tcp)
210 service_add_ptr(s->service, announce_ttl);
212 dns_send_answer(iface, sdtcp);
214 dns_send_answer(iface, sdudp);
215 service_reply(iface, s->service, announce_ttl);
220 service_announce(struct interface *iface)
222 service_announce_services(iface, sdudp);
223 service_announce_services(iface, sdtcp);
227 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
228 struct vlist_node *node_old)
230 struct interface *iface;
234 s = container_of(node_new, struct service, node);
235 if (service_init_announce)
236 vlist_for_each_element(&interfaces, iface, node) {
238 service_reply_single(iface, s, NULL, announce_ttl, 1);
243 s = container_of(node_old, struct service, node);
244 if (!node_new && service_init_announce)
245 vlist_for_each_element(&interfaces, iface, node)
246 service_reply_single(iface, s, NULL, 0, 1);
251 service_load_blob(struct blob_attr *b)
253 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
256 blob_for_each_attr(cur, b, rem) {
258 char *d_service, *d_id;
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_SERVICE])
268 if (_tb[SERVICE_SERVICE])
269 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
270 txt_len += 1 + strlen(blobmsg_get_string(txt));
272 s = calloc_a(sizeof(*s),
273 &d_id, strlen(blobmsg_name(cur)) + 1,
274 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
279 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
280 s->id = strcpy(d_id, blobmsg_name(cur));
281 s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
284 s->txt_len = txt_len;
287 if (_tb[SERVICE_SERVICE])
288 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
289 int len = strlen(blobmsg_get_string(txt));
296 memcpy(d_txt, blobmsg_get_string(txt), len);
300 vlist_add(&services, &s->node, s->id);
305 service_load(char *path)
310 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
313 for (i = 0; i < gl.gl_pathc; i++) {
314 blob_buf_init(&b, 0);
315 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
316 service_load_blob(b.head);
322 service_init(int announce)
324 service_init_announce = announce;
328 vlist_update(&services);
329 service_load("/tmp/run/mdnsd/*");
330 vlist_flush(&services);
334 service_cleanup(void)
336 vlist_flush(&services);