コード例 #1
0
ファイル: wpactrl.c プロジェクト: Saij/pywpactrl
/* ------------------------------------------------------------------------------ WPACtrl_attach */
static PyObject* WPACtrl_attach(WPACtrl* self)
{
	int ret;

	if (self->attached == 1)
		Py_RETURN_NONE;

	ret = wpa_ctrl_attach(self->ctrl_iface);

	switch (ret) {
	case 0:
		self->attached = 1;
		Py_RETURN_NONE;
	case -1:
		PyErr_SetString(WPACtrl_error, "wpa_ctrl_attach failed");
		break;
	case -2:
		PyErr_SetString(WPACtrl_error, "wpa_ctrl_attach timed out");
		break;
	default:
		PyErr_SetString(WPACtrl_error, "wpa_ctrl_attach returned unknown error");
		break;
	}

	return NULL;
}
コード例 #2
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;
}
コード例 #3
0
ファイル: WiFiNode.cpp プロジェクト: schapa/OrangeWiFi
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;
}
コード例 #4
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));
    }
     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;
}
コード例 #5
0
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();
	os_free(ctrl_ifname);
	ctrl_ifname = os_strdup(argv[0]);
	if (ctrl_ifname == NULL)
		return -1;

	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;
}
コード例 #6
0
ファイル: wpa_cli.c プロジェクト: flwh/Alcatel_OT_985_kernel
static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
{
	if (argc < 1) {
		wpa_cli_list_interfaces(ctrl);
		return 0;
	}

	wpa_cli_close_connection();
	os_free(ctrl_ifname);
	ctrl_ifname = os_strdup(argv[0]);

	if ((ctrl_conn = wpa_cli_open_connection(ctrl_ifname)) != NULL) {
		printf("Connected to interface '%s.\n", ctrl_ifname);
		if (wpa_ctrl_attach(monitor_conn) == 0) {
			wpa_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to "
			       "wpa_supplicant.\n");
		}
	} else {
		printf("Could not connect to interface '%s' - re-trying\n",
			ctrl_ifname);
	}
	return 0;
}
コード例 #7
0
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;
}
コード例 #8
0
static int hostapd_cli_reconnect(const char *ifname)
{
	char *next_ctrl_ifname;

	hostapd_cli_close_connection();

	if (!ifname)
		return -1;

	next_ctrl_ifname = os_strdup(ifname);
	os_free(ctrl_ifname);
	ctrl_ifname = next_ctrl_ifname;
	if (!ctrl_ifname)
		return -1;

	ctrl_conn = hostapd_cli_open_connection(ctrl_ifname);
	if (!ctrl_conn)
		return -1;
	if (!interactive && !action_file)
		return 0;
	if (wpa_ctrl_attach(ctrl_conn) == 0) {
		hostapd_cli_attached = 1;
		register_event_handler(ctrl_conn);
		update_stations(ctrl_conn);
	} else {
		printf("Warning: Failed to attach to hostapd.\n");
	}
	return 0;
}
コード例 #9
0
ファイル: wpatalk.c プロジェクト: KHATEEBNSIT/AP
/* 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;
}
コード例 #10
0
ファイル: wpa_helpers.c プロジェクト: 2asoft/freebsd
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;
}
コード例 #11
0
ファイル: wpa_cli.c プロジェクト: flwh/Alcatel_OT_985_kernel
static void wpa_cli_reconnect(void)
{
	wpa_cli_close_connection();
	ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
	if (ctrl_conn) {
		printf("Connection to wpa_supplicant re-established\n");
		if (wpa_ctrl_attach(monitor_conn) == 0) {
			wpa_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to "
			       "wpa_supplicant.\n");
		}
	}
}
コード例 #12
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;
}
コード例 #13
0
ファイル: wifi.c プロジェクト: TooLogic/allwinner_board_files
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;
}
コード例 #14
0
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;
}
コード例 #15
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;
}
コード例 #16
0
ファイル: wifi.c プロジェクト: XuQiufeng/CWifimanger
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;
}
コード例 #17
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);
}
コード例 #18
0
static void hostapd_cli_ping(void *eloop_ctx, void *timeout_ctx)
{
	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, 0);
	eloop_register_timeout(ping_interval, 0, hostapd_cli_ping, NULL, NULL);
}
コード例 #19
0
static void wpa_cli_alarm(int sig)
{
	if (ctrl_conn && _wpa_ctrl_command(ctrl_conn, "PING", 0)) {
		printf("Connection to wpa_supplicant lost - trying to "
		       "reconnect\n");
		wpa_cli_close_connection();
	}
	if (!ctrl_conn) {
		ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
		if (ctrl_conn) {
			printf("Connection to wpa_supplicant "
			       "re-established\n");
			if (wpa_ctrl_attach(ctrl_conn) == 0) {
				wpa_cli_attached = 1;
			} else {
				printf("Warning: Failed to attach to "
				       "wpa_supplicant.\n");
			}
		}
	}
	if (ctrl_conn)
		wpa_cli_recv_pending(ctrl_conn, 1, 0);
	alarm(1);
}
コード例 #20
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;
}
コード例 #21
0
ファイル: wpa_cli.c プロジェクト: flwh/Alcatel_OT_985_kernel
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;
}
コード例 #22
0
static void wpa_cli_reconnect(void)
{
	wpa_cli_close_connection();
	ctrl_conn = wpa_cli_open_connection(ctrl_ifname);
	if (ctrl_conn) {
		printf("Connection to wpa_supplicant re-established\n");
#ifdef ANDROID
		if (wpa_ctrl_attach(monitor_conn) == 0) {
#else
		if (wpa_ctrl_attach(ctrl_conn) == 0) {
#endif
			wpa_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to "
			       "wpa_supplicant.\n");
		}
	}
}


static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int in_read,
				 int action_monitor)
{
	int first = 1;
#ifdef ANDROID
	if (ctrl == NULL) {
#else
	if (ctrl_conn == NULL) {
#endif 
		wpa_cli_reconnect();
		return;
	}
	while (wpa_ctrl_pending(ctrl) > 0) {
		char buf[256];
		size_t len = sizeof(buf) - 1;
		if (wpa_ctrl_recv(ctrl, buf, &len) == 0) {
			buf[len] = '\0';
			if (action_monitor)
				wpa_cli_action_process(buf);
			else {
				if (in_read && first)
					printf("\n");
				first = 0;
				printf("%s\n", buf);
			}
		} else {
			printf("Could not read pending message.\n");
			break;
		}
	}

	if (wpa_ctrl_pending(ctrl) < 0) {
		printf("Connection to wpa_supplicant lost - trying to "
		       "reconnect\n");
		wpa_cli_reconnect();
	}
}


#ifdef CONFIG_READLINE
static char * wpa_cli_cmd_gen(const char *text, int state)
{
	static int i, len;
	const char *cmd;

	if (state == 0) {
		i = 0;
		len = os_strlen(text);
	}

	while ((cmd = wpa_cli_commands[i].cmd)) {
		i++;
		if (os_strncasecmp(cmd, text, len) == 0)
			return os_strdup(cmd);
	}

	return NULL;
}


static char * wpa_cli_dummy_gen(const char *text, int state)
{
	return NULL;
}


static char ** wpa_cli_completion(const char *text, int start, int end)
{
	return rl_completion_matches(text, start == 0 ?
				     wpa_cli_cmd_gen : wpa_cli_dummy_gen);
}
#endif /* CONFIG_READLINE */


