Simplify UDP vs. TCP handling in service_announce_services
[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 mdns_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 mdns_scan(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_scan();
47         return 0;
48 }
49
50 static int
51 mdns_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 (*((char *) s->avl.key) != '_')
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", (const char *) 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 mdns_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         char *buffer = (char *) mdns_buf;
101         void *c;
102
103         blob_buf_init(&b, 0);
104         avl_for_each_element(&services, s, avl) {
105                 char *local;
106                 if (*((char *) s->avl.key) == '_')
107                         continue;
108                 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->entry);
109                 local = strstr(buffer, "._");
110                 if (local)
111                         *local = '\0';
112                 c = blobmsg_open_table(&b, buffer);
113                 strncat(buffer, ".local", MAX_NAME_LEN);
114                 cache_dump_records(&b, buffer);
115                 cache_dump_records(&b, s->entry);
116                 blobmsg_close_table(&b, c);
117         }
118         ubus_send_reply(ctx, req, b.head);
119
120         return UBUS_STATUS_OK;
121 }
122
123 enum {
124         CFG_INTERFACES,
125         CFG_KEEP,
126         CFG_MAX
127 };
128
129 static const struct blobmsg_policy config_policy[] = {
130         [CFG_INTERFACES]        = { "interfaces", BLOBMSG_TYPE_ARRAY },
131         [CFG_KEEP]              = { "keep", BLOBMSG_TYPE_BOOL },
132 };
133
134 static int
135 mdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
136                     struct ubus_request_data *req, const char *method,
137                     struct blob_attr *msg)
138 {
139         struct blob_attr *data[CFG_MAX], *cur;
140         int rem, keep = false;
141
142         blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg));
143         if (!data[CFG_INTERFACES])
144                 return UBUS_STATUS_INVALID_ARGUMENT;
145
146         if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING))
147                 return UBUS_STATUS_INVALID_ARGUMENT;
148
149         keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]);
150         if (!keep) {
151                 vlist_update(&interfaces);
152                 ubus_notify(ctx, obj, "set_config", NULL, 1000);
153         }
154
155         blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem)
156                 interface_add(blobmsg_data(cur));
157
158         if (!keep)
159                 vlist_flush(&interfaces);
160
161         return 0;
162 }
163
164 enum query_attr {
165         QUERY_QUESTION,
166         QUERY_IFACE,
167         QUERY_TYPE,
168         QUERY_MAX
169 };
170
171 static const struct blobmsg_policy query_policy[QUERY_MAX] = {
172         [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING },
173         [QUERY_IFACE]   = { "interface", BLOBMSG_TYPE_STRING },
174         [QUERY_TYPE]    = { "type", BLOBMSG_TYPE_INT32 },
175 };
176
177 static int
178 mdns_query(struct ubus_context *ctx, struct ubus_object *obj,
179                     struct ubus_request_data *req, const char *method,
180                     struct blob_attr *msg)
181 {
182         struct blob_attr *tb[QUERY_MAX], *c;
183         const char *question = "_services._dns-sd._udp.local";
184         const char *ifname;
185         int type = TYPE_ANY;
186
187         blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg));
188
189         if (!(c = tb[QUERY_IFACE]))
190                 return UBUS_STATUS_INVALID_ARGUMENT;
191
192         ifname = blobmsg_get_string(c);
193
194         if ((c = tb[QUERY_QUESTION]))
195                 question = blobmsg_get_string(c);
196
197         if ((c = tb[QUERY_TYPE]))
198                 type = blobmsg_get_u32(c);
199
200         struct interface *iface_v4 = interface_get(ifname, 0, 1);
201         struct interface *iface_v6 = interface_get(ifname, 1, 1);
202
203         if (!iface_v4 && !iface_v6)
204                 return UBUS_STATUS_NOT_FOUND;
205
206         if (!strcmp(method, "query")) {
207                 if (iface_v4)
208                         dns_send_question(iface_v4, question, type, 1);
209
210                 if (iface_v6)
211                         dns_send_question(iface_v6, question, type, 1);
212
213                 return UBUS_STATUS_OK;
214         } else if (!strcmp(method, "fetch")) {
215                 blob_buf_init(&b, 0);
216                 void *k = blobmsg_open_array(&b, "records");
217                 cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6);
218                 blobmsg_close_array(&b, k);
219                 ubus_send_reply(ctx, req, b.head);
220                 return UBUS_STATUS_OK;
221         } else {
222                 return UBUS_STATUS_INVALID_ARGUMENT;
223         }
224 }
225
226
227 static const struct ubus_method mdns_methods[] = {
228         UBUS_METHOD("set_config", mdns_set_config, config_policy),
229         UBUS_METHOD("query", mdns_query, query_policy),
230         UBUS_METHOD("fetch", mdns_query, query_policy),
231         UBUS_METHOD_NOARG("scan", mdns_scan),
232         UBUS_METHOD_NOARG("browse", mdns_browse),
233         UBUS_METHOD_NOARG("hosts", mdns_hosts),
234         UBUS_METHOD_NOARG("reload", mdns_reload),
235 };
236
237 static struct ubus_object_type mdns_object_type =
238         UBUS_OBJECT_TYPE("mdns", mdns_methods);
239
240 static struct ubus_object mdns_object = {
241         .name = "mdns",
242         .type = &mdns_object_type,
243         .methods = mdns_methods,
244         .n_methods = ARRAY_SIZE(mdns_methods),
245 };
246
247 static void
248 ubus_connect_handler(struct ubus_context *ctx)
249 {
250         int ret;
251
252         ret = ubus_add_object(ctx, &mdns_object);
253         if (ret)
254                 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
255 }
256
257 void
258 ubus_startup(void)
259 {
260         conn.cb = ubus_connect_handler;
261         ubus_auto_connect(&conn);
262 }
263
264 int ubus_service_list(ubus_data_handler_t cb)
265 {
266         uint32_t id;
267         int ret;
268
269         blob_buf_init(&b, 0);
270         ret = ubus_lookup_id(&conn.ctx, "service", &id);
271         if (ret)
272                 return ret;
273
274         return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000);
275 }