Patchwork [RFC,4/4] wpa_cli: print nice prompt when remote udp

login
register
mail settings
Submitter Janusz.Dziedzic@tieto.com
Date Aug. 2, 2012, 11:01 a.m.
Message ID <3078A9B976EF864C8DDD0C499FFD07912150C655E7@EXMB01.eu.tieto.com>
Download mbox | patch
Permalink /patch/174731/
State Accepted
Headers show

Comments

Janusz.Dziedzic@tieto.com - Aug. 2, 2012, 11:01 a.m.
When CONFIG_CTRL_IFACE=udp-remote is used print
user frendly PS in wpa_cli.
eg.
localhost/wlan0>
192.168.1.1/p2p-wlan-0-0>

Signed-hostap: Janusz Dziedzic <janusz.dziedzic@tieto.com>
---
 src/common/wpa_ctrl.c     |   19 +++++++++++++++++++
 src/common/wpa_ctrl.h     |    2 ++
 src/utils/edit.c          |    2 +-
 src/utils/edit.h          |    2 +-
 src/utils/edit_readline.c |    2 +-
 src/utils/edit_simple.c   |   12 ++++++++----
 wlantest/wlantest_cli.c   |    2 +-
 wpa_supplicant/wpa_cli.c  |   11 ++++++++---
 8 files changed, 41 insertions(+), 11 deletions(-)

Patch

diff --git a/src/common/wpa_ctrl.c b/src/common/wpa_ctrl.c
index fe07c02..bba42c1 100644
--- a/src/common/wpa_ctrl.c
+++ b/src/common/wpa_ctrl.c
@@ -47,6 +47,8 @@  struct wpa_ctrl {
 	struct sockaddr_in local;
 	struct sockaddr_in dest;
 	char *cookie;
+	char *remote_ifname;
+	char *remote_ip;
 #endif /* CONFIG_CTRL_IFACE_UDP */
 #ifdef CONFIG_CTRL_IFACE_UNIX
 	int s;
@@ -282,10 +284,12 @@  struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
 
 		printf("UDP remote host: %s, port: %d\n", name, port_id);
 		h = gethostbyname(name);
+		ctrl->remote_ip = strdup(name);
 		free(name);
 		if (h == NULL) {
 			perror("gethostbyname");
 			close(ctrl->s);
+			free(ctrl->remote_ip);
 			os_free(ctrl);
 			return NULL;
 		}
@@ -308,14 +312,29 @@  struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
 		ctrl->cookie = os_strdup(buf);
 	}
 
+	if (wpa_ctrl_request(ctrl, "IFNAME", 6, buf, &len, NULL) == 0) {
+		buf[len] = '\0';
+		ctrl->remote_ifname = os_strdup(buf);
+	}
+
 	return ctrl;
 }
 
+char *wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl)
+{
+#define WPA_CTRL_MAX_PS_NAME 100
+	static char ps[WPA_CTRL_MAX_PS_NAME]={};
+	os_snprintf(ps, WPA_CTRL_MAX_PS_NAME, "%s/%s",
+			ctrl->remote_ip, ctrl->remote_ifname);
+	return ps;
+}
 
 void wpa_ctrl_close(struct wpa_ctrl *ctrl)
 {
 	close(ctrl->s);
 	os_free(ctrl->cookie);
+	os_free(ctrl->remote_ifname);
+	os_free(ctrl->remote_ip);
 	os_free(ctrl);
 }
 
diff --git a/src/common/wpa_ctrl.h b/src/common/wpa_ctrl.h
index 0f306dd..e69a425 100644
--- a/src/common/wpa_ctrl.h
+++ b/src/common/wpa_ctrl.h
@@ -286,6 +286,8 @@  int wpa_ctrl_pending(struct wpa_ctrl *ctrl);
  */
 int wpa_ctrl_get_fd(struct wpa_ctrl *ctrl);
 
