gpt4 book ai didi

android - 如何使用 nl80211.h 在 Android 上捕获所有 Wifi SCAN/MLME 事件?

转载 作者:行者123 更新时间:2023-12-04 14:00:46 26 4
gpt4 key购买 nike

我正在尝试在 Android 上编写一个简单的 native 应用程序,以通过 Netlink MLME 和 SCAN 事件捕获所有与 Wifi 相关的事件(特别是我需要 Wifi Scan、Association、DisAssociation、Authentication、DeAuthentication、Roaming 等事件)。

我开发了一个 native 应用程序并使用 adb 将其推送到设备(/system/bin)并以 root 身份运行。但是,当我打开/关闭 wifi 时,我只会收到 CONNECT/DISCONNECT 事件。我没有收到任何其他事件,例如:SCAN_TRIGGER、SCAN_RESULTS、(DIS)ASSOCIATION、(DE)AUTHENTICATION、ROAMING 等...

我在下面分享了代码。你能帮我解决这个问题(如果有的话),以便它适用于所有 Wifi 事件吗?提前致谢!

#include <netlink/netlink.h>
#include <netlink/attr.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>
#include <net/if.h>
#include <signal.h>
#include <stdint.h>
#include <linux/nl80211.h>

#define print_err(...) fprintf(stderr, __VA_ARGS__)

static struct nl_sock *sk = NULL;

static int nlCallback(struct nl_msg* msg, void* arg)
{
printf("\n\t nlCallback Start\n");
struct nlmsghdr* ret_hdr = nlmsg_hdr(msg);
struct genlmsghdr *gnlh = nlmsg_data(ret_hdr);

printf("nlCallback: Event Commmand: %d\n", gnlh->cmd);

switch(gnlh->cmd) {
case NL80211_CMD_TRIGGER_SCAN :
printf("nlCallback: cmd: NL80211_CMD_TRIGGER_SCAN \n");
break;
case NL80211_CMD_SCAN_ABORTED :
printf("nlCallback: cmd: NL80211_CMD_SCAN_ABORTED \n");
break;
case NL80211_CMD_NEW_SCAN_RESULTS :
printf("nlCallback: cmd: NL80211_CMD_NEW_SCAN_RESULTS \n");
break;
case NL80211_CMD_CONNECT :
printf("nlCallback: cmd: NL80211_CMD_CONNECT \n");
break;
case NL80211_CMD_DISCONNECT :
printf("nlCallback: cmd: NL80211_CMD_DISCONNECT \n");
break;
case NL80211_CMD_NEW_STATION:
printf("nlCallback: cmd: NL80211_CMD_NEW_STATION \n");
break;
case NL80211_CMD_DEL_STATION:
printf("nlCallback: cmd: NL80211_CMD_DEL_STATION\n");
break;
case NL80211_CMD_AUTHENTICATE:
printf("nlCallback: cmd: NL80211_CMD_AUTHENTICATE\n");
break;
case NL80211_CMD_DEAUTHENTICATE:
printf("nlCallback: cmd: NL80211_CMD_DEAUTHENTICATE\n");
break;
case NL80211_CMD_ASSOCIATE:
printf("nlCallback: cmd: NL80211_CMD_ASSOCIATE\n");
break;
case NL80211_CMD_DISASSOCIATE:
printf("nlCallback: cmd: NL80211_CMD_DISASSOCIATE\n");
break;
case NL80211_CMD_ROAM:
printf("nlCallback: cmd: NL80211_CMD_ROAM\n");
break;
default:
printf("nlCallback: Default multicast event: %d\n", gnlh->cmd);
return NL_SKIP;
}

return 0;
}

static int cleanup_and_exit(int ret)
{
if (sk != NULL)
nl_socket_free(sk);
exit(ret);
}

static void signal_handler(int sig)
{
cleanup_and_exit(EXIT_SUCCESS);
}

int main()
{
printf("\n\t ****** main() Start *******\n");
int ret;
int sk_fd;
fd_set rfds;

signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);

sk = nl_socket_alloc();
if (sk == NULL) {
print_err(" main(): ERROR : Unable to allocate Netlink socket\n");
exit(EXIT_FAILURE);
}

ret = genl_connect(sk);
if (ret < 0) {
print_err(" main(): ERROR : no connect %d\n", ret);
cleanup_and_exit(EXIT_FAILURE);
}

int mc_grp1 = genl_ctrl_resolve_grp(sk, "nl80211", "mlme");// will return 5.
if (mc_grp1 < 0) {
print_err("main(): ERROR : MLME group not found : %d\n", mc_grp1);
cleanup_and_exit(EXIT_FAILURE);
}

int mc_grp2 = genl_ctrl_resolve_grp(sk, "nl80211", "scan");// will return 3.
if (mc_grp2 < 0) {
print_err("main(): ERROR : SCAN group not found : %d\n", mc_grp2);
cleanup_and_exit(EXIT_FAILURE);
}

printf("\n\t main() Subcribed group ids:: MLME: %d, SCAN: %d\n", mc_grp1, mc_grp2);
ret = nl_socket_add_memberships(sk, mc_grp1, mc_grp2, 0);
if (ret < 0) {
print_err("main(): ERROR : Unable to join multicast group %d\n", ret);
cleanup_and_exit(EXIT_FAILURE);
}

nl_socket_disable_seq_check(sk);
ret = nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, nlCallback, NULL);
if (ret < 0) {
print_err(" main(): ERROR : Unable to register callback %d\n", ret);
cleanup_and_exit(EXIT_FAILURE);
}

while (1) {
printf("\nmain(): While(1): Waiting for MLME/SCAN events \n");
ret = nl_recvmsgs_default(sk);
if (ret < 0) {
cleanup_and_exit(EXIT_FAILURE);
}
printf("main(): While(1): MLME/SCAN event received: %d \n", ret);
}

printf("\n\t ****** main() End *******\n");
cleanup_and_exit(EXIT_FAILURE);
}

最佳答案

根据 wifi 驱动程序的能力,它可能会向用户空间发送不同的 NL80211 消息集。我认为您可以使用“iw”命令交叉检查结果,例如:

iw event -f
见: https://wireless.wiki.kernel.org/en/users/documentation/iw
您可能需要在目标系统上构建 iw。

关于android - 如何使用 nl80211.h 在 Android 上捕获所有 Wifi SCAN/MLME 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46451801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com