static void wpa_cli_interactive(void)
{
#define max_args 10
	char cmdbuf[256], *cmd, *argv[max_args], *pos;
	int argc;
#ifdef CONFIG_READLINE
	char *home, *hfile = NULL;
#endif /* CONFIG_READLINE */

	printf("\nInteractive mode\n\n");

#ifdef CONFIG_READLINE
	rl_attempted_completion_function = wpa_cli_completion;
	home = getenv("HOME");
	if (home) {
		const char *fname = ".wpa_cli_history";
		int hfile_len = os_strlen(home) + 1 + os_strlen(fname) + 1;
		hfile = os_malloc(hfile_len);
		if (hfile) {
			int res;
			res = os_snprintf(hfile, hfile_len, "%s/%s", home,
					  fname);
			if (res >= 0 && res < hfile_len) {
				hfile[hfile_len - 1] = '\0';
				read_history(hfile);
				stifle_history(100);
			}
		}
	}
#endif /* CONFIG_READLINE */

	do {
#ifdef ANDROID
		wpa_cli_recv_pending(monitor_conn, 0, 0);
#else
		wpa_cli_recv_pending(ctrl_conn, 0, 0);
#endif
#ifndef CONFIG_NATIVE_WINDOWS
		alarm(ping_interval);
#endif /* CONFIG_NATIVE_WINDOWS */
#ifdef CONFIG_READLINE
		cmd = readline("> ");
		if (cmd && *cmd) {
			HIST_ENTRY *h;
			while (next_history())
				;
			h = previous_history();
			if (h == NULL || os_strcmp(cmd, h->line) != 0)
				add_history(cmd);
			next_history();
		}
#else /* CONFIG_READLINE */
		printf("> ");
		cmd = fgets(cmdbuf, sizeof(cmdbuf), stdin);
#endif /* CONFIG_READLINE */
#ifndef CONFIG_NATIVE_WINDOWS
		alarm(0);
#endif /* CONFIG_NATIVE_WINDOWS */
		if (cmd == NULL)
			break;
#ifdef ANDROID
		wpa_cli_recv_pending(monitor_conn, 0, 0);
#else
		wpa_cli_recv_pending(ctrl_conn, 0, 0);
#endif
		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;
			if (*pos == '"') {
				char *pos2 = os_strrchr(pos, '"');
				if (pos2)
					pos = pos2 + 1;
			}
			while (*pos != '\0' && *pos != ' ')
				pos++;
			if (*pos == ' ')
				*pos++ = '\0';
		}
		if (argc)
			wpa_request(ctrl_conn, argc, argv);

		if (cmd != cmdbuf)
			os_free(cmd);
	} while (!wpa_cli_quit);

