Allow wpa_cli/hostapd_cli client socket directory to be specified
[mech_eap.git] / wpa_supplicant / wpa_cli.c
index 5fe4618..7ddae3d 100644 (file)
 #endif /* ANDROID */
 
 
-static const char *wpa_cli_version =
+static const char *const wpa_cli_version =
 "wpa_cli v" VERSION_STR "\n"
 "Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi> and contributors";
 
 
-static const char *wpa_cli_license =
+static const char *const wpa_cli_license =
 "This software may be distributed under the terms of the BSD license.\n"
 "See README for more details.\n";
 
-static const char *wpa_cli_full_license =
+static const char *const wpa_cli_full_license =
 "This software may be distributed under the terms of the BSD license.\n"
 "\n"
 "Redistribution and use in source and binary forms, with or without\n"
@@ -76,6 +76,7 @@ static int wpa_cli_last_id = 0;
 #define CONFIG_CTRL_IFACE_DIR "/var/run/wpa_supplicant"
 #endif /* CONFIG_CTRL_IFACE_DIR */
 static const char *ctrl_iface_dir = CONFIG_CTRL_IFACE_DIR;
+static const char *client_socket_dir = NULL;
 static char *ctrl_ifname = NULL;
 static const char *pid_file = NULL;
 static const char *action_file = NULL;
@@ -107,7 +108,9 @@ static void usage(void)
 {
        printf("wpa_cli [-p<path to ctrl sockets>] [-i<ifname>] [-hvB] "
               "[-a<action file>] \\\n"
-              "        [-P<pid file>] [-g<global ctrl>] [-G<ping interval>]  "
+              "        [-P<pid file>] [-g<global ctrl>] [-G<ping interval>] "
+              "\\\n"
+              "        [-s<wpa_client_socket_file_path>] "
               "[command..]\n"
               "  -h = help (show this usage text)\n"
               "  -v = shown version information\n"
@@ -330,6 +333,13 @@ static int wpa_cli_open_connection(const char *ifname, int attach)
        }
 #endif /* ANDROID */
 
+       if (client_socket_dir && client_socket_dir[0] &&
+           access(client_socket_dir, F_OK) < 0) {
+               perror(client_socket_dir);
+               os_free(cfile);
+               return -1;
+       }
+
        if (cfile == NULL) {
                flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2;
                cfile = os_malloc(flen);
@@ -343,14 +353,14 @@ static int wpa_cli_open_connection(const char *ifname, int attach)
                }
        }
 
-       ctrl_conn = wpa_ctrl_open(cfile);
+       ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir);
        if (ctrl_conn == NULL) {
                os_free(cfile);
                return -1;
        }
 
        if (attach && interactive)
-               mon_conn = wpa_ctrl_open(cfile);
+               mon_conn = wpa_ctrl_open2(cfile, client_socket_dir);
        else
                mon_conn = NULL;
        os_free(cfile);
@@ -502,6 +512,10 @@ static int wpa_cli_cmd_status(struct wpa_ctrl *ctrl, int argc, char *argv[])
                return wpa_ctrl_command(ctrl, "STATUS-WPS");
        if (argc > 0 && os_strcmp(argv[0], "driver") == 0)
                return wpa_ctrl_command(ctrl, "STATUS-DRIVER");
+#ifdef ANDROID
+       if (argc > 0 && os_strcmp(argv[0], "no_events") == 0)
+               return wpa_ctrl_command(ctrl, "STATUS-NO_EVENTS");
+#endif /* ANDROID */
        return wpa_ctrl_command(ctrl, "STATUS");
 }
 
@@ -618,6 +632,7 @@ static char ** wpa_cli_complete_set(const char *str, int pos)
                "eapol_version", "ap_scan", "bgscan",
 #ifdef CONFIG_MESH
                "user_mpm", "max_peer_links", "mesh_max_inactivity",
+               "dot11RSNASAERetransPeriod",
 #endif /* CONFIG_MESH */
                "disable_scan_offload", "fast_reauth", "opensc_engine_path",
                "pkcs11_engine_path", "pkcs11_module_path", "openssl_ciphers",
@@ -662,7 +677,7 @@ static char ** wpa_cli_complete_set(const char *str, int pos)
                "tdls_external_control", "osu_dir", "wowlan_triggers",
                "p2p_search_delay", "mac_addr", "rand_addr_lifetime",
                "preassoc_mac_addr", "key_mgmt_offload", "passive_scan",
-               "reassoc_same_bss_optim"
+               "reassoc_same_bss_optim", "wps_priority"
        };
        int i, num_fields = ARRAY_SIZE(fields);
 
