diff --git a/CHANGELOG.md b/CHANGELOG.md index 586c54719..30f6c8968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Extend osp with target's alive test option.[#312](https://github.com/greenbone/gvm-libs/pull/312) - Extend osp with target's reverse_lookup_* options.[#314](https://github.com/greenbone/gvm-libs/pull/314) - Add unit tests for osp. [#315](https://github.com/greenbone/gvm-libs/pull/315) +- Add support for test_alive_hosts_only feature of openvas. [#320](https://github.com/greenbone/gvm-libs/pull/320) [20.4]: https://github.com/greenbone/gvm-libs/compare/gvm-libs-11.0...master diff --git a/base/hosts.c b/base/hosts.c index a36472cf2..98ccbe845 100644 --- a/base/hosts.c +++ b/base/hosts.c @@ -894,7 +894,7 @@ gvm_host_free (gpointer host) * @param[in] hosts Hosts in which to insert the host. * @param[in] host Host to insert. */ -static void +void gvm_hosts_add (gvm_hosts_t *hosts, gvm_host_t *host) { if (hosts->count == hosts->max_size) @@ -1535,6 +1535,58 @@ gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str) return gvm_hosts_exclude_with_max (hosts, excluded_str, 0); } +/** + * @brief Creates a new gvm_host_t from a host string. + * + * @param[in] host_str The host string can consist of a hostname, IPv4 address + * or IPv6 address. + * + * @return NULL if error. Otherwise, a single host structure that should be put + * into a gvm_hosts_t structure for freeing with @ref gvm_hosts_free or + * freed directly via @ref gvm_host_free. + */ +gvm_host_t * +gvm_host_from_str (const gchar *host_str) +{ + int host_type; + + if (host_str == NULL) + return NULL; + + /* IPv4, hostname, IPv6 */ + /* -1 if error. */ + host_type = gvm_get_host_type (host_str); + + switch (host_type) + { + case HOST_TYPE_NAME: + case HOST_TYPE_IPV4: + case HOST_TYPE_IPV6: + { + /* New host. */ + gvm_host_t *host = gvm_host_new (); + host->type = host_type; + if (host_type == HOST_TYPE_NAME) + host->name = g_ascii_strdown (host_str, -1); + else if (host_type == HOST_TYPE_IPV4) + { + if (inet_pton (AF_INET, host_str, &host->addr) != 1) + break; + } + else if (host_type == HOST_TYPE_IPV6) + { + if (inet_pton (AF_INET6, host_str, &host->addr6) != 1) + break; + } + return host; + } + case -1: + default: + return NULL; + } + return NULL; +} + /** * @brief Checks for a host object reverse dns lookup existence. * diff --git a/base/hosts.h b/base/hosts.h index 98730f150..62453c002 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -100,6 +100,7 @@ struct gvm_hosts /* Function prototypes. */ /* gvm_hosts_t related */ + gvm_hosts_t * gvm_hosts_new (const gchar *); @@ -118,6 +119,9 @@ gvm_hosts_shuffle (gvm_hosts_t *); void gvm_hosts_reverse (gvm_hosts_t *); +void +gvm_hosts_add (gvm_hosts_t *, gvm_host_t *); + GSList * gvm_hosts_resolve (gvm_hosts_t *); @@ -147,6 +151,9 @@ gvm_hosts_removed (const gvm_hosts_t *); /* gvm_host_t related */ +gvm_host_t * +gvm_host_from_str (const gchar *hosts_str); + int gvm_host_in_hosts (const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *);