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

时间:2017-09-27 15:29:23

标签: android wifi netlink

我正在尝试在Android上编写一个简单的原生应用程序,通过Netlink MLME和SCAN事件捕获所有与Wifi相关的事件(特别是我需要Wifi扫描,关联,解除关联,身份验证,解除身份验证,漫游等事件)。 / p>

我开发了一个原生应用,并使用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);
}

0 个答案:

没有答案