From 0a971d39b86cdaa3eedbcfabac5ff791c7232ae4 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 18 Jul 2024 16:55:17 +0200 Subject: [PATCH] Remove duplicates from client name suggestions Signed-off-by: DL6ER --- src/api/queries.c | 3 +++ src/webserver/http-common.c | 43 +++++++++++++++++++++++++++++++++++++ src/webserver/http-common.h | 1 + test/pdns/setup.sh | 6 ++++++ 4 files changed, 53 insertions(+) diff --git a/src/api/queries.c b/src/api/queries.c index 77409438..bc8dac1d 100644 --- a/src/api/queries.c +++ b/src/api/queries.c @@ -117,6 +117,9 @@ int api_queries_suggestions(struct ftl_conn *api) cJSON *client_ip = get_top_clients(api, count, false, true, false); cJSON *client_name = get_top_clients(api, count, false, true, true); + // Delete duplicate entries from client_name + cJSON_unique_array(client_name); + // Get upstreams cJSON *upstream = get_top_upstreams(api, true); // Get types diff --git a/src/webserver/http-common.c b/src/webserver/http-common.c index 2154f0f6..75e687ba 100644 --- a/src/webserver/http-common.c +++ b/src/webserver/http-common.c @@ -662,3 +662,46 @@ char *__attribute__((malloc)) escape_json(const char *string) // Return the JSON escaped string return namep; } + +// Remove duplicates from a cJSON array +// This function uses the less efficient cJSON_GetArraySize() function compared +// to cJSON_ArrayForEach() as we are going to modify the array in-place while +// iterating over it +void cJSON_unique_array(cJSON *array) +{ + // Check if the array is an array + if(!cJSON_IsArray(array)) + return; + + for(int oi = 0; oi < cJSON_GetArraySize(array); oi++) + { + // Get the outer item + cJSON *outer_item = cJSON_GetArrayItem(array, oi); + // Check if the item is a string + if (!cJSON_IsString(outer_item)) + continue; + + // Check for duplicates in the remainder of the array + for(int ii = oi + 1; ii < cJSON_GetArraySize(array); ii++) + { + // Get the inner item + cJSON *inner_item = cJSON_GetArrayItem(array, ii); + // Check if the inner item is a string + if (!cJSON_IsString(inner_item)) + continue; + + // Compare the two strings + if(strcmp(outer_item->valuestring, inner_item->valuestring) == 0) + { + // Remove the duplicate item, this is safe as we are + // at least one item ahead of the outer item + cJSON_DeleteItemFromArray(array, ii); + // Compensate for removed item (the for loop + // will increment ii for the next step, thus + // we need to decrement it here) + ii--; + continue; + } + } + } +} diff --git a/src/webserver/http-common.h b/src/webserver/http-common.h index d8bfe1af..bde500e5 100644 --- a/src/webserver/http-common.h +++ b/src/webserver/http-common.h @@ -103,5 +103,6 @@ char * __attribute__((malloc)) escape_html(const char *string); int check_json_payload(struct ftl_conn *api); int parse_groupIDs(struct ftl_conn *api, tablerow *table, cJSON *row); char * __attribute__((malloc)) escape_json(const char *string); +void cJSON_unique_array(cJSON *array); #endif // HTTP_H diff --git a/test/pdns/setup.sh b/test/pdns/setup.sh index 715d5571..7a860fef 100644 --- a/test/pdns/setup.sh +++ b/test/pdns/setup.sh @@ -124,6 +124,12 @@ pdnsutil add-record arpa. 2.1.168.192.in-addr PTR a.ftl. pdnsutil add-record arpa. 1.0.c.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6 PTR ftl. pdnsutil add-record arpa. 2.0.c.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.e.f.ip6 PTR aaaa.ftl. +# Add DNSSEC zone +pdnsutil create-zone dnssec ns1.ftl + +# Create trust anchor +pdnsutil add-zone-key dnssec KSK active + # Calculates the ‘ordername’ and ‘auth’ fields for all zones so they comply with # DNSSEC settings. Can be used to fix up migrated data. Can always safely be # run, it does no harm.