wifi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* See LICENSE file for copyright and license details. */
  2. #if defined(__linux__)
  3. #include <errno.h>
  4. #include <ifaddrs.h>
  5. #include <limits.h>
  6. #include <linux/wireless.h>
  7. #include <sys/socket.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/ioctl.h>
  11. #include <unistd.h>
  12. #include "../util.h"
  13. const char *
  14. wifi_perc(const char *iface)
  15. {
  16. int i, cur;
  17. int total = 70; /* the max of /proc/net/wireless */
  18. char *p, *datastart;
  19. char path[PATH_MAX];
  20. char status[5];
  21. FILE *fp;
  22. snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
  23. "/operstate");
  24. if (!(fp = fopen(path, "r"))) {
  25. fprintf(stderr, "fopen '%s': %s\n", path,
  26. strerror(errno));
  27. return NULL;
  28. }
  29. p = fgets(status, 5, fp);
  30. fclose(fp);
  31. if(!p || strcmp(status, "up\n") != 0) {
  32. return NULL;
  33. }
  34. if (!(fp = fopen("/proc/net/wireless", "r"))) {
  35. fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
  36. strerror(errno));
  37. return NULL;
  38. }
  39. for (i = 0; i < 3; i++) {
  40. if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
  41. break;
  42. }
  43. fclose(fp);
  44. if (i < 2 || !p) {
  45. return NULL;
  46. }
  47. if (!(datastart = strstr(buf, iface))) {
  48. return NULL;
  49. }
  50. datastart = (datastart+(strlen(iface)+1));
  51. sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
  52. "%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
  53. return bprintf("%d", (int)((float)cur / total * 100));
  54. }
  55. const char *
  56. wifi_essid(const char *iface)
  57. {
  58. static char id[IW_ESSID_MAX_SIZE+1];
  59. int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  60. struct iwreq wreq;
  61. memset(&wreq, 0, sizeof(struct iwreq));
  62. wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
  63. snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
  64. if (sockfd < 0) {
  65. fprintf(stderr, "socket 'AF_INET': %s\n",
  66. strerror(errno));
  67. return NULL;
  68. }
  69. wreq.u.essid.pointer = id;
  70. if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
  71. fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
  72. close(sockfd);
  73. return NULL;
  74. }
  75. close(sockfd);
  76. if (!strcmp(id, "")) {
  77. return NULL;
  78. }
  79. return id;
  80. }
  81. #elif defined(__OpenBSD__)
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85. #include <errno.h>
  86. #include <ifaddrs.h>
  87. #include <unistd.h>
  88. #include <sys/ioctl.h>
  89. #include <sys/types.h>
  90. #include <sys/socket.h>
  91. #include <net/if.h>
  92. #include <net/if_media.h>
  93. #include <net80211/ieee80211.h>
  94. #include <net80211/ieee80211_ioctl.h>
  95. #include "../util.h"
  96. static int
  97. load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
  98. {
  99. struct ieee80211_bssid bssid;
  100. int sockfd;
  101. memset(&bssid, 0, sizeof(bssid);
  102. memset(nr, 0, sizeof(struct ieee80211_nodereq));
  103. if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
  104. fprintf(stderr, "socket 'AF_INET': %s\n",
  105. strerror(errno));
  106. return 0;
  107. }
  108. strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
  109. if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) == -1) {
  110. fprintf(stderr, "ioctl 'SIOCG80211BSSID': %s\n",
  111. strerror(errno));
  112. close(sockfd);
  113. return 0;
  114. }
  115. strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
  116. memmove(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
  117. if ((ioctl(sockfd, SIOCG80211NODE, nr)) == -1 && nr->nr_rssi) {
  118. fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
  119. strerror(errno));
  120. close(sockfd);
  121. return 0;
  122. }
  123. return close(sockfd), 1;
  124. }
  125. const char *
  126. wifi_perc(const char *iface)
  127. {
  128. struct ieee80211_nodereq nr;
  129. int q;
  130. if (load_ieee80211_nodereq(iface, &nr)) {
  131. if (nr.nr_max_rssi)
  132. q = IEEE80211_NODEREQ_RSSI(&nr);
  133. else
  134. q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
  135. (2 * (nr.nr_rssi + 100)));
  136. return bprintf("%d", q);
  137. }
  138. return NULL;
  139. }
  140. const char *
  141. wifi_essid(const char *iface)
  142. {
  143. struct ieee80211_nodereq nr;
  144. if (load_ieee80211_nodereq(iface, &nr)) {
  145. return bprintf("%s", nr.nr_nwid);
  146. }
  147. return NULL;
  148. }
  149. #endif