Check correct attributes when loading service from blob
[project/mdnsd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index c474bb5..19bc33f 100644 (file)
--- a/ubus.c
+++ b/ubus.c
 #include <stdio.h>
 
 #include <libubus.h>
-#include <libubox/avl.h>
+#include <libubox/vlist.h>
 #include <libubox/uloop.h>
 
+#include "util.h"
 #include "ubus.h"
 #include "cache.h"
+#include "service.h"
+#include "interface.h"
 
 static struct ubus_auto_conn conn;
 static struct blob_buf b;
@@ -31,6 +34,7 @@ mdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
+       service_init(1);
        return 0;
 }
 
@@ -43,63 +47,21 @@ mdns_scan(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
-static void
-mdns_add_records(char *name)
-{
-       struct cache_record *r, *q = avl_find_element(&records, name, r, avl);
-       char *txt;
-       char buffer[MAX_NAME_LEN];
-
-       if (!q)
-               return;
-
-       do {
-               r = q;
-               switch (r->type) {
-               case TYPE_TXT:
-                       if (r->txt && strlen(r->txt)) {
-                               txt = r->txt;
-                               do {
-                                       blobmsg_add_string(&b, "txt", txt);
-                                       txt = &txt[strlen(txt) + 1];
-                               } while (*txt);
-                       }
-                       break;
-
-               case TYPE_SRV:
-                       if (r->port)
-                               blobmsg_add_u32(&b, "port", r->port);
-                       break;
-
-               case TYPE_A:
-                       if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
-                               blobmsg_add_string(&b, "ipv4", buffer);
-                       break;
-
-               case TYPE_AAAA:
-                       if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
-                               blobmsg_add_string(&b, "ipv6", buffer);
-                       break;
-               }
-               q = avl_next_element(r, avl);
-       } while (q && !strcmp(r->record, q->record));
-}
-
 static int
 mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       struct cache_entry *s, *q;
-       char buffer[MAX_NAME_LEN];
+       struct cache_service *s, *q;
+       char *buffer = (char *) mdns_buf;
        void *c1 = NULL, *c2;
 
-        blob_buf_init(&b, 0);
-       avl_for_each_element(&entries, s, avl) {
+       blob_buf_init(&b, 0);
+       avl_for_each_element(&services, s, avl) {
                char *local;
                if (*((char *) s->avl.key) != '_')
                        continue;
-               snprintf(buffer, MAX_NAME_LEN, s->avl.key);
+               snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
                local = strstr(buffer, ".local");
                if (local)
                        *local = '\0';
@@ -107,22 +69,19 @@ mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
                        continue;
 
                if (!c1) {
-                       char *type = cache_lookup_name(buffer);
                        c1 = blobmsg_open_table(&b, buffer);
-                       if (type)
-                               blobmsg_add_string(&b, ".desc", type);
                }
-               snprintf(buffer, MAX_NAME_LEN, s->entry);
+               snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->entry);
                local = strstr(buffer, "._");
                if (local)
                        *local = '\0';
                c2 = blobmsg_open_table(&b, buffer);
                strncat(buffer, ".local", MAX_NAME_LEN);
-               mdns_add_records(buffer);
-               mdns_add_records(s->entry);
+               cache_dump_records(&b, buffer);
+               cache_dump_records(&b, s->entry);
                blobmsg_close_table(&b, c2);
                q = avl_next_element(s, avl);
-               if (!q || avl_is_last(&entries, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
+               if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
                        blobmsg_close_table(&b, c1);
                        c1 = NULL;
                }
@@ -137,23 +96,23 @@ mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       struct cache_entry *s;
-       char buffer[MAX_NAME_LEN];
+       struct cache_service *s;
+       char *buffer = (char *) mdns_buf;
        void *c;
 
-        blob_buf_init(&b, 0);
-       avl_for_each_element(&entries, s, avl) {
+       blob_buf_init(&b, 0);
+       avl_for_each_element(&services, s, avl) {
                char *local;
                if (*((char *) s->avl.key) == '_')
                        continue;
-               snprintf(buffer, MAX_NAME_LEN, s->entry);
+               snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->entry);
                local = strstr(buffer, "._");
                if (local)
                        *local = '\0';
                c = blobmsg_open_table(&b, buffer);
                strncat(buffer, ".local", MAX_NAME_LEN);
-               mdns_add_records(buffer);
-               mdns_add_records(s->entry);
+               cache_dump_records(&b, buffer);
+               cache_dump_records(&b, s->entry);
                blobmsg_close_table(&b, c);
        }
        ubus_send_reply(ctx, req, b.head);
@@ -161,7 +120,114 @@ mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
        return UBUS_STATUS_OK;
 }
 
+enum {
+       CFG_INTERFACES,
+       CFG_KEEP,
+       CFG_MAX
+};
+
+static const struct blobmsg_policy config_policy[] = {
+       [CFG_INTERFACES]        = { "interfaces", BLOBMSG_TYPE_ARRAY },
+       [CFG_KEEP]              = { "keep", BLOBMSG_TYPE_BOOL },
+};
+
+static int
+mdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
+                   struct ubus_request_data *req, const char *method,
+                   struct blob_attr *msg)
+{
+       struct blob_attr *data[CFG_MAX], *cur;
+       int rem, keep = false;
+
+       blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg));
+       if (!data[CFG_INTERFACES])
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING))
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]);
+       if (!keep) {
+               vlist_update(&interfaces);
+               ubus_notify(ctx, obj, "set_config", NULL, 1000);
+       }
+
+       blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem)
+               interface_add(blobmsg_data(cur));
+
+       if (!keep)
+               vlist_flush(&interfaces);
+
+       return 0;
+}
+
+enum query_attr {
+       QUERY_QUESTION,
+       QUERY_IFACE,
+       QUERY_TYPE,
+       QUERY_MAX
+};
+
+static const struct blobmsg_policy query_policy[QUERY_MAX] = {
+       [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING },
+       [QUERY_IFACE]   = { "interface", BLOBMSG_TYPE_STRING },
+       [QUERY_TYPE]    = { "type", BLOBMSG_TYPE_INT32 },
+};
+
+static int
+mdns_query(struct ubus_context *ctx, struct ubus_object *obj,
+                   struct ubus_request_data *req, const char *method,
+                   struct blob_attr *msg)
+{
+       struct blob_attr *tb[QUERY_MAX], *c;
+       const char *question = "_services._dns-sd._udp.local";
+       const char *ifname;
+       int type = TYPE_ANY;
+
+       blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg));
+
+       if (!(c = tb[QUERY_IFACE]))
+               return UBUS_STATUS_INVALID_ARGUMENT;
+
+       ifname = blobmsg_get_string(c);
+
+       if ((c = tb[QUERY_QUESTION]))
+               question = blobmsg_get_string(c);
+
+       if ((c = tb[QUERY_TYPE]))
+               type = blobmsg_get_u32(c);
+
+       struct interface *iface_v4 = interface_get(ifname, 0, 1);
+       struct interface *iface_v6 = interface_get(ifname, 1, 1);
+
+       if (!iface_v4 && !iface_v6)
+               return UBUS_STATUS_NOT_FOUND;
+
+       if (!strcmp(method, "query")) {
+               if (iface_v4)
+                       dns_send_question(iface_v4, question, type, 1);
+
+               if (iface_v6)
+                       dns_send_question(iface_v6, question, type, 1);
+
+               return UBUS_STATUS_OK;
+       } else if (!strcmp(method, "fetch")) {
+               blob_buf_init(&b, 0);
+               void *k = blobmsg_open_array(&b, "records");
+               cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6);
+               blobmsg_close_array(&b, k);
+               ubus_send_reply(ctx, req, b.head);
+               return UBUS_STATUS_OK;
+       } else {
+               return UBUS_STATUS_INVALID_ARGUMENT;
+       }
+}
+
+
 static const struct ubus_method mdns_methods[] = {
+       UBUS_METHOD("set_config", mdns_set_config, config_policy),
+       UBUS_METHOD("query", mdns_query, query_policy),
+       UBUS_METHOD("fetch", mdns_query, query_policy),
        UBUS_METHOD_NOARG("scan", mdns_scan),
        UBUS_METHOD_NOARG("browse", mdns_browse),
        UBUS_METHOD_NOARG("hosts", mdns_hosts),
@@ -194,3 +260,16 @@ ubus_startup(void)
        conn.cb = ubus_connect_handler;
        ubus_auto_connect(&conn);
 }
+
+int ubus_service_list(ubus_data_handler_t cb)
+{
+       uint32_t id;
+       int ret;
+
+       blob_buf_init(&b, 0);
+       ret = ubus_lookup_id(&conn.ctx, "service", &id);
+       if (ret)
+               return ret;
+
+       return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000);
+}