respondd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright (c) 2016, Matthias Schiffer <mschiffer@universe-factory.net>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  12. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  13. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  15. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  18. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. #include <respondd.h>
  23. #include <json-c/json.h>
  24. #include <libgluonutil.h>
  25. #include <uci.h>
  26. #include <stdbool.h>
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <sys/socket.h>
  31. #include <sys/un.h>
  32. static struct json_object * get_peer_groups(struct json_object *groups, struct json_object *peers);
  33. static struct json_object * get_fastd_version(void) {
  34. FILE *f = popen("exec fastd -v", "r");
  35. if (!f)
  36. return NULL;
  37. char *line = NULL;
  38. size_t len = 0;
  39. ssize_t r = getline(&line, &len, f);
  40. pclose(f);
  41. if (r >= 0) {
  42. len = strlen(line); /* The len given by getline is the buffer size, not the string length */
  43. if (len && line[len-1] == '\n')
  44. line[len-1] = 0;
  45. }
  46. else {
  47. free(line);
  48. line = NULL;
  49. }
  50. const char *version = line;
  51. if (strncmp(version, "fastd ", 6) == 0)
  52. version += 6;
  53. struct json_object *ret = gluonutil_wrap_string(version);
  54. free(line);
  55. return ret;
  56. }
  57. static struct json_object * get_fastd(void) {
  58. bool enabled = false;
  59. struct uci_context *ctx = uci_alloc_context();
  60. ctx->flags &= ~UCI_FLAG_STRICT;
  61. struct uci_package *p;
  62. if (uci_load(ctx, "fastd", &p))
  63. goto disabled;
  64. struct uci_section *s = uci_lookup_section(ctx, p, "mesh_vpn");
  65. if (!s)
  66. goto disabled;
  67. const char *enabled_str = uci_lookup_option_string(ctx, s, "enabled");
  68. if (!enabled_str || !strcmp(enabled_str, "1"))
  69. enabled = true;
  70. disabled:
  71. uci_free_context(ctx);
  72. struct json_object *ret = json_object_new_object();
  73. json_object_object_add(ret, "version", get_fastd_version());
  74. json_object_object_add(ret, "enabled", json_object_new_boolean(enabled));
  75. return ret;
  76. }
  77. static struct json_object * respondd_provider_nodeinfo(void) {
  78. struct json_object *ret = json_object_new_object();
  79. struct json_object *software = json_object_new_object();
  80. json_object_object_add(software, "fastd", get_fastd());
  81. json_object_object_add(ret, "software", software);
  82. return ret;
  83. }
  84. static const char * get_status_socket(struct uci_context *ctx, struct uci_section *s) {
  85. return uci_lookup_option_string(ctx, s, "status_socket");
  86. }
  87. static struct json_object * read_status(struct uci_context *ctx, struct uci_section *s) {
  88. const char *path = get_status_socket(ctx, s);
  89. size_t addrlen = strlen(path);
  90. /* Allocate enough space for arbitrary-length paths */
  91. char addrbuf[offsetof(struct sockaddr_un, sun_path) + addrlen + 1];
  92. memset(addrbuf, 0, sizeof(addrbuf));
  93. struct sockaddr_un *addr = (struct sockaddr_un *)addrbuf;
  94. addr->sun_family = AF_UNIX;
  95. memcpy(addr->sun_path, path, addrlen+1);
  96. int fd = socket(AF_UNIX, SOCK_STREAM, 0);
  97. if (fd < 0)
  98. return NULL;
  99. if (connect(fd, (struct sockaddr*)addr, sizeof(addrbuf)) < 0) {
  100. close(fd);
  101. return NULL;
  102. }
  103. struct json_object *ret = NULL;
  104. struct json_tokener *tok = json_tokener_new();
  105. do {
  106. char buf[1024];
  107. size_t len = read(fd, buf, sizeof(buf));
  108. if (len <= 0)
  109. break;
  110. ret = json_tokener_parse_ex(tok, buf, len);
  111. } while (!ret && json_tokener_get_error(tok) == json_tokener_continue);
  112. json_tokener_free(tok);
  113. close(fd);
  114. return ret;
  115. }
  116. static struct json_object * get_status(void) {
  117. struct json_object *ret = NULL;
  118. struct uci_context *ctx = uci_alloc_context();
  119. ctx->flags &= ~UCI_FLAG_STRICT;
  120. struct uci_package *p;
  121. if (!uci_load(ctx, "fastd", &p)) {
  122. struct uci_section *s = uci_lookup_section(ctx, p, "mesh_vpn");
  123. if (s)
  124. ret = read_status(ctx, s);
  125. }
  126. uci_free_context(ctx);
  127. return ret;
  128. }
  129. static bool get_peer_connection(struct json_object **ret, struct json_object *config, struct json_object *peers) {
  130. struct json_object *key_object;
  131. if (!json_object_object_get_ex(config, "key", &key_object))
  132. return false;
  133. const char *key = json_object_get_string(key_object);
  134. if (!key)
  135. return false;
  136. struct json_object *peer, *connection, *established;
  137. if (!json_object_object_get_ex(peers, key, &peer) ||
  138. !json_object_object_get_ex(peer, "connection", &connection))
  139. return false;
  140. if (json_object_object_get_ex(connection, "established", &established)) {
  141. int64_t established_time = json_object_get_int64(established);
  142. *ret = json_object_new_object();
  143. struct json_object *jso = json_object_new_double(established_time/1000.0);
  144. json_object_set_serializer(jso, json_object_double_to_json_string, "%.3f", NULL);
  145. json_object_object_add(*ret, "established", jso);
  146. }
  147. else {
  148. *ret = NULL;
  149. }
  150. return true;
  151. }
  152. static struct json_object * get_peer_group(struct json_object *config, struct json_object *peers) {
  153. struct json_object *ret = json_object_new_object();
  154. struct json_object *config_peers;
  155. if (json_object_object_get_ex(config, "peers", &config_peers) &&
  156. json_object_is_type(config_peers, json_type_object)) {
  157. struct json_object *ret_peers = json_object_new_object();
  158. json_object_object_foreach(config_peers, peername, peerconfig) {
  159. struct json_object *obj;
  160. if (get_peer_connection(&obj, peerconfig, peers))
  161. json_object_object_add(ret_peers, peername, obj);
  162. }
  163. if (json_object_object_length(ret_peers))
  164. json_object_object_add(ret, "peers", ret_peers);
  165. else
  166. json_object_put(ret_peers);
  167. }
  168. struct json_object *config_groups;
  169. if (json_object_object_get_ex(config, "groups", &config_groups)) {
  170. struct json_object *obj = get_peer_groups(config_groups, peers);
  171. if (obj)
  172. json_object_object_add(ret, "groups", obj);
  173. }
  174. if (!json_object_object_length(ret)) {
  175. json_object_put(ret);
  176. return NULL;
  177. }
  178. return ret;
  179. }
  180. static struct json_object * get_peer_groups(struct json_object *groups, struct json_object *peers) {
  181. if (!json_object_is_type(groups, json_type_object))
  182. return NULL;
  183. struct json_object *ret = json_object_new_object();
  184. json_object_object_foreach(groups, name, group) {
  185. struct json_object *g = get_peer_group(group, peers);
  186. if (g)
  187. json_object_object_add(ret, name, g);
  188. }
  189. if (!json_object_object_length(ret)) {
  190. json_object_put(ret);
  191. return NULL;
  192. }
  193. return ret;
  194. }
  195. static struct json_object * get_mesh_vpn(void) {
  196. struct json_object *ret = NULL;
  197. struct json_object *status = NULL;
  198. struct json_object *site = NULL;
  199. status = get_status();
  200. if (!status)
  201. goto end;
  202. struct json_object *peers;
  203. if (!json_object_object_get_ex(status, "peers", &peers))
  204. goto end;
  205. site = gluonutil_load_site_config();
  206. if (!site)
  207. goto end;
  208. struct json_object *fastd_mesh_vpn;
  209. if (!json_object_object_get_ex(site, "fastd_mesh_vpn", &fastd_mesh_vpn))
  210. goto end;
  211. ret = get_peer_group(fastd_mesh_vpn, peers);
  212. end:
  213. json_object_put(site);
  214. json_object_put(status);
  215. return ret;
  216. }
  217. static struct json_object * respondd_provider_statistics(void) {
  218. struct json_object *ret = json_object_new_object();
  219. struct json_object *mesh_vpn = get_mesh_vpn();
  220. if (mesh_vpn)
  221. json_object_object_add(ret, "mesh_vpn", mesh_vpn);
  222. return ret;
  223. }
  224. const struct respondd_provider_info respondd_providers[] = {
  225. {"nodeinfo", respondd_provider_nodeinfo},
  226. {"statistics", respondd_provider_statistics},
  227. {}
  228. };