import v0.1
[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 <netinet/in.h>
16 #include <arpa/nameser.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20
21 #include <resolv.h>
22 #include <glob.h>
23 #include <stdio.h>
24 #include <time.h>
25
26 #include <uci.h>
27 #include <uci_blob.h>
28
29 #include <libubox/avl.h>
30 #include <libubox/uloop.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/blobmsg_json.h>
33
34 #include "dns.h"
35 #include "service.h"
36 #include "util.h"
37
38 enum {
39         SERVICE_PORT,
40         SERVICE_TXT,
41         __SERVICE_MAX,
42 };
43
44 struct service {
45         struct avl_node avl;
46
47         time_t t;
48
49         char *service;
50         char *daemon;
51         char *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 const struct uci_blob_param_list service_attr_list = {
63         .n_params = __SERVICE_MAX,
64         .params = service_policy,
65 };
66
67 static struct blob_buf b;
68 static struct avl_tree services;
69 char *hostname = NULL;
70 static char *sdudp =  "_services._dns-sd._udp.local";
71 static char *sdtcp =  "_services._dns-sd._tcp.local";
72
73 char*
74 service_name(char *domain)
75 {
76         static char buffer[256];
77
78         snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
79
80         return buffer;
81 }
82
83 static void
84 service_send_ptr(struct uloop_fd *u, struct service *s)
85 {
86         unsigned char buffer[MAX_NAME_LEN];
87         char *host = service_name(s->service);
88         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
89
90         if (len < 1)
91                 return;
92
93         dns_add_answer(TYPE_PTR, buffer, len);
94 }
95
96 static void
97 service_send_ptr_c(struct uloop_fd *u, char *host)
98 {
99         unsigned char buffer[MAX_NAME_LEN];
100         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
101
102         if (len < 1)
103                 return;
104
105         dns_add_answer(TYPE_PTR, buffer, len);
106 }
107
108 static void
109 service_send_a(struct uloop_fd *u)
110 {
111         unsigned char buffer[MAX_NAME_LEN];
112         char *host = service_name("local");
113         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
114         struct in_addr in;
115
116         if (!inet_aton(iface_ip, &in)) {
117                 fprintf(stderr, "%s is not valid\n", iface_ip);
118                 return;
119         }
120
121         if (len < 1)
122                 return;
123
124         dns_add_answer(TYPE_A, (uint8_t *) &in.s_addr, 4);
125 }
126
127 static void
128 service_send_srv(struct uloop_fd *u, struct service *s)
129 {
130         unsigned char buffer[MAX_NAME_LEN];
131         struct dns_srv_data *sd;
132         char *host = service_name("local");
133         int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
134
135         if (len < 1)
136                 return;
137
138         sd = malloc(len + sizeof(struct dns_srv_data));
139         if (!sd)
140                 return;
141
142         memset(sd, 0, sizeof(struct dns_srv_data));
143         sd->port = cpu_to_be16(s->port);
144         memcpy(&sd[1], buffer, len);
145         host = service_name(s->service);
146         dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
147         free(sd);
148 }
149
150 #define TOUT_LOOKUP     60
151
152 static int
153 service_timeout(struct service *s)
154 {
155         time_t t = time(NULL);
156
157         if (t - s->t <= TOUT_LOOKUP)
158                 return 0;
159
160         s->t = t;
161
162         return 1;
163 }
164
165 void
166 service_reply_a(struct uloop_fd *u, int type)
167 {
168         if (type != TYPE_A)
169                 return;
170
171         dns_init_answer();
172         service_send_a(u);
173         dns_send_answer(u, service_name("local"));
174 }
175
176 void
177 service_reply(struct uloop_fd *u, char *match)
178 {
179         struct service *s;
180
181         avl_for_each_element(&services, s, avl) {
182                 char *host = service_name(s->service);
183                 char *service = strstr(host, "._");
184
185                 if (!s->active || !service || !service_timeout(s))
186                         continue;
187
188                 service++;
189
190                 if (match && strcmp(match, s->service))
191                         continue;
192
193                 dns_init_answer();
194                 service_send_ptr(u, s);
195                 dns_send_answer(u, service);
196
197                 dns_init_answer();
198                 service_send_srv(u, s);
199                 if (s->txt && s->txt_len)
200                         dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
201                 dns_send_answer(u, host);
202         }
203
204         if (match)
205                 return;
206
207         dns_init_answer();
208         service_send_a(u);
209         dns_send_answer(u, service_name("local"));
210 }
211
212 void
213 service_announce_services(struct uloop_fd *u, char *service)
214 {
215         struct service *s;
216         int tcp = 1;
217
218         if (!strcmp(service, sdudp))
219                 tcp = 0;
220         else if (strcmp(service, sdtcp))
221                 return;
222
223         avl_for_each_element(&services, s, avl) {
224                 if (!strstr(s->service, "._tcp") && tcp)
225                         continue;
226                 if (!strstr(s->service, "._udp") && !tcp)
227                         continue;
228                 s->t = 0;
229                 dns_init_answer();
230                 service_send_ptr_c(u, s->service);
231                 if (tcp)
232                         dns_send_answer(u, sdtcp);
233                 else
234                         dns_send_answer(u, sdudp);
235                 service_reply(u, s->service);
236         }
237 }
238
239 void
240 service_announce(struct uloop_fd *u)
241 {
242         service_announce_services(u, sdudp);
243         service_announce_services(u, sdtcp);
244 }
245
246 static void
247 service_load(char *path)
248 {
249         struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
250         int rem, i;
251         glob_t gl;
252
253         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
254                 return;
255
256         for (i = 0; i < gl.gl_pathc; i++) {
257                 blob_buf_init(&b, 0);
258
259                 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
260                         continue;
261                 blob_for_each_attr(cur, b.head, rem) {
262                         struct service *s;
263                         char *d_service, *d_txt, *d_daemon;
264                         int rem2;
265
266                         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
267                                 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
268                         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
269                                 continue;
270
271                         s = calloc_a(sizeof(*s),
272                                 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
273                                 &d_service, strlen(blobmsg_name(cur)) + 1,
274                                 &d_txt, blobmsg_data_len(_tb[SERVICE_TXT]));
275                         if (!s)
276                                 continue;
277
278                         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
279                         s->service = d_service;
280                         s->daemon = d_daemon;
281                         strcpy(s->service, blobmsg_name(cur));
282                         strcpy(s->daemon, gl.gl_pathv[i]);
283                         s->avl.key = s->service;
284                         s->active = 1;
285                         s->t = 0;
286                         avl_insert(&services, &s->avl);
287
288                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
289                                 s->txt_len += 1 + strlen(blobmsg_get_string(txt));
290
291                         if (!s->txt_len)
292                                 continue;
293
294                         d_txt = s->txt = malloc(s->txt_len);
295                         if (!s->txt)
296                                 continue;
297
298                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
299                                 int len = strlen(blobmsg_get_string(txt));
300                                 if (!len)
301                                         continue;
302                                 if (len > 0xff)
303                                         len = 0xff;
304                                 *d_txt = len;
305                                 d_txt++;
306                                 memcpy(d_txt, blobmsg_get_string(txt), len);
307                                 d_txt += len;
308                         }
309                 }
310         }
311 }
312
313 void
314 service_init(void)
315 {
316         if (!hostname)
317                 hostname = get_hostname();
318         avl_init(&services, avl_strcmp, true, NULL);
319         service_load("/tmp/run/mdnsd/*");
320 }
321
322 void
323 service_cleanup(void)
324 {
325 }