Greenbone Vulnerability Management Libraries  11.0.1
hosts.c File Reference

Implementation of an API to handle Hosts objects. More...

#include "hosts.h"
#include "networking.h"
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <malloc.h>
#include <netdb.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
Include dependency graph for hosts.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "base hosts"
 GLib log domain. More...
 

Functions

static int is_ipv4_address (const char *str)
 Checks if a buffer points to a valid IPv4 address. "192.168.11.1" is valid, "192.168.1.300" and "192.168.1.1e" are not. More...
 
static int is_ipv6_address (const char *str)
 Checks if a buffer points to a valid IPv6 address. "0:0:0:0:0:0:0:1", "::1" and "::FFFF:192.168.13.55" are valid "::1g" is not. More...
 
static int is_cidr_block (const char *str)
 Checks if a buffer points to an IPv4 CIDR-expressed block. "192.168.12.3/24" is valid, "192.168.1.3/31" is not. More...
 
static int cidr_get_block (const char *str, unsigned int *block)
 Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24. More...
 
static int cidr_get_ip (const char *str, struct in_addr *addr)
 Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10". More...
 
static int cidr_block_ips (const char *str, struct in_addr *first, struct in_addr *last)
 Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24" would give 192.168.1.1 as first and 192.168.1.254 as last. More...
 
static int is_long_range_network (const char *str)
 Checks if a buffer points to a valid long range-expressed network. "192.168.12.1-192.168.13.50" is valid. More...
 
static int long_range_network_ips (const char *str, struct in_addr *first, struct in_addr *last)
 Gets the first and last IPv4 addresses from a long range-expressed network. eg. "192.168.1.1-192.168.2.40" would give 192.168.1.1 as first and 192.168.2.40 as last. More...
 
static int is_short_range_network (const char *str)
 Checks if a buffer points to a valid short range-expressed network. "192.168.11.1-50" is valid, "192.168.1.1-50e" and "192.168.1.1-300" are not. More...
 
static int short_range_network_ips (const char *str, struct in_addr *first, struct in_addr *last)
 Gets the first and last IPv4 addresses from a short range-expressed network. "192.168.1.1-40" would give 192.168.1.1 as first and 192.168.1.40 as last. More...
 
static int is_hostname (const char *str)
 Checks if a buffer points to a valid hostname. Valid characters include: Alphanumerics, dot (.), dash (-) and underscore (_) up to 255 characters. More...
 
static int is_cidr6_block (const char *str)
 Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid, "2620:0:2d0:200::7/129" is not. More...
 
static int cidr6_get_block (const char *str, unsigned int *block)
 Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24. More...
 
static int cidr6_get_ip (const char *str, struct in6_addr *addr6)
 Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10". More...
 
static int cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
 Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24 would give 192.168.1.1 as first and 192.168.1.254 as last. Thus, it skips the network and broadcast addresses. More...
 
static int is_long_range6_network (const char *str)
 Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid. More...
 
static int long_range6_network_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
 Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:500" would give ::1:200:7 as first and ::1:205:500 as last. More...
 
static int is_short_range6_network (const char *str)
 Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid. More...
 
static int short_range6_network_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
 Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005" would give ::ffee:1:1001 as first and ::ffee:1:1005 as last. More...
 
int gvm_get_host_type (const gchar *str_stripped)
 Determines the host type in a buffer. More...
 
gvm_vhost_tgvm_vhost_new (char *value, char *source)
 Creates a new gvm_vhost_t object. More...
 
static void gvm_vhost_free (gpointer vhost)
 Frees the memory occupied by an gvm_vhost_t object. More...
 
static gvm_host_tgvm_host_new ()
 Creates a new gvm_host_t object. More...
 
static void gvm_host_free (gpointer host)
 Frees the memory occupied by an gvm_host_t object. More...
 
static void gvm_hosts_add (gvm_hosts_t *hosts, gvm_host_t *host)
 Inserts a host object at the end of a hosts collection. More...
 
static gvm_hosts_tgvm_hosts_init (const char *hosts_str)
 Creates a hosts collection from a hosts string. More...
 
static void gvm_hosts_fill_gaps (gvm_hosts_t *hosts)
 Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries. More...
 
static void gvm_hosts_deduplicate (gvm_hosts_t *hosts)
 Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current position. More...
 
gvm_hosts_tgvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str. More...
 
gvm_hosts_tgvm_hosts_new (const gchar *hosts_str)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str. More...
 
gvm_host_tgvm_hosts_next (gvm_hosts_t *hosts)
 Gets the next gvm_host_t from a gvm_hosts_t structure. The state of iteration is kept internally within the gvm_hosts structure. More...
 
void gvm_hosts_free (gvm_hosts_t *hosts)
 Frees memory occupied by an gvm_hosts_t structure. More...
 
void gvm_hosts_shuffle (gvm_hosts_t *hosts)
 Randomizes the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
void gvm_hosts_reverse (gvm_hosts_t *hosts)
 Reverses the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
GSList * gvm_hosts_resolve (gvm_hosts_t *hosts)
 Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
int gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
 Exclude a list of vhosts from a host's vhosts list. More...
 
int gvm_hosts_exclude_with_max (gvm_hosts_t *hosts, const char *excluded_str, unsigned int max_hosts)
 Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
int gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str)
 Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
char * gvm_host_reverse_lookup (gvm_host_t *host)
 Checks for a host object reverse dns lookup existence. More...
 
static int host_name_verify (gvm_host_t *host, const char *value)
 Verifies that hostname value resolves to a host's IP. More...
 
void gvm_host_add_reverse_lookup (gvm_host_t *host)
 Add a host's reverse-lookup name to the vhosts list. More...
 
int gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
 Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
int gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
 Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over the single hosts as it resets the iterator. More...
 
unsigned int gvm_hosts_count (const gvm_hosts_t *hosts)
 Gets the count of single hosts objects in a hosts collection. More...
 
unsigned int gvm_hosts_removed (const gvm_hosts_t *hosts)
 Gets the count of single values in hosts string that were removed (duplicates / excluded.) More...
 
int gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, const gvm_hosts_t *hosts)
 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. More...
 
enum host_type gvm_host_type (const gvm_host_t *host)
 Gets a host object's type. More...
 
gchar * gvm_host_type_str (const gvm_host_t *host)
 Gets a host's type in printable format. More...
 
gchar * gvm_host_value_str (const gvm_host_t *host)
 Gets a host's value in printable format. More...
 
int gvm_host_resolve (const gvm_host_t *host, void *dst, int family)
 Resolves a host object's name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAME. More...
 
int gvm_host_get_addr6 (const gvm_host_t *host, struct in6_addr *ip6)
 Gives a host object's value as an IPv6 address. If the host type is hostname, it resolves the IPv4 address then gives an IPv4-mapped IPv6 address (eg. ::ffff:192.168.1.1 .) If the host type is IPv4, it gives an IPv4-mapped IPv6 address. If the host's type is IPv6, it gives the value directly. More...
 

Variables

gchar * host_type_str [HOST_TYPE_MAX]
 

Detailed Description

Implementation of an API to handle Hosts objects.

This file contains all methods to handle Hosts collections (gvm_hosts_t) and single hosts objects (gvm_host_t.)

The module consequently uses glib datatypes.

Definition in file hosts.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "base hosts"

GLib log domain.

Definition at line 49 of file hosts.c.

Function Documentation

◆ cidr6_block_ips()

static int cidr6_block_ips ( const char *  str,
struct in6_addr *  first,
struct in6_addr *  last 
)
static

Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24 would give 192.168.1.1 as first and 192.168.1.254 as last. Thus, it skips the network and broadcast addresses.

Parameters
[in]strBuffer containing CIDR-expressed block.
[out]firstFirst IPv4 address in block.
[out]lastLast IPv4 address in block.
Returns
-1 if error, 0 else.

Definition at line 530 of file hosts.c.

531 {
532  unsigned int block;
533  int i, j;
534 
535  if (str == NULL || first == NULL || last == NULL)
536  return -1;
537 
538  /* Get IP and block values. */
539  if (cidr6_get_block (str, &block) == -1)
540  return -1;
541  if (cidr6_get_ip (str, first) == -1)
542  return -1;
543  memcpy (&last->s6_addr, &first->s6_addr, 16);
544 
545  /* /128 => Specified address is the first and last one. */
546  if (block == 128)
547  return 0;
548 
549  /* First IP: And with mask and increment to skip network address. */
550  j = 15;
551  for (i = (128 - block) / 8; i > 0; i--)
552  {
553  first->s6_addr[j] = 0;
554  j--;
555  }
556  first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
557 
558  /* Last IP: Broadcast address - 1. */
559  j = 15;
560  for (i = (128 - block) / 8; i > 0; i--)
561  {
562  last->s6_addr[j] = 0xff;
563  j--;
564  }
565  last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
566 
567  /* /127 => Only two addresses. Don't skip network / broadcast addresses.*/
568  if (block == 127)
569  return 0;
570 
571  /* Increment first IP. */
572  for (i = 15; i >= 0; --i)
573  if (first->s6_addr[i] < 255)
574  {
575  first->s6_addr[i]++;
576  break;
577  }
578  else
579  first->s6_addr[i] = 0;
580  /* Decrement last IP. */
581  for (i = 15; i >= 0; --i)
582  if (last->s6_addr[i] > 0)
583  {
584  last->s6_addr[i]--;
585  break;
586  }
587  else
588  last->s6_addr[i] = 0xff;
589 
590  return 0;
591 }

References cidr6_get_block(), and cidr6_get_ip().

Referenced by gvm_hosts_new_with_max().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cidr6_get_block()

static int cidr6_get_block ( const char *  str,
unsigned int *  block 
)
static

Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.

Parameters
[in]strBuffer containing CIDR-expressed block.
[out]blockVariable to store block value.
Returns
-1 if error, 0 otherwise.

Definition at line 474 of file hosts.c.

475 {
476  if (str == NULL || block == NULL)
477  return -1;
478 
479  if (sscanf (str, "%*[0-9a-fA-F.:]/%3u", block) != 1)
480  return -1;
481 
482  return 0;
483 }

Referenced by cidr6_block_ips().

Here is the caller graph for this function:

◆ cidr6_get_ip()

static int cidr6_get_ip ( const char *  str,
struct in6_addr *  addr6 
)
static

Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".

Parameters
[in]strString containing CIDR-expressed block.
[out]addr6Variable to store the IPv4 address value.
Returns
-1 if error, 0 otherwise.

Definition at line 495 of file hosts.c.

496 {
497  gchar *addr6_str, *tmp;
498 
499  if (str == NULL || addr6 == NULL)
500  return -1;
501 
502  addr6_str = g_strdup (str);
503  tmp = strchr (addr6_str, '/');
504  if (tmp == NULL)
505  {
506  g_free (addr6_str);
507  return -1;
508  }
509  *tmp = '\0';
510 
511  if (inet_pton (AF_INET6, addr6_str, addr6) != 1)
512  return -1;
513 
514  g_free (addr6_str);
515  return 0;
516 }

Referenced by cidr6_block_ips().

Here is the caller graph for this function:

◆ cidr_block_ips()

static int cidr_block_ips ( const char *  str,
struct in_addr *  first,
struct in_addr *  last 
)
static

Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24" would give 192.168.1.1 as first and 192.168.1.254 as last.

Both network and broadcast addresses are skipped:

  • They are never used as a host address. Not being included is the expected behaviour from users.
  • When needed, short/long ranges (eg. 192.168.1.0-255) are available.
Parameters
[in]strBuffer containing CIDR-expressed block.
[out]firstFirst IPv4 address in block.
[out]lastLast IPv4 address in block.
Returns
-1 if error, 0 else.

Definition at line 211 of file hosts.c.

212 {
213  unsigned int block;
214 
215  if (str == NULL || first == NULL || last == NULL)
216  return -1;
217 
218  /* Get IP and block values. */
219  if (cidr_get_block (str, &block) == -1)
220  return -1;
221  if (cidr_get_ip (str, first) == -1)
222  return -1;
223 
224  /* First IP: And with mask and increment. */
225  first->s_addr &= htonl (0xffffffff ^ ((1 << (32 - block)) - 1));
226  first->s_addr = htonl (ntohl (first->s_addr) + 1);
227 
228  /* Last IP: First IP + Number of usable hosts - 1. */
229  last->s_addr = htonl (ntohl (first->s_addr) + (1 << (32 - block)) - 3);
230  return 0;
231 }

References cidr_get_block(), and cidr_get_ip().

Referenced by gvm_hosts_new_with_max().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cidr_get_block()

static int cidr_get_block ( const char *  str,
unsigned int *  block 
)
static

Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.

Parameters
[in]strBuffer containing CIDR-expressed block.
[out]blockVariable to store block value.
Returns
-1 if error, 0 otherwise.

Definition at line 150 of file hosts.c.

151 {
152  if (str == NULL || block == NULL)
153  return -1;
154 
155  if (sscanf (str, "%*[0-9.]/%2u", block) != 1)
156  return -1;
157 
158  return 0;
159 }

Referenced by cidr_block_ips().

Here is the caller graph for this function:

◆ cidr_get_ip()

static int cidr_get_ip ( const char *  str,
struct in_addr *  addr 
)
static

Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".

Parameters
[in]strString containing CIDR-expressed block.
[out]addrVariable to store the IPv4 address value.
Returns
-1 if error, 0 otherwise.

Definition at line 171 of file hosts.c.

172 {
173  gchar *addr_str, *tmp;
174 
175  if (str == NULL || addr == NULL)
176  return -1;
177 
178  addr_str = g_strdup (str);
179  tmp = strchr (addr_str, '/');
180  if (tmp == NULL)
181  {
182  g_free (addr_str);
183  return -1;
184  }
185  *tmp = '\0';
186 
187  if (inet_pton (AF_INET, addr_str, addr) != 1)
188  return -1;
189 
190  g_free (addr_str);
191  return 0;
192 }

Referenced by cidr_block_ips().

Here is the caller graph for this function:

◆ gvm_get_host_type()

int gvm_get_host_type ( const gchar *  str_stripped)

Determines the host type in a buffer.

Parameters
[in]str_strippedBuffer that contains host definition, could a be hostname, single IPv4 or IPv6, CIDR-expressed block etc,.
Returns
Host_TYPE_*, -1 if error.

Definition at line 771 of file hosts.c.

772 {
773  /*
774  * We have a single element with no leading or trailing
775  * white spaces. This element could represent different host
776  * definitions: single IPs, host names, CIDR-expressed blocks,
777  * range-expressed networks, IPv6 addresses.
778  */
779 
780  /* Null or empty string. */
781  if (str_stripped == NULL || *str_stripped == '\0')
782  return -1;
783 
784  /* Check for regular single IPv4 address. */
785  if (is_ipv4_address (str_stripped))
786  return HOST_TYPE_IPV4;
787 
788  /* Check for regular single IPv6 address. */
789  if (is_ipv6_address (str_stripped))
790  return HOST_TYPE_IPV6;
791 
792  /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
793  if (is_cidr_block (str_stripped))
794  return HOST_TYPE_CIDR_BLOCK;
795 
796  /* Check for short range-expressed networks "192.168.12.5-40" */
797  if (is_short_range_network (str_stripped))
798  return HOST_TYPE_RANGE_SHORT;
799 
800  /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
801  if (is_long_range_network (str_stripped))
802  return HOST_TYPE_RANGE_LONG;
803 
804  /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
805  if (is_cidr6_block (str_stripped))
806  return HOST_TYPE_CIDR6_BLOCK;
807 
808  /* Check for short range-expressed networks "::1-ef12" */
809  if (is_short_range6_network (str_stripped))
810  return HOST_TYPE_RANGE6_SHORT;
811 
812  /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
813  if (is_long_range6_network (str_stripped))
814  return HOST_TYPE_RANGE6_LONG;
815 
816  /* Check for hostname. */
817  if (is_hostname (str_stripped))
818  return HOST_TYPE_NAME;
819 
820  return -1;
821 }

References HOST_TYPE_CIDR6_BLOCK, HOST_TYPE_CIDR_BLOCK, HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, HOST_TYPE_RANGE6_LONG, HOST_TYPE_RANGE6_SHORT, HOST_TYPE_RANGE_LONG, HOST_TYPE_RANGE_SHORT, is_cidr6_block(), is_cidr_block(), is_hostname(), is_ipv4_address(), is_ipv6_address(), is_long_range6_network(), is_long_range_network(), is_short_range6_network(), and is_short_range_network().

Referenced by gvm_hosts_new_with_max(), gvm_server_open_verify(), and osp_send_command().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_host_add_reverse_lookup()

void gvm_host_add_reverse_lookup ( gvm_host_t host)

Add a host's reverse-lookup name to the vhosts list.

Parameters
[in]hostThe host to which we add the vhost.

Definition at line 1633 of file hosts.c.

1634 {
1635  GSList *vhosts;
1636  gvm_vhost_t *vhost;
1637  char *value;
1638 
1639  if (!host || host->type == HOST_TYPE_NAME)
1640  return;
1641 
1642  value = gvm_host_reverse_lookup (host);
1643  if (!value)
1644  return;
1645  if (host_name_verify (host, value))
1646  {
1647  g_free (value);
1648  return;
1649  }
1650  /* Don't add vhost, if already in the list. */
1651  vhosts = host->vhosts;
1652  while (vhosts)
1653  {
1654  if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1655  {
1656  g_free (value);
1657  return;
1658  }
1659  vhosts = vhosts->next;
1660  }
1661  vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1662  host->vhosts = g_slist_prepend (host->vhosts, vhost);
1663 }

References gvm_host_reverse_lookup(), gvm_vhost_new(), host_name_verify(), HOST_TYPE_NAME, gvm_host::type, and gvm_host::vhosts.

Here is the call graph for this function:

◆ gvm_host_free()

static void gvm_host_free ( gpointer  host)
static

Frees the memory occupied by an gvm_host_t object.

Parameters
[in]hostHost to free.

Definition at line 880 of file hosts.c.

881 {
882  gvm_host_t *h = host;
883  if (h == NULL)
884  return;
885 
886  /* If host of type hostname, free the name buffer, first. */
887  if (h->type == HOST_TYPE_NAME)
888  g_free (h->name);
889 
890  g_slist_free_full (h->vhosts, gvm_vhost_free);
891  g_free (h);
892 }

References gvm_vhost_free(), HOST_TYPE_NAME, gvm_host::name, gvm_host::type, and gvm_host::vhosts.

Referenced by gvm_hosts_deduplicate(), gvm_hosts_exclude_with_max(), gvm_hosts_free(), gvm_hosts_resolve(), gvm_hosts_reverse_lookup_only(), and gvm_hosts_reverse_lookup_unify().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_host_get_addr6()

int gvm_host_get_addr6 ( const gvm_host_t host,
struct in6_addr *  ip6 
)

Gives a host object's value as an IPv6 address. If the host type is hostname, it resolves the IPv4 address then gives an IPv4-mapped IPv6 address (eg. ::ffff:192.168.1.1 .) If the host type is IPv4, it gives an IPv4-mapped IPv6 address. If the host's type is IPv6, it gives the value directly.

Parameters
[in]hostThe host object whose value to get as IPv6.
[out]ip6Buffer to store the IPv6 address.
Returns
-1 if error, 0 otherwise.

Definition at line 1955 of file hosts.c.

