From ad6701d493ddab43df3953719b01b5645cac206c Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakob.olsson@iopsys.eu>
Date: Mon, 14 Jun 2021 09:56:38 +0200
Subject: [PATCH] cntlr_map: don't publish multi cntlr ubus event unless
 supportedRole=cntlr

---
 src/core/cntlr_map.c | 59 +++++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 31 deletions(-)

diff --git a/src/core/cntlr_map.c b/src/core/cntlr_map.c
index 824d1260..6f78b7bf 100644
--- a/src/core/cntlr_map.c
+++ b/src/core/cntlr_map.c
@@ -194,7 +194,7 @@ int handle_ap_autoconfig_search(void *cntlr, struct cmdu_buff *rx_cmdu)
 	struct cmdu_buff *cmdu;
 	uint8_t aladdr_origin[6] = {0};
 	struct tlv *tv[6][16] = {0};
-	int ret = 0;
+	int ret = 0, i;
 
 	ret = cmdu_parse_tlvs(rx_cmdu, tv, a_policy, 6);
 
@@ -233,43 +233,40 @@ int handle_ap_autoconfig_search(void *cntlr, struct cmdu_buff *rx_cmdu)
 		return -1;
 
 	/* if supports agent, add to config */
-	if (tv[3][0]->data[0] > 0) {
-		int i;
+	for (i = 1; i <= tv[3][0]->data[0]; i++) {
 		char mac_str[18] = {0};
-		int len = 0;
 
 		if (!hwaddr_ntoa(aladdr_origin, mac_str))
 			return -1;
 
-		for (i = 1; i <= tv[3][0]->data[0]; i++) {
-			if (tv[3][0]->data[i] ==
-					SUPPORTED_SERVICE_MULTIAP_AGENT) {
-				ret = cntlr_config_add_agent(&c->cfg, mac_str);
-				if (!ret)
-					cntlr_config_reload(&c->cfg);
-			} else if (tv[3][0]->data[i] ==
-					SUPPORTED_SERVICE_MULTIAP_CONTROLLER) {
-				if (!memcmp(aladdr_origin, c->almac, 6)) {
-					trace("%s: Discard ap-autoconfig "\
-							"search from self\n",
-							__func__);
-					return -1;
-				}
+		if (tv[3][0]->data[i] ==
+				SUPPORTED_SERVICE_MULTIAP_AGENT) {
+			ret = cntlr_config_add_agent(&c->cfg, mac_str);
+			if (!ret)
+				cntlr_config_reload(&c->cfg);
+		} else if (tv[3][0]->data[i] ==
+				SUPPORTED_SERVICE_MULTIAP_CONTROLLER) {
+			if (!memcmp(aladdr_origin, c->almac, 6)) {
+				trace("%s: Discard ap-autoconfig search"\
+						" from self\n", __func__);
+				return -1;
 			}
-			break;
-		}
-	}
-
-	if (c->state == CNTLR_INIT) {
-		trace("Disable and exit\n");
-		set_value_by_string("mapcontroller", "controller", "enabled", "0",
-				UCI_TYPE_STRING);
-		exit(0);
-	} else {
-		char data[128] = {0};
 
-		snprintf(data, sizeof(data), "{\"type\":\"error\", \"reason\":\"multiple controllers\", \"almac\":\""MACFMT"\"}", MAC2STR(rx_cmdu->origin));
-		cntlr_notify_event(c, "map.controller", data);
+			if (c->state == CNTLR_INIT) {
+				trace("Disable and exit\n");
+				set_value_by_string("mapcontroller",
+						"controller",
+						"enabled", "0",
+						UCI_TYPE_STRING);
+				exit(0);
+			} else {
+				char data[128] = {0};
+
+				snprintf(data, sizeof(data), "{\"type\":\"error\", \"reason\":\"multiple controllers\", \"almac\":\""MACFMT"\"}", MAC2STR(rx_cmdu->origin));
+				cntlr_notify_event(c, "map.controller", data);
+			}
+		}
+		break;
 	}
 
 	/* TODO: don't answer if searched service does not include controller */
-- 
GitLab