0b42aeeb3c47c90bdc08c3f494044dd70df27e48
[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 <libubus.h>
28 #include <libubox/vlist.h>
29 #include <libubox/uloop.h>
30 #include <libubox/avl-cmp.h>
31 #include <libubox/blobmsg_json.h>
32
33 #include "ubus.h"
34 #include "dns.h"
35 #include "service.h"
36 #include "util.h"
37 #include "interface.h"
38 #include "announce.h"
39
40 enum {
41 SERVICE_SERVICE,
42 SERVICE_PORT,
43 SERVICE_TXT,
44 __SERVICE_MAX,
45 };
46
47 struct service {
48 struct vlist_node node;
49
50 time_t t;
51
52 const char *id;
53 const char *service;
54 const uint8_t *txt;
55 int txt_len;
56 int port;
57 int active;
58 };
59
60 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
61 [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
62 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
63 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
64 };
65
66 static void
67 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
68 struct vlist_node *node_old);
69
70 static struct blob_buf b;
71 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
72 static char *sdudp = "_services._dns-sd._udp.local";
73 static char *sdtcp = "_services._dns-sd._tcp.local";
74 static int service_init_announce;
75
76 static const char *
77 service_name(const char *domain)
78 {
79 static char buffer[256];
80
81 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
82
83 return buffer;
84 }
85
86 static void
87 service_add_ptr(const char *host, int ttl)
88 {
89 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
90
91 if (len < 1)
92 return;
93
94 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
95 }
96
97 static void
98 service_add_srv(struct service *s, int ttl)
99 {
100 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
101 int len = sizeof(*sd);
102
103 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
104 if (len <= sizeof(*sd))
105 return;
106
107 sd->port = cpu_to_be16(s->port);
108 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
109 }
110
111 #define TOUT_LOOKUP 60
112
113 static int
114 service_timeout(struct service *s)
115 {
116 time_t t = monotonic_time();
117
118 if (t - s->t <= TOUT_LOOKUP)
119 return 0;
120
121 s->t = t;
122
123 return 1;
124 }
125
126 void
127 service_reply_a(struct interface *iface, int ttl)
128 {
129 struct ifaddrs *ifap, *ifa;
130 struct sockaddr_in *sa;
131 struct sockaddr_in6 *sa6;
132
133 getifaddrs(&ifap);
134
135 dns_init_answer();
136 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
137 if (strcmp(ifa->ifa_name, iface->name))
138 continue;
139 if (ifa->ifa_addr->sa_family == AF_INET) {
140 sa = (struct sockaddr_in *) ifa->ifa_addr;
141 dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
142 }
143 if (ifa->ifa_addr->sa_family == AF_INET6) {
144 uint8_t ll_prefix[] = {0xfe, 0x80 };
145 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
146 if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
147 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
148 }
149 }
150 dns_send_answer(iface, mdns_hostname_local);
151
152 freeifaddrs(ifap);
153 }
154
155 static void
156 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
157 {
158 const char *host = service_name(s->service);
159 char *service = strstr(host, "._");
160
161 if (!force && (!s->active || !service || !service_timeout(s)))
162 return;
163
164 service++;
165
166 if (match && strcmp(match, s->service))
167 return;
168
169 dns_init_answer();
170 service_add_ptr(service_name(s->service), ttl);
171 dns_send_answer(iface, service);
172
173 dns_init_answer();
174 service_add_srv(s, ttl);
175 if (s->txt && s->txt_len)
176 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
177 dns_send_answer(iface, host);
178 }
179
180 void
181 service_reply(struct interface *iface, const char *match, int ttl)
182 {
183 struct service *s;
184
185 vlist_for_each_element(&services, s, node)
186 service_reply_single(iface, s, match, ttl, 0);
187
188 if (match)
189 return;
190
191 if (ttl)
192 service_reply_a(iface, ttl);
193 }
194
195 void
196 service_announce_services(struct interface *iface, const char *service, int ttl)
197 {
198 struct service *s;
199 int tcp = 1;
200
201 if (!strcmp(service, sdudp))
202 tcp = 0;
203 else if (strcmp(service, sdtcp))
204 return;
205
206 vlist_for_each_element(&services, s, node) {
207 if (!strstr(s->service, "._tcp") && tcp)
208 continue;
209 if (!strstr(s->service, "._udp") && !tcp)
210 continue;
211 s->t = 0;
212 if (ttl) {
213 dns_init_answer();
214 service_add_ptr(s->service, ttl);
215 if (tcp)
216 dns_send_answer(iface, sdtcp);
217 else
218 dns_send_answer(iface, sdudp);
219 }
220 service_reply(iface, s->service, ttl);
221 }
222 }
223
224 void
225 service_announce(struct interface *iface, int ttl)
226 {
227 service_announce_services(iface, sdudp, ttl);
228 service_announce_services(iface, sdtcp, ttl);
229 }
230
231 static void
232 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
233 struct vlist_node *node_old)
234 {
235 struct interface *iface;
236 struct service *s;
237
238 if (!node_old) {
239 s = container_of(node_new, struct service, node);
240 if (service_init_announce)
241 vlist_for_each_element(&interfaces, iface, node) {
242 s->t = 0;
243 service_reply_single(iface, s, NULL, announce_ttl, 1);
244 }
245 return;
246 }
247
248 s = container_of(node_old, struct service, node);
249 if (!node_new && service_init_announce)
250 vlist_for_each_element(&interfaces, iface, node)
251 service_reply_single(iface, s, NULL, 0, 1);
252 free(s);
253 }
254
255 static void
256 service_load_blob(struct blob_attr *b)
257 {
258 struct blob_attr *txt, *_tb[__SERVICE_MAX];
259 struct service *s;
260 char *d_service, *d_id;
261 uint8_t *d_txt;
262 int rem2;
263 int txt_len = 0;
264
265 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
266 _tb, blobmsg_data(b), blobmsg_data_len(b));
267 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
268 return;
269
270 if (_tb[SERVICE_SERVICE])
271 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
272 txt_len += 1 + strlen(blobmsg_get_string(txt));
273
274 s = calloc_a(sizeof(*s),
275 &d_id, strlen(blobmsg_name(b)) + 1,
276 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
277 &d_txt, txt_len);
278 if (!s)
279 return;
280
281 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
282 s->id = strcpy(d_id, blobmsg_name(b));
283 s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
284 s->active = 1;
285 s->t = 0;
286 s->txt_len = txt_len;
287 s->txt = d_txt;
288
289 if (_tb[SERVICE_SERVICE])
290 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
291 int len = strlen(blobmsg_get_string(txt));
292 if (!len)
293 return;
294 if (len > 0xff)
295 len = 0xff;
296 *d_txt = len;
297 d_txt++;
298 memcpy(d_txt, blobmsg_get_string(txt), len);
299 d_txt += len;
300 }
301
302 vlist_add(&services, &s->node, s->id);
303 }
304
305 static void
306 service_load(char *path)
307 {
308 struct blob_attr *cur;
309 glob_t gl;
310 int i, rem;
311
312 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
313 return;
314
315 for (i = 0; i < gl.gl_pathc; i++) {
316 blob_buf_init(&b, 0);
317 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
318 blob_for_each_attr(cur, b.head, rem)
319 service_load_blob(cur);
320 }
321 globfree(&gl);
322 }
323
324 static void
325 service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
326 {
327 struct blob_attr *cur;
328 int rem;
329
330 get_hostname();
331
332 vlist_update(&services);
333 service_load("/tmp/run/mdns/*");
334
335 blob_for_each_attr(cur, msg, rem) {
336 struct blob_attr *cur2;
337 int rem2;
338
339 blobmsg_for_each_attr(cur2, cur, rem2) {
340 struct blob_attr *cur3;
341 int rem3;
342
343 if (strcmp(blobmsg_name(cur2), "instances"))
344 continue;
345
346 blobmsg_for_each_attr(cur3, cur2, rem3) {
347 struct blob_attr *cur4;
348 int rem4;
349 int running = 0;
350
351 blobmsg_for_each_attr(cur4, cur3, rem4) {
352 const char *name = blobmsg_name(cur4);
353
354 if (!strcmp(name, "running")) {
355 running = blobmsg_get_bool(cur4);
356 } else if (running && !strcmp(name, "data")) {
357 struct blob_attr *cur5;
358 int rem5;
359
360 blobmsg_for_each_attr(cur5, cur4, rem5) {
361 struct blob_attr *cur6;
362 int rem6;
363
364 if (strcmp(blobmsg_name(cur5), "mdns"))
365 continue;
366
367 blobmsg_for_each_attr(cur6, cur5, rem6)
368 service_load_blob(cur6);
369 }
370 break;
371 }
372 }
373 }
374 }
375 }
376 vlist_flush(&services);
377 }
378
379 void
380 service_init(int announce)
381 {
382 get_hostname();
383
384 service_init_announce = announce;
385 ubus_service_list(service_init_cb);
386 }
387
388 void
389 service_cleanup(void)
390 {
391 vlist_flush(&services);
392 blob_buf_free(&b);
393 }