[PATCH 3/6] wpa_supplicant: use dl_list for UDP ctrl iface

Janusz Dziedzic janusz.dziedzic
Fri Sep 25 04:31:21 PDT 2015


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

diff --git a/wpa_supplicant/ctrl_iface_udp.c b/wpa_supplicant/ctrl_iface_udp.c
index 2929976..ce6874c 100644
--- a/wpa_supplicant/ctrl_iface_udp.c
+++ b/wpa_supplicant/ctrl_iface_udp.c
@@ -30,7 +30,7 @@
  * ctrl_iface_udp.c and should not be touched directly from other files.
  */
 struct wpa_ctrl_dst {
-	struct wpa_ctrl_dst *next;
+	struct dl_list list;
 	struct sockaddr_storage addr;
 	socklen_t addrlen;
 	int debug_level;
@@ -41,21 +41,21 @@ struct wpa_ctrl_dst {
 struct ctrl_iface_priv {
 	struct wpa_supplicant *wpa_s;
 	int sock;
-	struct wpa_ctrl_dst *ctrl_dst;
+	struct dl_list ctrl_dst;
 	u8 cookie[COOKIE_LEN];
 };
 
 
 struct ctrl_iface_global_priv {
 	int sock;
-	struct wpa_ctrl_dst *ctrl_dst;
+	struct dl_list ctrl_dst;
 	u8 cookie[COOKIE_LEN];
 };
 
 
 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 					   const char *ifname, int sock,
-					   struct wpa_ctrl_dst **head,
+					   struct dl_list *ctrl_dst,
 					   int level, const char *buf,
 					   size_t len);
 
@@ -129,7 +129,7 @@ static void sockaddr_print(int level, char *msg, struct sockaddr_storage *sock,
 	}
 }
 
