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: Add new functions related to reverse lookup #797

Merged
merged 1 commit into from
Oct 11, 2023
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
71 changes: 62 additions & 9 deletions base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1935,22 +1935,24 @@ gvm_host_add_reverse_lookup (gvm_host_t *host)
*
* @param[in] hosts The hosts collection to filter.
*
* @return Number of hosts removed, -1 if error.
* @return list of hosts removed, Null on error.
*/
int
gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
gvm_hosts_t *
gvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *hosts)
{
size_t i, count = 0;
gvm_hosts_t *excluded = gvm_hosts_new ("");

if (hosts == NULL)
return -1;
return NULL;

for (i = 0; i < hosts->count; i++)
{
gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);

if (name == NULL)
{
gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
count++;
Expand All @@ -1964,11 +1966,11 @@ gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
hosts->count -= count;
hosts->removed += count;
hosts->current = 0;
return count;
return excluded;
}

/**
* @brief Removes hosts duplicates that reverse-lookup to the same value.
* @brief Removes hosts that don't reverse-lookup from the hosts collection.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
Expand All @@ -1977,17 +1979,44 @@ gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
* @return Number of hosts removed, -1 if error.
*/
int
gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
{
gvm_hosts_t *excluded;
int count = 0;

if (hosts == NULL)
return -1;

excluded = gvm_hosts_reverse_lookup_only_excluded (hosts);
count = excluded->count;
gvm_hosts_free (excluded);

return count;
}

/**
* @brief Removes hosts duplicates that reverse-lookup to the same value.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to filter.
*
* @return List of hosts removed, Null if error.
*/
gvm_hosts_t *
gvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *hosts)
{
/**
* Uses a hash table in order to unify the hosts list in O(N) time.
*/
size_t i, count = 0;
GHashTable *name_table;
gvm_hosts_t *excluded = NULL;

if (hosts == NULL)
return -1;
return NULL;

excluded = gvm_hosts_new ("");
name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
for (i = 0; i < hosts->count; i++)
{
Expand All @@ -1997,6 +2026,7 @@ gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
{
if (g_hash_table_lookup (name_table, name))
{
gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
gvm_host_free (hosts->hosts[i]);
hosts->hosts[i] = NULL;
count++;
Expand All @@ -2016,9 +2046,32 @@ gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
hosts->removed += count;
hosts->count -= count;
hosts->current = 0;
return count;
return excluded;
}

/**
* @brief Removes hosts duplicates that reverse-lookup to the same value.
* Not to be used while iterating over the single hosts as it resets the
* iterator.
*
* @param[in] hosts The hosts collection to filter.
*
* @return Number of hosts removed, -1 if error.
*/
int
gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
{
gvm_hosts_t *excluded = NULL;
int count = 0;
if (hosts == NULL)
return -1;

excluded = gvm_hosts_reverse_lookup_unify_excluded (hosts);
count = excluded->count;
gvm_hosts_free (excluded);

return count;
}
/**
* @brief Gets the count of single hosts objects in a hosts collection.
*
Expand Down
12 changes: 12 additions & 0 deletions base/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
*/
#define FEATURE_HOSTS_ALLOWED_ONLY 1

/** @brief Flag that indecates that this version includes
* the functions gvm_reverse_lookup_only_excluded()
* and gvm_reverse_lookup_unify_excluded()
*/
#define FEATURE_REVERSE_LOOKUP_EXCLUDED 1

#include <glib.h> /* for gchar, GList */
#include <netinet/in.h> /* for in6_addr, in_addr */

Expand Down Expand Up @@ -141,6 +147,12 @@ gvm_hosts_reverse_lookup_only (gvm_hosts_t *);
int
gvm_hosts_reverse_lookup_unify (gvm_hosts_t *);

gvm_hosts_t *
gvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *);

gvm_hosts_t *
gvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *);

unsigned int
gvm_hosts_count (const gvm_hosts_t *);

Expand Down