1956 {
1957  if (host == NULL || ip6 == NULL)
1958  return -1;
1959 
1960  switch (gvm_host_type (host))
1961  {
1962  case HOST_TYPE_IPV6:
1963  memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
1964  return 0;
1965 
1966  case HOST_TYPE_IPV4:
1967  ipv4_as_ipv6 (&host->addr, ip6);
1968  return 0;
1969 
1970  case HOST_TYPE_NAME:
1971  {
1972  struct in_addr ip4;
1973 
1974  /* Fail if IPv4 and IPv6 both don't resolve. */
1975  if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
1976  ipv4_as_ipv6 (&ip4, ip6);
1977  else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
1978  return -1;
1979  return 0;
1980  }
1981 
1982  default:
1983  return -1;
1984  }
1985 }

References gvm_host::addr, gvm_host::addr6, gvm_host_resolve(), gvm_host_type(), HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, and ipv4_as_ipv6().

Referenced by gvm_host_in_hosts().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_host_in_hosts()

int gvm_host_in_hosts ( const gvm_host_t host,
const struct in6_addr *  addr,
const gvm_hosts_t hosts 
)

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.

Parameters
[in]hostThe host object.
[in]addrOptional pointer to ip address. Could be used so that host isn't resolved multiple times when type is HOST_TYPE_NAME.
[in]hostsHosts collection.
Returns
1 if host has equal in hosts, 0 otherwise.

Definition at line 1796 of file hosts.c.

1798 {
1799  char *host_str;
1800  size_t i;
1801 
1802  if (host == NULL || hosts == NULL)
1803  return 0;
1804 
1805  host_str = gvm_host_value_str (host);
1806 
1807  for (i = 0; i < hosts->count; i++)
1808  {
1809  gvm_host_t *current_host = hosts->hosts[i];
1810  char *tmp = gvm_host_value_str (current_host);
1811 
1812  if (strcasecmp (host_str, tmp) == 0)
1813  {
1814  g_free (host_str);
1815  g_free (tmp);
1816  return 1;
1817  }
1818  g_free (tmp);
1819 
1820  /* Hostnames in hosts list shouldn't be resolved. */
1821  if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
1822  {
1823  struct in6_addr tmpaddr;
1824  gvm_host_get_addr6 (current_host, &tmpaddr);
1825 
1826  if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
1827  {
1828  g_free (host_str);
1829  return 1;
1830  }
1831  }
1832  }
1833 
1834  g_free (host_str);
1835  return 0;
1836 }

References gvm_hosts::count, gvm_host_get_addr6(), gvm_host_type(), gvm_host_value_str(), HOST_TYPE_NAME, and gvm_hosts::hosts.

Here is the call graph for this function:

◆ gvm_host_new()

static gvm_host_t* gvm_host_new ( )
static

Creates a new gvm_host_t object.

Returns
Pointer to new host object, NULL if creation fails.

Definition at line 865 of file hosts.c.

866 {
867  gvm_host_t *host;
868 
869  host = g_malloc0 (sizeof (gvm_host_t));
870 
871  return host;
872 }

Referenced by gvm_hosts_new_with_max(), and gvm_hosts_resolve().

Here is the caller graph for this function:

◆ gvm_host_resolve()

int gvm_host_resolve ( const gvm_host_t host,
void *  dst,
int  family 
)

Resolves a host object's name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAME.

Parameters
[in]hostThe host object whose name to resolve.
[out]dstBuffer to store resolved address. Size must be at least 4 bytes for AF_INET and 16 bytes for AF_INET6.
[in]familyEither AF_INET or AF_INET6.
Returns
-1 if error, 0 otherwise.

Definition at line 1934 of file hosts.c.

1935 {
1936  if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
1937  return -1;
1938 
1939  return gvm_resolve (host->name, dst, family);
1940 }

References gvm_resolve(), HOST_TYPE_NAME, gvm_host::name, and gvm_host::type.

Referenced by gvm_host_get_addr6().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_host_reverse_lookup()

char* gvm_host_reverse_lookup ( gvm_host_t host)

Checks for a host object reverse dns lookup existence.

Parameters
[in]hostThe host to reverse-lookup.
Returns
Result of look-up, NULL otherwise.

Definition at line 1549 of file hosts.c.

1550 {
1551  int retry = 2;
1552  gchar hostname[NI_MAXHOST];
1553  void *addr;
1554  size_t addrlen;
1555  struct sockaddr_in sa;
1556  struct sockaddr_in6 sa6;
1557 
1558  if (!host)
1559  return NULL;
1560 
1561  if (host->type == HOST_TYPE_IPV4)
1562  {
1563  addr = &sa;
1564  addrlen = sizeof (sa);
1565  memset (addr, '\0', addrlen);
1566  sa.sin_addr = host->addr;
1567  sa.sin_family = AF_INET;
1568  }
1569  else if (host->type == HOST_TYPE_IPV6)
1570  {
1571  addr = &sa6;
1572  addrlen = sizeof (sa6);
1573  memset (&sa6, '\0', addrlen);
1574  memcpy (&sa6.sin6_addr, &host->addr6, 16);
1575  sa6.sin6_family = AF_INET6;
1576  }
1577  else
1578  return NULL;
1579 
1580  while (retry--)
1581  {
1582  int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
1583  0, NI_NAMEREQD);
1584  if (!ret)
1585  return g_ascii_strdown (hostname, -1);
1586  if (ret != EAI_AGAIN)
1587  break;
1588  }
1589  return NULL;
1590 }

References gvm_host::addr, gvm_host::addr6, HOST_TYPE_IPV4, HOST_TYPE_IPV6, and gvm_host::type.

Referenced by gvm_host_add_reverse_lookup(), gvm_hosts_reverse_lookup_only(), and gvm_hosts_reverse_lookup_unify().

Here is the caller graph for this function:

◆ gvm_host_type()

enum host_type gvm_host_type ( const gvm_host_t host)

Gets a host object's type.

Parameters
[in]hostThe host object.
Returns
Host type.

Definition at line 1846 of file hosts.c.

1847 {
1848  assert (host);
1849  return host->type;
1850 }

References gvm_host::type.

Referenced by gvm_host_get_addr6(), and gvm_host_in_hosts().

Here is the caller graph for this function:

◆ gvm_host_type_str()

gchar* gvm_host_type_str ( const gvm_host_t host)

Gets a host's type in printable format.

Parameters
[in]hostThe host object.
Returns
String representing host type. Statically allocated, thus, not to be freed.

Definition at line 1861 of file hosts.c.

1862 {
1863  if (host == NULL)
1864  return NULL;
1865 
1866  return host_type_str[host->type];
1867 }

References host_type_str, and gvm_host::type.

◆ gvm_host_value_str()

gchar* gvm_host_value_str ( const gvm_host_t host)

Gets a host's value in printable format.

Parameters
[in]hostThe host object.
Returns
String representing host value. To be freed with g_free().

Definition at line 1877 of file hosts.c.

1878 {
1879  if (host == NULL)
1880  return NULL;
1881 
1882  switch (host->type)
1883  {
1884  case HOST_TYPE_NAME:
1885  return g_strdup (host->name);
1886  break;
1887  case HOST_TYPE_IPV4:
1888  case HOST_TYPE_IPV6:
1889  /* Handle both cases using inet_ntop(). */
1890  {
1891  int family, size;
1892  gchar *str;
1893  const void *srcaddr;
1894 
1895  if (host->type == HOST_TYPE_IPV4)
1896  {
1897  family = AF_INET;
1898  size = INET_ADDRSTRLEN;
1899  srcaddr = &host->addr;
1900  }
1901  else
1902  {
1903  family = AF_INET6;
1904  size = INET6_ADDRSTRLEN;
1905  srcaddr = &host->addr6;
1906  }
1907 
1908  str = g_malloc0 (size);
1909  if (inet_ntop (family, srcaddr, str, size) == NULL)
1910  {
1911  perror ("inet_ntop");
1912  g_free (str);
1913  return NULL;
1914  }
1915  return str;
1916  }
1917  default:
1918  return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
1919  }
1920 }

References gvm_host::addr, gvm_host::addr6, HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, gvm_host::name, and gvm_host::type.

Referenced by gvm_host_in_hosts(), gvm_hosts_deduplicate(), gvm_hosts_exclude_with_max(), and host_name_verify().

Here is the caller graph for this function:

◆ gvm_hosts_add()

static void gvm_hosts_add ( gvm_hosts_t hosts,
gvm_host_t host 
)
static

Inserts a host object at the end of a hosts collection.

Parameters
[in]hostsHosts in which to insert the host.
[in]hostHost to insert.

Definition at line 901 of file hosts.c.

902 {
903  if (hosts->count == hosts->max_size)
904  {
905  hosts->max_size *= 4;
906  hosts->hosts =
907  g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
908  }
909  hosts->hosts[hosts->count] = host;
910  hosts->count++;
911 }

References gvm_hosts::count, gvm_hosts::hosts, and gvm_hosts::max_size.

Referenced by gvm_hosts_new_with_max(), and gvm_hosts_resolve().

Here is the caller graph for this function:

◆ gvm_hosts_count()

unsigned int gvm_hosts_count ( const gvm_hosts_t hosts)

Gets the count of single hosts objects in a hosts collection.

Parameters
[in]hostsThe hosts collection to count hosts of.
Returns
The number of single hosts.

Definition at line 1764 of file hosts.c.

1765 {
1766  return hosts ? hosts->count : 0;
1767 }

References gvm_hosts::count.

Referenced by gvm_hosts_exclude_with_max().

Here is the caller graph for this function:

◆ gvm_hosts_deduplicate()

static void gvm_hosts_deduplicate ( gvm_hosts_t hosts)
static

Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current position.

