ubus: allow filtering by service when invoking the browse method
[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
32 static int
33 umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
34 struct ubus_request_data *req, const char *method,
35 struct blob_attr *msg)
36 {
37 service_init(1);
38 return 0;
39 }
40
41 static int
42 umdns_update(struct ubus_context *ctx, struct ubus_object *obj,
43 struct ubus_request_data *req, const char *method,
44 struct blob_attr *msg)
45 {
46 cache_update();
47 return 0;
48 }
49
50 enum {
51 BROWSE_SERVICE,
52 BROWSE_MAX
53 };
54
55 static const struct blobmsg_policy browse_policy[] = {
56 [BROWSE_SERVICE] = { "service", BLOBMSG_TYPE_STRING },
57 };
58
59 static int
60 umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
61 struct ubus_request_data *req, const char *method,
62 struct blob_attr *msg)
63 {
64 struct cache_service *s, *q;
65 char *buffer = (char *) mdns_buf;
66 struct blob_attr *data[BROWSE_MAX];
67 void *c1 = NULL, *c2;
68 char *service = NULL;
69
70 blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg));
71 if (data[BROWSE_SERVICE])
72 service = blobmsg_get_string(data[BROWSE_SERVICE]);
73
74 blob_buf_init(&b, 0);
75 avl_for_each_element(&services, s, avl) {
76 char *local;
77
78 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
79 local = strstr(buffer, ".local");
80 if (local)
81 *local = '\0';
82 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
83 continue;
84 if (service && strcmp(buffer, service))
85 continue;
86 if (!c1) {
87 c1 = blobmsg_open_table(&b, buffer);
88 }
89 snprintf(buffer, MAX_NAME_LEN, "%s", s->entry);
90 local = strstr(buffer, "._");
91 if (local)
92 *local = '\0';
93 c2 = blobmsg_open_table(&b, buffer);
94 strncat(buffer, ".local", MAX_NAME_LEN);
95 cache_dump_records(&b, buffer);
96 cache_dump_records(&b, s->entry);
97 blobmsg_close_table(&b, c2);
98 q = avl_next_element(s, avl);
99 if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
100 blobmsg_close_table(&b, c1);
101 c1 = NULL;
102 }
103 }
104 ubus_send_reply(ctx, req, b.head);
105
106 return UBUS_STATUS_OK;
107 }
108
109 static int
110 umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
111 struct ubus_request_data *req, const char *method,
112 struct blob_attr *msg)
113 {
114 struct cache_record *prev = NULL;
115 struct cache_record *r;
116 void *c;
117
118 blob_buf_init(&b, 0);
119 avl_for_each_element(&records, r, avl) {
120 if (r->type != TYPE_A && r->type != TYPE_AAAA)
121 continue;
122 /* Query each domain just once */
123 if (!prev || strcmp(r->record, prev->record)) {
124 c = blobmsg_open_table(&b, r->record);
125 cache_dump_records(&b, r->record);
126 blobmsg_close_table(&b, c);
127 }
128 prev = r;
129 }
130 ubus_send_reply(ctx, req, b.head);
131
132 return UBUS_STATUS_OK;
133 }
134
135 enum {
136 CFG_INTERFACES,
137 CFG_KEEP,
138 CFG_MAX
139 };
140
141 static const struct blobmsg_policy config_policy[] = {
142 [CFG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_ARRAY },
143 [CFG_KEEP] = { "keep", BLOBMSG_TYPE_BOOL },
144 };
145
146 static int
147 umdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
148 struct ubus_request_data *req, const char *method,
149 struct blob_attr *msg)
150 {
151 struct blob_attr *data[CFG_MAX], *cur;
152 int rem, keep = false;
153
154 blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg));
155 if (!data[CFG_INTERFACES])
156 return UBUS_STATUS_INVALID_ARGUMENT;
157
158 if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING))
159 return UBUS_STATUS_INVALID_ARGUMENT;
160
161 keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]);
162 if (!keep) {
163 vlist_update(&interfaces);
164 ubus_notify(ctx, obj, "set_config", NULL, 1000);
165 }
166
167 blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem)
168 interface_add(blobmsg_data(cur));
169
170 if (!keep)
171 vlist_flush(&interfaces);
172
173 return 0;
174 }
175
176 enum query_attr {
177 QUERY_QUESTION,
178 QUERY_IFACE,
179 QUERY_TYPE,
180 QUERY_MAX
181 };
182
183 static const struct blobmsg_policy query_policy[QUERY_MAX] = {
184 [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING },
185 [QUERY_IFACE] = { "interface", BLOBMSG_TYPE_STRING },
186 [QUERY_TYPE] = { "type", BLOBMSG_TYPE_INT32 },
187 };
188
189 static int
190 umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
191 struct ubus_request_data *req, const char *method,
192 struct blob_attr *msg)
193 {
194 struct blob_attr *tb[QUERY_MAX], *c;
195 const char *question = C_DNS_SD;
196 const char *ifname;
197 int type = TYPE_ANY;
198
199 blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg));
200
201 if (!(c = tb[QUERY_IFACE]))
202 return UBUS_STATUS_INVALID_ARGUMENT;
203
204 ifname = blobmsg_get_string(c);
205
206 if ((c = tb[QUERY_QUESTION]))
207 question = blobmsg_get_string(c);
208
209 if ((c = tb[QUERY_TYPE]))
210 type = blobmsg_get_u32(c);
211
212 struct interface *iface_v4 = interface_get(ifname, 0, 1);
213 struct interface *iface_v6 = interface_get(ifname, 1, 1);
214
215 if (!iface_v4 && !iface_v6)
216 return UBUS_STATUS_NOT_FOUND;
217
218 if (!strcmp(method, "query")) {
219 if (iface_v4)
220 dns_send_question(iface_v4, NULL, question, type, 1);
221
222 if (iface_v6)
223 dns_send_question(iface_v6, NULL, question, type, 1);
224
225 return UBUS_STATUS_OK;
226 } else if (!strcmp(method, "fetch")) {
227 blob_buf_init(&b, 0);
228 void *k = blobmsg_open_array(&b, "records");
229 cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6);
230 blobmsg_close_array(&b, k);
231 ubus_send_reply(ctx, req, b.head);
232 return UBUS_STATUS_OK;
233 } else {
234 return UBUS_STATUS_INVALID_ARGUMENT;
235 }
236 }
237
238
239 static const struct ubus_method umdns_methods[] = {
240 UBUS_METHOD("set_config", umdns_set_config, config_policy),
241 UBUS_METHOD("query", umdns_query, query_policy),
242 UBUS_METHOD("fetch", umdns_query, query_policy),
243 UBUS_METHOD("browse", umdns_browse, browse_policy),
244 UBUS_METHOD_NOARG("update", umdns_update),
245 UBUS_METHOD_NOARG("hosts", umdns_hosts),
246 UBUS_METHOD_NOARG("reload", umdns_reload),
247 };
248
249 static struct ubus_object_type umdns_object_type =
250 UBUS_OBJECT_TYPE("umdns", umdns_methods);
251
252 static struct ubus_object umdns_object = {
253 .name = "umdns",
254 .type = &umdns_object_type,
255 .methods = umdns_methods,
256 .n_methods = ARRAY_SIZE(umdns_methods),
257 };
258
259 static void
260 ubus_connect_handler(struct ubus_context *ctx)
261 {
262 int ret;
263
264 ret = ubus_add_object(ctx, &umdns_object);
265 if (ret)
266 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
267 }
268
269 void
270 ubus_startup(void)
271 {
272 conn.cb = ubus_connect_handler;
273 ubus_auto_connect(&conn);
274 }
275
276 int ubus_service_list(ubus_data_handler_t cb)
277 {
278 uint32_t id;
279 int ret;
280
281 blob_buf_init(&b, 0);
282 ret = ubus_lookup_id(&conn.ctx, "service", &id);
283 if (ret)
284 return ret;
285
286 return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000);
287 }