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 printf("Invalid WPS_REG command: need two arguments:\n"
489 "- BSSID: use 'any' to select any\n"
494 res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s", argv[0], argv[1]);
495 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
496 printf("Too long WPS_REG command.\n");
499 return wpa_ctrl_command(ctrl, cmd);
503 static int wpa_cli_cmd_ibss_rsn(struct wpa_ctrl *ctrl, int argc, char *argv[])
509 printf("Invalid IBSS_RSN command: needs one argument "
510 "(Peer STA MAC address)\n");
514 res = os_snprintf(cmd, sizeof(cmd), "IBSS_RSN %s", argv[0]);
515 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
516 printf("Too long IBSS_RSN command.\n");
519 return wpa_ctrl_command(ctrl, cmd);
523 static int wpa_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
529 printf("Invalid LEVEL command: needs one argument (debug "
533 res = os_snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
534 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
535 printf("Too long LEVEL command.\n");
538 return wpa_ctrl_command(ctrl, cmd);
542 static int wpa_cli_cmd_identity(struct wpa_ctrl *ctrl, int argc, char *argv[])
544 char cmd[256], *pos, *end;
548 printf("Invalid IDENTITY command: needs two arguments "
549 "(network id and identity)\n");
553 end = cmd + sizeof(cmd);
555 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "IDENTITY-%s:%s",
557 if (ret < 0 || ret >= end - pos) {
558 printf("Too long IDENTITY command.\n");
562 for (i = 2; i < argc; i++) {
563 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
564 if (ret < 0 || ret >= end - pos) {
565 printf("Too long IDENTITY command.\n");
571 return wpa_ctrl_command(ctrl, cmd);
575 static int wpa_cli_cmd_password(struct wpa_ctrl *ctrl, int argc, char *argv[])
577 char cmd[256], *pos, *end;
581 printf("Invalid PASSWORD command: needs two arguments "
582 "(network id and password)\n");
586 end = cmd + sizeof(cmd);
588 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSWORD-%s:%s",
590 if (ret < 0 || ret >= end - pos) {
591 printf("Too long PASSWORD 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 PASSWORD command.\n");
604 return wpa_ctrl_command(ctrl, cmd);
608 static int wpa_cli_cmd_new_password(struct wpa_ctrl *ctrl, int argc,
611 char cmd[256], *pos, *end;
615 printf("Invalid NEW_PASSWORD command: needs two arguments "
616 "(network id and password)\n");
620 end = cmd + sizeof(cmd);
622 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "NEW_PASSWORD-%s:%s",
624 if (ret < 0 || ret >= end - pos) {
625 printf("Too long NEW_PASSWORD command.\n");
629 for (i = 2; i < argc; i++) {
630 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
631 if (ret < 0 || ret >= end - pos) {
632 printf("Too long NEW_PASSWORD command.\n");
638 return wpa_ctrl_command(ctrl, cmd);
642 static int wpa_cli_cmd_pin(struct wpa_ctrl *ctrl, int argc, char *argv[])
644 char cmd[256], *pos, *end;
648 printf("Invalid PIN command: needs two arguments "
649 "(network id and pin)\n");
653 end = cmd + sizeof(cmd);
655 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PIN-%s:%s",
657 if (ret < 0 || ret >= end - pos) {
658 printf("Too long PIN 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 PIN command.\n");
670 return wpa_ctrl_command(ctrl, cmd);
674 static int wpa_cli_cmd_otp(struct wpa_ctrl *ctrl, int argc, char *argv[])
676 char cmd[256], *pos, *end;
680 printf("Invalid OTP command: needs two arguments (network "
681 "id and password)\n");
685 end = cmd + sizeof(cmd);
687 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "OTP-%s:%s",
689 if (ret < 0 || ret >= end - pos) {
690 printf("Too long OTP command.\n");
694 for (i = 2; i < argc; i++) {
695 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
696 if (ret < 0 || ret >= end - pos) {
697 printf("Too long OTP command.\n");
703 return wpa_ctrl_command(ctrl, cmd);
707 static int wpa_cli_cmd_passphrase(struct wpa_ctrl *ctrl, int argc,
710 char cmd[256], *pos, *end;
714 printf("Invalid PASSPHRASE command: needs two arguments "
715 "(network id and passphrase)\n");
719 end = cmd + sizeof(cmd);
721 ret = os_snprintf(pos, end - pos, WPA_CTRL_RSP "PASSPHRASE-%s:%s",
723 if (ret < 0 || ret >= end - pos) {
724 printf("Too long PASSPHRASE command.\n");
728 for (i = 2; i < argc; i++) {
729 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
730 if (ret < 0 || ret >= end - pos) {
731 printf("Too long PASSPHRASE command.\n");
737 return wpa_ctrl_command(ctrl, cmd);
741 static int wpa_cli_cmd_bssid(struct wpa_ctrl *ctrl, int argc, char *argv[])
743 char cmd[256], *pos, *end;
747 printf("Invalid BSSID command: needs two arguments (network "
752 end = cmd + sizeof(cmd);
754 ret = os_snprintf(pos, end - pos, "BSSID");
755 if (ret < 0 || ret >= end - pos) {
756 printf("Too long BSSID command.\n");
760 for (i = 0; i < argc; i++) {
761 ret = os_snprintf(pos, end - pos, " %s", argv[i]);
762 if (ret < 0 || ret >= end - pos) {
763 printf("Too long BSSID command.\n");
769 return wpa_ctrl_command(ctrl, cmd);
773 static int wpa_cli_cmd_list_networks(struct wpa_ctrl *ctrl, int argc,
776 return wpa_ctrl_command(ctrl, "LIST_NETWORKS");
780 static int wpa_cli_cmd_select_network(struct wpa_ctrl *ctrl, int argc,
787 printf("Invalid SELECT_NETWORK command: needs one argument "
792 res = os_snprintf(cmd, sizeof(cmd), "SELECT_NETWORK %s", argv[0]);
793 if (res < 0 || (size_t) res >= sizeof(cmd))
795 cmd[sizeof(cmd) - 1] = '\0';
797 return wpa_ctrl_command(ctrl, cmd);
801 static int wpa_cli_cmd_enable_network(struct wpa_ctrl *ctrl, int argc,
808 printf("Invalid ENABLE_NETWORK command: needs one argument "
813 res = os_snprintf(cmd, sizeof(cmd), "ENABLE_NETWORK %s", argv[0]);
814 if (res < 0 || (size_t) res >= sizeof(cmd))
816 cmd[sizeof(cmd) - 1] = '\0';
818 return wpa_ctrl_command(ctrl, cmd);
822 static int wpa_cli_cmd_disable_network(struct wpa_ctrl *ctrl, int argc,
829 printf("Invalid DISABLE_NETWORK command: needs one argument "
834 res = os_snprintf(cmd, sizeof(cmd), "DISABLE_NETWORK %s", argv[0]);
835 if (res < 0 || (size_t) res >= sizeof(cmd))
837 cmd[sizeof(cmd) - 1] = '\0';
839 return wpa_ctrl_command(ctrl, cmd);
843 static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
846 return wpa_ctrl_command(ctrl, "ADD_NETWORK");
850 static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
857 printf("Invalid REMOVE_NETWORK command: needs one argument "
862 res = os_snprintf(cmd, sizeof(cmd), "REMOVE_NETWORK %s", argv[0]);
863 if (res < 0 || (size_t) res >= sizeof(cmd))
865 cmd[sizeof(cmd) - 1] = '\0';
867 return wpa_ctrl_command(ctrl, cmd);
871 static void wpa_cli_show_network_variables(void)
873 printf("set_network variables:\n"
874 " ssid (network name, SSID)\n"
875 " psk (WPA passphrase or pre-shared key)\n"
876 " key_mgmt (key management protocol)\n"
877 " identity (EAP identity)\n"
878 " password (EAP password)\n"
881 "Note: Values are entered in the same format as the "
882 "configuration file is using,\n"
883 "i.e., strings values need to be inside double quotation "
885 "For example: set_network 1 ssid \"network name\"\n"
887 "Please see wpa_supplicant.conf documentation for full list "
888 "of\navailable variables.\n");
892 static int wpa_cli_cmd_set_network(struct wpa_ctrl *ctrl, int argc,
899 wpa_cli_show_network_variables();
904 printf("Invalid SET_NETWORK command: needs three arguments\n"
905 "(network id, variable name, and value)\n");
909 res = os_snprintf(cmd, sizeof(cmd), "SET_NETWORK %s %s %s",
910 argv[0], argv[1], argv[2]);
911 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
912 printf("Too long SET_NETWORK command.\n");
915 return wpa_ctrl_command(ctrl, cmd);
919 static int wpa_cli_cmd_get_network(struct wpa_ctrl *ctrl, int argc,
926 wpa_cli_show_network_variables();
931 printf("Invalid GET_NETWORK command: needs two arguments\n"
932 "(network id and variable name)\n");
936 res = os_snprintf(cmd, sizeof(cmd), "GET_NETWORK %s %s",
938 if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
939 printf("Too long GET_NETWORK command.\n");
942 return wpa_ctrl_command(ctrl, cmd);
946 static int wpa_cli_cmd_disconnect(struct wpa_ctrl *ctrl, int argc,
949 return wpa_ctrl_command(ctrl, "DISCONNECT");
953 static int wpa_cli_cmd_reconnect(struct wpa_ctrl *ctrl, int argc,
956 return wpa_ctrl_command(ctrl, "RECONNECT");
960 static int wpa_cli_cmd_save_config(struct wpa_ctrl *ctrl, int argc,
963 return wpa_ctrl_command(ctrl, "SAVE_CONFIG");
967 static int wpa_cli_cmd_scan(struct wpa_ctrl *ctrl, int argc, char *argv[])
969 return wpa_ctrl_command(ctrl, "SCAN");
973 static int wpa_cli_cmd_scan_results(struct wpa_ctrl *ctrl, int argc,
976 return wpa_ctrl_command(ctrl, "SCAN_RESULTS");
980 static int wpa_cli_cmd_bss(struct wpa_ctrl *ctrl, int argc, char *argv[])
986 printf("Invalid BSS command: need one argument (index or "
991 res = os_snprintf(cmd, sizeof(cmd), "BSS %s", argv[0]);
992 if (res < 0 || (size_t) res >= sizeof(cmd))
994 cmd[sizeof(cmd) - 1] = '\0';
996 return wpa_ctrl_command(ctrl, cmd);
1000 static int wpa_cli_cmd_get_capability(struct wpa_ctrl *ctrl, int argc,
1006 if (argc < 1 || argc > 2) {
1007 printf("Invalid GET_CAPABILITY command: need either one or "
1012 if ((argc == 2) && os_strcmp(argv[1], "strict") != 0) {
1013 printf("Invalid GET_CAPABILITY command: second argument, "
1014 "if any, must be 'strict'\n");
1018 res = os_snprintf(cmd, sizeof(cmd), "GET_CAPABILITY %s%s", argv[0],
1019 (argc == 2) ? " strict" : "");
1020 if (res < 0 || (size_t) res >= sizeof(cmd))
1022 cmd[sizeof(cmd) - 1] = '\0';
1024 return wpa_ctrl_command(ctrl, cmd);
1028 static int wpa_cli_list_interfaces(struct wpa_ctrl *ctrl)
1030 printf("Available interfaces:\n");
1031 return wpa_ctrl_command(ctrl, "INTERFACES");
1035 static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
1038 wpa_cli_list_interfaces(ctrl);
1042 wpa_cli_close_connection();
1043 os_free(ctrl_ifname);
1044 ctrl_ifname = os_strdup(argv[0]);
1046 if (wpa_cli_open_connection(ctrl_ifname)) {
1047 printf("Connected to interface '%s.\n", ctrl_ifname);
1048 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1049 wpa_cli_attached = 1;
1051 printf("Warning: Failed to attach to "
1052 "wpa_supplicant.\n");
1055 printf("Could not connect to interface '%s' - re-trying\n",
1062 static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
1065 return wpa_ctrl_command(ctrl, "RECONFIGURE");
1069 static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
1072 return wpa_ctrl_command(ctrl, "TERMINATE");
1076 static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
1083 printf("Invalid INTERFACE_ADD command: needs at least one "
1084 "argument (interface name)\n"
1085 "All arguments: ifname confname driver ctrl_interface "
1086 "driver_param bridge_name\n");
1091 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
1092 * <driver_param>TAB<bridge_name>
1094 res = os_snprintf(cmd, sizeof(cmd),
1095 "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
1097 argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
1098 argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
1099 argc > 5 ? argv[5] : "");
1100 if (res < 0 || (size_t) res >= sizeof(cmd))
1102 cmd[sizeof(cmd) - 1] = '\0';
1103 return wpa_ctrl_command(ctrl, cmd);
1107 static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
1114 printf("Invalid INTERFACE_REMOVE command: needs one argument "
1115 "(interface name)\n");
1119 res = os_snprintf(cmd, sizeof(cmd), "INTERFACE_REMOVE %s", argv[0]);
1120 if (res < 0 || (size_t) res >= sizeof(cmd))
1122 cmd[sizeof(cmd) - 1] = '\0';
1123 return wpa_ctrl_command(ctrl, cmd);
1127 static int wpa_cli_cmd_interface_list(struct wpa_ctrl *ctrl, int argc,
1130 return wpa_ctrl_command(ctrl, "INTERFACE_LIST");
1134 enum wpa_cli_cmd_flags {
1135 cli_cmd_flag_none = 0x00,
1136 cli_cmd_flag_sensitive = 0x01
1139 struct wpa_cli_cmd {
1141 int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
1142 enum wpa_cli_cmd_flags flags;
1146 static struct wpa_cli_cmd wpa_cli_commands[] = {
1147 { "status", wpa_cli_cmd_status,
1149 "[verbose] = get current WPA/EAPOL/EAP status" },
1150 { "ping", wpa_cli_cmd_ping,
1152 "= pings wpa_supplicant" },
1153 { "mib", wpa_cli_cmd_mib,
1155 "= get MIB variables (dot1x, dot11)" },
1156 { "help", wpa_cli_cmd_help,
1158 "= show this usage help" },
1159 { "interface", wpa_cli_cmd_interface,
1161 "[ifname] = show interfaces/select interface" },
1162 { "level", wpa_cli_cmd_level,
1164 "<debug level> = change debug level" },
1165 { "license", wpa_cli_cmd_license,
1167 "= show full wpa_cli license" },
1168 { "quit", wpa_cli_cmd_quit,
1171 { "set", wpa_cli_cmd_set,
1173 "= set variables (shows list of variables when run without "
1175 { "logon", wpa_cli_cmd_logon,
1177 "= IEEE 802.1X EAPOL state machine logon" },
1178 { "logoff", wpa_cli_cmd_logoff,
1180 "= IEEE 802.1X EAPOL state machine logoff" },
1181 { "pmksa", wpa_cli_cmd_pmksa,
1183 "= show PMKSA cache" },
1184 { "reassociate", wpa_cli_cmd_reassociate,
1186 "= force reassociation" },
1187 { "preauthenticate", wpa_cli_cmd_preauthenticate,
1189 "<BSSID> = force preauthentication" },
1190 { "identity", wpa_cli_cmd_identity,
1192 "<network id> <identity> = configure identity for an SSID" },
1193 { "password", wpa_cli_cmd_password,
1194 cli_cmd_flag_sensitive,
1195 "<network id> <password> = configure password for an SSID" },
1196 { "new_password", wpa_cli_cmd_new_password,
1197 cli_cmd_flag_sensitive,
1198 "<network id> <password> = change password for an SSID" },
1199 { "pin", wpa_cli_cmd_pin,
1200 cli_cmd_flag_sensitive,
1201 "<network id> <pin> = configure pin for an SSID" },
1202 { "otp", wpa_cli_cmd_otp,
1203 cli_cmd_flag_sensitive,
1204 "<network id> <password> = configure one-time-password for an SSID"
1206 { "passphrase", wpa_cli_cmd_passphrase,
1207 cli_cmd_flag_sensitive,
1208 "<network id> <passphrase> = configure private key passphrase\n"
1210 { "bssid", wpa_cli_cmd_bssid,
1212 "<network id> <BSSID> = set preferred BSSID for an SSID" },
1213 { "list_networks", wpa_cli_cmd_list_networks,
1215 "= list configured networks" },
1216 { "select_network", wpa_cli_cmd_select_network,
1218 "<network id> = select a network (disable others)" },
1219 { "enable_network", wpa_cli_cmd_enable_network,
1221 "<network id> = enable a network" },
1222 { "disable_network", wpa_cli_cmd_disable_network,
1224 "<network id> = disable a network" },
1225 { "add_network", wpa_cli_cmd_add_network,
1227 "= add a network" },
1228 { "remove_network", wpa_cli_cmd_remove_network,
1230 "<network id> = remove a network" },
1231 { "set_network", wpa_cli_cmd_set_network,
1232 cli_cmd_flag_sensitive,
1233 "<network id> <variable> <value> = set network variables (shows\n"
1234 " list of variables when run without arguments)" },
1235 { "get_network", wpa_cli_cmd_get_network,
1237 "<network id> <variable> = get network variables" },
1238 { "save_config", wpa_cli_cmd_save_config,
1240 "= save the current configuration" },
1241 { "disconnect", wpa_cli_cmd_disconnect,
1243 "= disconnect and wait for reassociate/reconnect command before\n"
1245 { "reconnect", wpa_cli_cmd_reconnect,
1247 "= like reassociate, but only takes effect if already disconnected"
1249 { "scan", wpa_cli_cmd_scan,
1251 "= request new BSS scan" },
1252 { "scan_results", wpa_cli_cmd_scan_results,
1254 "= get latest scan results" },
1255 { "bss", wpa_cli_cmd_bss,
1257 "<<idx> | <bssid>> = get detailed scan result info" },
1258 { "get_capability", wpa_cli_cmd_get_capability,
1260 "<eap/pairwise/group/key_mgmt/proto/auth_alg> = get capabilies" },
1261 { "reconfigure", wpa_cli_cmd_reconfigure,
1263 "= force wpa_supplicant to re-read its configuration file" },
1264 { "terminate", wpa_cli_cmd_terminate,
1266 "= terminate wpa_supplicant" },
1267 { "interface_add", wpa_cli_cmd_interface_add,
1269 "<ifname> <confname> <driver> <ctrl_interface> <driver_param>\n"
1270 " <bridge_name> = adds new interface, all parameters but <ifname>\n"
1272 { "interface_remove", wpa_cli_cmd_interface_remove,
1274 "<ifname> = removes the interface" },
1275 { "interface_list", wpa_cli_cmd_interface_list,
1277 "= list available interfaces" },
1278 { "ap_scan", wpa_cli_cmd_ap_scan,
1280 "<value> = set ap_scan parameter" },
1281 { "stkstart", wpa_cli_cmd_stkstart,
1283 "<addr> = request STK negotiation with <addr>" },
1284 { "ft_ds", wpa_cli_cmd_ft_ds,
1286 "<addr> = request over-the-DS FT with <addr>" },
1287 { "wps_pbc", wpa_cli_cmd_wps_pbc,
1289 "[BSSID] = start Wi-Fi Protected Setup: Push Button Configuration" },
1290 { "wps_pin", wpa_cli_cmd_wps_pin,
1291 cli_cmd_flag_sensitive,
1292 "<BSSID> [PIN] = start WPS PIN method (returns PIN, if not "
1294 #ifdef CONFIG_WPS_OOB
1295 { "wps_oob", wpa_cli_cmd_wps_oob,
1296 cli_cmd_flag_sensitive,
1297 "<DEV_TYPE> <PATH> <METHOD> [DEV_NAME] = start WPS OOB" },
1298 #endif /* CONFIG_WPS_OOB */
1299 { "wps_reg", wpa_cli_cmd_wps_reg,
1300 cli_cmd_flag_sensitive,
1301 "<BSSID> <AP PIN> = start WPS Registrar to configure an AP" },
1302 { "ibss_rsn", wpa_cli_cmd_ibss_rsn,
1304 "<addr> = request RSN authentication with <addr> in IBSS" },
1305 { NULL, NULL, cli_cmd_flag_none, NULL }
1310 * Prints command usage, lines are padded with the specified string.
1312 static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad)
1317 printf("%s%s ", pad, cmd->cmd);
1318 for (n = 0; (c = cmd->usage[n]); n++) {
1327 static void print_help(void)
1330 printf("commands:\n");
1331 for (n = 0; wpa_cli_commands[n].cmd; n++)
1332 print_cmd_help(&wpa_cli_commands[n], " ");
1336 #ifdef CONFIG_READLINE
1337 static int cmd_has_sensitive_data(const char *cmd)
1339 const char *c, *delim;
1343 delim = os_strchr(cmd, ' ');
1347 len = os_strlen(cmd);
1349 for (n = 0; (c = wpa_cli_commands[n].cmd); n++) {
1350 if (os_strncasecmp(cmd, c, len) == 0 && len == os_strlen(c))
1351 return (wpa_cli_commands[n].flags &
1352 cli_cmd_flag_sensitive);
1356 #endif /* CONFIG_READLINE */
1359 static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
1361 struct wpa_cli_cmd *cmd, *match = NULL;
1366 cmd = wpa_cli_commands;
1368 if (os_strncasecmp(cmd->cmd, argv[0], os_strlen(argv[0])) == 0)
1371 if (os_strcasecmp(cmd->cmd, argv[0]) == 0) {
1372 /* we have an exact match */
1382 printf("Ambiguous command '%s'; possible commands:", argv[0]);
1383 cmd = wpa_cli_commands;
1385 if (os_strncasecmp(cmd->cmd, argv[0],
1386 os_strlen(argv[0])) == 0) {
1387 printf(" %s", cmd->cmd);
1393 } else if (count == 0) {
1394 printf("Unknown command '%s'\n", argv[0]);
1397 ret = match->handler(ctrl, argc - 1, &argv[1]);
1404 static int str_match(const char *a, const char *b)
1406 return os_strncmp(a, b, os_strlen(b)) == 0;
1410 static int wpa_cli_exec(const char *program, const char *arg1,
1418 len = os_strlen(program) + os_strlen(arg1) + os_strlen(arg2) + 3;
1419 cmd = os_malloc(len);
1422 res = os_snprintf(cmd, len, "%s %s %s", program, arg1, arg2);
1423 if (res < 0 || (size_t) res >= len) {
1427 cmd[len - 1] = '\0';
1429 if (system(cmd) < 0)
1431 #endif /* _WIN32_WCE */
1438 static void wpa_cli_action_process(const char *msg)
1441 char *copy = NULL, *id, *pos2;
1446 pos = os_strchr(pos, '>');
1453 if (str_match(pos, WPA_EVENT_CONNECTED)) {
1455 os_unsetenv("WPA_ID");
1456 os_unsetenv("WPA_ID_STR");
1457 os_unsetenv("WPA_CTRL_DIR");
1459 pos = os_strstr(pos, "[id=");
1461 copy = os_strdup(pos + 4);
1465 while (*pos2 && *pos2 != ' ')
1469 os_setenv("WPA_ID", id, 1);
1470 while (*pos2 && *pos2 != '=')
1475 while (*pos2 && *pos2 != ']')
1478 os_setenv("WPA_ID_STR", id, 1);
1482 os_setenv("WPA_CTRL_DIR", ctrl_iface_dir, 1);
1484 if (!wpa_cli_connected || new_id != wpa_cli_last_id) {
1485 wpa_cli_connected = 1;
1486 wpa_cli_last_id = new_id;
1487 wpa_cli_exec(action_file, ctrl_ifname, "CONNECTED");
1489 } else if (str_match(pos, WPA_EVENT_DISCONNECTED)) {
1490 if (wpa_cli_connected) {
1491 wpa_cli_connected = 0;
1492 wpa_cli_exec(action_file, ctrl_ifname, "DISCONNECTED");
1494 } else if (str_match(pos, WPA_EVENT_TERMINATING)) {
1495 printf("wpa_supplicant is terminating - stop monitoring\n");
1501 #ifndef CONFIG_ANSI_C_EXTRA
1502 static void wpa_cli_action_cb(char *msg, size_t len)
1504 wpa_cli_action_process(msg);
1506 #endif /* CONFIG_ANSI_C_EXTRA */
1509 static void wpa_cli_reconnect(void)
1511 wpa_cli_close_connection();
1512 ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
1514 printf("Connection to wpa_supplicant re-established\n");
1515 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1516 wpa_cli_attached = 1;
1518 printf("Warning: Failed to attach to "
1519 "wpa_supplicant.\n");
1525 static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
1529 if (ctrl_conn == NULL) {
1530 wpa_cli_reconnect();
1533 while (wpa_ctrl_pending(ctrl) > 0) {
1535 size_t len = sizeof(buf) - 1;
1536 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
1539 wpa_cli_action_process(buf);
1541 if (in_read && first)
1544 printf("%s\n", buf);
1547 printf("Could not read pending message.\n");
1552 if (wpa_ctrl_pending(ctrl) < 0) {
1553 printf("Connection to wpa_supplicant lost - trying to "
1555 wpa_cli_reconnect();
1560 #ifdef CONFIG_READLINE
1561 static char * wpa_cli_cmd_gen(const char *text, int state)
1568 len = os_strlen(text);
1571 while ((cmd = wpa_cli_commands[i].cmd)) {
1573 if (os_strncasecmp(cmd, text, len) == 0)
1574 return os_strdup(cmd);
1581 static char * wpa_cli_dummy_gen(const char *text, int state)
1587 static char ** wpa_cli_completion(const char *text, int start, int end)
1589 return rl_completion_matches(text, start == 0 ?
1590 wpa_cli_cmd_gen : wpa_cli_dummy_gen);
1592 #endif /* CONFIG_READLINE */
1595 static void wpa_cli_interactive(void)
1598 char cmdbuf[256], *cmd, *argv[max_args], *pos;
1600 #ifdef CONFIG_READLINE
1601 char *home, *hfile = NULL;
1602 #endif /* CONFIG_READLINE */
1604 printf("\nInteractive mode\n\n");
1606 #ifdef CONFIG_READLINE
1607 rl_attempted_completion_function = wpa_cli_completion;
1608 home = getenv("HOME");
1610 const char *fname = ".wpa_cli_history";
1611 int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
1612 hfile = os_malloc(hfile_len);
1615 res = os_snprintf(hfile, hfile_len, "%s/%s", home,
1617 if (res >= 0 && res < hfile_len) {
1618 hfile[hfile_len - 1] = '\0';
1619 read_history(hfile);
1620 stifle_history(100);
1624 #endif /* CONFIG_READLINE */
1627 wpa_cli_recv_pending(ctrl_conn, 0, 0);
1628 #ifndef CONFIG_NATIVE_WINDOWS
1629 alarm(ping_interval);
1630 #endif /* CONFIG_NATIVE_WINDOWS */
1631 #ifdef CONFIG_READLINE
1632 cmd = readline("> ");
1635 while (next_history())
1637 h = previous_history();
1638 if (h == NULL || os_strcmp(cmd, h->line) != 0)
1642 #else /* CONFIG_READLINE */
1644 cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
1645 #endif /* CONFIG_READLINE */
1646 #ifndef CONFIG_NATIVE_WINDOWS
1648 #endif /* CONFIG_NATIVE_WINDOWS */
1651 wpa_cli_recv_pending(ctrl_conn, 0, 0);
1653 while (*pos != '\0') {
1669 if (argc == max_args)
1672 char *pos2 = os_strrchr(pos, '"');
1676 while (*pos != '\0' && *pos != ' ')
1682 wpa_request(ctrl_conn, argc, argv);
1686 } while (!wpa_cli_quit);
1688 #ifdef CONFIG_READLINE
1690 /* Save command history, excluding lines that may contain
1694 while ((h = current_history())) {
1696 while (*p == ' ' || *p == '\t')
1698 if (cmd_has_sensitive_data(p)) {
1699 h = remove_history(where_history());
1709 write_history(hfile);
1712 #endif /* CONFIG_READLINE */
1716 static void wpa_cli_action(struct wpa_ctrl *ctrl)
1718 #ifdef CONFIG_ANSI_C_EXTRA
1719 /* TODO: ANSI C version(?) */
1720 printf("Action processing not supported in ANSI C build.\n");
1721 #else /* CONFIG_ANSI_C_EXTRA */
1725 char buf[256]; /* note: large enough to fit in unsolicited messages */
1728 fd = wpa_ctrl_get_fd(ctrl);
1730 while (!wpa_cli_quit) {
1733 tv.tv_sec = ping_interval;
1735 res = select(fd + 1, &rfds, NULL, NULL, &tv);
1736 if (res < 0 && errno != EINTR) {
1741 if (FD_ISSET(fd, &rfds))
1742 wpa_cli_recv_pending(ctrl, 0, 1);
1744 /* verify that connection is still working */
1745 len = sizeof(buf) - 1;
1746 if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
1747 wpa_cli_action_cb) < 0 ||
1748 len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
1749 printf("wpa_supplicant did not reply to PING "
1750 "command - exiting\n");
1755 #endif /* CONFIG_ANSI_C_EXTRA */
1759 static void wpa_cli_cleanup(void)
1761 wpa_cli_close_connection();
1763 os_daemonize_terminate(pid_file);
1765 os_program_deinit();
1768 static void wpa_cli_terminate(int sig)
1775 #ifndef CONFIG_NATIVE_WINDOWS
1776 static void wpa_cli_alarm(int sig)
1778 if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
1779 printf("Connection to wpa_supplicant lost - trying to "
1781 wpa_cli_close_connection();
1784 wpa_cli_reconnect();
1786 wpa_cli_recv_pending(ctrl_conn, 1, 0);
1787 alarm(ping_interval);
1789 #endif /* CONFIG_NATIVE_WINDOWS */
1792 static char * wpa_cli_get_default_ifname(void)
1794 char *ifname = NULL;
1796 #ifdef CONFIG_CTRL_IFACE_UNIX
1797 struct dirent *dent;
1798 DIR *dir = opendir(ctrl_iface_dir);
1801 while ((dent = readdir(dir))) {
1802 #ifdef _DIRENT_HAVE_D_TYPE
1804 * Skip the file if it is not a socket. Also accept
1805 * DT_UNKNOWN (0) in case the C library or underlying
1806 * file system does not support d_type.
1808 if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN)
1810 #endif /* _DIRENT_HAVE_D_TYPE */
1811 if (os_strcmp(dent->d_name, ".") == 0 ||
1812 os_strcmp(dent->d_name, "..") == 0)
1814 printf("Selected interface '%s'\n", dent->d_name);
1815 ifname = os_strdup(dent->d_name);
1819 #endif /* CONFIG_CTRL_IFACE_UNIX */
1821 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
1822 char buf[2048], *pos;
1824 struct wpa_ctrl *ctrl;
1827 ctrl = wpa_ctrl_open(NULL);
1831 len = sizeof(buf) - 1;
1832 ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL);
1835 pos = os_strchr(buf, '\n');
1838 ifname = os_strdup(buf);
1840 wpa_ctrl_close(ctrl);
1841 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1847 int main(int argc, char *argv[])
1850 int warning_displayed = 0;
1854 const char *global = NULL;
1856 if (os_program_init())
1860 c = getopt(argc, argv, "a:Bg:G:hi:p:P:v");
1865 action_file = optarg;
1874 ping_interval = atoi(optarg);
1880 printf("%s\n", wpa_cli_version);
1883 os_free(ctrl_ifname);
1884 ctrl_ifname = os_strdup(optarg);
1887 ctrl_iface_dir = optarg;
1898 interactive = (argc == optind) && (action_file == NULL);
1901 printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license);
1904 #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE
1905 ctrl_conn = wpa_ctrl_open(NULL);
1906 #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1907 ctrl_conn = wpa_ctrl_open(global);
1908 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
1909 if (ctrl_conn == NULL) {
1910 perror("Failed to connect to wpa_supplicant - "
1917 if (ctrl_ifname == NULL)
1918 ctrl_ifname = wpa_cli_get_default_ifname();
1919 ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
1921 if (warning_displayed)
1922 printf("Connection established.\n");
1927 perror("Failed to connect to wpa_supplicant - "
1932 if (!warning_displayed) {
1933 printf("Could not connect to wpa_supplicant - "
1935 warning_displayed = 1;
1942 signal(SIGINT, wpa_cli_terminate);
1943 signal(SIGTERM, wpa_cli_terminate);
1944 #endif /* _WIN32_WCE */
1945 #ifndef CONFIG_NATIVE_WINDOWS
1946 signal(SIGALRM, wpa_cli_alarm);
1947 #endif /* CONFIG_NATIVE_WINDOWS */
1949 if (interactive || action_file) {
1950 if (wpa_ctrl_attach(ctrl_conn) == 0) {
1951 wpa_cli_attached = 1;
1953 printf("Warning: Failed to attach to "
1954 "wpa_supplicant.\n");
1960 if (daemonize && os_daemonize(pid_file))
1964 wpa_cli_interactive();
1965 else if (action_file)
1966 wpa_cli_action(ctrl_conn);
1968 ret = wpa_request(ctrl_conn, argc - optind, &argv[optind]);
1970 os_free(ctrl_ifname);
1976 #else /* CONFIG_CTRL_IFACE */
1977 int main(int argc, char *argv[])
1979 printf("CONFIG_CTRL_IFACE not defined - wpa_cli disabled\n");
1982 #endif /* CONFIG_CTRL_IFACE */