Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to move a host to the end of the list #410

Merged
merged 2 commits into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions base/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

Expand Down
38 changes: 38 additions & 0 deletions base/hosts_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor nitpick. There is a is_equal_to_string() function for easy use. But it does not really matter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

{
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
Expand Down Expand Up @@ -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 ());

Expand Down