This is a small C/C++ library for monitoring signal strength of WiFi networks. It can collect data from:
- associated station only (rapidly)
- all networks around (slow passive scan)
Any platforms supporting nl80211 netlink interface (new 802.11 interface). Generally *nix platforms.
Wireless devices that have cfg80211/mac80211 driver. Currently all new Linux wireless drivers should be written targeting either cfg80211 for fullmac devices or mac80211 for softmac devices.
The return values of public library functions are subject to change (mainly error codes). Currently if anything goes wrong library dies cleanly with error message on stderr explaining what happened. Note that if library dies - it also kills your program and you have no chance to recover.
The library depends on libmnl for netlink nl80211 user space - kernel space communication.
$ sudo apt-get update
$ sudo apt-get install build-essential
$ sudo apt-get install libmnl0 libmnl-dev
sudo apt-get install git
git clone https://github.com/bmegli/wifi-scan.git
cd wifi-scan
make all
Check your wireless interface name with ifconfig
:
ifconfig
Run wifi-scan-station
with your interface, e.g. wlan0
./wifi-scan-station wlan0
Run wifi-scan-all
with your interface, e.g. wlan0
.
Triggering a scan needs permission so:
sudo ./wifi-scan-all wlan0
See examples directory for more complete and commented examples with error handling.
Normally you would call wifi_scan_station
or wifi_scan_all
in a loop.
struct station_info station;
struct wifi_scan *wifi = wifi_scan_init("wlan0");
if (wifi_scan_station(wifi, &station) > 0 )
printf("%s signal %d dBm %u rx %u tx\n",
station.ssid, station.signal_dbm,
station.rx_packets, station.tx_packets);
wifi_scan_close(wifi);
int status, i;
struct bss_info bss[10];
struct wifi_scan *wifi = wifi_scan_init("wlan0");
status=wifi_scan_all(wifi, bss, 10);
for(i=0;i<status && i<10;++i)
printf("%s signal %d dBm seen %d ms ago status %s\n",
bss[i].ssid, bss[i].signal_mbm/100, bss[i].seen_ms_ago,
(bss[i].status==BSS_ASSOCIATED ? "associated" : ""));
wifi_scan_close(wifi);
Don't forget to link with lmnl
C
gcc wifi_scan.c your_program.c -lmnl -o your-program
C++
gcc -c wifi_scan.c
g++ -c your_program.cpp
g++ wifi_scan.o your_program.o -lmnl -o your-program
Here are some of the resources that helped writing this library:
- Netlink Library (libnl) documentation
- Minimalistic Netlink Library (libmnl) doxygen documentation and code
- nl80211 header file
- iw code
- wavemon code
- wpa_supplicant and hostapd code
And finally the implementation wifi-scan.c
has some comments that may be usefull to you.