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