fe6e5a91d31bd3475e47b3775db3e3474b848de6
[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 int
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         s->t = t;
119
120         return 1;
121 }
122
123 void
124 service_reply_a(struct interface *iface, int ttl)
125 {
126         struct ifaddrs *ifap, *ifa;
127         struct sockaddr_in *sa;
128         struct sockaddr_in6 *sa6;
129
130         getifaddrs(&ifap);
131
132         dns_init_answer();
133         for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
134                 if (strcmp(ifa->ifa_name, iface->name))
135                         continue;
136                 if (ifa->ifa_addr->sa_family == AF_INET) {
137                         sa = (struct sockaddr_in *) ifa->ifa_addr;
138                         dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
139                 }
140                 if (ifa->ifa_addr->sa_family == AF_INET6) {
141                         uint8_t ll_prefix[] = {0xfe, 0x80 };
142                         sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
143                         if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
144                                 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
145                 }
146         }
147         dns_send_answer(iface, mdns_hostname_local);
148
149         freeifaddrs(ifap);
150 }
151
152 static void
153 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
154 {
155         const char *host = service_name(s->service);
156         char *service = strstr(host, "._");
157
158         if (!force && (!s->active || !service || !service_timeout(s)))
159                 return;
160
161         service++;
162
163         if (match && strcmp(match, s->service))
164                 return;
165
166         dns_init_answer();
167         service_add_ptr(service_name(s->service), ttl);
168         dns_send_answer(iface, service);
169
170         dns_init_answer();
171         service_add_srv(s, ttl);
172         if (s->txt && s->txt_len)
173                 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
174         dns_send_answer(iface, host);
175 }
176
177 void
178 service_reply(struct interface *iface, const char *match, int ttl)
179 {
180         struct service *s;
181
182         vlist_for_each_element(&services, s, node)
183                 service_reply_single(iface, s, match, ttl, 0);
184
185         if (match)
186                 return;
187
188         if (ttl)
189                 service_reply_a(iface, ttl);
190 }
191
192 void
193 service_announce_services(struct interface *iface, const char *service, int ttl)
194 {
195         struct service *s;
196         int tcp = 1;
197
198         if (!strcmp(service, sdudp))
199                 tcp = 0;
200         else if (strcmp(service, sdtcp))
201                 return;
202
203         vlist_for_each_element(&services, s, node) {
204                 if (!strstr(s->service, "._tcp") && tcp)
205                         continue;
206                 if (!strstr(s->service, "._udp") && !tcp)
207                         continue;
208                 s->t = 0;
209                 if (ttl) {
210                         dns_init_answer();
211                         service_add_ptr(s->service, ttl);
212                         if (tcp)
213                                 dns_send_answer(iface, sdtcp);
214                         else
215                                 dns_send_answer(iface, sdudp);
216                 }
217                 service_reply(iface, s->service, ttl);
218         }
219 }
220
221 void
222 service_announce(struct interface *iface, int ttl)
223 {
224         service_announce_services(iface, sdudp, ttl);
225         service_announce_services(iface, sdtcp, ttl);
226 }
227
228 static void
229 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
230                struct vlist_node *node_old)
231 {
232         struct interface *iface;
233         struct service *s;
234
235         if (!node_old) {
236                 s = container_of(node_new, struct service, node);
237                 if (service_init_announce)
238                         vlist_for_each_element(&interfaces, iface, node) {
239                                 s->t = 0;
240                                 service_reply_single(iface, s, NULL, announce_ttl, 1);
241                         }
242                 return;
243         }
244
245         s = container_of(node_old, struct service, node);
246         if (!node_new && service_init_announce)
247                 vlist_for_each_element(&interfaces, iface, node)
248                         service_reply_single(iface, s, NULL, 0, 1);
249         free(s);
250 }
251
252 static void
253 service_load_blob(struct blob_attr *b)
254 {
255         struct blob_attr *txt, *_tb[__SERVICE_MAX];
256         struct service *s;
257         char *d_service, *d_id;
258         uint8_t *d_txt;
259         int rem2;
260         int txt_len = 0;
261
262         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
263                 _tb, blobmsg_data(b), blobmsg_data_len(b));
264         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
265                 return;
266
267         if (_tb[SERVICE_SERVICE])
268                 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
269                         txt_len += 1 + strlen(blobmsg_get_string(txt));
270
271         s = calloc_a(sizeof(*s),
272                 &d_id, strlen(blobmsg_name(b)) + 1,
273                 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
274                 &d_txt, txt_len);
275         if (!s)
276                 return;
277
278         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
279         s->id = strcpy(d_id, blobmsg_name(b));
280         s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
281         s->active = 1;
282         s->t = 0;
283         s->txt_len = txt_len;
284         s->txt = d_txt;
285
286         if (_tb[SERVICE_SERVICE])
287                 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
288                         int len = strlen(blobmsg_get_string(txt));
289                         if (!len)
290                                 return;
291                         if (len > 0xff)
292                                 len = 0xff;
293                         *d_txt = len;
294                         d_txt++;
295                         memcpy(d_txt, blobmsg_get_string(txt), len);
296                         d_txt += len;
297                 }
298
299         vlist_add(&services, &s->node, s->id);
300 }
301
302 static void
303 service_load(char *path)
304 {
305         struct blob_attr *cur;
306         glob_t gl;
307         int i, rem;
308
309         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
310                 return;
311
312         for (i = 0; i < gl.gl_pathc; i++) {
313                 blob_buf_init(&b, 0);
314                 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
315                         blob_for_each_attr(cur, b.head, rem)
316                                 service_load_blob(cur);
317         }
318         globfree(&gl);
319 }
320
321 static void
322 service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
323 {
324         struct blob_attr *cur;
325         int rem;
326
327         get_hostname();
328
329         vlist_update(&services);
330         service_load("/tmp/run/mdns/*");
331
332         blob_for_each_attr(cur, msg, rem) {
333                 struct blob_attr *cur2;
334                 int rem2;
335
336                 blobmsg_for_each_attr(cur2, cur, rem2) {
337                         struct blob_attr *cur3;
338                         int rem3;
339
340                         if (strcmp(blobmsg_name(cur2), "instances"))
341                                 continue;
342
343                         blobmsg_for_each_attr(cur3, cur2, rem3) {
344                                 struct blob_attr *cur4;
345                                 int rem4;
346                                 int running = 0;
347
348                                 blobmsg_for_each_attr(cur4, cur3, rem4) {
349                                         const char *name = blobmsg_name(cur4);
350
351                                         if (!strcmp(name, "running")) {
352                                                 running = blobmsg_get_bool(cur4);
353                                         } else if (running && !strcmp(name, "data")) {
354                                                 struct blob_attr *cur5;
355                                                 int rem5;
356
357                                                 blobmsg_for_each_attr(cur5, cur4, rem5) {
358                                                         struct blob_attr *cur6;
359                                                         int rem6;
360
361                                                         if (strcmp(blobmsg_name(cur5), "mdns"))
362                                                                 continue;
363
364                                                         blobmsg_for_each_attr(cur6, cur5, rem6)
365                                                                 service_load_blob(cur6);
366                                                 }
367                                                 break;
368                                         }
369                                 }
370                         }
371                 }
372         }
373         vlist_flush(&services);
374 }
375
376 void
377 service_init(int announce)
378 {
379         get_hostname();
380
381         service_init_announce = announce;
382         ubus_service_list(service_init_cb);
383 }
384
385 void
386 service_cleanup(void)
387 {
388         vlist_flush(&services);
389         blob_buf_free(&b);
390 }