respondd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <respondd.h>
  2. #include <json-c/json.h>
  3. #include <libgluonutil.h>
  4. #include <uci.h>
  5. #include <string.h>
  6. #include <net/if.h>
  7. #define _STRINGIFY(s) #s
  8. #define STRINGIFY(s) _STRINGIFY(s)
  9. bool strstw(const char *pre, const char *str) {
  10. size_t lenpre = strlen(pre);
  11. return strlen(str) < lenpre ? false : strncmp(pre, str, lenpre) == 0;
  12. }
  13. bool strrmbs(char *line, int begin, int end) { // <- ist es hier sinvoller pointer auf die ints zu setzen??
  14. size_t len = strlen(line);
  15. if (len < begin)
  16. return false;
  17. memmove(line, line+begin, len - begin + 1);
  18. if (len < end)
  19. return false;
  20. line[len-end] = 0; //remove val of end characters on the end
  21. return true;
  22. }
  23. // extract hood informations
  24. static struct json_object * get_hoodselector(void) {
  25. FILE *f = fopen("/tmp/.hoodselector", "r");
  26. if (!f)
  27. return NULL;
  28. struct json_object *ret = json_object_new_object();
  29. char *line = NULL;
  30. size_t len = 0;
  31. while (getline(&line, &len, f) >= 0) {
  32. //1. Get md5 hash from current selected hood.
  33. if (strstw("\"md5hash\": ",line)) {
  34. if (!strrmbs(line, 12, 14))
  35. continue;
  36. json_object_object_add(ret, "md5hash", gluonutil_wrap_string(line));
  37. }
  38. //2. Get true or false string for VPN Router.
  39. if (strstw("\"vpnrouter\": ",line)) {
  40. if (!strrmbs(line, 14, 16))
  41. continue;
  42. json_object_object_add(ret, "vpnrouter", gluonutil_wrap_string(line));
  43. }
  44. //3. Get hoodname
  45. if (strstw("\"hoodname\": ",line)) {
  46. if (!strrmbs(line, 13, 15))
  47. continue;
  48. json_object_object_add(ret, "hoodname", gluonutil_wrap_string(line));
  49. }
  50. }
  51. free(line);
  52. fclose(f);
  53. return ret;
  54. }
  55. //Get currend selected BSSID
  56. static struct json_object * get_current_selected_bssid(void){
  57. struct uci_context *ctx = uci_alloc_context();
  58. ctx->flags &= ~UCI_FLAG_STRICT;
  59. struct uci_package *p;
  60. if (uci_load(ctx, "wireless", &p))
  61. goto end;
  62. struct uci_element *e;
  63. uci_foreach_element(&p->sections, e) {
  64. struct uci_section *s = uci_to_section(e);
  65. if (strcmp(s->type, "wifi-iface"))
  66. continue;
  67. if (strncmp(e->name, "ibss_", 5))
  68. continue;
  69. const char *bssid = uci_lookup_option_string(ctx, s, "bssid");
  70. if (!bssid)
  71. continue;
  72. struct json_object *ret = json_object_new_object();
  73. json_object_object_add(ret, "bssid", gluonutil_wrap_string(bssid));
  74. free(bssid);
  75. uci_free_context(ctx);
  76. return ret;
  77. }
  78. end:
  79. uci_free_context(ctx);
  80. return NULL;
  81. }
  82. // create final obj with logical structure
  83. static struct json_object * respondd_provider_hoodselector(void) {
  84. struct json_object *ret = json_object_new_object();
  85. struct json_object *hoodinfo = get_hoodselector();
  86. if(hoodinfo)
  87. json_object_object_add(ret, "hoodinfo", hoodinfo);
  88. struct json_object *selectedbssid = get_current_selected_bssid();
  89. if(selectedbssid)
  90. json_object_object_add(ret, "selectedbssid", selectedbssid);
  91. return ret;
  92. }
  93. // related to respondd_provider_hoodselector
  94. const struct respondd_provider_info respondd_providers[] = {
  95. {"hoodselector", respondd_provider_hoodselector},
  96. {}
  97. };