+char *wpa_ctrl_get_remote_ifname(struct wpa_ctrl *ctrl);
+
 #ifdef ANDROID
 /**
  * wpa_ctrl_cleanup() - Delete any local UNIX domain socket files that
diff --git a/src/utils/edit.c b/src/utils/edit.c
index 280d244..4ccef21 100644
--- a/src/utils/edit.c
+++ b/src/utils/edit.c
@@ -1112,7 +1112,7 @@  static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
 int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
 	      void (*eof_cb)(void *ctx),
 	      char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
-	      void *ctx, const char *history_file)
+	      void *ctx, const char *history_file, const char *ps)
 {
 	currbuf[0] = '\0';
 	dl_list_init(&history_list);
diff --git a/src/utils/edit.h b/src/utils/edit.h
index e8d04e7..ad27f1c 100644
--- a/src/utils/edit.h
+++ b/src/utils/edit.h
@@ -12,7 +12,7 @@ 
 int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
 	      void (*eof_cb)(void *ctx),
 	      char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
-	      void *ctx, const char *history_file);
+	      void *ctx, const char *history_file, const char *ps);
 void edit_deinit(const char *history_file,
 		 int (*filter_cb)(void *ctx, const char *cmd));
 void edit_clear_line(void);
diff --git a/src/utils/edit_readline.c b/src/utils/edit_readline.c
index c9ff0e6..9c27d72 100644
--- a/src/utils/edit_readline.c
+++ b/src/utils/edit_readline.c
@@ -112,7 +112,7 @@  static void readline_cmd_handler(char *cmd)
 int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
 	      void (*eof_cb)(void *ctx),
 	      char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
-	      void *ctx, const char *history_file)
+	      void *ctx, const char *history_file, const char *ps)
 {
 	edit_cb_ctx = ctx;
 	edit_cmd_cb = cmd_cb;
diff --git a/src/utils/edit_simple.c b/src/utils/edit_simple.c
index 0e3637c..740d346 100644
--- a/src/utils/edit_simple.c
+++ b/src/utils/edit_simple.c
@@ -22,6 +22,7 @@  static void (*edit_cmd_cb)(void *ctx, char *cmd);
 static void (*edit_eof_cb)(void *ctx);
 
 
+static const char *ps2 = NULL;
 static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
 {
 	int c;
@@ -41,7 +42,7 @@  static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
 		cmdbuf[cmdbuf_pos] = '\0';
 		cmdbuf_pos = 0;
 		edit_cmd_cb(edit_cb_ctx, cmdbuf);
-		printf("> ");
+		printf("%s> ", ps2);
 		fflush(stdout);
 		return;
 	}
@@ -53,18 +54,21 @@  static void edit_read_char(int sock, void *eloop_ctx, void *sock_ctx)
 	}
 }
 
-
 int edit_init(void (*cmd_cb)(void *ctx, char *cmd),
 	      void (*eof_cb)(void *ctx),
 	      char ** (*completion_cb)(void *ctx, const char *cmd, int pos),
-	      void *ctx, const char *history_file)
+	      void *ctx, const char *history_file, const char *ps)
 {
 	edit_cb_ctx = ctx;
 	edit_cmd_cb = cmd_cb;
 	edit_eof_cb = eof_cb;
 	eloop_register_read_sock(STDIN_FILENO, edit_read_char, NULL, NULL);
+	if (ps)
+		ps2 = ps;
+	else
+		ps2 = "";
 
-	printf("> ");
+	printf("%s> ", ps2);
 	fflush(stdout);
 
 	return 0;
diff --git a/wlantest/wlantest_cli.c b/wlantest/wlantest_cli.c
index ce95081..a417b3c 100644
--- a/wlantest/wlantest_cli.c
+++ b/wlantest/wlantest_cli.c
@@ -1663,7 +1663,7 @@  static void wlantest_cli_interactive(int s)
 	cli.s = s;
 	eloop_register_signal_terminate(wlantest_cli_eloop_terminate, &cli);
 	edit_init(wlantest_cli_edit_cmd_cb, wlantest_cli_edit_eof_cb,
-		  wlantest_cli_edit_completion_cb, &cli, hfile);
+		  wlantest_cli_edit_completion_cb, &cli, hfile, NULL);
 
 	eloop_run();
 
diff --git a/wpa_supplicant/wpa_cli.c b/wpa_supplicant/wpa_cli.c
index 7a06f09..fe3b7eb 100644
--- a/wpa_supplicant/wpa_cli.c
+++ b/wpa_supplicant/wpa_cli.c
@@ -82,6 +82,7 @@  static const char *action_file = NULL;
 static int ping_interval = 5;
 static int interactive = 0;
 
+
 struct cli_txt_entry {
 	struct dl_list list;
 	char *txt;
@@ -3801,7 +3802,7 @@  static void wpa_cli_edit_eof_cb(void *ctx)
 }
 
 
-static void wpa_cli_interactive(void)
+static void wpa_cli_interactive(const char *ps)
 {
 	char *home, *hfile = NULL;
 
@@ -3818,7 +3819,7 @@  static void wpa_cli_interactive(void)
 
 	eloop_register_signal_terminate(wpa_cli_eloop_terminate, NULL);
 	edit_init(wpa_cli_edit_cmd_cb, wpa_cli_edit_eof_cb,
-		  wpa_cli_edit_completion_cb, NULL, hfile);
+		  wpa_cli_edit_completion_cb, NULL, hfile, ps);
 	eloop_register_timeout(ping_interval, 0, wpa_cli_ping, NULL, NULL);
 
 	eloop_run();
@@ -4076,7 +4077,11 @@  int main(int argc, char *argv[])
 		return -1;
 
 	if (interactive)
-		wpa_cli_interactive();
+#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
+		wpa_cli_interactive(wpa_ctrl_get_remote_ifname(ctrl_conn));
+#else
+		wpa_cli_interactive(NULL);
+#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 	else if (action_file)
 		wpa_cli_action(ctrl_conn);
 	else