add the rfc
[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_PORT,
40 SERVICE_TXT,
41 __SERVICE_MAX,
42 };
43
44 struct service {
45 struct vlist_node node;
46
47 time_t t;
48
49 const char *service;
50 const char *daemon;
51 const uint8_t *txt;
52 int txt_len;
53 int port;
54 int active;
55 };
56
57 static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
58 [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
59 [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
60 };
61
62 static void
63 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
64 struct vlist_node *node_old);
65
66 static struct blob_buf b;
67 static VLIST_TREE(services, avl_strcmp, service_update, false, false);
68 static char *sdudp = "_services._dns-sd._udp.local";
69 static char *sdtcp = "_services._dns-sd._tcp.local";
70
71 static const char *
72 service_name(const char *domain)
73 {
74 static char buffer[256];
75
76 snprintf(buffer, sizeof(buffer), "%s.%s", mdns_hostname, domain);
77
78 return buffer;
79 }
80
81 static void
82 service_add_ptr(const char *host, int ttl)
83 {
84 int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
85
86 if (len < 1)
87 return;
88
89 dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
90 }
91
92 static void
93 service_add_srv(struct service *s, int ttl)
94 {
95 struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
96 int len = sizeof(*sd);
97
98 len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
99 if (len <= sizeof(*sd))
100 return;
101
102 sd->port = cpu_to_be16(s->port);
103 dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
104 }
105
106 #define TOUT_LOOKUP 60
107
108 static int
109 service_timeout(struct service *s)
110 {
111 time_t t = time(NULL);
112
113 if (t - s->t <= TOUT_LOOKUP)
114 return 0;
115
116 s->t = t;
117
118 return 1;
119 }
120
121 void
122 service_reply_a(struct interface *iface, int type, int ttl)
123 {
124 struct ifaddrs *ifap, *ifa;
125 struct sockaddr_in *sa;
126 struct sockaddr_in6 *sa6;
127 char *addr;
128
129 getifaddrs(&ifap);
130
131 dns_init_answer();
132 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
133 if (strcmp(ifa->ifa_name, iface->name))
134 continue;
135 if (ifa->ifa_addr->sa_family==AF_INET) {
136 sa = (struct sockaddr_in *) ifa->ifa_addr;
137 addr = inet_ntoa(sa->sin_addr);
138 printf("Interface: %s\tAddress4: %s\n", ifa->ifa_name, 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 char buf[64] = { 0 };
144 sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
145 if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2)) {
146 if (inet_ntop(AF_INET6, &sa6->sin6_addr, buf, 64))
147 printf("Interface: %s\tAddress6: %s\n", ifa->ifa_name, buf);
148 dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
149 }
150 }
151 }
152 dns_send_answer(iface, mdns_hostname_local);
153
154 freeifaddrs(ifap);
155 }
156
157 static void
158 service_reply_single(struct interface *iface, struct service *s, const char *match, int ttl)
159 {
160 const char *host = service_name(s->service);
161 char *service = strstr(host, "._");
162
163 if (!s->active || !service || !service_timeout(s))
164 return;
165
166 service++;
167
168 if (match && strcmp(match, s->service))
169 return;
170
171 dns_init_answer();
172 service_add_ptr(service_name(s->service), ttl);
173 dns_send_answer(iface, service);
174
175 dns_init_answer();
176 service_add_srv(s, ttl);
177 if (s->txt && s->txt_len)
178 dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
179 dns_send_answer(iface, host);
180 }
181
182 void
183 service_reply(struct interface *iface, const char *match, int ttl)
184 {
185 struct service *s;
186
187 vlist_for_each_element(&services, s, node)
188 service_reply_single(iface, s, match, ttl);
189
190 if (match)
191 return;
192
193 service_reply_a(iface, TYPE_A, ttl);
194 }
195
196 void
197 service_announce_services(struct interface *iface, const char *service)
198 {
199 struct service *s;
200 int tcp = 1;
201
202 if (!strcmp(service, sdudp))
203 tcp = 0;
204 else if (strcmp(service, sdtcp))
205 return;
206
207 vlist_for_each_element(&services, s, node) {
208 if (!strstr(s->service, "._tcp") && tcp)
209 continue;
210 if (!strstr(s->service, "._udp") && !tcp)
211 continue;
212 s->t = 0;
213 dns_init_answer();
214 service_add_ptr(s->service, announce_ttl);
215 if (tcp)
216 dns_send_answer(iface, sdtcp);
217 else
218 dns_send_answer(iface, sdudp);
219 service_reply(iface, s->service, announce_ttl);
220 }
221 }
222
223 void
224 service_announce(struct interface *iface)
225 {
226 service_announce_services(iface, sdudp);
227 service_announce_services(iface, sdtcp);
228 }
229
230 static void
231 service_update(struct vlist_tree *tree, struct vlist_node *node_new,
232 struct vlist_node *node_old)
233 {
234 struct interface *iface;
235 struct service *s;
236
237 if (!node_old)
238 return;
239
240 s = container_of(node_old, struct service, node);
241
242 if (!node_new)
243 vlist_for_each_element(&interfaces, iface, node)
244 service_reply_single(iface, s, NULL, 0);
245
246 free(s);
247 }
248
249 static void
250 service_load(char *path)
251 {
252 struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
253 int rem, i;
254 glob_t gl;
255
256 if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
257 return;
258
259 for (i = 0; i < gl.gl_pathc; i++) {
260 blob_buf_init(&b, 0);
261
262 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
263 continue;
264 blob_for_each_attr(cur, b.head, rem) {
265 struct service *s;
266 char *d_service, *d_daemon;
267 uint8_t *d_txt;
268 int rem2;
269 int txt_len = 0;
270
271 blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
272 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
273 if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
274 continue;
275
276 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
277 txt_len += 1 + strlen(blobmsg_get_string(txt));
278
279 s = calloc_a(sizeof(*s),
280 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
281 &d_service, strlen(blobmsg_name(cur)) + 1,
282 &d_txt, txt_len);
283 if (!s)
284 continue;
285
286 s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
287 s->service = strcpy(d_service, blobmsg_name(cur));
288 s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
289 s->active = 1;
290 s->t = 0;
291 s->txt_len = txt_len;
292 s->txt = d_txt;
293
294 blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
295 int len = strlen(blobmsg_get_string(txt));
296 if (!len)
297 continue;
298 if (len > 0xff)
299 len = 0xff;
300 *d_txt = len;
301 d_txt++;
302 memcpy(d_txt, blobmsg_get_string(txt), len);
303 d_txt += len;
304 }
305
306 vlist_add(&services, &s->node, s->service);
307 }
308 }
309 }
310
311 void
312 service_init(void)
313 {
314 get_hostname();
315
316 vlist_update(&services);
317 service_load("/tmp/run/mdnsd/*");
318 vlist_flush(&services);
319 }
320
321 void
322 service_cleanup(void)
323 {
324 vlist_flush(&services);
325 blob_buf_free(&b);
326 }