2
0

service.c 8.1 KB

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