[PATCH 8/9] wpa_cli: simplify ctrl UDP path

Janusz Dziedzic janusz.dziedzic
Wed Sep 23 05:04:18 PDT 2015


Kill #ifdefs, use common code for IPv4/IPv6
in case of UDP control path.

Signed-off-by: Janusz Dziedzic <janusz.dziedzic at tieto.com>
---
 src/common/wpa_ctrl.c | 159 ++++++++++++++------------------------------------
 1 file changed, 44 insertions(+), 115 deletions(-)

diff --git a/src/common/wpa_ctrl.c b/src/common/wpa_ctrl.c
index c3ffa6f..ad0dcb8 100644
--- a/src/common/wpa_ctrl.c
+++ b/src/common/wpa_ctrl.c
@@ -15,9 +15,9 @@
 #include <unistd.h>
 #include <fcntl.h>
 #endif /* CONFIG_CTRL_IFACE_UNIX */
-#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
+#ifdef CONFIG_CTRL_IFACE_UDP
 #include <netdb.h>
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
+#endif /* CONFIG_CTRL_IFACE_UDP */
 
 #ifdef ANDROID
 #include <dirent.h>
@@ -26,10 +26,6 @@
 #include "private/android_filesystem_config.h"
 #endif /* ANDROID */
 
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-#include <net/if.h>
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-
 #include "wpa_ctrl.h"
 #include "common.h"
 
@@ -51,13 +47,6 @@
 struct wpa_ctrl {
 #ifdef CONFIG_CTRL_IFACE_UDP
 	int s;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	struct sockaddr_in6 local;
-	struct sockaddr_in6 dest;
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	struct sockaddr_in local;
-	struct sockaddr_in dest;
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
 	char *cookie;
 	char *remote_ifname;
 	char *remote_ip;
@@ -294,134 +283,74 @@ 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 */
+	struct addrinfo hints = {0}, *res, *ressave;
+	char *port, *name;
+	int port_id = WPA_CTRL_IFACE_PORT;
+	char p[32] = {0};
+	int n;
 
 	ctrl = os_zalloc(sizeof(*ctrl));
 	if (ctrl == NULL)
 		return NULL;
 
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	ctrl->s = socket(PF_INET6, SOCK_DGRAM, 0);
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	ctrl->s = socket(PF_INET, SOCK_DGRAM, 0);
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	if (ctrl->s < 0) {
-		perror("socket");
-		os_free(ctrl);
-		return NULL;
-	}
-
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	ctrl->local.sin6_family = AF_INET6;
-#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
-	ctrl->local.sin6_addr = in6addr_any;
-#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
-	inet_pton(AF_INET6, "::1", &ctrl->local.sin6_addr);
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	ctrl->local.sin_family = AF_INET;
-#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
-	ctrl->local.sin_addr.s_addr = INADDR_ANY;
-#else /* CONFIG_CTRL_IFACE_UDP_REMOTE */
-	ctrl->local.sin_addr.s_addr = htonl((127 << 24) | 1);
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-
-	if (bind(ctrl->s, (struct sockaddr *) &ctrl->local,
-		 sizeof(ctrl->local)) < 0) {
-		close(ctrl->s);
-		os_free(ctrl);
-		return NULL;
-	}
-
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	ctrl->dest.sin6_family = AF_INET6;
-	inet_pton(AF_INET6, "::1", &ctrl->dest.sin6_addr);
-	ctrl->dest.sin6_port = htons(WPA_CTRL_IFACE_PORT);
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	ctrl->dest.sin_family = AF_INET;
-	ctrl->dest.sin_addr.s_addr = htonl((127 << 24) | 1);
-	ctrl->dest.sin_port = htons(WPA_CTRL_IFACE_PORT);
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-
-#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
+	/* get host/port */
 	if (ctrl_path) {
-		char *port, *name;
-		int port_id;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-		char *scope;
-		int scope_id = 0;
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-
 		name = os_strdup(ctrl_path);
-		if (name == NULL) {
-			close(ctrl->s);
-			os_free(ctrl);
+		if (name == NULL)
 			return NULL;
-		}
 
 		port = os_strchr(name, ',');
 
 		if (port) {
 			port_id = atoi(&port[1]);
 			port[0] = '\0';
-		} else
-			port_id = WPA_CTRL_IFACE_PORT;
-
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-		scope = os_strchr(name, '%');
-		if (scope) {
-			scope_id = if_nametoindex(&scope[1]);
-			scope[0] = '\0';
 		}
-		h = gethostbyname2(name, AF_INET6);
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-		h = gethostbyname(name);
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+
 		ctrl->remote_ip = os_strdup(name);
 		os_free(name);
-		if (h == NULL) {
-			perror("gethostbyname");
-			close(ctrl->s);
-			os_free(ctrl->remote_ip);
-			os_free(ctrl);
-			return NULL;
-		}
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-		ctrl->dest.sin6_scope_id = scope_id;
-		ctrl->dest.sin6_port = htons(port_id);
-		os_memcpy(&ctrl->dest.sin6_addr, h->h_addr, h->h_length);
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-		ctrl->dest.sin_port = htons(port_id);
-		os_memcpy(&ctrl->dest.sin_addr.s_addr, h->h_addr, h->h_length);
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
 	} else
 		ctrl->remote_ip = os_strdup("localhost");
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 
-	if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,
-		    sizeof(ctrl->dest)) < 0) {
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-		char addr[INET6_ADDRSTRLEN];
-		wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s",
-			   inet_ntop(AF_INET6, &ctrl->dest.sin6_addr, addr,
-				     sizeof(ctrl->dest)),
-			   ntohs(ctrl->dest.sin6_port),
-			   strerror(errno));
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-		wpa_printf(MSG_ERROR, "connect(%s:%d) failed: %s",
-			   inet_ntoa(ctrl->dest.sin_addr),
-			   ntohs(ctrl->dest.sin_port),
-			   strerror(errno));
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+	os_snprintf(p, sizeof(p), "%d", port_id);
+
+	/* Get IPv4/IPv6 addrinfo */
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_DGRAM;
+
+	n = getaddrinfo(ctrl->remote_ip, p, &hints, &res);
+	if (n < 0) {
+		fprintf(stderr, "getaddrinfo error:: [%s]\n", gai_strerror(n));
+		os_free(ctrl->remote_ip);
+		os_free(ctrl);
+		return NULL;
+	}
+
+	ressave = res;
+
+	ctrl->s = -1;
+	for (res = ressave; res != NULL; res = res->ai_next) {
+		ctrl->s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+		if (ctrl->s == -1)
+			continue;
+
+		if (connect(ctrl->s, res->ai_addr, res->ai_addrlen) != -1)
+			/* Success */
+			break;
+
 		close(ctrl->s);
+		ctrl->s = -1;
+	}
+
+	if (ctrl->s < 0) {
+		perror("socket/connect");
+		freeaddrinfo(ressave);
 		os_free(ctrl->remote_ip);
 		os_free(ctrl);
 		return NULL;
 	}
 
+	freeaddrinfo(ressave);
+
 	len = sizeof(buf) - 1;
 	if (wpa_ctrl_request(ctrl, "GET_COOKIE", 10, buf, &len, NULL) == 0) {
 		buf[len] = '\0';
-- 
1.9.1




More information about the Hostap mailing list