void wifi_close_supplicant_connection()
{
    char supp_status[PROPERTY_VALUE_MAX] = {'\0'};
    int count = 50; /* wait at most 5 seconds to ensure init has stopped stupplicant */

    if (ctrl_conn != NULL) {
        wpa_ctrl_close(ctrl_conn);
        ctrl_conn = NULL;
    }
    if (monitor_conn != NULL) {
        wpa_ctrl_close(monitor_conn);
        monitor_conn = NULL;
    }

    if (exit_sockets[0] >= 0) {
        close(exit_sockets[0]);
        exit_sockets[0] = -1;
    }

    if (exit_sockets[1] >= 0) {
        close(exit_sockets[1]);
        exit_sockets[1] = -1;
    }

    while (count-- > 0) {
        if (property_get(SUPP_PROP_NAME, supp_status, NULL)) {
            if (strcmp(supp_status, "stopped") == 0)
                return;
        }
        usleep(100000);
    }
}
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;
}
Example #4
0
void wifi_close_supplicant_connection()
{
    if (ctrl_conn != NULL) {
        wpa_ctrl_close(ctrl_conn);
        ctrl_conn = NULL;
    }
    if (monitor_conn != NULL) {
        wpa_ctrl_close(monitor_conn);
        monitor_conn = NULL;
    }
}
Example #5
0
WpaGui::~WpaGui()
{
	delete msgNotifier;

	if (monitor_conn) {
		wpa_ctrl_detach(monitor_conn);
		wpa_ctrl_close(monitor_conn);
		monitor_conn = NULL;
	}
	if (ctrl_conn) {
		wpa_ctrl_close(ctrl_conn);
		ctrl_conn = NULL;
	}

	if (eh) {
		eh->close();
		delete eh;
		eh = NULL;
	}

	if (scanres) {
		scanres->close();
		delete scanres;
		scanres = NULL;
	}

	if (peers) {
		peers->close();
		delete peers;
		peers = NULL;
	}

	if (add_iface) {
		add_iface->close();
		delete add_iface;
		add_iface = NULL;
	}

	if (udr) {
		udr->close();
		delete udr;
		udr = NULL;
	}

	free(ctrl_iface);
	ctrl_iface = NULL;

	free(ctrl_iface_dir);
	ctrl_iface_dir = NULL;
}
Example #6
0
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;
}
Example #7
0
static void wpa_cli_close_connection(void)
{
	if (ctrl_conn == NULL)
		return;

	if (wpa_cli_attached) {
		wpa_ctrl_detach(monitor_conn);
		wpa_cli_attached = 0;
	}
#ifdef CTRL_INTERFACE_2_SOCKETS
	wpa_ctrl_close(monitor_conn);
#endif
	wpa_ctrl_close(ctrl_conn);
	ctrl_conn = monitor_conn = NULL;
}
Example #8
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;
}
Example #9
0
int wait_ip_addr(const char *ifname, int timeout)
{
	char ip[30];
	int count = timeout;
	struct wpa_ctrl *ctrl;

	while (count > 0) {
		printf("%s: ifname='%s' - %d seconds remaining\n",
		       __func__, ifname, count);
		count--;
		if (get_wpa_status(ifname, "ip_address", ip, sizeof(ip)) == 0
		    && strlen(ip) > 0) {
			printf("IP address found: '%s'\n", ip);
			return 0;
		}
		ctrl = wpa_open_ctrl(ifname);
		if (ctrl == NULL)
			return -1;
		wpa_ctrl_close(ctrl);
		sleep(1);
	}
	printf("%s: Could not get IP address for ifname='%s'", __func__,
	       ifname);
	return -1;
}
Example #10
0
void WiFiNode::close() {
	if (wpaCtrl) {
		DBGMSG_M("Closing [%p]", wpaCtrl);
		wpa_ctrl_close(wpaCtrl);
		wpaCtrl = nullptr;
	}
}
Example #11
0
int get_wpa_status(const char *ifname, const char *field, char *obuf,
		   size_t obuf_size)
{
	struct wpa_ctrl *ctrl;
	char buf[4096];
	char *pos, *end;
	size_t len, flen;

	ctrl = wpa_open_ctrl(ifname);
	if (ctrl == NULL)
		return -1;
	len = sizeof(buf);
	if (wpa_ctrl_request(ctrl, "STATUS", 6, buf, &len, NULL) < 0) {
		wpa_ctrl_close(ctrl);
		return -1;
	}
	wpa_ctrl_close(ctrl);
	buf[len] = '\0';

	flen = strlen(field);
	pos = buf;
	while (pos + flen < buf + len) {
		if (pos > buf) {
			if (*pos != '\n') {
				pos++;
				continue;
			}
			pos++;
		}
		if (strncmp(pos, field, flen) != 0 || pos[flen] != '=') {
			pos++;
			continue;
		}
		pos += flen + 1;
		end = strchr(pos, '\n');
		if (end == NULL)
			return -1;
		*end++ = '\0';
		if (end - pos > (int) obuf_size)
			return -1;
		memcpy(obuf, pos, end - pos);
		return 0;
	}

	return -1;
}
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;
}
wifi_manager::~wifi_manager() {

	_is_running = false;

	pthread_join(_thread,NULL);

	clear();

	if(_ctrl)
		wpa_ctrl_close(_ctrl);
	
	if(_event_ctrl)
		wpa_ctrl_close(_event_ctrl);
	
	if(_buffer)
		delete[] _buffer;
}
Example #14
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();
}
Example #15
0
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 */
}
Example #16
0
int wpa_command_resp(const char *ifname, const char *cmd,
		     char *resp, size_t resp_size)
{
	struct wpa_ctrl *ctrl;
	size_t len;

	printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
	ctrl = wpa_open_ctrl(ifname);
	if (ctrl == NULL)
		return -1;
	len = resp_size;
	if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), resp, &len, NULL) < 0) {
		printf("wpa_command: wpa_ctrl_request failed\n");
		wpa_ctrl_close(ctrl);
		return -1;
	}
	wpa_ctrl_close(ctrl);
	resp[len] = '\0';
	return 0;
}
Example #17
0
static void hostapd_cli_close_connection(void)
{
	if (ctrl_conn == NULL)
		return;

	if (hostapd_cli_attached) {
		wpa_ctrl_detach(ctrl_conn);
		hostapd_cli_attached = 0;
	}
	wpa_ctrl_close(ctrl_conn);
	ctrl_conn = NULL;
}
Example #18
0
static void wpatalk_close_connection(void)
{
	if (ctrl_conn == NULL)
		return;

	if (wpatalk_attached) {
		wpa_ctrl_detach(ctrl_conn);
		wpatalk_attached = 0;
	}
	wpa_ctrl_close(ctrl_conn);
	ctrl_conn = NULL;
}
Example #19
0
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;
}
Example #20
0
void wifi_close_sockets()
{
    if (ctrl_conn != NULL) {
        wpa_ctrl_close(ctrl_conn);
        ctrl_conn = NULL;
    }

    if (monitor_conn != NULL) {
        wpa_ctrl_close(monitor_conn);
        monitor_conn = NULL;
    }

    if (exit_sockets[0] >= 0) {
        close(exit_sockets[0]);
        exit_sockets[0] = -1;
    }

    if (exit_sockets[1] >= 0) {
        close(exit_sockets[1]);
        exit_sockets[1] = -1;
    }
}
Example #21
0
void wifi_close_sockets(int index)
{
    if (ctrl_conn[index] != NULL) {
        wpa_ctrl_close(ctrl_conn[index]);
        ctrl_conn[index] = NULL;
    }

    if (monitor_conn[index] != NULL) {
        wpa_ctrl_close(monitor_conn[index]);
        monitor_conn[index] = NULL;
    }

    if (exit_sockets[index][0] >= 0) {
        close(exit_sockets[index][0]);
        exit_sockets[index][0] = -1;
    }

    if (exit_sockets[index][1] >= 0) {
        close(exit_sockets[index][1]);
        exit_sockets[index][1] = -1;
    }
}
Example #22
0
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;
	}
	wpa_ctrl_close(ctrl_conn);
	ctrl_conn = NULL;
}
int Supplicant::stop() {

    if (mListener->stopListener()) {
        LOGW("Unable to stop supplicant listener (%s)", strerror(errno));
        return -1;
    }

    if (mServiceManager->stop(SUPPLICANT_SERVICE_NAME)) {
        LOGW("Error stopping supplicant (%s)", strerror(errno));
    }

    if (mCtrl) {
        wpa_ctrl_close(mCtrl);
        mCtrl = NULL;
    }
    if (mMonitor) {
        wpa_ctrl_close(mMonitor);
        mMonitor = NULL;
    }

    return 0;
}
Example #24
0
WpaGui::~WpaGui()
{
    delete msgNotifier;

    if (monitor_conn) {
            wpa_ctrl_detach(monitor_conn);
            wpa_ctrl_close(monitor_conn);
            monitor_conn = NULL;
    }
    if (ctrl_conn) {
            wpa_ctrl_close(ctrl_conn);
            ctrl_conn = NULL;
    }

//        if (eh) {
//                eh->close();
//                delete eh;
//                eh = NULL;
//        }

    if (scanres) {
            scanres->close();
            delete scanres;
            scanres = NULL;
    }

//        if (udr) {
//                udr->close();
//                delete udr;
//                udr = NULL;
//        }

    free(ctrl_iface);
    ctrl_iface = NULL;

    free(ctrl_iface_dir);
    ctrl_iface_dir = NULL;
}
Example #25
0
struct wpa_ctrl * open_wpa_mon(const char *ifname)
{
	struct wpa_ctrl *ctrl;