-static int wpa_supplicant_ctrl_iface_attach(struct wpa_ctrl_dst **head,
+static int wpa_supplicant_ctrl_iface_attach(struct dl_list *ctrl_dst,
 					    struct sockaddr_storage *from,
 					    socklen_t fromlen)
 {
@@ -141,40 +141,33 @@ static int wpa_supplicant_ctrl_iface_attach(struct wpa_ctrl_dst **head,
 	os_memcpy(&dst->addr, from, fromlen);
 	dst->addrlen = fromlen;
 	dst->debug_level = MSG_INFO;
-	dst->next = *head;
-	*head = dst;
+	dl_list_add(ctrl_dst, &dst->list);
 
 	sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor attached", from, fromlen);
 	return 0;
 }
 
 
-static int wpa_supplicant_ctrl_iface_detach(struct wpa_ctrl_dst **head,
+static int wpa_supplicant_ctrl_iface_detach(struct dl_list *ctrl_dst,
 					    struct sockaddr_storage *from,
 					    socklen_t fromlen)
 {
-	struct wpa_ctrl_dst *dst, *prev = NULL;
+	struct wpa_ctrl_dst *dst;
 
-	dst = *head;
-	while (dst) {
+	dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
 		if (!sockaddr_compare(from, &dst->addr)) {
 			sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor detached", from, fromlen);
-
-			if (prev == NULL)
-				*head = dst->next;
-			else
-				prev->next = dst->next;
+			dl_list_del(&dst->list);
 			os_free(dst);
 			return 0;
 		}
-		prev = dst;
-		dst = dst->next;
 	}
+
 	return -1;
 }
 
 
-static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
+static int wpa_supplicant_ctrl_iface_level(struct dl_list *ctrl_dst,
 					   struct sockaddr_storage *from,
 					   socklen_t fromlen,
 					   char *level)
@@ -183,14 +176,12 @@ static int wpa_supplicant_ctrl_iface_level(struct ctrl_iface_priv *priv,
 
 	wpa_printf(MSG_DEBUG, "CTRL_IFACE LEVEL %s", level);
 
-	dst = priv->ctrl_dst;
-	while (dst) {
+	dl_list_for_each(dst, ctrl_dst, struct wpa_ctrl_dst, list) {
 		if (!sockaddr_compare(from, &dst->addr)) {
 			sockaddr_print(MSG_DEBUG, "CTRL_IFACE changed monitor level", from, fromlen);
 			dst->debug_level = atoi(level);
 			return 0;
 		}
-		dst = dst->next;
 	}
 
 	return -1;
@@ -288,7 +279,7 @@ static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,
 		else
 			reply_len = 2;
 	} else if (os_strncmp(pos, "LEVEL ", 6) == 0) {
-		if (wpa_supplicant_ctrl_iface_level(priv, (struct sockaddr_storage *)&from, fromlen,
+		if (wpa_supplicant_ctrl_iface_level(&priv->ctrl_dst, (struct sockaddr_storage *)&from, fromlen,
 						    pos + 6))
 			reply_len = 1;
 		else
@@ -327,7 +318,7 @@ static void wpa_supplicant_ctrl_iface_msg_cb(void *ctx, int level,
 
 	if (type != WPA_MSG_NO_GLOBAL && wpa_s->global->ctrl_iface) {
 		struct ctrl_iface_global_priv *priv = wpa_s->global->ctrl_iface;
-		if (priv->ctrl_dst) {
+		if (!dl_list_empty(&priv->ctrl_dst)) {
 			wpa_supplicant_ctrl_iface_send(
 					wpa_s,
 					type != WPA_MSG_PER_INTERFACE ?
@@ -357,6 +348,7 @@ wpa_supplicant_ctrl_iface_init(struct wpa_supplicant *wpa_s)
 	priv = os_zalloc(sizeof(*priv));
 	if (priv == NULL)
 		return NULL;
+	dl_list_init(&priv->ctrl_dst);
 	priv->wpa_s = wpa_s;
 	priv->sock = -1;
 	os_get_random(priv->cookie, COOKIE_LEN);
@@ -424,7 +416,7 @@ void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
 
 	if (priv->sock > -1) {
 		eloop_unregister_read_sock(priv->sock);
-		if (priv->ctrl_dst) {
+		if (!dl_list_empty(&priv->ctrl_dst)) {
 			/*
 			 * Wait before closing the control socket if
 			 * there are any attached monitors in order to allow
@@ -438,19 +430,15 @@ void wpa_supplicant_ctrl_iface_deinit(struct ctrl_iface_priv *priv)
 		priv->sock = -1;
 	}
 
-	dst = priv->ctrl_dst;
-	while (dst) {
-		prev = dst;
-		dst = dst->next;
-		os_free(prev);
-	}
+	dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, list)
+		os_free(dst);
 	os_free(priv);
 }
 
 
 static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 					   const char *ifname, int sock,
-					   struct wpa_ctrl_dst **head,
+					   struct dl_list *ctrl_dst,
 					   int level, const char *buf,
 					   size_t len)
 {
@@ -460,8 +448,7 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 	char *sbuf;
 	int llen;
 
-	dst = *head;
-	if (sock < 0 || dst == NULL)
+	if (sock < 0 || dl_list_empty(ctrl_dst))
 		return;
 
 	if (ifname)
@@ -479,8 +466,7 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 	os_memcpy(sbuf + llen, buf, len);
 
 	idx = 0;
-	while (dst) {
-		next = dst->next;
+	dl_list_for_each_safe(dst, next, ctrl_dst, struct wpa_ctrl_dst, list) {
 		if (level >= dst->debug_level) {
 			sockaddr_print(MSG_DEBUG, "CTRL_IFACE monitor send",
 				       &dst->addr, dst->addrlen);
@@ -494,14 +480,13 @@ static void wpa_supplicant_ctrl_iface_send(struct wpa_supplicant *wpa_s,
 				dst->errors++;
 				if (dst->errors > 10) {
 					wpa_supplicant_ctrl_iface_detach(
-						head, &dst->addr,
+						ctrl_dst, &dst->addr,
 						dst->addrlen);
 				}
 			} else
 				dst->errors = 0;
 		}
 		idx++;
-		dst = next;
 	}
 	os_free(sbuf);
 }
@@ -629,6 +614,7 @@ wpa_supplicant_global_ctrl_iface_init(struct wpa_global *global)
 	priv = os_zalloc(sizeof(*priv));
 	if (priv == NULL)
 		return NULL;
+	dl_list_init(&priv->ctrl_dst);
 	priv->sock = -1;
 	os_get_random(priv->cookie, COOKIE_LEN);
 
@@ -705,12 +691,7 @@ wpa_supplicant_global_ctrl_iface_deinit(struct ctrl_iface_global_priv *priv)
 		close(priv->sock);
 	}
 
-	dst = priv->ctrl_dst;
-	while (dst) {
-		prev = dst;
-		dst = dst->next;
-		os_free(prev);
-	}
-
+	dl_list_for_each_safe(dst, prev, &priv->ctrl_dst, struct wpa_ctrl_dst, list)
+		os_free(dst);
 	os_free(priv);
 }
-- 
1.9.1




More information about the Hostap mailing list