stations.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <json-c/json.h>
  5. #include <iwinfo.h>
  6. #include <net/if.h>
  7. #include <glob.h>
  8. #include <alloca.h>
  9. #define NETIF_PREFIX "/sys/class/net/"
  10. #define VIRTIF_PREFIX "/sys/devices/virtual/net/"
  11. #define LOWERGLOB_SUFFIX "/lower_*"
  12. static struct json_object *get_stations(const struct iwinfo_ops *iw, const char *ifname) {
  13. int len;
  14. char buf[IWINFO_BUFSIZE];
  15. struct json_object *stations = json_object_new_object();
  16. if (iw->assoclist(ifname, buf, &len) == -1)
  17. return stations;
  18. // This is just: for entry in assoclist(ifname)
  19. for (struct iwinfo_assoclist_entry *entry = (struct iwinfo_assoclist_entry *)buf;
  20. (char*)(entry+1) <= buf + len; entry++) {
  21. struct json_object *station = json_object_new_object();
  22. json_object_object_add(station, "signal", json_object_new_int(entry->signal));
  23. json_object_object_add(station, "noise", json_object_new_int(entry->noise));
  24. json_object_object_add(station, "inactive", json_object_new_int(entry->inactive));
  25. char macstr[18];
  26. snprintf(macstr, sizeof(macstr), "%02x:%02x:%02x:%02x:%02x:%02x",
  27. entry->mac[0], entry->mac[1], entry->mac[2],
  28. entry->mac[3], entry->mac[4], entry->mac[5]);
  29. json_object_object_add(stations, macstr, station);
  30. }
  31. return stations;
  32. }
  33. static void badrequest() {
  34. printf("Status: 400 Bad Request\n\n");
  35. exit(1);
  36. }
  37. // recurse down to the lowest layer-2 interface
  38. static int interface_get_lowest(const char *ifname, char *hwifname);
  39. static int interface_get_lowest(const char *ifname, char *hwifname) {
  40. glob_t globbuf;
  41. char *fnamebuf = alloca(1 + strlen(VIRTIF_PREFIX) + IF_NAMESIZE +
  42. strlen(LOWERGLOB_SUFFIX));
  43. char *lowentry = NULL;
  44. sprintf(fnamebuf, "%s%s%s", VIRTIF_PREFIX, ifname, LOWERGLOB_SUFFIX);
  45. glob(fnamebuf, GLOB_NOSORT | GLOB_NOESCAPE, NULL, &globbuf);
  46. if (globbuf.gl_pathc == 1) {
  47. lowentry = alloca(1 + strlen(globbuf.gl_pathv[0]));
  48. strncpy(lowentry, globbuf.gl_pathv[0], 1 + strlen(globbuf.gl_pathv[0]));
  49. }
  50. globfree(&globbuf);
  51. if (!lowentry) {
  52. char *path = alloca(1 + strlen(NETIF_PREFIX) + strlen(ifname));
  53. sprintf(path, "%s%s", NETIF_PREFIX, ifname);
  54. if(access(path, F_OK) != 0)
  55. return false;
  56. strncpy(hwifname, ifname, IF_NAMESIZE - 1);
  57. return true;
  58. } else {
  59. char buf[PATH_MAX];
  60. ssize_t len;
  61. if ((len = readlink(lowentry, buf, sizeof(buf)-1)) != -1)
  62. buf[len] = '\0';
  63. else
  64. return false;
  65. if (strncmp(buf, "../", 3) == 0) {
  66. return interface_get_lowest(strrchr(buf, '/') + 1, hwifname);
  67. } else {
  68. return false;
  69. }
  70. }
  71. }
  72. int main(int argc, char *argv[]) {
  73. if (argc != 2)
  74. badrequest();
  75. const char *ifname = argv[1];
  76. char hwifname[IF_NAMESIZE] = "";
  77. if (strlen(ifname) >= IF_NAMESIZE)
  78. badrequest();
  79. if (strcspn(ifname, "/\\[]{}*?") != strlen(ifname))
  80. badrequest();
  81. if (!interface_get_lowest(ifname, hwifname))
  82. badrequest();
  83. const struct iwinfo_ops *iw = iwinfo_backend(hwifname);
  84. if (iw == NULL)
  85. badrequest();
  86. printf("Content-type: text/event-stream\n\n");
  87. while (true) {
  88. struct json_object *obj;
  89. obj = get_stations(iw, hwifname);
  90. printf("data: %s\n\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  91. fflush(stdout);
  92. json_object_put(obj);
  93. usleep(150000);
  94. }
  95. return 0;
  96. }