ubus: assume that the service iface can be NULL
[project/mdnsd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 69db2ac8c34f60aa07a91f08f97477d33ef11a41..69912784784b80e3bb01bd233a8379a8554b8969 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -28,6 +28,7 @@
 
 static struct ubus_auto_conn conn;
 static struct blob_buf b;
+static struct ubus_subscriber udebug_sub;
 
 static int
 umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
@@ -47,6 +48,19 @@ umdns_update(struct ubus_context *ctx, struct ubus_object *obj,
        return 0;
 }
 
+enum {
+       BROWSE_SERVICE,
+       BROWSE_ARRAY,
+       BROWSE_ADDRESS,
+       BROWSE_MAX
+};
+
+static const struct blobmsg_policy browse_policy[] = {
+       [BROWSE_SERVICE]        = { "service", BLOBMSG_TYPE_STRING },
+       [BROWSE_ARRAY]          = { "array", BLOBMSG_TYPE_BOOL },
+       [BROWSE_ADDRESS]        = { "address", BLOBMSG_TYPE_BOOL },
+};
+
 static int
 umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
@@ -54,31 +68,47 @@ umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
 {
        struct cache_service *s, *q;
        char *buffer = (char *) mdns_buf;
+       struct blob_attr *data[BROWSE_MAX];
        void *c1 = NULL, *c2;
+       char *service = NULL;
+       int array = 0;
+       bool address = true;
+
+       blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg));
+       if (data[BROWSE_SERVICE])
+               service = blobmsg_get_string(data[BROWSE_SERVICE]);
+       if (data[BROWSE_ARRAY])
+               array = blobmsg_get_u8(data[BROWSE_ARRAY]);
+       if (data[BROWSE_ADDRESS])
+               address = blobmsg_get_bool(data[BROWSE_ADDRESS]);
 
        blob_buf_init(&b, 0);
        avl_for_each_element(&services, s, avl) {
+               const char *hostname = buffer;
                char *local;
-               if (*((char *) s->avl.key) != '_')
-                       continue;
+
                snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
                local = strstr(buffer, ".local");
                if (local)
                        *local = '\0';
                if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
                        continue;
-
+               if (service && strcmp(buffer, service))
+                       continue;
                if (!c1) {
                        c1 = blobmsg_open_table(&b, buffer);
                }
-               snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->entry);
+               snprintf(buffer, MAX_NAME_LEN, "%s", s->entry);
                local = strstr(buffer, "._");
                if (local)
                        *local = '\0';
                c2 = blobmsg_open_table(&b, buffer);
                strncat(buffer, ".local", MAX_NAME_LEN);
-               cache_dump_records(&b, buffer);
-               cache_dump_records(&b, s->entry);
+               if (s->iface)
+                       blobmsg_add_string(&b, "iface", s->iface->name);
+               cache_dump_records(&b, s->entry, array, &hostname);
+               if (address)
+                       cache_dump_records(&b, hostname, array, NULL);
                blobmsg_close_table(&b, c2);
                q = avl_next_element(s, avl);
                if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
@@ -91,29 +121,40 @@ umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
        return UBUS_STATUS_OK;
 }
 
+enum {
+       HOSTS_ARRAY,
+       __HOSTS_MAX
+};
+static const struct blobmsg_policy hosts_policy[] = {
+       [HOSTS_ARRAY] = { "array", BLOBMSG_TYPE_BOOL }
+};
+
 static int
 umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       struct cache_service *s;
-       char *buffer = (char *) mdns_buf;
+       struct cache_record *prev = NULL;
+       struct blob_attr *tb[__HOSTS_MAX];
+       struct cache_record *r;
+       bool array = false;
        void *c;
 
+       blobmsg_parse(hosts_policy, __HOSTS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
+       if (tb[HOSTS_ARRAY])
+               array = blobmsg_get_bool(tb[HOSTS_ARRAY]);
+
        blob_buf_init(&b, 0);
-       avl_for_each_element(&services, s, avl) {
-               char *local;
-               if (*((char *) s->avl.key) == '_')
+       avl_for_each_element(&records, r, avl) {
+               if (r->type != TYPE_A && r->type != TYPE_AAAA)
                        continue;
-               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);
-               cache_dump_records(&b, buffer);
-               cache_dump_records(&b, s->entry);
-               blobmsg_close_table(&b, c);
+               /* Query each domain just once */
+               if (!prev || strcmp(r->record, prev->record)) {
+                       c = blobmsg_open_table(&b, r->record);
+                       cache_dump_records(&b, r->record, array, NULL);
+                       blobmsg_close_table(&b, c);
+               }
+               prev = r;
        }
        ubus_send_reply(ctx, req, b.head);
 
@@ -180,7 +221,7 @@ umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
                    struct blob_attr *msg)
 {
        struct blob_attr *tb[QUERY_MAX], *c;
-       const char *question = "_services._dns-sd._udp.local";
+       const char *question = C_DNS_SD;
        const char *ifname;
        int type = TYPE_ANY;
 
@@ -197,18 +238,18 @@ umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
        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);
