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