[PATCH] Add "reconnect" cmdline argument to hostapd_cli/wpa_cli

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Bilal Hatipoglu <bilal.hatipoglu@xxxxxxxxxxx>

When newly added "-r" parameter is used, both clis will try to
reconnect forever on connection lost until signalled (ctrl+c) or terminated.
This is useful for using a single instance cli to retrieve events & get
status and the cli process stays even if hostapd/wpa_supplicant daemons
restart for some reason (e.g. configuration change).

Signed-off-by: Veli Demirel <veli.demirel@xxxxxxxxxxx>
---
 hostapd/hostapd_cli.c    | 20 +++++++++++++++-----
 wpa_supplicant/wpa_cli.c | 35 ++++++++++++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/hostapd/hostapd_cli.c b/hostapd/hostapd_cli.c
index 0460243..07b6ed0 100644
--- a/hostapd/hostapd_cli.c
+++ b/hostapd/hostapd_cli.c
@@ -2011,12 +2011,13 @@ int main(int argc, char *argv[])
 	int warning_displayed = 0;
 	int c;
 	int daemonize = 0;
+	int reconnect = 0;
 
 	if (os_program_init())
 		return -1;
 
 	for (;;) {
-		c = getopt(argc, argv, "a:BhG:i:p:P:s:v");
+		c = getopt(argc, argv, "a:BhG:i:p:P:s:vr");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -2048,6 +2049,9 @@ int main(int argc, char *argv[])
 		case 's':
 			client_socket_dir = optarg;
 			break;
+		case 'r':
+			reconnect = 1;
+			break;
 		default:
 			usage();
 			return -1;
@@ -2087,8 +2091,7 @@ int main(int argc, char *argv[])
 				printf("Connection established.\n");
 			break;
 		}
-
-		if (!interactive) {
+		if (!interactive && !reconnect) {
 			perror("Failed to connect to hostapd - "
 			       "wpa_ctrl_open");
 			return -1;
@@ -2106,8 +2109,15 @@ int main(int argc, char *argv[])
 		return -1;
 	if (daemonize && os_daemonize(pid_file) && eloop_sock_requeue())
 		return -1;
-
-	if (interactive)
+	if (reconnect && action_file && ctrl_ifname != NULL) {
+		do {
+			if (ctrl_conn) {
+				hostapd_cli_action(ctrl_conn);
+			}
+			os_sleep(1, 0);
+			hostapd_cli_reconnect(ctrl_ifname);
+		} while(!hostapd_cli_quit);
+	} else if (interactive)
 		hostapd_cli_interactive();
 	else if (action_file)
 		hostapd_cli_action(ctrl_conn);
diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c
index 43ac427..1c7d885 100644
--- a/wpa_supplicant/wpa_cli.c
+++ b/wpa_supplicant/wpa_cli.c
@@ -52,6 +52,7 @@ static char *ctrl_ifname = NULL;
 static const char *global = NULL;
 static const char *pid_file = NULL;
 static const char *action_file = NULL;
+static int reconnect = 0;
 static int ping_interval = 5;
 static int interactive = 0;
 static char *ifname_prefix = NULL;
@@ -4039,7 +4040,9 @@ static void wpa_cli_action_process(const char *msg)
 		wpa_cli_exec(action_file, ifname, pos);
 	} else if (str_starts(pos, WPA_EVENT_TERMINATING)) {
 		printf("wpa_supplicant is terminating - stop monitoring\n");
-		wpa_cli_quit = 1;
+		if (!reconnect) {
+			wpa_cli_quit = 1;
+		}
 	}
 }
 
@@ -4231,6 +4234,10 @@ static void wpa_cli_recv_pending(struct wpa_ctrl *ctrl, int action_monitor)
 	if (wpa_ctrl_pending(ctrl) < 0) {
 		printf("Connection to wpa_supplicant lost - trying to "
 		       "reconnect\n");
+		if (reconnect) {
+			eloop_terminate();
+			return;
+		}
 		wpa_cli_reconnect();
 	}
 }
@@ -4578,6 +4585,9 @@ static void wpa_cli_cleanup(void)
 static void wpa_cli_terminate(int sig, void *ctx)
 {
 	eloop_terminate();
+	if (reconnect) {
+		wpa_cli_quit = 1;
+	}
 }
 
 
@@ -4655,7 +4665,7 @@ int main(int argc, char *argv[])
 		return -1;
 
 	for (;;) {
-		c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:v");
+		c = getopt(argc, argv, "a:Bg:G:hi:p:P:s:vr");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -4690,6 +4700,9 @@ int main(int argc, char *argv[])
 		case 's':
 			client_socket_dir = optarg;
 			break;
+		case 'r':
+			reconnect = 1;
+			break;
 		default:
 			usage();
 			return -1;
@@ -4712,7 +4725,23 @@ int main(int argc, char *argv[])
 	if (ctrl_ifname == NULL)
 		ctrl_ifname = wpa_cli_get_default_ifname();
 
-	if (interactive) {
+	if (reconnect && action_file && ctrl_ifname) {
+		do {
+			if (ctrl_conn) {
+				wpa_cli_action(ctrl_conn);
+			} else
+				os_sleep(1, 0);
+			wpa_cli_close_connection();
+			wpa_cli_open_connection(ctrl_ifname, 0);
+			if (ctrl_conn) {
+				if (wpa_ctrl_attach(ctrl_conn) != 0) {
+					wpa_cli_close_connection();
+				} else {
+					wpa_cli_attached = 1;
+				}
+			}
+		} while (!wpa_cli_quit);
+	} else if (interactive) {
 		wpa_cli_interactive();
 	} else {
 		if (!global &&
-- 
2.7.4



This message has been scanned for malware by Websense. www.websense.com

_______________________________________________
Hostap mailing list
Hostap@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/hostap



[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux