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