summaryrefslogtreecommitdiff
path: root/components/wifi.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2017-09-24 15:33:01 +0200
committerAaron Marcher <me@drkhsh.at>2017-09-24 17:20:27 +0200
commit7246dc4381c6c95454672a5c1aff65a02d6d3747 (patch)
tree2264b09abf2e57daee396b632573f4b81e47aa59 /components/wifi.c
parent61e44e894890c1521a01148fbf969cbd4dbb4cae (diff)
Move components into dedicated subdirectory
This brings us a lot more tidiness.
Diffstat (limited to 'components/wifi.c')
-rw-r--r--components/wifi.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/components/wifi.c b/components/wifi.c
new file mode 100644
index 0000000..30b57ab
--- /dev/null
+++ b/components/wifi.c
@@ -0,0 +1,85 @@
+/* See LICENSE file for copyright and license details. */
+#include <err.h>
+#include <ifaddrs.h>
+#include <linux/wireless.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <limits.h>
+#include <string.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include "../util.h"
+
+const char *
+wifi_perc(const char *iface)
+{
+ int i, perc;
+ char *p, *datastart;
+ char path[PATH_MAX];
+ char status[5];
+ FILE *fp;
+
+ snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
+ fp = fopen(path, "r");
+ if (fp == NULL) {
+ warn("Failed to open file %s", path);
+ return NULL;
+ }
+ p = fgets(status, 5, fp);
+ fclose(fp);
+ if(!p || strcmp(status, "up\n") != 0) {
+ return NULL;
+ }
+
+ fp = fopen("/proc/net/wireless", "r");
+ if (fp == NULL) {
+ warn("Failed to open file /proc/net/wireless");
+ return NULL;
+ }
+
+ for (i = 0; i < 3; i++) {
+ if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
+ break;
+ }
+ fclose(fp);
+ if (i < 2 || !p)
+ return NULL;
+
+ if ((datastart = strstr(buf, iface)) == NULL)
+ return NULL;
+
+ datastart = (datastart+(strlen(iface)+1));
+ sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
+
+ return bprintf("%d", perc);
+}
+
+const char *
+wifi_essid(const char *iface)
+{
+ static char id[IW_ESSID_MAX_SIZE+1];
+ int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ struct iwreq wreq;
+
+ memset(&wreq, 0, sizeof(struct iwreq));
+ wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
+ snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
+
+ if (sockfd == -1) {
+ warn("Failed to get ESSID for interface %s", iface);
+ return NULL;
+ }
+ wreq.u.essid.pointer = id;
+ if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
+ warn("Failed to get ESSID for interface %s", iface);
+ return NULL;
+ }
+
+ close(sockfd);
+
+ if (strcmp(id, "") == 0)
+ return NULL;
+ else
+ return id;
+}