Greenbone Vulnerability Management Libraries  10.0.1
hosts.h File Reference

Protos and data structures for Hosts collections and single hosts objects. More...

#include <glib.h>
#include <netinet/in.h>
Include dependency graph for hosts.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  gvm_host
 The structure for a single host object. More...
 
struct  gvm_vhost
 The structure for a single vhost object. More...
 
struct  gvm_hosts
 The structure for Hosts collection. More...
 

Typedefs

typedef struct gvm_host gvm_host_t
 
typedef struct gvm_vhost gvm_vhost_t
 
typedef struct gvm_hosts gvm_hosts_t
 

Enumerations

enum  host_type {
  HOST_TYPE_NAME = 0, HOST_TYPE_IPV4, HOST_TYPE_CIDR_BLOCK, HOST_TYPE_RANGE_SHORT,
  HOST_TYPE_RANGE_LONG, HOST_TYPE_IPV6, HOST_TYPE_CIDR6_BLOCK, HOST_TYPE_RANGE6_LONG,
  HOST_TYPE_RANGE6_SHORT, HOST_TYPE_MAX
}
 

Functions

gvm_hosts_t * gvm_hosts_new (const gchar *)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str. More...
 
gvm_hosts_t * gvm_hosts_new_with_max (const gchar *, unsigned int)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str. More...
 
gvm_host_t * gvm_hosts_next (gvm_hosts_t *)
 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 *)
 Frees memory occupied by an gvm_hosts_t structure. More...
 
void gvm_hosts_shuffle (gvm_hosts_t *)
 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 *)
 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...
 
void gvm_hosts_resolve (gvm_hosts_t *)
 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_hosts_exclude (gvm_hosts_t *, const char *)
 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_vhosts_exclude (gvm_host_t *, const char *)
 Exclude a list of vhosts from a host's vhosts list. More...
 
int gvm_hosts_exclude_with_max (gvm_hosts_t *, const char *, unsigned int)
 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 *)
 Checks for a host object reverse dns lookup existence. More...
 
int gvm_hosts_reverse_lookup_only (gvm_hosts_t *)
 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 *)
 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 *)
 Gets the count of single hosts objects in a hosts collection. More...
 
unsigned int gvm_hosts_removed (const gvm_hosts_t *)
 Gets the count of single values in hosts string that were removed (duplicates / excluded.) More...
 
int gvm_host_in_hosts (const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *)
 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...
 
gchar * gvm_host_type_str (const gvm_host_t *)
 Gets a host's type in printable format. More...
 
enum host_type gvm_host_type (const gvm_host_t *)
 Gets a host object's type. More...
 
gchar * gvm_host_value_str (const gvm_host_t *)
 Gets a host's value in printable format. More...
 
int gvm_host_resolve (const gvm_host_t *, void *, int)
 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 *, struct in6_addr *)
 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...
 
void gvm_host_add_reverse_lookup (gvm_host_t *)
 Add a host's reverse-lookup name to the vhosts list. More...
 
gvm_vhost_t * gvm_vhost_new (char *, char *)
 Creates a new gvm_vhost_t object. More...
 
int gvm_get_host_type (const gchar *)
 Determines the host type in a buffer. More...
 

Detailed Description

Protos and data structures for Hosts collections and single hosts objects.

This file contains the protos for hosts.c

Definition in file hosts.h.

Typedef Documentation

◆ gvm_host_t

typedef struct gvm_host gvm_host_t

Definition at line 51 of file hosts.h.

◆ gvm_hosts_t

typedef struct gvm_hosts gvm_hosts_t

Definition at line 53 of file hosts.h.

◆ gvm_vhost_t

typedef struct gvm_vhost gvm_vhost_t

Definition at line 52 of file hosts.h.

Enumeration Type Documentation

◆ host_type

enum host_type
Enumerator
HOST_TYPE_NAME 
HOST_TYPE_IPV4 
HOST_TYPE_CIDR_BLOCK 
HOST_TYPE_RANGE_SHORT 
HOST_TYPE_RANGE_LONG 
HOST_TYPE_IPV6 
HOST_TYPE_CIDR6_BLOCK 
HOST_TYPE_RANGE6_LONG 
HOST_TYPE_RANGE6_SHORT 
HOST_TYPE_MAX 

Definition at line 36 of file hosts.h.

