OpenVAS Libraries  9.0.3
openvas_hosts.h File Reference

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

#include "openvas_networking.h"
#include <glib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>
Include dependency graph for openvas_hosts.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  openvas_host
 The structure for a single host object. More...
 
struct  openvas_hosts
 The structure for Hosts collection. More...
 

Typedefs

typedef struct openvas_host openvas_host_t
 
typedef struct openvas_hosts openvas_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

openvas_hosts_topenvas_hosts_new (const gchar *)
 Creates a new openvas_hosts_t structure and the associated hosts objects from the provided hosts_str. More...
 
openvas_hosts_topenvas_hosts_new_with_max (const gchar *, unsigned int)
 Creates a new openvas_hosts_t structure and the associated hosts objects from the provided hosts_str. More...
 
openvas_host_topenvas_hosts_next (openvas_hosts_t *)
 Gets the next openvas_host_t from a openvas_hosts_t structure. The state of iteration is kept internally within the openvas_hosts structure. More...
 
void openvas_hosts_free (openvas_hosts_t *)
 Frees memory occupied by an openvas_hosts_t structure. More...
 
void openvas_hosts_shuffle (openvas_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 openvas_hosts_reverse (openvas_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 openvas_hosts_resolve (openvas_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 openvas_hosts_exclude (openvas_hosts_t *, const gchar *, int)
 
char * openvas_host_reverse_lookup (openvas_host_t *)
 Checks for a host object reverse dns lookup existence. More...
 
int openvas_hosts_reverse_lookup_only (openvas_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 openvas_hosts_reverse_lookup_unify (openvas_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 openvas_hosts_count (const openvas_hosts_t *)
 Gets the count of single hosts objects in a hosts collection. More...
 
unsigned int openvas_hosts_removed (const openvas_hosts_t *)
 Gets the count of single values in hosts string that were removed (duplicates / excluded.) More...
 
int openvas_host_in_hosts (const openvas_host_t *, const struct in6_addr *, const openvas_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 * openvas_host_type_str (const openvas_host_t *)
 Gets a host's type in printable format. More...
 
enum host_type openvas_host_type (const openvas_host_t *)
 Gets a host object's type. More...
 
gchar * openvas_host_value_str (const openvas_host_t *)
 Gets a host's value in printable format. More...
 
int openvas_host_resolve (const openvas_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 openvas_host_get_addr6 (const openvas_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...
 
int openvas_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 openvas_hosts.h.

Typedef Documentation

◆ openvas_host_t

typedef struct openvas_host openvas_host_t

Definition at line 65 of file openvas_hosts.h.

◆ openvas_hosts_t

Definition at line 66 of file openvas_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 51 of file openvas_hosts.h.

51  {
52  HOST_TYPE_NAME = 0, /* Hostname eg. foo */
53  HOST_TYPE_IPV4, /* eg. 192.168.1.1 */
54  HOST_TYPE_CIDR_BLOCK, /* eg. 192.168.15.0/24 */
55  HOST_TYPE_RANGE_SHORT, /* eg. 192.168.15.10-20 */
56  HOST_TYPE_RANGE_LONG, /* eg. 192.168.15.10-192.168.18.3 */
57  HOST_TYPE_IPV6, /* eg. ::1 */
58  HOST_TYPE_CIDR6_BLOCK, /* eg. ::ffee/120 */
59  HOST_TYPE_RANGE6_LONG, /* eg. ::1:200:7-::1:205:500 */
60  HOST_TYPE_RANGE6_SHORT, /* eg. ::1-fe10 */
61  HOST_TYPE_MAX /* Boundary checking. */
62 };

Function Documentation

◆ openvas_get_host_type()

int openvas_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 758 of file openvas_hosts.c.

759 {
760  /*
761  * We have a single element with no leading or trailing
762  * white spaces. This element could represent different host
763  * definitions: single IPs, host names, CIDR-expressed blocks,
764  * range-expressed networks, IPv6 addresses.
765  */
766 
767  /* Null or empty string. */
768  if (str_stripped == NULL || *str_stripped == '\0')
769  return -1;
770 
771  /* Check for regular single IPv4 address. */
772  if (is_ipv4_address (str_stripped))
773  return HOST_TYPE_IPV4;
774 
775  /* Check for regular single IPv6 address. */
776  if (is_ipv6_address (str_stripped))
777  return HOST_TYPE_IPV6;
778 
779  /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
780  if (is_cidr_block (str_stripped))
781  return HOST_TYPE_CIDR_BLOCK;
782 
783  /* Check for short range-expressed networks "192.168.12.5-40" */
784  if (is_short_range_network (str_stripped))
785  return HOST_TYPE_RANGE_SHORT;
786 
787  /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
788  if (is_long_range_network (str_stripped))
789  return HOST_TYPE_RANGE_LONG;
790 
791  /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
792  if (is_cidr6_block (str_stripped))
793  return HOST_TYPE_CIDR6_BLOCK;
794 
795  /* Check for short range-expressed networks "::1-ef12" */
796  if (is_short_range6_network (str_stripped))
797  return HOST_TYPE_RANGE6_SHORT;
798 
799  /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
800  if (is_long_range6_network (str_stripped))
801  return HOST_TYPE_RANGE6_LONG;
802 
803  /* Check for hostname. */
804  if (is_hostname (str_stripped))
805  return HOST_TYPE_NAME;
806 
807  /* @todo: If everything else fails, fallback to hostname ? */
808  return -1;
809 }

Referenced by openvas_hosts_new_with_max().

Here is the caller graph for this function:

◆ openvas_host_get_addr6()

int openvas_host_get_addr6 ( const openvas_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 1723 of file openvas_hosts.c.

1724 {
1725  if (host == NULL || ip6 == NULL)
1726  return -1;
1727 
1728  switch (openvas_host_type (host))
1729  {
1730  case HOST_TYPE_IPV6:
1731  memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
1732  return 0;
1733 
1734  case HOST_TYPE_IPV4:
1735  ipv4_as_ipv6 (&host->addr, ip6);
1736  return 0;
1737 
1738  case HOST_TYPE_NAME:
1739  {
1740  struct in_addr ip4;
1741 
1742  /* Fail if IPv4 and IPv6 both don't resolve. */
1743  if (openvas_host_resolve (host, &ip4, AF_INET) == 0)
1744  ipv4_as_ipv6 (&ip4, ip6);
1745  else if (openvas_host_resolve (host, ip6, AF_INET6) == -1)
1746  return -1;
1747  return 0;
1748  }
1749 
1750  default:
1751  return -1;
1752  }
1753 }
int openvas_host_resolve(const openvas_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...
struct in_addr addr
Definition: openvas_hosts.h:80
struct in6_addr addr6
Definition: openvas_hosts.h:81
enum host_type openvas_host_type(const openvas_host_t *host)
Gets a host object's type.
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....

References openvas_host::addr, openvas_host::addr6, HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, ipv4_as_ipv6(), openvas_host_resolve(), and openvas_host_type().

Referenced by openvas_host_in_hosts().

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

◆ openvas_host_in_hosts()

int openvas_host_in_hosts ( const openvas_host_t host,
const struct in6_addr *  addr,
const openvas_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 1562 of file openvas_hosts.c.

1564 {
1565  char *host_str;
1566  GList *element;
1567 
1568  if (host == NULL || hosts == NULL)
1569  return 0;
1570 
1571  host_str = openvas_host_value_str (host);
1572 
1573  element = hosts->hosts;
1574  while (element)
1575  {
1576  char *tmp = openvas_host_value_str (element->data);
1577 
1578  if (strcasecmp (host_str, tmp) == 0)
1579  {
1580  g_free (host_str);
1581  g_free (tmp);
1582  return 1;
1583  }
1584  g_free (tmp);
1585 
1586  /* Hostnames in hosts list shouldn't be resolved. */
1587  if (addr && openvas_host_type (element->data) != HOST_TYPE_NAME)
1588  {
1589  struct in6_addr tmpaddr;
1590  openvas_host_get_addr6 (element->data, &tmpaddr);
1591 
1592  if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
1593  {
1594  g_free (host_str);
1595  return 1;
1596  }
1597 
1598  }
1599  element = element->next;
1600  }
1601 
1602  g_free (host_str);
1603  return 0;
1604 }
gchar * openvas_host_value_str(const openvas_host_t *host)
Gets a host's value in printable format.
enum host_type openvas_host_type(const openvas_host_t *host)
Gets a host object's type.
int openvas_host_get_addr6(const openvas_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...

References HOST_TYPE_NAME, openvas_hosts::hosts, openvas_host_get_addr6(), openvas_host_type(), and openvas_host_value_str().

Here is the call graph for this function:

◆ openvas_host_resolve()

int openvas_host_resolve ( const openvas_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 1702 of file openvas_hosts.c.

1703 {
1704  if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
1705  return -1;
1706 
1707  return openvas_resolve (host->name, dst, family);
1708 }
int openvas_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
enum host_type type
Definition: openvas_hosts.h:83
gchar * name
Definition: openvas_hosts.h:79

References HOST_TYPE_NAME, openvas_host::name, openvas_resolve(), and openvas_host::type.

Referenced by main(), openvas_host_get_addr6(), and openvas_hosts_resolve().

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

◆ openvas_host_reverse_lookup()

char* openvas_host_reverse_lookup ( openvas_host_t host)

Checks for a host object reverse dns lookup existence.

Parameters
[in]hostThe host to reverse-lookup.
Returns
Result of look-up or name if host of type name already, NULL otherwise. Free with g_free().

Definition at line 1378 of file openvas_hosts.c.

1379 {
1380 
1381  if (host == NULL)
1382  return NULL;
1383 
1384  if (host->type == HOST_TYPE_NAME)
1385  return g_strdup (host->name);
1386  else if (host->type == HOST_TYPE_IPV4)
1387  {
1388  struct sockaddr_in sa;
1389  int retry = 2;
1390  gchar hostname[1000];
1391 
1392  bzero (&sa, sizeof (struct sockaddr));
1393  sa.sin_addr = host->addr;
1394  sa.sin_family = AF_INET;
1395  while (retry--)
1396  {
1397  int ret = getnameinfo ((struct sockaddr *) &sa, sizeof (sa), hostname,
1398  sizeof (hostname), NULL, 0, NI_NAMEREQD);
1399  if (!ret)
1400  return g_strdup (hostname);
1401  if (ret != EAI_AGAIN)
1402  break;
1403  }
1404  return NULL;
1405  }
1406  else if (host->type == HOST_TYPE_IPV6)
1407  {
1408  struct sockaddr_in6 sa;
1409  char hostname[1000];
1410 
1411  bzero (&sa, sizeof (struct sockaddr));
1412  memcpy (&sa.sin6_addr, &host->addr6, 16);
1413  sa.sin6_family = AF_INET6;
1414 
1415  if (getnameinfo ((struct sockaddr *) &sa, sizeof (sa), hostname,
1416  sizeof (hostname), NULL, 0, NI_NAMEREQD))
1417  return NULL;
1418  else
1419  return g_strdup (hostname);
1420  }
1421  else
1422  return NULL;
1423 }
struct in_addr addr
Definition: openvas_hosts.h:80
struct in6_addr addr6
Definition: openvas_hosts.h:81
enum host_type type
Definition: openvas_hosts.h:83
gchar * name
Definition: openvas_hosts.h:79

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

Referenced by openvas_hosts_reverse_lookup_only(), and openvas_hosts_reverse_lookup_unify().

Here is the caller graph for this function:

◆ openvas_host_type()

enum host_type openvas_host_type ( const openvas_host_t host)

Gets a host object's type.

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

Definition at line 1614 of file openvas_hosts.c.

1615 {
1616  assert (host);
1617  return host->type;
1618 }
enum host_type type
Definition: openvas_hosts.h:83

References openvas_host::type.

Referenced by main(), openvas_host_get_addr6(), and openvas_host_in_hosts().

Here is the caller graph for this function:

◆ openvas_host_type_str()

gchar* openvas_host_type_str ( const openvas_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 1629 of file openvas_hosts.c.

1630 {
1631  if (host == NULL)
1632  return NULL;
1633 
1634  return host_type_str[host->type];
1635 }
gchar * host_type_str[HOST_TYPE_MAX]
Definition: openvas_hosts.c:41
enum host_type type
Definition: openvas_hosts.h:83

References host_type_str, and openvas_host::type.

Referenced by main().

Here is the caller graph for this function:

◆ openvas_host_value_str()

gchar* openvas_host_value_str ( const openvas_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 1645 of file openvas_hosts.c.

1646 {
1647  if (host == NULL)
1648  return NULL;
1649 
1650  switch (host->type)
1651  {
1652  case HOST_TYPE_NAME:
1653  return g_strdup (host->name);
1654  break;
1655  case HOST_TYPE_IPV4:
1656  case HOST_TYPE_IPV6:
1657  /* Handle both cases using inet_ntop(). */
1658  {
1659  int family, size;
1660  gchar *str;
1661  const void *srcaddr;
1662 
1663  if (host->type == HOST_TYPE_IPV4)
1664  {
1665  family = AF_INET;
1666  size = INET_ADDRSTRLEN;
1667  srcaddr = &host->addr;
1668  }
1669  else
1670  {
1671  family = AF_INET6;
1672  size = INET6_ADDRSTRLEN;
1673  srcaddr = &host->addr6;
1674  }
1675 
1676  str = g_malloc0 (size);
1677  if (inet_ntop (family, srcaddr, str, size) == NULL)
1678  {
1679  perror ("inet_ntop");
1680  g_free (str);
1681  return NULL;
1682  }
1683  return str;
1684  }
1685  default:
1686  return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
1687  }
1688 }
struct in_addr addr
Definition: openvas_hosts.h:80
struct in6_addr addr6
Definition: openvas_hosts.h:81
enum host_type type
Definition: openvas_hosts.h:83
gchar * name
Definition: openvas_hosts.h:79

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

Referenced by main(), openvas_host_in_hosts(), and openvas_hosts_exclude().

Here is the caller graph for this function:

◆ openvas_hosts_count()

unsigned int openvas_hosts_count ( const openvas_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 1530 of file openvas_hosts.c.

1531 {
1532  return hosts ? hosts->count : 0;
1533 }
unsigned int count
Definition: openvas_hosts.h:97

References openvas_hosts::count.

Referenced by main(), openvas_hosts_exclude(), and openvas_hosts_shuffle().

Here is the caller graph for this function:

◆ openvas_hosts_exclude()

int openvas_hosts_exclude ( openvas_hosts_t ,
const gchar *  ,
int   
)

◆ openvas_hosts_free()

void openvas_hosts_free ( openvas_hosts_t hosts)

Frees memory occupied by an openvas_hosts_t structure.

Parameters
[in]hostsThe hosts collection to free.

Definition at line 1145 of file openvas_hosts.c.

1146 {
1147  if (hosts == NULL)
1148  return;
1149 
1150  if (hosts->orig_str)
1151  g_free (hosts->orig_str);
1152 
1153  g_list_free_full (hosts->hosts, openvas_host_free);
1154 
1155  g_free (hosts);
1156 }
gchar * orig_str
Definition: openvas_hosts.h:94

References openvas_hosts::hosts, and openvas_hosts::orig_str.

Referenced by main(), and openvas_hosts_exclude().

Here is the caller graph for this function:

◆ openvas_hosts_new()

openvas_hosts_t* openvas_hosts_new ( const gchar *  hosts_str)

Creates a new openvas_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 openvas_hosts_free.

Definition at line 1111 of file openvas_hosts.c.

1112 {
1113  return openvas_hosts_new_with_max (hosts_str, 0);
1114 }
openvas_hosts_t * openvas_hosts_new_with_max(const gchar *hosts_str, unsigned int max_hosts)
Creates a new openvas_hosts_t structure and the associated hosts objects from the provided hosts_str.

References openvas_hosts_new_with_max().

Referenced by main(), and openvas_hosts_exclude().

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

◆ openvas_hosts_new_with_max()

openvas_hosts_t* openvas_hosts_new_with_max ( const gchar *  hosts_str,
unsigned int  max_hosts 
)

Creates a new openvas_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 openvas_hosts_free.

Definition at line 912 of file openvas_hosts.c.

913 {
914  openvas_hosts_t *hosts;
915  gchar **host_element, **split;
916  gchar *str;
917 
918  if (hosts_str == NULL)
919  return NULL;
920 
921  hosts = g_malloc0 (sizeof (openvas_hosts_t));
922  if (hosts == NULL)
923  return NULL;
924 
925  hosts->orig_str = g_strdup (hosts_str);
926  /* Normalize separator: Transform newlines into commas. */
927  str = hosts->orig_str;
928  while (*str)
929  {
930  if (*str == '\n') *str = ',';
931  str++;
932  }
933 
934  /* Split comma-separeted list into single host-specifications */
935  split = g_strsplit (hosts->orig_str, ",", 0);
936 
937  /* first element of the splitted list */
938  host_element = split;
939  while (*host_element)
940  {
941  int host_type;
942  gchar *stripped = g_strstrip (*host_element);
943 
944  if (stripped == NULL || *stripped == '\0')
945  {
946  host_element++;
947  continue;
948  }
949 
950  /* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ? */
951  /* -1 if error. */
952  host_type = openvas_get_host_type (stripped);
953 
954  switch (host_type)
955  {
956  case HOST_TYPE_NAME:
957  case HOST_TYPE_IPV4:
958  case HOST_TYPE_IPV6:
959  {
960  /* New host. */
961  openvas_host_t *host = openvas_host_new ();
962  host->type = host_type;
963  if (host_type == HOST_TYPE_NAME)
964  host->name = g_strdup (stripped);
965  else if (host_type == HOST_TYPE_IPV4)
966  inet_pton (AF_INET, stripped, &host->addr);
967  else if (host_type == HOST_TYPE_IPV6)
968  inet_pton (AF_INET6, stripped, &host->addr6);
969  /* Prepend to list of hosts. */
970  hosts->hosts = g_list_prepend (hosts->hosts, host);
971  hosts->count++;
972  break;
973  }
977  {
978  struct in_addr first, last;
979  uint32_t current;
980  int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
981 
983  ips_func = cidr_block_ips;
984  else if (host_type == HOST_TYPE_RANGE_SHORT)
985  ips_func = short_range_network_ips;
986  else if (host_type == HOST_TYPE_RANGE_LONG)
987  ips_func = long_range_network_ips;
988  else
989  break;
990 
991  if (ips_func (stripped, &first, &last) == -1)
992  break;
993 
994  /* Make sure that first actually comes before last */
995  if (ntohl (first.s_addr) > ntohl (last.s_addr))
996  break;
997 
998  /* Add addresses from first to last as single hosts. */
999  current = first.s_addr;
1000  while (ntohl (current) <= ntohl (last.s_addr))
1001  {
1002  openvas_host_t *host = openvas_host_new ();
1003  host->type = HOST_TYPE_IPV4;
1004  host->addr.s_addr = current;
1005  hosts->hosts = g_list_prepend (hosts->hosts, host);
1006  hosts->count++;
1007  if (max_hosts > 0 && hosts->count > max_hosts)
1008  {
1009  g_strfreev (split);
1010  openvas_hosts_free (hosts);
1011  return NULL;
1012  }
1013  /* Next IP address. */
1014  current = htonl (ntohl (current) + 1);
1015  }
1016  break;
1017  }
1018  case HOST_TYPE_CIDR6_BLOCK:
1019  case HOST_TYPE_RANGE6_LONG:
1021  {
1022  struct in6_addr first, last;
1023  unsigned char current[16];
1024  int (*ips_func) (const char *, struct in6_addr *, struct in6_addr *);
1025 
1027  ips_func = cidr6_block_ips;
1028  else if (host_type == HOST_TYPE_RANGE6_SHORT)
1029  ips_func = short_range6_network_ips;
1030  else if (host_type == HOST_TYPE_RANGE6_LONG)
1031  ips_func = long_range6_network_ips;
1032  else
1033  continue;
1034 
1035  if (ips_func (stripped, &first, &last) == -1)
1036  break;
1037 
1038  /* Make sure the first comes before the last. */
1039  if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1040  break;
1041 
1042  /* Add addresses from first to last as single hosts. */
1043  memcpy (current, &first.s6_addr, 16);
1044  while (memcmp (current, &last.s6_addr, 16) <= 0)
1045  {
1046  int i;
1047 
1048  openvas_host_t *host = openvas_host_new ();
1049  host->type = HOST_TYPE_IPV6;
1050  memcpy (host->addr6.s6_addr, current, 16);
1051  hosts->hosts = g_list_prepend (hosts->hosts, host);
1052  hosts->count++;
1053  if (max_hosts > 0 && hosts->count > max_hosts)
1054  {
1055  g_strfreev (split);
1056  openvas_hosts_free (hosts);
1057  return NULL;
1058  }
1059  /* Next IPv6 address. */
1060  for (i = 15; i >= 0; --i)
1061  if (current[i] < 255)
1062  {
1063  current[i]++;
1064  break;
1065  }
1066  else
1067  current[i] = 0;
1068  }
1069  break;
1070  }
1071  case -1:
1072  default:
1073  /* Invalid host string. */
1074  g_strfreev (split);
1075  openvas_hosts_free (hosts);
1076  return NULL;
1077  }
1078  host_element++; /* move on to next element of splitted list */
1079  if (max_hosts > 0 && hosts->count > max_hosts)
1080  {
1081  g_strfreev (split);
1082  openvas_hosts_free (hosts);
1083  return NULL;
1084  }
1085  }
1086 
1087  /* Reverse list, as we were prepending (for performance) to the list. */
1088  hosts->hosts = g_list_reverse (hosts->hosts);
1089 
1090  /* Remove duplicated values. */
1091  openvas_hosts_deduplicate (hosts);
1092 
1093  /* Set current to start of hosts list. */
1094  hosts->current = hosts->hosts;
1095 
1096  g_strfreev (split);
1097  return hosts;
1098 }
The structure for a single host object.
Definition: openvas_hosts.h:76
void openvas_hosts_free(openvas_hosts_t *hosts)
Frees memory occupied by an openvas_hosts_t structure.
GList * current
Definition: openvas_hosts.h:96
gchar * orig_str
Definition: openvas_hosts.h:94
struct in_addr addr
Definition: openvas_hosts.h:80
struct in6_addr addr6
Definition: openvas_hosts.h:81
host_type
Definition: openvas_hosts.h:51
The structure for Hosts collection.
Definition: openvas_hosts.h:92
enum host_type type
Definition: openvas_hosts.h:83
gchar * name
Definition: openvas_hosts.h:79
unsigned int count
Definition: openvas_hosts.h:97
int openvas_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.

References HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, openvas_get_host_type(), and openvas_hosts::orig_str.

Referenced by openvas_hosts_new().

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

◆ openvas_hosts_next()

openvas_host_t* openvas_hosts_next ( openvas_hosts_t hosts)

Gets the next openvas_host_t from a openvas_hosts_t structure. The state of iteration is kept internally within the openvas_hosts structure.

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

Definition at line 1125 of file openvas_hosts.c.

1126 {
1127  openvas_host_t *next;
1128 
1129  if (hosts == NULL || hosts->current == NULL)
1130  return NULL;
1131 
1132  next = hosts->current->data;
1133  hosts->current = g_list_next (hosts->current);
1134 
1135  return next;
1136 }
The structure for a single host object.
Definition: openvas_hosts.h:76
GList * current
Definition: openvas_hosts.h:96

References openvas_hosts::current.

Referenced by main(), and openvas_hosts_resolve().

Here is the caller graph for this function:

◆ openvas_hosts_removed()

unsigned int openvas_hosts_removed ( const openvas_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 1544 of file openvas_hosts.c.

1545 {
1546  return hosts ? hosts->removed : 0;
1547 }
unsigned int removed
Definition: openvas_hosts.h:98

References openvas_hosts::removed.

Referenced by main().

Here is the caller graph for this function:

◆ openvas_hosts_resolve()

void openvas_hosts_resolve ( openvas_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 1244 of file openvas_hosts.c.

1245 {
1246  openvas_host_t *host;
1247 
1248  hosts->current = hosts->hosts;
1249 
1250  while ((host = openvas_hosts_next (hosts)))
1251  {
1252  struct in_addr addr;
1253 
1254  if (host->type != HOST_TYPE_NAME)
1255  continue;
1256 
1257  if (openvas_host_resolve (host, &addr, AF_INET) == 0)
1258  {
1259  g_free (host->name);
1260  host->type = HOST_TYPE_IPV4;
1261  memcpy (&host->addr, &addr, sizeof (host->addr));
1262  }
1263  }
1264 
1265  hosts->current = hosts->hosts;
1266 }
The structure for a single host object.
Definition: openvas_hosts.h:76
openvas_host_t * openvas_hosts_next(openvas_hosts_t *hosts)
Gets the next openvas_host_t from a openvas_hosts_t structure. The state of iteration is kept interna...
GList * current
Definition: openvas_hosts.h:96
int openvas_host_resolve(const openvas_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...
struct in_addr addr
Definition: openvas_hosts.h:80
enum host_type type
Definition: openvas_hosts.h:83
gchar * name
Definition: openvas_hosts.h:79

References openvas_host::addr, openvas_hosts::current, HOST_TYPE_IPV4, HOST_TYPE_NAME, openvas_hosts::hosts, openvas_host::name, openvas_host_resolve(), openvas_hosts_next(), and openvas_host::type.

Referenced by openvas_hosts_exclude().

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

◆ openvas_hosts_reverse()

void openvas_hosts_reverse ( openvas_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 1206 of file openvas_hosts.c.

1207 {
1208  if (hosts == NULL || hosts->hosts == NULL)
1209  return;
1210 
1211  hosts->hosts = g_list_reverse (hosts->hosts);
1212  hosts->current = hosts->hosts;
1213 }
GList * current
Definition: openvas_hosts.h:96

References openvas_hosts::current, and openvas_hosts::hosts.

◆ openvas_hosts_reverse_lookup_only()

int openvas_hosts_reverse_lookup_only ( openvas_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 1435 of file openvas_hosts.c.

1436 {
1437  int count;
1438  GList *element;
1439 
1440  if (hosts == NULL)
1441  return -1;
1442 
1443  count = 0;
1444  element = hosts->hosts;
1445  while (element)
1446  {
1447  gchar *name = openvas_host_reverse_lookup (element->data);
1448 
1449  if (name == NULL)
1450  {
1451  element = openvas_hosts_remove_element (hosts, element);
1452  count++;
1453  }
1454  else
1455  {
1456  g_free (name);
1457  element = element->next;
1458  }
1459  }
1460 
1461  hosts->count -= count;
1462  hosts->removed += count;
1463  hosts->current = hosts->hosts;
1464  return count;
1465 }
unsigned int removed
Definition: openvas_hosts.h:98
GList * current
Definition: openvas_hosts.h:96
const char * name
Definition: nasl_init.c:524
unsigned int count
Definition: openvas_hosts.h:97
char * openvas_host_reverse_lookup(openvas_host_t *host)
Checks for a host object reverse dns lookup existence.

References openvas_hosts::hosts, name, and openvas_host_reverse_lookup().

Here is the call graph for this function:

◆ openvas_hosts_reverse_lookup_unify()

int openvas_hosts_reverse_lookup_unify ( openvas_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 1477 of file openvas_hosts.c.

1478 {
1482  int count;
1483  GList *element;
1484  GHashTable *name_table;
1485 
1486  if (hosts == NULL)
1487  return -1;
1488 
1489  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1490  count = 0;
1491  element = hosts->hosts;
1492  while (element)
1493  {
1494  gchar *name;
1495 
1496  if ((name = openvas_host_reverse_lookup (element->data)))
1497  {
1498  if (g_hash_table_lookup (name_table, name))
1499  {
1500  element = openvas_hosts_remove_element (hosts, element);
1501  count++;
1502  g_free (name);
1503  }
1504  else
1505  {
1506  /* Insert in the hash table. Value not important. */
1507  g_hash_table_insert (name_table, name, hosts);
1508  element = element->next;
1509  }
1510  }
1511  else
1512  element = element->next;
1513  }
1514 
1515  g_hash_table_destroy (name_table);
1516  hosts->removed += count;
1517  hosts->count -= count;
1518  hosts->current = hosts->hosts;
1519  return count;
1520 }
unsigned int removed
Definition: openvas_hosts.h:98
GList * current
Definition: openvas_hosts.h:96
const char * name
Definition: nasl_init.c:524
unsigned int count
Definition: openvas_hosts.h:97
char * openvas_host_reverse_lookup(openvas_host_t *host)
Checks for a host object reverse dns lookup existence.

References openvas_hosts::hosts, name, and openvas_host_reverse_lookup().

Here is the call graph for this function:

◆ openvas_hosts_shuffle()

void openvas_hosts_shuffle ( openvas_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 1166 of file openvas_hosts.c.

1167 {
1168  int count;
1169  GList *new_list;
1170  GRand *rand;
1171 
1172  if (hosts == NULL)
1173  return;
1174 
1175  count = openvas_hosts_count (hosts);
1176  new_list = NULL;
1177 
1178  rand = g_rand_new ();
1179 
1180  while (count)
1181  {
1182  GList *element;
1183 
1184  /* Get element from random position [0, count[. */
1185  element = g_list_nth (hosts->hosts, g_rand_int_range (rand, 0, count));
1186  /* Remove it. */
1187  hosts->hosts = g_list_remove_link (hosts->hosts, element);
1188  /* Insert it in new list */
1189  new_list = g_list_concat (element, new_list);
1190  count--;
1191  }
1192  hosts->hosts = new_list;
1193  hosts->current = hosts->hosts;
1194 
1195  g_rand_free (rand);
1196 }
GList * current
Definition: openvas_hosts.h:96
unsigned int openvas_hosts_count(const openvas_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.

References openvas_hosts::current, openvas_hosts::hosts, and openvas_hosts_count().

Here is the call graph for this function: