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