From e863f7d31891bf593f7ab06fba3ed8b937bca486 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Mon, 3 May 2021 08:02:27 -0500 Subject: [PATCH] Add function to find and return a host from a host list. --- CHANGELOG.md | 1 + base/hosts.c | 40 ++++++++++++++++++++++++++++++---------- base/hosts.h | 4 ++++ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d85b6a8e..360cfe66a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - 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. [490](https://github.com/greenbone/gvm-libs/pull/490) ### Changed ### Fixed diff --git a/base/hosts.c b/base/hosts.c index c85f1bc98..8b9b39ec4 100644 --- a/base/hosts.c +++ b/base/hosts.c @@ -1932,26 +1932,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); @@ -1964,7 +1962,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); @@ -1977,12 +1975,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 542d5d4aa..d8268b424 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -165,6 +165,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 *);