[PATCH 2/9] wpa_supplicant: use getnameinfo() for UDP ctrl iface

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


Kill not needed #ifdefs and use getnameinfo() that
will work for both IPv4 and IPv6.

Signed-off-by: Janusz Dziedzic <janusz.dziedzic at tieto.com>
---
 wpa_supplicant/ctrl_iface_udp.c | 111 +++++++++++++++-------------------------
 1 file changed, 41 insertions(+), 70 deletions(-)

diff --git a/wpa_supplicant/ctrl_iface_udp.c b/wpa_supplicant/ctrl_iface_udp.c
index 12b4d45..26fb3aa 100644
--- a/wpa_supplicant/ctrl_iface_udp.c
+++ b/wpa_supplicant/ctrl_iface_udp.c
@@ -63,9 +63,8 @@ static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
 					    socklen_t fromlen)
 {
 	struct wpa_ctrl_dst *dst;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	char addr[INET6_ADDRSTRLEN];
-#endif /* CONFIG_UDP_IPV6 */
+	char host[NI_MAXHOST] = {0};
+	char service[NI_MAXSERV] = {0};
 
 	dst = os_zalloc(sizeof(*dst));
 	if (dst == NULL)
@@ -75,14 +74,14 @@ static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
 	dst->debug_level = MSG_INFO;
 	dst->next = priv->ctrl_dst;
 	priv->ctrl_dst = dst;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
-		   inet_ntop(AF_INET6, &from->sin6_addr, addr, sizeof(*from)),
-		   ntohs(from->sin6_port));
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%d",
-		   inet_ntoa(from->sin_addr), ntohs(from->sin_port));
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+
+	getnameinfo((struct sockaddr *) from, fromlen,
+		    host, sizeof(host),
+		    service, sizeof(service),
+		    NI_NUMERICHOST);
+
+	wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor attached %s:%s",
+		   host, service);
 	return 0;
 }
 
@@ -96,9 +95,8 @@ static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
 					    socklen_t fromlen)
 {
 	struct wpa_ctrl_dst *dst, *prev = NULL;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	char addr[INET6_ADDRSTRLEN];
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+	char host[NI_MAXHOST] = {0};
+	char service[NI_MAXSERV] = {0};
 
 	dst = priv->ctrl_dst;
 	while (dst) {
@@ -106,17 +104,19 @@ static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
 		if (from->sin6_port == dst->addr.sin6_port &&
 		    !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
 			       sizeof(from->sin6_addr))) {
-			wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s:%d",
-				   inet_ntop(AF_INET6, &from->sin6_addr, addr,
-					     sizeof(*from)),
-				   ntohs(from->sin6_port));
 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
 		if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
 		    from->sin_port == dst->addr.sin_port) {
-			wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached "
-				   "%s:%d", inet_ntoa(from->sin_addr),
-				   ntohs(from->sin_port));
 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+
+			getnameinfo((struct sockaddr *) from, fromlen,
+				    host, sizeof(host),
+				    service, sizeof(service),
+				    NI_NUMERICHOST);
+
+			wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor detached %s:%s",
+				   host, service);
+
 			if (prev == NULL)
 				priv->ctrl_dst = dst->next;
 			else
@@ -141,9 +141,8 @@ static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
 					   char *level)
 {
 	struct wpa_ctrl_dst *dst;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	char addr[INET6_ADDRSTRLEN];
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+	char host[NI_MAXHOST] = {0};
+	char service[NI_MAXSERV] = {0};
 
 	wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
 
@@ -153,17 +152,17 @@ static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
 		if (from->sin6_port == dst->addr.sin6_port &&
 		    !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
 			       sizeof(from->sin6_addr))) {
-			wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level %s:%d",
-				   inet_ntop(AF_INET6, &from->sin6_addr, addr,
-					     sizeof(*from)),
-				   ntohs(from->sin6_port));
 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
 		if (from->sin_addr.s_addr == dst->addr.sin_addr.s_addr &&
 		    from->sin_port == dst->addr.sin_port) {
-			wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor "
-				   "level %s:%d", inet_ntoa(from->sin_addr),
-				   ntohs(from->sin_port));
 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+			getnameinfo((struct sockaddr *) from, fromlen,
+				    host, sizeof(host),
+				    service, sizeof(service),
+				    NI_NUMERICHOST);
+
+			wpa_printf(MSG_DEBUG, "CTRL_IFACE changed monitor level %s:%s",
+				   host, service);
 			dst->debug_level = atoi(level);
 			return 0;
 		}
@@ -203,9 +202,6 @@ static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
 	int res;
 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
 	struct sockaddr_in6 from;
-#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
-	char addr[INET6_ADDRSTRLEN];
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
 #else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
 	struct sockaddr_in from;
 #endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
@@ -223,28 +219,6 @@ static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
 		return;
 	}
 
-#ifndef CONFIG_CTRL_IFACE_UDP_REMOTE
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	inet_ntop(AF_INET6, &from.sin6_addr, addr, sizeof(from));
-	if (os_strcmp(addr, "::1")) {
-		wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected source %s",
-			   addr);
-	}
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-	if (from.sin_addr.s_addr != htonl((127 << 24) | 1)) {
-		/*
-		 * The OS networking stack is expected to drop this kind of
-		 * frames since the socket is bound to only localhost address.
-		 * Just in case, drop the frame if it is coming from any other
-		 * address.
-		 */
-		wpa_printf(MSG_DEBUG, "CTRL: Drop packet from unexpected "
-			   "source %s", inet_ntoa(from.sin_addr));
-		return;
-	}
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
-
 	buf[res] = '\0';
 
 	if (os_strcmp(buf, "GET_COOKIE") == 0) {
@@ -441,13 +415,12 @@ static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 					   size_t len)
 {
 	struct wpa_ctrl_dst *dst, *next;
+	char host[NI_MAXHOST] = {0};
+	char service[NI_MAXSERV] = {0};
 	char levelstr[10];
 	int idx;
 	char *sbuf;
 	int llen;
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-	char addr[INET6_ADDRSTRLEN];
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
 
 	dst = priv->ctrl_dst;
 	if (priv->sock < 0 || dst == NULL)
@@ -467,19 +440,17 @@ static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 	while (dst) {
 		next = dst->next;
 		if (level >= dst->debug_level) {
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-			wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
-				   inet_ntop(AF_INET6, &dst->addr.sin6_addr,
-					     addr, sizeof(dst->addr)),
-				   ntohs(dst->addr.sin6_port));
-#else /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-			wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%d",
-				   inet_ntoa(dst->addr.sin_addr),
-				   ntohs(dst->addr.sin_port));
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+			getnameinfo((struct sockaddr *) &dst->addr, dst->addrlen,
+				    host, sizeof(host),
+				    service, sizeof(service),
+				    NI_NUMERICHOST);
+
+			wpa_printf(MSG_DEBUG, "CTRL_IFACE monitor send %s:%s",
+				   host, service);
+
 			if (sendto(priv->sock, sbuf, llen + len, 0,
 				   (struct sockaddr *) &dst->addr,
-				   sizeof(dst->addr)) < 0) {
+				   dst->addrlen) < 0) {
 				wpa_printf(MSG_ERROR,
 					   "sendto(CTRL_IFACE monitor): %s",
 					   strerror(errno));
-- 
1.9.1




More information about the Hostap mailing list