[PATCH 1/2] hostapd: make it possible to remove addresses from maclists

Emanuel Taube emanuel.taube
Tue Feb 18 02:36:34 PST 2014


It is already possible to add mac addresses at runtime.
This patch allows also to remove some of them by the prefix "-"
in the address file.

Signed-off-by: Emanuel Taube <emanuel.taube at gmail.com>
---
 hostapd/config_file.c |   23 +++++++++++++++++++++--
 src/utils/os.h        |   19 +++++++++++++++++++
 2 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/hostapd/config_file.c b/hostapd/config_file.c
index 19d6ad3..c45d30e 100644
--- a/hostapd/config_file.c
+++ b/hostapd/config_file.c
@@ -116,6 +116,8 @@ static int hostapd_config_read_maclist(const char *fname,
 	char buf[128], *pos;
 	int line = 0;
 	u8 addr[ETH_ALEN];
+	Boolean remove;
+	int i;
 	struct mac_acl_entry *newacl;
 	int vlan_id;
 
@@ -143,14 +145,31 @@ static int hostapd_config_read_maclist(const char *fname,
 		}
 		if (buf[0] == '\0')
 			continue;
+		pos = buf;
+		if (buf[0] == '-') {
+			remove = TRUE;
+			pos++;
+		} else
+			remove = FALSE;
 
-		if (hwaddr_aton(buf, addr)) {
+		if (hwaddr_aton(pos, addr)) {
 			wpa_printf(MSG_ERROR, "Invalid MAC address '%s' at "
-				   "line %d in '%s'", buf, line, fname);
+				   "line %d in '%s'", pos, line, fname);
 			fclose(f);
 			return -1;
 		}
 
+		if (remove) {
+			for (i = 0; i < *num; i++)
+				if (os_memcmp((*acl)[i].addr, addr, ETH_ALEN) ==
+				    0) {
+					*acl = os_remove_in_array(
+						*acl, *num,
+						sizeof(**acl), i);
+					(*num)--;
+				}
+			continue;
+		}
 		vlan_id = 0;
 		pos = buf;
 		while (*pos != '\0' && *pos != ' ' && *pos != '\t')
diff --git a/src/utils/os.h b/src/utils/os.h
index 2e2350a..961020b 100644
--- a/src/utils/os.h
+++ b/src/utils/os.h
@@ -549,6 +549,25 @@ static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
 	return os_realloc(ptr, nmemb * size);
 }
 
+/**
+ * os_remove_in_array - Remove a member from an array by index
+ * @ptr: pointer to the array
+ * @nmemb: current member count of the array
+ * @size: the size per member of the array
+ * @idx: index of the member which should be removed
+ * Returns: pointer to the new array.
+ */
+static inline void * os_remove_in_array(void *ptr, size_t nmemb, size_t size,
+				        unsigned int idx)
+{
+	if (idx >= nmemb)
+		return ptr;
+	if (idx < nmemb - 1)
+		os_memmove(ptr + idx * size, ptr + (idx + 1) * size,
+			   (nmemb - idx - 1) * size);
+
+	return os_realloc_array(ptr, nmemb-1, size);
+}
 
 /**
  * os_strlcpy - Copy a string with size bound and NUL-termination
-- 
1.7.10.4




More information about the Hostap mailing list