f60431d350d27c708ed9b32373f4000149914e86
[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 <uci.h>
24 #include <uci_blob.h>
25
26 #include <libubox/vlist.h>
27 #include <libubox/uloop.h>
28 #include <libubox/avl-cmp.h>
29 #include <libubox/blobmsg_json.h>
30
31 #include "dns.h"
32 #include "service.h"
33 #include "util.h"
34 #include "interface.h"
35
36 enum {
37         SERVICE_PORT,
38         SERVICE_TXT,
39         __SERVICE_MAX,
40 };
41
42 struct service {
43         struct vlist_node node;
44
45         time_t t;
46
47         const char *service;
48         const char *daemon;
49         const uint8_t *txt;
50         int txt_len;
51         int port;
52         int active;
53 };
54
55 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
56         [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
57         [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
58 };
59
60 static const struct uci_blob_param_list service_attr_list = {
61         .n_params = __SERVICE_MAX,
62         .params = service_policy,
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 char *hostname = NULL;
72 static char *sdudp =  "_services._dns-sd._udp.local";
73 static char *sdtcp =  "_services._dns-sd._tcp.local";
74
75 char *
76 service_name(const char *domain)
77 {
78         static char buffer[256];
79
80         snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
81
82         return buffer;
83 }
84
85 static void
86 service_add_ptr(const char *host)
87 {
88         unsigned char buffer[MAX_NAME_LEN];
89         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
90
91         if (len < 1)
92                 return;
93
94         dns_add_answer(TYPE_PTR, buffer, len);
95 }
96
97 static void
98 service_send_a(struct interface *iface)
99 {
100         unsigned char buffer[MAX_NAME_LEN];
101         char *host = service_name("local");
102         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
103
104         if (len < 1)
105                 return;
106
107         dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
108 }
109
110 static void
111 service_add_srv(struct service *s)
112 {
113         unsigned char buffer[MAX_NAME_LEN];
114         struct dns_srv_data *sd;
115         char *host = service_name("local");
116         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
117
118         if (len < 1)
119                 return;
120
121         sd = calloc(1, len + sizeof(struct dns_srv_data));
122         if (!sd)
123                 return;
124
125         sd->port = cpu_to_be16(s->port);
126         memcpy(&sd[1], buffer, len);
127         host = service_name(s->service);
128         dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
129         free(sd);
130 }
131
132 #define TOUT_LOOKUP     60
133
134 static int
135 service_timeout(struct service *s)
136 {
137         time_t t = time(NULL);
138
139         if (t - s->t <= TOUT_LOOKUP)
140                 return 0;
141
142         s->t = t;
143
144         return 1;
145 }
146
147 void
148 service_reply_a(struct interface *iface, int type)
149 {
150         if (type != TYPE_A)
151                 return;
152
153         dns_init_answer();
154         service_send_a(iface);
155         dns_send_answer(iface, service_name("local"));
156 }
157
158 void
159 service_reply(struct interface *iface, const char *match)
160 {
161         struct service *s;
162
163         vlist_for_each_element(&services, s, node) {
164                 char *host = service_name(s->service);
165                 char *service = strstr(host, "._");
166
167                 if (!s->active || !service || !service_timeout(s))
168                         continue;
169
170                 service++;
171
172                 if (match && strcmp(match, s->service))
173                         continue;
174
175                 dns_init_answer();
176                 service_add_ptr(service_name(s->service));
177                 dns_send_answer(iface, service);
178
179                 dns_init_answer();
180                 service_add_srv(s);
181                 if (s->txt && s->txt_len)
182                         dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
183                 dns_send_answer(iface, host);
184         }
185
186         if (match)
187                 return;
188
189         dns_init_answer();
190         service_send_a(iface);
191         dns_send_answer(iface, service_name("local"));
192 }
193
194 void
195 service_announce_services(struct interface *iface, const char *service)
196 {
197         struct service *s;
198         int tcp = 1;
199
200         if (!strcmp(service, sdudp))
201                 tcp = 0;
202         else if (strcmp(service, sdtcp))
203                 return;
204
205         vlist_for_each_element(&services, s, node) {
206                 if (!strstr(s->service, "._tcp") && tcp)
207                         continue;
208                 if (!strstr(s->service, "._udp") && !tcp)
209                         continue;
210                 s->t = 0;
211                 dns_init_answer();
212                 service_add_ptr(s->service);
213                 if (tcp)
214                         dns_send_answer(iface, sdtcp);
215                 else
216                         dns_send_answer(iface, sdudp);
217                 service_reply(iface, s->service);
218         }
219 }
220
221 void
222 service_announce(struct interface *iface)
223 {
224         service_announce_services(iface, sdudp);
225         service_announce_services(iface, sdtcp);
226 }
227
228 static void
229 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
230                struct vlist_node *node_old)
231 {
232         struct service *s;
233
234         if (!node_old)
235                 return;
236
237         s = container_of(node_old, struct service, node);
238         free(s);
239 }
240
241 static void
242 service_load(char *path)
243 {
244         struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
245         int rem, i;
246         glob_t gl;
247
248         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
249                 return;
250
251         for (i = 0; i < gl.gl_pathc; i++) {
252                 blob_buf_init(&b, 0);
253
254                 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
255                         continue;
256                 blob_for_each_attr(cur, b.head, rem) {
257                         struct service *s;
258                         char *d_service, *d_daemon;
259                         uint8_t *d_txt;
260                         int rem2;
261                         int txt_len = 0;
262
263                         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
264                                 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
265                         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
266                                 continue;
267
268                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
269                                 txt_len += 1 + strlen(blobmsg_get_string(txt));
270
271                         s = calloc_a(sizeof(*s),
272                                 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
273                                 &d_service, strlen(blobmsg_name(cur)) + 1,
274                                 &d_txt, txt_len);
275                         if (!s)
276                                 continue;
277
278                         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
279                         s->service = strcpy(d_service, blobmsg_name(cur));
280                         s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
281                         s->active = 1;
282                         s->t = 0;
283                         s->txt_len = txt_len;
284                         s->txt = d_txt;
285
286                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
287                                 int len = strlen(blobmsg_get_string(txt));
288                                 if (!len)
289                                         continue;
290                                 if (len > 0xff)
291                                         len = 0xff;
292                                 *d_txt = len;
293                                 d_txt++;
294                                 memcpy(d_txt, blobmsg_get_string(txt), len);
295                                 d_txt += len;
296                         }
297
298                         vlist_add(&services, &s->node, s->service);
299                 }
300         }
301 }
302
303 void
304 service_init(void)
305 {
306         if (!hostname)
307                 hostname = get_hostname();
308
309         vlist_update(&services);
310         service_load("/tmp/run/mdnsd/*");
311         vlist_flush(&services);
312 }
313
314 void
315 service_cleanup(void)
316 {
317         vlist_flush(&services);
318         blob_buf_free(&b);
319 }