service: make the service txt field const uint8_t to reflect its use more accurately
[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         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 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 = calloc(1, len + sizeof(struct dns_srv_data));
139         if (!sd)
140                 return;
141
142         sd->port = cpu_to_be16(s->port);
143         memcpy(&sd[1], buffer, len);
144         host = service_name(s->service);
145         dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
146         free(sd);
147 }
148
149 #define TOUT_LOOKUP     60
150
151 static int
152 service_timeout(struct service *s)
153 {
154         time_t t = time(NULL);
155
156         if (t - s->t <= TOUT_LOOKUP)
157                 return 0;
158
159         s->t = t;
160
161         return 1;
162 }
163
164 void
165 service_reply_a(struct uloop_fd *u, int type)
166 {
167         if (type != TYPE_A)
168                 return;
169
170         dns_init_answer();
171         service_send_a(u);
172         dns_send_answer(u, service_name("local"));
173 }
174
175 void
176 service_reply(struct uloop_fd *u, char *match)
177 {
178         struct service *s;
179
180         avl_for_each_element(&services, s, avl) {
181                 char *host = service_name(s->service);
182                 char *service = strstr(host, "._");
183
184                 if (!s->active || !service || !service_timeout(s))
185                         continue;
186
187                 service++;
188
189                 if (match && strcmp(match, s->service))
190                         continue;
191
192                 dns_init_answer();
193                 service_send_ptr(u, s);
194                 dns_send_answer(u, service);
195
196                 dns_init_answer();
197                 service_send_srv(u, s);
198                 if (s->txt && s->txt_len)
199                         dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
200                 dns_send_answer(u, host);
201         }
202
203         if (match)
204                 return;
205
206         dns_init_answer();
207         service_send_a(u);
208         dns_send_answer(u, service_name("local"));
209 }
210
211 void
212 service_announce_services(struct uloop_fd *u, char *service)
213 {
214         struct service *s;
215         int tcp = 1;
216
217         if (!strcmp(service, sdudp))
218                 tcp = 0;
219         else if (strcmp(service, sdtcp))
220                 return;
221
222         avl_for_each_element(&services, s, avl) {
223                 if (!strstr(s->service, "._tcp") && tcp)
224                         continue;
225                 if (!strstr(s->service, "._udp") && !tcp)
226                         continue;
227                 s->t = 0;
228                 dns_init_answer();
229                 service_send_ptr_c(u, s->service);
230                 if (tcp)
231                         dns_send_answer(u, sdtcp);
232                 else
233                         dns_send_answer(u, sdudp);
234                 service_reply(u, s->service);
235         }
236 }
237
238 void
239 service_announce(struct uloop_fd *u)
240 {
241         service_announce_services(u, sdudp);
242         service_announce_services(u, sdtcp);
243 }
244
245 static void
246 service_load(char *path)
247 {
248         struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
249         int rem, i;
250         glob_t gl;
251
252         if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
253                 return;
254
255         for (i = 0; i < gl.gl_pathc; i++) {
256                 blob_buf_init(&b, 0);
257
258                 if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
259                         continue;
260                 blob_for_each_attr(cur, b.head, rem) {
261                         struct service *s;
262                         char *d_service, *d_daemon;
263                         uint8_t *d_txt;
264                         int rem2;
265                         int txt_len = 0;
266
267                         blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
268                                 _tb, blobmsg_data(cur), blobmsg_data_len(cur));
269                         if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
270                                 continue;
271
272                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
273                                 txt_len += 1 + strlen(blobmsg_get_string(txt));
274
275                         s = calloc_a(sizeof(*s),
276                                 &d_daemon, strlen(gl.gl_pathv[i]) + 1,
277                                 &d_service, strlen(blobmsg_name(cur)) + 1,
278                                 &d_txt, txt_len);
279                         if (!s)
280                                 continue;
281
282                         s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
283                         s->service = d_service;
284                         s->daemon = d_daemon;
285                         strcpy(s->service, blobmsg_name(cur));
286                         strcpy(s->daemon, gl.gl_pathv[i]);
287                         s->avl.key = s->service;
288                         s->active = 1;
289                         s->t = 0;
290                         s->txt_len = txt_len;
291                         s->txt = d_txt;
292                         avl_insert(&services, &s->avl);
293
294                         if (!s->txt_len)
295                                 continue;
296
297                         blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
298                                 int len = strlen(blobmsg_get_string(txt));
299                                 if (!len)
300                                         continue;
301                                 if (len > 0xff)
302                                         len = 0xff;
303                                 *d_txt = len;
304                                 d_txt++;
305                                 memcpy(d_txt, blobmsg_get_string(txt), len);
306                                 d_txt += len;
307                         }
308                 }
309         }
310 }
311
312 void
313 service_init(void)
314 {
315         if (!hostname)
316                 hostname = get_hostname();
317         avl_init(&services, avl_strcmp, true, NULL);
318         service_load("/tmp/run/mdnsd/*");
319 }
320
321 void
322 service_cleanup(void)
323 {
324 }