[PATCH 3/9] wpa_supplicant: add helper function sockaddr_compare

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


Kill not needed #ifdefs and use helper sockaddr_compare
function.

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

diff --git a/wpa_supplicant/ctrl_iface_udp.c b/wpa_supplicant/ctrl_iface_udp.c
index 26fb3aa..e219ff4 100644
--- a/wpa_supplicant/ctrl_iface_udp.c
+++ b/wpa_supplicant/ctrl_iface_udp.c
@@ -53,6 +53,39 @@ static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,
 					   int level, const char *buf,
 					   size_t len);
 
+static int sockaddr_compare(struct sockaddr *a, struct sockaddr *b)
+{
+	struct sockaddr_in *in_a, *in_b;
+	struct sockaddr_in6 *in6_a, *in6_b;
+
+	if (a->sa_family != b->sa_family)
+		return 1;
+
+	switch (a->sa_family) {
+	case AF_INET:
+		in_a = (struct sockaddr_in *) a;
+		in_b = (struct sockaddr_in *) b;
+
+		if (in_a->sin_port != in_b->sin_port)
+			return 1;
+		if (in_a->sin_addr.s_addr != in_b->sin_addr.s_addr)
+			return 1;
+		break;
+	case AF_INET6:
+		in6_a = (struct sockaddr_in6 *) a;
+		in6_b = (struct sockaddr_in6 *) b;
+
+		if (in6_a->sin6_port != in6_b->sin6_port)
+			return 1;
+		if (os_memcmp(&in6_a->sin6_addr, &in6_b->sin6_addr, sizeof(in6_a->sin6_addr)))
+			return 1;
+		break;
+	default:
+		return 1;
+	}
+
+	return 0;
+}
 
 static int wpa_supplicant_ctrl_iface_attach(struct ctrl_iface_priv *priv,
 #ifdef CONFIG_CTRL_IFACE_UDP_IPV6
@@ -100,15 +133,7 @@ static int wpa_supplicant_ctrl_iface_detach(struct ctrl_iface_priv *priv,
 
 	dst = priv->ctrl_dst;
 	while (dst) {
-#ifdef CONFIG_CTRL_IFACE_UDP_IPV6
-		if (from->sin6_port == dst->addr.sin6_port &&
-		    !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
-			       sizeof(from->sin6_addr))) {
-#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) {
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
-
+		if (!sockaddr_compare((struct sockaddr *) from, (struct sockaddr *) &dst->addr)) {
 			getnameinfo((struct sockaddr *) from, fromlen,
 				    host, sizeof(host),
 				    service, sizeof(service),
@@ -148,14 +173,7 @@ static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
 
 	dst = priv->ctrl_dst;
 	while (dst) {
-#if CONFIG_CTRL_IFACE_UDP_IPV6
-		if (from->sin6_port == dst->addr.sin6_port &&
-		    !os_memcmp(&from->sin6_addr, &dst->addr.sin6_addr,
-			       sizeof(from->sin6_addr))) {
-#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) {
-#endif /* CONFIG_CTRL_IFACE_UDP_IPV6 */
+		if (!sockaddr_compare((struct sockaddr *) from, (struct sockaddr *) &dst->addr)) {
 			getnameinfo((struct sockaddr *) from, fromlen,
 				    host, sizeof(host),
 				    service, sizeof(service),
-- 
1.9.1




More information about the Hostap mailing list