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