Skip to content

Commit

Permalink
duplicate function for host and vhost
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraemii committed Sep 28, 2021
1 parent 05845e3 commit 94f756d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[#538](https://github.com/greenbone/gvm-libs/pull/538)
- Add function for mqtt init status [#567](https://github.com/greenbone/gvm-libs/pull/567)
- Add function to get the severity_vector, otherwise the cvss_base_vector. [#568](https://github.com/greenbone/gvm-libs/pull/568)
- Add function to duplicate host and vhost objects [#590](https://github.com/greenbone/gvm-libs/pull/590)

### Changed

Expand Down
59 changes: 59 additions & 0 deletions base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,28 @@ gvm_vhost_free (gpointer vhost)
g_free (vhost);
}

/**
* @brief Creates a deep copy of a gvm_vhost_t object.
*
* @param vhost source vhost
* @param data dummy for g_slist_copy_deep
* @return gpointer copy of vhost
*/
static gpointer
gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
{
(void) (data);
gvm_vhost_t *ret = NULL;

if (!vhost)
return NULL;

ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
g_strdup (((gvm_vhost_t *) vhost)->source));

return ret;
}

/**
* @brief Creates a new gvm_host_t object.
*
Expand Down Expand Up @@ -1986,6 +2008,43 @@ gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
return NULL;
}

/**
* @brief Creates a deep copy of a host. gvm_host_free has to be called on it.
*
* @param host source host
* @return gvm_host_t* copy of host
*/
gvm_host_t *
gvm_duplicate_host (gvm_host_t *host)
{
gvm_host_t *ret = NULL;

if (host == NULL)
return NULL;

ret = gvm_host_new ();

ret->type = host->type;
switch (host->type)
{
case HOST_TYPE_NAME:
ret->name = g_strdup (host->name);
break;
case HOST_TYPE_IPV4:
ret->addr.s_addr = host->addr.s_addr;
break;
case HOST_TYPE_IPV6:
ret->addr6.__in6_u = host->addr6.__in6_u;
break;
default:
g_free (ret);
return NULL;
}
ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);

return ret;
}

/**
* @brief Returns whether a host has an equal host in a hosts collection.
* eg. 192.168.10.1 has an equal in list created from
Expand Down
5 changes: 5 additions & 0 deletions base/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ gvm_host_add_reverse_lookup (gvm_host_t *);

void gvm_host_free (gpointer);

static gpointer gvm_duplicate_vhost (gconstpointer, gpointer);

gvm_host_t *
gvm_duplicate_host (gvm_host_t *);

/* Miscellaneous functions */

gvm_vhost_t *
Expand Down

0 comments on commit 94f756d

Please sign in to comment.