37 {
38  HOST_TYPE_NAME = 0, /* Hostname eg. foo */
39  HOST_TYPE_IPV4, /* eg. 192.168.1.1 */
40  HOST_TYPE_CIDR_BLOCK, /* eg. 192.168.15.0/24 */
41  HOST_TYPE_RANGE_SHORT, /* eg. 192.168.15.10-20 */
42  HOST_TYPE_RANGE_LONG, /* eg. 192.168.15.10-192.168.18.3 */
43  HOST_TYPE_IPV6, /* eg. ::1 */
44  HOST_TYPE_CIDR6_BLOCK, /* eg. ::ffee/120 */
45  HOST_TYPE_RANGE6_LONG, /* eg. ::1:200:7-::1:205:500 */
46  HOST_TYPE_RANGE6_SHORT, /* eg. ::1-fe10 */
47  HOST_TYPE_MAX /* Boundary checking. */
48 };

Function Documentation

◆ 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 768 of file hosts.c.

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

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 1629 of file hosts.c.

1630 {
1631  GSList *vhosts;
1632  gvm_vhost_t *vhost;
1633  char *value;
1634 
1635  if (!host || host->type == HOST_TYPE_NAME)
1636  return;
1637 
1638  value = gvm_host_reverse_lookup (host);
1639  if (!value)
1640  return;
1641  if (host_name_verify (host, value))
1642  {
1643  g_free (value);
1644  return;
1645  }
1646  /* Don't add vhost, if already in the list. */
1647  vhosts = host->vhosts;
1648  while (vhosts)
1649  {
1650  if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1651  {
1652  g_free (value);
1653  return;
1654  }
1655  vhosts = vhosts->next;
1656  }
1657  vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1658  host->vhosts = g_slist_prepend (host->vhosts, vhost);
1659 }
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:1597
GSList * vhosts
Definition: hosts.h:72
The structure for a single vhost object.
Definition: hosts.h:78
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition: hosts.c:829
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition: hosts.c:1542
enum host_type type
Definition: hosts.h:71

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_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 1951 of file hosts.c.

1952 {
1953  if (host == NULL || ip6 == NULL)
1954  return -1;
1955 
1956  switch (gvm_host_type (host))
1957  {
1958  case HOST_TYPE_IPV6:
1959  memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
1960  return 0;
1961 
1962  case HOST_TYPE_IPV4:
1963  ipv4_as_ipv6 (&host->addr, ip6);
1964  return 0;
1965 
1966  case HOST_TYPE_NAME:
1967  {
1968  struct in_addr ip4;
1969 
1970  /* Fail if IPv4 and IPv6 both don't resolve. */
1971  if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
1972  ipv4_as_ipv6 (&ip4, ip6);
1973  else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
1974  return -1;
1975  return 0;
1976  }
1977 
1978  default:
1979  return -1;
1980  }
1981 }
struct in_addr addr
Definition: hosts.h:68
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:1930
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition: hosts.c:1842
struct in6_addr addr6
Definition: hosts.h:69
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

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 1792 of file hosts.c.

1794 {
1795  char *host_str;
1796  size_t i;
1797 
1798  if (host == NULL || hosts == NULL)
1799  return 0;
1800 
1801  host_str = gvm_host_value_str (host);
1802 
1803  for (i = 0; i < hosts->count; i++)
1804  {
1805  gvm_host_t *current_host = hosts->hosts[i];
1806  char *tmp = gvm_host_value_str (current_host);
1807 
1808  if (strcasecmp (host_str, tmp) == 0)
1809  {
1810  g_free (host_str);
1811  g_free (tmp);
1812  return 1;
1813  }
1814  g_free (tmp);
1815 
1816  /* Hostnames in hosts list shouldn't be resolved. */
1817  if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
1818  {
1819  struct in6_addr tmpaddr;
1820  gvm_host_get_addr6 (current_host, &tmpaddr);
1821 
1822  if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
1823  {
1824  g_free (host_str);
1825  return 1;
1826  }
1827  }
1828  }
1829 
1830  g_free (host_str);
1831  return 0;
1832 }
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:1951
The structure for a single host object.
Definition: hosts.h:63
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition: hosts.c:1842
size_t count
Definition: hosts.h:96
gvm_host_t ** hosts
Definition: hosts.h:93
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:1873

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_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 1930 of file hosts.c.

