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