+
+static int get_wireless_info(const char *interface, wireless_info_t *info) {
+ memset(info, 0, sizeof(wireless_info_t));
+
+#ifdef LINUX
+ struct nl_sock *sk = nl_socket_alloc();
+ if (genl_connect(sk) != 0)
+ goto error1;
+
+ if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_scan_cb, info) < 0)
+ goto error1;
+
+ const int nl80211_id = genl_ctrl_resolve(sk, "nl80211");
+ if (nl80211_id < 0)
+ goto error1;
+
+ const unsigned int ifidx = if_nametoindex(interface);
+ if (ifidx == 0)
+ goto error1;
+
+ struct nl_msg *msg = NULL;
+ if ((msg = nlmsg_alloc()) == NULL)
+ goto error1;
+
+ if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0) ||
+ nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0)
+ goto error2;
+
+ if (nl_send_sync(sk, msg) < 0)
+ // nl_send_sync calls nlmsg_free()
+ goto error1;
+ msg = NULL;
+
+ if (nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, gwi_sta_cb, info) < 0)
+ goto error1;
+
+ if ((msg = nlmsg_alloc()) == NULL)
+ goto error1;
+
+ if (!genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, nl80211_id, 0, NLM_F_DUMP, NL80211_CMD_GET_STATION, 0) || nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifidx) < 0 || nla_put(msg, NL80211_ATTR_MAC, 6, info->bssid) < 0)
+ goto error2;
+
+ if (nl_send_sync(sk, msg) < 0)
+ // nl_send_sync calls nlmsg_free()
+ goto error1;
+ msg = NULL;
+
+ nl_socket_free(sk);
+ return 1;
+
+error2:
+ nlmsg_free(msg);
+error1:
+ nl_socket_free(sk);
+ return 0;
+
+#endif
+#if defined(__FreeBSD__) || defined(__DragonFly__)
+ int s, inwid;
+ union {
+ struct ieee80211req_sta_req req;
+ uint8_t buf[24 * 1024];
+ } u;
+ struct ieee80211req na;
+ char bssid[IEEE80211_ADDR_LEN];
+ size_t len;
+
+ if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ return (0);
+
+ memset(&na, 0, sizeof(na));
+ strlcpy(na.i_name, interface, sizeof(na.i_name));
+ na.i_type = IEEE80211_IOC_SSID;
+ na.i_data = &info->essid[0];
+ na.i_len = IEEE80211_NWID_LEN + 1;
+ if ((inwid = ioctl(s, SIOCG80211, (caddr_t)&na)) == -1) {
+ close(s);
+ return (0);
+ }
+ if (inwid == 0) {
+ if (na.i_len <= IEEE80211_NWID_LEN)
+ len = na.i_len + 1;
+ else
+ len = IEEE80211_NWID_LEN + 1;
+ info->essid[len - 1] = '\0';
+ } else {
+ close(s);
+ return (0);
+ }
+ info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
+
+ memset(&na, 0, sizeof(na));
+ strlcpy(na.i_name, interface, sizeof(na.i_name));
+ na.i_type = IEEE80211_IOC_BSSID;
+ na.i_data = bssid;
+ na.i_len = sizeof(bssid);
+
+ if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
+ close(s);
+ return (0);
+ }
+
+ memcpy(u.req.is_u.macaddr, bssid, sizeof(bssid));
+ memset(&na, 0, sizeof(na));
+ strlcpy(na.i_name, interface, sizeof(na.i_name));
+ na.i_type = IEEE80211_IOC_STA_INFO;
+ na.i_data = &u;
+ na.i_len = sizeof(u);
+
+ if (ioctl(s, SIOCG80211, (caddr_t)&na) == -1) {
+ close(s);
+ return (0);
+ }
+
+ close(s);
+ if (na.i_len >= sizeof(u.req)) {
+ /*
+ * Just use the first BSSID returned even if there are
+ * multiple APs sharing the same BSSID.
+ */
+ info->signal_level = u.req.info[0].isi_rssi / 2 +
+ u.req.info[0].isi_noise;
+ info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
+ info->noise_level = u.req.info[0].isi_noise;
+ info->flags |= WIRELESS_INFO_FLAG_HAS_NOISE;
+ }
+
+ return 1;
+#endif
+#ifdef __OpenBSD__
+ struct ifreq ifr;
+ struct ieee80211_bssid bssid;
+ struct ieee80211_nwid nwid;
+ struct ieee80211_nodereq nr;
+
+ struct ether_addr ea;
+
+ int s, len, ibssid, inwid;
+ u_int8_t zero_bssid[IEEE80211_ADDR_LEN];
+
+ if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
+ return (0);
+
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_data = (caddr_t)&nwid;
+ (void)strlcpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name));
+ inwid = ioctl(s, SIOCG80211NWID, (caddr_t)&ifr);
+
+ memset(&bssid, 0, sizeof(bssid));
+ strlcpy(bssid.i_name, interface, sizeof(bssid.i_name));
+ ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
+
+ if (ibssid != 0 || inwid != 0) {
+ close(s);
+ return 0;
+ }
+
+ /* NWID */
+ {
+ if (nwid.i_len <= IEEE80211_NWID_LEN)
+ len = nwid.i_len + 1;
+ else
+ len = IEEE80211_NWID_LEN + 1;
+
+ strncpy(&info->essid[0], (char *)nwid.i_nwid, len);
+ info->essid[IW_ESSID_MAX_SIZE] = '\0';
+ info->flags |= WIRELESS_INFO_FLAG_HAS_ESSID;
+ }
+
+ /* Signal strength */
+ {
+ memset(&zero_bssid, 0, sizeof(zero_bssid));
+ if (ibssid == 0 && memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
+ memcpy(&ea.ether_addr_octet, bssid.i_bssid, sizeof(ea.ether_addr_octet));
+
+ bzero(&nr, sizeof(nr));
+ bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
+ strlcpy(nr.nr_ifname, interface, sizeof(nr.nr_ifname));
+
+ if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
+ info->signal_level = nr.nr_rssi;
+ if (nr.nr_max_rssi)
+ info->signal_level_max = nr.nr_max_rssi;
+
+ info->flags |= WIRELESS_INFO_FLAG_HAS_SIGNAL;
+ }
+ }
+ }
+
+ close(s);
+ return 1;
+#endif
+ return 0;