|
| 1 | +#include "wifi.h" |
| 2 | +#include "common/io/io.h" |
| 3 | +#include "util/stringUtils.h" |
| 4 | + |
| 5 | +#include <sys/ioctl.h> |
| 6 | +#include <sys/socket.h> |
| 7 | +#include <net/if.h> |
| 8 | +#include <net80211/ieee80211.h> |
| 9 | +#include <net80211/ieee80211_ioctl.h> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +const char* ffDetectWifi(FFlist* result) |
| 13 | +{ |
| 14 | + struct if_nameindex* infs = if_nameindex(); |
| 15 | + if(!infs) { |
| 16 | + return "if_nameindex() failed"; |
| 17 | + } |
| 18 | + |
| 19 | + FF_AUTO_CLOSE_FD int sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 20 | + if(sock < 0) { |
| 21 | + return "socket() failed"; |
| 22 | + } |
| 23 | + |
| 24 | + for(struct if_nameindex* i = infs; !(i->if_index == 0 && i->if_name == NULL); ++i) |
| 25 | + { |
| 26 | + if (!ffStrStartsWith(i->if_name, "iwm")) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + FFWifiResult* item = (FFWifiResult*) ffListAdd(result); |
| 31 | + ffStrbufInitS(&item->inf.description, i->if_name); |
| 32 | + ffStrbufInit(&item->inf.status); |
| 33 | + ffStrbufInit(&item->conn.status); |
| 34 | + ffStrbufInit(&item->conn.ssid); |
| 35 | + ffStrbufInit(&item->conn.bssid); |
| 36 | + ffStrbufInit(&item->conn.protocol); |
| 37 | + ffStrbufInit(&item->conn.security); |
| 38 | + item->conn.signalQuality = 0.0/0.0; |
| 39 | + item->conn.rxRate = 0.0/0.0; |
| 40 | + item->conn.txRate = 0.0/0.0; |
| 41 | + item->conn.channel = 0; |
| 42 | + item->conn.frequency = 0; |
| 43 | + |
| 44 | + struct ieee80211_nodereq nr = {}; |
| 45 | + strlcpy(nr.nr_ifname, i->if_name, sizeof(nr.nr_ifname)); |
| 46 | + |
| 47 | + // 首先检查接口状态 |
| 48 | + struct ifreq ifr = {}; |
| 49 | + strlcpy(ifr.ifr_name, i->if_name, sizeof(ifr.ifr_name)); |
| 50 | + if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) { |
| 51 | + ffStrbufSetStatic(&item->inf.status, "Unknown"); |
| 52 | + } else { |
| 53 | + ffStrbufSetStatic(&item->inf.status, ifr.ifr_flags & IFF_UP ? "Up" : "Down"); |
| 54 | + } |
| 55 | + |
| 56 | + // 尝试获取当前连接的节点信息 |
| 57 | + if (ioctl(sock, SIOCG80211NODE, &nr) < 0) { |
| 58 | + ffStrbufSetStatic(&item->conn.status, "Not associated"); |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + // 获取SSID |
| 63 | + if (nr.nr_nwid_len > 0) { |
| 64 | + ffStrbufSetStatic(&item->conn.status, "Associated"); |
| 65 | + ffStrbufAppendNS(&item->conn.ssid, nr.nr_nwid_len, (char*)nr.nr_nwid); |
| 66 | + } else { |
| 67 | + ffStrbufSetStatic(&item->conn.status, "Not associated"); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // 获取BSSID |
| 72 | + ffStrbufSetF(&item->conn.bssid, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 73 | + nr.nr_bssid[0], nr.nr_bssid[1], nr.nr_bssid[2], |
| 74 | + nr.nr_bssid[3], nr.nr_bssid[4], nr.nr_bssid[5]); |
| 75 | + |
| 76 | + // 获取信道和频率 |
| 77 | + item->conn.channel = nr.nr_channel; |
| 78 | + |
| 79 | + // 获取信号强度 |
| 80 | + if (nr.nr_max_rssi) { |
| 81 | + item->conn.signalQuality = ((float)nr.nr_rssi / nr.nr_max_rssi) * 100.0; |
| 82 | + } |
| 83 | + |
| 84 | + // 确定协议类型 |
| 85 | + if (nr.nr_flags & IEEE80211_NODEREQ_HT) { |
| 86 | + ffStrbufSetStatic(&item->conn.protocol, "802.11n (Wi-Fi 4)"); |
| 87 | + } else if (nr.nr_flags & IEEE80211_NODEREQ_VHT) { |
| 88 | + ffStrbufSetStatic(&item->conn.protocol, "802.11ac (Wi-Fi 5)"); |
| 89 | + } else if (nr.nr_chan_flags & IEEE80211_CHANINFO_5GHZ) { |
| 90 | + ffStrbufSetStatic(&item->conn.protocol, "802.11a"); |
| 91 | + } else if (nr.nr_chan_flags & IEEE80211_CHANINFO_2GHZ) { |
| 92 | + ffStrbufSetStatic(&item->conn.protocol, "802.11g"); |
| 93 | + } |
| 94 | + |
| 95 | + // 获取安全设置 |
| 96 | + struct ieee80211_wpaparams wpa = {}; |
| 97 | + strlcpy(wpa.i_name, i->if_name, sizeof(wpa.i_name)); |
| 98 | + |
| 99 | + if (ioctl(sock, SIOCG80211WPAPARMS, &wpa) >= 0 && wpa.i_enabled) { |
| 100 | + if (wpa.i_protos & IEEE80211_WPA_PROTO_WPA2) |
| 101 | + ffStrbufSetStatic(&item->conn.security, "WPA2"); |
| 102 | + else if (wpa.i_protos & IEEE80211_WPA_PROTO_WPA1) |
| 103 | + ffStrbufSetStatic(&item->conn.security, "WPA"); |
| 104 | + } else { |
| 105 | + struct ieee80211_nwkey nwkey = {}; |
| 106 | + strlcpy(nwkey.i_name, i->if_name, sizeof(nwkey.i_name)); |
| 107 | + |
| 108 | + if (ioctl(sock, SIOCG80211NWKEY, &nwkey) >= 0) { |
| 109 | + if (nwkey.i_wepon) |
| 110 | + ffStrbufSetStatic(&item->conn.security, "WEP"); |
| 111 | + else |
| 112 | + ffStrbufSetStatic(&item->conn.security, "Open"); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if_freenameindex(infs); |
| 118 | + return NULL; |
| 119 | +} |
0 commit comments