1931 {
1932  if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
1933  return -1;
1934 
1935  return gvm_resolve (host->name, dst, family);
1936 }
int gvm_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
Definition: networking.c:388
gchar * name
Definition: hosts.h:67
enum host_type type
Definition: hosts.h:71

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 1542 of file hosts.c.

1543 {
1544  if (host == NULL)
1545  return NULL;
1546 
1547  if (host->type == HOST_TYPE_NAME)
1548  return NULL;
1549  else if (host->type == HOST_TYPE_IPV4)
1550  {
1551  struct sockaddr_in sa;
1552  int retry = 2;
1553  gchar hostname[1000];
1554 
1555  bzero (&sa, sizeof (struct sockaddr));
1556  sa.sin_addr = host->addr;
1557  sa.sin_family = AF_INET;
1558  while (retry--)
1559  {
1560  int ret = getnameinfo ((struct sockaddr *) &sa, sizeof (sa), hostname,
1561  sizeof (hostname), NULL, 0, NI_NAMEREQD);
1562  if (!ret)
1563  return g_ascii_strdown (hostname, -1);
1564  if (ret != EAI_AGAIN)
1565  break;
1566  }
1567  return NULL;
1568  }
1569  else if (host->type == HOST_TYPE_IPV6)
1570  {
1571  struct sockaddr_in6 sa;
1572  char hostname[1000];
1573 
1574  bzero (&sa, sizeof (struct sockaddr));
1575  memcpy (&sa.sin6_addr, &host->addr6, 16);
1576  sa.sin6_family = AF_INET6;
1577 
1578  if (getnameinfo ((struct sockaddr *) &sa, sizeof (sa), hostname,
1579  sizeof (hostname), NULL, 0, NI_NAMEREQD))
1580  return NULL;
1581  else
1582  return g_ascii_strdown (hostname, -1);
1583  }
1584  else
1585  return NULL;
1586 }
struct in_addr addr
Definition: hosts.h:68
struct in6_addr addr6
Definition: hosts.h:69
enum host_type type
Definition: hosts.h:71

References gvm_host::addr, gvm_host::addr6, HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, 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 1842 of file hosts.c.

1843 {
1844  assert (host);
1845  return host->type;
1846 }
enum host_type type
Definition: hosts.h:71

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 1857 of file hosts.c.

1858 {
1859  if (host == NULL)
1860  return NULL;
1861 
1862  return host_type_str[host->type];
1863 }
gchar * host_type_str[HOST_TYPE_MAX]
Definition: hosts.c:53
enum host_type type
Definition: hosts.h:71

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 1873 of file hosts.c.

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

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_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 1760 of file hosts.c.

1761 {
1762  return hosts ? hosts->count : 0;
1763 }
size_t count
Definition: hosts.h:96

References gvm_hosts::count.

Referenced by gvm_hosts_exclude_with_max().

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 1529 of file hosts.c.

1530 {
1531  return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1532 }
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:1455

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 1455 of file hosts.c.

1457 {
1461  gvm_hosts_t *excluded_hosts;
1462  GHashTable *name_table;
1463  size_t excluded = 0, i;
1464 
1465  if (hosts == NULL || excluded_str == NULL)
1466  return -1;
1467 
1468  excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1469  if (excluded_hosts == NULL)
1470  return -1;
1471 
1472  if (gvm_hosts_count (excluded_hosts) == 0)
1473  {
1474  gvm_hosts_free (excluded_hosts);
1475  return 0;
1476  }
1477 
1478  /* Hash host values from excluded hosts list. */
1479  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1480  for (i = 0; i < excluded_hosts->count; i++)
1481  {
1482  gchar *name;
1483 
1484  if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
1485  g_hash_table_insert (name_table, name, hosts);
1486  }
1487 
1488  /* Check for hosts values in hash table. */
1489  for (i = 0; i < hosts->count; i++)
1490  {
1491  gchar *name;
1492 
1493  if ((name = gvm_host_value_str (hosts->hosts[i])))
1494  {
1495  if (g_hash_table_lookup (name_table, name))
1496  {
1497  gvm_host_free (hosts->hosts[i]);
1498  hosts->hosts[i] = NULL;
1499  excluded++;
1500  g_free (name);
1501  continue;
1502  }
1503  g_free (name);
1504  }
1505  }
1506 
1507  /* Cleanup. */
1508  if (excluded)
1509  gvm_hosts_fill_gaps (hosts);
1510  hosts->count -= excluded;
1511  hosts->removed += excluded;
1512  hosts->current = 0;
1513  g_hash_table_destroy (name_table);
1514  gvm_hosts_free (excluded_hosts);
1515  return excluded;
1516 }
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition: hosts.c:1760
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:936
size_t removed
Definition: hosts.h:97
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:1031
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition: hosts.c:1255
gvm_host_t ** hosts
Definition: hosts.h:93
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:1873
The structure for Hosts collection.
Definition: hosts.h:90
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:877

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_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 1255 of file hosts.c.

