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