diff --git a/CHANGELOG.md b/CHANGELOG.md index dce544be7..00fa8b390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/base/hosts.c b/base/hosts.c index b252b6b93..fb8442284 100644 --- a/base/hosts.c +++ b/base/hosts.c @@ -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. * @@ -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 diff --git a/base/hosts.h b/base/hosts.h index d8268b424..cbeedc6a2 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -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 *