a3a5bb49be452382d803b56d3a315f923f1faf85
[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 <resolv.h>
19 #include <glob.h>
20 #include <stdio.h>
21 #include <time.h>
22
23 #include <libubus.h>
24 #include <libubox/vlist.h>
25 #include <libubox/uloop.h>
26 #include <libubox/avl-cmp.h>
27 #include <libubox/blobmsg_json.h>
28
29 #include "ubus.h"
30 #include "dns.h"
31 #include "service.h"
32 #include "util.h"
33 #include "interface.h"
34 #include "announce.h"
35
36 enum {
37         SERVICE_SERVICE,
38         SERVICE_PORT,
39         SERVICE_TXT,
40         __SERVICE_MAX,
41 };
42
43 struct service {
44         struct vlist_node node;
45
46         time_t t;
47
48         const char *id;
49         const char *service;
50         const uint8_t *txt;
51         int txt_len;
52         int port;
53         int active;
54 };
55
56 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
57         [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
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 time_t
110 service_timeout(struct service *s)
111 {
112         time_t t = monotonic_time();
113
114         if (t - s->t <= TOUT_LOOKUP)
115                 return 0;
116
117         return t;
118 }
119
120 static void
121 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
122 {
123         const char *host = service_name(s->service);
124         char *service = strstr(host, "._");
125         time_t t = service_timeout(s);
126
127
128         if (!force && (!s->active || !service || !t))
129                 return;
130
131         service++;
132
133         if (match && strcmp(match, s->service))
134                 return;
135
136         s->t = t;
137
138         dns_init_answer();
139         service_add_ptr(service_name(s->service), ttl);
140         dns_send_answer(iface, service);
141
142         dns_init_answer();
143         service_add_srv(s, ttl);
144         if (s->txt && s->txt_len)
145                 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
146         dns_send_answer(iface, host);
147 }
148
149 void
150 service_reply(struct interface *iface, const char *match, int ttl)
151 {
152         struct service *s;
153
154         vlist_for_each_element(&services, s, node)
155                 service_reply_single(iface, s, match, ttl, 0);
156
157         if (match)
158                 return;
159
160         if (ttl)
161                 dns_reply_a(iface, ttl);
162 }
163
164 void
165 service_announce_services(struct interface *iface, const char *service, int ttl)
166 {
167         struct service *s;
168         int tcp = 1;
169
170         if (!strcmp(service, sdudp))
171                 tcp = 0;
172         else if (strcmp(service, sdtcp))
173                 return;
174
175         vlist_for_each_element(&services, s, node) {
176                 if (!strstr(s->service, "._tcp") && tcp)
177                         continue;
178                 if (!strstr(s->service, "._udp") && !tcp)
179                         continue;
180                 s->t = 0;
181                 if (ttl) {
182                         dns_init_answer();
183                         service_add_ptr(s->service, ttl);
184                         if (tcp)
185                                 dns_send_answer(iface, sdtcp);
186                         else
187                                 dns_send_answer(iface, sdudp);
188                 }
189                 service_reply(iface, s->service, ttl);
190         }
191 }
192
193 void
194 service_announce(struct interface *iface, int ttl)
195 {
196         service_announce_services(iface, sdudp, ttl);
197         service_announce_services(iface, sdtcp, ttl);
198 }
199
200 static void
201 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
202                struct vlist_node *node_old)
203 {
204         struct interface *iface;
205         struct service *s;
206
207         if (!node_old) {
208                 s = container_of(node_new, struct service, node);
209                 if (service_init_announce)
210                         vlist_for_each_element(&interfaces, iface, node) {
211                                 s->t = 0;
212                                 service_reply_single(iface, s, NULL, announce_ttl, 1);
213                         }
214                 return;
215         }
216
217         s = container_of(node_old, struct service, node);
218         if (!node_new && service_init_announce)
219                 vlist_for_each_element(&interfaces, iface, node)
220                         service_reply_single(iface, s, NULL, 0, 1);
221         free(s);
222 }
223
224 static void
225 service_load_blob(struct blob_attr *b)
226 {
227         struct blob_attr *txt, *_tb[__SERVICE_MAX];
228         struct service *s;
229         char *d_service, *d_id;
230         uint8_t *d_txt;
231         int rem2;
232         int txt_len = 0;
233
234         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
235                 _tb, blobmsg_data(b), blobmsg_data_len(b));
236         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
237                 return;
238
239         if (_tb[SERVICE_SERVICE])
240                 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
241                         txt_len += 1 + strlen(blobmsg_get_string(txt));
242
243         s = calloc_a(sizeof(*s),
244                 &d_id, strlen(blobmsg_name(b)) + 1,
245                 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
246                 &d_txt, txt_len);
247         if (!s)
248                 return;
249
250         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
251         s->id = strcpy(d_id, blobmsg_name(b));
252         s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
253         s->active = 1;
254         s->t = 0;
255         s->txt_len = txt_len;
256         s->txt = d_txt;
257
258         if (_tb[SERVICE_SERVICE])
259                 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
260                         int len = strlen(blobmsg_get_string(txt));
261                         if (!len)
262                                 return;
263                         if (len > 0xff)
264                                 len = 0xff;
265                         *d_txt = len;
266                         d_txt++;
267                         memcpy(d_txt, blobmsg_get_string(txt), len);
268                         d_txt += len;
269                 }
270
271         vlist_add(&services, &s->node, s->id);
272 }
273
274 static void
275 service_load(char *path)
276 {
277         struct blob_attr *cur;
278         glob_t gl;
279         int i, rem;
280
281         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
282                 return;
283
284         for (i = 0; i < gl.gl_pathc; i++) {
285                 blob_buf_init(&b, 0);
286                 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
287                         blob_for_each_attr(cur, b.head, rem)
288                                 service_load_blob(cur);
289         }
290         globfree(&gl);
291 }
292
293 static void
294 service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
295 {
296         struct blob_attr *cur;
297         int rem;
298
299         get_hostname();
300
301         vlist_update(&services);
302         service_load("/tmp/run/mdns/*");
303
304         blob_for_each_attr(cur, msg, rem) {
305                 struct blob_attr *cur2;
306                 int rem2;
307
308                 blobmsg_for_each_attr(cur2, cur, rem2) {
309                         struct blob_attr *cur3;
310                         int rem3;
311
312                         if (strcmp(blobmsg_name(cur2), "instances"))
313                                 continue;
314
315                         blobmsg_for_each_attr(cur3, cur2, rem3) {
316                                 struct blob_attr *cur4;
317                                 int rem4;
318                                 int running = 0;
319
320                                 blobmsg_for_each_attr(cur4, cur3, rem4) {
321                                         const char *name = blobmsg_name(cur4);
322
323                                         if (!strcmp(name, "running")) {
324                                                 running = blobmsg_get_bool(cur4);
325                                         } else if (running && !strcmp(name, "data")) {
326                                                 struct blob_attr *cur5;
327                                                 int rem5;
328
329                                                 blobmsg_for_each_attr(cur5, cur4, rem5) {
330                                                         struct blob_attr *cur6;
331                                                         int rem6;
332
333                                                         if (strcmp(blobmsg_name(cur5), "mdns"))
334                                                                 continue;
335
336                                                         blobmsg_for_each_attr(cur6, cur5, rem6)
337                                                                 service_load_blob(cur6);
338                                                 }
339                                                 break;
340                                         }
341                                 }
342                         }
343                 }
344         }
345         vlist_flush(&services);
346 }
347
348 void
349 service_init(int announce)
350 {
351         get_hostname();
352
353         service_init_announce = announce;
354         ubus_service_list(service_init_cb);
355 }
356
357 void
358 service_cleanup(void)
359 {
360         vlist_flush(&services);
361         blob_buf_free(&b);
362 }