Share a single str_starts() implementation
[mech_eap.git] / hostapd / hostapd_cli.c
index 2a45ca4..376a025 100644 (file)
@@ -15,6 +15,7 @@
 #include "utils/eloop.h"
 #include "utils/edit.h"
 #include "common/version.h"
+#include "common/cli.h"
 
 #ifndef CONFIG_NO_CTRL_IFACE
 
@@ -22,42 +23,6 @@ static const char *const hostapd_cli_version =
 "hostapd_cli v" VERSION_STR "\n"
 "Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi> and contributors";
 
-
-static const char *const hostapd_cli_license =
-"This software may be distributed under the terms of the BSD license.\n"
-"See README for more details.\n";
-
-static const char *const hostapd_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"
-"modification, are permitted provided that the following conditions are\n"
-"met:\n"
-"\n"
-"1. Redistributions of source code must retain the above copyright\n"
-"   notice, this list of conditions and the following disclaimer.\n"
-"\n"
-"2. Redistributions in binary form must reproduce the above copyright\n"
-"   notice, this list of conditions and the following disclaimer in the\n"
-"   documentation and/or other materials provided with the distribution.\n"
-"\n"
-"3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
-"   names of its contributors may be used to endorse or promote products\n"
-"   derived from this software without specific prior written permission.\n"
-"\n"
-"THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
-"\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
-"LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
-"A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
-"OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
-"SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
-"LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
-"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
-"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
-"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
-"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
-"\n";
-
 static struct wpa_ctrl *ctrl_conn;
 static int hostapd_cli_quit = 0;
 static int hostapd_cli_attached = 0;
@@ -73,9 +38,13 @@ static const char *pid_file = NULL;
 static const char *action_file = NULL;
 static int ping_interval = 5;
 static int interactive = 0;
+static int event_handler_registered = 0;
+
+static DEFINE_DL_LIST(stations); /* struct cli_txt_entry */
 
 static void print_help(FILE *stream, const char *cmd);
 static char ** list_cmd_list(void);
+static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx);
 
 
 static void usage(void)
@@ -123,9 +92,27 @@ static int get_cmd_arg_num(const char *str, int pos)
 }
 
 
-static int str_starts(const char *src, const char *match)
+static void register_event_handler(struct wpa_ctrl *ctrl)
 {
-       return os_strncmp(src, match, os_strlen(match)) == 0;
+       if (!ctrl_conn)
+               return;
+       if (interactive) {
+               event_handler_registered =
+                       !eloop_register_read_sock(wpa_ctrl_get_fd(ctrl),
+                                                 hostapd_cli_receive,
+                                                 NULL, NULL);
+       }
+}
+
+
+static void unregister_event_handler(struct wpa_ctrl *ctrl)
+{
+       if (!ctrl_conn)
+               return;
+       if (interactive && event_handler_registered) {
+               eloop_unregister_read_sock(wpa_ctrl_get_fd(ctrl));
+               event_handler_registered = 0;
+       }
 }
 
 
@@ -168,6 +155,7 @@ static void hostapd_cli_close_connection(void)
        if (ctrl_conn == NULL)
                return;
 
+       unregister_event_handler(ctrl_conn);
        if (hostapd_cli_attached) {
                wpa_ctrl_detach(ctrl_conn);
                hostapd_cli_attached = 0;
@@ -378,6 +366,21 @@ static int hostapd_cli_cmd_deauthenticate(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static char ** hostapd_complete_deauthenticate(const char *str, int pos)
+{
+       int arg = get_cmd_arg_num(str, pos);
+       char **res = NULL;
+
+       switch (arg) {
+       case 1:
+               res = cli_txt_list_array(&stations);
+               break;
+       }
+
+       return res;
+}
+
+
 static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
                                        char *argv[])
 {
@@ -396,6 +399,21 @@ static int hostapd_cli_cmd_disassociate(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static char ** hostapd_complete_disassociate(const char *str, int pos)
+{
+       int arg = get_cmd_arg_num(str, pos);
+       char **res = NULL;
+
+       switch (arg) {
+       case 1:
+               res = cli_txt_list_array(&stations);
+               break;
+       }
+
+       return res;
+}
+
+
 #ifdef CONFIG_IEEE80211W
 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
                                    char *argv[])
@@ -791,7 +809,7 @@ static char ** hostapd_cli_complete_help(const char *str, int pos)
 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
                                   char *argv[])
 {
-       printf("%s\n\n%s\n", hostapd_cli_version, hostapd_cli_full_license);
+       printf("%s\n\n%s\n", hostapd_cli_version, cli_full_license);
        return 0;
 }
 
@@ -902,6 +920,28 @@ static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
 }
 
 
