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