Add query ubus call
authorSteven Barth <steven@midlink.org>
Sun, 26 Oct 2014 19:00:21 +0000 (20:00 +0100)
committerSteven Barth <steven@midlink.org>
Sun, 26 Oct 2014 19:00:21 +0000 (20:00 +0100)
Signed-off-by: Steven Barth <steven@midlink.org>
interface.c
interface.h
ubus.c

index 70c02aa..c8d8972 100644 (file)
@@ -627,4 +627,13 @@ void interface_shutdown(void)
                interface_close(iface);
 }
 
+struct interface*
+interface_get(const char *name, int v6, int multicast)
+{
+       char id_buf[32];
+       snprintf(id_buf, sizeof(id_buf), "%d_%d_%s", multicast, v6, name);
+       struct interface *iface = vlist_find(&interfaces, id_buf, iface, node);
+       return iface;
+}
+
 VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);
index 4bd8e0c..50d7071 100644 (file)
@@ -54,5 +54,6 @@ struct interface {
 int interface_add(const char *name);
 void interface_shutdown(void);
 int interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len);
+struct interface* interface_get(const char *name, int v6, int multicast);
 
 #endif
diff --git a/ubus.c b/ubus.c
index 67c3af7..ebd3157 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -147,9 +147,61 @@ mdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
        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 (iface_v4)
+               dns_send_question(iface_v4, question, type, 0);
+
+       if (iface_v6)
+               dns_send_question(iface_v6, question, type, 0);
+
+       return UBUS_STATUS_OK;
+}
+
 
 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_NOARG("scan", mdns_scan),
        UBUS_METHOD_NOARG("browse", mdns_browse),
        UBUS_METHOD_NOARG("hosts", mdns_hosts),