+static void hostapd_cli_get_interfaces(struct wpa_ctrl *ctrl,
+                                      struct dl_list *interfaces)
+{
+       struct dirent *dent;
+       DIR *dir;
+
+       if (!ctrl || !interfaces)
+               return;
+       dir = opendir(ctrl_iface_dir);
+       if (dir == NULL)
+               return;
+
+       while ((dent = readdir(dir))) {
+               if (strcmp(dent->d_name, ".") == 0 ||
+                   strcmp(dent->d_name, "..") == 0)
+                       continue;
+               cli_txt_list_add(interfaces, dent->d_name);
+       }
+       closedir(dir);
+}
+
+
 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
 {
        struct dirent *dent;
@@ -943,6 +983,7 @@ static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
                printf("Connected to interface '%s.\n", ctrl_ifname);
                if (wpa_ctrl_attach(ctrl_conn) == 0) {
                        hostapd_cli_attached = 1;
+                       register_event_handler(ctrl_conn);
                } else {
                        printf("Warning: Failed to attach to "
                               "hostapd.\n");
@@ -955,6 +996,24 @@ static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
 }
 
 
+static char ** hostapd_complete_interface(const char *str, int pos)
+{
+       int arg = get_cmd_arg_num(str, pos);
+       char **res = NULL;
+       DEFINE_DL_LIST(interfaces);
+
+       switch (arg) {
+       case 1:
+               hostapd_cli_get_interfaces(ctrl_conn, &interfaces);
+               res = cli_txt_list_array(&interfaces);
+               cli_txt_list_flush(&interfaces);
+               break;
+       }
+
+       return res;
+}
+
+
 static int hostapd_cli_cmd_set(struct wpa_ctrl *ctrl, int argc, char *argv[])
 {
        char cmd[256];
@@ -1254,9 +1313,11 @@ static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
           "= get MIB variables for all stations" },
        { "new_sta", hostapd_cli_cmd_new_sta, NULL,
          "<addr> = add a new station" },
-       { "deauthenticate", hostapd_cli_cmd_deauthenticate, NULL,
+       { "deauthenticate", hostapd_cli_cmd_deauthenticate,
+         hostapd_complete_deauthenticate,
          "<addr> = deauthenticate a station" },
-       { "disassociate", hostapd_cli_cmd_disassociate, NULL,
+       { "disassociate", hostapd_cli_cmd_disassociate,
+         hostapd_complete_disassociate,
          "<addr> = disassociate a station" },
 #ifdef CONFIG_IEEE80211W
        { "sa_query", hostapd_cli_cmd_sa_query, NULL,
@@ -1295,7 +1356,7 @@ static const struct hostapd_cli_cmd hostapd_cli_commands[] = {
          "= show current configuration" },
        { "help", hostapd_cli_cmd_help, hostapd_cli_complete_help,
          "= show this usage help" },
-       { "interface", hostapd_cli_cmd_interface, NULL,
+       { "interface", hostapd_cli_cmd_interface, hostapd_complete_interface,
          "[ifname] = show interfaces/select interface" },
 #ifdef CONFIG_FST
        { "fst", hostapd_cli_cmd_fst, NULL, NULL },
@@ -1403,6 +1464,34 @@ static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
 }
 
 
+static void cli_event(const char *str)
+{
+       const char *start, *s;
+
+       start = os_strchr(str, '>');
+       if (start == NULL)
+               return;
+
+       start++;
+
+       if (str_starts(start, AP_STA_CONNECTED)) {
+               s = os_strchr(start, ' ');
+               if (s == NULL)
+                       return;
+               cli_txt_list_add(&stations, s + 1);
+               return;
+       }
+
+       if (str_starts(start, AP_STA_DISCONNECTED)) {
+               s = os_strchr(start, ' ');
+               if (s == NULL)
+                       return;
+               cli_txt_list_del_addr(&stations, s + 1);
+               return;
+       }
+}
+
+
 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
                                     int action_monitor)
 {
@@ -1417,6 +1506,7 @@ static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
                        if (action_monitor)
                                hostapd_cli_action_process(buf, len);
                        else {
+                               cli_event(buf);
                                if (in_read && first)
                                        printf("\n");
                                first = 0;
@@ -1430,6 +1520,12 @@ static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
 }
 
 
+static void hostapd_cli_receive(int sock, void *eloop_ctx, void *sock_ctx)
+{
+       hostapd_cli_recv_pending(ctrl_conn, 0, 0);
+}
+
+
 #define max_args 10
 
 static int tokenize_cmd(char *cmd, char *argv[])
@@ -1474,6 +1570,7 @@ static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
                        printf("Connection to hostapd re-established\n");
                        if (wpa_ctrl_attach(ctrl_conn) == 0) {
                                hostapd_cli_attached = 1;
+                               register_event_handler(ctrl_conn);
                        } else {
                                printf("Warning: Failed to attach to "
                                       "hostapd.\n");
@@ -1583,6 +1680,7 @@ static void hostapd_cli_interactive(void)
 
        eloop_run();
 
+       cli_txt_list_flush(&stations);
        edit_deinit(NULL, NULL);
        eloop_cancel_timeout(hostapd_cli_ping, NULL, NULL);
 }
@@ -1686,8 +1784,7 @@ int main(int argc, char *argv[])
        interactive = (argc == optind) && (action_file == NULL);
 
        if (interactive) {
-               printf("%s\n\n%s\n\n", hostapd_cli_version,
-                      hostapd_cli_license);
+               printf("%s\n\n%s\n\n", hostapd_cli_version, cli_license);
        }
 
        if (eloop_init())
@@ -1735,6 +1832,7 @@ int main(int argc, char *argv[])
        if (interactive || action_file) {
                if (wpa_ctrl_attach(ctrl_conn) == 0) {
                        hostapd_cli_attached = 1;
+                       register_event_handler(ctrl_conn);
                } else {
                        printf("Warning: Failed to attach to hostapd.\n");
                        if (action_file)
@@ -1752,6 +1850,7 @@ int main(int argc, char *argv[])
        else
                wpa_request(ctrl_conn, argc - optind, &argv[optind]);
 
+       unregister_event_handler(ctrl_conn);
        os_free(ctrl_ifname);
        eloop_destroy();
        hostapd_cli_cleanup();