1256 {
1257  size_t i;
1258 
1259  if (hosts == NULL)
1260  return;
1261 
1262  if (hosts->orig_str)
1263  g_free (hosts->orig_str);
1264  for (i = 0; i < hosts->count; i++)
1265  gvm_host_free (hosts->hosts[i]);
1266  g_free (hosts->hosts);
1267  g_free (hosts);
1268 }
size_t count
Definition: hosts.h:96
gvm_host_t ** hosts
Definition: hosts.h:93
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:877
gchar * orig_str
Definition: hosts.h:92

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_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 1226 of file hosts.c.

1227 {
1228  return gvm_hosts_new_with_max (hosts_str, 0);
1229 }
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:1031

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 1031 of file hosts.c.

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

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 1240 of file hosts.c.

1241 {
1242  if (!hosts || hosts->current == hosts->count)
1243  return NULL;
1244 
1245  return hosts->hosts[hosts->current++];
1246 }
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
gvm_host_t ** hosts
Definition: hosts.h:93

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 1774 of file hosts.c.

1775 {
1776  return hosts ? hosts->removed : 0;
1777 }
size_t removed
Definition: hosts.h:97

References gvm_hosts::removed.

◆ gvm_hosts_resolve()

void 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.

Definition at line 1334 of file hosts.c.

1335 {
1336  size_t i, new_entries = 0 , resolved = 0;
1337 
1338  for (i = 0; i < hosts->count; i++)
1339  {
1340  GSList *list, *tmp;
1341  gvm_host_t *host = hosts->hosts[i];
1342 
1343  if (host->type != HOST_TYPE_NAME)
1344  continue;
1345 
1346  list = tmp = gvm_resolve_list (host->name);
1347  while (tmp)
1348  {
1349  /* Create a new host for each IP address. */
1350  gvm_host_t *new;
1351  struct in6_addr *ip6 = tmp->data;
1352  gvm_vhost_t *vhost;
1353 
1354  new = gvm_host_new ();
1355  if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1356  || ip6->s6_addr32[2] != htonl (0xffff))
1357  {
1358  new->type = HOST_TYPE_IPV6;
1359  memcpy (&new->addr6, ip6, sizeof (new->addr6));
1360  }
1361  else
1362  {
1363  new->type = HOST_TYPE_IPV4;
1364  memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1365  }
1366  vhost =
1367  gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1368  new->vhosts = g_slist_prepend (new->vhosts, vhost);
1369  gvm_hosts_add (hosts, new);
1370  tmp = tmp->next;
1371  new_entries = 1;
1372  }
1373  /* Remove hostname from list, as it was either replaced by IPs, or
1374  * is unresolvable. */
1375  hosts->hosts[i] = NULL;
1376  resolved++;
1377  if (!list)
1378  g_warning ("Couldn't resolve hostname %s", host->name);
1379  gvm_host_free (host);
1380  g_slist_free_full (list, g_free);
1381  }
1382  if (resolved)
1383  gvm_hosts_fill_gaps (hosts);
1384  hosts->count -= resolved;
1385  hosts->removed += resolved;
1386  if (new_entries)
1387  gvm_hosts_deduplicate (hosts);
1388  hosts->current = 0;
1389 }
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:936
size_t removed
Definition: hosts.h:97
The structure for a single host object.
Definition: hosts.h:63
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:974
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
The structure for a single vhost object.
Definition: hosts.h:78
gvm_host_t ** hosts
Definition: hosts.h:93
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition: hosts.c:829
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition: networking.c:338
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:877
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition: hosts.c:862
gchar * name
Definition: hosts.h:67
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:898
enum host_type type
Definition: hosts.h:71

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 1310 of file hosts.c.

