|
|
@@ -1,11 +1,15 @@
|
|
|
#include <linux/netlink.h>
|
|
|
#include <linux/rtnetlink.h>
|
|
|
#include <netlink/netlink.h>
|
|
|
+#include <netlink/cache.h>
|
|
|
#include <netlink/genl/genl.h>
|
|
|
#include <netlink/genl/ctrl.h>
|
|
|
+#include <netlink/route/link.h>
|
|
|
+#include <netlink/route/addr.h>
|
|
|
#include <netlink/msg.h>
|
|
|
#include <netlink/attr.h>
|
|
|
#include <linux/nl80211.h>
|
|
|
+#include <ifaddrs.h>
|
|
|
#include <net/if.h>
|
|
|
#include <unistd.h>
|
|
|
#include <sys/ioctl.h>
|
|
|
@@ -21,8 +25,7 @@
|
|
|
#include "paras.h"
|
|
|
#include "common.h"
|
|
|
|
|
|
-#define USE_WIFI
|
|
|
-#define USE_LIBNL
|
|
|
+//#define USE_WIFI
|
|
|
|
|
|
#define ONCE_CNT 20
|
|
|
#define WLAN_IFNAME "wlan0"
|
|
|
@@ -76,7 +79,6 @@ static void res_print(result_t *res)
|
|
|
printf("___[%d].ssid: %s\n", i, info->ssid);
|
|
|
printf("___[%d].bssid: %s\n", i, info->bssid);
|
|
|
printf("___[%d].freq: %dMHz\n", i, info->freq);
|
|
|
- printf("___[%d].intensity: %d\n", i, info->intensity);
|
|
|
printf("___[%d].auth: %s\n", i, auth_str[info->auth]);
|
|
|
printf("___[%d].crypt: %s\n", i, crypt_str[info->crypt]);
|
|
|
printf("\n");
|
|
|
@@ -111,13 +113,6 @@ static int sta_start(ap_info_t *info, int on)
|
|
|
sprintf(tmp, "wpa_supplicant -B -i %s -c %s", WLAN_IFNAME, STA_CONF);
|
|
|
r = system(tmp);
|
|
|
|
|
|
- ipv = sys_get_ip_ver(info->ipaddr);
|
|
|
- if(ipv==IP_V6) {
|
|
|
- mask = 64;
|
|
|
- }
|
|
|
- sprintf(tmp, "ip addr add %s/%d dev %s", info->ipaddr, mask, WLAN_IFNAME);
|
|
|
- r = system(tmp);
|
|
|
-
|
|
|
sprintf(tmp, "udhcpc -b -i %s", WLAN_IFNAME);
|
|
|
r = system(tmp);
|
|
|
}
|
|
|
@@ -130,7 +125,7 @@ static int sta_start(ap_info_t *info, int on)
|
|
|
}
|
|
|
static int ap_start(ap_info_t *info, int on)
|
|
|
{
|
|
|
- int r;
|
|
|
+ int r,ipv,mask=24;
|
|
|
char tmp[100];
|
|
|
|
|
|
//https://blog.csdn.net/qq_40311939/article/details/139552431
|
|
|
@@ -139,8 +134,11 @@ static int ap_start(ap_info_t *info, int on)
|
|
|
sprintf(tmp, "hostapd -B %s", AP_CONF);
|
|
|
r = system(tmp);
|
|
|
|
|
|
- //ipv = sys_get_ip_ver(info->ipaddr);
|
|
|
- sprintf(tmp, "ip addr add %s/24 dev %s", info->ipaddr, WLAN_IFNAME);
|
|
|
+ ipv = sys_get_ip_ver(info->ipaddr);
|
|
|
+ if(ipv==IP_V6) {
|
|
|
+ mask = 64;
|
|
|
+ }
|
|
|
+ sprintf(tmp, "ip addr add %s/%d dev %s", info->ipaddr, mask, WLAN_IFNAME);
|
|
|
r = system(tmp);
|
|
|
|
|
|
sprintf(tmp, "udhcpd -i %s -SC /etc/udhcpd.conf", WLAN_IFNAME);
|
|
|
@@ -243,6 +241,37 @@ static int no_seq_check(struct nl_msg *msg, void *arg)
|
|
|
// Callback for NL_CB_SEQ_CHECK.
|
|
|
return NL_OK;
|
|
|
}
|
|
|
+static int callback_stat(struct nl_msg *msg, void *arg)
|
|
|
+{
|
|
|
+ struct nlattr *tb[NL80211_ATTR_MAX + 1];
|
|
|
+ struct nlattr *bss[NL80211_BSS_MAX + 1];
|
|
|
+ struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
|
|
|
+ struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
|
|
|
+ struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
|
|
|
+ [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 }, // 信号强度
|
|
|
+ [NL80211_STA_INFO_CONNECTED_TIME] = { .type = NLA_U32 }, // 连接时间
|
|
|
+ };
|
|
|
+ ap_stat_t *stat=(ap_stat_t*)arg;
|
|
|
+
|
|
|
+ nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
|
|
|
+ genlmsg_attrlen(gnlh, 0), NULL);
|
|
|
+
|
|
|
+ if (tb[NL80211_ATTR_STA_INFO]) {
|
|
|
+ stat->link = 1;
|
|
|
+ nla_parse(sinfo, NL80211_STA_INFO_MAX, nla_data(tb[NL80211_ATTR_STA_INFO]),
|
|
|
+ nla_len(tb[NL80211_ATTR_STA_INFO]),stats_policy);
|
|
|
+ if (sinfo[NL80211_STA_INFO_SIGNAL]) {
|
|
|
+ stat->signal = nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ stat->signal = 0;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ stat->link = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ return NL_OK;
|
|
|
+}
|
|
|
static int callback_trigger(struct nl_msg *msg, void *arg)
|
|
|
{
|
|
|
// Called by the kernel when the scan is done or has been aborted.
|
|
|
@@ -269,7 +298,6 @@ static int callback_dump(struct nl_msg *msg, void *arg)
|
|
|
{
|
|
|
// Called by the kernel with a dump of the successful scan's data. Called for each SSID.
|
|
|
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
|
|
|
- char mac_addr[20];
|
|
|
struct nlattr *tb[NL80211_ATTR_MAX + 1];
|
|
|
struct nlattr *bss[NL80211_BSS_MAX + 1];
|
|
|
static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
|
|
|
@@ -304,11 +332,6 @@ static int callback_dump(struct nl_msg *msg, void *arg)
|
|
|
sprintf(info->bssid, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1], bssid[2], bssid[3], bssid[4], bssid[5]);
|
|
|
}
|
|
|
|
|
|
- if (bss[NL80211_BSS_SIGNAL_MBM]) {
|
|
|
- int32_t signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
|
|
|
- info->intensity = (signal/100);
|
|
|
- }
|
|
|
-
|
|
|
if (bss[NL80211_BSS_FREQUENCY]) {
|
|
|
uint32_t freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
|
|
|
info->freq = freq;
|
|
|
@@ -429,7 +452,7 @@ static int nl80211_init(nl_handle_t *h)
|
|
|
goto fail;
|
|
|
}
|
|
|
|
|
|
- h->msg = nlmsg_alloc();
|
|
|
+ h->msg = nlmsg_alloc();
|
|
|
if (!h->msg) {
|
|
|
fprintf(stderr, "Failed to allocate netlink message\n");
|
|
|
goto fail;
|
|
|
@@ -449,7 +472,74 @@ static int nl80211_free(nl_handle_t *h)
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
+static int get_ipaddr(char *ifname, char *ipaddr)
|
|
|
+{
|
|
|
+ int r=-1;
|
|
|
+ struct ifaddrs *ifap, *ifa;
|
|
|
|
|
|
+ if (getifaddrs(&ifap) == -1) {
|
|
|
+ printf("___getifaddrs() error\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
|
|
+ if (ifa->ifa_addr != NULL && strcmp(ifa->ifa_name, ifname)==0) {
|
|
|
+ if(ifa->ifa_addr->sa_family==AF_INET) {
|
|
|
+ struct sockaddr_in *sa,*msk;
|
|
|
+ sa = (struct sockaddr_in *) ifa->ifa_addr;
|
|
|
+ msk = (struct sockaddr_in *) ifa->ifa_netmask;
|
|
|
+
|
|
|
+ char *p = (char*)inet_ntop(ifa->ifa_addr->sa_family, &sa->sin_addr, ipaddr, INET6_ADDRSTRLEN);
|
|
|
+ if(p) r = 0;
|
|
|
+ if(r==0) break;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ struct sockaddr_in6 *sa,*msk;
|
|
|
+ sa = (struct sockaddr_in6 *) ifa->ifa_addr;
|
|
|
+ msk = (struct sockaddr_in6 *) ifa->ifa_netmask;
|
|
|
+
|
|
|
+ char *p = (char*)inet_ntop(ifa->ifa_addr->sa_family, &sa->sin6_addr, ipaddr, INET6_ADDRSTRLEN);
|
|
|
+ if(p) r = 0;
|
|
|
+ if(r==0) break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ freeifaddrs(ifap);
|
|
|
+
|
|
|
+ return r;
|
|
|
+}
|
|
|
+static int nl_get_ip(ap_stat_t *stat)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+
|
|
|
+ stat->ipaddr[0] = 0;
|
|
|
+ r = get_ipaddr(WLAN_IFNAME, stat->ipaddr);
|
|
|
+
|
|
|
+ return r;
|
|
|
+}
|
|
|
+static int nl_get_signal(ap_stat_t *stat)
|
|
|
+{
|
|
|
+ int r;
|
|
|
+ nl_handle_t nl,*h;
|
|
|
+
|
|
|
+ r = nl80211_init(&nl);
|
|
|
+ if(r) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ h = &nl;
|
|
|
+
|
|
|
+ stat->link = stat->signal = 0;
|
|
|
+ genlmsg_put(h->msg, 0, 0, h->nl_id, 0, NLM_F_DUMP, NL80211_CMD_GET_STATION, 0); // Setup which command to run.
|
|
|
+ nla_put_u32(h->msg, NL80211_ATTR_IFINDEX, h->ifindex); // Add message attribute, which interface to use.
|
|
|
+ nl_socket_modify_cb(h->sock, NL_CB_VALID, NL_CB_CUSTOM, callback_stat, stat);
|
|
|
+ r = nl_send_auto(h->sock, h->msg);
|
|
|
+ if(r>=0) {
|
|
|
+ nl_recvmsgs_default(h->sock);
|
|
|
+ r = 0;
|
|
|
+ }
|
|
|
+ nl80211_free(h);
|
|
|
+ return r;
|
|
|
+}
|
|
|
static int nl_scan(result_t *res)
|
|
|
{
|
|
|
int r;
|
|
|
@@ -595,9 +685,7 @@ int wifi_init(void)
|
|
|
|
|
|
*wm = __globalDeviceManage._globalDevInfo.wifi;
|
|
|
#ifdef USE_WIFI
|
|
|
- {
|
|
|
- r = wifi_set(wm);
|
|
|
- }
|
|
|
+ r = wifi_set(wm);
|
|
|
#endif
|
|
|
|
|
|
return r;
|
|
|
@@ -625,7 +713,7 @@ int wifi_scan(ap_list_t *list)
|
|
|
|
|
|
int wifi_set(WIFIManager *wm)
|
|
|
{
|
|
|
- int r;
|
|
|
+ int r=-1;
|
|
|
ap_info_t info;
|
|
|
wifi_handle_t *h=&wfHandle;
|
|
|
|
|
|
@@ -634,15 +722,8 @@ int wifi_set(WIFIManager *wm)
|
|
|
if(wm->mode) {
|
|
|
memset(&info, 0, sizeof(info));
|
|
|
|
|
|
- #if 0
|
|
|
strcpy(info.ssid, wm->WIFI_name);
|
|
|
strcpy(info.password, wm->WIFI_code);
|
|
|
- //strcpy(info.ipaddr, wm->WIFI_ip);
|
|
|
- #else
|
|
|
- strcpy(info.ssid, "ChinaNet-Gowone");
|
|
|
- strcpy(info.password, "gowone520");
|
|
|
- //strcpy(info.ipaddr, "");
|
|
|
- #endif
|
|
|
r = sta_start(&info, 1);
|
|
|
}
|
|
|
else {
|
|
|
@@ -661,13 +742,28 @@ int wifi_sta_info(ap_info_t *info)
|
|
|
wifi_handle_t *h=&wfHandle;
|
|
|
|
|
|
#ifdef USE_WIFI
|
|
|
-
|
|
|
+
|
|
|
#endif
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
+int wifi_sta_stat(ap_stat_t *stat)
|
|
|
+{
|
|
|
+ int r=-1;
|
|
|
+ wifi_handle_t *h=&wfHandle;
|
|
|
+
|
|
|
+#ifdef USE_WIFI
|
|
|
+ r = nl_get_signal(stat);
|
|
|
+ r *= nl_get_ip(stat);
|
|
|
+#endif
|
|
|
+
|
|
|
+ return r;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
int wifi_ap_info(ap_info_t *info)
|
|
|
{
|
|
|
int r=-1;
|