610cac5b7a792890634e6f451475842873b55bec
[project/mdnsd.git] / service.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
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
7 *
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.
12 */
13
14 #include <sys/types.h>
15 #include <arpa/nameser.h>
16 #include <sys/socket.h>
17
18 #include <ifaddrs.h>
19 #include <resolv.h>
20 #include <glob.h>
21 #include <stdio.h>
22 #include <time.h>
23
24 #include <uci.h>
25 #include <uci_blob.h>
26
27 #include <libubox/vlist.h>
28 #include <libubox/uloop.h>
29 #include <libubox/avl-cmp.h>
30 #include <libubox/blobmsg_json.h>
31
32 #include "dns.h"
33 #include "service.h"
34 #include "util.h"
35 #include "interface.h"
36 #include "announce.h"
37
38 enum {
39 SERVICE_PORT,
40 SERVICE_TXT,
41 __SERVICE_MAX,
42 };
43
44 struct service {
45 struct vlist_node node;
46
47 time_t t;
48
49 const char *service;
50 const char *daemon;
51 const uint8_t *txt;
52 int txt_len;
53 int port;
54 int active;
55 };
56
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 },
60 };
61
62 static void
63 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
64 struct vlist_node *node_old);
65
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";
70 static int service_init_announce;
71
72 static const char *
73 service_name(const char *domain)
74 {
75 static char buffer[256];
76
77 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
78
79 return buffer;
80 }
81
82 static void
83 service_add_ptr(const char *host, int ttl)
84 {
85 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
86
87 if (len < 1)
88 return;
89
90 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
91 }
92
93 static void
94 service_add_srv(struct service *s, int ttl)
95 {
96 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
97 int len = sizeof(*sd);
98
99 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
100 if (len <= sizeof(*sd))
101 return;
102
103 sd->port = cpu_to_be16(s->port);
104 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
105 }
106
107 #define TOUT_LOOKUP 60
108
109 static int
110 service_timeout(struct service *s)
111 {
112 time_t t = time(NULL);
113
114 if (t - s->t <= TOUT_LOOKUP)
115 return 0;
116
117 s->t = t;
118
119 return 1;
120 }
121
122 void
123 service_reply_a(struct interface *iface, int type, int ttl)
124 {
125 struct ifaddrs *ifap, *ifa;
126 struct sockaddr_in *sa;
127 struct sockaddr_in6 *sa6;
128
129 getifaddrs(&ifap);
130
131 dns_init_answer();
132 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
133 if (strcmp(ifa->ifa_name, iface->name))
134 continue;
135 if (ifa->ifa_addr->sa_family==AF_INET) {
136 sa = (struct sockaddr_in *) ifa->ifa_addr;
137 dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
138 }
139 if (ifa->ifa_addr->sa_family==AF_INET6) {
140 uint8_t ll_prefix[] = {0xfe, 0x80 };
141 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
142 if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
143 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
144 }
145 }
146 dns_send_answer(iface, mdns_hostname_local);
147
148 freeifaddrs(ifap);
149 }
150
151 static void
152 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
153 {
154 const char *host = service_name(s->service);
155 char *service = strstr(host, "._");
156
157 if (!force && (!s->active || !service || !service_timeout(s)))
158 return;
159
160 service++;
161
162 if (match && strcmp(match, s->service))
163 return;
164
165 dns_init_answer();
166 service_add_ptr(service_name(s->service), ttl);
167 dns_send_answer(iface, service);
168
169 dns_init_answer();
170 service_add_srv(s, ttl);
171 if (s->txt && s->txt_len)
172 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
173 dns_send_answer(iface, host);
174 }
175
176 void
177 service_reply(struct interface *iface, const char *match, int ttl)
178 {
179 struct service *s;
180
181 vlist_for_each_element(&services, s, node)
182 service_reply_single(iface, s, match, ttl, 0);
183
184 if (match)
185 return;
186
187 service_reply_a(iface, TYPE_A, ttl);
188 }
189
190 void
191 service_announce_services(struct interface *iface, const char *service)
192 {
193 struct service *s;
194 int tcp = 1;
195
196 if (!strcmp(service, sdudp))
197 tcp = 0;
198 else if (strcmp(service, sdtcp))
199 return;
200
201 vlist_for_each_element(&services, s, node) {
202 if (!strstr(s->service, "._tcp") && tcp)
203 continue;
204 if (!strstr(s->service, "._udp") && !tcp)
205 continue;
206 s->t = 0;
207 dns_init_answer();
208 service_add_ptr(s->service, announce_ttl);
209 if (tcp)
210 dns_send_answer(iface, sdtcp);
211 else
212 dns_send_answer(iface, sdudp);
213 service_reply(iface, s->service, announce_ttl);
214 }
215 }
216
217 void
218 service_announce(struct interface *iface)
219 {
220 service_announce_services(iface, sdudp);
221 service_announce_services(iface, sdtcp);
222 }
223
224 static void
225 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
226 struct vlist_node *node_old)
227 {
228 struct interface *iface;
229 struct service *s;
230
231 if (!node_old) {
232 s = container_of(node_new, struct service, node);
233 if (service_init_announce)
234 vlist_for_each_element(&interfaces, iface, node) {
235 s->t = 0;
236 service_reply_single(iface, s, NULL, announce_ttl, 1);
237 }
238 return;
239 }
240
241 s = container_of(node_old, struct service, node);
242 if (!node_new && service_init_announce)
243 vlist_for_each_element(&interfaces, iface, node)
244 service_reply_single(iface, s, NULL, 0, 1);
245 free(s);
246 }
247
248 static void
249 service_load(char *path)
250 {
251 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
252 int rem, i;
253 glob_t gl;
254
255 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
256 return;
257
258 for (i = 0; i < gl.gl_pathc; i++) {
259 blob_buf_init(&b, 0);
260
261 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
262 continue;
263 blob_for_each_attr(cur, b.head, rem) {
264 struct service *s;
265 char *d_service, *d_daemon;
266 uint8_t *d_txt;
267 int rem2;
268 int txt_len = 0;
269
270 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
271 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
272 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
273 continue;
274
275 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
276 txt_len += 1 + strlen(blobmsg_get_string(txt));
277
278 s = calloc_a(sizeof(*s),
279 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
280 &d_service, strlen(blobmsg_name(cur)) + 1,
281 &d_txt, txt_len);
282 if (!s)
283 continue;
284
285 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
286 s->service = strcpy(d_service, blobmsg_name(cur));
287 s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
288 s->active = 1;
289 s->t = 0;
290 s->txt_len = txt_len;
291 s->txt = d_txt;
292
293 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
294 int len = strlen(blobmsg_get_string(txt));
295 if (!len)
296 continue;
297 if (len > 0xff)
298 len = 0xff;
299 *d_txt = len;
300 d_txt++;
301 memcpy(d_txt, blobmsg_get_string(txt), len);
302 d_txt += len;
303 }
304
305 vlist_add(&services, &s->node, s->service);
306 }
307 }
308 }
309
310 void
311 service_init(int announce)
312 {
313 service_init_announce = announce;
314
315 get_hostname();
316
317 vlist_update(&services);
318 service_load("/tmp/run/mdnsd/*");
319 vlist_flush(&services);
320 }
321
322 void
323 service_cleanup(void)
324 {
325 vlist_flush(&services);
326 blob_buf_free(&b);
327 }