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