#ifdef CONFIG_READLINE
	if (hfile) {
		/* Save command history, excluding lines that may contain
		 * passwords. */
		HIST_ENTRY *h;
		history_set_pos(0);
		while ((h = current_history())) {
			char *p = h->line;
			while (*p == ' ' || *p == '\t')
				p++;
			if (cmd_has_sensitive_data(p)) {
				h = remove_history(where_history());
				if (h) {
					os_free(h->line);
					os_free(h->data);
					os_free(h);
				} else
					next_history();
			} else
				next_history();
		}
		write_history(hfile);
		os_free(hfile);
	}
#endif /* CONFIG_READLINE */
}
コード例 #23
0
ファイル: wpagui.cpp プロジェクト: LucidOne/Rovio
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;
}
コード例 #24
0
static int wpa_cli_cmd_interface(struct wpa_ctrl *ctrl, int argc, char *argv[])
{
	if (argc < 1) {
		wpa_cli_list_interfaces(ctrl);
		return 0;
	}

	wpa_cli_close_connection();
	os_free(ctrl_ifname);
	ctrl_ifname = os_strdup(argv[0]);
#ifdef ANDROID
	if ((ctrl_conn = wpa_cli_open_connection(ctrl_ifname)) != NULL) {
#else
	if (wpa_cli_open_connection(ctrl_ifname)) {
#endif
		printf("Connected to interface '%s.\n", ctrl_ifname);
#ifdef ANDROID
		if (wpa_ctrl_attach(monitor_conn) == 0) {
#else
		if (wpa_ctrl_attach(ctrl_conn) == 0) {
#endif
			wpa_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to "
			       "wpa_supplicant.\n");
		}
	} else {
		printf("Could not connect to interface '%s' - re-trying\n",
		       ctrl_ifname);
	}
	return 0;
}


static int wpa_cli_cmd_reconfigure(struct wpa_ctrl *ctrl, int argc,
				   char *argv[])
{
	return wpa_ctrl_command(ctrl, "RECONFIGURE");
}


static int wpa_cli_cmd_terminate(struct wpa_ctrl *ctrl, int argc,
				 char *argv[])
{
	return wpa_ctrl_command(ctrl, "TERMINATE");
}


static int wpa_cli_cmd_interface_add(struct wpa_ctrl *ctrl, int argc,
				     char *argv[])
{
	char cmd[256];
	int res;

	if (argc < 1) {
		printf("Invalid INTERFACE_ADD command: needs at least one "
		       "argument (interface name)\n"
		       "All arguments: ifname confname driver ctrl_interface "
		       "driver_param bridge_name\n");
		return -1;
	}

	/*
	 * INTERFACE_ADD <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB
	 * <driver_param>TAB<bridge_name>
	 */
	res = os_snprintf(cmd, sizeof(cmd),
			  "INTERFACE_ADD %s\t%s\t%s\t%s\t%s\t%s",
			  argv[0],
			  argc > 1 ? argv[1] : "", argc > 2 ? argv[2] : "",
			  argc > 3 ? argv[3] : "", argc > 4 ? argv[4] : "",
			  argc > 5 ? argv[5] : "");
	if (res < 0 || (size_t) res >= sizeof(cmd))
		return -1;
	cmd[sizeof(cmd) - 1] = '\0';
	return wpa_ctrl_command(ctrl, cmd);
}


static int wpa_cli_cmd_interface_remove(struct wpa_ctrl *ctrl, int argc,
					char *argv[])
{
	char cmd[128];
	int res;

	if (argc != 1) {
		printf("Invalid INTERFACE_REMOVE command: needs one argument "
		       "(interface name)\n");
		return -1;
	}

	res = os_snprintf(cmd, sizeof(cmd), "INTERFACE_REMOVE %s", argv[0]);
	if (res < 0 || (size_t) res >= sizeof(cmd))
		return -1;
	cmd[sizeof(cmd) - 1] = '\0';
	return wpa_ctrl_command(ctrl, cmd);
}
コード例 #25
0
ファイル: hostapd_cli.c プロジェクト: PavanKulk/codebase
int main(int argc, char *argv[])
{
	int warning_displayed = 0;
	int c;
	int daemonize = 0;

        printf("In hostapd cli main\n");

	if (os_program_init())
		return -1;

	for (;;) {
		c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
		if (c < 0) {
			break;
                        printf("c<0 getting out of for loop\n");
                }
		switch (c) {
		case 'a':
			action_file = optarg;
			break;
		case 'B':
			daemonize = 1;
			break;
		case 'G':
			ping_interval = atoi(optarg);
			break;
		case 'h':
			usage();
			return 0;
		case 'v':
			printf("%s\n", hostapd_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;
		case 's':
			client_socket_dir = optarg;
			break;
		default:
			usage();
			return -1;
		}
	}

	interactive = (argc == optind) && (action_file == NULL);

	if (interactive) {
		printf("%s\n\n%s\n\n", hostapd_cli_version,
		       hostapd_cli_license);
	}

	if (eloop_init())
		return -1;

	for (;;) {
		if (ctrl_ifname == NULL) {
			struct dirent *dent;
			DIR *dir = opendir(ctrl_iface_dir);
			if (dir) {
				while ((dent = readdir(dir))) {
					if (os_strcmp(dent->d_name, ".") == 0
					    ||
					    os_strcmp(dent->d_name, "..") == 0)
						continue;
					printf("Selected interface '%s'\n",
					       dent->d_name);
					ctrl_ifname = os_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;
		}
		os_sleep(1, 0);
		continue;
	}

	if (interactive || action_file) {
		if (wpa_ctrl_attach(ctrl_conn) == 0) {
			hostapd_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to hostapd.\n");
			if (action_file)
				return -1;
		}
	}

	if (daemonize && os_daemonize(pid_file))
		return -1;
      
        //sharing pid start
        int ShmID;      
        pid_t *ShmPTR;
        pid_t pid = getpid();
        key_t MyKey;
        MyKey = 1234;
        ShmID   = shmget(MyKey, sizeof(pid_t), IPC_CREAT | 0666);
        ShmPTR  = (pid_t *) shmat(ShmID, NULL, 0);
        *ShmPTR = pid;
        //sharing pid end
 
        if (signal(SIGINT, SIGINT_handler) == SIG_ERR) {
            printf("SIGINT install error\n");
            exit(1);
        }

        if (signal(SIGQUIT, SIGQUIT_handler) == SIG_ERR) {
            printf("SIGQUIT install error\n");
            exit(1);
        }

        printf("Waiting for the signals from HTTP server to arrive\n");
        while (1);

        /*..derecated from the original implementation..
        hostapd_cli_list_interfaces(ctrl_conn);

	os_free(ctrl_ifname);
	eloop_destroy();
	hostapd_cli_cleanup();
	return 0;
        */
}
コード例 #26
0
ファイル: main.c プロジェクト: KHATEEBNSIT/AP
int main(int argc, char *argv[])
{
    int i, j; 
    int c, res, ret = -1;
    const char *global_wpa_s_ctrl_intf = NULL;
    const char *wrapd_ctrl_intf = NULL;
    const char *vma_conf_file = NULL;
    const char *wpa_s_conf_file = NULL;
    const char *add_psta_addr = NULL;
    const char *remove_psta_addr = NULL;

    int daemonize = 0;
    int hostapd_num = 0;
    int list_psta_addr = 0;
    int do_mat = 0;
    int do_isolation = 0;
    int do_timer = 0;
    int conn_cnt = 0;
    int slave_mode = 0;
    char msg[128] = {0};
    
    if (os_program_init())
        return -1;

    for (i = 0; i < HOSTAPD_CNT; i ++) { 
        ap_ifname[i] = NULL;
        wrapd_hostapd_conn[i] = NULL;
    }

    for (;;) {
        c = getopt(argc, argv, "g:a:p:w:A:R:BLMSITc:v:d:h");
        if (c < 0)
            break;
        switch (c) {
            case 'g':  
                wrapd_ctrl_intf = optarg;
                break;            
            case 'w':
                global_wpa_s_ctrl_intf = optarg;
                break;
            case 'a':
                if (hostapd_num >= HOSTAPD_CNT) {
                    usage(); 
                    goto out;
                }                    
                ap_ifname[hostapd_num ++] = os_strdup(optarg);
                break;
            case 'p':
                mpsta_ifname = os_strdup(optarg);
                break;       
            case 'd':
                dbdc_ifname = os_strdup(optarg);
                break;                      
		    case 'B':
			    daemonize++;
			    break;                
            case 'A':
                add_psta_addr = optarg;
                break;
            case 'R':
                remove_psta_addr = optarg;
                break;  
            case 'L':     
                list_psta_addr = 1;
                break;  
            case 'M':     
                do_mat = 1;
                break;    
            case 'I':     
                do_isolation = 1;
                break;  
            case 'S':     
                slave_mode = 1;
                break;    
            case 'T':
                do_timer = 1;
                break;               
            case 'c':
                wpa_s_conf_file = optarg;
                break;
            case 'v':
                vma_conf_file = optarg;
                break;   
            case 'h':
                usage(); 
                ret = 0;
                goto out;
            default:
                usage();
                goto out;

        }
    }

    for (i = 0; i < hostapd_num - 1; i ++) {
        for (j = i + 1; j < hostapd_num; j ++) {
            if (os_strcmp(ap_ifname[i], ap_ifname[j]) == 0) {
                wrapd_printf("duplicated ap_ifname[%d] of ap_ifname[%d]", i, j);
                goto out;
            }
        }
    }

    if(NULL == wrapd_ctrl_intf)
        wrapd_ctrl_intf = wrapd_ctrl_iface_path;

    if (slave_mode) {
        if(add_psta_addr) {
            if (do_mat) {
                if (dbdc_ifname) {
                    wrapd_printf("Invalid MAT option, DBDC is enabled");
                    goto out;                  
                }
                res = os_snprintf(msg, sizeof(msg),"ETH_PSTA_ADD MAT %s %s", ap_ifname[0], add_psta_addr);
            } else
                res = os_snprintf(msg, sizeof(msg),"ETH_PSTA_ADD %s %s", ap_ifname[0], add_psta_addr);
                
            if (res < 0 || res >= sizeof(msg)){
                wrapd_printf("Fail to build ETH_PSTA_ADD msg"); 
                goto out;
            }
            wrapd_send_msg(msg, 128, wrapd_ctrl_intf);
            ret = 0;
            goto out;
            
        } else if (remove_psta_addr) {
            res = os_snprintf(msg, sizeof(msg),"ETH_PSTA_REMOVE %s", remove_psta_addr);
            if (res < 0 || res >= sizeof(msg)){
                wrapd_printf("Fail to build ETH_PSTA_REMOVE msg"); 
                goto out;
            }
            wrapd_send_msg(msg, (16 + 17), wrapd_ctrl_intf);
            ret = 0;
            goto out;

        } else if (list_psta_addr) {
            wrapd_send_msg("PSTA_LIST", 9, wrapd_ctrl_intf);
            ret = 0;
            goto out;
        }
    } 

    if(NULL == global_wpa_s_ctrl_intf)
        global_wpa_s_ctrl_intf = global_wpa_s_ctrl_iface_path;

    if (eloop_init()) {
        wrapd_printf("Failed to initialize event loop");
        goto out;
    }
    
    wrapd_handle = wrapd_conn_to_global_wpa_s(global_wpa_s_ctrl_intf, wpa_s_conf_file, do_isolation, do_timer);
    if (wrapd_handle == NULL) 
        goto out;

    wrapd_conn = wrapd_ctrl_open(wrapd_ctrl_intf, wrapd_handle);

    for (i = 0; i < HOSTAPD_CNT; i ++) { 
        if(ap_ifname[i]) {
            for (conn_cnt = 0; conn_cnt < HOSTAPD_CONN_TIMES; conn_cnt ++) {
                wrapd_hostapd_conn[i] = wrapd_conn_to_hostapd(ap_ifname[i]);
                if (wrapd_hostapd_conn[i]) {
                    wrapd_printf("WRAP hostapd(%s) connected", ap_ifname[i]);
                    break;
                }
                os_sleep(1, 0);
            }   
            if(wrapd_hostapd_conn[i]) {
                if (wpa_ctrl_attach((struct wpa_ctrl *)wrapd_hostapd_conn[i]) != 0) {
                    wrapd_printf("Failed to attach to WRAP hostapd(%s)", ap_ifname[i]);;
                    goto out;
                }
                wrapd_printf("WRAP hostapd(%s) attached", ap_ifname[i]);
                eloop_register_read_sock(wrapd_hostapd_conn[i]->sock, wrapd_hostapd_ctrl_iface_receive, wrapd_handle, (void *)ap_ifname[i]); 
            } else {
                wrapd_printf("WRAP hostapd(%s) not exists", ap_ifname[i]);
            }
        }
    }

    if(mpsta_ifname == NULL) {
        wrapd_printf("Failed to connect to MPSTA wpa_s - mpsta_ifname == NULL");
        goto out;
    }    

    for (conn_cnt = 0; conn_cnt < WPA_S_CONN_TIMES; conn_cnt ++) {
        /*
         * Delay to ensure scan doesn't overlap with ht40 intol acs scan, else will cause 
         * scan to fail and will take more time for MPSTA to associate.
         * EV 131644 
         */
        if(conn_cnt == 0)
            os_sleep(3, 0);
        wrapd_wpa_s_conn = wrapd_conn_to_mpsta_wpa_s(mpsta_ifname);
        if (wrapd_wpa_s_conn) {
            wrapd_printf("MPSTA wpa_s(%s) connected", mpsta_ifname);
            break;
        }
        os_sleep(1, 0);
    }   
    if(wrapd_wpa_s_conn) {
        if (wpa_ctrl_attach((struct wpa_ctrl *)wrapd_wpa_s_conn) != 0) {
            wrapd_printf("Failed to attach to MPSTA wpa_s(%s)", mpsta_ifname);
            goto out;
        }
        wrapd_printf("MPSTA wpa_s(%s) attached", mpsta_ifname);
        eloop_register_read_sock(wrapd_wpa_s_conn->sock, wrapd_wpa_s_ctrl_iface_receive, wrapd_handle, NULL); 
    } else {
        wrapd_printf("MPSTA wpa_s(%s) not exists", mpsta_ifname);
    }

    if (vma_conf_file) {
        wrapd_load_vma_list(vma_conf_file, wrapd_handle);
    }

	if (daemonize && os_daemonize(NULL)) {
		wrapd_printf("daemon");
		goto out;
	}

    eloop_run();

out:

    for (i = 0; i < HOSTAPD_CNT; i ++) { 
        if (ap_ifname[i])
            os_free(ap_ifname[i]);
            
        if (wrapd_hostapd_conn[i])
            wpa_ctrl_close((struct wpa_ctrl *)wrapd_hostapd_conn[i]);
    }
        
    if (dbdc_ifname)
        os_free(dbdc_ifname);
    if (mpsta_ifname)
        os_free(mpsta_ifname);

    if (wrapd_wpa_s_conn)
        wpa_ctrl_close((struct wpa_ctrl *)wrapd_wpa_s_conn); 
    
	os_program_deinit();    
    
    return ret;  
    
}
コード例 #27
0
int main(int argc, char *argv[])
{
	int warning_displayed = 0;
	int c;
	int daemonize = 0;

	if (os_program_init())
		return -1;

	for (;;) {
		c = getopt(argc, argv, "a:BhG:i:p:v");
		if (c < 0)
			break;
		switch (c) {
		case 'a':
			action_file = optarg;
			break;
		case 'B':
			daemonize = 1;
			break;
		case 'G':
			ping_interval = atoi(optarg);
			break;
		case 'h':
			usage();
			return 0;
		case 'v':
			printf("%s\n", hostapd_cli_version);
			return 0;
		case 'i':
			os_free(ctrl_ifname);
			ctrl_ifname = os_strdup(optarg);
			break;
		case 'p':
			ctrl_iface_dir = optarg;
			break;
		default:
			usage();
			return -1;
		}
	}

	interactive = (argc == optind) && (action_file == NULL);

	if (interactive) {
		printf("%s\n\n%s\n\n", hostapd_cli_version,
		       hostapd_cli_license);
	}

	if (eloop_init())
		return -1;

	for (;;) {
		if (ctrl_ifname == NULL) {
			struct dirent *dent;
			DIR *dir = opendir(ctrl_iface_dir);
			if (dir) {
				while ((dent = readdir(dir))) {
					if (os_strcmp(dent->d_name, ".") == 0
					    ||
					    os_strcmp(dent->d_name, "..") == 0)
						continue;
					printf("Selected interface '%s'\n",
					       dent->d_name);
					ctrl_ifname = os_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;
		}
		os_sleep(1, 0);
		continue;
	}

	if (interactive || action_file) {
		if (wpa_ctrl_attach(ctrl_conn) == 0) {
			hostapd_cli_attached = 1;
		} else {
			printf("Warning: Failed to attach to hostapd.\n");
			if (action_file)
				return -1;
		}
	}

	if (daemonize && os_daemonize(pid_file))
		return -1;

	if (interactive)
		hostapd_cli_interactive();
	else if (action_file)
		hostapd_cli_action(ctrl_conn);
	else
		wpa_request(ctrl_conn, argc - optind, &argv[optind]);

	os_free(ctrl_ifname);
	eloop_destroy();
	hostapd_cli_cleanup();
	return 0;
}
コード例 #28
0
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;
}
コード例 #29
0
ファイル: wpagui.cpp プロジェクト: OSLL/avmconf
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;
}