Skip to content

Commit

Permalink
Add: Add new functions related to reverse lookup (#797)
Browse files Browse the repository at this point in the history
Add gvm_reverse_lookup_only_excluded() and gvm_reverse_lookup_unify_excluded()
This functions return a hosts structure containing the excluded hosts which did not satisfy the condition
  • Loading branch information
jjnicola committed Oct 11, 2023
1 parent 9b16eef commit c8c6d83
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 9 deletions.
71 changes: 62 additions & 9 deletions base/hosts.c
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
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

0 comments on commit c8c6d83

Please sign in to comment.