Support specifying instance name in JSON file
[project/mdnsd.git] / ubus.c
diff --git a/ubus.c b/ubus.c
index 19bc33f0f7a6008e927a5afac9aa81065a0c118d..7b1c811c67fb4699778370f48ee37ef5b89029de 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -30,7 +30,7 @@ static struct ubus_auto_conn conn;
 static struct blob_buf b;
 
 static int
-mdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
+umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
@@ -39,16 +39,16 @@ mdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int
-mdns_scan(struct ubus_context *ctx, struct ubus_object *obj,
+umdns_update(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
-       cache_scan();
+       cache_update();
        return 0;
 }
 
 static int
-mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
+umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
                struct ubus_request_data *req, const char *method,
                struct blob_attr *msg)
 {
@@ -59,8 +59,7 @@ mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
        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", (const char *) s->avl.key);
                local = strstr(buffer, ".local");
                if (local)
@@ -71,7 +70,7 @@ mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
                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';
@@ -92,28 +91,25 @@ mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 static int
-mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
+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 cache_record *r;
        void *c;
 
        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);
+                       blobmsg_close_table(&b, c);
+               }
+               prev = r;
        }
        ubus_send_reply(ctx, req, b.head);
 
@@ -132,7 +128,7 @@ static const struct blobmsg_policy config_policy[] = {
 };
 
 static int
-mdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
+umdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
                    struct ubus_request_data *req, const char *method,
                    struct blob_attr *msg)
 {
@@ -175,12 +171,12 @@ static const struct blobmsg_policy query_policy[QUERY_MAX] = {
 };
 
 static int
-mdns_query(struct ubus_context *ctx, struct ubus_object *obj,
+umdns_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 *question = C_DNS_SD;
        const char *ifname;
        int type = TYPE_ANY;
 
@@ -205,10 +201,10 @@ mdns_query(struct ubus_context *ctx, struct ubus_object *obj,
 
        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")) {
@@ -224,24 +220,24 @@ mdns_query(struct ubus_context *ctx, struct ubus_object *obj,
 }
 
 
-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),
-       UBUS_METHOD_NOARG("reload", mdns_reload),
+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_NOARG("update", umdns_update),
+       UBUS_METHOD_NOARG("browse", umdns_browse),
+       UBUS_METHOD_NOARG("hosts", umdns_hosts),
+       UBUS_METHOD_NOARG("reload", umdns_reload),
 };
 
-static struct ubus_object_type mdns_object_type =
-       UBUS_OBJECT_TYPE("mdns", mdns_methods);
+static struct ubus_object_type umdns_object_type =
+       UBUS_OBJECT_TYPE("umdns", umdns_methods);
 
-static struct ubus_object mdns_object = {
-       .name = "mdns",
-       .type = &mdns_object_type,
-       .methods = mdns_methods,
-       .n_methods = ARRAY_SIZE(mdns_methods),
+static struct ubus_object umdns_object = {
+       .name = "umdns",
+       .type = &umdns_object_type,
+       .methods = umdns_methods,
+       .n_methods = ARRAY_SIZE(umdns_methods),
 };
 
 static void
@@ -249,7 +245,7 @@ ubus_connect_handler(struct ubus_context *ctx)
 {
        int ret;
 
-       ret = ubus_add_object(ctx, &mdns_object);
+       ret = ubus_add_object(ctx, &umdns_object);
        if (ret)
                fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
 }