@@ -967,12 +982,12 @@ static int wpa_cli_cmd_wps_reg(struct wpa_ctrl *ctrl, int argc, char *argv[])
                res = os_snprintf(cmd, sizeof(cmd), "WPS_REG %s %s",
                                  argv[0], argv[1]);
        else if (argc == 5 || argc == 6) {
-               char ssid_hex[2 * 32 + 1];
+               char ssid_hex[2 * SSID_MAX_LEN + 1];
                char key_hex[2 * 64 + 1];
                int i;
 
                ssid_hex[0] = '\0';
-               for (i = 0; i < 32; i++) {
+               for (i = 0; i < SSID_MAX_LEN; i++) {
                        if (argv[2][i] == '\0')
                                break;
                        os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
@@ -1096,12 +1111,12 @@ static int wpa_cli_cmd_wps_er_config(struct wpa_ctrl *ctrl, int argc,
        int res;
 
        if (argc == 5 || argc == 6) {
-               char ssid_hex[2 * 32 + 1];
+               char ssid_hex[2 * SSID_MAX_LEN + 1];
                char key_hex[2 * 64 + 1];
                int i;
 
                ssid_hex[0] = '\0';
-               for (i = 0; i < 32; i++) {
+               for (i = 0; i < SSID_MAX_LEN; i++) {
                        if (argv[2][i] == '\0')
                                break;
                        os_snprintf(&ssid_hex[i * 2], 3, "%02x", argv[2][i]);
@@ -1456,7 +1471,8 @@ static int wpa_cli_cmd_add_network(struct wpa_ctrl *ctrl, int argc,
                                   char *argv[])
 {
        int res = wpa_ctrl_command(ctrl, "ADD_NETWORK");
-       update_networks(ctrl);
+       if (interactive)
+               update_networks(ctrl);
        return res;
 }
 
@@ -1465,7 +1481,8 @@ static int wpa_cli_cmd_remove_network(struct wpa_ctrl *ctrl, int argc,
                                      char *argv[])
 {
        int res = wpa_cli_cmd(ctrl, "REMOVE_NETWORK", 1, argc, argv);
-       update_networks(ctrl);
+       if (interactive)
+               update_networks(ctrl);
        return res;
 }
 
@@ -2653,6 +2670,13 @@ static int wpa_cli_cmd_tdls_teardown(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static int wpa_cli_cmd_tdls_link_status(struct wpa_ctrl *ctrl, int argc,
+                                       char *argv[])
+{
+       return wpa_cli_cmd(ctrl, "TDLS_LINK_STATUS", 1, argc, argv);
+}
+
+
 static int wpa_cli_cmd_wmm_ac_addts(struct wpa_ctrl *ctrl, int argc,
                                    char *argv[])
 {
@@ -2792,6 +2816,13 @@ static int wpa_cli_cmd_mac_rand_scan(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static int wpa_cli_cmd_get_pref_freq_list(struct wpa_ctrl *ctrl, int argc,
+                                         char *argv[])
+{
+       return wpa_cli_cmd(ctrl, "GET_PREF_FREQ_LIST", 1, argc, argv);
+}
+
+
 enum wpa_cli_cmd_flags {
        cli_cmd_flag_none               = 0x00,
        cli_cmd_flag_sensitive          = 0x01
@@ -2805,7 +2836,7 @@ struct wpa_cli_cmd {
        const char *usage;
 };
 
-static struct wpa_cli_cmd wpa_cli_commands[] = {
+static const struct wpa_cli_cmd wpa_cli_commands[] = {
        { "status", wpa_cli_cmd_status, NULL,
          cli_cmd_flag_none,
          "[verbose] = get current WPA/EAPOL/EAP status" },
@@ -3280,6 +3311,9 @@ static struct wpa_cli_cmd wpa_cli_commands[] = {
        { "tdls_teardown", wpa_cli_cmd_tdls_teardown, NULL,
          cli_cmd_flag_none,
          "<addr> = tear down TDLS with <addr>" },
+       { "tdls_link_status", wpa_cli_cmd_tdls_link_status, NULL,
+         cli_cmd_flag_none,
+         "<addr> = TDLS link status with <addr>" },
        { "wmm_ac_addts", wpa_cli_cmd_wmm_ac_addts, NULL,
          cli_cmd_flag_none,
          "<uplink/downlink/bidi> <tsid=0..7> <up=0..7> [nominal_msdu_size=#] "
@@ -3343,6 +3377,9 @@ static struct wpa_cli_cmd wpa_cli_commands[] = {
          "<scan|sched|pno|all> enable=<0/1> [addr=mac-address "
          "mask=mac-address-mask] = scan MAC randomization"
        },
+       { "get_pref_freq_list", wpa_cli_cmd_get_pref_freq_list, NULL,
+         cli_cmd_flag_none,
+         "<interface type> = retrieve preferred freq list for the specified interface type" },
        { NULL, NULL, NULL, cli_cmd_flag_none, NULL }
 };
 
@@ -3350,7 +3387,7 @@ static struct wpa_cli_cmd wpa_cli_commands[] = {
 /*
  * Prints command usage, lines are padded with the specified string.
  */
-static void print_cmd_help(struct wpa_cli_cmd *cmd, const char *pad)
+static void print_cmd_help(const struct wpa_cli_cmd *cmd, const char *pad)
 {
        char c;
        size_t n;
@@ -3488,7 +3525,7 @@ static char ** wpa_cli_edit_completion_cb(void *ctx, const char *str, int pos)
 
 static int wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
 {
-       struct wpa_cli_cmd *cmd, *match = NULL;
+       const struct wpa_cli_cmd *cmd, *match = NULL;
        int count;
        int ret = 0;
 
@@ -3573,6 +3610,9 @@ static void wpa_cli_action_process(const char *msg)
        const char *ifname = ctrl_ifname;
        char ifname_buf[100];
 
+       if (eloop_terminated())
+               return;
+
        pos = msg;
        if (os_strncmp(pos, "IFNAME=", 7) == 0) {
                const char *end;
@@ -4087,45 +4127,49 @@ static void wpa_cli_interactive(void)
 }
 
 
+static void wpa_cli_action_ping(void *eloop_ctx, void *timeout_ctx)
+{
+       struct wpa_ctrl *ctrl = eloop_ctx;
+       char buf[256];
+       size_t len;
+
+       /* verify that connection is still working */
+       len = sizeof(buf) - 1;
+       if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
+                            wpa_cli_action_cb) < 0 ||
+           len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
+               printf("wpa_supplicant did not reply to PING command - exiting\n");
+               eloop_terminate();
+               return;
+       }
+       eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
+                              ctrl, NULL);
+}
+
+
+static void wpa_cli_action_receive(int sock, void *eloop_ctx, void *sock_ctx)
+{
+       struct wpa_ctrl *ctrl = eloop_ctx;
+
+       wpa_cli_recv_pending(ctrl, 1);
+}
+
+
 static void wpa_cli_action(struct wpa_ctrl *ctrl)
 {
 #ifdef CONFIG_ANSI_C_EXTRA
        /* TODO: ANSI C version(?) */
        printf("Action processing not supported in ANSI C build.\n");
 #else /* CONFIG_ANSI_C_EXTRA */
-       fd_set rfds;
-       int fd, res;
-       struct timeval tv;
-       char buf[256]; /* note: large enough to fit in unsolicited messages */
-       size_t len;
+       int fd;
 
        fd = wpa_ctrl_get_fd(ctrl);
-
-       while (!wpa_cli_quit) {
-               FD_ZERO(&rfds);
-               FD_SET(fd, &rfds);
-               tv.tv_sec = ping_interval;
-               tv.tv_usec = 0;
-               res = select(fd + 1, &rfds, NULL, NULL, &tv);
-               if (res < 0 && errno != EINTR) {
-                       perror("select");
-                       break;
-               }
-
-               if (FD_ISSET(fd, &rfds))
-                       wpa_cli_recv_pending(ctrl, 1);
-               else {
-                       /* verify that connection is still working */
-                       len = sizeof(buf) - 1;
-                       if (wpa_ctrl_request(ctrl, "PING", 4, buf, &len,
-                                            wpa_cli_action_cb) < 0 ||
-                           len < 4 || os_memcmp(buf, "PONG", 4) != 0) {
-                               printf("wpa_supplicant did not reply to PING "
-                                      "command - exiting\n");
-                               break;
-                       }
-               }
-       }
+       eloop_register_timeout(ping_interval, 0, wpa_cli_action_ping,
+                              ctrl, NULL);
+       eloop_register_read_sock(fd, wpa_cli_action_receive, ctrl, NULL);
+       eloop_run();
+       eloop_cancel_timeout(wpa_cli_action_ping, ctrl, NULL);
+       eloop_unregister_read_sock(fd);
 #endif /* CONFIG_ANSI_C_EXTRA */
 }
 
@@ -4150,18 +4194,17 @@ static char * wpa_cli_get_default_ifname(void)
 {
        char *ifname = NULL;
 
+#ifdef ANDROID
+       char ifprop[PROPERTY_VALUE_MAX];
+       if (property_get("wifi.interface", ifprop, NULL) != 0) {
+               ifname = os_strdup(ifprop);
+               printf("Using interface '%s'\n", ifname ? ifname : "N/A");
+       }
+#else /* ANDROID */
 #ifdef CONFIG_CTRL_IFACE_UNIX
        struct dirent *dent;
        DIR *dir = opendir(ctrl_iface_dir);
        if (!dir) {
-#ifdef ANDROID
-               char ifprop[PROPERTY_VALUE_MAX];
-               if (property_get("wifi.interface", ifprop, NULL) != 0) {
-                       ifname = os_strdup(ifprop);
-                       printf("Using interface '%s'\n", ifname);
-                       return ifname;
-               }
-#endif /* ANDROID */
                return NULL;
        }
        while ((dent = readdir(dir))) {
@@ -4205,6 +4248,7 @@ static char * wpa_cli_get_default_ifname(void)
        }
        wpa_ctrl_close(ctrl);
 #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */
+#endif /* ANDROID */
 
        return ifname;
 }
@@ -4221,7 +4265,7 @@ int main(int argc, char *argv[])
                return -1;
 
        for (;;) {
-               c = getopt(argc, argv, "a:Bg:G:hi:p:P:v");
+               c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:v");
                if (c < 0)
                        break;
                switch (c) {
@@ -4253,6 +4297,9 @@ int main(int argc, char *argv[])
                case 'P':
                        pid_file = optarg;
                        break;
+               case 's':
+                       client_socket_dir = optarg;
+                       break;
                default:
                        usage();
                        return -1;