bool wifi_manager::init(const char* path) { bool ret = false; do { if(_buffer == NULL) _buffer = new char[WIFI_BUFFER_LEN]; _ctrl = wpa_ctrl_open(path); if(!_ctrl) { printf("wifi open %s failed\n",path); break; } _event_ctrl = wpa_ctrl_open(path); if(!_event_ctrl) { printf("wifi open %s failed\n",path); break; } if(wpa_ctrl_attach(_event_ctrl)) break; _is_running = true; if (0 != pthread_create(&_thread, NULL, wifi_manager::_wifi_recv_thread, (void*)this)) { printf("error when create pthread in wifi_manager,%d\n", errno); break; } ret = true; }while(0); return ret; }
int wifi_connect_to_supplicant() { char ifname[256]; char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; /* Make sure supplicant is running */ if (!property_get(SUPP_PROP_NAME, supp_status, NULL) || strcmp(supp_status, "running") != 0) { LOGE("Supplicant not running, cannot connect"); return -1; } if (access(IFACE_DIR, F_OK) == 0) { snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface); } else { strlcpy(ifname, iface, sizeof(ifname)); } if (cur_module == ATHEROS_ATH6K) { LOGD ("execute the chmod_ath0 shell\n"); usleep(100000); property_set("ctl.start", "chmod_ath0"); } ctrl_conn = wpa_ctrl_open(ifname); if (ctrl_conn == NULL) { LOGE("Unable to open connection to supplicant on \"%s\": %s", ifname, strerror(errno)); return -1; } monitor_conn = wpa_ctrl_open(ifname); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } if (wpa_ctrl_attach(monitor_conn) != 0) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } { char ifname[IFNAMSIZ]; char buf[256]; strlcpy(ifname, iface, sizeof(ifname)); rtw_issue_driver_cmd(ifname, "BLOCK 0", buf, 256); } return 0; }
int Supplicant::connectToSupplicant() { if (!isStarted()) LOGW("Supplicant service not running"); mCtrl = wpa_ctrl_open("tiwlan0"); // XXX: if (mCtrl == NULL) { LOGE("Unable to open connection to supplicant on \"%s\": %s", "tiwlan0", strerror(errno)); return -1; } mMonitor = wpa_ctrl_open("tiwlan0"); if (mMonitor == NULL) { wpa_ctrl_close(mCtrl); mCtrl = NULL; return -1; } if (wpa_ctrl_attach(mMonitor) != 0) { wpa_ctrl_close(mMonitor); wpa_ctrl_close(mCtrl); mCtrl = mMonitor = NULL; return -1; } mListener = new SupplicantListener(mHandlers, mMonitor); if (mListener->startListener()) { LOGE("Error - unable to start supplicant listener"); stop(); return -1; } return 0; }
static struct wpa_ctrl * wpa_cli_open_connection(const char *ifname) { #ifdef ANDROID struct wpa_ctrl *cur_conn; #endif #if defined(CONFIG_CTRL_IFACE_UDP) || defined(CONFIG_CTRL_IFACE_NAMED_PIPE) #ifdef ANDROID cur_conn = wpa_ctrl_open(ifname); return cur_conn; #else ctrl_conn = wpa_ctrl_open(ifname); return ctrl_conn; #endif #else /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */ char *cfile; int flen, res; if (ifname == NULL) return NULL; #ifdef ANDROID if (access(ctrl_iface_dir, F_OK) < 0) cfile = (char *)ifname; else{ #endif flen = os_strlen(ctrl_iface_dir) + os_strlen(ifname) + 2; cfile = os_malloc(flen); if (cfile == NULL) return NULL; res = os_snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); if (res < 0 || res >= flen) { os_free(cfile); return NULL; } #ifdef ANDROID } cur_conn = wpa_ctrl_open(cfile); #ifdef CTRL_INTERFACE_2_SOCKETS monitor_conn = wpa_ctrl_open(cfile); #else monitor_conn = cur_conn; #endif if (cfile != ifname) os_free(cfile); return cur_conn; #else ctrl_conn = wpa_ctrl_open(cfile); os_free(cfile); return ctrl_conn; #endif #endif /* CONFIG_CTRL_IFACE_UDP || CONFIG_CTRL_IFACE_NAMED_PIPE */ }
int wifi_connect_to_supplicant() { char ifname[256]; static int cleaned_up = 0; property_get("wifi.interface", iface, "sta"); if (access(IFACE_DIR, F_OK) == 0) { snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface); } else { strlcpy(ifname, iface, sizeof(ifname)); } ctrl_conn = wpa_ctrl_open(ifname); if (ctrl_conn == NULL) { LOGD("Unable to open connection to supplicant on \"%s\": %s", ifname, strerror(errno)); /* * errno == ENOENT means the supplicant daemon isn't * running. Take this opportunity to clear out any * stale socket files that might be left over. Note * there's a possible race with the command line client * trying to connect to the daemon, but it would require * that the supplicant be started and the command line * client connect to it during the window between the * error check and the removal of the files. And in * any event, the remedy is that the user would simply * have to run the command line program again. */ if (!cleaned_up && (errno == ENOENT || errno == EADDRINUSE)) { cleaned_up = 1; /* do this just once */ wpa_ctrl_cleanup(); } return -1; } monitor_conn = wpa_ctrl_open(ifname); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } if (wpa_ctrl_attach(monitor_conn) != 0) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } return 0; }
int wifi_connect_to_supplicant() { char ifname[256]; char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; int supplicant_timeout = SUPPLICANT_TIMEOUT; LOGD("[wifiHW] wifi connect to supplicant"); /* Make sure supplicant is running */ if (!property_get(SUPP_PROP_NAME, supp_status, NULL) || strcmp(supp_status, "running") != 0) { LOGE("Supplicant not running, cannot connect"); return -1; } property_get("wifi.interface", iface, WIFI_TEST_INTERFACE); if (access(IFACE_DIR, F_OK) == 0) { snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface); } else { strlcpy(ifname, iface, sizeof(ifname)); } ctrl_conn = wpa_ctrl_open(ifname); while (ctrl_conn == NULL && supplicant_timeout > 0) { usleep(SUPPLICANT_TIMEOUT_STEP); supplicant_timeout -= SUPPLICANT_TIMEOUT_STEP; ctrl_conn = wpa_ctrl_open(ifname); } if (ctrl_conn == NULL) { LOGE("Unable to open connection to supplicant on \"%s\": %s", ifname, strerror(errno)); return -1; } monitor_conn = wpa_ctrl_open(ifname); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } if (wpa_ctrl_attach(monitor_conn) != 0) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } return 0; }
static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname) { #ifndef CONFIG_CTRL_IFACE_UDP char *cfile; int flen; #endif /* !CONFIG_CTRL_IFACE_UDP */ if (ifname == NULL) return NULL; #ifdef CONFIG_CTRL_IFACE_UDP ctrl_conn = wpa_ctrl_open(ifname); return ctrl_conn; #else /* CONFIG_CTRL_IFACE_UDP */ flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; cfile = malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); if (client_socket_dir && client_socket_dir[0] && access(client_socket_dir, F_OK) < 0) { perror(client_socket_dir); free(cfile); return NULL; } ctrl_conn = wpa_ctrl_open2(cfile, client_socket_dir); free(cfile); return ctrl_conn; #endif /* CONFIG_CTRL_IFACE_UDP */ }
/* -------------------------------------------------------------------------------- WPACtrl_open */ static int WPACtrl_open(WPACtrl* self, PyObject* args, UNUSED PyObject* kargs) { char* path; if (!PyArg_ParseTuple(args, "s", &path)) { PyErr_SetString(WPACtrl_error, "failed to parse ctrl_iface_path string"); return -1; } if (strlen(path) >= MAXPATHLEN) { PyErr_SetString(WPACtrl_error, "ctrl_iface_path string length too long"); return -1; } self->ctrl_iface_path = malloc(MAXPATHLEN); if (!self->ctrl_iface_path) { PyErr_SetString(WPACtrl_error, "failed to allocate memory for ctrl_iface_path"); return -1; } strcpy(self->ctrl_iface_path, path); self->ctrl_iface = wpa_ctrl_open(self->ctrl_iface_path); if (!self->ctrl_iface) { PyErr_SetString(WPACtrl_error, "wpa_ctrl_open failed"); return -1; } self->attached = 0; return 0; }
bool WiFiNode::open() { bool result = false; do { if (!ctrlIfaceName) { DBGMSG_ERR("ctrlIfaceName is [%p]", ctrlIfaceName); break; } const size_t wpaPathLength = strlen(ctrlIfaceBasePath) + strlen(ctrlIfaceName) + 1; char path[wpaPathLength]; snprintf(path, wpaPathLength, "%s%s", ctrlIfaceBasePath, ctrlIfaceName); DBGMSG_M("Trying to open [%s]", path); wpaCtrl = wpa_ctrl_open(path); if (!wpaCtrl) { DBGMSG_ERR("Failed to Open Interface"); break; } if (wpa_ctrl_attach(wpaCtrl)) { DBGMSG_ERR("Failed to Attach To Interface %d"); break; } result = true; } while (0); DBGMSG_M("Result %s", result ? "Ok" : "Error"); return result; }
wrapd_hdl_t * wrapd_conn_to_global_wpa_s(const char *ifname, const char *confname, int isolation, int timer) { struct wrap_demon *aptr; char *realname = (void *)ifname; struct netlink_config *cfg; if (!realname) return NULL; aptr = os_zalloc(sizeof(*aptr)); if (!aptr) return NULL; aptr->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); if (aptr->ioctl_sock < 0) { wrapd_printf("socket[PF_INET,SOCK_DGRAM]"); return NULL; } aptr->global = wpa_ctrl_open(realname); if (!aptr->global) { close(aptr->ioctl_sock); os_free(aptr); wrapd_printf("Fail to connect global wpa_s"); return NULL; } cfg = os_zalloc(sizeof(*cfg)); if (cfg == NULL) { close(aptr->ioctl_sock); os_free(aptr); return NULL; } cfg->ctx = aptr; cfg->newlink_cb = wrapd_event_rtm_newlink; aptr->netlink = netlink_init(cfg); if (aptr->netlink == NULL) { close(aptr->ioctl_sock); os_free(cfg); os_free(aptr); return NULL; } if (confname) aptr->wpa_conf_file = os_strdup(confname); if (isolation == 1) aptr->do_isolation = 1; else aptr->do_isolation = 0; if (timer == 1) aptr->do_timer = 1; else aptr->do_timer = 0; return (void *) aptr; }
static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname) { char *cfile = NULL; int flen; if (ifname == NULL) return NULL; #ifdef ANDROID if (access(ctrl_iface_dir, F_OK) < 0) { cfile = os_strdup(ifname); if (cfile == NULL) return NULL; } else { flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; cfile = malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); } #else flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; cfile = malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); #endif /* ANDROID */ ctrl_conn = wpa_ctrl_open(cfile); free(cfile); return ctrl_conn; }
static int wpa_connect(const char *if_name) { char *cfile; int flen, res; flen = (strlen(WPASUPP_CTRL) + strlen(if_name) + 2); cfile = malloc(flen); if (cfile == NULL) { di_info("Can't allocate memory for WPA control interface."); return 1; } res = snprintf(cfile, flen, "%s/%s", WPASUPP_CTRL, if_name); if ((res < 0) || (res >= flen)) { free(cfile); return 1; } ctrl = wpa_ctrl_open(cfile); free(cfile); if (ctrl == NULL) { di_info("Couldn't connect to wpasupplicant"); return 1; } else return 0; }
int wifi_connect_to_supplicant() { char ifname[256]; char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; /* Make sure supplicant is running */ if (!property_get(SUPP_PROP_NAME, supp_status, NULL) || strcmp(supp_status, "running") != 0) { LOGE("Supplicant not running, cannot connect"); return -1; } if (access(IFACE_DIR, F_OK) == 0) { snprintf(ifname, sizeof(ifname), "%s/%s", IFACE_DIR, iface); } else { strlcpy(ifname, iface, sizeof(ifname)); } ctrl_conn = wpa_ctrl_open(ifname); if (ctrl_conn == NULL) { LOGE("Unable to open connection to supplicant on \"%s\": %s", ifname, strerror(errno)); return -1; } monitor_conn = wpa_ctrl_open(ifname); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } if (wpa_ctrl_attach(monitor_conn) != 0) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets) == -1) { wpa_ctrl_close(monitor_conn); wpa_ctrl_close(ctrl_conn); ctrl_conn = monitor_conn = NULL; return -1; } return 0; }
void AddInterface::interfaceSelected(QTreeWidgetItem *sel) { if (!sel) return; #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE struct wpa_ctrl *ctrl; int ret; char buf[20], cmd[256]; size_t len; /* * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB * <driver_param>TAB<bridge_name> */ snprintf(cmd, sizeof(cmd), "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s", sel->text(1).toAscii().constData(), "default", sel->text(0).toAscii().constData(), "yes", "", ""); cmd[sizeof(cmd) - 1] = '\0'; ctrl = wpa_ctrl_open(NULL); if (ctrl == NULL) return; len = sizeof(buf) - 1; ret = wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL); wpa_ctrl_close(ctrl); if (ret < 0) { QMessageBox::warning(this, "wpa_gui", tr("Add interface command could not be " "completed.")); return; } buf[len] = '\0'; if (buf[0] != 'O' || buf[1] != 'K') { QMessageBox::warning(this, "wpa_gui", tr("Failed to add the interface.")); return; } #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ #ifdef CONFIG_NATIVE_WINDOWS if (!addRegistryInterface(sel->text(1))) { QMessageBox::information(this, "wpa_gui", tr("Failed to add the interface into " "registry.")); } #endif /* CONFIG_NATIVE_WINDOWS */ wpagui->selectAdapter(sel->text(1)); close(); }
static struct wpa_ctrl * wpa_open_ctrl(const char *ifname) { char buf[128]; struct wpa_ctrl *ctrl; os_snprintf(buf, sizeof(buf), "%s%s", wpas_ctrl_path, ifname); ctrl = wpa_ctrl_open(buf); if (ctrl == NULL) printf("wpa_command: wpa_ctrl_open(%s) failed\n", buf); return ctrl; }
int wifi_connect_on_socket_path(int index, const char *path) { char supp_status[PROPERTY_VALUE_MAX] = {'\0'}; ALOGI("Connect socket path:%s", path); /* Make sure supplicant is running */ if (!property_get(supplicant_prop_name, supp_status, NULL) || strcmp(supp_status, "running") != 0) { ALOGE("Supplicant not running, cannot connect"); return -1; } ctrl_conn[index] = wpa_ctrl_open(path); if (ctrl_conn[index] == NULL) { ALOGE("Unable to open connection to supplicant on \"%s\": %s", path, strerror(errno)); return -1; } monitor_conn[index] = wpa_ctrl_open(path); if (monitor_conn[index] == NULL) { wpa_ctrl_close(ctrl_conn[index]); ctrl_conn[index] = NULL; return -1; } if (wpa_ctrl_attach(monitor_conn[index]) != 0) { wpa_ctrl_close(monitor_conn[index]); wpa_ctrl_close(ctrl_conn[index]); ctrl_conn[index] = monitor_conn[index] = NULL; return -1; } if (socketpair(AF_UNIX, SOCK_STREAM, 0, exit_sockets[index]) == -1) { wpa_ctrl_close(monitor_conn[index]); wpa_ctrl_close(ctrl_conn[index]); ctrl_conn[index] = monitor_conn[index] = NULL; return -1; } return 0; }
/* wpatalk_reconnect -- maybe reconnect. * If previously connected, disconnects and aborts unless "keep_going" * is set (-k option). */ static void wpatalk_reconnect(void) { if (ctrl_conn) { wpatalk_close_connection(); if (!keep_going) { wpatalk_fatal("Exiting due to lost connection"); } } for (;;) { if (wpatalk_get_socket_path() == 0) { #ifdef CONFIG_CTRL_IFACE_UNIX ctrl_conn = wpa_ctrl_open(socket_path); #else #error "wpatalk_open_connection configuration error" #endif } if (ctrl_conn) { if (wpa_ctrl_attach(ctrl_conn) == 0) { if (warning_displayed || verbose) { wpatalk_info( "Connection (re)established to daemon=%s interface=%s", daemon_name, ctrl_ifname); wpatalk_info("... using socket-file=%s", socket_path); warning_displayed = 0; } wpatalk_attached = 1; return; } else { if (!warning_displayed) { wpatalk_warning( "Failed to attach to daemon %s", daemon_name); } wpatalk_close_connection(); ctrl_conn = NULL; } } if (!keep_going) { wpatalk_fatal("Failed to connect to daemon %s errno %d " "using socket file %s", daemon_name, errno, socket_path); return; } if (!warning_displayed) { wpatalk_info("Could not connect to daemon %s -- re-trying", daemon_name); warning_displayed = 1; } os_sleep(1, 0); } return; }
static struct wpa_ctrl * wpa_cli_open_connection(const char *ifname) { #ifdef CONFIG_CTRL_IFACE_UDP ctrl_conn = wpa_ctrl_open(""); return ctrl_conn; #else /* CONFIG_CTRL_IFACE_UDP */ char *cfile; int flen; if (ifname == NULL) return NULL; flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; cfile = malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); ctrl_conn = wpa_ctrl_open(cfile); free(cfile); return ctrl_conn; #endif /* CONFIG_CTRL_IFACE_UDP */ }
int main(int argc, char *argv[]) { if (argc != 2) errx(EXIT_FAILURE, "%s <path to supplicant control pipe>", argv[0]); ctrl = wpa_ctrl_open(argv[1]); if (!ctrl) err(EXIT_FAILURE, "Couldn't open '%s'", argv[1]); if (wpa_ctrl_attach(ctrl) < 0) err(EXIT_FAILURE, "wpa_ctrl_attach"); int wpa_fd = wpa_ctrl_get_fd(ctrl); for (;;) { struct pollfd fdset[2]; fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[0].revents = 0; fdset[1].fd = wpa_fd; fdset[1].events = POLLIN; fdset[1].revents = 0; debug("waiting on poll\n"); int rc = poll(fdset, 2, -1); if (rc < 0) { // Retry if EINTR if (errno == EINTR) continue; err(EXIT_FAILURE, "poll"); } debug("poll: revents[0]=%08x, revents[1]=%08x\n", fdset[0].revents, fdset[1].revents); if (fdset[0].revents & (POLLIN | POLLHUP)) process_erl(); if (fdset[1].revents & POLLIN) process_wpa(); } return 0; }
static struct wpa_ctrl * hostapd_cli_open_connection(const char *ifname) { char *cfile; int flen; if (ifname == NULL) return NULL; flen = strlen(ctrl_iface_dir) + strlen(ifname) + 2; cfile = malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ifname); ctrl_conn = wpa_ctrl_open(cfile); free(cfile); return ctrl_conn; }
static S32 IpcWpa_Sockets_Open(TIpcWpa* pIpcWpa, PS8 pSupplIfFile) { S32 i; for (i=0; i< IPC_WPA_CTRL_OPEN_RETRIES; i++) { pIpcWpa->pWpaCtrl = wpa_ctrl_open((char*)pSupplIfFile); if (pIpcWpa->pWpaCtrl) break; } if (pIpcWpa->pWpaCtrl == NULL) { os_error_printf(CU_MSG_ERROR, (PS8)"ERROR - IpcWpa_Sockets_Open - can't connect the socket\n"); return EOALERR_IPC_WPA_ERROR_CANT_CONNECT_TO_SUPPL; } return OK; }
void AddInterface::addInterfaces() { #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE struct wpa_ctrl *ctrl; int ret; char buf[2048]; size_t len; ctrl = wpa_ctrl_open(NULL); if (ctrl == NULL) return; len = sizeof(buf) - 1; ret = wpa_ctrl_request(ctrl, "INTERFACE_LIST", 14, buf, &len, NULL); if (ret < 0) { wpa_ctrl_close(ctrl); return; } buf[len] = '\0'; wpa_ctrl_close(ctrl); QString ifaces(buf); QStringList lines = ifaces.split(QRegExp("\\n")); for (QStringList::Iterator it = lines.begin(); it != lines.end(); it++) { QStringList arg = (*it).split(QChar('\t')); if (arg.size() < 3) continue; QTreeWidgetItem *item = new QTreeWidgetItem(interfaceWidget); if (!item) break; item->setText(0, arg[0]); item->setText(1, arg[1]); item->setText(2, arg[2]); } interfaceWidget->resizeColumnToContents(0); interfaceWidget->resizeColumnToContents(1); interfaceWidget->resizeColumnToContents(2); #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ }
int connect_wpa_supplicant(char *if_name) { char unixsock [256]; snprintf(unixsock, 256, "%s/%s", wpa_daemon_ctrl, if_name); printf("starting wpa_supplicant conversation\n"); if (!wpa_client) wpa_ctrl_close(wpa_client); wpa_client = 0; for (int retries = 3; retries > 0; retries--) { wpa_client = wpa_ctrl_open(unixsock); if (wpa_client) break; sleep(2); } if (!wpa_client) { printf("failed to create wpa_supplicant connection\n"); return 0; } return 1; }
struct wrapd_ctrl * wrapd_conn_to_mpsta_wpa_s(const char *ifname) { struct wpa_ctrl *priv = NULL; char *cfile; int flen; if (ifname == NULL) return NULL; flen = strlen(wpa_s_ctrl_iface_dir) + strlen(ifname) + 2; cfile = malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", wpa_s_ctrl_iface_dir, ifname); priv = wpa_ctrl_open(cfile); free(cfile); return (struct wrapd_ctrl *)priv; }
struct wrapd_ctrl * wrapd_conn_to_hostapd(const char *ifname) { struct wpa_ctrl *priv = NULL; char *cfile; int flen; if (ifname == NULL) return NULL; flen = os_strlen(hostapd_ctrl_iface_dir) + strlen(ifname) + 2; cfile = os_malloc(flen); if (cfile == NULL) return NULL; snprintf(cfile, flen, "%s/%s", hostapd_ctrl_iface_dir, ifname); priv = wpa_ctrl_open(cfile); os_free(cfile); return (struct wrapd_ctrl *)priv; }
int WpaGui::openCtrlConnection(const char *ifname) { char *cfile; int flen; char buf[2048], *pos, *pos2; size_t len; if (ifname) { if (ifname != ctrl_iface) { free(ctrl_iface); ctrl_iface = strdup(ifname); } } else { #ifdef CONFIG_CTRL_IFACE_UDP free(ctrl_iface); ctrl_iface = strdup("udp"); #endif /* CONFIG_CTRL_IFACE_UDP */ #ifdef CONFIG_CTRL_IFACE_UNIX struct dirent *dent; DIR *dir = opendir(ctrl_iface_dir); free(ctrl_iface); ctrl_iface = NULL; if (dir) { while ((dent = readdir(dir))) { #ifdef _DIRENT_HAVE_D_TYPE /* Skip the file if it is not a socket. * Also accept DT_UNKNOWN (0) in case * the C library or underlying file * system does not support d_type. */ if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) continue; #endif /* _DIRENT_HAVE_D_TYPE */ if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; printf("Selected interface '%s'\n", dent->d_name); ctrl_iface = strdup(dent->d_name); break; } closedir(dir); } #endif /* CONFIG_CTRL_IFACE_UNIX */ #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE struct wpa_ctrl *ctrl; int ret; free(ctrl_iface); ctrl_iface = NULL; ctrl = wpa_ctrl_open(NULL); if (ctrl) { len = sizeof(buf) - 1; ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL); if (ret >= 0) { connectedToService = true; buf[len] = '\0'; pos = strchr(buf, '\n'); if (pos) *pos = '\0'; ctrl_iface = strdup(buf); } wpa_ctrl_close(ctrl); } #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ } if (ctrl_iface == NULL) { #ifdef CONFIG_NATIVE_WINDOWS static bool first = true; if (first && !serviceRunning()) { first = false; if (QMessageBox::warning( this, qAppName(), tr("wpa_supplicant service is not " "running.\n" "Do you want to start it?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) startService(); } #endif /* CONFIG_NATIVE_WINDOWS */ return -1; } #ifdef CONFIG_CTRL_IFACE_UNIX flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2; cfile = (char *) malloc(flen); if (cfile == NULL) return -1; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface); #else /* CONFIG_CTRL_IFACE_UNIX */ flen = strlen(ctrl_iface) + 1; cfile = (char *) malloc(flen); if (cfile == NULL) return -1; snprintf(cfile, flen, "%s", ctrl_iface); #endif /* CONFIG_CTRL_IFACE_UNIX */ if (ctrl_conn) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; } if (monitor_conn) { delete msgNotifier; msgNotifier = NULL; wpa_ctrl_detach(monitor_conn); wpa_ctrl_close(monitor_conn); monitor_conn = NULL; } printf("Trying to connect to '%s'\n", cfile); ctrl_conn = wpa_ctrl_open(cfile); if (ctrl_conn == NULL) { free(cfile); return -1; } monitor_conn = wpa_ctrl_open(cfile); free(cfile); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); return -1; } if (wpa_ctrl_attach(monitor_conn)) { printf("Failed to attach to wpa_supplicant\n"); wpa_ctrl_close(monitor_conn); monitor_conn = NULL; wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP) msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn), QSocketNotifier::Read, this); connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs())); #endif adapterSelect->clear(); adapterSelect->addItem(ctrl_iface); adapterSelect->setCurrentIndex(0); len = sizeof(buf) - 1; if (wpa_ctrl_request(ctrl_conn, "INTERFACES", 10, buf, &len, NULL) >= 0) { buf[len] = '\0'; pos = buf; while (*pos) { pos2 = strchr(pos, '\n'); if (pos2) *pos2 = '\0'; if (strcmp(pos, ctrl_iface) != 0) adapterSelect->addItem(pos); if (pos2) pos = pos2 + 1; else break; } } len = sizeof(buf) - 1; if (wpa_ctrl_request(ctrl_conn, "GET_CAPABILITY eap", 18, buf, &len, NULL) >= 0) { buf[len] = '\0'; QString res(buf); QStringList types = res.split(QChar(' ')); bool wps = types.contains("WSC"); actionWPS->setEnabled(wps); wpsTab->setEnabled(wps); wpaguiTab->setTabEnabled(wpaguiTab->indexOf(wpsTab), wps); } return 0; }
int main(int argc, char *argv[]) { int interactive; int warning_displayed = 0; int c; int daemonize = 0; const char *global = NULL; if (os_program_init()) return -1; for (;;) { c = getopt(argc, argv, "a:Bg:hi:p:P:v"); if (c < 0) break; switch (c) { case 'a': action_file = optarg; break; case 'B': daemonize = 1; break; case 'g': global = optarg; break; case 'h': usage(); return 0; case 'v': printf("%s\n", wpa_cli_version); return 0; case 'i': ctrl_ifname = strdup(optarg); break; case 'p': ctrl_iface_dir = optarg; break; case 'P': pid_file = optarg; break; default: usage(); return -1; } } interactive = (argc == optind) && (action_file == NULL); if (interactive) printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license); if (global) { ctrl_conn = wpa_ctrl_open(global); if (ctrl_conn == NULL) { perror("Failed to connect to wpa_supplicant - " "wpa_ctrl_open"); return -1; } } for (; !global;) { #ifndef CONFIG_CTRL_IFACE_UDP if (ctrl_ifname == NULL) { struct dirent *dent; DIR *dir = opendir(ctrl_iface_dir); if (dir) { while ((dent = readdir(dir))) { #ifdef _DIRENT_HAVE_D_TYPE /* Skip the file if it is not a socket. * Also accept DT_UNKNOWN (0) in case * the C library or underlying file * system does not support d_type. */ if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) continue; #endif /* _DIRENT_HAVE_D_TYPE */ 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); } } #endif /* CONFIG_CTRL_IFACE_UDP */ ctrl_conn = wpa_cli_open_connection(ctrl_ifname); if (ctrl_conn) { if (warning_displayed) printf("Connection established.\n"); break; } if (!interactive) { perror("Failed to connect to wpa_supplicant - " "wpa_ctrl_open"); return -1; } if (!warning_displayed) { printf("Could not connect to wpa_supplicant - " "re-trying\n"); warning_displayed = 1; } os_sleep(1, 0); continue; } #ifndef _WIN32_WCE signal(SIGINT, wpa_cli_terminate); signal(SIGTERM, wpa_cli_terminate); #endif /* _WIN32_WCE */ #ifndef CONFIG_NATIVE_WINDOWS signal(SIGALRM, wpa_cli_alarm); #endif /* CONFIG_NATIVE_WINDOWS */ if (interactive || action_file) { if (wpa_ctrl_attach(ctrl_conn) == 0) { wpa_cli_attached = 1; } else { printf("Warning: Failed to attach to " "wpa_supplicant.\n"); if (!interactive) return -1; } } if (daemonize && os_daemonize(pid_file)) return -1; if (interactive) wpa_cli_interactive(); else if (action_file) wpa_cli_action(ctrl_conn); else wpa_request(ctrl_conn, argc - optind, &argv[optind]); free(ctrl_ifname); wpa_cli_cleanup(); return 0; }
int WpaGui::openCtrlConnection(const char *ifname) { char *cfile; int flen; char buf[2048], *pos, *pos2; size_t len; if (ifname) { if (ifname != ctrl_iface) { free(ctrl_iface); ctrl_iface = strdup(ifname); } } else { #ifdef CONFIG_CTRL_IFACE_UDP free(ctrl_iface); ctrl_iface = strdup("udp"); #endif /* CONFIG_CTRL_IFACE_UDP */ #ifdef CONFIG_CTRL_IFACE_UNIX struct dirent *dent; DIR *dir = opendir(ctrl_iface_dir); free(ctrl_iface); ctrl_iface = NULL; if (dir) { while ((dent = readdir(dir))) { #ifdef _DIRENT_HAVE_D_TYPE /* Skip the file if it is not a socket. * Also accept DT_UNKNOWN (0) in case * the C library or underlying file * system does not support d_type. */ if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) continue; #endif /* _DIRENT_HAVE_D_TYPE */ if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; printf("Selected interface '%s'\n", dent->d_name); ctrl_iface = strdup(dent->d_name); break; } closedir(dir); } #endif /* CONFIG_CTRL_IFACE_UNIX */ #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE struct wpa_ctrl *ctrl; int ret; free(ctrl_iface); ctrl_iface = NULL; ctrl = wpa_ctrl_open(NULL); if (ctrl) { len = sizeof(buf) - 1; ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL); if (ret >= 0) { buf[len] = '\0'; pos = strchr(buf, '\n'); if (pos) *pos = '\0'; ctrl_iface = strdup(buf); } wpa_ctrl_close(ctrl); } #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ } if (ctrl_iface == NULL) return -1; #ifdef CONFIG_CTRL_IFACE_UNIX flen = strlen(ctrl_iface_dir) + strlen(ctrl_iface) + 2; cfile = (char *) malloc(flen); if (cfile == NULL) return -1; snprintf(cfile, flen, "%s/%s", ctrl_iface_dir, ctrl_iface); #else /* CONFIG_CTRL_IFACE_UNIX */ flen = strlen(ctrl_iface) + 1; cfile = (char *) malloc(flen); if (cfile == NULL) return -1; snprintf(cfile, flen, "%s", ctrl_iface); #endif /* CONFIG_CTRL_IFACE_UNIX */ if (ctrl_conn) { wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; } if (monitor_conn) { delete msgNotifier; msgNotifier = NULL; wpa_ctrl_detach(monitor_conn); wpa_ctrl_close(monitor_conn); monitor_conn = NULL; } printf("Trying to connect to '%s'\n", cfile); ctrl_conn = wpa_ctrl_open(cfile); if (ctrl_conn == NULL) { free(cfile); return -1; } monitor_conn = wpa_ctrl_open(cfile); free(cfile); if (monitor_conn == NULL) { wpa_ctrl_close(ctrl_conn); return -1; } if (wpa_ctrl_attach(monitor_conn)) { printf("Failed to attach to wpa_supplicant\n"); wpa_ctrl_close(monitor_conn); monitor_conn = NULL; wpa_ctrl_close(ctrl_conn); ctrl_conn = NULL; return -1; } #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP) msgNotifier = new QSocketNotifier(wpa_ctrl_get_fd(monitor_conn), QSocketNotifier::Read, this); connect(msgNotifier, SIGNAL(activated(int)), SLOT(receiveMsgs())); #endif adapterSelect->clear(); adapterSelect->insertItem(ctrl_iface); adapterSelect->setCurrentItem(0); len = sizeof(buf) - 1; if (wpa_ctrl_request(ctrl_conn, "INTERFACES", 10, buf, &len, NULL) >= 0) { buf[len] = '\0'; pos = buf; while (*pos) { pos2 = strchr(pos, '\n'); if (pos2) *pos2 = '\0'; if (strcmp(pos, ctrl_iface) != 0) adapterSelect->insertItem(pos); if (pos2) pos = pos2 + 1; else break; } } return 0; }
static char * wpa_cli_get_default_ifname(void) { char *ifname = NULL; #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 return NULL; } while ((dent = readdir(dir))) { #ifdef _DIRENT_HAVE_D_TYPE /* * Skip the file if it is not a socket. Also accept * DT_UNKNOWN (0) in case the C library or underlying * file system does not support d_type. */ if (dent->d_type != DT_SOCK && dent->d_type != DT_UNKNOWN) continue; #endif /* _DIRENT_HAVE_D_TYPE */ if (os_strcmp(dent->d_name, ".") == 0 || os_strcmp(dent->d_name, "..") == 0) continue; printf("Selected interface '%s'\n", dent->d_name); ifname = os_strdup(dent->d_name); break; } closedir(dir); #endif /* CONFIG_CTRL_IFACE_UNIX */ #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE char buf[4096], *pos; size_t len; struct wpa_ctrl *ctrl; int ret; ctrl = wpa_ctrl_open(NULL); if (ctrl == NULL) return NULL; len = sizeof(buf) - 1; ret = wpa_ctrl_request(ctrl, "INTERFACES", 10, buf, &len, NULL); if (ret >= 0) { buf[len] = '\0'; pos = os_strchr(buf, '\n'); if (pos) *pos = '\0'; ifname = os_strdup(buf); } wpa_ctrl_close(ctrl); #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ return ifname; }
int main(int argc, char *argv[]) { int interactive; int warning_displayed = 0; int c; int daemonize = 0; int ret = 0; const char *global = NULL; if (os_program_init()) return -1; for (;;) { c = getopt(argc, argv, "a:Bg:hi:p:P:v"); if (c < 0) break; switch (c) { case 'a': action_file = optarg; break; case 'B': daemonize = 1; break; case 'g': global = optarg; break; case 'h': usage(); return 0; case 'v': printf("%s\n", wpa_cli_version); return 0; case 'i': os_free(ctrl_ifname); ctrl_ifname = os_strdup(optarg); break; case 'p': ctrl_iface_dir = optarg; break; case 'P': pid_file = optarg; break; default: usage(); return -1; } } interactive = (argc == optind) && (action_file == NULL); if (interactive) printf("%s\n\n%s\n\n", wpa_cli_version, wpa_cli_license); if (global) { #ifdef CONFIG_CTRL_IFACE_NAMED_PIPE ctrl_conn = wpa_ctrl_open(NULL); #else /* CONFIG_CTRL_IFACE_NAMED_PIPE */ ctrl_conn = wpa_ctrl_open(global); #endif /* CONFIG_CTRL_IFACE_NAMED_PIPE */ if (ctrl_conn == NULL) { perror("Failed to connect to wpa_supplicant - " "wpa_ctrl_open"); return -1; } } for (; !global;) { if (ctrl_ifname == NULL) ctrl_ifname = wpa_cli_get_default_ifname(); ctrl_conn = wpa_cli_open_connection(ctrl_ifname); if (ctrl_conn) { if (warning_displayed) printf("Connection established.\n"); break; } if (!interactive) { perror("Failed to connect to wpa_supplicant - " "wpa_ctrl_open"); return -1; } if (!warning_displayed) { printf("Could not connect to wpa_supplicant - " "re-trying\n"); warning_displayed = 1; } os_sleep(1, 0); continue; } #ifndef _WIN32_WCE signal(SIGINT, wpa_cli_terminate); signal(SIGTERM, wpa_cli_terminate); #endif /* _WIN32_WCE */ #ifndef CONFIG_NATIVE_WINDOWS signal(SIGALRM, wpa_cli_alarm); #endif /* CONFIG_NATIVE_WINDOWS */ if (interactive || action_file) { if (wpa_ctrl_attach(monitor_conn) == 0) { wpa_cli_attached = 1; } else { printf("Warning: Failed to attach to " "wpa_supplicant.\n"); if (!interactive) return -1; } } if (daemonize && os_daemonize(pid_file)) return -1; if (interactive) wpa_cli_interactive(); else if (action_file) wpa_cli_action(ctrl_conn); else ret = wpa_request(ctrl_conn, argc - optind, &argv[optind]); os_free(ctrl_ifname); wpa_cli_cleanup(); return ret; }