Patchwork [RFC,2/4] wpa_cli: add support for remote access

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

Comments

Janusz.Dziedzic@tieto.com - Aug. 2, 2012, 11:01 a.m.
wpa_cli can be used now as a client for
remote access to ctrl_interface of wpa_supplicant
when UDP and remote options are used.

You can simply run:
wpa_cli -i <hostname>:[port]
wpa_cli -i <IP>:[port]

Signed-hostap: Janusz Dziedzic <janusz.dziedzic@tieto.com>
---
 src/common/wpa_ctrl.c |   39 +++++++++++++++++++++++++++++++++++++++
 1 files changed, 39 insertions(+), 0 deletions(-)

Patch

diff --git a/src/common/wpa_ctrl.c b/src/common/wpa_ctrl.c
index b2b0683..fe07c02 100644
--- a/src/common/wpa_ctrl.c
+++ b/src/common/wpa_ctrl.c
@@ -26,6 +26,9 @@ 
 
 #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
 #define CTRL_IFACE_SOCKET
+#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
+#include <netdb.h>
+#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 #endif /* CONFIG_CTRL_IFACE_UNIX || CONFIG_CTRL_IFACE_UDP */
 
 
@@ -230,6 +233,9 @@  struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
 	struct wpa_ctrl *ctrl;
 	char buf[128];
 	size_t len;
+#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
+	struct hostent *h;
+#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 
 	ctrl = os_malloc(sizeof(*ctrl));
 	if (ctrl == NULL)
@@ -244,7 +250,11 @@  struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
 	}
 
 	ctrl->local.sin_family = AF_INET;
+#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
 	ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
+#else
+	ctrl->local.sin_addr.s_addr = INADDR_ANY;
+#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 	if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
 		 sizeof(ctrl->local)) < 0) {
 		close(ctrl->s);
@@ -253,8 +263,37 @@  struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path)
 	}
 
 	ctrl->dest.sin_family = AF_INET;
+#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
 	ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
 	ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
+#else
+	if (ctrl_path) {
+		char *port, *name;
+		int port_id;
+
+		name = strdup(ctrl_path);
+		port = strchr(name, ':');
+
+		if (port) {
+			port_id = atoi(&port[1]);
+			port[0] = '\0';
+		} else
+			port_id = WPA_CTRL_IFACE_PORT;
+
+		printf("UDP remote host: %s, port: %d\n", name, port_id);
+		h = gethostbyname(name);
+		free(name);
+		if (h == NULL) {
+			perror("gethostbyname");
+			close(ctrl->s);
+			os_free(ctrl);
+			return NULL;
+		}
+		ctrl->dest.sin_port = htons(port_id);
+		bcopy(h->h_addr, (char *) &ctrl->dest.sin_addr.s_addr,
+		      h->h_length);
+	}
+#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 	if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
 		    sizeof(ctrl->dest)) < 0) {
 		perror("connect");