2017-09-17 16:45:03 +01:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2018-05-16 21:57:13 +01:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
2018-05-17 17:14:08 +01:00
|
|
|
#include <sys/socket.h>
|
2018-05-16 21:57:13 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "../util.h"
|
|
|
|
|
2019-02-13 00:47:23 +00:00
|
|
|
#define RSSI_TO_PERC(rssi) \
|
|
|
|
rssi >= -50 ? 100 : \
|
|
|
|
(rssi <= -100 ? 0 : \
|
|
|
|
(2 * (rssi + 100)))
|
|
|
|
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 17:10:39 +01:00
|
|
|
#if defined(__linux__)
|
2018-05-07 10:21:59 +01:00
|
|
|
#include <limits.h>
|
2018-05-01 18:01:22 +01:00
|
|
|
#include <linux/wireless.h>
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-01 18:01:22 +01:00
|
|
|
const char *
|
2018-07-06 07:08:48 +01:00
|
|
|
wifi_perc(const char *interface)
|
2018-05-01 18:01:22 +01:00
|
|
|
{
|
2018-07-07 13:05:53 +01:00
|
|
|
int cur;
|
|
|
|
size_t i;
|
2018-05-01 18:01:22 +01:00
|
|
|
char *p, *datastart;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char status[5];
|
|
|
|
FILE *fp;
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-07-08 16:42:58 +01:00
|
|
|
if (esnprintf(path, sizeof(path), "/sys/class/net/%s/operstate",
|
2018-07-06 07:08:48 +01:00
|
|
|
interface) < 0) {
|
2018-05-19 18:33:04 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-02 07:49:06 +01:00
|
|
|
if (!(fp = fopen(path, "r"))) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("fopen '%s':", path);
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-02 18:41:53 +01:00
|
|
|
p = fgets(status, 5, fp);
|
|
|
|
fclose(fp);
|
2018-07-08 16:44:53 +01:00
|
|
|
if (!p || strcmp(status, "up\n") != 0) {
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-02 07:49:06 +01:00
|
|
|
if (!(fp = fopen("/proc/net/wireless", "r"))) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("fopen '/proc/net/wireless':");
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-01 18:01:22 +01:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (!(p = fgets(buf, sizeof(buf) - 1, fp)))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2018-05-06 21:28:56 +01:00
|
|
|
if (i < 2 || !p) {
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
2018-05-01 18:01:22 +01:00
|
|
|
|
2018-07-06 07:08:48 +01:00
|
|
|
if (!(datastart = strstr(buf, interface))) {
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-07-06 07:08:48 +01:00
|
|
|
datastart = (datastart+(strlen(interface)+1));
|
2018-05-01 18:01:22 +01:00
|
|
|
sscanf(datastart + 1, " %*d %d %*d %*d\t\t %*d\t "
|
|
|
|
"%*d\t\t%*d\t\t %*d\t %*d\t\t %*d", &cur);
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-07-07 13:05:53 +01:00
|
|
|
/* 70 is the max of /proc/net/wireless */
|
|
|
|
return bprintf("%d", (int)((float)cur / 70 * 100));
|
2018-05-01 18:01:22 +01:00
|
|
|
}
|
2017-10-24 10:03:17 +01:00
|
|
|
|
2018-05-01 18:01:22 +01:00
|
|
|
const char *
|
2018-07-06 07:08:48 +01:00
|
|
|
wifi_essid(const char *interface)
|
2018-05-01 18:01:22 +01:00
|
|
|
{
|
|
|
|
static char id[IW_ESSID_MAX_SIZE+1];
|
2018-05-17 16:59:05 +01:00
|
|
|
int sockfd;
|
2018-05-01 18:01:22 +01:00
|
|
|
struct iwreq wreq;
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-01 18:01:22 +01:00
|
|
|
memset(&wreq, 0, sizeof(struct iwreq));
|
|
|
|
wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
|
2018-07-08 16:42:58 +01:00
|
|
|
if (esnprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s",
|
|
|
|
interface) < 0) {
|
2018-05-19 18:33:04 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-17 16:59:05 +01:00
|
|
|
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("socket 'AF_INET':");
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wreq.u.essid.pointer = id;
|
2018-05-06 21:28:56 +01:00
|
|
|
if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("ioctl 'SIOCGIWESSID':");
|
2018-05-01 18:01:22 +01:00
|
|
|
close(sockfd);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-01-06 22:37:09 +00:00
|
|
|
close(sockfd);
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-06 21:28:56 +01:00
|
|
|
if (!strcmp(id, "")) {
|
2018-05-01 18:01:22 +01:00
|
|
|
return NULL;
|
2018-05-06 21:28:56 +01:00
|
|
|
}
|
2018-05-02 07:49:06 +01:00
|
|
|
|
|
|
|
return id;
|
2018-05-01 18:01:22 +01:00
|
|
|
}
|
|
|
|
#elif defined(__OpenBSD__)
|
2018-05-16 21:37:43 +01:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_media.h>
|
|
|
|
#include <net80211/ieee80211.h>
|
2018-05-17 17:14:08 +01:00
|
|
|
#include <sys/select.h> /* before <sys/ieee80211_ioctl.h> for NBBY */
|
2018-05-16 21:37:43 +01:00
|
|
|
#include <net80211/ieee80211_ioctl.h>
|
2018-05-16 21:57:13 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2018-05-16 21:37:43 +01:00
|
|
|
|
|
|
|
static int
|
2018-07-06 07:08:48 +01:00
|
|
|
load_ieee80211_nodereq(const char *interface, struct ieee80211_nodereq *nr)
|
2018-05-16 21:37:43 +01:00
|
|
|
{
|
|
|
|
struct ieee80211_bssid bssid;
|
|
|
|
int sockfd;
|
2018-05-31 12:53:49 +01:00
|
|
|
uint8_t zero_bssid[IEEE80211_ADDR_LEN];
|
2018-05-16 22:17:30 +01:00
|
|
|
|
2018-05-16 22:08:33 +01:00
|
|
|
memset(&bssid, 0, sizeof(bssid));
|
2018-05-16 21:37:43 +01:00
|
|
|
memset(nr, 0, sizeof(struct ieee80211_nodereq));
|
2018-05-16 22:17:30 +01:00
|
|
|
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("socket 'AF_INET':");
|
2018-05-16 21:37:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2018-07-06 07:08:48 +01:00
|
|
|
strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
|
2018-05-16 22:17:30 +01:00
|
|
|
if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("ioctl 'SIOCG80211BSSID':");
|
2018-05-16 21:37:43 +01:00
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-05-31 12:53:49 +01:00
|
|
|
memset(&zero_bssid, 0, sizeof(zero_bssid));
|
|
|
|
if (memcmp(bssid.i_bssid, zero_bssid,
|
|
|
|
IEEE80211_ADDR_LEN) == 0) {
|
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-06 07:08:48 +01:00
|
|
|
strlcpy(nr->nr_ifname, interface, sizeof(nr->nr_ifname));
|
2018-05-17 17:08:31 +01:00
|
|
|
memcpy(&nr->nr_macaddr, bssid.i_bssid, sizeof(nr->nr_macaddr));
|
2018-05-16 22:17:30 +01:00
|
|
|
if ((ioctl(sockfd, SIOCG80211NODE, nr)) < 0 && nr->nr_rssi) {
|
2018-05-18 09:59:05 +01:00
|
|
|
warn("ioctl 'SIOCG80211NODE':");
|
2018-05-16 21:37:43 +01:00
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-16 22:17:30 +01:00
|
|
|
return close(sockfd), 1;
|
2018-05-16 21:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2018-07-06 07:08:48 +01:00
|
|
|
wifi_perc(const char *interface)
|
2018-05-16 21:37:43 +01:00
|
|
|
{
|
|
|
|
struct ieee80211_nodereq nr;
|
|
|
|
int q;
|
|
|
|
|
2018-07-06 07:08:48 +01:00
|
|
|
if (load_ieee80211_nodereq(interface, &nr)) {
|
2018-05-16 22:17:30 +01:00
|
|
|
if (nr.nr_max_rssi) {
|
2018-05-16 21:37:43 +01:00
|
|
|
q = IEEE80211_NODEREQ_RSSI(&nr);
|
2018-05-16 22:17:30 +01:00
|
|
|
} else {
|
2019-02-13 00:47:23 +00:00
|
|
|
q = RSSI_TO_PERC(nr.nr_rssi);
|
2018-05-16 22:17:30 +01:00
|
|
|
}
|
2018-05-20 23:16:54 +01:00
|
|
|
return bprintf("%d", q);
|
2018-05-16 21:37:43 +01:00
|
|
|
}
|
2018-05-16 22:17:30 +01:00
|
|
|
|
2018-05-16 21:37:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2018-07-06 07:08:48 +01:00
|
|
|
wifi_essid(const char *interface)
|
2018-05-16 21:37:43 +01:00
|
|
|
{
|
|
|
|
struct ieee80211_nodereq nr;
|
|
|
|
|
2018-07-06 07:08:48 +01:00
|
|
|
if (load_ieee80211_nodereq(interface, &nr)) {
|
2018-05-16 21:37:43 +01:00
|
|
|
return bprintf("%s", nr.nr_nwid);
|
|
|
|
}
|
2018-05-16 22:17:30 +01:00
|
|
|
|
2019-02-13 00:47:23 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net80211/ieee80211_ioctl.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
load_ieee80211req(int sock, const char *interface, void *data, int type, size_t *len)
|
|
|
|
{
|
|
|
|
char warn_buf[256];
|
|
|
|
struct ieee80211req ireq;
|
|
|
|
memset(&ireq, 0, sizeof(ireq));
|
|
|
|
ireq.i_type = type;
|
|
|
|
ireq.i_data = (caddr_t) data;
|
|
|
|
ireq.i_len = *len;
|
|
|
|
|
|
|
|
strlcpy(ireq.i_name, interface, sizeof(ireq.i_name));
|
|
|
|
if (ioctl(sock, SIOCG80211, &ireq) < 0) {
|
|
|
|
snprintf(warn_buf, sizeof(warn_buf),
|
|
|
|
"ioctl: 'SIOCG80211': %d", type);
|
|
|
|
warn(warn_buf);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = ireq.i_len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
wifi_perc(const char *interface)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
struct ieee80211req_sta_req sta;
|
|
|
|
uint8_t buf[24 * 1024];
|
|
|
|
} info;
|
|
|
|
uint8_t bssid[IEEE80211_ADDR_LEN];
|
|
|
|
int rssi_dbm;
|
|
|
|
int sockfd;
|
|
|
|
size_t len;
|
2019-02-13 14:16:17 +00:00
|
|
|
const char *fmt;
|
2019-02-13 00:47:23 +00:00
|
|
|
|
|
|
|
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
warn("socket 'AF_INET':");
|
2019-02-13 14:16:17 +00:00
|
|
|
return NULL;
|
2019-02-13 00:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Retreive MAC address of interface */
|
|
|
|
len = IEEE80211_ADDR_LEN;
|
2019-02-13 14:16:17 +00:00
|
|
|
fmt = NULL;
|
2019-02-13 00:47:23 +00:00
|
|
|
if (load_ieee80211req(sockfd, interface, &bssid, IEEE80211_IOC_BSSID, &len))
|
|
|
|
{
|
|
|
|
/* Retrieve info on station with above BSSID */
|
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
memcpy(info.sta.is_u.macaddr, bssid, sizeof(bssid));
|
|
|
|
|
|
|
|
len = sizeof(info);
|
|
|
|
if (load_ieee80211req(sockfd, interface, &info, IEEE80211_IOC_STA_INFO, &len)) {
|
|
|
|
rssi_dbm = info.sta.info[0].isi_noise +
|
|
|
|
info.sta.info[0].isi_rssi / 2;
|
2019-02-13 14:16:17 +00:00
|
|
|
|
|
|
|
fmt = bprintf("%d", RSSI_TO_PERC(rssi_dbm));
|
2019-02-13 00:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sockfd);
|
2019-02-13 14:16:17 +00:00
|
|
|
return fmt;
|
2019-02-13 00:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
wifi_essid(const char *interface)
|
|
|
|
{
|
|
|
|
char ssid[IEEE80211_NWID_LEN + 1];
|
|
|
|
size_t len;
|
|
|
|
int sockfd;
|
2019-02-13 14:16:17 +00:00
|
|
|
const char *fmt;
|
2019-02-13 00:47:23 +00:00
|
|
|
|
|
|
|
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
warn("socket 'AF_INET':");
|
2019-02-13 14:16:17 +00:00
|
|
|
return NULL;
|
2019-02-13 00:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:16:17 +00:00
|
|
|
fmt = NULL;
|
2019-02-13 00:47:23 +00:00
|
|
|
len = sizeof(ssid);
|
|
|
|
memset(&ssid, 0, len);
|
|
|
|
if (load_ieee80211req(sockfd, interface, &ssid, IEEE80211_IOC_SSID, &len )) {
|
|
|
|
if (len < sizeof(ssid))
|
|
|
|
len += 1;
|
|
|
|
else
|
|
|
|
len = sizeof(ssid);
|
|
|
|
|
|
|
|
ssid[len - 1] = '\0';
|
2019-02-13 14:16:17 +00:00
|
|
|
fmt = bprintf("%s", ssid);
|
2019-02-13 00:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-13 14:16:17 +00:00
|
|
|
close(sockfd);
|
|
|
|
return fmt;
|
2018-05-16 21:37:43 +01:00
|
|
|
}
|
Revert component-split
this reverts the commits from 92ab9ef52ebcb097add97d9f78e67ad1c1d6a6ec up to
d42870d6ca7fb587b38f8cf6d6821ae33a53a696.
After heavy consideration, the component split has more disadvantages
than advantages, especially given there will be utility-functions
sharing quite a lot of code that would then need to be duplicated, as it
does not fit into the util.c due to its speciality.
One big advantage of the component-wise build is readability, and
without doubt, this was achieved here. This point will be addressed
with a different approach that will be visible in the upcoming commits.
One big disadvantage of the component build is the fact that it
introduces state to the build process which is not necessary. Before its
introduction, the only influencing factors where the system-defines
__linux__ and __OpenBSD__. With the components, we are also relying on
the output of uname(1).
Additionally, if the os.mk is not present, make gives the output
$ make
Makefile:5: os.mk: No such file or directory
make: *** No rule to make target 'os.mk'. Stop.
This could easily be fixed by providing some sort of meta-rule for this
file, however, it indicates the problem we have here, and this entire
statefulness will heavily complicate packaging of this tool and makes
the build process much more complex than it actually has to be.
2018-05-01 17:10:39 +01:00
|
|
|
#endif
|