net: wifi: shell: adopt getopt_state for safer optarg access
By using `getopt_state` to access `optarg`, offering a better alternative to direct global `optarg` access. This approach mitigates the risks associated with concurrent access to the global variable. Signed-off-by: Pisit Sawangvonganan <pisit@ndrsolution.com>
This commit is contained in:
parent
a6865c4191
commit
e145eb9201
1 changed files with 20 additions and 14 deletions
|
|
@ -660,19 +660,19 @@ static int wifi_scan_args_to_params(const struct shell *sh,
|
||||||
state = getopt_state_get();
|
state = getopt_state_get();
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 't':
|
case 't':
|
||||||
if (!strncasecmp(optarg, "passive", 7)) {
|
if (!strncasecmp(state->optarg, "passive", 7)) {
|
||||||
params->scan_type = WIFI_SCAN_TYPE_PASSIVE;
|
params->scan_type = WIFI_SCAN_TYPE_PASSIVE;
|
||||||
} else if (!strncasecmp(optarg, "active", 6)) {
|
} else if (!strncasecmp(state->optarg, "active", 6)) {
|
||||||
params->scan_type = WIFI_SCAN_TYPE_ACTIVE;
|
params->scan_type = WIFI_SCAN_TYPE_ACTIVE;
|
||||||
} else {
|
} else {
|
||||||
PR_ERROR("Invalid scan type %s\n", optarg);
|
PR_ERROR("Invalid scan type %s\n", state->optarg);
|
||||||
return -ENOEXEC;
|
return -ENOEXEC;
|
||||||
}
|
}
|
||||||
|
|
||||||
opt_num++;
|
opt_num++;
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
if (wifi_utils_parse_scan_bands(optarg, ¶ms->bands)) {
|
if (wifi_utils_parse_scan_bands(state->optarg, ¶ms->bands)) {
|
||||||
PR_ERROR("Invalid band value(s)\n");
|
PR_ERROR("Invalid band value(s)\n");
|
||||||
return -ENOEXEC;
|
return -ENOEXEC;
|
||||||
}
|
}
|
||||||
|
|
@ -680,7 +680,7 @@ static int wifi_scan_args_to_params(const struct shell *sh,
|
||||||
opt_num++;
|
opt_num++;
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
val = atoi(optarg);
|
val = atoi(state->optarg);
|
||||||
|
|
||||||
if ((val < 5) || (val > 1000)) {
|
if ((val < 5) || (val > 1000)) {
|
||||||
PR_ERROR("Invalid dwell_time_active val\n");
|
PR_ERROR("Invalid dwell_time_active val\n");
|
||||||
|
|
@ -691,7 +691,7 @@ static int wifi_scan_args_to_params(const struct shell *sh,
|
||||||
opt_num++;
|
opt_num++;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
val = atoi(optarg);
|
val = atoi(state->optarg);
|
||||||
|
|
||||||
if ((val < 10) || (val > 1000)) {
|
if ((val < 10) || (val > 1000)) {
|
||||||
PR_ERROR("Invalid dwell_time_passive val\n");
|
PR_ERROR("Invalid dwell_time_passive val\n");
|
||||||
|
|
@ -702,7 +702,7 @@ static int wifi_scan_args_to_params(const struct shell *sh,
|
||||||
opt_num++;
|
opt_num++;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
if (wifi_utils_parse_scan_ssids(optarg,
|
if (wifi_utils_parse_scan_ssids(state->optarg,
|
||||||
params->ssids,
|
params->ssids,
|
||||||
ARRAY_SIZE(params->ssids))) {
|
ARRAY_SIZE(params->ssids))) {
|
||||||
PR_ERROR("Invalid SSID(s)\n");
|
PR_ERROR("Invalid SSID(s)\n");
|
||||||
|
|
@ -712,7 +712,7 @@ static int wifi_scan_args_to_params(const struct shell *sh,
|
||||||
opt_num++;
|
opt_num++;
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
val = atoi(optarg);
|
val = atoi(state->optarg);
|
||||||
|
|
||||||
if ((val < 0) || (val > 65535)) {
|
if ((val < 0) || (val > 65535)) {
|
||||||
PR_ERROR("Invalid max_bss val\n");
|
PR_ERROR("Invalid max_bss val\n");
|
||||||
|
|
@ -723,7 +723,7 @@ static int wifi_scan_args_to_params(const struct shell *sh,
|
||||||
opt_num++;
|
opt_num++;
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
if (wifi_utils_parse_scan_chan(optarg,
|
if (wifi_utils_parse_scan_chan(state->optarg,
|
||||||
params->band_chan,
|
params->band_chan,
|
||||||
ARRAY_SIZE(params->band_chan))) {
|
ARRAY_SIZE(params->band_chan))) {
|
||||||
PR_ERROR("Invalid band or channel value(s)\n");
|
PR_ERROR("Invalid band or channel value(s)\n");
|
||||||
|
|
@ -1484,6 +1484,7 @@ void parse_mode_args_to_params(const struct shell *sh, int argc,
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
struct getopt_state *state;
|
||||||
|
|
||||||
static const struct option long_options[] = {{"if-index", optional_argument, 0, 'i'},
|
static const struct option long_options[] = {{"if-index", optional_argument, 0, 'i'},
|
||||||
{"sta", no_argument, 0, 's'},
|
{"sta", no_argument, 0, 's'},
|
||||||
|
|
@ -1495,6 +1496,7 @@ void parse_mode_args_to_params(const struct shell *sh, int argc,
|
||||||
{0, 0, 0, 0}};
|
{0, 0, 0, 0}};
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "i:smtpakgh", long_options, &option_index)) != -1) {
|
while ((opt = getopt_long(argc, argv, "i:smtpakgh", long_options, &option_index)) != -1) {
|
||||||
|
state = getopt_state_get();
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 's':
|
case 's':
|
||||||
mode->mode |= WIFI_STA_MODE;
|
mode->mode |= WIFI_STA_MODE;
|
||||||
|
|
@ -1512,7 +1514,7 @@ void parse_mode_args_to_params(const struct shell *sh, int argc,
|
||||||
mode->oper = WIFI_MGMT_GET;
|
mode->oper = WIFI_MGMT_GET;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
mode->if_index = (uint8_t)atoi(optarg);
|
mode->if_index = (uint8_t)atoi(state->optarg);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
shell_help(sh);
|
shell_help(sh);
|
||||||
|
|
@ -1583,6 +1585,7 @@ void parse_channel_args_to_params(const struct shell *sh, int argc,
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
struct getopt_state *state;
|
||||||
|
|
||||||
static const struct option long_options[] = {{"if-index", optional_argument, 0, 'i'},
|
static const struct option long_options[] = {{"if-index", optional_argument, 0, 'i'},
|
||||||
{"channel", required_argument, 0, 'c'},
|
{"channel", required_argument, 0, 'c'},
|
||||||
|
|
@ -1591,12 +1594,13 @@ void parse_channel_args_to_params(const struct shell *sh, int argc,
|
||||||
{0, 0, 0, 0}};
|
{0, 0, 0, 0}};
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "i:c:gh", long_options, &option_index)) != -1) {
|
while ((opt = getopt_long(argc, argv, "i:c:gh", long_options, &option_index)) != -1) {
|
||||||
|
state = getopt_state_get();
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
channel->channel = (uint16_t)atoi(optarg);
|
channel->channel = (uint16_t)atoi(state->optarg);
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
channel->if_index = (uint8_t)atoi(optarg);
|
channel->if_index = (uint8_t)atoi(state->optarg);
|
||||||
break;
|
break;
|
||||||
case 'g':
|
case 'g':
|
||||||
channel->oper = WIFI_MGMT_GET;
|
channel->oper = WIFI_MGMT_GET;
|
||||||
|
|
@ -1678,6 +1682,7 @@ void parse_filter_args_to_params(const struct shell *sh, int argc,
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
int option_index = 0;
|
int option_index = 0;
|
||||||
|
struct getopt_state *state;
|
||||||
|
|
||||||
static const struct option long_options[] = {{"if-index", optional_argument, 0, 'i'},
|
static const struct option long_options[] = {{"if-index", optional_argument, 0, 'i'},
|
||||||
{"capture-len", optional_argument, 0, 'b'},
|
{"capture-len", optional_argument, 0, 'b'},
|
||||||
|
|
@ -1690,6 +1695,7 @@ void parse_filter_args_to_params(const struct shell *sh, int argc,
|
||||||
{0, 0, 0, 0}};
|
{0, 0, 0, 0}};
|
||||||
|
|
||||||
while ((opt = getopt_long(argc, argv, "i:b:amcdgh", long_options, &option_index)) != -1) {
|
while ((opt = getopt_long(argc, argv, "i:b:amcdgh", long_options, &option_index)) != -1) {
|
||||||
|
state = getopt_state_get();
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'a':
|
case 'a':
|
||||||
filter->filter |= WIFI_PACKET_FILTER_ALL;
|
filter->filter |= WIFI_PACKET_FILTER_ALL;
|
||||||
|
|
@ -1704,10 +1710,10 @@ void parse_filter_args_to_params(const struct shell *sh, int argc,
|
||||||
filter->filter |= WIFI_PACKET_FILTER_CTRL;
|
filter->filter |= WIFI_PACKET_FILTER_CTRL;
|
||||||
break;
|
break;
|
||||||
case 'i':
|
case 'i':
|
||||||
filter->if_index = (uint8_t)atoi(optarg);
|
filter->if_index = (uint8_t)atoi(state->optarg);
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
filter->buffer_size = (uint16_t)atoi(optarg);
|
filter->buffer_size = (uint16_t)atoi(state->optarg);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
shell_help(sh);
|
shell_help(sh);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue