remove a few TODO items
[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(struct service *s)
87 {
88         unsigned char buffer[MAX_NAME_LEN];
89         const char *host = service_name(s->service);
90         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
91
92         if (len < 1)
93                 return;
94
95         dns_add_answer(TYPE_PTR, buffer, len);
96 }
97
98 static void
99 service_add_ptr_c(const char *host)
100 {
101         unsigned char buffer[MAX_NAME_LEN];
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_PTR, buffer, len);
108 }
109
110 static void
111 service_send_a(struct interface *iface)
112 {
113         unsigned char buffer[MAX_NAME_LEN];
114         char *host = service_name("local");
115         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
116
117         if (len < 1)
118                 return;
119
120         dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
121 }
122
123 static void
124 service_add_srv(struct service *s)
125 {
126         unsigned char buffer[MAX_NAME_LEN];
127         struct dns_srv_data *sd;
128         char *host = service_name("local");
129         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
130
131         if (len < 1)
132                 return;
133
134         sd = calloc(1, len + sizeof(struct dns_srv_data));
135         if (!sd)
136                 return;
137
138         sd->port = cpu_to_be16(s->port);
139         memcpy(&sd[1], buffer, len);
140         host = service_name(s->service);
141         dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
142         free(sd);
143 }
144
145 #define TOUT_LOOKUP     60
146
147 static int
148 service_timeout(struct service *s)
149 {
150         time_t t = time(NULL);
151
152         if (t - s->t <= TOUT_LOOKUP)
153                 return 0;
154
155         s->t = t;
156
157         return 1;
158 }
159
160 void
161 service_reply_a(struct interface *iface, int type)
162 {
163         if (type != TYPE_A)
164                 return;
165
166         dns_init_answer();
167         service_send_a(iface);
168         dns_send_answer(iface, service_name("local"));
169 }
170
171 void
172 service_reply(struct interface *iface, const char *match)
173 {
174         struct service *s;
175
176         vlist_for_each_element(&services, s, node) {
177                 char *host = service_name(s->service);
178                 char *service = strstr(host, "._");
179
180                 if (!s->active || !service || !service_timeout(s))
181                         continue;
182
183                 service++;
184
185                 if (match && strcmp(match, s->service))
186                         continue;
187
188                 dns_init_answer();
189                 service_add_ptr(s);
190                 dns_send_answer(iface, service);
191
192                 dns_init_answer();
193                 service_add_srv(s);
194                 if (s->txt && s->txt_len)
195                         dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
196                 dns_send_answer(iface, host);
197         }
198
199         if (match)
200                 return;
201
202         dns_init_answer();
203         service_send_a(iface);
204         dns_send_answer(iface, service_name("local"));
205 }
206
207 void
208 service_announce_services(struct interface *iface, const char *service)
209 {
210         struct service *s;
211         int tcp = 1;
212
213         if (!strcmp(service, sdudp))
214                 tcp = 0;
215         else if (strcmp(service, sdtcp))
216                 return;
217
218         vlist_for_each_element(&services, s, node) {
219                 if (!strstr(s->service, "._tcp") && tcp)
220                         continue;
221                 if (!strstr(s->service, "._udp") && !tcp)
222                         continue;
223                 s->t = 0;
224                 dns_init_answer();
225                 service_add_ptr_c(s->service);
226                 if (tcp)
227                         dns_send_answer(iface, sdtcp);
228                 else
229                         dns_send_answer(iface, sdudp);
230                 service_reply(iface, s->service);
231         }
232 }
233
234 void
235 service_announce(struct interface *iface)
236 {
237         service_announce_services(iface, sdudp);
238         service_announce_services(iface, sdtcp);
239 }
240
241 static void
242 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
243                struct vlist_node *node_old)
244 {
245         struct service *s;
246
247         if (!node_old)
248                 return;
249
250         s = container_of(node_old, struct service, node);
251         free(s);
252 }
253
254 static void
255 service_load(char *path)
256 {
257         struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
258         int rem, i;
259         glob_t gl;
260
261         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
262                 return;
263
264         for (i = 0; i < gl.gl_pathc; i++) {
265                 blob_buf_init(&b, 0);
266
267                 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
268                         continue;
269                 blob_for_each_attr(cur, b.head, rem) {
270                         struct service *s;
271                         char *d_service, *d_daemon;
272                         uint8_t *d_txt;
273                         int rem2;
274                         int txt_len = 0;
275
276                         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
277                                 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
278                         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
279                                 continue;
280
281                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
282                                 txt_len += 1 + strlen(blobmsg_get_string(txt));
283
284                         s = calloc_a(sizeof(*s),
285                                 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
286                                 &d_service, strlen(blobmsg_name(cur)) + 1,
287                                 &d_txt, txt_len);
288                         if (!s)
289                                 continue;
290
291                         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
292                         s->service = strcpy(d_service, blobmsg_name(cur));
293                         s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
294                         s->active = 1;
295                         s->t = 0;
296                         s->txt_len = txt_len;
297                         s->txt = d_txt;
298
299                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
300                                 int len = strlen(blobmsg_get_string(txt));
301                                 if (!len)
302                                         continue;
303                                 if (len > 0xff)
304                                         len = 0xff;
305                                 *d_txt = len;
306                                 d_txt++;
307                                 memcpy(d_txt, blobmsg_get_string(txt), len);
308                                 d_txt += len;
309                         }
310
311                         vlist_add(&services, &s->node, s->service);
312                 }
313         }
314 }
315
316 void
317 service_init(void)
318 {
319         if (!hostname)
320                 hostname = get_hostname();
321
322         vlist_update(&services);
323         service_load("/tmp/run/mdnsd/*");
324         vlist_flush(&services);
325 }
326
327 void
328 service_cleanup(void)
329 {
330         vlist_flush(&services);
331 }