Parameters
[in]hostshosts collection from which to remove duplicates.

Uses a hash table in order to deduplicate the hosts list in O(N) time.

Definition at line 977 of file hosts.c.

978 {
982  GHashTable *name_table;
983  size_t i, duplicates = 0;
984 
985  if (hosts == NULL)
986  return;
987  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
988 
989  for (i = 0; i < hosts->count; i++)
990  {
991  gchar *name;
992 
993  if ((name = gvm_host_value_str (hosts->hosts[i])))
994  {
995  gvm_host_t *host, *removed = hosts->hosts[i];
996 
997  host = g_hash_table_lookup (name_table, name);
998  if (host)
999  {
1000  /* Remove duplicate host. Add its vhosts to the original host. */
1001  host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
1002  removed->vhosts = NULL;
1003  gvm_host_free (removed);
1004  hosts->hosts[i] = NULL;
1005  duplicates++;
1006  g_free (name);
1007  }
1008  else
1009  g_hash_table_insert (name_table, name, hosts->hosts[i]);
1010  }
1011  }
1012 
1013  if (duplicates)
1014  gvm_hosts_fill_gaps (hosts);
1015  g_hash_table_destroy (name_table);
1016  hosts->count -= duplicates;
1017  hosts->removed += duplicates;
1018  hosts->current = 0;
1019  malloc_trim (0);
1020 }

References gvm_hosts::count, gvm_hosts::current, gvm_host_free(), gvm_host_value_str(), gvm_hosts_fill_gaps(), gvm_hosts::hosts, gvm_hosts::removed, and gvm_host::vhosts.

Referenced by gvm_hosts_new_with_max(), and gvm_hosts_resolve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_hosts_exclude()

int gvm_hosts_exclude ( gvm_hosts_t hosts,
const char *  excluded_str 
)

Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection from which to exclude.
[in]excluded_strString of hosts to exclude.
Returns
Number of excluded hosts, -1 if error.

Definition at line 1536 of file hosts.c.

1537 {
1538  return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1539 }

References gvm_hosts_exclude_with_max().

Here is the call graph for this function:

◆ gvm_hosts_exclude_with_max()

int gvm_hosts_exclude_with_max ( gvm_hosts_t hosts,
const char *  excluded_str,
unsigned int  max_hosts 
)

Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection from which to exclude.
[in]excluded_strString of hosts to exclude.
[in]max_hostsMax number of hosts in hosts_str. 0 means unlimited.
Returns
Number of excluded hosts, -1 if error.

Uses a hash table in order to exclude hosts in O(N+M) time.

Definition at line 1462 of file hosts.c.

1464 {
1468  gvm_hosts_t *excluded_hosts;
1469  GHashTable *name_table;
1470  size_t excluded = 0, i;
1471 
1472  if (hosts == NULL || excluded_str == NULL)
1473  return -1;
1474 
1475  excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1476  if (excluded_hosts == NULL)
1477  return -1;
1478 
1479  if (gvm_hosts_count (excluded_hosts) == 0)
1480  {
1481  gvm_hosts_free (excluded_hosts);
1482  return 0;
1483  }
1484 
1485  /* Hash host values from excluded hosts list. */
1486  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1487  for (i = 0; i < excluded_hosts->count; i++)
1488  {
1489  gchar *name;
1490 
1491  if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
1492  g_hash_table_insert (name_table, name, hosts);
1493  }
1494 
1495  /* Check for hosts values in hash table. */
1496  for (i = 0; i < hosts->count; i++)
1497  {
1498  gchar *name;
1499 
1500  if ((name = gvm_host_value_str (hosts->hosts[i])))
1501  {
1502  if (g_hash_table_lookup (name_table, name))
1503  {
1504  gvm_host_free (hosts->hosts[i]);
1505  hosts->hosts[i] = NULL;
1506  excluded++;
1507  g_free (name);
1508  continue;
1509  }
1510  g_free (name);
1511  }
1512  }
1513 
1514  /* Cleanup. */
1515  if (excluded)
1516  gvm_hosts_fill_gaps (hosts);
1517  hosts->count -= excluded;
1518  hosts->removed += excluded;
1519  hosts->current = 0;
1520  g_hash_table_destroy (name_table);
1521  gvm_hosts_free (excluded_hosts);
1522  return excluded;
1523 }

References gvm_hosts::count, gvm_hosts::current, gvm_host_free(), gvm_host_value_str(), gvm_hosts_count(), gvm_hosts_fill_gaps(), gvm_hosts_free(), gvm_hosts_new_with_max(), gvm_hosts::hosts, and gvm_hosts::removed.

Referenced by gvm_hosts_exclude().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_hosts_fill_gaps()

static void gvm_hosts_fill_gaps ( gvm_hosts_t hosts)
static

Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries.

Parameters
[in]hostsHosts collection to fill gaps in.

Definition at line 939 of file hosts.c.

940 {
941  size_t i;
942  if (!hosts)
943  return;
944 
945  for (i = 0; i < hosts->max_size; i++)
946  {
947  if (!hosts->hosts[i])
948  {
949  size_t j;
950 
951  /* Fill the gap with the closest host entry, in order to keep the
952  * sequential ordering. */
953  for (j = i + 1; j < hosts->max_size; j++)
954  {
955  if (hosts->hosts[j])
956  {
957  hosts->hosts[i] = hosts->hosts[j];
958  hosts->hosts[j] = NULL;
959  break;
960  }
961  }
962  /* No more entries left, ie. the empty space between count and
963  * max_size. */
964  if (!hosts->hosts[i])
965  return;
966  }
967  }
968 }

References gvm_hosts::hosts, and gvm_hosts::max_size.

Referenced by gvm_hosts_deduplicate(), gvm_hosts_exclude_with_max(), gvm_hosts_resolve(), gvm_hosts_reverse_lookup_only(), and gvm_hosts_reverse_lookup_unify().

Here is the caller graph for this function:

◆ gvm_hosts_free()

void gvm_hosts_free ( gvm_hosts_t hosts)

Frees memory occupied by an gvm_hosts_t structure.

Parameters
[in]hostsThe hosts collection to free.

Definition at line 1258 of file hosts.c.

1259 {
1260  size_t i;
1261 
1262  if (hosts == NULL)
1263  return;
1264 
1265  if (hosts->orig_str)
1266  g_free (hosts->orig_str);
1267  for (i = 0; i < hosts->count; i++)
1268  gvm_host_free (hosts->hosts[i]);
1269  g_free (hosts->hosts);
1270  g_free (hosts);
1271 }

References gvm_hosts::count, gvm_host_free(), gvm_hosts::hosts, and gvm_hosts::orig_str.

Referenced by gvm_hosts_exclude_with_max(), and gvm_hosts_new_with_max().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_hosts_init()

static gvm_hosts_t* gvm_hosts_init ( const char *  hosts_str)
static

Creates a hosts collection from a hosts string.

Parameters
[in]hosts_strString of hosts.
Returns
Hosts collection.

Definition at line 921 of file hosts.c.

922 {
923  gvm_hosts_t *hosts;
924 
925  hosts = g_malloc0 (sizeof (gvm_hosts_t));
926  hosts->max_size = 1024;
927  hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
928  hosts->orig_str = g_strdup (hosts_str);
929  return hosts;
930 }

References gvm_hosts::hosts, gvm_hosts::max_size, and gvm_hosts::orig_str.

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ gvm_hosts_new()

gvm_hosts_t* gvm_hosts_new ( const gchar *  hosts_str)

Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.

Parameters
[in]hosts_strThe hosts string. A copy will be created of this within the returned struct.
Returns
NULL if error, otherwise, a hosts structure that should be released using gvm_hosts_free.

Definition at line 1229 of file hosts.c.

1230 {
1231  return gvm_hosts_new_with_max (hosts_str, 0);
1232 }

References gvm_hosts_new_with_max().

Here is the call graph for this function:

◆ gvm_hosts_new_with_max()

gvm_hosts_t* gvm_hosts_new_with_max ( const gchar *  hosts_str,
unsigned int  max_hosts 
)

Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.

Parameters
[in]hosts_strThe hosts string. A copy will be created of this within the returned struct.
[in]max_hostsMax number of hosts in hosts_str. 0 means unlimited.
Returns
NULL if error or hosts_str contains more than max hosts. Otherwise, a hosts structure that should be released using gvm_hosts_free.

Definition at line 1034 of file hosts.c.

