2
0

service.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <stdio.h>
  19. #include <time.h>
  20. #include <uci.h>
  21. #include <uci_blob.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 "dns.h"
  27. #include "service.h"
  28. #include "util.h"
  29. #include "interface.h"
  30. enum {
  31. SERVICE_PORT,
  32. SERVICE_TXT,
  33. __SERVICE_MAX,
  34. };
  35. struct service {
  36. struct vlist_node node;
  37. time_t t;
  38. const char *service;
  39. const char *daemon;
  40. const uint8_t *txt;
  41. int txt_len;
  42. int port;
  43. int active;
  44. };
  45. static const struct blobmsg_policy service_policy[__SERVICE_MAX] = {
  46. [SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
  47. [SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
  48. };
  49. static const struct uci_blob_param_list service_attr_list = {
  50. .n_params = __SERVICE_MAX,
  51. .params = service_policy,
  52. };
  53. static void
  54. service_update(struct vlist_tree *tree, struct vlist_node *node_new,
  55. struct vlist_node *node_old);
  56. static struct blob_buf b;
  57. static VLIST_TREE(services, avl_strcmp, service_update, false, false);
  58. char *hostname = NULL;
  59. static char *sdudp = "_services._dns-sd._udp.local";
  60. static char *sdtcp = "_services._dns-sd._tcp.local";
  61. char *
  62. service_name(const char *domain)
  63. {
  64. static char buffer[256];
  65. snprintf(buffer, sizeof(buffer), "%s.%s", hostname, domain);
  66. return buffer;
  67. }
  68. static void
  69. service_add_ptr(const char *host)
  70. {
  71. unsigned char buffer[MAX_NAME_LEN];
  72. int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
  73. if (len < 1)
  74. return;
  75. dns_add_answer(TYPE_PTR, buffer, len);
  76. }
  77. static void
  78. service_add_srv(struct service *s)
  79. {
  80. unsigned char buffer[MAX_NAME_LEN];
  81. struct dns_srv_data *sd;
  82. char *host = service_name("local");
  83. int len = dn_comp(host, buffer, MAX_NAME_LEN, NULL, NULL);
  84. if (len < 1)
  85. return;
  86. sd = calloc(1, len + sizeof(struct dns_srv_data));
  87. if (!sd)
  88. return;
  89. sd->port = cpu_to_be16(s->port);
  90. memcpy(&sd[1], buffer, len);
  91. host = service_name(s->service);
  92. dns_add_answer(TYPE_SRV, (uint8_t *) sd, len + sizeof(struct dns_srv_data));
  93. free(sd);
  94. }
  95. #define TOUT_LOOKUP 60
  96. static int
  97. service_timeout(struct service *s)
  98. {
  99. time_t t = time(NULL);
  100. if (t - s->t <= TOUT_LOOKUP)
  101. return 0;
  102. s->t = t;
  103. return 1;
  104. }
  105. void
  106. service_reply_a(struct interface *iface, int type)
  107. {
  108. if (type != TYPE_A)
  109. return;
  110. dns_init_answer();
  111. dns_add_answer(TYPE_A, (uint8_t *) &iface->v4_addr.s_addr, 4);
  112. dns_send_answer(iface, service_name("local"));
  113. }
  114. void
  115. service_reply(struct interface *iface, const char *match)
  116. {
  117. struct service *s;
  118. vlist_for_each_element(&services, s, node) {
  119. char *host = service_name(s->service);
  120. char *service = strstr(host, "._");
  121. if (!s->active || !service || !service_timeout(s))
  122. continue;
  123. service++;
  124. if (match && strcmp(match, s->service))
  125. continue;
  126. dns_init_answer();
  127. service_add_ptr(service_name(s->service));
  128. dns_send_answer(iface, service);
  129. dns_init_answer();
  130. service_add_srv(s);
  131. if (s->txt && s->txt_len)
  132. dns_add_answer(TYPE_TXT, (uint8_t *) s->txt, s->txt_len);
  133. dns_send_answer(iface, host);
  134. }
  135. if (match)
  136. return;
  137. service_reply_a(iface, TYPE_A);
  138. }
  139. void
  140. service_announce_services(struct interface *iface, const char *service)
  141. {
  142. struct service *s;
  143. int tcp = 1;
  144. if (!strcmp(service, sdudp))
  145. tcp = 0;
  146. else if (strcmp(service, sdtcp))
  147. return;
  148. vlist_for_each_element(&services, s, node) {
  149. if (!strstr(s->service, "._tcp") && tcp)
  150. continue;
  151. if (!strstr(s->service, "._udp") && !tcp)
  152. continue;
  153. s->t = 0;
  154. dns_init_answer();
  155. service_add_ptr(s->service);
  156. if (tcp)
  157. dns_send_answer(iface, sdtcp);
  158. else
  159. dns_send_answer(iface, sdudp);
  160. service_reply(iface, s->service);
  161. }
  162. }
  163. void
  164. service_announce(struct interface *iface)
  165. {
  166. service_announce_services(iface, sdudp);
  167. service_announce_services(iface, sdtcp);
  168. }
  169. static void
  170. service_update(struct vlist_tree *tree, struct vlist_node *node_new,
  171. struct vlist_node *node_old)
  172. {
  173. struct service *s;
  174. if (!node_old)
  175. return;
  176. s = container_of(node_old, struct service, node);
  177. free(s);
  178. }
  179. static void
  180. service_load(char *path)
  181. {
  182. struct blob_attr *txt, *cur, *_tb[__SERVICE_MAX];
  183. int rem, i;
  184. glob_t gl;
  185. if (glob(path, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl))
  186. return;
  187. for (i = 0; i < gl.gl_pathc; i++) {
  188. blob_buf_init(&b, 0);
  189. if (!blobmsg_add_json_from_file(&b, gl.gl_pathv[i]))
  190. continue;
  191. blob_for_each_attr(cur, b.head, rem) {
  192. struct service *s;
  193. char *d_service, *d_daemon;
  194. uint8_t *d_txt;
  195. int rem2;
  196. int txt_len = 0;
  197. blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
  198. _tb, blobmsg_data(cur), blobmsg_data_len(cur));
  199. if (!_tb[SERVICE_PORT] || !_tb[SERVICE_TXT])
  200. continue;
  201. blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
  202. txt_len += 1 + strlen(blobmsg_get_string(txt));
  203. s = calloc_a(sizeof(*s),
  204. &d_daemon, strlen(gl.gl_pathv[i]) + 1,
  205. &d_service, strlen(blobmsg_name(cur)) + 1,
  206. &d_txt, txt_len);
  207. if (!s)
  208. continue;
  209. s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
  210. s->service = strcpy(d_service, blobmsg_name(cur));
  211. s->daemon = strcpy(d_daemon, gl.gl_pathv[i]);
  212. s->active = 1;
  213. s->t = 0;
  214. s->txt_len = txt_len;
  215. s->txt = d_txt;
  216. blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2) {
  217. int len = strlen(blobmsg_get_string(txt));
  218. if (!len)
  219. continue;
  220. if (len > 0xff)
  221. len = 0xff;
  222. *d_txt = len;
  223. d_txt++;
  224. memcpy(d_txt, blobmsg_get_string(txt), len);
  225. d_txt += len;
  226. }
  227. vlist_add(&services, &s->node, s->service);
  228. }
  229. }
  230. }
  231. void
  232. service_init(void)
  233. {
  234. if (!hostname)
  235. hostname = get_hostname();
  236. vlist_update(&services);
  237. service_load("/tmp/run/mdnsd/*");
  238. vlist_flush(&services);
  239. }
  240. void
  241. service_cleanup(void)
  242. {
  243. vlist_flush(&services);
  244. blob_buf_free(&b);
  245. }