Skip to content

Commit

Permalink
wapi: Add pscan cmd to trigger a passive scan.
Browse files Browse the repository at this point in the history
Added scanning in passive mode without affecting the scan command.

Signed-off-by: xuchaojie <[email protected]>
  • Loading branch information
xuchaojie authored and xiaoxiang781216 committed Jan 25, 2024
1 parent a16fb23 commit ca52936
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
25 changes: 25 additions & 0 deletions include/wireless/wapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,31 @@ int wapi_scan_channel_init(int sock, FAR const char *ifname,
FAR const char *essid,
uint8_t *channels, int num_channels);

/****************************************************************************
* Name: wapi_escan_init
*
* Description:
* Starts a extended scan on the given interface, you can specify the scan
* type. Root privileges are required to start a scan.
*
****************************************************************************/

int wapi_escan_init(int sock, FAR const char *ifname,
uint8_t scan_type, FAR const char *essid);

/****************************************************************************
* Name: wapi_escan_channel_init
*
* Description:
* Starts a scan on the given interface. Root privileges are required to
* start a scan with specified channels.
*
****************************************************************************/

int wapi_escan_channel_init(int sock, FAR const char *ifname,
uint8_t scan_type, FAR const char *essid,
uint8_t *channels, int num_channels);

/****************************************************************************
* Name: wapi_scan_stat
*
Expand Down
38 changes: 36 additions & 2 deletions wireless/wapi/src/wapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static int wapi_bitrate_cmd (int sock, int argc, FAR char **argv);
static int wapi_txpower_cmd (int sock, int argc, FAR char **argv);
static int wapi_scan_results_cmd (int sock, int argc, FAR char **argv);
static int wapi_scan_cmd (int sock, int argc, FAR char **argv);
static int wapi_pscan_cmd (int sock, int argc, FAR char **argv);
static int wapi_country_cmd (int sock, int argc, FAR char **argv);
static int wapi_sense_cmd (int sock, int argc, FAR char **argv);
#ifdef CONFIG_WIRELESS_WAPI_INITCONF
Expand All @@ -114,6 +115,7 @@ static const struct wapi_command_s g_wapi_commands[] =
{"help", 0, 0, NULL},
{"show", 1, 1, wapi_show_cmd},
{"scan", 1, 2, wapi_scan_cmd},
{"pscan", 1, 2, wapi_pscan_cmd},
{"scan_results", 1, 1, wapi_scan_results_cmd},
{"ip", 2, 2, wapi_ip_cmd},
{"mask", 2, 2, wapi_mask_cmd},
Expand Down Expand Up @@ -833,7 +835,8 @@ static int wapi_scan_results_cmd(int sock, int argc, FAR char **argv)
* Name: wapi_scan_cmd
*
* Description:
* Scans available APs in the range using given ifname interface.
* Use the given ifname interface and active mode to scan the APs
* available in the range.
*
* Returned Value:
* None
Expand All @@ -849,7 +852,37 @@ static int wapi_scan_cmd(int sock, int argc, FAR char **argv)

/* Start scan */

ret = wapi_scan_init(sock, argv[0], essid);
ret = wapi_escan_init(sock, argv[0], IW_SCAN_TYPE_ACTIVE, essid);
if (ret < 0)
{
return ret;
}

return wapi_scan_results_cmd(sock, 1, argv);
}

/****************************************************************************
* Name: wapi_pscan_cmd
*
* Description:
* Use the given ifname interface and passive mode to scan the APs
* available in the range.
*
* Returned Value:
* None
*
****************************************************************************/

static int wapi_pscan_cmd(int sock, int argc, FAR char **argv)
{
FAR const char *essid;
int ret;

essid = argc > 1 ? argv[1] : NULL;

/* Start scan */

ret = wapi_escan_init(sock, argv[0], IW_SCAN_TYPE_PASSIVE, essid);
if (ret < 0)
{
return ret;
Expand Down Expand Up @@ -1095,6 +1128,7 @@ static void wapi_showusage(FAR const char *progname, int exitcode)
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t%s show <ifname>\n", progname);
fprintf(stderr, "\t%s scan <ifname>\n", progname);
fprintf(stderr, "\t%s pscan <ifname>\n", progname);
fprintf(stderr, "\t%s scan_results <ifname>\n", progname);
fprintf(stderr, "\t%s ip <ifname> <IP address>\n", progname);
fprintf(stderr, "\t%s mask <ifname> <mask>\n", progname);
Expand Down
49 changes: 42 additions & 7 deletions wireless/wapi/src/wireless.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,23 @@ int wapi_set_txpower(int sock, FAR const char *ifname, int power,
int wapi_scan_channel_init(int sock, FAR const char *ifname,
FAR const char *essid,
uint8_t *channels, int num_channels)
{
return wapi_escan_channel_init(sock, ifname, IW_SCAN_TYPE_ACTIVE, essid,
channels, num_channels);
}

/****************************************************************************
* Name: wapi_escan_channel_init
*
* Description:
* Starts a scan on the given interface. Root privileges are required to
* start a scan with specified channels.
*
****************************************************************************/

int wapi_escan_channel_init(int sock, FAR const char *ifname,
uint8_t scan_type, FAR const char *essid,
uint8_t *channels, int num_channels)
{
struct iw_scan_req req;
struct iwreq wrq =
Expand All @@ -1193,16 +1210,13 @@ int wapi_scan_channel_init(int sock, FAR const char *ifname,
int ret;
int i;

memset(&req, 0, sizeof(req));

if (essid && (essid_len = strlen(essid)) > 0)
{
memset(&req, 0, sizeof(req));
req.essid_len = essid_len;
req.bssid.sa_family = ARPHRD_ETHER;
memset(req.bssid.sa_data, 0xff, IFHWADDRLEN);
req.essid_len = essid_len;
memcpy(req.essid, essid, essid_len);
wrq.u.data.pointer = (caddr_t)&req;
wrq.u.data.length = sizeof(req);
wrq.u.data.flags = IW_SCAN_THIS_ESSID;
wrq.u.data.flags = IW_SCAN_THIS_ESSID;
}

if (channels && num_channels > 0)
Expand All @@ -1214,6 +1228,12 @@ int wapi_scan_channel_init(int sock, FAR const char *ifname,
}
}

req.scan_type = scan_type;
req.bssid.sa_family = ARPHRD_ETHER;
memset(req.bssid.sa_data, 0xff, IFHWADDRLEN);
wrq.u.data.pointer = (caddr_t)&req;
wrq.u.data.length = sizeof(req);

strlcpy(wrq.ifr_name, ifname, IFNAMSIZ);
ret = ioctl(sock, SIOCSIWSCAN, (unsigned long)((uintptr_t)&wrq));
if (ret < 0)
Expand All @@ -1240,6 +1260,21 @@ int wapi_scan_init(int sock, FAR const char *ifname, FAR const char *essid)
return wapi_scan_channel_init(sock, ifname, essid, NULL, 0);
}

/****************************************************************************
* Name: wapi_escan_init
*
* Description:
* Starts a extended scan on the given interface, you can specify the scan
* type. Root privileges are required to start a scan.
*
****************************************************************************/

int wapi_escan_init(int sock, FAR const char *ifname,
uint8_t scan_type, FAR const char *essid)
{
return wapi_escan_channel_init(sock, ifname, scan_type, essid, NULL, 0);
}

/****************************************************************************
* Name: wapi_scan_stat
*
Expand Down

0 comments on commit ca52936

Please sign in to comment.