2 * hostapd - command line interface for hostapd daemon
3 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.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.
26 static const char *hostapd_cli_version
=
27 "hostapd_cli v" VERSION_STR
"\n"
28 "Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi> and contributors";
31 static const char *hostapd_cli_license
=
32 "This program is free software. You can distribute it and/or modify it\n"
33 "under the terms of the GNU General Public License version 2.\n"
35 "Alternatively, this software may be distributed under the terms of the\n"
36 "BSD license. See README and COPYING for more details.\n";
38 static const char *hostapd_cli_full_license
=
39 "This program is free software; you can redistribute it and/or modify\n"
40 "it under the terms of the GNU General Public License version 2 as\n"
41 "published by the Free Software Foundation.\n"
43 "This program is distributed in the hope that it will be useful,\n"
44 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
45 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
46 "GNU General Public License for more details.\n"
48 "You should have received a copy of the GNU General Public License\n"
49 "along with this program; if not, write to the Free Software\n"
50 "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
52 "Alternatively, this software may be distributed under the terms of the\n"
55 "Redistribution and use in source and binary forms, with or without\n"
56 "modification, are permitted provided that the following conditions are\n"
59 "1. Redistributions of source code must retain the above copyright\n"
60 " notice, this list of conditions and the following disclaimer.\n"
62 "2. Redistributions in binary form must reproduce the above copyright\n"
63 " notice, this list of conditions and the following disclaimer in the\n"
64 " documentation and/or other materials provided with the distribution.\n"
66 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
67 " names of its contributors may be used to endorse or promote products\n"
68 " derived from this software without specific prior written permission.\n"
70 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
71 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
72 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
73 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
74 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
75 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
76 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
77 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
78 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
79 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
80 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
83 static const char *commands_help
=
85 " mib get MIB variables (dot1x, dot11, radius)\n"
86 " sta <addr> get MIB vatiables for one station\n"
87 " all_sta get MIB variables for all stations\n"
88 " help show this usage help\n"
89 " interface [ifname] show interfaces/select interface\n"
90 " level <debug level> change debug level\n"
91 " license show full hostapd_cli license\n"
92 " quit exit hostapd_cli\n";
94 static struct wpa_ctrl
*ctrl_conn
;
95 static int hostapd_cli_quit
= 0;
96 static int hostapd_cli_attached
= 0;
97 static const char *ctrl_iface_dir
= "/var/run/hostapd";
98 static char *ctrl_ifname
= NULL
;
101 static void usage(void)
103 fprintf(stderr
, "%s\n", hostapd_cli_version
);
106 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hv] "
110 " -h help (show this usage text)\n"
111 " -v shown version information\n"
112 " -p<path> path to find control sockets (default: "
113 "/var/run/hostapd)\n"
114 " -i<ifname> Interface to listen on (default: first "
115 "interface found in the\n"
122 static struct wpa_ctrl
* hostapd_cli_open_connection(const char *ifname
)
130 flen
= strlen(ctrl_iface_dir
) + strlen(ifname
) + 2;
131 cfile
= malloc(flen
);
134 snprintf(cfile
, flen
, "%s/%s", ctrl_iface_dir
, ifname
);
136 ctrl_conn
= wpa_ctrl_open(cfile
);
142 static void hostapd_cli_close_connection(void)
144 if (ctrl_conn
== NULL
)
147 if (hostapd_cli_attached
) {
148 wpa_ctrl_detach(ctrl_conn
);
149 hostapd_cli_attached
= 0;
151 wpa_ctrl_close(ctrl_conn
);
156 static void hostapd_cli_msg_cb(char *msg
, size_t len
)
162 static int _wpa_ctrl_command(struct wpa_ctrl
*ctrl
, char *cmd
, int print
)
168 if (ctrl_conn
== NULL
) {
169 printf("Not connected to hostapd - command dropped.\n");
172 len
= sizeof(buf
) - 1;
173 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
176 printf("'%s' command timed out.\n", cmd
);
178 } else if (ret
< 0) {
179 printf("'%s' command failed.\n", cmd
);
190 static inline int wpa_ctrl_command(struct wpa_ctrl
*ctrl
, char *cmd
)
192 return _wpa_ctrl_command(ctrl
, cmd
, 1);
196 static int hostapd_cli_cmd_ping(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
198 return wpa_ctrl_command(ctrl
, "PING");
202 static int hostapd_cli_cmd_mib(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
204 return wpa_ctrl_command(ctrl
, "MIB");
208 static int hostapd_cli_cmd_sta(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
212 printf("Invalid 'sta' command - exactly one argument, STA "
213 "address, is required.\n");
216 snprintf(buf
, sizeof(buf
), "STA %s", argv
[0]);
217 return wpa_ctrl_command(ctrl
, buf
);
221 static int wpa_ctrl_command_sta(struct wpa_ctrl
*ctrl
, char *cmd
,
222 char *addr
, size_t addr_len
)
224 char buf
[4096], *pos
;
228 if (ctrl_conn
== NULL
) {
229 printf("Not connected to hostapd - command dropped.\n");
232 len
= sizeof(buf
) - 1;
233 ret
= wpa_ctrl_request(ctrl
, cmd
, strlen(cmd
), buf
, &len
,
236 printf("'%s' command timed out.\n", cmd
);
238 } else if (ret
< 0) {
239 printf("'%s' command failed.\n", cmd
);
244 if (memcmp(buf
, "FAIL", 4) == 0)
249 while (*pos
!= '\0' && *pos
!= '\n')
252 snprintf(addr
, addr_len
, "%s", buf
);
257 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl
*ctrl
, int argc
,
260 char addr
[32], cmd
[64];
262 if (wpa_ctrl_command_sta(ctrl
, "STA-FIRST", addr
, sizeof(addr
)))
265 snprintf(cmd
, sizeof(cmd
), "STA-NEXT %s", addr
);
266 } while (wpa_ctrl_command_sta(ctrl
, cmd
, addr
, sizeof(addr
)) == 0);
272 static int hostapd_cli_cmd_help(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
274 printf("%s", commands_help
);
279 static int hostapd_cli_cmd_license(struct wpa_ctrl
*ctrl
, int argc
,
282 printf("%s\n\n%s\n", hostapd_cli_version
, hostapd_cli_full_license
);
287 static int hostapd_cli_cmd_quit(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
289 hostapd_cli_quit
= 1;
294 static int hostapd_cli_cmd_level(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
298 printf("Invalid LEVEL command: needs one argument (debug "
302 snprintf(cmd
, sizeof(cmd
), "LEVEL %s", argv
[0]);
303 return wpa_ctrl_command(ctrl
, cmd
);
307 static void hostapd_cli_list_interfaces(struct wpa_ctrl
*ctrl
)
312 dir
= opendir(ctrl_iface_dir
);
314 printf("Control interface directory '%s' could not be "
315 "openned.\n", ctrl_iface_dir
);
319 printf("Available interfaces:\n");
320 while ((dent
= readdir(dir
))) {
321 if (strcmp(dent
->d_name
, ".") == 0 ||
322 strcmp(dent
->d_name
, "..") == 0)
324 printf("%s\n", dent
->d_name
);
330 static int hostapd_cli_cmd_interface(struct wpa_ctrl
*ctrl
, int argc
,
334 hostapd_cli_list_interfaces(ctrl
);
338 hostapd_cli_close_connection();
340 ctrl_ifname
= strdup(argv
[0]);
342 if (hostapd_cli_open_connection(ctrl_ifname
)) {
343 printf("Connected to interface '%s.\n", ctrl_ifname
);
344 if (wpa_ctrl_attach(ctrl_conn
) == 0) {
345 hostapd_cli_attached
= 1;
347 printf("Warning: Failed to attach to "
351 printf("Could not connect to interface '%s' - re-trying\n",
358 struct hostapd_cli_cmd
{
360 int (*handler
)(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[]);
363 static struct hostapd_cli_cmd hostapd_cli_commands
[] = {
364 { "ping", hostapd_cli_cmd_ping
},
365 { "mib", hostapd_cli_cmd_mib
},
366 { "sta", hostapd_cli_cmd_sta
},
367 { "all_sta", hostapd_cli_cmd_all_sta
},
368 { "help", hostapd_cli_cmd_help
},
369 { "interface", hostapd_cli_cmd_interface
},
370 { "level", hostapd_cli_cmd_level
},
371 { "license", hostapd_cli_cmd_license
},
372 { "quit", hostapd_cli_cmd_quit
},
377 static void wpa_request(struct wpa_ctrl
*ctrl
, int argc
, char *argv
[])
379 struct hostapd_cli_cmd
*cmd
, *match
= NULL
;
383 cmd
= hostapd_cli_commands
;
385 if (strncasecmp(cmd
->cmd
, argv
[0], strlen(argv
[0])) == 0) {
393 printf("Ambiguous command '%s'; possible commands:", argv
[0]);
394 cmd
= hostapd_cli_commands
;
396 if (strncasecmp(cmd
->cmd
, argv
[0], strlen(argv
[0])) ==
398 printf(" %s", cmd
->cmd
);
403 } else if (count
== 0) {
404 printf("Unknown command '%s'\n", argv
[0]);
406 match
->handler(ctrl
, argc
- 1, &argv
[1]);
411 static void hostapd_cli_recv_pending(struct wpa_ctrl
*ctrl
, int in_read
)
414 if (ctrl_conn
== NULL
)
416 while (wpa_ctrl_pending(ctrl
)) {
418 size_t len
= sizeof(buf
) - 1;
419 if (wpa_ctrl_recv(ctrl
, buf
, &len
) == 0) {
421 if (in_read
&& first
)
426 printf("Could not read pending message.\n");
433 static void hostapd_cli_interactive(void)
435 const int max_args
= 10;
436 char cmd
[256], *res
, *argv
[max_args
], *pos
;
439 printf("\nInteractive mode\n\n");
442 hostapd_cli_recv_pending(ctrl_conn
, 0);
445 res
= fgets(cmd
, sizeof(cmd
), stdin
);
450 while (*pos
!= '\0') {
466 if (argc
== max_args
)
468 while (*pos
!= '\0' && *pos
!= ' ')
474 wpa_request(ctrl_conn
, argc
, argv
);
475 } while (!hostapd_cli_quit
);
479 static void hostapd_cli_terminate(int sig
)
481 hostapd_cli_close_connection();
486 static void hostapd_cli_alarm(int sig
)
488 if (ctrl_conn
&& _wpa_ctrl_command(ctrl_conn
, "PING", 0)) {
489 printf("Connection to hostapd lost - trying to reconnect\n");
490 hostapd_cli_close_connection();
493 ctrl_conn
= hostapd_cli_open_connection(ctrl_ifname
);
495 printf("Connection to hostapd re-established\n");
496 if (wpa_ctrl_attach(ctrl_conn
) == 0) {
497 hostapd_cli_attached
= 1;
499 printf("Warning: Failed to attach to "
505 hostapd_cli_recv_pending(ctrl_conn
, 1);
510 int main(int argc
, char *argv
[])
513 int warning_displayed
= 0;
517 c
= getopt(argc
, argv
, "hi:p:v");
525 printf("%s\n", hostapd_cli_version
);
528 ctrl_ifname
= strdup(optarg
);
531 ctrl_iface_dir
= optarg
;
539 interactive
= argc
== optind
;
542 printf("%s\n\n%s\n\n", hostapd_cli_version
,
543 hostapd_cli_license
);
547 if (ctrl_ifname
== NULL
) {
549 DIR *dir
= opendir(ctrl_iface_dir
);
551 while ((dent
= readdir(dir
))) {
552 if (strcmp(dent
->d_name
, ".") == 0 ||
553 strcmp(dent
->d_name
, "..") == 0)
555 printf("Selected interface '%s'\n",
557 ctrl_ifname
= strdup(dent
->d_name
);
563 ctrl_conn
= hostapd_cli_open_connection(ctrl_ifname
);
565 if (warning_displayed
)
566 printf("Connection established.\n");
571 perror("Failed to connect to hostapd - "
576 if (!warning_displayed
) {
577 printf("Could not connect to hostapd - re-trying\n");
578 warning_displayed
= 1;
584 signal(SIGINT
, hostapd_cli_terminate
);
585 signal(SIGTERM
, hostapd_cli_terminate
);
586 signal(SIGALRM
, hostapd_cli_alarm
);
589 if (wpa_ctrl_attach(ctrl_conn
) == 0) {
590 hostapd_cli_attached
= 1;
592 printf("Warning: Failed to attach to hostapd.\n");
594 hostapd_cli_interactive();
596 wpa_request(ctrl_conn
, argc
- optind
, &argv
[optind
]);
599 hostapd_cli_close_connection();