+       struct interface *iface_v4 = interface_get(ifname, SOCK_MC_IPV4);
+       struct interface *iface_v6 = interface_get(ifname, SOCK_MC_IPV6);
 
        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);
+                       dns_send_question(iface_v4, NULL, question, type, 1);
 
                if (iface_v6)
-                       dns_send_question(iface_v6, question, type, 1);
+                       dns_send_question(iface_v6, NULL, question, type, 1);
 
                return UBUS_STATUS_OK;
        } else if (!strcmp(method, "fetch")) {
@@ -228,9 +269,9 @@ static const struct ubus_method umdns_methods[] = {
        UBUS_METHOD("set_config", umdns_set_config, config_policy),
        UBUS_METHOD("query", umdns_query, query_policy),
        UBUS_METHOD("fetch", umdns_query, query_policy),
+       UBUS_METHOD("browse", umdns_browse, browse_policy),
        UBUS_METHOD_NOARG("update", umdns_update),
-       UBUS_METHOD_NOARG("browse", umdns_browse),
-       UBUS_METHOD_NOARG("hosts", umdns_hosts),
+       UBUS_METHOD("hosts", umdns_hosts, hosts_policy),
        UBUS_METHOD_NOARG("reload", umdns_reload),
 };
 
@@ -244,14 +285,87 @@ static struct ubus_object umdns_object = {
        .n_methods = ARRAY_SIZE(umdns_methods),
 };
 
+static struct blob_attr *
+find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
+{
+       struct blobmsg_policy policy = { name, type };
+       struct blob_attr *ret;
+
+       if (!attr)
+               return NULL;
+
+       blobmsg_parse_attr(&policy, 1, &ret, attr);
+
+       return ret;
+}
+
+static void
+umdns_udebug_config_cb(struct blob_attr *data)
+{
+       enum {
+               CFG_ATTR_ENABLED,
+               __CFG_ATTR_MAX
+       };
+       static const struct blobmsg_policy policy[__CFG_ATTR_MAX] = {
+               [CFG_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_STRING },
+       };
+       struct blob_attr *tb[__CFG_ATTR_MAX];
+       bool en;
+
+       data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
+       data = find_attr(data, "umdns", BLOBMSG_TYPE_TABLE);
+       if (!data)
+               return;
+
+       blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data);
+       if (!tb[CFG_ATTR_ENABLED])
+               return;
+
+       en = !!atoi(blobmsg_get_string(tb[CFG_ATTR_ENABLED]));
+       umdns_udebug_set_enabled(en);
+}
+
+static int
+umdns_udebug_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
+                       struct ubus_request_data *req, const char *method,
+                       struct blob_attr *msg)
+{
+       umdns_udebug_config_cb(msg);
+
+       return 0;
+}
+
+static void
+umdns_udebug_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+       umdns_udebug_config_cb(msg);
+}
+
+static bool
+umdns_udebug_sub_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
+                    const char *path)
+{
+       return !strcmp(path, "udebug");
+}
+
+
 static void
 ubus_connect_handler(struct ubus_context *ctx)
 {
+       uint32_t id;
        int ret;
 
        ret = ubus_add_object(ctx, &umdns_object);
        if (ret)
                fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
+
+       udebug_sub.cb = umdns_udebug_notify_cb;
+       udebug_sub.new_obj_cb = umdns_udebug_sub_cb;
+       ubus_register_subscriber(&conn.ctx, &udebug_sub);
+       if (ubus_lookup_id(&conn.ctx, "udebug", &id) == 0) {
+               ubus_subscribe(&conn.ctx, &udebug_sub, id);
+               ubus_invoke(&conn.ctx, id, "get_config", NULL, umdns_udebug_req_cb, NULL, 1000);
+       }
 }
 
 void