diff --git a/CHANGELOG.md b/CHANGELOG.md index 3826ce3cd..462c9edb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Ensure that new kb taken by the scanner are always clean. [#469](https://github.com/greenbone/gvm-libs/pull/469) - Validate for max_scan_hosts scanner preference. [#482](https://github.com/greenbone/gvm-libs/pull/482) - Possibility to use lcrypt with `$6$` (sha512) for authentication [484](https://github.com/greenbone/gvm-libs/pull/484) +- Add function to find and return a host from a host list. Backport of [PR 490](https://github.com/greenbone/gvm-libs/pull/490). [494](https://github.com/greenbone/gvm-libs/pull/494). ### Changed Use a char pointer instead of an zero-lenght array as kb_redis struct member. [443](https://github.com/greenbone/gvm-libs/pull/443) diff --git a/base/hosts.c b/base/hosts.c index 502e4a2d0..aae2e43fd 100644 --- a/base/hosts.c +++ b/base/hosts.c @@ -1899,26 +1899,24 @@ gvm_hosts_duplicated (const gvm_hosts_t *hosts) } /** - * @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 - * "192.168.10.1-5, 192.168.10.10-20" string while 192.168.10.7 doesn't. + * @brief Find the gvm_host_t from a gvm_hosts_t structure. * * @param[in] host The host object. * @param[in] addr Optional pointer to ip address. Could be used so that host * isn't resolved multiple times when type is HOST_TYPE_NAME. * @param[in] hosts Hosts collection. * - * @return 1 if host has equal in hosts, 0 otherwise. + * @return Pointer to host if found. NULL if error or host not found */ -int -gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, - const gvm_hosts_t *hosts) +gvm_host_t * +gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, + const gvm_hosts_t *hosts) { char *host_str; size_t i; if (host == NULL || hosts == NULL) - return 0; + return NULL; host_str = gvm_host_value_str (host); @@ -1931,7 +1929,7 @@ gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, { g_free (host_str); g_free (tmp); - return 1; + return current_host; } g_free (tmp); @@ -1944,12 +1942,34 @@ gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0) { g_free (host_str); - return 1; + return current_host; } } } g_free (host_str); + return NULL; +} + +/** + * @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 + * "192.168.10.1-5, 192.168.10.10-20" string while 192.168.10.7 doesn't. + * + * @param[in] host The host object. + * @param[in] addr Optional pointer to ip address. Could be used so that host + * isn't resolved multiple times when type is HOST_TYPE_NAME. + * @param[in] hosts Hosts collection. + * + * @return 1 if host has equal in hosts, 0 otherwise. + */ +int +gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, + const gvm_hosts_t *hosts) +{ + if (gvm_host_find_in_hosts (host, addr, hosts)) + return 1; + return 0; } diff --git a/base/hosts.h b/base/hosts.h index d6859f9af..91b19337f 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -162,6 +162,10 @@ int gvm_host_in_hosts (const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *); +gvm_host_t * +gvm_host_find_in_hosts (const gvm_host_t *, const struct in6_addr *, + const gvm_hosts_t *); + gchar * gvm_host_type_str (const gvm_host_t *);