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"
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 },
63 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
64 struct vlist_node *node_old);
66 static struct blob_buf b;
67 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
68 static char *sdudp = "_services._dns-sd._udp.local";
69 static char *sdtcp = "_services._dns-sd._tcp.local";
72 service_name(const char *domain)
74 static char buffer[256];
76 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
82 service_add_ptr(const char *host)
84 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
89 dns_add_answer(TYPE_PTR, mdns_buf, len, announce_ttl);
93 service_add_srv(struct service *s)
95 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
96 int len = sizeof(*sd);
98 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
99 if (len <= sizeof(*sd))
102 sd->port = cpu_to_be16(s->port);
103 dns_add_answer(TYPE_SRV, mdns_buf, len, announce_ttl);
106 #define TOUT_LOOKUP 60
109 service_timeout(struct service *s)
111 time_t t = time(NULL);
113 if (t - s->t <= TOUT_LOOKUP)
122 service_reply_a(struct interface *iface, int type)
124 struct ifaddrs *ifap, *ifa;
125 struct sockaddr_in *sa;
126 struct sockaddr_in6 *sa6;
132 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
133 if (strcmp(ifa->ifa_name, iface->name))
135 if (ifa->ifa_addr->sa_family==AF_INET) {
136 sa = (struct sockaddr_in *) ifa->ifa_addr;
137 addr = inet_ntoa(sa->sin_addr);
138 printf("Interface: %s\tAddress4: %s\n", ifa->ifa_name, addr);
139 dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, announce_ttl);
141 if (ifa->ifa_addr->sa_family==AF_INET6) {
142 uint8_t ll_prefix[] = {0xfe, 0x80 };
143 char buf[64] = { 0 };
144 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
145 if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2)) {
146 if (inet_ntop(AF_INET6, &sa6->sin6_addr, buf, 64))
147 printf("Interface: %s\tAddress6: %s\n", ifa->ifa_name, buf);
148 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, announce_ttl);
152 dns_send_answer(iface, mdns_hostname_local);
158 service_reply(struct interface *iface, const char *match)
162 vlist_for_each_element(&services, s, node) {
163 const char *host = service_name(s->service);
164 char *service = strstr(host, "._");
166 if (!s->active || !service || !service_timeout(s))
171 if (match && strcmp(match, s->service))
175 service_add_ptr(service_name(s->service));
176 dns_send_answer(iface, service);
180 if (s->txt && s->txt_len)
181 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, announce_ttl);
182 dns_send_answer(iface, host);
188 service_reply_a(iface, TYPE_A);
192 service_announce_services(struct interface *iface, const char *service)
197 if (!strcmp(service, sdudp))
199 else if (strcmp(service, sdtcp))
202 vlist_for_each_element(&services, s, node) {
203 if (!strstr(s->service, "._tcp") && tcp)
205 if (!strstr(s->service, "._udp") && !tcp)
209 service_add_ptr(s->service);
211 dns_send_answer(iface, sdtcp);
213 dns_send_answer(iface, sdudp);
214 service_reply(iface, s->service);
219 service_announce(struct interface *iface)
221 service_announce_services(iface, sdudp);
222 service_announce_services(iface, sdtcp);
226 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
227 struct vlist_node *node_old)
234 s = container_of(node_old, struct service, node);
239 service_load(char *path)
241 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
245 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
248 for (i = 0; i < gl.gl_pathc; i++) {
249 blob_buf_init(&b, 0);
251 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
253 blob_for_each_attr(cur, b.head, rem) {
255 char *d_service, *d_daemon;
260 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
261 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
262 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
265 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
266 txt_len += 1 + strlen(blobmsg_get_string(txt));
268 s = calloc_a(sizeof(*s),
269 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
270 &d_service, strlen(blobmsg_name(cur)) + 1,
275 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
276 s->service = strcpy(d_service, blobmsg_name(cur));
277 s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
280 s->txt_len = txt_len;
283 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
284 int len = strlen(blobmsg_get_string(txt));
291 memcpy(d_txt, blobmsg_get_string(txt), len);
295 vlist_add(&services, &s->node, s->service);
305 vlist_update(&services);
306 service_load("/tmp/run/mdnsd/*");
307 vlist_flush(&services);
311 service_cleanup(void)
313 vlist_flush(&services);