service.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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/nameser.h>
  15. #include <sys/socket.h>
  16. #include <resolv.h>
  17. #include <glob.h>
  18. #include <inttypes.h>
  19. #include <stdio.h>
  20. #include <time.h>
  21. #include <libubus.h>
  22. #include <libubox/vlist.h>
  23. #include <libubox/uloop.h>
  24. #include <libubox/avl-cmp.h>
  25. #include <libubox/blobmsg_json.h>
  26. #include "ubus.h"
  27. #include "dns.h"
  28. #include "service.h"
  29. #include "util.h"
  30. #include "interface.h"
  31. #include "announce.h"
  32. enum {
  33. SERVICE_INSTANCE,
  34. SERVICE_SERVICE,
  35. SERVICE_PORT,
  36. SERVICE_TXT,
  37. SERVICE_HOSTNAME,
  38. __SERVICE_MAX,
  39. };
  40. struct service {
  41. struct vlist_node node;
  42. time_t t;
  43. const char *id;
  44. const char *instance;
  45. const char *service;
  46. const uint8_t *txt;
  47. int txt_len;
  48. int port;
  49. int active;
  50. };
  51. static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
  52. [SERVICE_INSTANCE] = { .name = "instance", .type = BLOBMSG_TYPE_STRING },
  53. [SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
  54. [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
  55. [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
  56. [SERVICE_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
  57. };
  58. static void
  59. service_update(struct vlist_tree *tree, struct vlist_node *node_new,
  60. struct vlist_node *node_old);
  61. static void
  62. hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
  63. struct vlist_node *node_old);
  64. static struct blob_buf b;
  65. static VLIST_TREE(services, avl_strcmp, service_update, false, false);
  66. VLIST_TREE(hostnames, avl_strcmp, hostname_update, false, false);
  67. static int service_init_announce;
  68. /**
  69. * service_instance_name - construct Service Instance Name as in RFC 6763
  70. *
  71. * RFC 6763 specifies Service Instance Names in the following way:
  72. *
  73. * Service Instance Name = <Instance> . <Service> . <Domain>
  74. *
  75. * @s: service to generate service instance name for
  76. */
  77. static const char *
  78. service_instance_name(struct service *s)
  79. {
  80. static char buffer[256];
  81. snprintf(buffer, sizeof(buffer), "%s.%s", s->instance, s->service);
  82. return buffer;
  83. }
  84. static void
  85. service_add_ptr(const char *host, int ttl)
  86. {
  87. int len = dn_comp(host, mdns_buf, sizeof(mdns_buf), NULL, NULL);
  88. if (len < 1)
  89. return;
  90. dns_add_answer(TYPE_PTR, mdns_buf, len, ttl);
  91. }
  92. static void
  93. service_add_srv(struct service *s, int ttl)
  94. {
  95. struct dns_srv_data *sd = (struct dns_srv_data *) mdns_buf;
  96. int len = sizeof(*sd);
  97. len += dn_comp(mdns_hostname_local, mdns_buf + len, sizeof(mdns_buf) - len, NULL, NULL);
  98. if (len <= sizeof(*sd))
  99. return;
  100. sd->port = cpu_to_be16(s->port);
  101. dns_add_answer(TYPE_SRV, mdns_buf, len, ttl);
  102. }
  103. #define TOUT_LOOKUP 60
  104. static time_t
  105. service_timeout(struct service *s)
  106. {
  107. time_t t = monotonic_time();
  108. if (t - s->t <= TOUT_LOOKUP) {
  109. DBG(2, "t=%" PRId64 ", s->t=%" PRId64 ", t - s->t = %" PRId64 "\n", (int64_t)t, (int64_t)s->t, (int64_t)(t - s->t));
  110. return 0;
  111. }
  112. return t;
  113. }
  114. static void
  115. service_reply_single(struct interface *iface, struct sockaddr *to, struct service *s, int ttl, int force)
  116. {
  117. const char *host = service_instance_name(s);
  118. char *service = strstr(host, "._");
  119. time_t t = service_timeout(s);
  120. if (!force && (!s->active || !service || !t))
  121. return;
  122. service++;
  123. s->t = t;
  124. dns_init_answer();
  125. service_add_ptr(service_instance_name(s), ttl);
  126. dns_send_answer(iface, to, service);
  127. dns_init_answer();
  128. service_add_srv(s, ttl);
  129. if (s->txt && s->txt_len)
  130. dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len, ttl);
  131. dns_send_answer(iface, to, host);
  132. }
  133. void
  134. service_reply(struct interface *iface, struct sockaddr *to, const char *instance, const char *service_domain, int ttl)
  135. {
  136. struct service *s;
  137. vlist_for_each_element(&services, s, node) {
  138. if (instance && strcmp(s->instance, instance))
  139. continue;
  140. if (service_domain && strcmp(s->service, service_domain))
  141. continue;
  142. service_reply_single(iface, to, s, ttl, 0);
  143. }
  144. }
  145. void
  146. service_announce_services(struct interface *iface, struct sockaddr *to, int ttl)
  147. {
  148. struct service *s;
  149. vlist_for_each_element(&services, s, node) {
  150. s->t = 0;
  151. if (ttl) {
  152. dns_init_answer();
  153. service_add_ptr(s->service, ttl);
  154. dns_send_answer(iface, to, C_DNS_SD);
  155. }
  156. service_reply_single(iface, to, s, ttl, 0);
  157. }
  158. }
  159. static void
  160. service_update(struct vlist_tree *tree, struct vlist_node *node_new,
  161. struct vlist_node *node_old)
  162. {
  163. struct interface *iface;
  164. struct service *s;
  165. if (!node_old) {
  166. s = container_of(node_new, struct service, node);
  167. if (service_init_announce)
  168. vlist_for_each_element(&interfaces, iface, node) {
  169. s->t = 0;
  170. service_reply_single(iface, NULL, s, announce_ttl, 1);
  171. }
  172. return;
  173. }
  174. s = container_of(node_old, struct service, node);
  175. if (!node_new && service_init_announce)
  176. vlist_for_each_element(&interfaces, iface, node)
  177. service_reply_single(iface, NULL, s, 0, 1);
  178. free(s);
  179. }
  180. static void
  181. hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
  182. struct vlist_node *node_old)
  183. {
  184. struct interface *iface;
  185. struct hostname *h;
  186. if (!node_old) {
  187. h = container_of(node_new, struct hostname, node);
  188. vlist_for_each_element(&interfaces, iface, node)
  189. dns_reply_a(iface, NULL, announce_ttl, h->hostname);
  190. return;
  191. }
  192. h = container_of(node_old, struct hostname, node);
  193. if (!node_new)
  194. vlist_for_each_element(&interfaces, iface, node)
  195. dns_reply_a(iface, NULL, 0, h->hostname);
  196. free(h);
  197. }
  198. static void
  199. service_load_hostname(struct blob_attr *b)
  200. {
  201. struct hostname *h;
  202. char *hostname, *d_hostname;
  203. hostname = blobmsg_get_string(b);
  204. h = calloc_a(sizeof(*h), &d_hostname, strlen(hostname) + 1);
  205. if (!h)
  206. return;
  207. h->hostname = strcpy(d_hostname, hostname);
  208. vlist_add(&hostnames, &h->node, h->hostname);
  209. }
  210. static void
  211. service_load_blob(struct blob_attr *b)
  212. {
  213. struct blob_attr *txt, *_tb[__SERVICE_MAX];
  214. struct service *s;
  215. char *d_instance, *d_service, *d_id;
  216. uint8_t *d_txt;
  217. int rem2;
  218. int txt_len = 0;
  219. unsigned int n;
  220. blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
  221. _tb, blobmsg_data(b), blobmsg_data_len(b));
  222. if (_tb[SERVICE_HOSTNAME]) {
  223. service_load_hostname(_tb[SERVICE_HOSTNAME]);
  224. return;
  225. }
  226. if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
  227. return;
  228. if (_tb[SERVICE_TXT])
  229. blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
  230. txt_len += 1 + strlen(blobmsg_get_string(txt));
  231. n = strlen(blobmsg_name(b));
  232. s = calloc_a(sizeof(*s),
  233. &d_id, n + 1,
  234. &d_instance, _tb[SERVICE_INSTANCE] ? strlen(blobmsg_get_string(_tb[SERVICE_INSTANCE])) + 1 : 0,
  235. &d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
  236. &d_txt, txt_len);
  237. if (!s)
  238. return;
  239. s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
  240. s->id = strncpy(d_id, blobmsg_name(b), n);
  241. if (_tb[SERVICE_INSTANCE])
  242. s->instance = strcpy(d_instance, blobmsg_get_string(_tb[SERVICE_INSTANCE]));
  243. else
  244. s->instance = umdns_host_label;
  245. s->service = strcpy(d_service, blobmsg_get_string(_tb[SERVICE_SERVICE]));
  246. s->active = 1;
  247. s->t = 0;
  248. s->txt_len = txt_len;
  249. s->txt = d_txt;
  250. if (_tb[SERVICE_TXT])
  251. blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
  252. int len = strlen(blobmsg_get_string(txt));
  253. if (!len)
  254. return;
  255. if (len > 0xff)
  256. len = 0xff;
  257. *d_txt = len;
  258. d_txt++;
  259. memcpy(d_txt, blobmsg_get_string(txt), len);
  260. d_txt += len;
  261. }
  262. vlist_add(&services, &s->node, s->id);
  263. }
  264. static void
  265. service_load(char *path)
  266. {
  267. struct blob_attr *cur;
  268. glob_t gl;
  269. int i, rem;
  270. if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
  271. return;
  272. for (i = 0; i < gl.gl_pathc; i++) {
  273. blob_buf_init(&b, 0);
  274. if (blobmsg_add_json_from_file(&b, gl.gl_pathv[i])) {
  275. blob_for_each_attr(cur, b.head, rem)
  276. service_load_blob(cur);
  277. } else {
  278. fprintf(stderr, "Error reading %s JSON\n", gl.gl_pathv[i]);
  279. }
  280. }
  281. globfree(&gl);
  282. }
  283. static void
  284. service_init_cb(struct ubus_request *req, int type, struct blob_attr *msg)
  285. {
  286. struct blob_attr *cur;
  287. int rem;
  288. get_hostname();
  289. vlist_update(&services);
  290. vlist_update(&hostnames);
  291. service_load("/etc/umdns/*");
  292. blob_for_each_attr(cur, msg, rem) {
  293. struct blob_attr *cur2;
  294. int rem2;
  295. blobmsg_for_each_attr(cur2, cur, rem2) {
  296. struct blob_attr *cur3;
  297. int rem3;
  298. if (strcmp(blobmsg_name(cur2), "instances"))
  299. continue;
  300. blobmsg_for_each_attr(cur3, cur2, rem3) {
  301. struct blob_attr *cur4;
  302. int rem4;
  303. int running = 0;
  304. blobmsg_for_each_attr(cur4, cur3, rem4) {
  305. const char *name = blobmsg_name(cur4);
  306. if (!strcmp(name, "running")) {
  307. running = blobmsg_get_bool(cur4);
  308. } else if (running && !strcmp(name, "data")) {
  309. struct blob_attr *cur5;
  310. int rem5;
  311. blobmsg_for_each_attr(cur5, cur4, rem5) {
  312. struct blob_attr *cur6;
  313. int rem6;
  314. if (strcmp(blobmsg_name(cur5), "mdns"))
  315. continue;
  316. blobmsg_for_each_attr(cur6, cur5, rem6)
  317. service_load_blob(cur6);
  318. }
  319. break;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. vlist_flush(&services);
  326. vlist_flush(&hostnames);
  327. }
  328. void
  329. service_init(int announce)
  330. {
  331. get_hostname();
  332. service_init_announce = announce;
  333. ubus_service_list(service_init_cb);
  334. }
  335. void
  336. service_cleanup(void)
  337. {
  338. vlist_flush(&services);
  339. blob_buf_free(&b);
  340. }