neighbours-batadv.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <json-c/json.h>
  5. #include <net/if.h>
  6. #define STR(x) #x
  7. #define XSTR(x) STR(x)
  8. static json_object *neighbours(void) {
  9. struct json_object *obj = json_object_new_object();
  10. FILE *f;
  11. f = fopen("/tmp/batman-adv-visdata/bat0/originators" , "r");
  12. if (f == NULL)
  13. return NULL;
  14. while (!feof(f)) {
  15. char mac1[18];
  16. char mac2[18];
  17. char ifname[IF_NAMESIZE+1];
  18. int tq;
  19. double lastseen;
  20. int count = fscanf(f, "%17s%*[\t ]%lfs%*[\t (]%d) %17s%*[[ ]%" XSTR(IF_NAMESIZE) "[^]]]", mac1, &lastseen, &tq, mac2, ifname);
  21. if (count != 5)
  22. continue;
  23. if (strcmp(mac1, mac2) == 0) {
  24. struct json_object *neigh = json_object_new_object();
  25. json_object_object_add(neigh, "tq", json_object_new_int(tq));
  26. json_object_object_add(neigh, "lastseen", json_object_new_double(lastseen));
  27. json_object_object_add(neigh, "ifname", json_object_new_string(ifname));
  28. json_object_object_add(obj, mac1, neigh);
  29. }
  30. }
  31. fclose(f);
  32. return obj;
  33. }
  34. int main(void) {
  35. struct json_object *obj;
  36. printf("Content-type: text/event-stream\n\n");
  37. fflush(stdout);
  38. while (1) {
  39. obj = neighbours();
  40. if (obj) {
  41. printf("data: %s\n\n", json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN));
  42. fflush(stdout);
  43. json_object_put(obj);
  44. }
  45. sleep(10);
  46. }
  47. return 0;
  48. }