hostapd_cli.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 #include <dirent.h>
00018 
00019 #include "wpa_ctrl.h"
00020 #include "common.h"
00021 #include "version.h"
00022 
00023 
00024 static const char *hostapd_cli_version =
00025 "hostapd_cli v" VERSION_STR "\n"
00026 "Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> and contributors";
00027 
00028 
00029 static const char *hostapd_cli_license =
00030 "This program is free software. You can distribute it and/or modify it\n"
00031 "under the terms of the GNU General Public License version 2.\n"
00032 "\n"
00033 "Alternatively, this software may be distributed under the terms of the\n"
00034 "BSD license. See README and COPYING for more details.\n";
00035 
00036 static const char *hostapd_cli_full_license =
00037 "This program is free software; you can redistribute it and/or modify\n"
00038 "it under the terms of the GNU General Public License version 2 as\n"
00039 "published by the Free Software Foundation.\n"
00040 "\n"
00041 "This program is distributed in the hope that it will be useful,\n"
00042 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
00043 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
00044 "GNU General Public License for more details.\n"
00045 "\n"
00046 "You should have received a copy of the GNU General Public License\n"
00047 "along with this program; if not, write to the Free Software\n"
00048 "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n"
00049 "\n"
00050 "Alternatively, this software may be distributed under the terms of the\n"
00051 "BSD license.\n"
00052 "\n"
00053 "Redistribution and use in source and binary forms, with or without\n"
00054 "modification, are permitted provided that the following conditions are\n"
00055 "met:\n"
00056 "\n"
00057 "1. Redistributions of source code must retain the above copyright\n"
00058 "   notice, this list of conditions and the following disclaimer.\n"
00059 "\n"
00060 "2. Redistributions in binary form must reproduce the above copyright\n"
00061 "   notice, this list of conditions and the following disclaimer in the\n"
00062 "   documentation and/or other materials provided with the distribution.\n"
00063 "\n"
00064 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
00065 "   names of its contributors may be used to endorse or promote products\n"
00066 "   derived from this software without specific prior written permission.\n"
00067 "\n"
00068 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
00069 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
00070 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
00071 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
00072 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
00073 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
00074 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
00075 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
00076 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
00077 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
00078 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
00079 "\n";
00080 
00081 static const char *commands_help =
00082 "Commands:\n"
00083 "   mib                  get MIB variables (dot1x, dot11, radius)\n"
00084 "   sta <addr>           get MIB variables for one station\n"
00085 "   all_sta              get MIB variables for all stations\n"
00086 "   new_sta <addr>       add a new station\n"
00087 #ifdef CONFIG_IEEE80211W
00088 "   sa_query <addr>      send SA Query to a station\n"
00089 #endif /* CONFIG_IEEE80211W */
00090 #ifdef CONFIG_WPS
00091 "   wps_pin <uuid> <pin> [timeout]  add WPS Enrollee PIN (Device Password)\n"
00092 "   wps_pbc              indicate button pushed to initiate PBC\n"
00093 #ifdef CONFIG_WPS_OOB
00094 "   wps_oob <type> <path> <method>  use WPS with out-of-band (UFD)\n"
00095 #endif /* CONFIG_WPS_OOB */
00096 #endif /* CONFIG_WPS */
00097 "   help                 show this usage help\n"
00098 "   interface [ifname]   show interfaces/select interface\n"
00099 "   level <debug level>  change debug level\n"
00100 "   license              show full hostapd_cli license\n"
00101 "   quit                 exit hostapd_cli\n";
00102 
00103 static struct wpa_ctrl *ctrl_conn;
00104 static int hostapd_cli_quit = 0;
00105 static int hostapd_cli_attached = 0;
00106 static const char *ctrl_iface_dir = "/var/run/hostapd";
00107 static char *ctrl_ifname = NULL;
00108 static int ping_interval = 5;
00109 
00110 
00111 static void usage(void)
00112 {
00113         fprintf(stderr, "%s\n", hostapd_cli_version);
00114         fprintf(stderr, 
00115                 "\n"    
00116                 "usage: hostapd_cli [-p<path>] [-i<ifname>] [-hv] "
00117                 "[-G<ping interval>] \\\n"
00118                 "        [command..]\n"
00119                 "\n"
00120                 "Options:\n"
00121                 "   -h           help (show this usage text)\n"
00122                 "   -v           shown version information\n"
00123                 "   -p<path>     path to find control sockets (default: "
00124                 "/var/run/hostapd)\n"
00125                 "   -i<ifname>   Interface to listen on (default: first "
00126                 "interface found in the\n"
00127                 "                socket path)\n\n"
00128                 "%s",
00129                 commands_help);
00130 }
00131 
00132 
00133 static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname)
00134 {
00135         char *cfile;
00136         int flen;
00137 
00138         if (ifname == NULL)
00139                 return NULL;
00140 
00141         flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2;
00142         cfile = malloc(flen);
00143         if (cfile == NULL)
00144                 return NULL;
00145         snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname);
00146 
00147         ctrl_conn = wpa_ctrl_open(cfile);
00148         free(cfile);
00149         return ctrl_conn;
00150 }
00151 
00152 
00153 static void hostapd_cli_close_connection(void)
00154 {
00155         if (ctrl_conn == NULL)
00156                 return;
00157 
00158         if (hostapd_cli_attached) {
00159                 wpa_ctrl_detach(ctrl_conn);
00160                 hostapd_cli_attached = 0;
00161         }
00162         wpa_ctrl_close(ctrl_conn);
00163         ctrl_conn = NULL;
00164 }
00165 
00166 
00167 static void hostapd_cli_msg_cb(char *msg, size_t len)
00168 {
00169         printf("%s\n", msg);
00170 }
00171 
00172 
00173 static int _wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd, int print)
00174 {
00175         char buf[4096];
00176         size_t len;
00177         int ret;
00178 
00179         if (ctrl_conn == NULL) {
00180                 printf("Not connected to hostapd - command dropped.\n");
00181                 return -1;
00182         }
00183         len = sizeof(buf) - 1;
00184         ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
00185                                hostapd_cli_msg_cb);
00186         if (ret == -2) {
00187                 printf("'%s' command timed out.\n", cmd);
00188                 return -2;
00189         } else if (ret < 0) {
00190                 printf("'%s' command failed.\n", cmd);
00191                 return -1;
00192         }
00193         if (print) {
00194                 buf[len] = '\0';
00195                 printf("%s", buf);
00196         }
00197         return 0;
00198 }
00199 
00200 
00201 static inline int wpa_ctrl_command(struct wpa_ctrl *ctrl, char *cmd)
00202 {
00203         return _wpa_ctrl_command(ctrl, cmd, 1);
00204 }
00205 
00206 
00207 static int hostapd_cli_cmd_ping(struct wpa_ctrl *ctrl, int argc, char *argv[])
00208 {
00209         return wpa_ctrl_command(ctrl, "PING");
00210 }
00211 
00212 
00213 static int hostapd_cli_cmd_mib(struct wpa_ctrl *ctrl, int argc, char *argv[])
00214 {
00215         return wpa_ctrl_command(ctrl, "MIB");
00216 }
00217 
00218 
00219 static int hostapd_cli_cmd_sta(struct wpa_ctrl *ctrl, int argc, char *argv[])
00220 {
00221         char buf[64];
00222         if (argc != 1) {
00223                 printf("Invalid 'sta' command - exactly one argument, STA "
00224                        "address, is required.\n");
00225                 return -1;
00226         }
00227         snprintf(buf, sizeof(buf), "STA %s", argv[0]);
00228         return wpa_ctrl_command(ctrl, buf);
00229 }
00230 
00231 
00232 static int hostapd_cli_cmd_new_sta(struct wpa_ctrl *ctrl, int argc,
00233                                    char *argv[])
00234 {
00235         char buf[64];
00236         if (argc != 1) {
00237                 printf("Invalid 'new_sta' command - exactly one argument, STA "
00238                        "address, is required.\n");
00239                 return -1;
00240         }
00241         snprintf(buf, sizeof(buf), "NEW_STA %s", argv[0]);
00242         return wpa_ctrl_command(ctrl, buf);
00243 }
00244 
00245 
00246 #ifdef CONFIG_IEEE80211W
00247 static int hostapd_cli_cmd_sa_query(struct wpa_ctrl *ctrl, int argc,
00248                                     char *argv[])
00249 {
00250         char buf[64];
00251         if (argc != 1) {
00252                 printf("Invalid 'sa_query' command - exactly one argument, "
00253                        "STA address, is required.\n");
00254                 return -1;
00255         }
00256         snprintf(buf, sizeof(buf), "SA_QUERY %s", argv[0]);
00257         return wpa_ctrl_command(ctrl, buf);
00258 }
00259 #endif /* CONFIG_IEEE80211W */
00260 
00261 
00262 #ifdef CONFIG_WPS
00263 static int hostapd_cli_cmd_wps_pin(struct wpa_ctrl *ctrl, int argc,
00264                                    char *argv[])
00265 {
00266         char buf[64];
00267         if (argc < 2) {
00268                 printf("Invalid 'wps_pin' command - at least two arguments, "
00269                        "UUID and PIN, are required.\n");
00270                 return -1;
00271         }
00272         if (argc > 2)
00273                 snprintf(buf, sizeof(buf), "WPS_PIN %s %s %s",
00274                          argv[0], argv[1], argv[2]);
00275         else
00276                 snprintf(buf, sizeof(buf), "WPS_PIN %s %s", argv[0], argv[1]);
00277         return wpa_ctrl_command(ctrl, buf);
00278 }
00279 
00280 
00281 static int hostapd_cli_cmd_wps_pbc(struct wpa_ctrl *ctrl, int argc,
00282                                    char *argv[])
00283 {
00284         return wpa_ctrl_command(ctrl, "WPS_PBC");
00285 }
00286 
00287 
00288 #ifdef CONFIG_WPS_OOB
00289 static int hostapd_cli_cmd_wps_oob(struct wpa_ctrl *ctrl, int argc,
00290                                    char *argv[])
00291 {
00292         char cmd[256];
00293         int res;
00294 
00295         if (argc != 3 && argc != 4) {
00296                 printf("Invalid WPS_OOB command: need three or four "
00297                        "arguments:\n"
00298                        "- DEV_TYPE: use 'ufd' or 'nfc'\n"
00299                        "- PATH: path of OOB device like '/mnt'\n"
00300                        "- METHOD: OOB method 'pin-e' or 'pin-r', "
00301                        "'cred'\n"
00302                        "- DEV_NAME: (only for NFC) device name like "
00303                        "'pn531'\n");
00304                 return -1;
00305         }
00306 
00307         if (argc == 3)
00308                 res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s",
00309                                   argv[0], argv[1], argv[2]);
00310         else
00311                 res = os_snprintf(cmd, sizeof(cmd), "WPS_OOB %s %s %s %s",
00312                                   argv[0], argv[1], argv[2], argv[3]);
00313         if (res < 0 || (size_t) res >= sizeof(cmd) - 1) {
00314                 printf("Too long WPS_OOB command.\n");
00315                 return -1;
00316         }
00317         return wpa_ctrl_command(ctrl, cmd);
00318 }
00319 #endif /* CONFIG_WPS_OOB */
00320 #endif /* CONFIG_WPS */
00321 
00322 
00323 static int wpa_ctrl_command_sta(struct wpa_ctrl *ctrl, char *cmd,
00324                                 char *addr, size_t addr_len)
00325 {
00326         char buf[4096], *pos;
00327         size_t len;
00328         int ret;
00329 
00330         if (ctrl_conn == NULL) {
00331                 printf("Not connected to hostapd - command dropped.\n");
00332                 return -1;
00333         }
00334         len = sizeof(buf) - 1;
00335         ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len,
00336                                hostapd_cli_msg_cb);
00337         if (ret == -2) {
00338                 printf("'%s' command timed out.\n", cmd);
00339                 return -2;
00340         } else if (ret < 0) {
00341                 printf("'%s' command failed.\n", cmd);
00342                 return -1;
00343         }
00344 
00345         buf[len] = '\0';
00346         if (memcmp(buf, "FAIL", 4) == 0)
00347                 return -1;
00348         printf("%s", buf);
00349 
00350         pos = buf;
00351         while (*pos != '\0' && *pos != '\n')
00352                 pos++;
00353         *pos = '\0';
00354         os_strlcpy(addr, buf, addr_len);
00355         return 0;
00356 }
00357 
00358 
00359 static int hostapd_cli_cmd_all_sta(struct wpa_ctrl *ctrl, int argc,
00360                                    char *argv[])
00361 {
00362         char addr[32], cmd[64];
00363 
00364         if (wpa_ctrl_command_sta(ctrl, "STA-FIRST", addr, sizeof(addr)))
00365                 return 0;
00366         do {
00367                 snprintf(cmd, sizeof(cmd), "STA-NEXT %s", addr);
00368         } while (wpa_ctrl_command_sta(ctrl, cmd, addr, sizeof(addr)) == 0);
00369 
00370         return -1;
00371 }
00372 
00373 
00374 static int hostapd_cli_cmd_help(struct wpa_ctrl *ctrl, int argc, char *argv[])
00375 {
00376         printf("%s", commands_help);
00377         return 0;
00378 }
00379 
00380 
00381 static int hostapd_cli_cmd_license(struct wpa_ctrl *ctrl, int argc,
00382                                    char *argv[])
00383 {
00384         printf("%s\n\n%s\n", hostapd_cli_version, hostapd_cli_full_license);
00385         return 0;
00386 }
00387 
00388 
00389 static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[])
00390 {
00391         hostapd_cli_quit = 1;
00392         return 0;
00393 }
00394 
00395 
00396 static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[])
00397 {
00398         char cmd[256];
00399         if (argc != 1) {
00400                 printf("Invalid LEVEL command: needs one argument (debug "
00401                        "level)\n");
00402                 return 0;
00403         }
00404         snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]);
00405         return wpa_ctrl_command(ctrl, cmd);
00406 }
00407 
00408 
00409 static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl)
00410 {
00411         struct dirent *dent;
00412         DIR *dir;
00413 
00414         dir = opendir(ctrl_iface_dir);
00415         if (dir == NULL) {
00416                 printf("Control interface directory '%s' could not be "
00417                        "openned.\n", ctrl_iface_dir);
00418                 return;
00419         }
00420 
00421         printf("Available interfaces:\n");
00422         while ((dent = readdir(dir))) {
00423                 if (strcmp(dent->d_name, ".") == 0 ||
00424                     strcmp(dent->d_name, "..") == 0)
00425                         continue;
00426                 printf("%s\n", dent->d_name);
00427         }
00428         closedir(dir);
00429 }
00430 
00431 
00432 static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc,
00433                                      char *argv[])
00434 {
00435         if (argc < 1) {
00436                 hostapd_cli_list_interfaces(ctrl);
00437                 return 0;
00438         }
00439 
00440         hostapd_cli_close_connection();
00441         free(ctrl_ifname);
00442         ctrl_ifname = strdup(argv[0]);
00443 
00444         if (hostapd_cli_open_connection(ctrl_ifname)) {
00445                 printf("Connected to interface '%s.\n", ctrl_ifname);
00446                 if (wpa_ctrl_attach(ctrl_conn) == 0) {
00447                         hostapd_cli_attached = 1;
00448                 } else {
00449                         printf("Warning: Failed to attach to "
00450                                "hostapd.\n");
00451                 }
00452         } else {
00453                 printf("Could not connect to interface '%s' - re-trying\n",
00454                         ctrl_ifname);
00455         }
00456         return 0;
00457 }
00458 
00459 
00460 struct hostapd_cli_cmd {
00461         const char *cmd;
00462         int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);
00463 };
00464 
00465 static struct hostapd_cli_cmd hostapd_cli_commands[] = {
00466         { "ping", hostapd_cli_cmd_ping },
00467         { "mib", hostapd_cli_cmd_mib },
00468         { "sta", hostapd_cli_cmd_sta },
00469         { "all_sta", hostapd_cli_cmd_all_sta },
00470         { "new_sta", hostapd_cli_cmd_new_sta },
00471 #ifdef CONFIG_IEEE80211W
00472         { "sa_query", hostapd_cli_cmd_sa_query },
00473 #endif /* CONFIG_IEEE80211W */
00474 #ifdef CONFIG_WPS
00475         { "wps_pin", hostapd_cli_cmd_wps_pin },
00476         { "wps_pbc", hostapd_cli_cmd_wps_pbc },
00477 #ifdef CONFIG_WPS_OOB
00478         { "wps_oob", hostapd_cli_cmd_wps_oob },
00479 #endif /* CONFIG_WPS_OOB */
00480 #endif /* CONFIG_WPS */
00481         { "help", hostapd_cli_cmd_help },
00482         { "interface", hostapd_cli_cmd_interface },
00483         { "level", hostapd_cli_cmd_level },
00484         { "license", hostapd_cli_cmd_license },
00485         { "quit", hostapd_cli_cmd_quit },
00486         { NULL, NULL }
00487 };
00488 
00489 
00490 static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[])
00491 {
00492         struct hostapd_cli_cmd *cmd, *match = NULL;
00493         int count;
00494 
00495         count = 0;
00496         cmd = hostapd_cli_commands;
00497         while (cmd->cmd) {
00498                 if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) {
00499                         match = cmd;
00500                         count++;
00501                 }
00502                 cmd++;
00503         }
00504 
00505         if (count > 1) {
00506                 printf("Ambiguous command '%s'; possible commands:", argv[0]);
00507                 cmd = hostapd_cli_commands;
00508                 while (cmd->cmd) {
00509                         if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) ==
00510                             0) {
00511                                 printf(" %s", cmd->cmd);
00512                         }
00513                         cmd++;
00514                 }
00515                 printf("\n");
00516         } else if (count == 0) {
00517                 printf("Unknown command '%s'\n", argv[0]);
00518         } else {
00519                 match->handler(ctrl, argc - 1, &argv[1]);
00520         }
00521 }
00522 
00523 
00524 static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read)
00525 {
00526         int first = 1;
00527         if (ctrl_conn == NULL)
00528                 return;
00529         while (wpa_ctrl_pending(ctrl)) {
00530                 char buf[256];
00531                 size_t len = sizeof(buf) - 1;
00532                 if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
00533                         buf[len] = '\0';
00534                         if (in_read && first)
00535                                 printf("\n");
00536                         first = 0;
00537                         printf("%s\n", buf);
00538                 } else {
00539                         printf("Could not read pending message.\n");
00540                         break;
00541                 }
00542         }
00543 }
00544 
00545 
00546 static void hostapd_cli_interactive(void)
00547 {
00548         const int max_args = 10;
00549         char cmd[256], *res, *argv[max_args], *pos;
00550         int argc;
00551 
00552         printf("\nInteractive mode\n\n");
00553 
00554         do {
00555                 hostapd_cli_recv_pending(ctrl_conn, 0);
00556                 printf("> ");
00557                 alarm(ping_interval);
00558                 res = fgets(cmd, sizeof(cmd), stdin);
00559                 alarm(0);
00560                 if (res == NULL)
00561                         break;
00562                 pos = cmd;
00563                 while (*pos != '\0') {
00564                         if (*pos == '\n') {
00565                                 *pos = '\0';
00566                                 break;
00567                         }
00568                         pos++;
00569                 }
00570                 argc = 0;
00571                 pos = cmd;
00572                 for (;;) {
00573                         while (*pos == ' ')
00574                                 pos++;
00575                         if (*pos == '\0')
00576                                 break;
00577                         argv[argc] = pos;
00578                         argc++;
00579                         if (argc == max_args)
00580                                 break;
00581                         while (*pos != '\0' && *pos != ' ')
00582                                 pos++;
00583                         if (*pos == ' ')
00584                                 *pos++ = '\0';
00585                 }
00586                 if (argc)
00587                         wpa_request(ctrl_conn, argc, argv);
00588         } while (!hostapd_cli_quit);
00589 }
00590 
00591 
00592 static void hostapd_cli_terminate(int sig)
00593 {
00594         hostapd_cli_close_connection();
00595         exit(0);
00596 }
00597 
00598 
00599 static void hostapd_cli_alarm(int sig)
00600 {
00601         if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
00602                 printf("Connection to hostapd lost - trying to reconnect\n");
00603                 hostapd_cli_close_connection();
00604         }
00605         if (!ctrl_conn) {
00606                 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
00607                 if (ctrl_conn) {
00608                         printf("Connection to hostapd re-established\n");
00609                         if (wpa_ctrl_attach(ctrl_conn) == 0) {
00610                                 hostapd_cli_attached = 1;
00611                         } else {
00612                                 printf("Warning: Failed to attach to "
00613                                        "hostapd.\n");
00614                         }
00615                 }
00616         }
00617         if (ctrl_conn)
00618                 hostapd_cli_recv_pending(ctrl_conn, 1);
00619         alarm(ping_interval);
00620 }
00621 
00622 
00623 int main(int argc, char *argv[])
00624 {
00625         int interactive;
00626         int warning_displayed = 0;
00627         int c;
00628 
00629         for (;;) {
00630                 c = getopt(argc, argv, "hG:i:p:v");
00631                 if (c < 0)
00632                         break;
00633                 switch (c) {
00634                 case 'G':
00635                         ping_interval = atoi(optarg);
00636                         break;
00637                 case 'h':
00638                         usage();
00639                         return 0;
00640                 case 'v':
00641                         printf("%s\n", hostapd_cli_version);
00642                         return 0;
00643                 case 'i':
00644                         free(ctrl_ifname);
00645                         ctrl_ifname = strdup(optarg);
00646                         break;
00647                 case 'p':
00648                         ctrl_iface_dir = optarg;
00649                         break;
00650                 default:
00651                         usage();
00652                         return -1;
00653                 }
00654         }
00655 
00656         interactive = argc == optind;
00657 
00658         if (interactive) {
00659                 printf("%s\n\n%s\n\n", hostapd_cli_version,
00660                        hostapd_cli_license);
00661         }
00662 
00663         for (;;) {
00664                 if (ctrl_ifname == NULL) {
00665                         struct dirent *dent;
00666                         DIR *dir = opendir(ctrl_iface_dir);
00667                         if (dir) {
00668                                 while ((dent = readdir(dir))) {
00669                                         if (strcmp(dent->d_name, ".") == 0 ||
00670                                             strcmp(dent->d_name, "..") == 0)
00671                                                 continue;
00672                                         printf("Selected interface '%s'\n",
00673                                                dent->d_name);
00674                                         ctrl_ifname = strdup(dent->d_name);
00675                                         break;
00676                                 }
00677                                 closedir(dir);
00678                         }
00679                 }
00680                 ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
00681                 if (ctrl_conn) {
00682                         if (warning_displayed)
00683                                 printf("Connection established.\n");
00684                         break;
00685                 }
00686 
00687                 if (!interactive) {
00688                         perror("Failed to connect to hostapd - "
00689                                "wpa_ctrl_open");
00690                         return -1;
00691                 }
00692 
00693                 if (!warning_displayed) {
00694                         printf("Could not connect to hostapd - re-trying\n");
00695                         warning_displayed = 1;
00696                 }
00697                 sleep(1);
00698                 continue;
00699         }
00700 
00701         signal(SIGINT, hostapd_cli_terminate);
00702         signal(SIGTERM, hostapd_cli_terminate);
00703         signal(SIGALRM, hostapd_cli_alarm);
00704 
00705         if (interactive) {
00706                 if (wpa_ctrl_attach(ctrl_conn) == 0) {
00707                         hostapd_cli_attached = 1;
00708                 } else {
00709                         printf("Warning: Failed to attach to hostapd.\n");
00710                 }
00711                 hostapd_cli_interactive();
00712         } else
00713                 wpa_request(ctrl_conn, argc - optind, &argv[optind]);
00714 
00715         free(ctrl_ifname);
00716         hostapd_cli_close_connection();
00717         return 0;
00718 }
00719 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:48 2009 for hostapd by  doxygen 1.6.1