1035 {
1036  gvm_hosts_t *hosts;
1037  gchar **host_element, **split;
1038  gchar *str;
1039 
1040  if (hosts_str == NULL)
1041  return NULL;
1042 
1043  /* Normalize separator: Transform newlines into commas. */
1044  hosts = gvm_hosts_init (hosts_str);
1045  str = hosts->orig_str;
1046  while (*str)
1047  {
1048  if (*str == '\n')
1049  *str = ',';
1050  str++;
1051  }
1052 
1053  /* Split comma-separated list into single host-specifications */
1054  split = g_strsplit (hosts->orig_str, ",", 0);
1055 
1056  /* first element of the split list */
1057  host_element = split;
1058  while (*host_element)
1059  {
1060  int host_type;
1061  gchar *stripped = g_strstrip (*host_element);
1062 
1063  if (stripped == NULL || *stripped == '\0')
1064  {
1065  host_element++;
1066  continue;
1067  }
1068 
1069  /* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ?
1070  */
1071  /* -1 if error. */
1072  host_type = gvm_get_host_type (stripped);
1073 
1074  switch (host_type)
1075  {
1076  case HOST_TYPE_NAME:
1077  case HOST_TYPE_IPV4:
1078  case HOST_TYPE_IPV6:
1079  {
1080  /* New host. */
1081  gvm_host_t *host = gvm_host_new ();
1082  host->type = host_type;
1083  if (host_type == HOST_TYPE_NAME)
1084  host->name = g_ascii_strdown (stripped, -1);
1085  else if (host_type == HOST_TYPE_IPV4)
1086  {
1087  if (inet_pton (AF_INET, stripped, &host->addr) != 1)
1088  break;
1089  }
1090  else if (host_type == HOST_TYPE_IPV6)
1091  {
1092  if (inet_pton (AF_INET6, stripped, &host->addr6) != 1)
1093  break;
1094  }
1095  gvm_hosts_add (hosts, host);
1096  break;
1097  }
1098  case HOST_TYPE_CIDR_BLOCK:
1099  case HOST_TYPE_RANGE_SHORT:
1100  case HOST_TYPE_RANGE_LONG:
1101  {
1102  struct in_addr first, last;
1103  uint32_t current;
1104  int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
1105 
1107  ips_func = cidr_block_ips;
1108  else if (host_type == HOST_TYPE_RANGE_SHORT)
1109  ips_func = short_range_network_ips;
1110  else
1111  ips_func = long_range_network_ips;
1112 
1113  if (ips_func (stripped, &first, &last) == -1)
1114  break;
1115 
1116  /* Make sure that first actually comes before last */
1117  if (ntohl (first.s_addr) > ntohl (last.s_addr))
1118  break;
1119 
1120  /* Add addresses from first to last as single hosts. */
1121  current = first.s_addr;
1122  while (ntohl (current) <= ntohl (last.s_addr))
1123  {
1124  gvm_host_t *host;
1125  if (max_hosts > 0 && hosts->count > max_hosts)
1126  {
1127  g_strfreev (split);
1128  gvm_hosts_free (hosts);
1129  return NULL;
1130  }
1131  host = gvm_host_new ();
1132  host->type = HOST_TYPE_IPV4;
1133  host->addr.s_addr = current;
1134  gvm_hosts_add (hosts, host);
1135  /* Next IP address. */
1136  current = htonl (ntohl (current) + 1);
1137  }
1138  break;
1139  }
1140  case HOST_TYPE_CIDR6_BLOCK:
1141  case HOST_TYPE_RANGE6_LONG:
1143  {
1144  struct in6_addr first, last;
1145  unsigned char current[16];
1146  int (*ips_func) (const char *, struct in6_addr *,
1147  struct in6_addr *);
1148 
1150  ips_func = cidr6_block_ips;
1151  else if (host_type == HOST_TYPE_RANGE6_SHORT)
1152  ips_func = short_range6_network_ips;
1153  else
1154  ips_func = long_range6_network_ips;
1155 
1156  if (ips_func (stripped, &first, &last) == -1)
1157  break;
1158 
1159  /* Make sure the first comes before the last. */
1160  if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1161  break;
1162 
1163  /* Add addresses from first to last as single hosts. */
1164  memcpy (current, &first.s6_addr, 16);
1165  while (memcmp (current, &last.s6_addr, 16) <= 0)
1166  {
1167  int i;
1168  gvm_host_t *host;
1169 
1170  if (max_hosts > 0 && hosts->count > max_hosts)
1171  {
1172  g_strfreev (split);
1173  gvm_hosts_free (hosts);
1174  return NULL;
1175  }
1176  host = gvm_host_new ();
1177  host->type = HOST_TYPE_IPV6;
1178  memcpy (host->addr6.s6_addr, current, 16);
1179  gvm_hosts_add (hosts, host);
1180  /* Next IPv6 address. */
1181  for (i = 15; i >= 0; --i)
1182  if (current[i] < 255)
1183  {
1184  current[i]++;
1185  break;
1186  }
1187  else
1188  current[i] = 0;
1189  }
1190  break;
1191  }
1192  case -1:
1193  default:
1194  /* Invalid host string. */
1195  g_strfreev (split);
1196  gvm_hosts_free (hosts);
1197  return NULL;
1198  }
1199  host_element++; /* move on to next element of split list */
1200  if (max_hosts > 0 && hosts->count > max_hosts)
1201  {
1202  g_strfreev (split);
1203  gvm_hosts_free (hosts);
1204  return NULL;
1205  }
1206  }
1207 
1208  /* No need to check for duplicates when a hosts string contains a
1209  * single (IP/Hostname/Range/Subnetwork) entry. */
1210  if (g_strv_length (split) > 1)
1211  gvm_hosts_deduplicate (hosts);
1212 
1213  g_strfreev (split);
1214  malloc_trim (0);
1215  return hosts;
1216 }

References gvm_host::addr, gvm_host::addr6, cidr6_block_ips(), cidr_block_ips(), gvm_hosts::count, gvm_get_host_type(), gvm_host_new(), gvm_hosts_add(), gvm_hosts_deduplicate(), gvm_hosts_free(), gvm_hosts_init(), HOST_TYPE_CIDR6_BLOCK, HOST_TYPE_CIDR_BLOCK, HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, HOST_TYPE_RANGE6_LONG, HOST_TYPE_RANGE6_SHORT, HOST_TYPE_RANGE_LONG, HOST_TYPE_RANGE_SHORT, long_range6_network_ips(), long_range_network_ips(), gvm_host::name, gvm_hosts::orig_str, short_range6_network_ips(), short_range_network_ips(), and gvm_host::type.

Referenced by gvm_hosts_exclude_with_max(), and gvm_hosts_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gvm_hosts_next()

gvm_host_t* gvm_hosts_next ( gvm_hosts_t hosts)

Gets the next gvm_host_t from a gvm_hosts_t structure. The state of iteration is kept internally within the gvm_hosts structure.

Parameters
[in]hostsgvm_hosts_t structure to get next host from.
Returns
Pointer to host. NULL if error or end of hosts.

Definition at line 1243 of file hosts.c.

1244 {
1245  if (!hosts || hosts->current == hosts->count)
1246  return NULL;
1247 
1248  return hosts->hosts[hosts->current++];
1249 }

References gvm_hosts::count, gvm_hosts::current, and gvm_hosts::hosts.

◆ gvm_hosts_removed()

unsigned int gvm_hosts_removed ( const gvm_hosts_t hosts)

Gets the count of single values in hosts string that were removed (duplicates / excluded.)

Parameters
[in]hostsThe hosts collection.
Returns
The number of removed values.

Definition at line 1778 of file hosts.c.

1779 {
1780  return hosts ? hosts->removed : 0;
1781 }

References gvm_hosts::removed.

◆ gvm_hosts_resolve()

GSList* gvm_hosts_resolve ( gvm_hosts_t hosts)

Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection from which to exclude.
Returns
List of unresolved hostnames.

Definition at line 1339 of file hosts.c.

1340 {
1341  size_t i, new_entries = 0, resolved = 0;
1342  GSList *unresolved = NULL;
1343 
1344  for (i = 0; i < hosts->count; i++)
1345  {
1346  GSList *list, *tmp;
1347  gvm_host_t *host = hosts->hosts[i];
1348 
1349  if (host->type != HOST_TYPE_NAME)
1350  continue;
1351 
1352  list = tmp = gvm_resolve_list (host->name);
1353  while (tmp)
1354  {
1355  /* Create a new host for each IP address. */
1356  gvm_host_t *new;
1357  struct in6_addr *ip6 = tmp->data;
1358  gvm_vhost_t *vhost;
1359 
1360  new = gvm_host_new ();
1361  if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1362  || ip6->s6_addr32[2] != htonl (0xffff))
1363  {
1364  new->type = HOST_TYPE_IPV6;
1365  memcpy (&new->addr6, ip6, sizeof (new->addr6));
1366  }
1367  else
1368  {
1369  new->type = HOST_TYPE_IPV4;
1370  memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1371  }
1372  vhost =
1373  gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1374  new->vhosts = g_slist_prepend (new->vhosts, vhost);
1375  gvm_hosts_add (hosts, new);
1376  tmp = tmp->next;
1377  new_entries = 1;
1378  }
1379  /* Remove hostname from list, as it was either replaced by IPs, or
1380  * is unresolvable. */
1381  hosts->hosts[i] = NULL;
1382  resolved++;
1383  if (!list)
1384  unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
1385  gvm_host_free (host);
1386  g_slist_free_full (list, g_free);
1387  }
1388  if (resolved)
1389  gvm_hosts_fill_gaps (hosts);
1390  hosts->count -= resolved;
1391  hosts->removed += resolved;
1392  if (new_entries)
1393  gvm_hosts_deduplicate (hosts);
1394  hosts->current = 0;
1395  return unresolved;
1396 }

References gvm_hosts::count, gvm_hosts::current, gvm_host_free(), gvm_host_new(), gvm_hosts_add(), gvm_hosts_deduplicate(), gvm_hosts_fill_gaps(), gvm_resolve_list(), gvm_vhost_new(), HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, gvm_hosts::hosts, gvm_host::name, gvm_hosts::removed, and gvm_host::type.

Here is the call graph for this function:

◆ gvm_hosts_reverse()

void gvm_hosts_reverse ( gvm_hosts_t hosts)

Reverses the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection to reverse.

Definition at line 1313 of file hosts.c.

1314 {
1315  size_t i, j;
1316  if (hosts == NULL)
1317  return;
1318 
1319  for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1320  {
1321  gvm_host_t *tmp = hosts->hosts[i];
1322  hosts->hosts[i] = hosts->hosts[j];
1323  hosts->hosts[j] = tmp;
1324  }
1325  hosts->current = 0;
1326 }

References gvm_hosts::count, gvm_hosts::current, and gvm_hosts::hosts.

◆ gvm_hosts_reverse_lookup_only()

int gvm_hosts_reverse_lookup_only ( gvm_hosts_t hosts)

Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection to filter.
Returns
Number of hosts removed, -1 if error.

