split the service_load() function into 2. this allows us to reuse the parsing
[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 <ifaddrs.h>
19 #include <resolv.h>
20 #include <glob.h>
21 #include <stdio.h>
22 #include <time.h>
23
24 #include <uci.h>
25 #include <uci_blob.h>
26
27 #include <libubox/vlist.h>
28 #include <libubox/uloop.h>
29 #include <libubox/avl-cmp.h>
30 #include <libubox/blobmsg_json.h>
31
32 #include "dns.h"
33 #include "service.h"
34 #include "util.h"
35 #include "interface.h"
36 #include "announce.h"
37
38 enum {
39 SERVICE_SERVICE,
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 *id;
51 const char *service;
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_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
60 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
61 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
62 };
63
64 static void
65 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
66 struct vlist_node *node_old);
67
68 static struct blob_buf b;
69 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
70 static char *sdudp = "_services._dns-sd._udp.local";
71 static char *sdtcp = "_services._dns-sd._tcp.local";
72 static int service_init_announce;
73
74 static const char *
75 service_name(const char *domain)
76 {
77 static char buffer[256];
78
79 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
80
81 return buffer;
82 }
83
84 static void
85 service_add_ptr(const char *host, int ttl)
86 {
87 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
88
89 if (len < 1)
90 return;
91
92 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
93 }
94
95 static void
96 service_add_srv(struct service *s, int ttl)
97 {
98 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
99 int len = sizeof(*sd);
100
101 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
102 if (len <= sizeof(*sd))
103 return;
104
105 sd->port = cpu_to_be16(s->port);
106 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
107 }
108
109 #define TOUT_LOOKUP 60
110
111 static int
112 service_timeout(struct service *s)
113 {
114 time_t t = time(NULL);
115
116 if (t - s->t <= TOUT_LOOKUP)
117 return 0;
118
119 s->t = t;
120
121 return 1;
122 }
123
124 void
125 service_reply_a(struct interface *iface, int type, int ttl)
126 {
127 struct ifaddrs *ifap, *ifa;
128 struct sockaddr_in *sa;
129 struct sockaddr_in6 *sa6;
130
131 getifaddrs(&ifap);
132
133 dns_init_answer();
134 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
135 if (strcmp(ifa->ifa_name, iface->name))
136 continue;
137 if (ifa->ifa_addr->sa_family==AF_INET) {
138 sa = (struct sockaddr_in *) ifa->ifa_addr;
139 dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
140 }
141 if (ifa->ifa_addr->sa_family==AF_INET6) {
142 uint8_t ll_prefix[] = {0xfe, 0x80 };
143 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
144 if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
145 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
146 }
147 }
148 dns_send_answer(iface, mdns_hostname_local);
149
150 freeifaddrs(ifap);
151 }
152
153 static void
154 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl, int force)
155 {
156 const char *host = service_name(s->service);
157 char *service = strstr(host, "._");
158
159 if (!force && (!s->active || !service || !service_timeout(s)))
160 return;
161
162 service++;
163
164 if (match && strcmp(match, s->service))
165 return;
166
167 dns_init_answer();
168 service_add_ptr(service_name(s->service), ttl);
169 dns_send_answer(iface, service);
170
171 dns_init_answer();
172 service_add_srv(s, ttl);
173 if (s->txt && s->txt_len)
174 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
175 dns_send_answer(iface, host);
176 }
177
178 void
179 service_reply(struct interface *iface, const char *match, int ttl)
180 {
181 struct service *s;
182
183 vlist_for_each_element(&services, s, node)
184 service_reply_single(iface, s, match, ttl, 0);
185
186 if (match)
187 return;
188
189 service_reply_a(iface, TYPE_A, ttl);
190 }
191
192 void
193 service_announce_services(struct interface *iface, const char *service)
194 {
195 struct service *s;
196 int tcp = 1;
197
198 if (!strcmp(service, sdudp))
199 tcp = 0;
200 else if (strcmp(service, sdtcp))
201 return;
202
203 vlist_for_each_element(&services, s, node) {
204 if (!strstr(s->service, "._tcp") && tcp)
205 continue;
206 if (!strstr(s->service, "._udp") && !tcp)
207 continue;
208 s->t = 0;
209 dns_init_answer();
210 service_add_ptr(s->service, announce_ttl);
211 if (tcp)
212 dns_send_answer(iface, sdtcp);
213 else
214 dns_send_answer(iface, sdudp);
215 service_reply(iface, s->service, announce_ttl);
216 }
217 }
218
219 void
220 service_announce(struct interface *iface)
221 {
222 service_announce_services(iface, sdudp);
223 service_announce_services(iface, sdtcp);
224 }
225
226 static void
227 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
228 struct vlist_node *node_old)
229 {
230 struct interface *iface;
231 struct service *s;
232
233 if (!node_old) {
234 s = container_of(node_new, struct service, node);
235 if (service_init_announce)
236 vlist_for_each_element(&interfaces, iface, node) {
237 s->t = 0;
238 service_reply_single(iface, s, NULL, announce_ttl, 1);
239 }
240 return;
241 }
242
243 s = container_of(node_old, struct service, node);
244 if (!node_new && service_init_announce)
245 vlist_for_each_element(&interfaces, iface, node)
246 service_reply_single(iface, s, NULL, 0, 1);
247 free(s);
248 }
249
250 static void
251 service_load_blob(struct blob_attr *b)
252 {
253 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
254 int rem;
255
256 blob_for_each_attr(cur, b, rem) {
257 struct service *s;
258 char *d_service, *d_id;
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_SERVICE])
266 continue;
267
268 if (_tb[SERVICE_SERVICE])
269 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
270 txt_len += 1 + strlen(blobmsg_get_string(txt));
271
272 s = calloc_a(sizeof(*s),
273 &d_id, strlen(blobmsg_name(cur)) + 1,
274 &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
275 &d_txt, txt_len);
276 if (!s)
277 continue;
278
279 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
280 s->id = strcpy(d_id, blobmsg_name(cur));
281 s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
282 s->active = 1;
283 s->t = 0;
284 s->txt_len = txt_len;
285 s->txt = d_txt;
286
287 if (_tb[SERVICE_SERVICE])
288 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
289 int len = strlen(blobmsg_get_string(txt));
290 if (!len)
291 continue;
292 if (len > 0xff)
293 len = 0xff;
294 *d_txt = len;
295 d_txt++;
296 memcpy(d_txt, blobmsg_get_string(txt), len);
297 d_txt += len;
298 }
299
300 vlist_add(&services, &s->node, s->id);
301 }
302 }
303
304 static void
305 service_load(char *path)
306 {
307 glob_t gl;
308 int i;
309
310 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
311 return;
312
313 for (i = 0; i < gl.gl_pathc; i++) {
314 blob_buf_init(&b, 0);
315 if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
316 service_load_blob(b.head);
317 }
318 globfree(&gl);
319 }
320
321 void
322 service_init(int announce)
323 {
324 service_init_announce = announce;
325
326 get_hostname();
327
328 vlist_update(&services);
329 service_load("/tmp/run/mdnsd/*");
330 vlist_flush(&services);
331 }
332
333 void
334 service_cleanup(void)
335 {
336 vlist_flush(&services);
337 blob_buf_free(&b);
338 }