c8187de408726dbb8e43466d3c0b6e4f504b9400
[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 int service_init_announce;
69
70 /**
71  * service_instance_name - construct Service Instance Name as in RFC 6763
72  *
73  * RFC 6763 specifies Service Instance Names in the following way:
74  *
75  * Service Instance Name = <Instance> . <Service> . <Domain>
76  *
77  * @service_domain: service name (a pair of labels) with domain name appended
78  */
79 static const char *
80 service_instance_name(const char *service_domain)
81 {
82         static char buffer[256];
83
84         snprintf(buffer, sizeof(buffer), "%s.%s", umdns_host_label, service_domain);
85
86         return buffer;
87 }
88
89 static void
90 service_add_ptr(const char *host, int ttl)
91 {
92         int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
93
94         if (len < 1)
95                 return;
96
97         dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
98 }
99
100 static void
101 service_add_srv(struct service *s, int ttl)
102 {
103         struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
104         int len = sizeof(*sd);
105
106         len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
107         if (len <= sizeof(*sd))
108                 return;
109
110         sd->port = cpu_to_be16(s->port);
111         dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
112 }
113
114 #define TOUT_LOOKUP     60
115
116 static time_t
117 service_timeout(struct service *s)
118 {
119         time_t t = monotonic_time();
120
121         if (t - s->t <= TOUT_LOOKUP)
122                 return 0;
123
124         return t;
125 }
126
127 static void
128 service_reply_single(struct interface *iface, struct sockaddr *to, struct service *s, int ttl, int force)
129 {
130         const char *host = service_instance_name(s->service);
131         char *service = strstr(host, "._");
132         time_t t = service_timeout(s);
133
134
135         if (!force && (!s->active || !service || !t))
136                 return;
137
138         service++;
139
140         s->t = t;
141
142         dns_init_answer();
143         service_add_ptr(service_instance_name(s->service), ttl);
144         dns_send_answer(iface, to, service);
145
146         dns_init_answer();
147         service_add_srv(s, ttl);
148         if (s->txt && s->txt_len)
149                 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
150         dns_send_answer(iface, to, host);
151 }
152
153 void
154 service_reply(struct interface *iface, struct sockaddr *to, const char *match, int ttl)
155 {
156         struct service *s;
157
158         vlist_for_each_element(&services, s, node) {
159                 if (!match || !strcmp(s->service, match))
160                         service_reply_single(iface, to, s, ttl, 0);
161         }
162 }
163
164 void
165 service_announce_services(struct interface *iface, struct sockaddr *to, int ttl)
166 {
167         struct service *s;
168
169         vlist_for_each_element(&services, s, node) {
170                 s->t = 0;
171                 if (ttl) {
172                         dns_init_answer();
173                         service_add_ptr(s->service, ttl);
174                         dns_send_answer(iface, to, C_DNS_SD);
175                 }
176                 service_reply_single(iface, to, s, ttl, 0);
177         }
178 }
179
180 static void
181 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
182                struct vlist_node *node_old)
183 {
184         struct interface *iface;
185         struct service *s;
186
187         if (!node_old) {
188                 s = container_of(node_new, struct service, node);
189                 if (service_init_announce)
190                         vlist_for_each_element(&interfaces, iface, node) {
191                                 s->t = 0;
192                                 service_reply_single(iface, NULL, s, announce_ttl, 1);
193                         }
194                 return;
195         }
196
197         s = container_of(node_old, struct service, node);
198         if (!node_new && service_init_announce)
199                 vlist_for_each_element(&interfaces, iface, node)
200                         service_reply_single(iface, NULL, s, 0, 1);
201         free(s);
202 }
203
204 static void
205 service_load_blob(struct blob_attr *b)
206 {
207         struct blob_attr *txt, *_tb[__SERVICE_MAX];
208         struct service *s;
209         char *d_service, *d_id;
210         uint8_t *d_txt;
211         int rem2;
212         int txt_len = 0;
213
214         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
215                 _tb, blobmsg_data(b), blobmsg_data_len(b));
216         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
217                 return;
218
219         if (_tb[SERVICE_TXT])
220                 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
221                         txt_len += 1 + strlen(blobmsg_get_string(txt));
222
223         s = calloc_a(sizeof(*s),
224                 &d_id, strlen(blobmsg_name(b)) + 1,
225                 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
226                 &d_txt, txt_len);
227         if (!s)
228                 return;
229
230         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
231         s->id = strcpy(d_id, blobmsg_name(b));
232         s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
233         s->active = 1;
234         s->t = 0;
235         s->txt_len = txt_len;
236         s->txt = d_txt;
237
238         if (_tb[SERVICE_TXT])
239                 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
240                         int len = strlen(blobmsg_get_string(txt));
241                         if (!len)
242                                 return;
243                         if (len > 0xff)
244                                 len = 0xff;
245                         *d_txt = len;
246                         d_txt++;
247                         memcpy(d_txt, blobmsg_get_string(txt), len);
248                         d_txt += len;
249                 }
250
251         vlist_add(&services, &s->node, s->id);
252 }
253
254 static void
255 service_load(char *path)
256 {
257         struct blob_attr *cur;
258         glob_t gl;
259         int i, rem;
260
261         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
262                 return;
263
264         for (i = 0; i < gl.gl_pathc; i++) {
265                 blob_buf_init(&b, 0);
266                 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i])) {
267                         blob_for_each_attr(cur, b.head, rem)
268                                 service_load_blob(cur);
269                 } else {
270                         fprintf(stderr, "Error reading %s JSON\n", gl.gl_pathv[i]);
271                 }
272         }
273         globfree(&gl);
274 }
275
276 static void
277 service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
278 {
279         struct blob_attr *cur;
280         int rem;
281
282         get_hostname();
283
284         vlist_update(&services);
285         service_load("/etc/umdns/*");
286
287         blob_for_each_attr(cur, msg, rem) {
288                 struct blob_attr *cur2;
289                 int rem2;
290
291                 blobmsg_for_each_attr(cur2, cur, rem2) {
292                         struct blob_attr *cur3;
293                         int rem3;
294
295                         if (strcmp(blobmsg_name(cur2), "instances"))
296                                 continue;
297
298                         blobmsg_for_each_attr(cur3, cur2, rem3) {
299                                 struct blob_attr *cur4;
300                                 int rem4;
301                                 int running = 0;
302
303                                 blobmsg_for_each_attr(cur4, cur3, rem4) {
304                                         const char *name = blobmsg_name(cur4);
305
306                                         if (!strcmp(name, "running")) {
307                                                 running = blobmsg_get_bool(cur4);
308                                         } else if (running && !strcmp(name, "data")) {
309                                                 struct blob_attr *cur5;
310                                                 int rem5;
311
312                                                 blobmsg_for_each_attr(cur5, cur4, rem5) {
313                                                         struct blob_attr *cur6;
314                                                         int rem6;
315
316                                                         if (strcmp(blobmsg_name(cur5), "mdns"))
317                                                                 continue;
318
319                                                         blobmsg_for_each_attr(cur6, cur5, rem6)
320                                                                 service_load_blob(cur6);
321                                                 }
322                                                 break;
323                                         }
324                                 }
325                         }
326                 }
327         }
328         vlist_flush(&services);
329 }
330
331 void
332 service_init(int announce)
333 {
334         get_hostname();
335
336         service_init_announce = announce;
337         ubus_service_list(service_init_cb);
338 }
339
340 void
341 service_cleanup(void)
342 {
343         vlist_flush(&services);
344         blob_buf_free(&b);
345 }