Definition at line 1675 of file hosts.c.

1676 {
1677  size_t i, count = 0;
1678 
1679  if (hosts == NULL)
1680  return -1;
1681 
1682  for (i = 0; i < hosts->count; i++)
1683  {
1684  gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1685 
1686  if (name == NULL)
1687  {
1688  gvm_host_free (hosts->hosts[i]);
1689  hosts->hosts[i] = NULL;
1690  count++;
1691  }
1692  else
1693  g_free (name);
1694  }
1695 
1696  if (count)
1697  gvm_hosts_fill_gaps (hosts);
1698  hosts->count -= count;
1699  hosts->removed += count;
1700  hosts->current = 0;
1701  return count;
1702 }

References gvm_hosts::count, gvm_hosts::current, gvm_host_free(), gvm_host_reverse_lookup(), gvm_hosts_fill_gaps(), gvm_hosts::hosts, and gvm_hosts::removed.

Here is the call graph for this function:

◆ gvm_hosts_reverse_lookup_unify()

int gvm_hosts_reverse_lookup_unify ( gvm_hosts_t hosts)

Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection to filter.
Returns
Number of hosts removed, -1 if error.

Uses a hash table in order to unify the hosts list in O(N) time.

Definition at line 1714 of file hosts.c.

1715 {
1719  size_t i, count = 0;
1720  GHashTable *name_table;
1721 
1722  if (hosts == NULL)
1723  return -1;
1724 
1725  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1726  for (i = 0; i < hosts->count; i++)
1727  {
1728  gchar *name;
1729 
1730  if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
1731  {
1732  if (g_hash_table_lookup (name_table, name))
1733  {
1734  gvm_host_free (hosts->hosts[i]);
1735  hosts->hosts[i] = NULL;
1736  count++;
1737  g_free (name);
1738  }
1739  else
1740  {
1741  /* Insert in the hash table. Value not important. */
1742  g_hash_table_insert (name_table, name, hosts);
1743  }
1744  }
1745  }
1746 
1747  if (count)
1748  gvm_hosts_fill_gaps (hosts);
1749  g_hash_table_destroy (name_table);
1750  hosts->removed += count;
1751  hosts->count -= count;
1752  hosts->current = 0;
1753  return count;
1754 }

References gvm_hosts::count, gvm_hosts::current, gvm_host_free(), gvm_host_reverse_lookup(), gvm_hosts_fill_gaps(), gvm_hosts::hosts, and gvm_hosts::removed.

Here is the call graph for this function:

◆ gvm_hosts_shuffle()

void gvm_hosts_shuffle ( gvm_hosts_t hosts)

Randomizes the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator.

Parameters
[in]hostsThe hosts collection to shuffle.

Definition at line 1281 of file hosts.c.

1282 {
1283  size_t i = 0;
1284  GRand *rand;
1285 
1286  if (hosts == NULL)
1287  return;
1288 
1289  /* Shuffle the array. */
1290  rand = g_rand_new ();
1291  for (i = 0; i < hosts->count; i++)
1292  {
1293  void *tmp;
1294  int j = g_rand_int_range (rand, 0, hosts->count);
1295 
1296  tmp = hosts->hosts[i];
1297  hosts->hosts[i] = hosts->hosts[j];
1298  hosts->hosts[j] = tmp;
1299  }
1300 
1301  hosts->current = 0;
1302  g_rand_free (rand);
1303 }

References gvm_hosts::count, gvm_hosts::current, and gvm_hosts::hosts.

◆ gvm_vhost_free()

static void gvm_vhost_free ( gpointer  vhost)
static

Frees the memory occupied by an gvm_vhost_t object.

Parameters
[in]vhostVhost to free.

Definition at line 849 of file hosts.c.

850 {
851  if (vhost)
852  {
853  g_free (((gvm_vhost_t *) vhost)->value);
854  g_free (((gvm_vhost_t *) vhost)->source);
855  }
856  g_free (vhost);
857 }

Referenced by gvm_host_free(), and gvm_vhosts_exclude().

Here is the caller graph for this function:

◆ gvm_vhost_new()

gvm_vhost_t* gvm_vhost_new ( char *  value,
char *  source 
)

Creates a new gvm_vhost_t object.

Parameters
[in]valueVhost value.
[in]sourceSource of hostname.
Returns
Pointer to new vhost object.

Definition at line 832 of file hosts.c.

833 {
834  gvm_vhost_t *vhost;
835 
836  vhost = g_malloc0 (sizeof (gvm_vhost_t));
837  vhost->value = value;
838  vhost->source = source;
839 
840  return vhost;
841 }

References gvm_vhost::source, and gvm_vhost::value.

Referenced by gvm_host_add_reverse_lookup(), and gvm_hosts_resolve().

Here is the caller graph for this function:

◆ gvm_vhosts_exclude()

int gvm_vhosts_exclude ( gvm_host_t host,
const char *  excluded_str 
)

Exclude a list of vhosts from a host's vhosts list.

Parameters
[in]hostThe host whose vhosts are to be excluded from.
[in]excluded_strString of hosts to exclude.
Returns
Number of excluded vhosts.

Definition at line 1407 of file hosts.c.

1408 {
1409  GSList *vhost;
1410  char **excluded;
1411  int ret = 0;
1412 
1413  if (!host || !excluded_str)
1414  return ret;
1415 
1416  vhost = host->vhosts;
1417  excluded = g_strsplit (excluded_str, ",", 0);
1418  if (!excluded || !*excluded)
1419  {
1420  g_strfreev (excluded);
1421  return ret;
1422  }
1423  while (vhost)
1424  {
1425  char **tmp = excluded;
1426  char *value = ((gvm_vhost_t *) vhost->data)->value;
1427 
1428  while (*tmp)
1429  {
1430  if (!strcasecmp (value, g_strstrip (*tmp)))
1431  {
1432  gvm_vhost_free (vhost->data);
1433  host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1434  ret++;
1435  break;
1436  }
1437  tmp++;
1438  if (!*tmp)
1439  {
1440  vhost = vhost->next;
1441  break;
1442  }
1443  }
1444  }
1445  g_strfreev (excluded);
1446 
1447  return ret;
1448 }

References gvm_vhost_free(), and gvm_host::vhosts.

Here is the call graph for this function:

◆ host_name_verify()

static int host_name_verify ( gvm_host_t host,
const char *  value 
)
static

Verifies that hostname value resolves to a host's IP.

Parameters
[in]hostThe host whose IP is to be checked against.
[in]valueHostname value to verify.
Returns
0 if hostname resolves to host's IP, -1 otherwise.

Definition at line 1601 of file hosts.c.

1602 {
1603  GSList *list, *tmp;
1604  char *host_str;
1605  int ret = -1;
1606 
1607  assert (host);
1608  assert (value);
1609  host_str = gvm_host_value_str (host);
1610  list = tmp = gvm_resolve_list (value);
1611  while (tmp)
1612  {
1613  char buffer[INET6_ADDRSTRLEN];
1614  addr6_to_str (tmp->data, buffer);
1615  if (!strcasecmp (host_str, buffer))
1616  {
1617  ret = 0;
1618  break;
1619  }
1620  tmp = tmp->next;
1621  }
1622  g_free (host_str);
1623  g_slist_free_full (list, g_free);
1624  return ret;
1625 }

References addr6_to_str(), gvm_host_value_str(), and gvm_resolve_list().

Referenced by gvm_host_add_reverse_lookup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_cidr6_block()

static int is_cidr6_block ( const char *  str)
static

Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid, "2620:0:2d0:200::7/129" is not.

Parameters
[in]strBuffer to check in.
Returns
1 if valid IPv6 CIDR-expressed block, 0 otherwise.

Definition at line 431 of file hosts.c.

432 {
433  long block;
434  char *addr6_str, *block_str, *p;
435 
436  addr6_str = g_strdup (str);
437  block_str = strchr (addr6_str, '/');
438  if (block_str == NULL)
439  {
440  g_free (addr6_str);
441  return 0;
442  }
443 
444  /* Separate the address from the block value. */
445  *block_str = '\0';
446  block_str++;
447 
448  if (!is_ipv6_address (addr6_str) || !isdigit (*block_str))
449  {
450  g_free (addr6_str);
451  return 0;
452  }
453 
454  p = NULL;
455  block = strtol (block_str, &p, 10);
456  g_free (addr6_str);
457 
458  if (*p || block <= 0 || block > 128)
459  return 0;
460 
461  return 1;
462 }

References is_ipv6_address().

Referenced by gvm_get_host_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_cidr_block()

static int is_cidr_block ( const char *  str)
static

Checks if a buffer points to an IPv4 CIDR-expressed block. "192.168.12.3/24" is valid, "192.168.1.3/31" is not.

Parameters
[in]strBuffer to check in.
Returns
1 if valid CIDR-expressed block, 0 otherwise.

Definition at line 104 of file hosts.c.

105 {
106  long block;
107  char *addr_str, *block_str, *p;
108 
109  addr_str = g_strdup (str);
110  block_str = strchr (addr_str, '/');
111  if (block_str == NULL)
112  {
113  g_free (addr_str);
114  return 0;
115  }
116 
117  /* Separate the address from the block value. */
118  *block_str = '\0';
119  block_str++;
120 
121  if (!is_ipv4_address (addr_str) || !isdigit (*block_str))
122  {
123  g_free (addr_str);
124  return 0;
125  }
126 
127  p = NULL;
128  block = strtol (block_str, &p, 10);
129 
130  if (*p || block <= 0 || block > 30)
131  {
132  g_free (addr_str);
133  return 0;
134  }
135 
136  g_free (addr_str);
137  return 1;
138 }

References is_ipv4_address().

Referenced by gvm_get_host_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_hostname()

