Greenbone Vulnerability Management Libraries  10.0.0
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...
 
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. 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 527 of file hosts.c.

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

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

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

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

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

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

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

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

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

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

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

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

1629 {
1630  GSList *vhosts;
1631  gvm_vhost_t *vhost;
1632  char *value;
1633 
1634  if (!host || host->type == HOST_TYPE_NAME)
1635  return;
1636 
1637  value = gvm_host_reverse_lookup (host);
1638  if (!value)
1639  return;
1640  if (host_name_verify (host, value))
1641  {
1642  g_free (value);
1643  return;
1644  }
1645  /* Don't add vhost, if already in the list. */
1646  vhosts = host->vhosts;
1647  while (vhosts)
1648  {
1649  if (!strcmp (((gvm_vhost_t *) vhosts->data)->value, value))
1650  {
1651  g_free (value);
1652  return;
1653  }
1654  vhosts = vhosts->next;
1655  }
1656  vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1657  host->vhosts = g_slist_prepend (host->vhosts, vhost);
1658 }
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:1596
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:1541
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_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 877 of file hosts.c.

878 {
879  gvm_host_t *h = host;
880  if (h == NULL)
881  return;
882 
883  /* If host of type hostname, free the name buffer, first. */
884  if (h->type == HOST_TYPE_NAME)
885  g_free (h->name);
886 
887  g_slist_free_full (h->vhosts, gvm_vhost_free);
888  g_free (h);
889 }
The structure for a single host object.
Definition: hosts.h:63
GSList * vhosts
Definition: hosts.h:72
gchar * name
Definition: hosts.h:67
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition: hosts.c:846
enum host_type type
Definition: hosts.h:71

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

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

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

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

863 {
864  gvm_host_t *host;
865 
866  host = g_malloc0 (sizeof (gvm_host_t));
867 
868  return host;
869 }
The structure for a single host object.
Definition: hosts.h:63

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

1930 {
1931  if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
1932  return -1;
1933 
1934  return gvm_resolve (host->name, dst, family);
1935 }
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 1541 of file hosts.c.

1542 {
1543  if (host == NULL)
1544  return NULL;
1545 
1546  if (host->type == HOST_TYPE_NAME)
1547  return NULL;
1548  else if (host->type == HOST_TYPE_IPV4)
1549  {
1550  struct sockaddr_in sa;
1551  int retry = 2;
1552  gchar hostname[1000];
1553 
1554  bzero (&sa, sizeof (struct sockaddr));
1555  sa.sin_addr = host->addr;
1556  sa.sin_family = AF_INET;
1557  while (retry--)
1558  {
1559  int ret = getnameinfo ((struct sockaddr *) &sa, sizeof (sa), hostname,
1560  sizeof (hostname), NULL, 0, NI_NAMEREQD);
1561  if (!ret)
1562  return g_strdup (hostname);
1563  if (ret != EAI_AGAIN)
1564  break;
1565  }
1566  return NULL;
1567  }
1568  else if (host->type == HOST_TYPE_IPV6)
1569  {
1570  struct sockaddr_in6 sa;
1571  char hostname[1000];
1572 
1573  bzero (&sa, sizeof (struct sockaddr));
1574  memcpy (&sa.sin6_addr, &host->addr6, 16);
1575  sa.sin6_family = AF_INET6;
1576 
1577  if (getnameinfo ((struct sockaddr *) &sa, sizeof (sa), hostname,
1578  sizeof (hostname), NULL, 0, NI_NAMEREQD))
1579  return NULL;
1580  else
1581  return g_strdup (hostname);
1582  }
1583  else
1584  return NULL;
1585 }
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 1841 of file hosts.c.

1842 {
1843  assert (host);
1844  return host->type;
1845 }
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 1856 of file hosts.c.

1857 {
1858  if (host == NULL)
1859  return NULL;
1860 
1861  return host_type_str[host->type];
1862 }
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 1872 of file hosts.c.

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

899 {
900  if (hosts->count == hosts->max_size)
901  {
902  hosts->max_size *= 4;
903  hosts->hosts =
904  g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
905  }
906  hosts->hosts[hosts->count] = host;
907  hosts->count++;
908 }
size_t max_size
Definition: hosts.h:94
size_t count
Definition: hosts.h:96
gvm_host_t ** hosts
Definition: hosts.h:93

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

1760 {
1761  return hosts ? hosts->count : 0;
1762 }
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_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 974 of file hosts.c.

975 {
979  GHashTable *name_table;
980  size_t i, duplicates = 0;
981 
982  if (hosts == NULL)
983  return;
984  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
985 
986  for (i = 0; i < hosts->count; i++)
987  {
988  gchar *name;
989 
990  if ((name = gvm_host_value_str (hosts->hosts[i])))
991  {
992  gvm_host_t *host, *removed = hosts->hosts[i];
993 
994  host = g_hash_table_lookup (name_table, name);
995  if (host)
996  {
997  /* Remove duplicate host. Add its vhosts to the original host. */
998  host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
999  removed->vhosts = NULL;
1000  gvm_host_free (removed);
1001  hosts->hosts[i] = NULL;
1002  duplicates++;
1003  g_free (name);
1004  }
1005  else
1006  g_hash_table_insert (name_table, name, hosts->hosts[i]);
1007  }
1008  }
1009 
1010  if (duplicates)
1011  gvm_hosts_fill_gaps (hosts);
1012  g_hash_table_destroy (name_table);
1013  hosts->count -= duplicates;
1014  hosts->removed += duplicates;
1015  hosts->current = 0;
1016  malloc_trim (0);
1017 }
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
GSList * vhosts
Definition: hosts.h:72
size_t count
Definition: hosts.h:96
size_t current
Definition: hosts.h:95
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:1872
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_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 1528 of file hosts.c.

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

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

1456 {
1460  gvm_hosts_t *excluded_hosts;
1461  GHashTable *name_table;
1462  size_t excluded = 0, i;
1463 
1464  if (hosts == NULL || excluded_str == NULL)
1465  return -1;
1466 
1467  excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1468  if (excluded_hosts == NULL)
1469  return -1;
1470 
1471  if (gvm_hosts_count (excluded_hosts) == 0)
1472  {
1473  gvm_hosts_free (excluded_hosts);
1474  return 0;
1475  }
1476 
1477  /* Hash host values from excluded hosts list. */
1478  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1479  for (i = 0; i < excluded_hosts->count; i++)
1480  {
1481  gchar *name;
1482 
1483  if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
1484  g_hash_table_insert (name_table, name, hosts);
1485  }
1486 
1487  /* Check for hosts values in hash table. */
1488  for (i = 0; i < hosts->count; i++)
1489  {
1490  gchar *name;
1491 
1492  if ((name = gvm_host_value_str (hosts->hosts[i])))
1493  {
1494  if (g_hash_table_lookup (name_table, name))
1495  {
1496  gvm_host_free (hosts->hosts[i]);
1497  hosts->hosts[i] = NULL;
1498  excluded++;
1499  g_free (name);
1500  continue;
1501  }
1502  g_free (name);
1503  }
1504  }
1505 
1506  /* Cleanup. */
1507  if (excluded)
1508  gvm_hosts_fill_gaps (hosts);
1509  hosts->count -= excluded;
1510  hosts->removed += excluded;
1511  hosts->current = 0;
1512  g_hash_table_destroy (name_table);
1513  gvm_hosts_free (excluded_hosts);
1514  return excluded;
1515 }
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition: hosts.c:1759
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:1872
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_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 936 of file hosts.c.

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

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

919 {
920  gvm_hosts_t *hosts;
921 
922  hosts = g_malloc0 (sizeof (gvm_hosts_t));
923  hosts->max_size = 1024;
924  hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
925  hosts->orig_str = g_strdup (hosts_str);
926  return hosts;
927 }
The structure for a single host object.
Definition: hosts.h:63
size_t max_size
Definition: hosts.h:94
gvm_host_t ** hosts
Definition: hosts.h:93
The structure for Hosts collection.
Definition: hosts.h:90
gchar * orig_str
Definition: hosts.h:92

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 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_strdup (stripped);
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 1773 of file hosts.c.

1774 {
1775  return hosts ? hosts->removed : 0;
1776 }
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;
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  gvm_host_free (host);
1376  hosts->hosts[i] = NULL;
1377  hosts->count--;
1378  hosts->removed++;
1379  if (!list)
1380  g_warning ("Couldn't resolve hostname %s", host->name);
1381  else
1382  gvm_hosts_fill_gaps (hosts);
1383  g_slist_free_full (list, g_free);
1384  }
1385  if (new_entries)
1386  gvm_hosts_deduplicate (hosts);
1387  hosts->current = 0;
1388 }
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 1670 of file hosts.c.

1671 {
1672  size_t i, count = 0;
1673 
1674  if (hosts == NULL)
1675  return -1;
1676 
1677  for (i = 0; i < hosts->count; i++)
1678  {
1679  gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1680 
1681  if (name == NULL)
1682  {
1683  gvm_host_free (hosts->hosts[i]);
1684  hosts->hosts[i] = NULL;
1685  count++;
1686  }
1687  else
1688  g_free (name);
1689  }
1690 
1691  if (count)
1692  gvm_hosts_fill_gaps (hosts);
1693  hosts->count -= count;
1694  hosts->removed += count;
1695  hosts->current = 0;
1696  return count;
1697 }
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:1541
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 1709 of file hosts.c.

1710 {
1714  size_t i, count = 0;
1715  GHashTable *name_table;
1716 
1717  if (hosts == NULL)
1718  return -1;
1719 
1720  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1721  for (i = 0; i < hosts->count; i++)
1722  {
1723  gchar *name;
1724 
1725  if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
1726  {
1727  if (g_hash_table_lookup (name_table, name))
1728  {
1729  gvm_host_free (hosts->hosts[i]);
1730  hosts->hosts[i] = NULL;
1731  count++;
1732  g_free (name);
1733  }
1734  else
1735  {
1736  /* Insert in the hash table. Value not important. */
1737  g_hash_table_insert (name_table, name, hosts);
1738  }
1739  }
1740  }
1741 
1742  if (count)
1743  gvm_hosts_fill_gaps (hosts);
1744  g_hash_table_destroy (name_table);
1745  hosts->removed += count;
1746  hosts->count -= count;
1747  hosts->current = 0;
1748  return count;
1749 }
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:1541
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_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 846 of file hosts.c.

847 {
848  if (vhost)
849  {
850  g_free (((gvm_vhost_t *) vhost)->value);
851  g_free (((gvm_vhost_t *) vhost)->source);
852  }
853  g_free (vhost);
854 }
The structure for a single vhost object.
Definition: hosts.h:78

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

1400 {
1401  GSList *vhost;
1402  char **excluded;
1403  int ret = 0;
1404 
1405  if (!host || !excluded_str)
1406  return ret;
1407 
1408  vhost = host->vhosts;
1409  excluded = g_strsplit (excluded_str, ",", 0);
1410  if (!excluded || !*excluded)
1411  {
1412  g_strfreev (excluded);
1413  return ret;
1414  }
1415  while (vhost)
1416  {
1417  char **tmp = excluded;
1418  char *value = ((gvm_vhost_t *) vhost->data)->value;
1419 
1420  while (*tmp)
1421  {
1422  if (!strcmp (value, g_strstrip (*tmp)))
1423  {
1424  gvm_vhost_free (vhost->data);
1425  host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1426  ret++;
1427  break;
1428  }
1429  tmp++;
1430  if (!*tmp)
1431  {
1432  vhost = vhost->next;
1433  break;
1434  }
1435  }
1436  }
1437  g_strfreev (excluded);
1438 
1439  return ret;
1440 }
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:

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

1597 {
1598  GSList *list, *tmp;
1599  char *host_str;
1600  int ret = -1;
1601 
1602  assert (host);
1603  assert (value);
1604  host_str = gvm_host_value_str (host);
1605  list = tmp = gvm_resolve_list (value);
1606  while (tmp)
1607  {
1608  char buffer[INET6_ADDRSTRLEN];
1609  addr6_to_str (tmp->data, buffer);
1610  if (!strcmp (host_str, buffer))
1611  {
1612  ret = 0;
1613  break;
1614  }
1615  tmp = tmp->next;
1616  }
1617  g_free (host_str);
1618  g_slist_free_full (list, g_free);
1619  return ret;
1620 }
void addr6_to_str(const struct in6_addr *addr6, char *str)
Stringifies an IP address.
Definition: networking.c:260
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition: networking.c:338
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:1872

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

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

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  g_free (addr_str);
130 
131  if (*p || block <= 0 || block > 30)
132  return 0;
133 
134  return 1;
135 }
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

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

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

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

600 {
601  char *first_str, *second_str;
602  int ret;
603 
604  first_str = g_strdup (str);
605  second_str = strchr (first_str, '-');
606  if (second_str == NULL)
607  {
608  g_free (first_str);
609  return 0;
610  }
611 
612  /* Separate the addresses. */
613  *second_str = '\0';
614  second_str++;
615 
616  ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
617  g_free (first_str);
618 
619  return ret;
620 }
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

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

240 {
241  char *first_str, *second_str;
242  int ret;
243 
244  first_str = g_strdup (str);
245  second_str = strchr (first_str, '-');
246  if (second_str == NULL)
247  {
248  g_free (first_str);
249  return 0;
250  }
251 
252  /* Separate the addresses. */
253  *second_str = '\0';
254  second_str++;
255 
256  ret = is_ipv4_address (first_str) && is_ipv4_address (second_str);
257  g_free (first_str);
258 
259  return ret;
260 }
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

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

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

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

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

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

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

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

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

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

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

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

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

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().