X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=ubus.c;h=9a16a8645a7aeb2b4202b34ef0bc6f9c6e436740;hb=95d8e0d4f03dd7c1211d3f98dc31074365717acf;hp=32e30876ed5c276ee4962e68679ef277b39316bf;hpb=aee2d5582e56582ed0dc3096b364630b2ecfd56a;p=project%2Fmdnsd.git diff --git a/ubus.c b/ubus.c index 32e3087..9a16a86 100644 --- 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,7 +39,7 @@ mdns_reload(struct ubus_context *ctx, struct ubus_object *obj, } static int -mdns_scan(struct ubus_context *ctx, struct ubus_object *obj, +umdns_scan(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { @@ -48,16 +48,16 @@ mdns_scan(struct ubus_context *ctx, struct ubus_object *obj, } 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) { - struct cache_entry *s, *q; + 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) { + avl_for_each_element(&services, s, avl) { char *local; if (*((char *) s->avl.key) != '_') continue; @@ -81,7 +81,7 @@ mdns_browse(struct ubus_context *ctx, struct ubus_object *obj, 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; } @@ -92,16 +92,16 @@ 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_entry *s; + struct cache_service *s; char *buffer = (char *) mdns_buf; void *c; blob_buf_init(&b, 0); - avl_for_each_element(&entries, s, avl) { + avl_for_each_element(&services, s, avl) { char *local; if (*((char *) s->avl.key) == '_') continue; @@ -120,50 +120,128 @@ 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[] = { - { "interfaces", BLOBMSG_TYPE_ARRAY }, + [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, +umdns_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, *cur; - int rem; + struct blob_attr *data[CFG_MAX], *cur; + int rem, keep = false; - blobmsg_parse(config_policy, 1, &data, blob_data(msg), blob_len(msg)); - if (!data) + 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, BLOBMSG_TYPE_STRING)) + if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING)) return UBUS_STATUS_INVALID_ARGUMENT; - vlist_update(&interfaces); - blobmsg_for_each_attr(cur, data, rem) + 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)); - vlist_flush(&interfaces); + + 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 +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 *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_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("scan", umdns_scan), + 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 @@ -171,7 +249,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)); }