static int is_hostname ( const char *  str)
static

Checks if a buffer points to a valid hostname. Valid characters include: Alphanumerics, dot (.), dash (-) and underscore (_) up to 255 characters.

Parameters
[in]strBuffer to check in.
Returns
1 if valid hostname, 0 otherwise.

Definition at line 408 of file hosts.c.

409 {
410  const char *h = str;
411 
412  while (*h && (isalnum (*h) || strchr ("-_.", *h)))
413  h++;
414 
415  /* Valid string if no other chars, and length is 255 at most. */
416  if (*h == '\0' && h - str < 256)
417  return 1;
418 
419  return 0;
420 }

Referenced by gvm_get_host_type().

Here is the caller graph for this function:

◆ is_ipv4_address()

static int is_ipv4_address ( const char *  str)
static

Checks if a buffer points to a valid IPv4 address. "192.168.11.1" is valid, "192.168.1.300" and "192.168.1.1e" are not.

Parameters
[in]strBuffer to check in.
Returns
1 if valid IPv4 address, 0 otherwise.

Definition at line 72 of file hosts.c.

73 {
74  struct sockaddr_in sa;
75 
76  return inet_pton (AF_INET, str, &(sa.sin_addr)) == 1;
77 }

Referenced by gvm_get_host_type(), is_cidr_block(), is_long_range_network(), and is_short_range_network().

Here is the caller graph for this function:

◆ is_ipv6_address()

static int is_ipv6_address ( const char *  str)
static

Checks if a buffer points to a valid IPv6 address. "0:0:0:0:0:0:0:1", "::1" and "::FFFF:192.168.13.55" are valid "::1g" is not.

Parameters
[in]strBuffer to check in.
Returns
1 if valid IPv6 address, 0 otherwise.

Definition at line 88 of file hosts.c.

89 {
90  struct sockaddr_in6 sa6;
91 
92  return inet_pton (AF_INET6, str, &(sa6.sin6_addr)) == 1;
93 }

Referenced by gvm_get_host_type(), is_cidr6_block(), is_long_range6_network(), and is_short_range6_network().

Here is the caller graph for this function:

◆ is_long_range6_network()

static int is_long_range6_network ( const char *  str)
static

Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid.

Parameters
[in]strBuffer to check in.
Returns
1 if valid long range-expressed network, 0 otherwise.

Definition at line 602 of file hosts.c.

603 {
604  char *first_str, *second_str;
605  int ret;
606 
607  first_str = g_strdup (str);
608  second_str = strchr (first_str, '-');
609  if (second_str == NULL)
610  {
611  g_free (first_str);
612  return 0;
613  }
614 
615  /* Separate the addresses. */
616  *second_str = '\0';
617  second_str++;
618 
619  ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
620  g_free (first_str);
621 
622  return ret;
623 }

References is_ipv6_address().

Referenced by gvm_get_host_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_long_range_network()

static int is_long_range_network ( const char *  str)
static

Checks if a buffer points to a valid long range-expressed network. "192.168.12.1-192.168.13.50" is valid.

Parameters
[in]strBuffer to check in.
Returns
1 if valid long range-expressed network, 0 otherwise.

Definition at line 242 of file hosts.c.

243 {
244  char *first_str, *second_str;
245  int ret;
246 
247  first_str = g_strdup (str);
248  second_str = strchr (first_str, '-');
249  if (second_str == NULL)
250  {
251  g_free (first_str);
252  return 0;
253  }
254 
255  /* Separate the addresses. */
256  *second_str = '\0';
257  second_str++;
258 
259  ret = is_ipv4_address (first_str) && is_ipv4_address (second_str);
260  g_free (first_str);
261 
262  return ret;
263 }

References is_ipv4_address().

Referenced by gvm_get_host_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_short_range6_network()

static int is_short_range6_network ( const char *  str)
static

Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid.

Parameters
strString to check in.
Returns
1 if str points to a valid short-range IPv6 network, 0 otherwise.

Definition at line 677 of file hosts.c.

678 {
679  char *ip_str, *end_str, *p;
680 
681  ip_str = g_strdup (str);
682  end_str = strchr (ip_str, '-');
683  if (end_str == NULL)
684  {
685  g_free (ip_str);
686  return 0;
687  }
688 
689  /* Separate the addresses. */
690  *end_str = '\0';
691  end_str++;
692 
693  if (!is_ipv6_address (ip_str) || *end_str == '\0')
694  {
695  g_free (ip_str);
696  return 0;
697  }
698 
699  p = end_str;
700  /* Check that the 2nd part is at most 4 hexadecimal characters. */
701  while (isxdigit (*p) && p++)
702  ;
703  if (*p || p - end_str > 4)
704  {
705  g_free (ip_str);
706  return 0;
707  }
708 
709  g_free (ip_str);
710  return 1;
711 }

References is_ipv6_address().

Referenced by gvm_get_host_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_short_range_network()

static int is_short_range_network ( const char *  str)
static

Checks if a buffer points to a valid short range-expressed network. "192.168.11.1-50" is valid, "192.168.1.1-50e" and "192.168.1.1-300" are not.

Parameters
strString to check in.
Returns
1 if str points to a valid short range-network, 0 otherwise.

Definition at line 317 of file hosts.c.

318 {
319  long end;
320  char *ip_str, *end_str, *p;
321 
322  ip_str = g_strdup (str);
323  end_str = strchr (ip_str, '-');
324  if (end_str == NULL)
325  {
326  g_free (ip_str);
327  return 0;
328  }
329 
330  /* Separate the addresses. */
331  *end_str = '\0';
332  end_str++;
333 
334  if (!is_ipv4_address (ip_str) || !isdigit (*end_str))
335  {
336  g_free (ip_str);
337  return 0;
338  }
339 
340  p = NULL;
341  end = strtol (end_str, &p, 10);
342  g_free (ip_str);
343 
344  if (*p || end < 0 || end > 255)
345  return 0;
346 
347  return 1;
348 }

References is_ipv4_address().

Referenced by gvm_get_host_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ long_range6_network_ips()

static int long_range6_network_ips ( const char *  str,
struct in6_addr *  first,
struct in6_addr *  last 
)
static

Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:500" would give ::1:200:7 as first and ::1:205:500 as last.

Parameters
[in]strString containing long IPv6 range-expressed network.
[out]firstFirst IPv6 address in range.
[out]lastLast IPv6 address in range.
Returns
-1 if error, 0 else.

Definition at line 637 of file hosts.c.

639 {
640  char *first_str, *last_str;
641 
642  if (str == NULL || first == NULL || last == NULL)
643  return -1;
644 
645  first_str = g_strdup (str);
646  last_str = strchr (first_str, '-');
647  if (last_str == NULL)
648  {
649  g_free (first_str);
650  return -1;
651  }
652 
653  /* Separate the two IPs. */
654  *last_str = '\0';
655  last_str++;
656 
657  if (inet_pton (AF_INET6, first_str, first) != 1
658  || inet_pton (AF_INET6, last_str, last) != 1)
659  {
660  g_free (first_str);
661  return -1;
662  }
663 
664  g_free (first_str);
665  return 0;
666 }

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ long_range_network_ips()

static int long_range_network_ips ( const char *  str,
struct in_addr *  first,
struct in_addr *  last 
)
static

Gets the first and last IPv4 addresses from a long range-expressed network. eg. "192.168.1.1-192.168.2.40" would give 192.168.1.1 as first and 192.168.2.40 as last.

Parameters
[in]strString containing long range-expressed network.
[out]firstFirst IP address in block.
[out]lastLast IP address in block.
Returns
-1 if error, 0 else.

Definition at line 277 of file hosts.c.

279 {
280  char *first_str, *last_str;
281 
282  if (str == NULL || first == NULL || last == NULL)
283  return -1;
284 
285  first_str = g_strdup (str);
286  last_str = strchr (first_str, '-');
287  if (last_str == NULL)
288  {
289  g_free (first_str);
290  return -1;
291  }
292 
293  /* Separate the two IPs. */
294  *last_str = '\0';
295  last_str++;
296 
297  if (inet_pton (AF_INET, first_str, first) != 1
298  || inet_pton (AF_INET, last_str, last) != 1)
299  {
300  g_free (first_str);
301  return -1;
302  }
303 
304  g_free (first_str);
305  return 0;
306 }

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ short_range6_network_ips()

static int short_range6_network_ips ( const char *  str,
struct in6_addr *  first,
struct in6_addr *  last 
)
static

Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005" would give ::ffee:1:1001 as first and ::ffee:1:1005 as last.

Parameters
[in]strString containing short IPv6 range-expressed network.
[out]firstFirst IPv6 address in range.
[out]lastLast IPv6 address in range.
Returns
-1 if error, 0 else.

Definition at line 725 of file hosts.c.

727 {
728  char *first_str, *last_str;
729  long int end;
730 
731  if (str == NULL || first == NULL || last == NULL)
732  return -1;
733 
734  first_str = g_strdup (str);
735  last_str = strchr (first_str, '-');
736  if (last_str == NULL)
737  {
738  g_free (first_str);
739  return -1;
740  }
741 
742  /* Separate the first IP. */
743  *last_str = '\0';
744  last_str++;
745 
746  if (inet_pton (AF_INET6, first_str, first) != 1)
747  {
748  g_free (first_str);
749  return -1;
750  }
751 
752  /* Calculate the last IP. */
753  memcpy (last, first, sizeof (*last));
754  end = strtol (last_str, NULL, 16);
755  memcpy (&last->s6_addr[15], &end, 1);
756  memcpy (&last->s6_addr[14], ((char *) &end) + 1, 1);
757 
758  g_free (first_str);
759  return 0;
760 }

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ short_range_network_ips()

static int short_range_network_ips ( const char *  str,
struct in_addr *  first,
struct in_addr *  last 
)
static

