17
17
#ifdef CONFIG_CTRL_IFACE
19
#ifndef CONFIG_CTRL_IFACE_UDP
19
#ifdef CONFIG_CTRL_IFACE_UNIX
20
20
#include <dirent.h>
21
#endif /* CONFIG_CTRL_IFACE_UDP */
21
#endif /* CONFIG_CTRL_IFACE_UNIX */
22
22
#ifdef CONFIG_READLINE
23
23
#include <readline/readline.h>
24
24
#include <readline/history.h>
164
164
static struct wpa_ctrl * wpa_cli_open_connection(const char *ifname)
166
#ifdef CONFIG_CTRL_IFACE_UDP
167
ctrl_conn = wpa_ctrl_open("");
166
#if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
167
ctrl_conn = wpa_ctrl_open(ifname);
168
168
return ctrl_conn;
169
#else /* CONFIG_CTRL_IFACE_UDP */
169
#else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
395
395
static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
399
401
printf("Invalid AP_SCAN command: needs one argument (ap_scan "
403
snprintf(cmd, sizeof(cmd), "AP_SCAN %s", argv[0]);
405
res = snprintf(cmd, sizeof(cmd), "AP_SCAN %s", argv[0]);
406
if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
407
printf("Too long AP_SCAN command.\n");
404
410
return wpa_ctrl_command(ctrl, cmd);
429
435
static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
433
441
printf("Invalid LEVEL command: needs one argument (debug "
437
snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
445
res = snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
446
if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
447
printf("Too long LEVEL command.\n");
438
450
return wpa_ctrl_command(ctrl, cmd);
442
454
static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
444
456
char cmd[256], *pos, *end;
448
460
printf("Invalid IDENTITY command: needs two arguments "
453
465
end = cmd + sizeof(cmd);
455
pos += snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
467
ret = snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
456
468
argv[0], argv[1]);
457
for (i = 2; i < argc; i++)
458
pos += snprintf(pos, end - pos, " %s", argv[i]);
469
if (ret < 0 || ret >= end - pos) {
470
printf("Too long IDENTITY command.\n");
474
for (i = 2; i < argc; i++) {
475
ret = snprintf(pos, end - pos, " %s", argv[i]);
476
if (ret < 0 || ret >= end - pos) {
477
printf("Too long IDENTITY command.\n");
460
483
return wpa_ctrl_command(ctrl, cmd);
464
487
static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
466
489
char cmd[256], *pos, *end;
470
493
printf("Invalid PASSWORD command: needs two arguments "
475
498
end = cmd + sizeof(cmd);
477
pos += snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
500
ret = snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
478
501
argv[0], argv[1]);
479
for (i = 2; i < argc; i++)
480
pos += snprintf(pos, end - pos, " %s", argv[i]);
502
if (ret < 0 || ret >= end - pos) {
503
printf("Too long PASSWORD command.\n");
507
for (i = 2; i < argc; i++) {
508
ret = snprintf(pos, end - pos, " %s", argv[i]);
509
if (ret < 0 || ret >= end - pos) {
510
printf("Too long PASSWORD command.\n");
482
516
return wpa_ctrl_command(ctrl, cmd);
498
532
end = cmd + sizeof(cmd);
500
pos += snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
534
ret = snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
501
535
argv[0], argv[1]);
502
for (i = 2; i < argc; i++)
503
pos += snprintf(pos, end - pos, " %s", argv[i]);
536
if (ret < 0 || ret >= end - pos) {
537
printf("Too long NEW_PASSWORD command.\n");
541
for (i = 2; i < argc; i++) {
542
ret = snprintf(pos, end - pos, " %s", argv[i]);
543
if (ret < 0 || ret >= end - pos) {
544
printf("Too long NEW_PASSWORD command.\n");
505
550
return wpa_ctrl_command(ctrl, cmd);
509
554
static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
511
556
char cmd[256], *pos, *end;
515
560
printf("Invalid PIN command: needs two arguments "
520
565
end = cmd + sizeof(cmd);
522
pos += snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
524
for (i = 2; i < argc; i++)
525
pos += snprintf(pos, end - pos, " %s", argv[i]);
567
ret = snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
569
if (ret < 0 || ret >= end - pos) {
570
printf("Too long PIN command.\n");
574
for (i = 2; i < argc; i++) {
575
ret = snprintf(pos, end - pos, " %s", argv[i]);
576
if (ret < 0 || ret >= end - pos) {
577
printf("Too long PIN command.\n");
527
582
return wpa_ctrl_command(ctrl, cmd);
531
586
static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
533
588
char cmd[256], *pos, *end;
537
592
printf("Invalid OTP command: needs two arguments (network "
542
597
end = cmd + sizeof(cmd);
544
pos += snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
599
ret = snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
545
600
argv[0], argv[1]);
546
for (i = 2; i < argc; i++)
547
pos += snprintf(pos, end - pos, " %s", argv[i]);
601
if (ret < 0 || ret >= end - pos) {
602
printf("Too long OTP command.\n");
606
for (i = 2; i < argc; i++) {
607
ret = snprintf(pos, end - pos, " %s", argv[i]);
608
if (ret < 0 || ret >= end - pos) {
609
printf("Too long OTP command.\n");
549
615
return wpa_ctrl_command(ctrl, cmd);
565
631
end = cmd + sizeof(cmd);
567
pos += snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
633
ret = snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
568
634
argv[0], argv[1]);
569
for (i = 2; i < argc; i++)
570
pos += snprintf(pos, end - pos, " %s", argv[i]);
635
if (ret < 0 || ret >= end - pos) {
636
printf("Too long PASSPHRASE command.\n");
640
for (i = 2; i < argc; i++) {
641
ret = snprintf(pos, end - pos, " %s", argv[i]);
642
if (ret < 0 || ret >= end - pos) {
643
printf("Too long PASSPHRASE command.\n");
572
649
return wpa_ctrl_command(ctrl, cmd);
576
653
static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
578
655
char cmd[256], *pos, *end;
582
659
printf("Invalid BSSID command: needs two arguments (network "
587
664
end = cmd + sizeof(cmd);
589
pos += snprintf(pos, end - pos, "BSSID");
590
for (i = 0; i < argc; i++)
591
pos += snprintf(pos, end - pos, " %s", argv[i]);
666
ret = snprintf(pos, end - pos, "BSSID");
667
if (ret < 0 || ret >= end - pos) {
668
printf("Too long BSSID command.\n");
672
for (i = 0; i < argc; i++) {
673
ret = snprintf(pos, end - pos, " %s", argv[i]);
674
if (ret < 0 || ret >= end - pos) {
675
printf("Too long BSSID command.\n");
593
681
return wpa_ctrl_command(ctrl, cmd);
615
703
snprintf(cmd, sizeof(cmd), "SELECT_NETWORK %s", argv[0]);
704
cmd[sizeof(cmd) - 1] = '\0';
617
706
return wpa_ctrl_command(ctrl, cmd);
632
721
snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %s", argv[0]);
722
cmd[sizeof(cmd) - 1] = '\0';
634
724
return wpa_ctrl_command(ctrl, cmd);
649
739
snprintf(cmd, sizeof(cmd), "DISABLE_NETWORK %s", argv[0]);
740
cmd[sizeof(cmd) - 1] = '\0';
651
742
return wpa_ctrl_command(ctrl, cmd);
673
764
snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %s", argv[0]);
765
cmd[sizeof(cmd) - 1] = '\0';
675
767
return wpa_ctrl_command(ctrl, cmd);
787
printf("Invalid GET_CAPABILITY command: needs one argument\n");
791
snprintf(cmd, sizeof(cmd), "GET_CAPABILITY %s", argv[0]);
878
if (argc < 1 || argc > 2) {
879
printf("Invalid GET_CAPABILITY command: need either one or "
884
if ((argc == 2) && strcmp(argv[1], "strict") != 0) {
885
printf("Invalid GET_CAPABILITY command: second argument, "
886
"if any, must be 'strict'\n");
890
snprintf(cmd, sizeof(cmd), "GET_CAPABILITY %s%s", argv[0],
891
(argc == 2) ? " strict" : "");
892
cmd[sizeof(cmd) - 1] = '\0';
793
894
return wpa_ctrl_command(ctrl, cmd);
797
static void wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
898
static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
799
#ifndef CONFIG_CTRL_IFACE_UDP
803
dir = opendir(ctrl_iface_dir);
805
printf("Control interface directory '%s' could not be "
806
"openned.\n", ctrl_iface_dir);
810
900
printf("Available interfaces:\n");
811
while ((dent = readdir(dir))) {
812
if (strcmp(dent->d_name, ".") == 0 ||
813
strcmp(dent->d_name, "..") == 0)
815
printf("%s\n", dent->d_name);
818
#endif /* CONFIG_CTRL_IFACE_UDP */
901
return wpa_ctrl_command(ctrl, "INTERFACES");
882
965
argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
883
966
argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
884
967
argc > 5 ? argv[5] : "");
968
cmd[sizeof(cmd) - 1] = '\0';
885
969
return wpa_ctrl_command(ctrl, cmd);
900
984
snprintf(cmd, sizeof(cmd), "INTERFACE_REMOVE %s", argv[0]);
985
cmd[sizeof(cmd) - 1] = '\0';
901
986
return wpa_ctrl_command(ctrl, cmd);
1089
1175
#endif /* CONFIG_ANSI_C_EXTRA */
1178
static void wpa_cli_reconnect(void)
1180
wpa_cli_close_connection();
1181
ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
1183
printf("Connection to wpa_supplicant re-established\n");
1184
if (wpa_ctrl_attach(ctrl_conn) == 0) {
1185
wpa_cli_attached = 1;
1187
printf("Warning: Failed to attach to "
1188
"wpa_supplicant.\n");
1092
1194
static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1093
1195
int action_monitor)
1096
if (ctrl_conn == NULL)
1198
if (ctrl_conn == NULL) {
1199
wpa_cli_reconnect();
1098
while (wpa_ctrl_pending(ctrl)) {
1202
while (wpa_ctrl_pending(ctrl) > 0) {
1100
1204
size_t len = sizeof(buf) - 1;
1101
1205
if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1171
1281
hfile = malloc(hfile_len);
1173
1283
snprintf(hfile, hfile_len, "%s/%s", home, fname);
1284
hfile[hfile_len - 1] = '\0';
1174
1285
read_history(hfile);
1175
1286
stifle_history(100);
1337
1448
"reconnect\n");
1338
1449
wpa_cli_close_connection();
1341
ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
1343
printf("Connection to wpa_supplicant "
1344
"re-established\n");
1345
if (wpa_ctrl_attach(ctrl_conn) == 0) {
1346
wpa_cli_attached = 1;
1348
printf("Warning: Failed to attach to "
1349
"wpa_supplicant.\n");
1452
wpa_cli_reconnect();
1354
1454
wpa_cli_recv_pending(ctrl_conn, 1, 0);
1357
1457
#endif /* CONFIG_NATIVE_WINDOWS */
1460
static char * wpa_cli_get_default_ifname(void)
1462
char *ifname = NULL;
1464
#ifdef CONFIG_CTRL_IFACE_UNIX
1465
struct dirent *dent;
1466
DIR *dir = opendir(ctrl_iface_dir);
1469
while ((dent = readdir(dir))) {
1470
#ifdef _DIRENT_HAVE_D_TYPE
1472
* Skip the file if it is not a socket. Also accept
1473
* DT_UNKNOWN (0) in case the C library or underlying
1474
* file system does not support d_type.
1476
if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
1478
#endif /* _DIRENT_HAVE_D_TYPE */
1479
if (strcmp(dent->d_name, ".") == 0 ||
1480
strcmp(dent->d_name, "..") == 0)
1482
printf("Selected interface '%s'\n", dent->d_name);
1483
ifname = strdup(dent->d_name);
1487
#endif /* CONFIG_CTRL_IFACE_UNIX */
1489
#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
1490
char buf[2048], *pos;
1492
struct wpa_ctrl *ctrl;
1495
ctrl = wpa_ctrl_open(NULL);
1499
len = sizeof(buf) - 1;
1500
ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
1503
pos = strchr(buf, '\n');
1506
ifname = strdup(buf);
1508
wpa_ctrl_close(ctrl);
1509
#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1360
1515
int main(int argc, char *argv[])
1362
1517
int interactive;
1409
1564
printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license);
1567
#ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
1568
ctrl_conn = wpa_ctrl_open(NULL);
1569
#else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1412
1570
ctrl_conn = wpa_ctrl_open(global);
1571
#endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1413
1572
if (ctrl_conn == NULL) {
1414
1573
perror("Failed to connect to wpa_supplicant - "
1415
1574
"wpa_ctrl_open");
1420
1579
for (; !global;) {
1421
#ifndef CONFIG_CTRL_IFACE_UDP
1422
if (ctrl_ifname == NULL) {
1423
struct dirent *dent;
1424
DIR *dir = opendir(ctrl_iface_dir);
1426
while ((dent = readdir(dir))) {
1427
#ifdef _DIRENT_HAVE_D_TYPE
1428
/* Skip the file if it is not a socket.
1429
* Also accept DT_UNKNOWN (0) in case
1430
* the C library or underlying file
1431
* system does not support d_type. */
1432
if (dent->d_type != DT_SOCK &&
1433
dent->d_type != DT_UNKNOWN)
1435
#endif /* _DIRENT_HAVE_D_TYPE */
1436
if (strcmp(dent->d_name, ".") == 0 ||
1437
strcmp(dent->d_name, "..") == 0)
1439
printf("Selected interface '%s'\n",
1441
ctrl_ifname = strdup(dent->d_name);
1447
#endif /* CONFIG_CTRL_IFACE_UDP */
1580
if (ctrl_ifname == NULL)
1581
ctrl_ifname = wpa_cli_get_default_ifname();
1448
1582
ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
1449
1583
if (ctrl_conn) {
1450
1584
if (warning_displayed)