X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/ebf5a35052add4298e43e0f261927b7ab2a2e7b9..cd884c2f0a585f8c6a49f1faacacb7925d75dbb0:/components/wifi.c?ds=inline

diff --git a/components/wifi.c b/components/wifi.c
index 084ab99..b3e1723 100644
--- a/components/wifi.c
+++ b/components/wifi.c
@@ -1,92 +1,167 @@
 /* See LICENSE file for copyright and license details. */
-#ifdef __linux__
-#include <err.h>
+#include <errno.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 <sys/socket.h>
 #include <unistd.h>
 
 #include "../util.h"
 
-const char *
-wifi_perc(const char *iface)
-{
-	int i, cur;
-	float perc;
-	int total = 70; /* the max of /proc/net/wireless */
-	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;
-	}
+#if defined(__linux__)
+	#include <limits.h>
+	#include <linux/wireless.h>
 
-	fp = fopen("/proc/net/wireless", "r");
-	if (fp == NULL) {
-		warn("Failed to open file /proc/net/wireless");
-		return NULL;
-	}
+	const char *
+	wifi_perc(const char *iface)
+	{
+		int i, cur;
+		int total = 70; /* the max of /proc/net/wireless */
+		char *p, *datastart;
+		char path[PATH_MAX];
+		char status[5];
+		FILE *fp;
+
+		if (esnprintf(path, sizeof(path),
+		              "/sys/class/net/%s/operstate",
+		              iface) < 0) {
+			return NULL;
+		}
+		if (!(fp = fopen(path, "r"))) {
+			warn("fopen '%s':", path);
+			return NULL;
+		}
+		p = fgets(status, 5, fp);
+		fclose(fp);
+		if(!p || strcmp(status, "up\n") != 0) {
+			return NULL;
+		}
+
+		if (!(fp = fopen("/proc/net/wireless", "r"))) {
+			warn("fopen '/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;
+		}
 
-	for (i = 0; i < 3; i++) {
-		if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
-			break;
+		if (!(datastart = strstr(buf, iface))) {
+			return NULL;
+		}
+
+		datastart = (datastart+(strlen(iface)+1));
+		sscanf(datastart + 1, " %*d   %d  %*d  %*d\t\t  %*d\t   "
+		       "%*d\t\t%*d\t\t %*d\t  %*d\t\t %*d", &cur);
+
+		return bprintf("%d", (int)((float)cur / total * 100));
 	}
-	fclose(fp);
-	if (i < 2 || !p)
-		return NULL;
 
-	if ((datastart = strstr(buf, iface)) == NULL)
-		return NULL;
+	const char *
+	wifi_essid(const char *iface)
+	{
+		static char id[IW_ESSID_MAX_SIZE+1];
+		int sockfd;
+		struct iwreq wreq;
 
-	datastart = (datastart+(strlen(iface)+1));
-	sscanf(datastart + 1, " %*d   %d  %*d  %*d		  %*d	   %*d		%*d		 %*d	  %*d		 %*d", &cur);
+		memset(&wreq, 0, sizeof(struct iwreq));
+		wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
+		if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name),
+		              "%s", iface) < 0) {
+			return NULL;
+		}
 
-	perc = (float)cur / total * 100.0;
+		if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+			warn("socket 'AF_INET':");
+			return NULL;
+		}
+		wreq.u.essid.pointer = id;
+		if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
+			warn("ioctl 'SIOCGIWESSID':");
+			close(sockfd);
+			return NULL;
+		}
 
-	return bprintf("%.0f", perc);
-}
+		close(sockfd);
 
-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;
+		if (!strcmp(id, "")) {
+			return NULL;
+		}
 
-	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);
+		return id;
+	}
+#elif defined(__OpenBSD__)
+	#include <net/if.h>
+	#include <net/if_media.h>
+	#include <net80211/ieee80211.h>
+	#include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */
+	#include <net80211/ieee80211_ioctl.h>
+	#include <stdlib.h>
+	#include <sys/types.h>
 
-	if (sockfd == -1) {
-		warn("Failed to get ESSID for interface %s", iface);
-		return NULL;
+	static int
+	load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
+	{
+		struct ieee80211_bssid bssid;
+		int sockfd;
+
+		memset(&bssid, 0, sizeof(bssid));
+		memset(nr, 0, sizeof(struct ieee80211_nodereq));
+		if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
+			warn("socket 'AF_INET':");
+			return 0;
+		}
+		strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
+		if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
+			warn("ioctl 'SIOCG80211BSSID':");
+			close(sockfd);
+			return 0;
+		}
+		strlcpy(nr->nr_ifname, iface, sizeof(nr->nr_ifname));
+		memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
+		if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
+			warn("ioctl 'SIOCG80211NODE':");
+			close(sockfd);
+			return 0;
+		}
+
+		return close(sockfd), 1;
 	}
-	wreq.u.essid.pointer = id;
-	if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
-		warn("Failed to get ESSID for interface %s", iface);
-		close(sockfd);
+
+	const char *
+	wifi_perc(const char *iface)
+	{
+		struct ieee80211_nodereq nr;
+		int q;
+
+		if (load_ieee80211_nodereq(iface, &nr)) {
+			if (nr.nr_max_rssi) {
+				q = IEEE80211_NODEREQ_RSSI(&nr);
+			} else {
+				q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
+				(2 * (nr.nr_rssi + 100)));
+			}
+			return bprintf("%d", q);
+		}
+
 		return NULL;
 	}
 
-	close(sockfd);
+	const char *
+	wifi_essid(const char *iface)
+	{
+		struct ieee80211_nodereq nr;
+
+		if (load_ieee80211_nodereq(iface, &nr)) {
+			return bprintf("%s", nr.nr_nwid);
+		}
 
-	if (strcmp(id, "") == 0)
 		return NULL;
-	else
-		return id;
-}
+	}
 #endif