2 * WPA Supplicant - command line interface for wpa_supplicant daemon
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
17 #ifdef CONFIG_CTRL_IFACE
19 #ifdef CONFIG_CTRL_IFACE_UNIX
21 #endif /* CONFIG_CTRL_IFACE_UNIX */
22 #ifdef CONFIG_READLINE
23 #include <readline/readline.h>
24 #include <readline/history.h>
25 #endif /* CONFIG_READLINE */
32 static const char *wpa_cli_version =
33 "wpa_cli v" VERSION_STR "\n"
34 "Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> and contributors";
37 static const char *wpa_cli_license =
38 "This program is free software. You can distribute it and/or modify it\n"
39 "under the terms of the GNU General Public License version 2.\n"
41 "Alternatively, this software may be distributed under the terms of the\n"
42 "BSD license. See README and COPYING for more details.\n";
44 static const char *wpa_cli_full_license =
45 "This program is free software; you can redistribute it and/or modify\n"
46 "it under the terms of the GNU General Public License version 2 as\n"
47 "published by the Free Software Foundation.\n"
49 "This program is distributed in the hope that it will be useful,\n"
50 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
51 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
52 "GNU General Public License for more details.\n"
54 "You should have received a copy of the GNU General Public License\n"
55 "along with this program; if not, write to the Free Software\n"
56 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
58 "Alternatively, this software may be distributed under the terms of the\n"
61 "Redistribution and use in source and binary forms, with or without\n"
62 "modification, are permitted provided that the following conditions are\n"
65 "1. Redistributions of source code must retain the above copyright\n"
66 " notice, this list of conditions and the following disclaimer.\n"
68 "2. Redistributions in binary form must reproduce the above copyright\n"
69 " notice, this list of conditions and the following disclaimer in the\n"
70 " documentation and/or other materials provided with the distribution.\n"
72 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
73 " names of its contributors may be used to endorse or promote products\n"
74 " derived from this software without specific prior written permission.\n"
76 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
77 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
78 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
79 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
80 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
81 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
82 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
83 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
84 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
85 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
86 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
89 static struct wpa_ctrl *ctrl_conn;
90 static int wpa_cli_quit = 0;
91 static int wpa_cli_attached = 0;
92 static int wpa_cli_connected = 0;
93 static int wpa_cli_last_id = 0;
94 static const char *ctrl_iface_dir = "/var/run/wpa_supplicant";
95 static char *ctrl_ifname = NULL;
96 static const char *pid_file = NULL;
97 static const char *action_file = NULL;
98 static int ping_interval = 5;
101 static void print_help();
104 static void usage(void)
106 printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
107 "[-a<action file>] \\\n"
108 " [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
110 " -h = help (show this usage text)\n"
111 " -v = shown version information\n"
112 " -a = run in daemon mode executing the action file based on "
115 " -B = run a daemon in the background\n"
116 " default path: /var/run/wpa_supplicant\n"
117 " default interface: first interface found in socket path\n");
122 static struct wpa_ctrl * wpa_cli_open_connection(const char *ifname)
124 #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE)
125 ctrl_conn = wpa_ctrl_open(ifname);
127 #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
134 flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2;
135 cfile = os_malloc(flen);
138 res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
139 if (res < 0 || res >= flen) {
144 ctrl_conn = wpa_ctrl_open(cfile);
147 #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */
151 static void wpa_cli_close_connection(void)
153 if (ctrl_conn == NULL)
156 if (wpa_cli_attached) {
157 wpa_ctrl_detach(ctrl_conn);
158 wpa_cli_attached = 0;
160 wpa_ctrl_close(ctrl_conn);
165 static void wpa_cli_msg_cb(char *msg, size_t len)
171 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
177 if (ctrl_conn == NULL) {
178 printf("Not connected to wpa_supplicant - command dropped.\n");
181 len = sizeof(buf) - 1;
182 ret = wpa_ctrl_request(ctrl, cmd, os_strlen(cmd), buf, &len,
185 printf("'%s' command timed out.\n", cmd);
187 } else if (ret < 0) {
188 printf("'%s' command failed.\n", cmd);
199 static int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
201 return _wpa_ctrl_command(ctrl, cmd, 1);
205 static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
207 int verbose = argc > 0 && os_strcmp(argv[0], "verbose") == 0;
208 return wpa_ctrl_command(ctrl, verbose ? "STATUS-VERBOSE" : "STATUS");
212 static int wpa_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
214 return wpa_ctrl_command(ctrl, "PING");
218 static int wpa_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
220 return wpa_ctrl_command(ctrl, "MIB");
224 static int wpa_cli_cmd_pmksa(struct wpa_ctrl *ctrl, int argc, char *argv[])
226 return wpa_ctrl_command(ctrl, "PMKSA");
230 static int wpa_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
237 static int wpa_cli_cmd_license(struct wpa_ctrl *ctrl, int argc, char *argv[])
239 printf("%s\n\n%s\n", wpa_cli_version, wpa_cli_full_license);
244 static int wpa_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
251 static void wpa_cli_show_variables(void)
253 printf("set variables:\n"
254 " EAPOL::heldPeriod (EAPOL state machine held period, "
256 " EAPOL::authPeriod (EAPOL state machine authentication "
257 "period, in seconds)\n"
258 " EAPOL::startPeriod (EAPOL state machine start period, in "
260 " EAPOL::maxStart (EAPOL state machine maximum start "
262 printf(" dot11RSNAConfigPMKLifetime (WPA/WPA2 PMK lifetime in "
264 " dot11RSNAConfigPMKReauthThreshold (WPA/WPA2 reauthentication"
265 " threshold\n\tpercentage)\n"
266 " dot11RSNAConfigSATimeout (WPA/WPA2 timeout for completing "
267 "security\n\tassociation in seconds)\n");
271 static int wpa_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
277 wpa_cli_show_variables();
282 printf("Invalid SET command: needs two arguments (variable "
283 "name and value)\n");
287 res = os_snprintf(cmd, sizeof(cmd), "SET %s %s", argv[0], argv[1]);
288 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
289 printf("Too long SET command.\n");
292 return wpa_ctrl_command(ctrl, cmd);
296 static int wpa_cli_cmd_logoff(struct wpa_ctrl *ctrl, int argc, char *argv[])
298 return wpa_ctrl_command(ctrl, "LOGOFF");
302 static int wpa_cli_cmd_logon(struct wpa_ctrl *ctrl, int argc, char *argv[])
304 return wpa_ctrl_command(ctrl, "LOGON");
308 static int wpa_cli_cmd_reassociate(struct wpa_ctrl *ctrl, int argc,
311 return wpa_ctrl_command(ctrl, "REASSOCIATE");
315 static int wpa_cli_cmd_preauthenticate(struct wpa_ctrl *ctrl, int argc,
322 printf("Invalid PREAUTH command: needs one argument "
327 res = os_snprintf(cmd, sizeof(cmd), "PREAUTH %s", argv[0]);
328 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
329 printf("Too long PREAUTH command.\n");
332 return wpa_ctrl_command(ctrl, cmd);
336 static int wpa_cli_cmd_ap_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
342 printf("Invalid AP_SCAN command: needs one argument (ap_scan "
346 res = os_snprintf(cmd, sizeof(cmd), "AP_SCAN %s", argv[0]);
347 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
348 printf("Too long AP_SCAN command.\n");
351 return wpa_ctrl_command(ctrl, cmd);
355 static int wpa_cli_cmd_stkstart(struct wpa_ctrl *ctrl, int argc,
362 printf("Invalid STKSTART command: needs one argument "
363 "(Peer STA MAC address)\n");
367 res = os_snprintf(cmd, sizeof(cmd), "STKSTART %s", argv[0]);
368 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
369 printf("Too long STKSTART command.\n");
372 return wpa_ctrl_command(ctrl, cmd);
376 static int wpa_cli_cmd_ft_ds(struct wpa_ctrl *ctrl, int argc, char *argv[])
382 printf("Invalid FT_DS command: needs one argument "
383 "(Target AP MAC address)\n");
387 res = os_snprintf(cmd, sizeof(cmd), "FT_DS %s", argv[0]);
388 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
389 printf("Too long FT_DS command.\n");
392 return wpa_ctrl_command(ctrl, cmd);
396 static int wpa_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc, char *argv[])
403 return wpa_ctrl_command(ctrl, "WPS_PBC");
407 res = os_snprintf(cmd, sizeof(cmd), "WPS_PBC %s", argv[0]);
408 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
409 printf("Too long WPS_PBC command.\n");
412 return wpa_ctrl_command(ctrl, cmd);
416 static int wpa_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
422 printf("Invalid WPS_PIN command: need one or two arguments:\n"
423 "- BSSID: use 'any' to select any\n"
424 "- PIN: optional, used only with devices that have no "
430 /* Use dynamically generated PIN (returned as reply) */
431 res = os_snprintf(cmd, sizeof(cmd), "WPS_PIN %s", argv[0]);
432 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
433 printf("Too long WPS_PIN command.\n");
436 return wpa_ctrl_command(ctrl, cmd);
439 /* Use hardcoded PIN from a label */
440 res = os_snprintf(cmd, sizeof(cmd), "WPS_PIN %s %s", argv[0], argv[1]);
441 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
442 printf("Too long WPS_PIN command.\n");
445 return wpa_ctrl_command(ctrl, cmd);
449 #ifdef CONFIG_WPS_OOB
450 static int wpa_cli_cmd_wps_oob(struct wpa_ctrl *ctrl, int argc, char *argv[])
455 if (argc != 3 && argc != 4) {
456 printf("Invalid WPS_OOB command: need three or four "
458 "- DEV_TYPE: use 'ufd' or 'nfc'\n"
459 "- PATH: path of OOB device like '/mnt'\n"
460 "- METHOD: OOB method 'pin-e' or 'pin-r', "
462 "- DEV_NAME: (only for NFC) device name like "
468 res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s",
469 argv[0], argv[1], argv[2]);
471 res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s %s",
472 argv[0], argv[1], argv[2], argv[3]);
473 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
474 printf("Too long WPS_OOB command.\n");
477 return wpa_ctrl_command(ctrl, cmd);
479 #endif /* CONFIG_WPS_OOB */
482 static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
488 res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
490 else if (argc == 6) {
491 char ssid_hex[2 * 32 + 1];
492 char key_hex[2 * 64 + 1];
496 for (i = 0; i < 32; i++) {
497 if (argv[2][i] == '\0')
499 os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
503 for (i = 0; i < 64; i++) {
504 if (argv[5][i] == '\0')
506 os_snprintf(&key_hex[i * 2], 3, "%02x", argv[5][i]);
509 res = os_snprintf(cmd, sizeof(cmd),
510 "WPS_REG %s %s %s %s %s %s",
511 argv[0], argv[1], ssid_hex, argv[3], argv[4],
514 printf("Invalid WPS_REG command: need two arguments:\n"
515 "- BSSID: use 'any' to select any\n"
517 printf("Alternatively, six arguments can be used to "
518 "reconfigure the AP:\n"
519 "- BSSID: use 'any' to select any\n"
522 "- new auth (OPEN, WPAPSK, WPA2PSK)\n"
523 "- new encr (NONE, WEP, TKIP, CCMP)\n"
528 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
529 printf("Too long WPS_REG command.\n");
532 return wpa_ctrl_command(ctrl, cmd);
536 static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
542 printf("Invalid IBSS_RSN command: needs one argument "
543 "(Peer STA MAC address)\n");
547 res = os_snprintf(cmd, sizeof(cmd), "IBSS_RSN %s", argv[0]);
548 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
549 printf("Too long IBSS_RSN command.\n");
552 return wpa_ctrl_command(ctrl, cmd);
556 static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
562 printf("Invalid LEVEL command: needs one argument (debug "
566 res = os_snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
567 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
568 printf("Too long LEVEL command.\n");
571 return wpa_ctrl_command(ctrl, cmd);
575 static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
577 char cmd[256], *pos, *end;
581 printf("Invalid IDENTITY command: needs two arguments "
582 "(network id and identity)\n");
586 end = cmd + sizeof(cmd);
588 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
590 if (ret < 0 || ret >= end - pos) {
591 printf("Too long IDENTITY command.\n");
595 for (i = 2; i < argc; i++) {
596 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
597 if (ret < 0 || ret >= end - pos) {
598 printf("Too long IDENTITY command.\n");
604 return wpa_ctrl_command(ctrl, cmd);
608 static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
610 char cmd[256], *pos, *end;
614 printf("Invalid PASSWORD command: needs two arguments "
615 "(network id and password)\n");
619 end = cmd + sizeof(cmd);
621 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
623 if (ret < 0 || ret >= end - pos) {
624 printf("Too long PASSWORD command.\n");
628 for (i = 2; i < argc; i++) {
629 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
630 if (ret < 0 || ret >= end - pos) {
631 printf("Too long PASSWORD command.\n");
637 return wpa_ctrl_command(ctrl, cmd);
641 static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
644 char cmd[256], *pos, *end;
648 printf("Invalid NEW_PASSWORD command: needs two arguments "
649 "(network id and password)\n");
653 end = cmd + sizeof(cmd);
655 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
657 if (ret < 0 || ret >= end - pos) {
658 printf("Too long NEW_PASSWORD command.\n");
662 for (i = 2; i < argc; i++) {
663 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
664 if (ret < 0 || ret >= end - pos) {
665 printf("Too long NEW_PASSWORD command.\n");
671 return wpa_ctrl_command(ctrl, cmd);
675 static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
677 char cmd[256], *pos, *end;
681 printf("Invalid PIN command: needs two arguments "
682 "(network id and pin)\n");
686 end = cmd + sizeof(cmd);
688 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
690 if (ret < 0 || ret >= end - pos) {
691 printf("Too long PIN command.\n");
695 for (i = 2; i < argc; i++) {
696 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
697 if (ret < 0 || ret >= end - pos) {
698 printf("Too long PIN command.\n");
703 return wpa_ctrl_command(ctrl, cmd);
707 static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
709 char cmd[256], *pos, *end;
713 printf("Invalid OTP command: needs two arguments (network "
714 "id and password)\n");
718 end = cmd + sizeof(cmd);
720 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
722 if (ret < 0 || ret >= end - pos) {
723 printf("Too long OTP command.\n");
727 for (i = 2; i < argc; i++) {
728 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
729 if (ret < 0 || ret >= end - pos) {
730 printf("Too long OTP command.\n");
736 return wpa_ctrl_command(ctrl, cmd);
740 static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
743 char cmd[256], *pos, *end;
747 printf("Invalid PASSPHRASE command: needs two arguments "
748 "(network id and passphrase)\n");
752 end = cmd + sizeof(cmd);
754 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
756 if (ret < 0 || ret >= end - pos) {
757 printf("Too long PASSPHRASE command.\n");
761 for (i = 2; i < argc; i++) {
762 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
763 if (ret < 0 || ret >= end - pos) {
764 printf("Too long PASSPHRASE command.\n");
770 return wpa_ctrl_command(ctrl, cmd);
774 static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
776 char cmd[256], *pos, *end;
780 printf("Invalid BSSID command: needs two arguments (network "
785 end = cmd + sizeof(cmd);
787 ret = os_snprintf(pos, end - pos, "BSSID");
788 if (ret < 0 || ret >= end - pos) {
789 printf("Too long BSSID command.\n");
793 for (i = 0; i < argc; i++) {
794 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
795 if (ret < 0 || ret >= end - pos) {
796 printf("Too long BSSID command.\n");
802 return wpa_ctrl_command(ctrl, cmd);
806 static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
809 return wpa_ctrl_command(ctrl, "LIST_NETWORKS");
813 static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
820 printf("Invalid SELECT_NETWORK command: needs one argument "
825 res = os_snprintf(cmd, sizeof(cmd), "SELECT_NETWORK %s", argv[0]);
826 if (res < 0 || (size_t) res >= sizeof(cmd))
828 cmd[sizeof(cmd) - 1] = '\0';
830 return wpa_ctrl_command(ctrl, cmd);
834 static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
841 printf("Invalid ENABLE_NETWORK command: needs one argument "
846 res = os_snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %s", argv[0]);
847 if (res < 0 || (size_t) res >= sizeof(cmd))
849 cmd[sizeof(cmd) - 1] = '\0';
851 return wpa_ctrl_command(ctrl, cmd);
855 static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
862 printf("Invalid DISABLE_NETWORK command: needs one argument "
867 res = os_snprintf(cmd, sizeof(cmd), "DISABLE_NETWORK %s", argv[0]);
868 if (res < 0 || (size_t) res >= sizeof(cmd))
870 cmd[sizeof(cmd) - 1] = '\0';
872 return wpa_ctrl_command(ctrl, cmd);
876 static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
879 return wpa_ctrl_command(ctrl, "ADD_NETWORK");
883 static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
890 printf("Invalid REMOVE_NETWORK command: needs one argument "
895 res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %s", argv[0]);
896 if (res < 0 || (size_t) res >= sizeof(cmd))
898 cmd[sizeof(cmd) - 1] = '\0';
900 return wpa_ctrl_command(ctrl, cmd);
904 static void wpa_cli_show_network_variables(void)
906 printf("set_network variables:\n"
907 " ssid (network name, SSID)\n"
908 " psk (WPA passphrase or pre-shared key)\n"
909 " key_mgmt (key management protocol)\n"
910 " identity (EAP identity)\n"
911 " password (EAP password)\n"
914 "Note: Values are entered in the same format as the "
915 "configuration file is using,\n"
916 "i.e., strings values need to be inside double quotation "
918 "For example: set_network 1 ssid \"network name\"\n"
920 "Please see wpa_supplicant.conf documentation for full list "
921 "of\navailable variables.\n");
925 static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
932 wpa_cli_show_network_variables();
937 printf("Invalid SET_NETWORK command: needs three arguments\n"
938 "(network id, variable name, and value)\n");
942 res = os_snprintf(cmd, sizeof(cmd), "SET_NETWORK %s %s %s",
943 argv[0], argv[1], argv[2]);
944 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
945 printf("Too long SET_NETWORK command.\n");
948 return wpa_ctrl_command(ctrl, cmd);
952 static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
959 wpa_cli_show_network_variables();
964 printf("Invalid GET_NETWORK command: needs two arguments\n"
965 "(network id and variable name)\n");
969 res = os_snprintf(cmd, sizeof(cmd), "GET_NETWORK %s %s",
971 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
972 printf("Too long GET_NETWORK command.\n");
975 return wpa_ctrl_command(ctrl, cmd);
979 static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
982 return wpa_ctrl_command(ctrl, "DISCONNECT");
986 static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
989 return wpa_ctrl_command(ctrl, "RECONNECT");
993 static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
996 return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
1000 static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
1002 return wpa_ctrl_command(ctrl, "SCAN");
1006 static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
1009 return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
1013 static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
1019 printf("Invalid BSS command: need one argument (index or "
1024 res = os_snprintf(cmd, sizeof(cmd), "BSS %s", argv[0]);
1025 if (res < 0 || (size_t) res >= sizeof(cmd))
1027 cmd[sizeof(cmd) - 1] = '\0';
1029 return wpa_ctrl_command(ctrl, cmd);
1033 static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
1039 if (argc < 1 || argc > 2) {
1040 printf("Invalid GET_CAPABILITY command: need either one or "
1045 if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) {
1046 printf("Invalid GET_CAPABILITY command: second argument, "
1047 "if any, must be 'strict'\n");
1051 res = os_snprintf(cmd, sizeof(cmd), "GET_CAPABILITY %s%s", argv[0],
1052 (argc == 2) ? " strict" : "");
1053 if (res < 0 || (size_t) res >= sizeof(cmd))
1055 cmd[sizeof(cmd) - 1] = '\0';
1057 return wpa_ctrl_command(ctrl, cmd);
1061 static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
1063 printf("Available interfaces:\n");
1064 return wpa_ctrl_command(ctrl, "INTERFACES");
1068 static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
1071 wpa_cli_list_interfaces(ctrl);
1075 wpa_cli_close_connection();
1076 os_free(ctrl_ifname);
1077 ctrl_ifname = os_strdup(argv[0]);
1079 if (wpa_cli_open_connection(ctrl_ifname)) {
1080 printf("Connected to interface '%s.\n", ctrl_ifname);
1081 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1082 wpa_cli_attached = 1;
1084 printf("Warning: Failed to attach to "
1085 "wpa_supplicant.\n");
1088 printf("Could not connect to interface '%s' - re-trying\n",
1095 static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
1098 return wpa_ctrl_command(ctrl, "RECONFIGURE");
1102 static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
1105 return wpa_ctrl_command(ctrl, "TERMINATE");
1109 static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
1116 printf("Invalid INTERFACE_ADD command: needs at least one "
1117 "argument (interface name)\n"
1118 "All arguments: ifname confname driver ctrl_interface "
1119 "driver_param bridge_name\n");
1124 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
1125 * <driver_param>TAB<bridge_name>
1127 res = os_snprintf(cmd, sizeof(cmd),
1128 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
1130 argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
1131 argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
1132 argc > 5 ? argv[5] : "");
1133 if (res < 0 || (size_t) res >= sizeof(cmd))
1135 cmd[sizeof(cmd) - 1] = '\0';
1136 return wpa_ctrl_command(ctrl, cmd);
1140 static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
1147 printf("Invalid INTERFACE_REMOVE command: needs one argument "
1148 "(interface name)\n");
1152 res = os_snprintf(cmd, sizeof(cmd), "INTERFACE_REMOVE %s", argv[0]);
1153 if (res < 0 || (size_t) res >= sizeof(cmd))
1155 cmd[sizeof(cmd) - 1] = '\0';
1156 return wpa_ctrl_command(ctrl, cmd);
1160 static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
1163 return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
1168 static int wpa_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1172 printf("Invalid 'sta' command - exactly one argument, STA "
1173 "address, is required.\n");
1176 snprintf(buf, sizeof(buf), "STA %s", argv[0]);
1177 return wpa_ctrl_command(ctrl, buf);
1181 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
1182 char *addr, size_t addr_len)
1184 char buf[4096], *pos;
1188 if (ctrl_conn == NULL) {
1189 printf("Not connected to hostapd - command dropped.\n");
1192 len = sizeof(buf) - 1;
1193 ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
1196 printf("'%s' command timed out.\n", cmd);
1198 } else if (ret < 0) {
1199 printf("'%s' command failed.\n", cmd);
1204 if (memcmp(buf, "FAIL", 4) == 0)
1209 while (*pos != '\0' && *pos != '\n')
1212 os_strlcpy(addr, buf, addr_len);
1217 static int wpa_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
1219 char addr[32], cmd[64];
1221 if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
1224 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
1225 } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
1229 #endif /* CONFIG_AP */
1232 enum wpa_cli_cmd_flags {
1233 cli_cmd_flag_none = 0x00,
1234 cli_cmd_flag_sensitive = 0x01
1237 struct wpa_cli_cmd {
1239 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
1240 enum wpa_cli_cmd_flags flags;
1244 static struct wpa_cli_cmd wpa_cli_commands[] = {
1245 { "status", wpa_cli_cmd_status,
1247 "[verbose] = get current WPA/EAPOL/EAP status" },
1248 { "ping", wpa_cli_cmd_ping,
1250 "= pings wpa_supplicant" },
1251 { "mib", wpa_cli_cmd_mib,
1253 "= get MIB variables (dot1x, dot11)" },
1254 { "help", wpa_cli_cmd_help,
1256 "= show this usage help" },
1257 { "interface", wpa_cli_cmd_interface,
1259 "[ifname] = show interfaces/select interface" },
1260 { "level", wpa_cli_cmd_level,
1262 "<debug level> = change debug level" },
1263 { "license", wpa_cli_cmd_license,
1265 "= show full wpa_cli license" },
1266 { "quit", wpa_cli_cmd_quit,
1269 { "set", wpa_cli_cmd_set,
1271 "= set variables (shows list of variables when run without "
1273 { "logon", wpa_cli_cmd_logon,
1275 "= IEEE 802.1X EAPOL state machine logon" },
1276 { "logoff", wpa_cli_cmd_logoff,
1278 "= IEEE 802.1X EAPOL state machine logoff" },
1279 { "pmksa", wpa_cli_cmd_pmksa,
1281 "= show PMKSA cache" },
1282 { "reassociate", wpa_cli_cmd_reassociate,
1284 "= force reassociation" },
1285 { "preauthenticate", wpa_cli_cmd_preauthenticate,
1287 "<BSSID> = force preauthentication" },
1288 { "identity", wpa_cli_cmd_identity,
1290 "<network id> <identity> = configure identity for an SSID" },
1291 { "password", wpa_cli_cmd_password,
1292 cli_cmd_flag_sensitive,
1293 "<network id> <password> = configure password for an SSID" },
1294 { "new_password", wpa_cli_cmd_new_password,
1295 cli_cmd_flag_sensitive,
1296 "<network id> <password> = change password for an SSID" },
1297 { "pin", wpa_cli_cmd_pin,
1298 cli_cmd_flag_sensitive,
1299 "<network id> <pin> = configure pin for an SSID" },
1300 { "otp", wpa_cli_cmd_otp,
1301 cli_cmd_flag_sensitive,
1302 "<network id> <password> = configure one-time-password for an SSID"
1304 { "passphrase", wpa_cli_cmd_passphrase,
1305 cli_cmd_flag_sensitive,
1306 "<network id> <passphrase> = configure private key passphrase\n"
1308 { "bssid", wpa_cli_cmd_bssid,
1310 "<network id> <BSSID> = set preferred BSSID for an SSID" },
1311 { "list_networks", wpa_cli_cmd_list_networks,
1313 "= list configured networks" },
1314 { "select_network", wpa_cli_cmd_select_network,
1316 "<network id> = select a network (disable others)" },
1317 { "enable_network", wpa_cli_cmd_enable_network,
1319 "<network id> = enable a network" },
1320 { "disable_network", wpa_cli_cmd_disable_network,
1322 "<network id> = disable a network" },
1323 { "add_network", wpa_cli_cmd_add_network,
1325 "= add a network" },
1326 { "remove_network", wpa_cli_cmd_remove_network,
1328 "<network id> = remove a network" },
1329 { "set_network", wpa_cli_cmd_set_network,
1330 cli_cmd_flag_sensitive,
1331 "<network id> <variable> <value> = set network variables (shows\n"
1332 " list of variables when run without arguments)" },
1333 { "get_network", wpa_cli_cmd_get_network,
1335 "<network id> <variable> = get network variables" },
1336 { "save_config", wpa_cli_cmd_save_config,
1338 "= save the current configuration" },
1339 { "disconnect", wpa_cli_cmd_disconnect,
1341 "= disconnect and wait for reassociate/reconnect command before\n"
1343 { "reconnect", wpa_cli_cmd_reconnect,
1345 "= like reassociate, but only takes effect if already disconnected"
1347 { "scan", wpa_cli_cmd_scan,
1349 "= request new BSS scan" },
1350 { "scan_results", wpa_cli_cmd_scan_results,
1352 "= get latest scan results" },
1353 { "bss", wpa_cli_cmd_bss,
1355 "<<idx> | <bssid>> = get detailed scan result info" },
1356 { "get_capability", wpa_cli_cmd_get_capability,
1358 "<eap/pairwise/group/key_mgmt/proto/auth_alg> = get capabilies" },
1359 { "reconfigure", wpa_cli_cmd_reconfigure,
1361 "= force wpa_supplicant to re-read its configuration file" },
1362 { "terminate", wpa_cli_cmd_terminate,
1364 "= terminate wpa_supplicant" },
1365 { "interface_add", wpa_cli_cmd_interface_add,
1367 "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
1368 " <bridge_name> = adds new interface, all parameters but <ifname>\n"
1370 { "interface_remove", wpa_cli_cmd_interface_remove,
1372 "<ifname> = removes the interface" },
1373 { "interface_list", wpa_cli_cmd_interface_list,
1375 "= list available interfaces" },
1376 { "ap_scan", wpa_cli_cmd_ap_scan,
1378 "<value> = set ap_scan parameter" },
1379 { "stkstart", wpa_cli_cmd_stkstart,
1381 "<addr> = request STK negotiation with <addr>" },
1382 { "ft_ds", wpa_cli_cmd_ft_ds,
1384 "<addr> = request over-the-DS FT with <addr>" },
1385 { "wps_pbc", wpa_cli_cmd_wps_pbc,
1387 "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
1388 { "wps_pin", wpa_cli_cmd_wps_pin,
1389 cli_cmd_flag_sensitive,
1390 "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
1392 #ifdef CONFIG_WPS_OOB
1393 { "wps_oob", wpa_cli_cmd_wps_oob,
1394 cli_cmd_flag_sensitive,
1395 "<DEV_TYPE> <PATH> <METHOD> [DEV_NAME] = start WPS OOB" },
1396 #endif /* CONFIG_WPS_OOB */
1397 { "wps_reg", wpa_cli_cmd_wps_reg,
1398 cli_cmd_flag_sensitive,
1399 "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
1400 { "ibss_rsn", wpa_cli_cmd_ibss_rsn,
1402 "<addr> = request RSN authentication with <addr> in IBSS" },
1404 { "sta", wpa_cli_cmd_sta,
1406 "<addr> = get information about an associated station (AP)" },
1407 { "all_sta", wpa_cli_cmd_all_sta,
1409 "= get information about all associated stations (AP)" },
1410 #endif /* CONFIG_AP */
1411 { NULL, NULL, cli_cmd_flag_none, NULL }
1416 * Prints command usage, lines are padded with the specified string.
1418 static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad)
1423 printf("%s%s ", pad, cmd->cmd);
1424 for (n = 0; (c = cmd->usage[n]); n++) {
1433 static void print_help(void)
1436 printf("commands:\n");
1437 for (n = 0; wpa_cli_commands[n].cmd; n++)
1438 print_cmd_help(&wpa_cli_commands[n], " ");
1442 #ifdef CONFIG_READLINE
1443 static int cmd_has_sensitive_data(const char *cmd)
1445 const char *c, *delim;
1449 delim = os_strchr(cmd, ' ');
1453 len = os_strlen(cmd);
1455 for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
1456 if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
1457 return (wpa_cli_commands[n].flags &
1458 cli_cmd_flag_sensitive);
1462 #endif /* CONFIG_READLINE */
1465 static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1467 struct wpa_cli_cmd *cmd, *match = NULL;
1472 cmd = wpa_cli_commands;
1474 if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
1477 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1478 /* we have an exact match */
1488 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1489 cmd = wpa_cli_commands;
1491 if (os_strncasecmp(cmd->cmd, argv[0],
1492 os_strlen(argv[0])) == 0) {
1493 printf(" %s", cmd->cmd);
1499 } else if (count == 0) {
1500 printf("Unknown command '%s'\n", argv[0]);
1503 ret = match->handler(ctrl, argc - 1, &argv[1]);
1510 static int str_match(const char *a, const char *b)
1512 return os_strncmp(a, b, os_strlen(b)) == 0;
1516 static int wpa_cli_exec(const char *program, const char *arg1,
1524 len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
1525 cmd = os_malloc(len);
1528 res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
1529 if (res < 0 || (size_t) res >= len) {
1533 cmd[len - 1] = '\0';
1535 if (system(cmd) < 0)
1537 #endif /* _WIN32_WCE */
1544 static void wpa_cli_action_process(const char *msg)
1547 char *copy = NULL, *id, *pos2;
1552 pos = os_strchr(pos, '>');
1559 if (str_match(pos, WPA_EVENT_CONNECTED)) {
1561 os_unsetenv("WPA_ID");
1562 os_unsetenv("WPA_ID_STR");
1563 os_unsetenv("WPA_CTRL_DIR");
1565 pos = os_strstr(pos, "[id=");
1567 copy = os_strdup(pos + 4);
1571 while (*pos2 && *pos2 != ' ')
1575 os_setenv("WPA_ID", id, 1);
1576 while (*pos2 && *pos2 != '=')
1581 while (*pos2 && *pos2 != ']')
1584 os_setenv("WPA_ID_STR", id, 1);
1588 os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
1590 if (!wpa_cli_connected || new_id != wpa_cli_last_id) {
1591 wpa_cli_connected = 1;
1592 wpa_cli_last_id = new_id;
1593 wpa_cli_exec(action_file, ctrl_ifname, "CONNECTED");
1595 } else if (str_match(pos, WPA_EVENT_DISCONNECTED)) {
1596 if (wpa_cli_connected) {
1597 wpa_cli_connected = 0;
1598 wpa_cli_exec(action_file, ctrl_ifname, "DISCONNECTED");
1600 } else if (str_match(pos, WPA_EVENT_TERMINATING)) {
1601 printf("wpa_supplicant is terminating - stop monitoring\n");
1607 #ifndef CONFIG_ANSI_C_EXTRA
1608 static void wpa_cli_action_cb(char *msg, size_t len)
1610 wpa_cli_action_process(msg);
1612 #endif /* CONFIG_ANSI_C_EXTRA */
1615 static void wpa_cli_reconnect(void)
1617 wpa_cli_close_connection();
1618 ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
1620 printf("Connection to wpa_supplicant re-established\n");
1621 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1622 wpa_cli_attached = 1;
1624 printf("Warning: Failed to attach to "
1625 "wpa_supplicant.\n");
1631 static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1635 if (ctrl_conn == NULL) {
1636 wpa_cli_reconnect();
1639 while (wpa_ctrl_pending(ctrl) > 0) {
1641 size_t len = sizeof(buf) - 1;
1642 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1645 wpa_cli_action_process(buf);
1647 if (in_read && first)
1650 printf("%s\n", buf);
1653 printf("Could not read pending message.\n");
1658 if (wpa_ctrl_pending(ctrl) < 0) {
1659 printf("Connection to wpa_supplicant lost - trying to "
1661 wpa_cli_reconnect();
1666 #ifdef CONFIG_READLINE
1667 static char * wpa_cli_cmd_gen(const char *text, int state)
1674 len = os_strlen(text);
1677 while ((cmd = wpa_cli_commands[i].cmd)) {
1679 if (os_strncasecmp(cmd, text, len) == 0)
1680 return os_strdup(cmd);
1687 static char * wpa_cli_dummy_gen(const char *text, int state)
1693 static char ** wpa_cli_completion(const char *text, int start, int end)
1695 return rl_completion_matches(text, start == 0 ?
1696 wpa_cli_cmd_gen : wpa_cli_dummy_gen);
1698 #endif /* CONFIG_READLINE */
1701 static void wpa_cli_interactive(void)
1704 char cmdbuf[256], *cmd, *argv[max_args], *pos;
1706 #ifdef CONFIG_READLINE
1707 char *home, *hfile = NULL;
1708 #endif /* CONFIG_READLINE */
1710 printf("\nInteractive mode\n\n");
1712 #ifdef CONFIG_READLINE
1713 rl_attempted_completion_function = wpa_cli_completion;
1714 home = getenv("HOME");
1716 const char *fname = ".wpa_cli_history";
1717 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
1718 hfile = os_malloc(hfile_len);
1721 res = os_snprintf(hfile, hfile_len, "%s/%s", home,
1723 if (res >= 0 && res < hfile_len) {
1724 hfile[hfile_len - 1] = '\0';
1725 read_history(hfile);
1726 stifle_history(100);
1730 #endif /* CONFIG_READLINE */
1733 wpa_cli_recv_pending(ctrl_conn, 0, 0);
1734 #ifndef CONFIG_NATIVE_WINDOWS
1735 alarm(ping_interval);
1736 #endif /* CONFIG_NATIVE_WINDOWS */
1737 #ifdef CONFIG_READLINE
1738 cmd = readline("> ");
1741 while (next_history())
1743 h = previous_history();
1744 if (h == NULL || os_strcmp(cmd, h->line) != 0)
1748 #else /* CONFIG_READLINE */
1750 cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
1751 #endif /* CONFIG_READLINE */
1752 #ifndef CONFIG_NATIVE_WINDOWS
1754 #endif /* CONFIG_NATIVE_WINDOWS */
1757 wpa_cli_recv_pending(ctrl_conn, 0, 0);
1759 while (*pos != '\0') {
1775 if (argc == max_args)
1778 char *pos2 = os_strrchr(pos, '"');
1782 while (*pos != '\0' && *pos != ' ')
1788 wpa_request(ctrl_conn, argc, argv);
1792 } while (!wpa_cli_quit);
1794 #ifdef CONFIG_READLINE
1796 /* Save command history, excluding lines that may contain
1800 while ((h = current_history())) {
1802 while (*p == ' ' || *p == '\t')
1804 if (cmd_has_sensitive_data(p)) {
1805 h = remove_history(where_history());
1815 write_history(hfile);
1818 #endif /* CONFIG_READLINE */
1822 static void wpa_cli_action(struct wpa_ctrl *ctrl)
1824 #ifdef CONFIG_ANSI_C_EXTRA
1825 /* TODO: ANSI C version(?) */
1826 printf("Action processing not supported in ANSI C build.\n");
1827 #else /* CONFIG_ANSI_C_EXTRA */
1831 char buf[256]; /* note: large enough to fit in unsolicited messages */
1834 fd = wpa_ctrl_get_fd(ctrl);
1836 while (!wpa_cli_quit) {
1839 tv.tv_sec = ping_interval;
1841 res = select(fd + 1, &rfds, NULL, NULL, &tv);
1842 if (res < 0 && errno != EINTR) {
1847 if (FD_ISSET(fd, &rfds))
1848 wpa_cli_recv_pending(ctrl, 0, 1);
1850 /* verify that connection is still working */
1851 len = sizeof(buf) - 1;
1852 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
1853 wpa_cli_action_cb) < 0 ||
1854 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
1855 printf("wpa_supplicant did not reply to PING "
1856 "command - exiting\n");
1861 #endif /* CONFIG_ANSI_C_EXTRA */
1865 static void wpa_cli_cleanup(void)
1867 wpa_cli_close_connection();
1869 os_daemonize_terminate(pid_file);
1871 os_program_deinit();
1874 static void wpa_cli_terminate(int sig)
1881 #ifndef CONFIG_NATIVE_WINDOWS
1882 static void wpa_cli_alarm(int sig)
1884 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1885 printf("Connection to wpa_supplicant lost - trying to "
1887 wpa_cli_close_connection();
1890 wpa_cli_reconnect();
1892 wpa_cli_recv_pending(ctrl_conn, 1, 0);
1893 alarm(ping_interval);
1895 #endif /* CONFIG_NATIVE_WINDOWS */
1898 static char * wpa_cli_get_default_ifname(void)
1900 char *ifname = NULL;
1902 #ifdef CONFIG_CTRL_IFACE_UNIX
1903 struct dirent *dent;
1904 DIR *dir = opendir(ctrl_iface_dir);
1907 while ((dent = readdir(dir))) {
1908 #ifdef _DIRENT_HAVE_D_TYPE
1910 * Skip the file if it is not a socket. Also accept
1911 * DT_UNKNOWN (0) in case the C library or underlying
1912 * file system does not support d_type.
1914 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
1916 #endif /* _DIRENT_HAVE_D_TYPE */
1917 if (os_strcmp(dent->d_name, ".") == 0 ||
1918 os_strcmp(dent->d_name, "..") == 0)
1920 printf("Selected interface '%s'\n", dent->d_name);
1921 ifname = os_strdup(dent->d_name);
1925 #endif /* CONFIG_CTRL_IFACE_UNIX */
1927 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
1928 char buf[2048], *pos;
1930 struct wpa_ctrl *ctrl;
1933 ctrl = wpa_ctrl_open(NULL);
1937 len = sizeof(buf) - 1;
1938 ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
1941 pos = os_strchr(buf, '\n');
1944 ifname = os_strdup(buf);
1946 wpa_ctrl_close(ctrl);
1947 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1953 int main(int argc, char *argv[])
1956 int warning_displayed = 0;
1960 const char *global = NULL;
1962 if (os_program_init())
1966 c = getopt(argc, argv, "a:Bg:G:hi:p:P:v");
1971 action_file = optarg;
1980 ping_interval = atoi(optarg);
1986 printf("%s\n", wpa_cli_version);
1989 os_free(ctrl_ifname);
1990 ctrl_ifname = os_strdup(optarg);
1993 ctrl_iface_dir = optarg;
2004 interactive = (argc == optind) && (action_file == NULL);
2007 printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license);
2010 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
2011 ctrl_conn = wpa_ctrl_open(NULL);
2012 #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
2013 ctrl_conn = wpa_ctrl_open(global);
2014 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
2015 if (ctrl_conn == NULL) {
2016 perror("Failed to connect to wpa_supplicant - "
2023 if (ctrl_ifname == NULL)
2024 ctrl_ifname = wpa_cli_get_default_ifname();
2025 ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
2027 if (warning_displayed)
2028 printf("Connection established.\n");
2033 perror("Failed to connect to wpa_supplicant - "
2038 if (!warning_displayed) {
2039 printf("Could not connect to wpa_supplicant - "
2041 warning_displayed = 1;
2048 signal(SIGINT, wpa_cli_terminate);
2049 signal(SIGTERM, wpa_cli_terminate);
2050 #endif /* _WIN32_WCE */
2051 #ifndef CONFIG_NATIVE_WINDOWS
2052 signal(SIGALRM, wpa_cli_alarm);
2053 #endif /* CONFIG_NATIVE_WINDOWS */
2055 if (interactive || action_file) {
2056 if (wpa_ctrl_attach(ctrl_conn) == 0) {
2057 wpa_cli_attached = 1;
2059 printf("Warning: Failed to attach to "
2060 "wpa_supplicant.\n");
2066 if (daemonize && os_daemonize(pid_file))
2070 wpa_cli_interactive();
2071 else if (action_file)
2072 wpa_cli_action(ctrl_conn);
2074 ret = wpa_request(ctrl_conn, argc - optind, &argv[optind]);
2076 os_free(ctrl_ifname);
2082 #else /* CONFIG_CTRL_IFACE */
2083 int main(int argc, char *argv[])
2085 printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
2088 #endif /* CONFIG_CTRL_IFACE */