service: filter out duplicate members
authorFelix Fietkau <nbd@nbd.name>
Fri, 17 Jun 2022 12:26:00 +0000 (14:26 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 17 Jun 2022 12:26:00 +0000 (14:26 +0200)
Signed-off-by: Felix Fietkau <nbd@nbd.name>
service.c

index 29b41a20a684c6e417761e5be7a1478b78a569e1..a9c45a3a6fe0c272f5464286b4be7d59ba280d36 100644 (file)
--- a/service.c
+++ b/service.c
@@ -23,6 +23,31 @@ void network_services_free(struct network *net)
        vlist_flush_all(&net->services);
 }
 
+static int
+__service_add_member(struct network_host **list, int *n, struct network_host *member)
+{
+       int i;
+
+       for (i = 0; i < *n; i++) {
+               if (list[i] == member)
+                       return 0;
+       }
+
+       list[(*n)++] = member;
+       return 1;
+}
+
+static int
+__service_add_group(struct network_host **list, int *n, struct network_group *group)
+{
+       int i, count = 0;
+
+       for (i = 0; i < group->n_members; i++)
+               count += __service_add_member(list, n, group->members[i]);
+
+       return count;
+}
+
 static int
 __service_parse_members(struct network *net, struct network_service *s,
                        const char *name)
@@ -37,7 +62,7 @@ __service_parse_members(struct network *net, struct network_service *s,
                        return 0;
 
                if (s)
-                       s->members[s->n_members++] = host;
+                       __service_add_member(s->members, &s->n_members, host);
 
                return 1;
        }
@@ -47,13 +72,10 @@ __service_parse_members(struct network *net, struct network_service *s,
        if (!group)
                return 0;
 
-       if (s) {
-               memcpy(&s->members[s->n_members], group->members,
-                      group->n_members * sizeof(group->members[0]));
-               s->n_members += group->n_members;
-       }
-
-       return group->n_members;
+       if (s)
+               return __service_add_group(s->members, &s->n_members, group);
+       else
+               return group->n_members;
 }
 
 static int