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 <unistd.h>`

Go to the source code of this file.

## Macros

#define | G_LOG_DOMAIN "libgvm base"
| GLib log domain.

## 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.
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.
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.
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.
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".
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.
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.
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.
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.
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.
static int | is_hostname (const char *str)
| Checks if a buffer points to a valid hostname.
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.
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.
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".
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.
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.
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.
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.
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.
int | gvm_get_host_type (const gchar *str_stripped)
| Determines the host type in a buffer.
gvm_vhost_t * | gvm_vhost_new (char *value, char *source)
| Creates a new gvm_vhost_t object.
static void | gvm_vhost_free (gpointer vhost)
| Frees the memory occupied by an gvm_vhost_t object.
gpointer | gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
| Creates a deep copy of a gvm_vhost_t object.
static gvm_host_t * | gvm_host_new ()
| Creates a new gvm_host_t object.
void | gvm_host_free (gpointer host)
| Frees the memory occupied by an gvm_host_t object.
void | gvm_hosts_add (gvm_hosts_t *hosts, gvm_host_t *host)
| Inserts a host object at the end of a hosts collection.
static gvm_hosts_t * | gvm_hosts_init (const char *hosts_str)
| Creates a hosts collection from a hosts string.
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.
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.
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.
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.
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.
void | gvm_hosts_move_current_host_to_end (gvm_hosts_t *hosts)
| Move the current gvm_host_t from a gvm_hosts_t structure to the end of the hosts list.
void | gvm_hosts_free (gvm_hosts_t *hosts)
| Frees memory occupied by an gvm_hosts_t structure.
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.
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.
GSList * | gvm_hosts_resolve (gvm_hosts_t *hosts)
| Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values. Not to be used while iterating over the single hosts as it resets the iterator.
int | gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
| Exclude a list of vhosts from a host's vhosts list.
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.
GSList * | gvm_hosts_allowed_only (gvm_hosts_t *hosts, const char *deny_hosts_str, const char *allow_hosts_str)
| Returns a list of hosts after a host authorization check.
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.
gvm_host_t * | gvm_host_from_str (const gchar *host_str)
| Creates a new gvm_host_t from a host string.
char * | gvm_host_reverse_lookup (gvm_host_t *host)
| Checks for a host object reverse dns lookup existence.
static int | host_name_verify (gvm_host_t *host, const char *value)
| Verifies that hostname value resolves to a host's IP.
void | gvm_host_add_reverse_lookup (gvm_host_t *host)
| Add a host's reverse-lookup name to the vhosts list.
gvm_hosts_t * | gvm_hosts_reverse_lookup_only_excluded (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.
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.
gvm_hosts_t * | gvm_hosts_reverse_lookup_unify_excluded (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.
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.
unsigned int | gvm_hosts_count (const gvm_hosts_t *hosts)
| Gets the count of single hosts objects in a hosts collection.
unsigned int | gvm_hosts_removed (const gvm_hosts_t *hosts)
| Gets the count of single values in hosts string that were removed (duplicates / excluded.)
unsigned int | gvm_hosts_duplicated (const gvm_hosts_t *hosts)
| Gets the count of single values in hosts string that were duplicated and therefore removed from the list.
gvm_host_t * | gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, const gvm_hosts_t *hosts)
| Find the gvm_host_t from a gvm_hosts_t structure.
gvm_host_t * | gvm_duplicate_host (gvm_host_t *host)
| Creates a deep copy of a host. gvm_host_free has to be called on it.
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.
enum host_type | gvm_host_type (const gvm_host_t *host)
| Gets a host object's type.
gchar * | gvm_host_type_str (const gvm_host_t *host)
| Gets a host's type in printable format.
gchar * | gvm_host_value_str (const gvm_host_t *host)
| Gets a host's value in printable format.
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.
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.

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

## Macro Definition Documentation

## G_LOG_DOMAIN

#define G_LOG_DOMAIN "libgvm base"

GLib log domain.

## 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] | str | Buffer containing CIDR-expressed block.
  [out] | first | First IPv4 address in block.
  [out] | last | Last IPv4 address in block.

Returns
  -1 if error, 0 else.

## 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] | str | Buffer containing CIDR-expressed block.
  [out] | block | Variable to store block value.

Returns
  -1 if error, 0 otherwise.

## 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] | str | String containing CIDR-expressed block.
  [out] | addr6 | Variable to store the IPv4 address value.

Returns
  -1 if error, 0 otherwise.

## 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] | str | Buffer containing CIDR-expressed block.
  [out] | first | First IPv4 address in block.
  [out] | last | Last IPv4 address in block.

Returns
  -1 if error, 0 else.

## 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] | str | Buffer containing CIDR-expressed block.
  [out] | block | Variable to store block value.

Returns
  -1 if error, 0 otherwise.

## 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] | str | String containing CIDR-expressed block.
  [out] | addr | Variable to store the IPv4 address value.

Returns
  -1 if error, 0 otherwise.

## gvm_duplicate_host()

gvm_host_t * gvm_duplicate_host | ( | gvm_host_t * | host | ) |

Creates a deep copy of a host. gvm_host_free has to be called on it.

Parameters

  host | source host

Returns
  gvm_host_t* copy of host

## gvm_duplicate_vhost()

gpointer gvm_duplicate_vhost | ( | gconstpointer | vhost,
|  | gpointer | data
| ) |  |

Creates a deep copy of a gvm_vhost_t object.

Parameters

  vhost | source vhost
  data | dummy for g_slist_copy_deep

Returns
  gpointer copy of vhost

## gvm_get_host_type()

int gvm_get_host_type | ( | const gchar * | str_stripped | ) |

Determines the host type in a buffer.

Parameters

  [in] | str_stripped | Buffer that contains host definition, could a be hostname, single IPv4 or IPv6, CIDR-expressed block etc,.

Returns
  Host_TYPE_*, -1 if error.

## 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] | host | The host to which we add the vhost.

## gvm_host_find_in_hosts()

gvm_host_t * gvm_host_find_in_hosts | ( | const gvm_host_t * | host,
|  | const struct in6_addr * | addr,
|  | const gvm_hosts_t * | hosts
| ) |  |

Find the gvm_host_t from a gvm_hosts_t structure.

Parameters

  [in] | host | The host object.
  [in] | addr | Optional pointer to ip address. Could be used so that host isn't resolved multiple times when type is HOST_TYPE_NAME.
  [in] | hosts | Hosts collection.

Returns
  Pointer to host if found. NULL if error or host not found

## gvm_host_free()

void gvm_host_free | ( | gpointer | host | ) |

Frees the memory occupied by an gvm_host_t object.

Parameters

  [in] | host | Host to free.

## gvm_host_from_str()

gvm_host_t * gvm_host_from_str | ( | const gchar * | host_str | ) |

Creates a new gvm_host_t from a host string.

Parameters

  [in] | host_str | The host string can consist of a hostname, IPv4 address or IPv6 address.

Returns
  NULL if error. Otherwise, a single host structure that should be put into a gvm_hosts_t structure for freeing with gvm_hosts_free or freed directly via gvm_host_free.

## 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] | host | The host object whose value to get as IPv6.
  [out] | ip6 | Buffer to store the IPv6 address.

Returns
  -1 if error, 0 otherwise.

## 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] | host | The host object.
  [in] | addr | Optional pointer to ip address. Could be used so that host isn't resolved multiple times when type is HOST_TYPE_NAME.
  [in] | hosts | Hosts collection.

Returns
  1 if host has equal in hosts, 0 otherwise.

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

## 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] | host | The host object whose name to resolve.
  [out] | dst | Buffer to store resolved address. Size must be at least 4 bytes for AF_INET and 16 bytes for AF_INET6.
  [in] | family | Either AF_INET or AF_INET6.

Returns
  -1 if error, 0 otherwise.

## gvm_host_reverse_lookup()

char * gvm_host_reverse_lookup | ( | gvm_host_t * | host | ) |

Checks for a host object reverse dns lookup existence.

Parameters

  [in] | host | The host to reverse-lookup.

Returns
  Result of look-up, NULL otherwise.

## gvm_host_type()

enum host_type gvm_host_type | ( | const gvm_host_t * | host | ) |

Gets a host object's type.

Parameters

  [in] | host | The host object.

Returns
  Host type.

## gvm_host_type_str()

gchar * gvm_host_type_str | ( | const gvm_host_t * | host | ) |

Gets a host's type in printable format.

Parameters

  [in] | host | The host object.

Returns
  String representing host type. Statically allocated, thus, not to be freed.

## gvm_host_value_str()

gchar * gvm_host_value_str | ( | const gvm_host_t * | host | ) |

Gets a host's value in printable format.

Parameters

  [in] | host | The host object.

Returns
  String representing host value. To be freed with g_free().

## gvm_hosts_add()

void gvm_hosts_add | ( | gvm_hosts_t * | hosts,
|  | gvm_host_t * | host
| ) |  |

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

Parameters

  [in] | hosts | Hosts in which to insert the host.
  [in] | host | Host to insert.

## gvm_hosts_allowed_only()

GSList * gvm_hosts_allowed_only | ( | gvm_hosts_t * | hosts,
|  | const char * | deny_hosts_str,
|  | const char * | allow_hosts_str
| ) |  |

Returns a list of hosts after a host authorization check.

Parameters

  | [in/out] | hosts The hosts collection from which to exclude.
  [in] | deny_hosts_str | String of denied hosts. This hosts will be removed from the hosts list
  [in] | allow_hosts_str | String of allow hosts. This hosts will be kept in the hosts list

Returns
  List of non-authorized hosts if any, otherwise Null. The returned list must be free()'d by the caller functions.

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

## 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] | hosts | The hosts collection to count hosts of.

Returns
  The number of single hosts.

## 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] | hosts | hosts collection from which to remove duplicates.

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

## gvm_hosts_duplicated()

unsigned int gvm_hosts_duplicated | ( | const gvm_hosts_t * | hosts | ) |

Gets the count of single values in hosts string that were duplicated and therefore removed from the list.

Parameters

  [in] | hosts | The hosts collection.

Returns
  The number of duplicated values.

## 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] | hosts | The hosts collection from which to exclude.
  [in] | excluded_str | String of hosts to exclude.

Returns
  Number of excluded hosts, -1 if error.

## 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/out] | hosts The hosts collection from which to exclude.
  [in] | excluded_str | String of hosts to exclude.
  [in] | max_hosts | Max 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.

## 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] | hosts | Hosts collection to fill gaps in.

## gvm_hosts_free()

void gvm_hosts_free | ( | gvm_hosts_t * | hosts | ) |

Frees memory occupied by an gvm_hosts_t structure.

Parameters

  [in] | hosts | The hosts collection to free.

## 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_str | String of hosts.

Returns
  Hosts collection.

## gvm_hosts_move_current_host_to_end()

void gvm_hosts_move_current_host_to_end | ( | gvm_hosts_t * | hosts | ) |

Move the current gvm_host_t from a gvm_hosts_t structure to the end of the hosts list.

Parameters

  [in/out] | hosts gvm_hosts_t structure which hosts must be rearange. The hosts->current index points to the last used hosts and gvm_hosts_next() must be called to get the next host in the list.

## 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_str | The 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.

## 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_str | The hosts string. A copy will be created of this within the returned struct.
  [in] | max_hosts | Max 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.

## 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] | hosts | gvm_hosts_t structure to get next host from.

Returns
  Pointer to host. NULL if error or end of 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] | hosts | The hosts collection.

Returns
  The number of removed values.

## gvm_hosts_resolve()

GSList * gvm_hosts_resolve | ( | gvm_hosts_t * | hosts | ) |

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

Parameters

  [in] | hosts | The hosts collection from which to exclude.

Returns
  List of unresolved hostnames.

## 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] | hosts | The hosts collection to reverse.

## 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] | hosts | The hosts collection to filter.

Returns
  Number of hosts removed, -1 if error.

## gvm_hosts_reverse_lookup_only_excluded()

gvm_hosts_t * gvm_hosts_reverse_lookup_only_excluded | ( | 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] | hosts | The hosts collection to filter.

Returns
  list of hosts removed, Null on error.

## 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] | hosts | The hosts collection to filter.

Returns
  Number of hosts removed, -1 if error.

## gvm_hosts_reverse_lookup_unify_excluded()

gvm_hosts_t * gvm_hosts_reverse_lookup_unify_excluded | ( | 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] | hosts | The hosts collection to filter.

Returns
  List of hosts removed, Null if error.

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

## 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] | hosts | The hosts collection to shuffle.

## gvm_vhost_free()

static void gvm_vhost_free | ( | gpointer | vhost | ) |

static

Frees the memory occupied by an gvm_vhost_t object.

Parameters

  [in] | vhost | Vhost to free.

## gvm_vhost_new()

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

Creates a new gvm_vhost_t object.

Parameters

  [in] | value | Vhost value.
  [in] | source | Source of hostname.

Returns
  Pointer to new vhost object.

## 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] | host | The host whose vhosts are to be excluded from.
  [in] | excluded_str | String of hosts to exclude.

Returns
  Number of excluded vhosts.

## 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] | host | The host whose IP is to be checked against.
  [in] | value | Hostname value to verify.

Returns
  0 if hostname resolves to host's IP, -1 otherwise.

## 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] | str | Buffer to check in.

Returns
  1 if valid IPv6 CIDR-expressed block, 0 otherwise.

## 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] | str | Buffer to check in.

Returns
  1 if valid CIDR-expressed block, 0 otherwise.

## is_hostname()

static int is_hostname | ( | const char * | str | ) |

static

Checks if a buffer points to a valid hostname.

Parameters

  [in] | str | String to check.

Returns
  1 if valid hostname, 0 otherwise.

## 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] | str | Buffer to check in.

Returns
  1 if valid IPv4 address, 0 otherwise.

## 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] | str | Buffer to check in.

Returns
  1 if valid IPv6 address, 0 otherwise.

## 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] | str | Buffer to check in.

Returns
  1 if valid long range-expressed network, 0 otherwise.

## 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] | str | Buffer to check in.

Returns
  1 if valid long range-expressed network, 0 otherwise.

## 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

  str | String to check in.

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

## 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

  str | String to check in.

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

## 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] | str | String containing long IPv6 range-expressed network.
  [out] | first | First IPv6 address in range.
  [out] | last | Last IPv6 address in range.

Returns
  -1 if error, 0 else.

## 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] | str | String containing long range-expressed network.
  [out] | first | First IP address in block.
  [out] | last | Last IP address in block.

Returns
  -1 if error, 0 else.

## 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] | str | String containing short IPv6 range-expressed network.
  [out] | first | First IPv6 address in range.
  [out] | last | Last IPv6 address in range.

Returns
  -1 if error, 0 else.

## 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] | str | String containing short range-expressed network.
  [out] | first | First IP address in block.
  [out] | last | Last IP address in block.

Returns
  -1 if error, 0 else.

## 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"}
HOST_TYPE_RANGE_SHORT
@ HOST_TYPE_RANGE_SHORT
Definition hosts.h:38
HOST_TYPE_RANGE_LONG
@ HOST_TYPE_RANGE_LONG
Definition hosts.h:39
HOST_TYPE_NAME
@ HOST_TYPE_NAME
Definition hosts.h:35
HOST_TYPE_IPV6
@ HOST_TYPE_IPV6
Definition hosts.h:40
HOST_TYPE_IPV4
@ HOST_TYPE_IPV4
Definition hosts.h:36
HOST_TYPE_CIDR_BLOCK
@ HOST_TYPE_CIDR_BLOCK
Definition hosts.h:37