概述
有这几个功能:
a. 自动扫描WIFI热点
b. 点击某个WIFI热点后会去连接它, 必要时让你输入密码
c. 曾经连接过的WIFI热点会保留它的密码等信息, 以后会自动连接
测试环境是在Linux环境下实验的,代码有点多,下面看不清楚的点击这里下载源码!
wpa_cli.c文件:
/*
* WPA Supplicant - command line interface for wpa_supplicant daemon
* Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
*
* This software may be distributed under the terms of the BSD license.
* See README for more details.
*/
#include "includes.h"
#ifdef CONFIG_CTRL_IFACE
#ifdef CONFIG_CTRL_IFACE_UNIX
#include <dirent.h>
#endif /* CONFIG_CTRL_IFACE_UNIX */
#include "common/wpa_ctrl.h"
#include "utils/common.h"
#include "utils/eloop.h"
#include "utils/edit.h"
#include "utils/list.h"
#include "common/version.h"
#include "common/ieee802_11_defs.h"
#ifdef ANDROID
#include <cutils/properties.h>
#endif /* ANDROID */
#include <unistd.h>
#include <termios.h>
static const char *wpa_cli_version =
"wpa_cli v" VERSION_STR "n"
"Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi> and contributors";
static const char *wpa_cli_license =
"This software may be distributed under the terms of the BSD license.n"
"See README for more details.n";
static const char *wpa_cli_full_license =
"This software may be distributed under the terms of the BSD license.n"
"n"
"Redistribution and use in source and binary forms, with or withoutn"
"modification, are permitted provided that the following conditions aren"
"met:n"
"n"
"1. Redistributions of source code must retain the above copyrightn"
" notice, this list of conditions and the following disclaimer.n"
"n"
"2. Redistributions in binary form must reproduce the above copyrightn"
" notice, this list of conditions and the following disclaimer in then"
" documentation and/or other materials provided with the distribution.n"
"n"
"3. Neither the name(s) of the above-listed copyright holder(s) nor then"
" names of its contributors may be used to endorse or promote productsn"
" derived from this software without specific prior written permission.n"
"n"
"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORSn"
""AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTn"
"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORn"
"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTn"
"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,n"
"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTn"
"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,n"
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYn"
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORTn"
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEn"
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.n"
"n";
static struct wpa_ctrl *ctrl_conn;
static struct wpa_ctrl *mon_conn;
static int wpa_cli_quit = 0;
static int wpa_cli_attached = 0;
static int wpa_cli_connected = 0;
static int wpa_cli_last_id = 0;
#ifndef CONFIG_CTRL_IFACE_DIR
#define CONFIG_CTRL_IFACE_DIR "/var/run/wpa_supplicant"
#endif /* CONFIG_CTRL_IFACE_DIR */
static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
static char *ctrl_ifname = NULL;
static const char *pid_file = NULL;
static const char *action_file = NULL;
static int ping_interval = 5;
static int interactive = 0;
struct cli_txt_entry {
struct dl_list list;
char *txt;
};
static DEFINE_DL_LIST(bsses); /* struct cli_txt_entry */
static DEFINE_DL_LIST(p2p_peers); /* struct cli_txt_entry */
static DEFINE_DL_LIST(p2p_groups); /* struct cli_txt_entry */
static void print_help(const char *cmd);
static void wpa_cli_mon_receive(int sock, void *eloop_ctx, void *sock_ctx);
static void wpa_cli_close_connection(void);
static char * wpa_cli_get_default_ifname(void);
static char ** wpa_list_cmd_list(void);
static void usage(void)
{
printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
"[-a<action file>] \n"
" [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
"[command..]n"
" -h = help (show this usage text)n"
" -v = shown version informationn"
" -a = run in daemon mode executing the action file based on "
"events fromn"
" wpa_supplicantn"
" -B = run a daemon in the backgroundn"
" default path: " CONFIG_CTRL_IFACE_DIR "n"
" default interface: first interface found in socket pathn");
print_help(NULL);
}
static void cli_txt_list_free(struct cli_txt_entry *e)
{
dl_list_del(&e->list);
os_free(e->txt);
os_free(e);
}
static void cli_txt_list_flush(struct dl_list *list)
{
struct cli_txt_entry *e;
while ((e = dl_list_first(list, struct cli_txt_entry, list)))
cli_txt_list_free(e);
}
static struct cli_txt_entry * cli_txt_list_get(struct dl_list *txt_list,
const char *txt)
{
struct cli_txt_entry *e;
dl_list_for_each(e, txt_list, struct cli_txt_entry, list) {
if (os_strcmp(e->txt, txt) == 0)
return e;
}
return NULL;
}
static void cli_txt_list_del(struct dl_list *txt_list, const char *txt)
{
struct cli_txt_entry *e;
e = cli_txt_list_get(txt_list, txt);
if (e)
cli_txt_list_free(e);
}
static void cli_txt_list_del_addr(struct dl_list *txt_list, const char *txt)
{
u8 addr[ETH_ALEN];
char buf[18];
if (hwaddr_aton(txt, addr) < 0)
return;
os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(addr));
cli_txt_list_del(txt_list, buf);
}
#ifdef CONFIG_P2P
static void cli_txt_list_del_word(struct dl_list *txt_list, const char *txt)
{
const char *end;
char *buf;
end = os_strchr(txt, ' ');
if (end == NULL)
end = txt + os_strlen(txt);
buf = os_malloc(end - txt + 1);
if (buf == NULL)
return;
os_memcpy(buf, txt, end - txt);
buf[end - txt] = '