[PATCH 2/6] wpa_supplicant: add sockaddr_print function

Janusz Dziedzic janusz.dziedzic at tieto.com
Fri Sep 25 07:31:20 EDT 2015


Add sockaddr_print function and change prototype
of sockaddr_compare.

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

diff --git a/wpa_supplicant/ctrl_iface_udp.c b/wpa_supplicant/ctrl_iface_udp.c
index f8826de..2929976 100644
--- a/wpa_supplicant/ctrl_iface_udp.c
+++ b/wpa_supplicant/ctrl_iface_udp.c
@@ -59,16 +59,16 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 					   int level, const char *buf,
 					   size_t len);
 
-static int sockaddr_compare(struct sockaddr *a, struct sockaddr *b)
+static int sockaddr_compare(struct sockaddr_storage *a, struct sockaddr_storage *b)
 {
 	struct sockaddr_in *in_a, *in_b;
 	struct sockaddr_in6 *in6_a, *in6_b;
 	struct sockaddr_un *u_a, *u_b;
 
-	if (a->sa_family != b->sa_family)
+	if (a->ss_family != b->ss_family)
 		return 1;
 
-	switch (a->sa_family) {
+	switch (a->ss_family) {
 	case AF_INET:
 		in_a = (struct sockaddr_in *) a;
 		in_b = (struct sockaddr_in *) b;
@@ -101,13 +101,39 @@ static int sockaddr_compare(struct sockaddr *a, struct sockaddr *b)
 	return 0;
 }
 
+static void sockaddr_print(int level, char *msg, struct sockaddr_storage *sock, socklen_t socklen)
+{
+	char host[NI_MAXHOST] = {0};
+	char service[NI_MAXSERV] = {0};
+	char addr_txt[200];
+
+	switch (sock->ss_family) {
+	case AF_INET:
+	case AF_INET6:
+		getnameinfo((struct sockaddr *) sock, socklen,
+			    host, sizeof(host),
+			    service, sizeof(service),
+			    NI_NUMERICHOST);
+
+		wpa_printf(level, "%s %s:%s", msg, host, service);
+		break;
+	case AF_UNIX:
+		printf_encode(addr_txt, sizeof(addr_txt),
+			      (u8 *) ((struct sockaddr_un *) sock)->sun_path,
+			      socklen - offsetof(struct sockaddr_un, sun_path));
+		wpa_printf(level, "%s %s", msg, addr_txt);
+		break;
+	default:
+		wpa_printf(level, "%s", msg);
+		break;
+	}
+}
+
 static int wpa_supplicant_ctrl_iface_attach(struct wpa_ctrl_dst **head,
 					    struct sockaddr_storage *from,
 					    socklen_t fromlen)
 {
 	struct wpa_ctrl_dst *dst;
-	char host[NI_MAXHOST] = {0};
-	char service[NI_MAXSERV] = {0};
 
 	dst = os_zalloc(sizeof(*dst));
 	if (dst == NULL)
@@ -118,13 +144,7 @@ static int wpa_supplicant_ctrl_iface_attach(struct wpa_ctrl_dst **head,
 	dst->next = *head;
 	*head = dst;
 
-	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);
+	sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor attached", from, fromlen);
 	return 0;
 }
 
@@ -134,19 +154,11 @@ static int wpa_supplicant_ctrl_iface_detach(struct wpa_ctrl_dst **head,
 					    socklen_t fromlen)
 {
 	struct wpa_ctrl_dst *dst, *prev = NULL;
-	char host[NI_MAXHOST] = {0};
-	char service[NI_MAXSERV] = {0};
 
 	dst = *head;
 	while (dst) {
-		if (!sockaddr_compare((struct sockaddr *) from, (struct sockaddr *) &dst->addr)) {
-			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 (!sockaddr_compare(from, &dst->addr)) {
+			sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor detached", from, fromlen);
 
 			if (prev == NULL)
 				*head = dst->next;
@@ -168,21 +180,13 @@ static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
 					   char *level)
 {
 	struct wpa_ctrl_dst *dst;
-	char host[NI_MAXHOST] = {0};
-	char service[NI_MAXSERV] = {0};
 
 	wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
 
 	dst = priv->ctrl_dst;
 	while (dst) {
-		if (!sockaddr_compare((struct sockaddr *) from, (struct sockaddr *) &dst->addr)) {
-			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);
+		if (!sockaddr_compare(from, &dst->addr)) {
+			sockaddr_print(MSG_DEBUG, "CTRL_IFACE changed monitor level", from, fromlen);
 			dst->debug_level = atoi(level);
 			return 0;
 		}
@@ -451,8 +455,6 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 					   size_t len)
 {
 	struct wpa_ctrl_dst *dst, *next;
-	char host[NI_MAXHOST] = {0};
-	char service[NI_MAXSERV] = {0};
 	char levelstr[64];
 	int idx;
 	char *sbuf;
@@ -480,13 +482,8 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 	while (dst) {
 		next = dst->next;
 		if (level >= dst->debug_level) {
-			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);
+			sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
+				       &dst->addr, dst->addrlen);
 
 			if (sendto(sock, sbuf, llen + len, 0,
 				   (struct sockaddr *) &dst->addr,
-- 
1.9.1



More information about the HostAP mailing list