30e612193e541b42d35d93aaec13fba77af5e387
[project/mdnsd.git] / ubus.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/inet.h>
16
17 #include <stdio.h>
18
19 #include <libubus.h>
20 #include <libubox/vlist.h>
21 #include <libubox/uloop.h>
22
23 #include "util.h"
24 #include "ubus.h"
25 #include "cache.h"
26 #include "service.h"
27 #include "interface.h"
28
29 static struct ubus_auto_conn conn;
30 static struct blob_buf b;
31 static struct ubus_subscriber udebug_sub;
32
33 static int
34 umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
35 struct ubus_request_data *req, const char *method,
36 struct blob_attr *msg)
37 {
38 service_init(1);
39 return 0;
40 }
41
42 static int
43 umdns_update(struct ubus_context *ctx, struct ubus_object *obj,
44 struct ubus_request_data *req, const char *method,
45 struct blob_attr *msg)
46 {
47 cache_update();
48 return 0;
49 }
50
51 enum {
52 BROWSE_SERVICE,
53 BROWSE_ARRAY,
54 BROWSE_ADDRESS,
55 BROWSE_MAX
56 };
57
58 static const struct blobmsg_policy browse_policy[] = {
59 [BROWSE_SERVICE] = { "service", BLOBMSG_TYPE_STRING },
60 [BROWSE_ARRAY] = { "array", BLOBMSG_TYPE_BOOL },
61 [BROWSE_ADDRESS] = { "address", BLOBMSG_TYPE_BOOL },
62 };
63
64 static int
65 umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
66 struct ubus_request_data *req, const char *method,
67 struct blob_attr *msg)
68 {
69 struct cache_service *s, *q;
70 char *buffer = (char *) mdns_buf;
71 struct blob_attr *data[BROWSE_MAX];
72 void *c1 = NULL, *c2;
73 char *service = NULL;
74 int array = 0;
75 bool address = true;
76
77 blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg));
78 if (data[BROWSE_SERVICE])
79 service = blobmsg_get_string(data[BROWSE_SERVICE]);
80 if (data[BROWSE_ARRAY])
81 array = blobmsg_get_u8(data[BROWSE_ARRAY]);
82 if (data[BROWSE_ADDRESS])
83 address = blobmsg_get_bool(data[BROWSE_ADDRESS]);
84
85 blob_buf_init(&b, 0);
86 avl_for_each_element(&services, s, avl) {
87 const char *hostname = buffer;
88 char *local;
89
90 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
91 local = strstr(buffer, ".local");
92 if (local)
93 *local = '\0';
94 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
95 continue;
96 if (service && strcmp(buffer, service))
97 continue;
98 if (!c1) {
99 c1 = blobmsg_open_table(&b, buffer);
100 }
101 snprintf(buffer, MAX_NAME_LEN, "%s", s->entry);
102 local = strstr(buffer, "._");
103 if (local)
104 *local = '\0';
105 c2 = blobmsg_open_table(&b, buffer);
106 strncat(buffer, ".local", MAX_NAME_LEN);
107 blobmsg_add_string(&b, "iface", s->iface->name);
108 cache_dump_records(&b, s->entry, array, &hostname);
109 if (address)
110 cache_dump_records(&b, hostname, array, NULL);
111 blobmsg_close_table(&b, c2);
112 q = avl_next_element(s, avl);
113 if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
114 blobmsg_close_table(&b, c1);
115 c1 = NULL;
116 }
117 }
118 ubus_send_reply(ctx, req, b.head);
119
120 return UBUS_STATUS_OK;
121 }
122
123 enum {
124 HOSTS_ARRAY,
125 __HOSTS_MAX
126 };
127 static const struct blobmsg_policy hosts_policy[] = {
128 [HOSTS_ARRAY] = { "array", BLOBMSG_TYPE_BOOL }
129 };
130
131 static int
132 umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
133 struct ubus_request_data *req, const char *method,
134 struct blob_attr *msg)
135 {
136 struct cache_record *prev = NULL;
137 struct blob_attr *tb[__HOSTS_MAX];
138 struct cache_record *r;
139 bool array = false;
140 void *c;
141
142 blobmsg_parse(hosts_policy, __HOSTS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
143 if (tb[HOSTS_ARRAY])
144 array = blobmsg_get_bool(tb[HOSTS_ARRAY]);
145
146 blob_buf_init(&b, 0);
147 avl_for_each_element(&records, r, avl) {
148 if (r->type != TYPE_A && r->type != TYPE_AAAA)
149 continue;
150 /* Query each domain just once */
151 if (!prev || strcmp(r->record, prev->record)) {
152 c = blobmsg_open_table(&b, r->record);
153 cache_dump_records(&b, r->record, array, NULL);
154 blobmsg_close_table(&b, c);
155 }
156 prev = r;
157 }
158 ubus_send_reply(ctx, req, b.head);
159
160 return UBUS_STATUS_OK;
161 }
162
163 enum {
164 CFG_INTERFACES,
165 CFG_KEEP,
166 CFG_MAX
167 };
168
169 static const struct blobmsg_policy config_policy[] = {
170 [CFG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_ARRAY },
171 [CFG_KEEP] = { "keep", BLOBMSG_TYPE_BOOL },
172 };
173
174 static int
175 umdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
176 struct ubus_request_data *req, const char *method,
177 struct blob_attr *msg)
178 {
179 struct blob_attr *data[CFG_MAX], *cur;
180 int rem, keep = false;
181
182 blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg));
183 if (!data[CFG_INTERFACES])
184 return UBUS_STATUS_INVALID_ARGUMENT;
185
186 if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING))
187 return UBUS_STATUS_INVALID_ARGUMENT;
188
189 keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]);
190 if (!keep) {
191 vlist_update(&interfaces);
192 ubus_notify(ctx, obj, "set_config", NULL, 1000);
193 }
194
195 blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem)
196 interface_add(blobmsg_data(cur));
197
198 if (!keep)
199 vlist_flush(&interfaces);
200
201 return 0;
202 }
203
204 enum query_attr {
205 QUERY_QUESTION,
206 QUERY_IFACE,
207 QUERY_TYPE,
208 QUERY_MAX
209 };
210
211 static const struct blobmsg_policy query_policy[QUERY_MAX] = {
212 [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING },
213 [QUERY_IFACE] = { "interface", BLOBMSG_TYPE_STRING },
214 [QUERY_TYPE] = { "type", BLOBMSG_TYPE_INT32 },
215 };
216
217 static int
218 umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
219 struct ubus_request_data *req, const char *method,
220 struct blob_attr *msg)
221 {
222 struct blob_attr *tb[QUERY_MAX], *c;
223 const char *question = C_DNS_SD;
224 const char *ifname;
225 int type = TYPE_ANY;
226
227 blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg));
228
229 if (!(c = tb[QUERY_IFACE]))
230 return UBUS_STATUS_INVALID_ARGUMENT;
231
232 ifname = blobmsg_get_string(c);
233
234 if ((c = tb[QUERY_QUESTION]))
235 question = blobmsg_get_string(c);
236
237 if ((c = tb[QUERY_TYPE]))
238 type = blobmsg_get_u32(c);
239
240 struct interface *iface_v4 = interface_get(ifname, SOCK_MC_IPV4);
241 struct interface *iface_v6 = interface_get(ifname, SOCK_MC_IPV6);
242
243 if (!iface_v4 && !iface_v6)
244 return UBUS_STATUS_NOT_FOUND;
245
246 if (!strcmp(method, "query")) {
247 if (iface_v4)
248 dns_send_question(iface_v4, NULL, question, type, 1);
249
250 if (iface_v6)
251 dns_send_question(iface_v6, NULL, question, type, 1);
252
253 return UBUS_STATUS_OK;
254 } else if (!strcmp(method, "fetch")) {
255 blob_buf_init(&b, 0);
256 void *k = blobmsg_open_array(&b, "records");
257 cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6);
258 blobmsg_close_array(&b, k);
259 ubus_send_reply(ctx, req, b.head);
260 return UBUS_STATUS_OK;
261 } else {
262 return UBUS_STATUS_INVALID_ARGUMENT;
263 }
264 }
265
266
267 static const struct ubus_method umdns_methods[] = {
268 UBUS_METHOD("set_config", umdns_set_config, config_policy),
269 UBUS_METHOD("query", umdns_query, query_policy),
270 UBUS_METHOD("fetch", umdns_query, query_policy),
271 UBUS_METHOD("browse", umdns_browse, browse_policy),
272 UBUS_METHOD_NOARG("update", umdns_update),
273 UBUS_METHOD("hosts", umdns_hosts, hosts_policy),
274 UBUS_METHOD_NOARG("reload", umdns_reload),
275 };
276
277 static struct ubus_object_type umdns_object_type =
278 UBUS_OBJECT_TYPE("umdns", umdns_methods);
279
280 static struct ubus_object umdns_object = {
281 .name = "umdns",
282 .type = &umdns_object_type,
283 .methods = umdns_methods,
284 .n_methods = ARRAY_SIZE(umdns_methods),
285 };
286
287 static struct blob_attr *
288 find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
289 {
290 struct blobmsg_policy policy = { name, type };
291 struct blob_attr *ret;
292
293 if (!attr)
294 return NULL;
295
296 blobmsg_parse_attr(&policy, 1, &ret, attr);
297
298 return ret;
299 }
300
301 static void
302 umdns_udebug_config_cb(struct blob_attr *data)
303 {
304 enum {
305 CFG_ATTR_ENABLED,
306 __CFG_ATTR_MAX
307 };
308 static const struct blobmsg_policy policy[__CFG_ATTR_MAX] = {
309 [CFG_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_STRING },
310 };
311 struct blob_attr *tb[__CFG_ATTR_MAX];
312 bool en;
313
314 data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
315 data = find_attr(data, "umdns", BLOBMSG_TYPE_TABLE);
316 if (!data)
317 return;
318
319 blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data);
320 if (!tb[CFG_ATTR_ENABLED])
321 return;
322
323 en = !!atoi(blobmsg_get_string(tb[CFG_ATTR_ENABLED]));
324 umdns_udebug_set_enabled(en);
325 }
326
327 static int
328 umdns_udebug_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
329 struct ubus_request_data *req, const char *method,
330 struct blob_attr *msg)
331 {
332 umdns_udebug_config_cb(msg);
333
334 return 0;
335 }
336
337 static void
338 umdns_udebug_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
339 {
340 umdns_udebug_config_cb(msg);
341 }
342
343 static bool
344 umdns_udebug_sub_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
345 const char *path)
346 {
347 return !strcmp(path, "udebug");
348 }
349
350
351 static void
352 ubus_connect_handler(struct ubus_context *ctx)
353 {
354 uint32_t id;
355 int ret;
356
357 ret = ubus_add_object(ctx, &umdns_object);
358 if (ret)
359 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
360
361 udebug_sub.cb = umdns_udebug_notify_cb;
362 udebug_sub.new_obj_cb = umdns_udebug_sub_cb;
363 ubus_register_subscriber(&conn.ctx, &udebug_sub);
364 if (ubus_lookup_id(&conn.ctx, "udebug", &id) == 0) {
365 ubus_subscribe(&conn.ctx, &udebug_sub, id);
366 ubus_invoke(&conn.ctx, id, "get_config", NULL, umdns_udebug_req_cb, NULL, 1000);
367 }
368 }
369
370 void
371 ubus_startup(void)
372 {
373 conn.cb = ubus_connect_handler;
374 ubus_auto_connect(&conn);
375 }
376
377 int ubus_service_list(ubus_data_handler_t cb)
378 {
379 uint32_t id;
380 int ret;
381
382 blob_buf_init(&b, 0);
383 ret = ubus_lookup_id(&conn.ctx, "service", &id);
384 if (ret)
385 return ret;
386
387 return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000);
388 }