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