Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnoStiefvater committed Feb 26, 2020
1 parent 8441af3 commit 27fdf1d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
56 changes: 55 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,60 @@ 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 the provided host_str. host_str can
* consist of hostnam, IPv4 address or IPv6 address.
*
* @param[in] host_str The host string. A copy will be created of this within
* the returned struct.
*
* @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)
{
if (host_str == NULL)
{
return NULL;
}
int host_type;

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

/* gvm_hosts_t related */
void
gvm_hosts_add (gvm_hosts_t *, gvm_host_t *);

gvm_hosts_t *
gvm_hosts_new (const gchar *);

Expand Down Expand Up @@ -147,6 +150,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 27fdf1d

Please sign in to comment.