Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get new gvm_host_t by string #320

Merged
merged 3 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
ArnoStiefvater marked this conversation as resolved.
Show resolved Hide resolved

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