1311 {
1312  size_t i, j;
1313  if (hosts == NULL)
1314  return;
1315 
1316  for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1317  {
1318  gvm_host_t *tmp = hosts->hosts[i];
1319  hosts->hosts[i] = hosts->hosts[j];
1320  hosts->hosts[j] = tmp;
1321  }
1322  hosts->current = 0;
1323 }
The structure for a single host object.
Definition: hosts.h:63
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
gvm_host_t ** hosts
Definition: hosts.h:93

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 1671 of file hosts.c.

1672 {
1673  size_t i, count = 0;
1674 
1675  if (hosts == NULL)
1676  return -1;
1677 
1678  for (i = 0; i < hosts->count; i++)
1679  {
1680  gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1681 
1682  if (name == NULL)
1683  {
1684  gvm_host_free (hosts->hosts[i]);
1685  hosts->hosts[i] = NULL;
1686  count++;
1687  }
1688  else
1689  g_free (name);
1690  }
1691 
1692  if (count)
1693  gvm_hosts_fill_gaps (hosts);
1694  hosts->count -= count;
1695  hosts->removed += count;
1696  hosts->current = 0;
1697  return count;
1698 }
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:936
size_t removed
Definition: hosts.h:97
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
gvm_host_t ** hosts
Definition: hosts.h:93
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition: hosts.c:1542
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:877

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 1710 of file hosts.c.

1711 {
1715  size_t i, count = 0;
1716  GHashTable *name_table;
1717 
1718  if (hosts == NULL)
1719  return -1;
1720 
1721  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1722  for (i = 0; i < hosts->count; i++)
1723  {
1724  gchar *name;
1725 
1726  if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
1727  {
1728  if (g_hash_table_lookup (name_table, name))
1729  {
1730  gvm_host_free (hosts->hosts[i]);
1731  hosts->hosts[i] = NULL;
1732  count++;
1733  g_free (name);
1734  }
1735  else
1736  {
1737  /* Insert in the hash table. Value not important. */
1738  g_hash_table_insert (name_table, name, hosts);
1739  }
1740  }
1741  }
1742 
1743  if (count)
1744  gvm_hosts_fill_gaps (hosts);
1745  g_hash_table_destroy (name_table);
1746  hosts->removed += count;
1747  hosts->count -= count;
1748  hosts->current = 0;
1749  return count;
1750 }
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:936
size_t removed
Definition: hosts.h:97
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
gvm_host_t ** hosts
Definition: hosts.h:93
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition: hosts.c:1542
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:877

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 1278 of file hosts.c.

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

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

◆ 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 829 of file hosts.c.

830 {
831  gvm_vhost_t *vhost;
832 
833  vhost = g_malloc0 (sizeof (gvm_vhost_t));
834  vhost->value = value;
835  vhost->source = source;
836 
837  return vhost;
838 }
The structure for a single vhost object.
Definition: hosts.h:78
char * source
Definition: hosts.h:81
char * value
Definition: hosts.h:80

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 1400 of file hosts.c.

1401 {
1402  GSList *vhost;
1403  char **excluded;
1404  int ret = 0;
1405 
1406  if (!host || !excluded_str)
1407  return ret;
1408 
1409  vhost = host->vhosts;
1410  excluded = g_strsplit (excluded_str, ",", 0);
1411  if (!excluded || !*excluded)
1412  {
1413  g_strfreev (excluded);
1414  return ret;
1415  }
1416  while (vhost)
1417  {
1418  char **tmp = excluded;
1419  char *value = ((gvm_vhost_t *) vhost->data)->value;
1420 
1421  while (*tmp)
1422  {
1423  if (!strcasecmp (value, g_strstrip (*tmp)))
1424  {
1425  gvm_vhost_free (vhost->data);
1426  host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1427  ret++;
1428  break;
1429  }
1430  tmp++;
1431  if (!*tmp)
1432  {
1433  vhost = vhost->next;
1434  break;
1435  }
1436  }
1437  }
1438  g_strfreev (excluded);
1439 
1440  return ret;
1441 }
GSList * vhosts
Definition: hosts.h:72
The structure for a single vhost object.
Definition: hosts.h:78
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition: hosts.c:846

References gvm_vhost_free(), and gvm_host::vhosts.

Here is the call graph for this function: