Skip to content

Commit

Permalink
Merge pull request #320 from ArnoStiefvater/test-alive-hosts-only
Browse files Browse the repository at this point in the history
get new gvm_host_t by string
  • Loading branch information
mattmundell authored Mar 3, 2020
2 parents 8441af3 + 43414d7 commit dbef141
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
54 changes: 53 additions & 1 deletion base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions base/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ struct gvm_hosts
/* Function prototypes. */

/* gvm_hosts_t related */

gvm_hosts_t *
gvm_hosts_new (const gchar *);

Expand All @@ -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 *);

Expand Down Expand Up @@ -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 *);
Expand Down

0 comments on commit dbef141

Please sign in to comment.