From 1be8151af11315b48e3c2ab85f12946cf56067c2 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 15 Oct 2020 02:55:18 -0500 Subject: [PATCH 1/2] Add function to move a host to the end of the list Allows to put a host in the hosts structure at the end of the host list without reset the iterator. --- CHANGELOG.md | 4 +++- base/hosts.c | 33 +++++++++++++++++++++++++++++++++ base/hosts.h | 3 +++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 349e3ea49..d5091873f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - Use dedicated port list for alive detection (Boreas only) if supplied via OSP. [#391](https://github.com/greenbone/gvm-libs/pull/391) -- Allow to re allocate the finish flag in the host queue for alive tests. [#407](https://github.com/greenbone/gvm-libs/pull/407) +- Allow to re allocate the finish flag in the host queue for alive tests. + [#407](https://github.com/greenbone/gvm-libs/pull/407) + [#410](https://github.com/greenbone/gvm-libs/pull/410) ### Added - Add multiple severities for nvti [#317](https://github.com/greenbone/gvm-libs/pull/317) diff --git a/base/hosts.c b/base/hosts.c index cca3e2b48..54cc7be17 100644 --- a/base/hosts.c +++ b/base/hosts.c @@ -1300,6 +1300,39 @@ gvm_hosts_next (gvm_hosts_t *hosts) return hosts->hosts[hosts->current++]; } +/** + * @brief Move the current gvm_host_t from a gvm_hosts_t structure to + * the end of the hosts list. + * + * @param[in/out] hosts gvm_hosts_t structure which hosts must be + * rearange. The hosts->current index points to the last used hosts and + * gvm_hosts_next() must be called to get the next host in the list. + * + */ +void +gvm_hosts_move_current_host_to_end (gvm_hosts_t *hosts) +{ + void *host_tmp; + size_t i; + + if (!hosts) + return; + + if (hosts->current == hosts->count) + { + hosts->current -= 1; + return; + } + + hosts->current -= 1; + host_tmp = hosts->hosts[hosts->current]; + + for (i = hosts->current; i < hosts->count; i++) + hosts->hosts[i - 1] = hosts->hosts[i]; + + hosts->hosts[hosts->count - 1] = host_tmp; +} + /** * @brief Frees memory occupied by an gvm_hosts_t structure. * diff --git a/base/hosts.h b/base/hosts.h index dd6654282..1b2137d56 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -111,6 +111,9 @@ gvm_hosts_new_with_max (const gchar *, unsigned int); gvm_host_t * gvm_hosts_next (gvm_hosts_t *); +void +gvm_hosts_move_current_host_to_end (gvm_hosts_t *); + void gvm_hosts_free (gvm_hosts_t *); From db470cc71c3b910d72ac7163d8bba0b7cbeee29b Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 15 Oct 2020 07:34:10 -0500 Subject: [PATCH 2/2] Add tests --- base/hosts_tests.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/base/hosts_tests.c b/base/hosts_tests.c index a9bf98eef..2e7450ca1 100644 --- a/base/hosts_tests.c +++ b/base/hosts_tests.c @@ -212,6 +212,42 @@ Ensure (hosts, gvm_hosts_new_with_max_returns_error) assert_that (gvm_hosts_new_with_max ("127.0.0.1|127.0.0.2", 2), is_null); } +Ensure (hosts, gvm_hosts_move_host_to_end) +{ + gvm_hosts_t *hosts = NULL; + gvm_host_t *host = NULL; + int totalhosts; + size_t current; + + hosts = gvm_hosts_new ("192.168.0.0/28"); + + // Get first host + host = gvm_hosts_next (hosts); + + totalhosts = gvm_hosts_count (hosts); + assert_that (totalhosts, is_equal_to (14)); + + while (g_strcmp0 (gvm_host_value_str (host), "192.168.0.9")) + { + host = gvm_hosts_next (hosts); + } + assert_that (g_strcmp0 (gvm_host_value_str (host), "192.168.0.9"), + is_equal_to (0)); + + current = hosts->current; + gvm_hosts_move_current_host_to_end (hosts); + assert_that (hosts->current, is_equal_to (current - 1)); + + host = gvm_hosts_next (hosts); + assert_that (g_strcmp0 (gvm_host_value_str (host), "192.168.0.10"), + is_equal_to (0)); + assert_that (g_strcmp0 (gvm_host_value_str (hosts->hosts[totalhosts - 1]), + "192.168.0.9"), + is_equal_to (0)); + + gvm_hosts_free (hosts); +} + /* Test suite. */ int @@ -245,6 +281,8 @@ main (int argc, char **argv) add_test_with_context (suite, hosts, gvm_hosts_new_with_max_returns_error); add_test_with_context (suite, hosts, gvm_hosts_new_with_max_returns_success); + add_test_with_context (suite, hosts, gvm_hosts_move_host_to_end); + if (argc > 1) return run_single_test (suite, argv[1], create_text_reporter ());