Gets the first and last IPv4 addresses from a short range-expressed network. "192.168.1.1-40" would give 192.168.1.1 as first and 192.168.1.40 as last.

Parameters
[in]strString containing short range-expressed network.
[out]firstFirst IP address in block.
[out]lastLast IP address in block.
Returns
-1 if error, 0 else.

Definition at line 362 of file hosts.c.

364 {
365  char *first_str, *last_str;
366  int end;
367 
368  if (str == NULL || first == NULL || last == NULL)
369  return -1;
370 
371  first_str = g_strdup (str);
372  last_str = strchr (first_str, '-');
373  if (last_str == NULL)
374  {
375  g_free (first_str);
376  return -1;
377  }
378 
379  /* Separate the two IPs. */
380  *last_str = '\0';
381  last_str++;
382  end = atoi (last_str);
383 
384  /* Get the first IP */
385  if (inet_pton (AF_INET, first_str, first) != 1)
386  {
387  g_free (first_str);
388  return -1;
389  }
390 
391  /* Get the last IP */
392  last->s_addr = htonl ((ntohl (first->s_addr) & 0xffffff00) + end);
393 
394  g_free (first_str);
395  return 0;
396 }

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

Variable Documentation

◆ host_type_str

gchar* host_type_str[HOST_TYPE_MAX]
Initial value:
= {
[HOST_TYPE_NAME] = "Hostname",
[HOST_TYPE_IPV4] = "IPv4",
[HOST_TYPE_IPV6] = "IPv6",
[HOST_TYPE_CIDR_BLOCK] = "IPv4 CIDR block",
[HOST_TYPE_RANGE_SHORT] = "IPv4 short range",
[HOST_TYPE_RANGE_LONG] = "IPv4 long range"}

Definition at line 53 of file hosts.c.

Referenced by gvm_host_type_str().

gvm_host_value_str
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:1877
long_range6_network_ips
static int long_range6_network_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:50...
Definition: hosts.c:637
gvm_host::name
gchar * name
Definition: hosts.h:67
addr6_to_str
void addr6_to_str(const struct in6_addr *addr6, char *str)
Stringifies an IP address.
Definition: networking.c:260
cidr6_block_ips
static int cidr6_block_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192....
Definition: hosts.c:530
long_range_network_ips
static int long_range_network_ips(const char *str, struct in_addr *first, struct in_addr *last)
Gets the first and last IPv4 addresses from a long range-expressed network. eg. "192....
Definition: hosts.c:277
is_short_range_network
static int is_short_range_network(const char *str)
Checks if a buffer points to a valid short range-expressed network. "192.168.11.1-50" is valid,...
Definition: hosts.c:317
is_hostname
static int is_hostname(const char *str)
Checks if a buffer points to a valid hostname. Valid characters include: Alphanumerics,...
Definition: hosts.c:408
gvm_hosts::count
size_t count
Definition: hosts.h:96
gvm_hosts::removed
size_t removed
Definition: hosts.h:97
gvm_hosts_free
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition: hosts.c:1258
gvm_resolve_list
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition: networking.c:338
is_long_range_network
static int is_long_range_network(const char *str)
Checks if a buffer points to a valid long range-expressed network. "192.168.12.1-192....
Definition: hosts.c:242
gvm_hosts_init
static gvm_hosts_t * gvm_hosts_init(const char *hosts_str)
Creates a hosts collection from a hosts string.
Definition: hosts.c:921
is_cidr6_block
static int is_cidr6_block(const char *str)
Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid,...
Definition: hosts.c:431
ipv4_as_ipv6
void ipv4_as_ipv6(const struct in_addr *ip4, struct in6_addr *ip6)
Maps an IPv4 address as an IPv6 address. eg. 192.168.10.20 would map to ::ffff:192....
Definition: networking.c:242
HOST_TYPE_IPV6
@ HOST_TYPE_IPV6
Definition: hosts.h:43
gvm_host_free
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:880
short_range6_network_ips
static int short_range6_network_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005...
Definition: hosts.c:725
gvm_hosts::max_size
size_t max_size
Definition: hosts.h:94
gvm_hosts_count
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition: hosts.c:1764
is_long_range6_network
static int is_long_range6_network(const char *str)
Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid.
Definition: hosts.c:602
HOST_TYPE_RANGE_SHORT
@ HOST_TYPE_RANGE_SHORT
Definition: hosts.h:41
gvm_hosts_new_with_max
gvm_hosts_t * gvm_hosts_new_with_max(const gchar *hosts_str, unsigned int max_hosts)
Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
Definition: hosts.c:1034
gvm_host::type
enum host_type type
Definition: hosts.h:71
gvm_resolve
int gvm_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
Definition: networking.c:388
gvm_host_get_addr6
int gvm_host_get_addr6(const gvm_host_t *host, struct in6_addr *ip6)
Gives a host object's value as an IPv6 address. If the host type is hostname, it resolves the IPv4 ad...
Definition: hosts.c:1955
gvm_vhost
The structure for a single vhost object.
Definition: hosts.h:78
HOST_TYPE_IPV4
@ HOST_TYPE_IPV4
Definition: hosts.h:39
cidr6_get_ip
static int cidr6_get_ip(const char *str, struct in6_addr *addr6)
Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192....
Definition: hosts.c:495
gvm_hosts_add
static void gvm_hosts_add(gvm_hosts_t *hosts, gvm_host_t *host)
Inserts a host object at the end of a hosts collection.
Definition: hosts.c:901
gvm_hosts::hosts
gvm_host_t ** hosts
Definition: hosts.h:93
gvm_get_host_type
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition: hosts.c:771
gvm_host
The structure for a single host object.
Definition: hosts.h:63
HOST_TYPE_CIDR6_BLOCK
@ HOST_TYPE_CIDR6_BLOCK
Definition: hosts.h:44
gvm_hosts::current
size_t current
Definition: hosts.h:95
cidr_block_ips
static int cidr_block_ips(const char *str, struct in_addr *first, struct in_addr *last)
Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192....
Definition: hosts.c:211
gvm_vhost::value
char * value
Definition: hosts.h:80
HOST_TYPE_RANGE_LONG
@ HOST_TYPE_RANGE_LONG
Definition: hosts.h:42
gvm_host::addr
struct in_addr addr
Definition: hosts.h:68
HOST_TYPE_RANGE6_LONG
@ HOST_TYPE_RANGE6_LONG
Definition: hosts.h:45
gvm_host_type
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition: hosts.c:1846
is_cidr_block
static int is_cidr_block(const char *str)
Checks if a buffer points to an IPv4 CIDR-expressed block. "192.168.12.3/24" is valid,...
Definition: hosts.c:104
gvm_hosts_exclude_with_max
int gvm_hosts_exclude_with_max(gvm_hosts_t *hosts, const char *excluded_str, unsigned int max_hosts)
Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating ...
Definition: hosts.c:1462
is_ipv4_address
static int is_ipv4_address(const char *str)
Checks if a buffer points to a valid IPv4 address. "192.168.11.1" is valid, "192.168....
Definition: hosts.c:72
gvm_host_new
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition: hosts.c:865
cidr_get_ip
static int cidr_get_ip(const char *str, struct in_addr *addr)
Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192....
Definition: hosts.c:171
gvm_vhost::source
char * source
Definition: hosts.h:81
gvm_host_reverse_lookup
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition: hosts.c:1549
is_short_range6_network
static int is_short_range6_network(const char *str)
Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid.
Definition: hosts.c:677
gvm_vhost_free
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition: hosts.c:849
gvm_hosts::orig_str
gchar * orig_str
Definition: hosts.h:92
host_name_verify
static int host_name_verify(gvm_host_t *host, const char *value)
Verifies that hostname value resolves to a host's IP.
Definition: hosts.c:1601
host_type
host_type
Definition: hosts.h:36
host_type_str
gchar * host_type_str[HOST_TYPE_MAX]
Definition: hosts.c:53
cidr_get_block
static int cidr_get_block(const char *str, unsigned int *block)
Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
Definition: hosts.c:150
gvm_host::addr6
struct in6_addr addr6
Definition: hosts.h:69
gvm_vhost_new
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition: hosts.c:832
HOST_TYPE_CIDR_BLOCK
@ HOST_TYPE_CIDR_BLOCK
Definition: hosts.h:40
gvm_hosts_fill_gaps
static void gvm_hosts_fill_gaps(gvm_hosts_t *hosts)
Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries.
Definition: hosts.c:939
short_range_network_ips
static int short_range_network_ips(const char *str, struct in_addr *first, struct in_addr *last)
Gets the first and last IPv4 addresses from a short range-expressed network. "192....
Definition: hosts.c:362
cidr6_get_block
static int cidr6_get_block(const char *str, unsigned int *block)
Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
Definition: hosts.c:474
HOST_TYPE_RANGE6_SHORT
@ HOST_TYPE_RANGE6_SHORT
Definition: hosts.h:46
gvm_hosts_deduplicate
static void gvm_hosts_deduplicate(gvm_hosts_t *hosts)
Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current positi...
Definition: hosts.c:977
is_ipv6_address
static int is_ipv6_address(const char *str)
Checks if a buffer points to a valid IPv6 address. "0:0:0:0:0:0:0:1", "::1" and "::FFFF:192....
Definition: hosts.c:88
HOST_TYPE_NAME
@ HOST_TYPE_NAME
Definition: hosts.h:38
gvm_host_resolve
int gvm_host_resolve(const gvm_host_t *host, void *dst, int family)
Resolves a host object's name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAM...
Definition: hosts.c:1934
gvm_host::vhosts
GSList * vhosts
Definition: hosts.h:72
gvm_hosts
The structure for Hosts collection.
Definition: hosts.h:90