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 <errno.h>
|
|
|
|
#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"
|
|
|
|
|
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 *
|
|
|
|
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;
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-01 18:01:22 +01:00
|
|
|
snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface,
|
|
|
|
"/operstate");
|
2018-05-02 07:49:06 +01:00
|
|
|
if (!(fp = fopen(path, "r"))) {
|
2018-05-01 18:01:22 +01:00
|
|
|
fprintf(stderr, "fopen '%s': %s\n", path,
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-05-02 18:41:53 +01:00
|
|
|
p = fgets(status, 5, fp);
|
|
|
|
fclose(fp);
|
|
|
|
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-01 18:01:22 +01:00
|
|
|
fprintf(stderr, "fopen '/proc/net/wireless': %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
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-05-06 21:28:56 +01:00
|
|
|
if (!(datastart = strstr(buf, iface))) {
|
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-05-01 18:01:22 +01:00
|
|
|
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);
|
2017-09-17 15:18:17 +01:00
|
|
|
|
2018-05-07 11:25:34 +01:00
|
|
|
return bprintf("%d", (int)((float)cur / total * 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 *
|
|
|
|
wifi_essid(const char *iface)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
|
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-01 18:01:22 +01:00
|
|
|
fprintf(stderr, "socket 'AF_INET': %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
wreq.u.essid.pointer = id;
|
2018-05-06 21:28:56 +01:00
|
|
|
if (ioctl(sockfd,SIOCGIWESSID, &wreq) < 0) {
|
|
|
|
fprintf(stderr, "ioctl 'SIOCGIWESSID': %s\n", strerror(errno));
|
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
|
|
|
|
load_ieee80211_nodereq(const char *iface, struct ieee80211_nodereq *nr)
|
|
|
|
{
|
|
|
|
struct ieee80211_bssid bssid;
|
|
|
|
int sockfd;
|
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-16 21:37:43 +01:00
|
|
|
fprintf(stderr, "socket 'AF_INET': %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strlcpy(bssid.i_name, iface, sizeof(bssid.i_name));
|
2018-05-16 22:17:30 +01:00
|
|
|
if ((ioctl(sockfd, SIOCG80211BSSID, &bssid)) < 0) {
|
2018-05-16 21:37:43 +01:00
|
|
|
fprintf(stderr, "ioctl 'SIOCG80211BSSID': %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
close(sockfd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strlcpy(nr->nr_ifname, iface, 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-16 21:37:43 +01:00
|
|
|
fprintf(stderr, "ioctl 'SIOCG80211NODE': %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
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 *
|
|
|
|
wifi_perc(const char *iface)
|
|
|
|
{
|
|
|
|
struct ieee80211_nodereq nr;
|
|
|
|
int q;
|
|
|
|
|
|
|
|
if (load_ieee80211_nodereq(iface, &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 {
|
2018-05-16 21:37:43 +01:00
|
|
|
q = nr.nr_rssi >= -50 ? 100 : (nr.nr_rssi <= -100 ? 0 :
|
|
|
|
(2 * (nr.nr_rssi + 100)));
|
2018-05-16 22:17:30 +01:00
|
|
|
}
|
2018-05-16 21:37:43 +01:00
|
|
|
return bprintf("%d", q);
|
|
|
|
}
|
2018-05-16 22:17:30 +01:00
|
|
|
|
2018-05-16 21:37:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
wifi_essid(const char *iface)
|
|
|
|
{
|
|
|
|
struct ieee80211_nodereq nr;
|
|
|
|
|
|
|
|
if (load_ieee80211_nodereq(iface, &nr)) {
|
|
|
|
return bprintf("%s", nr.nr_nwid);
|
|
|
|
}
|
2018-05-16 22:17:30 +01:00
|
|
|
|
2018-05-16 21:37:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
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
|