	ctrl = wpa_open_ctrl(ifname);
	if (ctrl == NULL)
		return NULL;
	if (wpa_ctrl_attach(ctrl) < 0) {
		wpa_ctrl_close(ctrl);
		return NULL;
	}

	return ctrl;
}
Example #26
0
int wpa_command(const char *ifname, const char *cmd)
{
	struct wpa_ctrl *ctrl;
	char buf[128];
	size_t len;

	printf("wpa_command(ifname='%s', cmd='%s')\n", ifname, cmd);
	ctrl = wpa_open_ctrl(ifname);
	if (ctrl == NULL)
		return -1;
	len = sizeof(buf);
	if (wpa_ctrl_request(ctrl, cmd, strlen(cmd), buf, &len, NULL) < 0) {
		printf("wpa_command: wpa_ctrl_request failed\n");
		wpa_ctrl_close(ctrl);
		return -1;
	}
	wpa_ctrl_close(ctrl);
	buf[len] = '\0';
	if (strncmp(buf, "FAIL", 4) == 0) {
		printf("wpa_command: Command failed (FAIL received)\n");
		return -1;
	}
	return 0;
}
Example #27
0
/* ------------------------------------------------------------------------------- WPACtrl_close */
static void WPACtrl_close(WPACtrl* self)
{
	if (self->ctrl_iface) {
		if (self->attached == 1)
			WPACtrl_detach(self);

		wpa_ctrl_close(self->ctrl_iface);
		self->ctrl_iface = NULL;
	}

	if (self->ctrl_iface_path) {
		free(self->ctrl_iface_path);
		self->ctrl_iface_path = NULL;
	}

	if (self->ob_type)
		self->ob_type->tp_free((PyObject*)(self));
}
Example #28
0
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;
} 
Example #29
0
int 
config_wpa_supplicant(char *if_name, struct config_ssid * match, int toggle) {
        char 		repbuf   [256];
        int 		res      , network_number = -1, wait = 5;

        if (!connect_wpa_supplicant(if_name))
            return 0;
        if (toggle) {
            res = sup_cmd(repbuf, "AP_SCAN 0");
            if (res < 0)
                return res;
            res = sup_cmd(repbuf, "ADD_NETWORK");
            if (res < 0)
                return res;
            sscanf(repbuf, "%d", &network_number);
            if (network_number < 0)
                return -1;
                /* Failed to create new network. */
            res = sup_cmd(repbuf, "SET_NETWORK %d auth_alg OPEN", network_number);
            if (res < 0)
                return res;
            if (strlen(match->ssid_key_mgmt)) {
                res = sup_cmd(repbuf, "SET_NETWORK %d key_mgmt %s", network_number, match->ssid_key_mgmt);
                if (res < 0)
                    return res;
            } 
            if (strlen(match->ssid_pairwise)) {
                res = sup_cmd(repbuf, "SET_NETWORK %d pairwise %s", network_number, match->ssid_pairwise);
                if (res < 0)
                    return res;
            } 
            if (strlen(match->ssid_group)) {
                res = sup_cmd(repbuf, "SET_NETWORK %d group %s", network_number, match->ssid_group);
                if (res < 0)
                    return res;
            }
            res = sup_cmd(repbuf, "SET_NETWORK %d proto WPA2", network_number);
            if (res < 0)
                return res;
            if (strlen(match->ssid_eap)) {
                res = sup_cmd(repbuf, "SET_NETWORK %d eap %s", network_number, match->ssid_eap);
                if (res < 0)
                    return res;
            } 
            if (strlen(match->ssid_phase1)) {
                printf("phase 1: %s\n", match->ssid_phase1);
                res = sup_cmd(repbuf, "SET_NETWORK %d phase1 \"%s\"", network_number, match->ssid_phase1);
                if (res < 0)
                    return res;
            } 
            if (strlen(match->ssid_phase2)) {
                printf("phase 2: %s\n", match->ssid_phase2);
                res = sup_cmd(repbuf, "SET_NETWORK %d phase2 \"%s\"", network_number, match->ssid_phase2);
                if (res < 0)
                    return res;
            } 
            if (strlen(match->ssid_ca_cert)) {
                printf("ca_cert: %s\n", match->ssid_ca_cert);
                res = sup_cmd(repbuf, "SET_NETWORK %d ca_cert %s", network_number, match->ssid_ca_cert);
                if (res < 0)
                    return res;
            } 
			if (strlen(match->ssid_client_cert)) {
                printf("client_cert: %s\n", match->ssid_client_cert);
                res = sup_cmd(repbuf, "SET_NETWORK %d client_cert %s", network_number, match->ssid_client_cert);
                if (res < 0)
                    return res;
            } 
            if (strlen(match->ssid_private_key)) {
                printf("private_key: %s\n", match->ssid_private_key);
                res = sup_cmd(repbuf, "SET_NETWORK %d private_key %s", network_number, match->ssid_private_key);
                if (res < 0)
                    return res;
            }
			if (strlen(match->ssid_private_key_pwd)) {
                printf("private key password: %s\n", match->ssid_private_key_pwd);
                res = sup_cmd(repbuf, "SET_NETWORK %d private_key_passwd %s", network_number, match->ssid_private_key_pwd);
                if (res < 0)
                    return res;
            }
			if (match->ssid_eapol) {
                printf("eapol_flags: %d\n", match->ssid_eapol);
                res = sup_cmd(repbuf, "SET_NETWORK %d eapol_flags %d", network_number, *match->ssid_eapol);
                if (res < 0)
                    return res;
            }
            if (strlen(match->ssid_identity)) {
                printf("identity: %s\n", match->ssid_identity);
                res = sup_cmd(repbuf, "SET_NETWORK %d identity \"%s\"", network_number, match->ssid_identity);
                if (res < 0)
                    return res;
            }
            if (strlen(match->ssid_anonymous)) {
                printf("anonymous identity: %s\n", match->ssid_anonymous);
                res = sup_cmd(repbuf, "SET_NETWORK %d anonymous_identity \"%s\"", network_number, match->ssid_anonymous);
                if (res < 0)
                    return res;
            }                        
            if (strlen(match->ssid_pass)) {
                printf("pass: %s\n", match->ssid_pass);
                res = sup_cmd(repbuf, "SET_NETWORK %d password \"%s\"", network_number, match->ssid_pass);
                if (res < 0)
                    return res;
            }
            res = sup_cmd(repbuf, "SET_NETWORK %d mode 0", network_number);
            if (res < 0)
                return res;
            if (match->ssid_bssid) {
                res = sup_cmd(repbuf, "BSSID %d %s", network_number, match->ssid_bssid);
                if (res < 0)
                    return res;
            }
            if (match->ssid_name)
                res = sup_cmd(repbuf, "SET_NETWORK %d ssid \"%s\"", network_number, match->ssid_name);             
            if (res < 0)
                return res;
            res = sup_cmd(repbuf, "SELECT_NETWORK %d", network_number);
            if (res < 0)
                return res;
            res = sup_cmd(repbuf, "ENABLE_NETWORK %d", network_number);
            if (res < 0)
                return res;
            res = sup_cmd(repbuf, "REASSOCIATE");
            if (res < 0)
                return res;
            for (wait = 5; wait > 0; wait--) {
                if (status_wpa_supplicant(if_name))
                    break;
                sleep(5);
            } 
        } else {
            res = sup_cmd(repbuf, "DISABLE_NETWORK all");
            if (res < 0)
                return res;
            res = sup_cmd(repbuf, "REMOVE_NETWORK all");
            if (res < 0)
                return res;
        }
        wpa_ctrl_close(wpa_client);
        wpa_client = 0;
        return res;
}
Example #30
0
int wpa_supplicant_start(struct debconfclient *client, const struct netcfg_interface *interface)
{
    int retry = 0;
    
    enum { CHECK_DAEMON,
           START_DAEMON,
           CONNECT,
           PING,
           ADD_NETWORK,
           SET_ESSID,
           SET_PSK,
           SET_SCAN_SSID,
           ENABLE_NETWORK,
           POLL,
           ABORT,
           SUCCESS } state = CHECK_DAEMON;
    
    for (;;) {
        switch(state) {
        
        case CHECK_DAEMON:
            wpa_daemon_running();
            if (wpa_is_running)
                state = CONNECT;
            else
                state = START_DAEMON;
            break;
            
        case START_DAEMON:
            if (!start_wpa_daemon(client, interface->name))
                state = CONNECT;
            else
                state = ABORT;
            break;
        
        case CONNECT:
            if (wpa_connect(interface->name) == 0)
                state = PING;
            else
                state = ABORT;
            break;
        
        case PING:
            /* if the daemon doesn't respond, try and ping
             * it and increment retry. If we have done
             * this 4 times, something must be wrong
             * so bail out.  */
            retry++;
            if (retry > 4)
                state = ABORT;
            else if (netcfg_wpa_cmd("PING")) {
                kill_wpa_supplicant();
                state = START_DAEMON;
                break;
            }
            else
                state = ADD_NETWORK;
            break;
        
        case ADD_NETWORK:
            if (wpa_is_running) {
                state = SET_ESSID;
                break;
            }
            if (netcfg_wpa_cmd("ADD_NETWORK"))
                state = PING;
            else
                state = SET_ESSID;
            break;
        
        case SET_ESSID:
            if (wpa_set_ssid(interface->essid))
                state = PING;
            else
                state = SET_PSK;
            break;
        
        case SET_PSK:
            if (wpa_set_psk(interface->passphrase))
                state = PING;
            else
                state = SET_SCAN_SSID;
            break;
        
        case SET_SCAN_SSID:
            if (netcfg_wpa_cmd("SET_NETWORK 0 scan_ssid 1"))
                state = PING;
            else
                state = ENABLE_NETWORK;
            break;
        
        case ENABLE_NETWORK:
             if (netcfg_wpa_cmd("ENABLE_NETWORK 0"))
                 state = PING;
             else
                 state = POLL;
             break;
        
        case POLL:
            if (poll_wpa_supplicant(client))
                state = ABORT;
            else
                state = SUCCESS;
            break;
             
        case ABORT:
            if (ctrl == NULL) 
                return GO_BACK;
            else {
                wpa_ctrl_close(ctrl);
                ctrl = NULL;
                return GO_BACK;
            }
             
         case SUCCESS:
             if (ctrl == NULL)
                 return 0;
             else {
                 wpa_ctrl_close(ctrl);
                 ctrl = NULL;
                 return 0;
             }
        }
    }
}