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