ubus.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /*
  2. * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License version 2.1
  6. * as published by the Free Software Foundation
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <sys/types.h>
  14. #include <arpa/inet.h>
  15. #include <stdio.h>
  16. #include <libubus.h>
  17. #include <libubox/vlist.h>
  18. #include <libubox/uloop.h>
  19. #include "util.h"
  20. #include "ubus.h"
  21. #include "cache.h"
  22. #include "service.h"
  23. #include "interface.h"
  24. static struct ubus_auto_conn conn;
  25. static struct blob_buf b;
  26. static struct ubus_subscriber udebug_sub;
  27. static int
  28. umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
  29. struct ubus_request_data *req, const char *method,
  30. struct blob_attr *msg)
  31. {
  32. service_init(1);
  33. return 0;
  34. }
  35. static int
  36. umdns_update(struct ubus_context *ctx, struct ubus_object *obj,
  37. struct ubus_request_data *req, const char *method,
  38. struct blob_attr *msg)
  39. {
  40. cache_update();
  41. return 0;
  42. }
  43. enum {
  44. BROWSE_SERVICE,
  45. BROWSE_ARRAY,
  46. BROWSE_ADDRESS,
  47. BROWSE_MAX
  48. };
  49. static const struct blobmsg_policy browse_policy[] = {
  50. [BROWSE_SERVICE] = { "service", BLOBMSG_TYPE_STRING },
  51. [BROWSE_ARRAY] = { "array", BLOBMSG_TYPE_BOOL },
  52. [BROWSE_ADDRESS] = { "address", BLOBMSG_TYPE_BOOL },
  53. };
  54. static int
  55. umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
  56. struct ubus_request_data *req, const char *method,
  57. struct blob_attr *msg)
  58. {
  59. struct cache_service *s, *q;
  60. char *buffer = (char *) mdns_buf;
  61. struct blob_attr *data[BROWSE_MAX];
  62. void *c1 = NULL, *c2;
  63. char *service = NULL;
  64. int array = 0;
  65. bool address = true;
  66. blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg));
  67. if (data[BROWSE_SERVICE])
  68. service = blobmsg_get_string(data[BROWSE_SERVICE]);
  69. if (data[BROWSE_ARRAY])
  70. array = blobmsg_get_u8(data[BROWSE_ARRAY]);
  71. if (data[BROWSE_ADDRESS])
  72. address = blobmsg_get_bool(data[BROWSE_ADDRESS]);
  73. blob_buf_init(&b, 0);
  74. avl_for_each_element(&services, s, avl) {
  75. const char *hostname = buffer;
  76. char *local;
  77. snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
  78. local = strstr(buffer, ".local");
  79. if (local)
  80. *local = '\0';
  81. if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
  82. continue;
  83. if (service && strcmp(buffer, service))
  84. continue;
  85. if (!c1) {
  86. c1 = blobmsg_open_table(&b, buffer);
  87. }
  88. snprintf(buffer, MAX_NAME_LEN, "%s", s->entry);
  89. local = strstr(buffer, "._");
  90. if (local)
  91. *local = '\0';
  92. c2 = blobmsg_open_table(&b, buffer);
  93. strncat(buffer, ".local", MAX_NAME_LEN);
  94. if (s->iface)
  95. blobmsg_add_string(&b, "iface", s->iface->name);
  96. cache_dump_records(&b, s->entry, array, &hostname);
  97. if (address)
  98. cache_dump_records(&b, hostname, array, NULL);
  99. blobmsg_close_table(&b, c2);
  100. q = avl_next_element(s, avl);
  101. if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
  102. blobmsg_close_table(&b, c1);
  103. c1 = NULL;
  104. }
  105. }
  106. ubus_send_reply(ctx, req, b.head);
  107. return UBUS_STATUS_OK;
  108. }
  109. enum {
  110. HOSTS_ARRAY,
  111. __HOSTS_MAX
  112. };
  113. static const struct blobmsg_policy hosts_policy[] = {
  114. [HOSTS_ARRAY] = { "array", BLOBMSG_TYPE_BOOL }
  115. };
  116. static int
  117. umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
  118. struct ubus_request_data *req, const char *method,
  119. struct blob_attr *msg)
  120. {
  121. struct cache_record *prev = NULL;
  122. struct blob_attr *tb[__HOSTS_MAX];
  123. struct cache_record *r;
  124. bool array = false;
  125. void *c;
  126. blobmsg_parse(hosts_policy, __HOSTS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
  127. if (tb[HOSTS_ARRAY])
  128. array = blobmsg_get_bool(tb[HOSTS_ARRAY]);
  129. blob_buf_init(&b, 0);
  130. avl_for_each_element(&records, r, avl) {
  131. if (r->type != TYPE_A && r->type != TYPE_AAAA)
  132. continue;
  133. /* Query each domain just once */
  134. if (!prev || strcmp(r->record, prev->record)) {
  135. c = blobmsg_open_table(&b, r->record);
  136. cache_dump_records(&b, r->record, array, NULL);
  137. blobmsg_close_table(&b, c);
  138. }
  139. prev = r;
  140. }
  141. ubus_send_reply(ctx, req, b.head);
  142. return UBUS_STATUS_OK;
  143. }
  144. enum {
  145. CFG_INTERFACES,
  146. CFG_KEEP,
  147. CFG_MAX
  148. };
  149. static const struct blobmsg_policy config_policy[] = {
  150. [CFG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_ARRAY },
  151. [CFG_KEEP] = { "keep", BLOBMSG_TYPE_BOOL },
  152. };
  153. static int
  154. umdns_set_config(struct ubus_context *ctx, struct ubus_object *obj,
  155. struct ubus_request_data *req, const char *method,
  156. struct blob_attr *msg)
  157. {
  158. struct blob_attr *data[CFG_MAX], *cur;
  159. int rem, keep = false;
  160. blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg));
  161. if (!data[CFG_INTERFACES])
  162. return UBUS_STATUS_INVALID_ARGUMENT;
  163. if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING))
  164. return UBUS_STATUS_INVALID_ARGUMENT;
  165. keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]);
  166. if (!keep) {
  167. vlist_update(&interfaces);
  168. ubus_notify(ctx, obj, "set_config", NULL, 1000);
  169. }
  170. blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem)
  171. interface_add(blobmsg_data(cur));
  172. if (!keep)
  173. vlist_flush(&interfaces);
  174. return 0;
  175. }
  176. enum query_attr {
  177. QUERY_QUESTION,
  178. QUERY_IFACE,
  179. QUERY_TYPE,
  180. QUERY_MAX
  181. };
  182. static const struct blobmsg_policy query_policy[QUERY_MAX] = {
  183. [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING },
  184. [QUERY_IFACE] = { "interface", BLOBMSG_TYPE_STRING },
  185. [QUERY_TYPE] = { "type", BLOBMSG_TYPE_INT32 },
  186. };
  187. static int
  188. umdns_query(struct ubus_context *ctx, struct ubus_object *obj,
  189. struct ubus_request_data *req, const char *method,
  190. struct blob_attr *msg)
  191. {
  192. struct blob_attr *tb[QUERY_MAX], *c;
  193. const char *question = C_DNS_SD;
  194. const char *ifname;
  195. int type = TYPE_ANY;
  196. blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg));
  197. if (!(c = tb[QUERY_IFACE]))
  198. return UBUS_STATUS_INVALID_ARGUMENT;
  199. ifname = blobmsg_get_string(c);
  200. if ((c = tb[QUERY_QUESTION]))
  201. question = blobmsg_get_string(c);
  202. if ((c = tb[QUERY_TYPE]))
  203. type = blobmsg_get_u32(c);
  204. struct interface *iface_v4 = interface_get(ifname, SOCK_MC_IPV4);
  205. struct interface *iface_v6 = interface_get(ifname, SOCK_MC_IPV6);
  206. if (!iface_v4 && !iface_v6)
  207. return UBUS_STATUS_NOT_FOUND;
  208. if (!strcmp(method, "query")) {
  209. if (iface_v4)
  210. dns_send_question(iface_v4, NULL, question, type, 1);
  211. if (iface_v6)
  212. dns_send_question(iface_v6, NULL, question, type, 1);
  213. return UBUS_STATUS_OK;
  214. } else if (!strcmp(method, "fetch")) {
  215. blob_buf_init(&b, 0);
  216. void *k = blobmsg_open_array(&b, "records");
  217. cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6);
  218. blobmsg_close_array(&b, k);
  219. ubus_send_reply(ctx, req, b.head);
  220. return UBUS_STATUS_OK;
  221. } else {
  222. return UBUS_STATUS_INVALID_ARGUMENT;
  223. }
  224. }
  225. static const struct ubus_method umdns_methods[] = {
  226. UBUS_METHOD("set_config", umdns_set_config, config_policy),
  227. UBUS_METHOD("query", umdns_query, query_policy),
  228. UBUS_METHOD("fetch", umdns_query, query_policy),
  229. UBUS_METHOD("browse", umdns_browse, browse_policy),
  230. UBUS_METHOD_NOARG("update", umdns_update),
  231. UBUS_METHOD("hosts", umdns_hosts, hosts_policy),
  232. UBUS_METHOD_NOARG("reload", umdns_reload),
  233. };
  234. static struct ubus_object_type umdns_object_type =
  235. UBUS_OBJECT_TYPE("umdns", umdns_methods);
  236. static struct ubus_object umdns_object = {
  237. .name = "umdns",
  238. .type = &umdns_object_type,
  239. .methods = umdns_methods,
  240. .n_methods = ARRAY_SIZE(umdns_methods),
  241. };
  242. static struct blob_attr *
  243. find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
  244. {
  245. struct blobmsg_policy policy = { name, type };
  246. struct blob_attr *ret;
  247. if (!attr)
  248. return NULL;
  249. blobmsg_parse_attr(&policy, 1, &ret, attr);
  250. return ret;
  251. }
  252. static void
  253. umdns_udebug_config_cb(struct blob_attr *data)
  254. {
  255. enum {
  256. CFG_ATTR_ENABLED,
  257. __CFG_ATTR_MAX
  258. };
  259. static const struct blobmsg_policy policy[__CFG_ATTR_MAX] = {
  260. [CFG_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_STRING },
  261. };
  262. struct blob_attr *tb[__CFG_ATTR_MAX];
  263. bool en;
  264. data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
  265. data = find_attr(data, "umdns", BLOBMSG_TYPE_TABLE);
  266. if (!data)
  267. return;
  268. blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data);
  269. if (!tb[CFG_ATTR_ENABLED])
  270. return;
  271. en = !!atoi(blobmsg_get_string(tb[CFG_ATTR_ENABLED]));
  272. umdns_udebug_set_enabled(en);
  273. }
  274. static int
  275. umdns_udebug_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
  276. struct ubus_request_data *req, const char *method,
  277. struct blob_attr *msg)
  278. {
  279. umdns_udebug_config_cb(msg);
  280. return 0;
  281. }
  282. static void
  283. umdns_udebug_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  284. {
  285. umdns_udebug_config_cb(msg);
  286. }
  287. static bool
  288. umdns_udebug_sub_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
  289. const char *path)
  290. {
  291. return !strcmp(path, "udebug");
  292. }
  293. static void
  294. ubus_connect_handler(struct ubus_context *ctx)
  295. {
  296. uint32_t id;
  297. int ret;
  298. ret = ubus_add_object(ctx, &umdns_object);
  299. if (ret)
  300. fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
  301. udebug_sub.cb = umdns_udebug_notify_cb;
  302. udebug_sub.new_obj_cb = umdns_udebug_sub_cb;
  303. ubus_register_subscriber(&conn.ctx, &udebug_sub);
  304. if (ubus_lookup_id(&conn.ctx, "udebug", &id) == 0) {
  305. ubus_subscribe(&conn.ctx, &udebug_sub, id);
  306. ubus_invoke(&conn.ctx, id, "get_config", NULL, umdns_udebug_req_cb, NULL, 1000);
  307. }
  308. }
  309. void
  310. ubus_startup(void)
  311. {
  312. conn.cb = ubus_connect_handler;
  313. ubus_auto_connect(&conn);
  314. }
  315. int ubus_service_list(ubus_data_handler_t cb)
  316. {
  317. uint32_t id;
  318. int ret;
  319. blob_buf_init(&b, 0);
  320. ret = ubus_lookup_id(&conn.ctx, "service", &id);
  321. if (ret)
  322. return ret;
  323. return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000);
  324. }