From 0aff1bddf19e679549910a3dc2687974321634b7 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 15 Oct 2020 02:55:18 -0500 Subject: [PATCH] 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..d5b7458f1 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)// hosts->current == hosts->count) + 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 *);