2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
14 #include <sys/types.h>
15 #include <arpa/inet.h>
20 #include <libubox/vlist.h>
21 #include <libubox/uloop.h>
27 #include "interface.h"
29 static struct ubus_auto_conn conn;
30 static struct blob_buf b;
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)
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)
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)
55 struct cache_service *s, *q;
56 char *buffer = (char *) mdns_buf;
60 avl_for_each_element(&services, s, avl) {
62 if (cache_service_is_host(s))
64 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
65 local = strstr(buffer, ".local");
68 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
72 c1 = blobmsg_open_table(&b, buffer);
74 snprintf(buffer, MAX_NAME_LEN, "%s", s->entry);
75 local = strstr(buffer, "._");
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);
89 ubus_send_reply(ctx, req, b.head);
91 return UBUS_STATUS_OK;
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)
99 struct cache_record *prev = NULL;
100 struct cache_record *r;
103 blob_buf_init(&b, 0);
104 avl_for_each_element(&records, r, avl) {
105 if (r->type != TYPE_A && r->type != TYPE_AAAA)
107 /* Query each domain just once */
108 if (!prev || strcmp(r->record, prev->record)) {
109 c = blobmsg_open_table(&b, r->record);
110 cache_dump_records(&b, r->record);
111 blobmsg_close_table(&b, c);
115 ubus_send_reply(ctx, req, b.head);
117 return UBUS_STATUS_OK;
126 static const struct blobmsg_policy config_policy[] = {
127 [CFG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_ARRAY },
128 [CFG_KEEP] = { "keep", BLOBMSG_TYPE_BOOL },
132 umdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
133 struct ubus_request_data *req, const char *method,
134 struct blob_attr *msg)
136 struct blob_attr *data[CFG_MAX], *cur;
137 int rem, keep = false;
139 blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg));
140 if (!data[CFG_INTERFACES])
141 return UBUS_STATUS_INVALID_ARGUMENT;
143 if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING))
144 return UBUS_STATUS_INVALID_ARGUMENT;
146 keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]);
148 vlist_update(&interfaces);
149 ubus_notify(ctx, obj, "set_config", NULL, 1000);
152 blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem)
153 interface_add(blobmsg_data(cur));
156 vlist_flush(&interfaces);
168 static const struct blobmsg_policy query_policy[QUERY_MAX] = {
169 [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING },
170 [QUERY_IFACE] = { "interface", BLOBMSG_TYPE_STRING },
171 [QUERY_TYPE] = { "type", BLOBMSG_TYPE_INT32 },
175 umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
176 struct ubus_request_data *req, const char *method,
177 struct blob_attr *msg)
179 struct blob_attr *tb[QUERY_MAX], *c;
180 const char *question = "_services._dns-sd._udp.local";
184 blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg));
186 if (!(c = tb[QUERY_IFACE]))
187 return UBUS_STATUS_INVALID_ARGUMENT;
189 ifname = blobmsg_get_string(c);
191 if ((c = tb[QUERY_QUESTION]))
192 question = blobmsg_get_string(c);
194 if ((c = tb[QUERY_TYPE]))
195 type = blobmsg_get_u32(c);
197 struct interface *iface_v4 = interface_get(ifname, 0, 1);
198 struct interface *iface_v6 = interface_get(ifname, 1, 1);
200 if (!iface_v4 && !iface_v6)
201 return UBUS_STATUS_NOT_FOUND;
203 if (!strcmp(method, "query")) {
205 dns_send_question(iface_v4, question, type, 1);
208 dns_send_question(iface_v6, question, type, 1);
210 return UBUS_STATUS_OK;
211 } else if (!strcmp(method, "fetch")) {
212 blob_buf_init(&b, 0);
213 void *k = blobmsg_open_array(&b, "records");
214 cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6);
215 blobmsg_close_array(&b, k);
216 ubus_send_reply(ctx, req, b.head);
217 return UBUS_STATUS_OK;
219 return UBUS_STATUS_INVALID_ARGUMENT;
224 static const struct ubus_method umdns_methods[] = {
225 UBUS_METHOD("set_config", umdns_set_config, config_policy),
226 UBUS_METHOD("query", umdns_query, query_policy),
227 UBUS_METHOD("fetch", umdns_query, query_policy),
228 UBUS_METHOD_NOARG("update", umdns_update),
229 UBUS_METHOD_NOARG("browse", umdns_browse),
230 UBUS_METHOD_NOARG("hosts", umdns_hosts),
231 UBUS_METHOD_NOARG("reload", umdns_reload),
234 static struct ubus_object_type umdns_object_type =
235 UBUS_OBJECT_TYPE("umdns", umdns_methods);
237 static struct ubus_object umdns_object = {
239 .type = &umdns_object_type,
240 .methods = umdns_methods,
241 .n_methods = ARRAY_SIZE(umdns_methods),
245 ubus_connect_handler(struct ubus_context *ctx)
249 ret = ubus_add_object(ctx, &umdns_object);
251 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
257 conn.cb = ubus_connect_handler;
258 ubus_auto_connect(&conn);
261 int ubus_service_list(ubus_data_handler_t cb)
266 blob_buf_init(&b, 0);
267 ret = ubus_lookup_id(&conn.ctx, "service", &id);
271 return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000);