📄 hostapd_cli.c
字号:
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); return 0;}static int hostapd_cli_cmd_quit(struct wpa_ctrl *ctrl, int argc, char *argv[]){ hostapd_cli_quit = 1; return 0;}static int hostapd_cli_cmd_level(struct wpa_ctrl *ctrl, int argc, char *argv[]){ char cmd[256]; if (argc != 1) { printf("Invalid LEVEL command: needs one argument (debug " "level)\n"); return 0; } snprintf(cmd, sizeof(cmd), "LEVEL %s", argv[0]); return wpa_ctrl_command(ctrl, cmd);}static void hostapd_cli_list_interfaces(struct wpa_ctrl *ctrl){ struct dirent *dent; DIR *dir; dir = opendir(ctrl_iface_dir); if (dir == NULL) { printf("Control interface directory '%s' could not be " "openned.\n", ctrl_iface_dir); return; } printf("Available interfaces:\n"); while ((dent = readdir(dir))) { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; printf("%s\n", dent->d_name); } closedir(dir);}static int hostapd_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[]){ if (argc < 1) { hostapd_cli_list_interfaces(ctrl); return 0; } hostapd_cli_close_connection(); free(ctrl_ifname); ctrl_ifname = strdup(argv[0]); if (hostapd_cli_open_connection(ctrl_ifname)) { printf("Connected to interface '%s.\n", ctrl_ifname); if (wpa_ctrl_attach(ctrl_conn) == 0) { hostapd_cli_attached = 1; } else { printf("Warning: Failed to attach to " "hostapd.\n"); } } else { printf("Could not connect to interface '%s' - re-trying\n", ctrl_ifname); } return 0;}struct hostapd_cli_cmd { const char *cmd; int (*handler)(struct wpa_ctrl *ctrl, int argc, char *argv[]);};static struct hostapd_cli_cmd hostapd_cli_commands[] = { { "ping", hostapd_cli_cmd_ping }, { "mib", hostapd_cli_cmd_mib }, { "sta", hostapd_cli_cmd_sta }, { "all_sta", hostapd_cli_cmd_all_sta }, { "new_sta", hostapd_cli_cmd_new_sta },#ifdef CONFIG_IEEE80211W { "sa_query", hostapd_cli_cmd_sa_query },#endif /* CONFIG_IEEE80211W */#ifdef CONFIG_WPS { "wps_pin", hostapd_cli_cmd_wps_pin }, { "wps_pbc", hostapd_cli_cmd_wps_pbc },#endif /* CONFIG_WPS */ { "help", hostapd_cli_cmd_help }, { "interface", hostapd_cli_cmd_interface }, { "level", hostapd_cli_cmd_level }, { "license", hostapd_cli_cmd_license }, { "quit", hostapd_cli_cmd_quit }, { NULL, NULL }};static void wpa_request(struct wpa_ctrl *ctrl, int argc, char *argv[]){ struct hostapd_cli_cmd *cmd, *match = NULL; int count; count = 0; cmd = hostapd_cli_commands; while (cmd->cmd) { if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) { match = cmd; count++; } cmd++; } if (count > 1) { printf("Ambiguous command '%s'; possible commands:", argv[0]); cmd = hostapd_cli_commands; while (cmd->cmd) { if (strncasecmp(cmd->cmd, argv[0], strlen(argv[0])) == 0) { printf(" %s", cmd->cmd); } cmd++; } printf("\n"); } else if (count == 0) { printf("Unknown command '%s'\n", argv[0]); } else { match->handler(ctrl, argc - 1, &argv[1]); }}static void hostapd_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read){ int first = 1; if (ctrl_conn == NULL) return; while (wpa_ctrl_pending(ctrl)) { char buf[256]; size_t len = sizeof(buf) - 1; if (wpa_ctrl_recv(ctrl, buf, &len) == 0) { buf[len] = '\0'; if (in_read && first) printf("\n"); first = 0; printf("%s\n", buf); } else { printf("Could not read pending message.\n"); break; } }}static void hostapd_cli_interactive(void){ const int max_args = 10; char cmd[256], *res, *argv[max_args], *pos; int argc; printf("\nInteractive mode\n\n"); do { hostapd_cli_recv_pending(ctrl_conn, 0); printf("> "); alarm(ping_interval); res = fgets(cmd, sizeof(cmd), stdin); alarm(0); if (res == NULL) break; pos = cmd; while (*pos != '\0') { if (*pos == '\n') { *pos = '\0'; break; } pos++; } argc = 0; pos = cmd; for (;;) { while (*pos == ' ') pos++; if (*pos == '\0') break; argv[argc] = pos; argc++; if (argc == max_args) break; while (*pos != '\0' && *pos != ' ') pos++; if (*pos == ' ') *pos++ = '\0'; } if (argc) wpa_request(ctrl_conn, argc, argv); } while (!hostapd_cli_quit);}static void hostapd_cli_terminate(int sig){ hostapd_cli_close_connection(); exit(0);}static void hostapd_cli_alarm(int sig){ if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) { printf("Connection to hostapd lost - trying to reconnect\n"); hostapd_cli_close_connection(); } if (!ctrl_conn) { ctrl_conn = hostapd_cli_open_connection(ctrl_ifname); if (ctrl_conn) { printf("Connection to hostapd re-established\n"); if (wpa_ctrl_attach(ctrl_conn) == 0) { hostapd_cli_attached = 1; } else { printf("Warning: Failed to attach to " "hostapd.\n"); } } } if (ctrl_conn) hostapd_cli_recv_pending(ctrl_conn, 1); alarm(ping_interval);}int main(int argc, char *argv[]){ int interactive; int warning_displayed = 0; int c; for (;;) { c = getopt(argc, argv, "hG:i:p:v"); if (c < 0) break; switch (c) { case 'G': ping_interval = atoi(optarg); break; case 'h': usage(); return 0; case 'v': printf("%s\n", hostapd_cli_version); return 0; case 'i': free(ctrl_ifname); ctrl_ifname = strdup(optarg); break; case 'p': ctrl_iface_dir = optarg; break; default: usage(); return -1; } } interactive = argc == optind; if (interactive) { printf("%s\n\n%s\n\n", hostapd_cli_version, hostapd_cli_license); } for (;;) { if (ctrl_ifname == NULL) { struct dirent *dent; DIR *dir = opendir(ctrl_iface_dir); if (dir) { while ((dent = readdir(dir))) { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; printf("Selected interface '%s'\n", dent->d_name); ctrl_ifname = strdup(dent->d_name); break; } closedir(dir); } } ctrl_conn = hostapd_cli_open_connection(ctrl_ifname); if (ctrl_conn) { if (warning_displayed) printf("Connection established.\n"); break; } if (!interactive) { perror("Failed to connect to hostapd - " "wpa_ctrl_open"); return -1; } if (!warning_displayed) { printf("Could not connect to hostapd - re-trying\n"); warning_displayed = 1; } sleep(1); continue; } signal(SIGINT, hostapd_cli_terminate); signal(SIGTERM, hostapd_cli_terminate); signal(SIGALRM, hostapd_cli_alarm); if (interactive) { if (wpa_ctrl_attach(ctrl_conn) == 0) { hostapd_cli_attached = 1; } else { printf("Warning: Failed to attach to hostapd.\n"); } hostapd_cli_interactive(); } else wpa_request(ctrl_conn, argc - optind, &argv[optind]); free(ctrl_ifname); hostapd_cli_close_connection(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -