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