Greenbone Vulnerability Management Libraries 22.8.0
hosts.h File Reference

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

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

Go to the source code of this file.

Data Structures

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

Macros

#define FEATURE_HOSTS_ALLOWED_ONLY   1
 Flag that indecates that this version includes the function gvm_hosts_allowed_only()
 
#define FEATURE_REVERSE_LOOKUP_EXCLUDED   1
 Flag that indecates that this version includes the functions gvm_reverse_lookup_only_excluded() and gvm_reverse_lookup_unify_excluded()
 

Typedefs

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

Enumerations

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

Functions

gvm_hosts_tgvm_hosts_new (const gchar *)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
 
gvm_hosts_tgvm_hosts_new_with_max (const gchar *, unsigned int)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
 
gvm_host_tgvm_hosts_next (gvm_hosts_t *)
 Gets the next gvm_host_t from a gvm_hosts_t structure. The state of iteration is kept internally within the gvm_hosts structure.
 
void gvm_hosts_move_current_host_to_end (gvm_hosts_t *)
 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 *)
 Frees memory occupied by an gvm_hosts_t structure.
 
void gvm_hosts_shuffle (gvm_hosts_t *)
 Randomizes the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator.
 
void gvm_hosts_reverse (gvm_hosts_t *)
 Reverses the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator.
 
void gvm_hosts_add (gvm_hosts_t *, gvm_host_t *)
 Inserts a host object at the end of a hosts collection.
 
GSList * gvm_hosts_resolve (gvm_hosts_t *)
 Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values. Not to be used while iterating over the single hosts as it resets the iterator.
 
int gvm_hosts_exclude (gvm_hosts_t *, const char *)
 Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
 
int gvm_vhosts_exclude (gvm_host_t *, const char *)
 Exclude a list of vhosts from a host's vhosts list.
 
int gvm_hosts_exclude_with_max (gvm_hosts_t *, const char *, unsigned int)
 Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
 
GSList * gvm_hosts_allowed_only (gvm_hosts_t *, const char *, const char *)
 Returns a list of hosts after a host authorization check.
 
char * gvm_host_reverse_lookup (gvm_host_t *)
 Checks for a host object reverse dns lookup existence.
 
int gvm_hosts_reverse_lookup_only (gvm_hosts_t *)
 Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
 
int gvm_hosts_reverse_lookup_unify (gvm_hosts_t *)
 Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over the single hosts as it resets the iterator.
 
gvm_hosts_tgvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *)
 Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
 
gvm_hosts_tgvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *)
 Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over the single hosts as it resets the iterator.
 
unsigned int gvm_hosts_count (const gvm_hosts_t *)
 Gets the count of single hosts objects in a hosts collection.
 
unsigned int gvm_hosts_removed (const gvm_hosts_t *)
 Gets the count of single values in hosts string that were removed (duplicates / excluded.)
 
unsigned int gvm_hosts_duplicated (const gvm_hosts_t *)
 Gets the count of single values in hosts string that were duplicated and therefore removed from the list.
 
gvm_host_tgvm_host_from_str (const gchar *hosts_str)
 Creates a new gvm_host_t from a host string.
 
int gvm_host_in_hosts (const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *)
 Returns whether a host has an equal host in a hosts collection. eg. 192.168.10.1 has an equal in list created from "192.168.10.1-5, 192.168.10.10-20" string while 192.168.10.7 doesn't.
 
gvm_host_tgvm_host_find_in_hosts (const gvm_host_t *, const struct in6_addr *, const gvm_hosts_t *)
 Find the gvm_host_t from a gvm_hosts_t structure.
 
gchar * gvm_host_type_str (const gvm_host_t *)
 Gets a host's type in printable format.
 
enum host_type gvm_host_type (const gvm_host_t *)
 Gets a host object's type.
 
gchar * gvm_host_value_str (const gvm_host_t *)
 Gets a host's value in printable format.
 
int gvm_host_resolve (const gvm_host_t *, void *, int)
 Resolves a host object's name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAME.
 
int gvm_host_get_addr6 (const gvm_host_t *, struct in6_addr *)
 Gives a host object's value as an IPv6 address. If the host type is hostname, it resolves the IPv4 address then gives an IPv4-mapped IPv6 address (eg. ::ffff:192.168.1.1 .) If the host type is IPv4, it gives an IPv4-mapped IPv6 address. If the host's type is IPv6, it gives the value directly.
 
void gvm_host_add_reverse_lookup (gvm_host_t *)
 Add a host's reverse-lookup name to the vhosts list.
 
void gvm_host_free (gpointer)
 Frees the memory occupied by an gvm_host_t object.
 
gpointer gvm_duplicate_vhost (gconstpointer, gpointer)
 Creates a deep copy of a gvm_vhost_t object.
 
gvm_host_tgvm_duplicate_host (gvm_host_t *)
 Creates a deep copy of a host. gvm_host_free has to be called on it.
 
gvm_vhost_tgvm_vhost_new (char *, char *)
 Creates a new gvm_vhost_t object.
 
int gvm_get_host_type (const gchar *)
 Determines the host type in a buffer.
 

Detailed Description

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

This file contains the protos for hosts.c

Definition in file hosts.h.

Macro Definition Documentation

◆ FEATURE_HOSTS_ALLOWED_ONLY

#define FEATURE_HOSTS_ALLOWED_ONLY   1

Flag that indecates that this version includes the function gvm_hosts_allowed_only()

Definition at line 20 of file hosts.h.

◆ FEATURE_REVERSE_LOOKUP_EXCLUDED

#define FEATURE_REVERSE_LOOKUP_EXCLUDED   1

Flag that indecates that this version includes the functions gvm_reverse_lookup_only_excluded() and gvm_reverse_lookup_unify_excluded()

Definition at line 26 of file hosts.h.

Typedef Documentation

◆ gvm_host_t

typedef struct gvm_host gvm_host_t

Definition at line 48 of file hosts.h.

◆ gvm_hosts_t

typedef struct gvm_hosts gvm_hosts_t

Definition at line 50 of file hosts.h.

◆ gvm_vhost_t

typedef struct gvm_vhost gvm_vhost_t

Definition at line 49 of file hosts.h.

Enumeration Type Documentation

◆ host_type

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

Definition at line 33 of file hosts.h.

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

Function Documentation

◆ 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
hostsource host
Returns
gvm_host_t* copy of host

Definition at line 2176 of file hosts.c.

2177{
2178 gvm_host_t *ret = NULL;
2179
2180 if (host == NULL)
2181 return NULL;
2182
2183 ret = gvm_host_new ();
2184
2185 ret->type = host->type;
2186 switch (host->type)
2187 {
2188 case HOST_TYPE_NAME:
2189 ret->name = g_strdup (host->name);
2190 break;
2191 case HOST_TYPE_IPV4:
2192 ret->addr.s_addr = host->addr.s_addr;
2193 break;
2194 case HOST_TYPE_IPV6:
2195 ret->addr6.__in6_u = host->addr6.__in6_u;
2196 break;
2197 default:
2198 g_free (ret);
2199 return NULL;
2200 }
2201 ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);
2202
2203 return ret;
2204}
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition: hosts.c:926
gpointer gvm_duplicate_vhost(gconstpointer vhost, gpointer data)
Creates a deep copy of a gvm_vhost_t object.
Definition: hosts.c:906
The structure for a single host object.
Definition: hosts.h:61
struct in6_addr addr6
Definition: hosts.h:66
gchar * name
Definition: hosts.h:64
GSList * vhosts
Definition: hosts.h:69
struct in_addr addr
Definition: hosts.h:65
enum host_type type
Definition: hosts.h:68

References gvm_host::addr, gvm_host::addr6, gvm_duplicate_vhost(), gvm_host_new(), HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, gvm_host::name, gvm_host::type, and gvm_host::vhosts.

Referenced by gvm_hosts_reverse_lookup_only_excluded(), and gvm_hosts_reverse_lookup_unify_excluded().

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

◆ gvm_duplicate_vhost()

gpointer gvm_duplicate_vhost ( gconstpointer  vhost,
gpointer  data 
)

Creates a deep copy of a gvm_vhost_t object.

Parameters
vhostsource vhost
datadummy for g_slist_copy_deep
Returns
gpointer copy of vhost

Definition at line 906 of file hosts.c.

907{
908 (void) (data);
909 gvm_vhost_t *ret = NULL;
910
911 if (!vhost)
912 return NULL;
913
914 ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
915 g_strdup (((gvm_vhost_t *) vhost)->source));
916
917 return ret;
918}
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition: hosts.c:871
The structure for a single vhost object.
Definition: hosts.h:76

References gvm_vhost_new().

Referenced by gvm_duplicate_host().

Here is the call graph for this function:
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 810 of file hosts.c.

811{
812 /*
813 * We have a single element with no leading or trailing
814 * white spaces. This element could represent different host
815 * definitions: single IPs, host names, CIDR-expressed blocks,
816 * range-expressed networks, IPv6 addresses.
817 */
818
819 /* Null or empty string. */
820 if (str_stripped == NULL || *str_stripped == '\0')
821 return -1;
822
823 /* Check for regular single IPv4 address. */
824 if (is_ipv4_address (str_stripped))
825 return HOST_TYPE_IPV4;
826
827 /* Check for regular single IPv6 address. */
828 if (is_ipv6_address (str_stripped))
829 return HOST_TYPE_IPV6;
830
831 /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
832 if (is_cidr_block (str_stripped))
834
835 /* Check for short range-expressed networks "192.168.12.5-40" */
836 if (is_short_range_network (str_stripped))
838
839 /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
840 if (is_long_range_network (str_stripped))
842
843 /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
844 if (is_cidr6_block (str_stripped))
846
847 /* Check for short range-expressed networks "::1-ef12" */
848 if (is_short_range6_network (str_stripped))
850
851 /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
852 if (is_long_range6_network (str_stripped))
854
855 /* Check for hostname. */
856 if (is_hostname (str_stripped))
857 return HOST_TYPE_NAME;
858
859 return -1;
860}
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:229
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:641
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:91
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:304
static int is_hostname(const char *str)
Checks if a buffer points to a valid hostname.
Definition: hosts.c:396
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:467
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:716
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:75
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:59

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 Ensure(), gvm_host_from_str(), 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 1899 of file hosts.c.

1900{
1901 GSList *vhosts;
1902 gvm_vhost_t *vhost;
1903 char *value;
1904
1905 if (!host || host->type == HOST_TYPE_NAME)
1906 return;
1907
1908 value = gvm_host_reverse_lookup (host);
1909 if (!value)
1910 return;
1911 if (host_name_verify (host, value))
1912 {
1913 g_free (value);
1914 return;
1915 }
1916 /* Don't add vhost, if already in the list. */
1917 vhosts = host->vhosts;
1918 while (vhosts)
1919 {
1920 if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1921 {
1922 g_free (value);
1923 return;
1924 }
1925 vhosts = vhosts->next;
1926 }
1927 vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1928 host->vhosts = g_slist_prepend (host->vhosts, vhost);
1929}
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition: hosts.c:1814
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:1867

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_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]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
Pointer to host if found. NULL if error or host not found

Definition at line 2127 of file hosts.c.

2129{
2130 char *host_str;
2131 size_t i;
2132
2133 if (host == NULL || hosts == NULL)
2134 return NULL;
2135
2136 host_str = gvm_host_value_str (host);
2137
2138 for (i = 0; i < hosts->count; i++)
2139 {
2140 gvm_host_t *current_host = hosts->hosts[i];
2141 char *tmp = gvm_host_value_str (current_host);
2142
2143 if (strcasecmp (host_str, tmp) == 0)
2144 {
2145 g_free (host_str);
2146 g_free (tmp);
2147 return current_host;
2148 }
2149 g_free (tmp);
2150
2151 /* Hostnames in hosts list shouldn't be resolved. */
2152 if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
2153 {
2154 struct in6_addr tmpaddr;
2155 gvm_host_get_addr6 (current_host, &tmpaddr);
2156
2157 if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
2158 {
2159 g_free (host_str);
2160 return current_host;
2161 }
2162 }
2163 }
2164
2165 g_free (host_str);
2166 return NULL;
2167}
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:2267
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:2345
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition: hosts.c:2236
size_t count
Definition: hosts.h:93
gvm_host_t ** hosts
Definition: hosts.h:90

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

Referenced by gvm_host_in_hosts().

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

◆ gvm_host_free()

void gvm_host_free ( gpointer  host)

Frees the memory occupied by an gvm_host_t object.

Parameters
[in]hostHost to free.

Definition at line 941 of file hosts.c.

942{
943 gvm_host_t *h = host;
944 if (h == NULL)
945 return;
946
947 /* If host of type hostname, free the name buffer, first. */
948 if (h->type == HOST_TYPE_NAME)
949 g_free (h->name);
950
951 g_slist_free_full (h->vhosts, gvm_vhost_free);
952 g_free (h);
953}
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition: hosts.c:888

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

Referenced by gvm_hosts_allowed_only(), gvm_hosts_deduplicate(), gvm_hosts_exclude_with_max(), gvm_hosts_free(), gvm_hosts_resolve(), gvm_hosts_reverse_lookup_only_excluded(), and gvm_hosts_reverse_lookup_unify_excluded().

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

◆ 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_strThe 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.

Definition at line 1765 of file hosts.c.

1766{
1767 int host_type;
1768
1769 if (host_str == NULL)
1770 return NULL;
1771
1772 /* IPv4, hostname, IPv6 */
1773 /* -1 if error. */
1774 host_type = gvm_get_host_type (host_str);
1775
1776 switch (host_type)
1777 {
1778 case HOST_TYPE_NAME:
1779 case HOST_TYPE_IPV4:
1780 case HOST_TYPE_IPV6:
1781 {
1782 /* New host. */
1783 gvm_host_t *host = gvm_host_new ();
1784 host->type = host_type;
1786 host->name = g_ascii_strdown (host_str, -1);
1787 else if (host_type == HOST_TYPE_IPV4)
1788 {
1789 if (inet_pton (AF_INET, host_str, &host->addr) != 1)
1790 break;
1791 }
1792 else if (host_type == HOST_TYPE_IPV6)
1793 {
1794 if (inet_pton (AF_INET6, host_str, &host->addr6) != 1)
1795 break;
1796 }
1797 return host;
1798 }
1799 case -1:
1800 default:
1801 return NULL;
1802 }
1803 return NULL;
1804}
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition: hosts.c:810
host_type
Definition: hosts.h:34

References gvm_host::addr, gvm_host::addr6, gvm_get_host_type(), gvm_host_new(), HOST_TYPE_IPV4, HOST_TYPE_IPV6, HOST_TYPE_NAME, gvm_host::name, and gvm_host::type.

Here is the call graph for this function:

◆ gvm_host_get_addr6()

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

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

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

Definition at line 2345 of file hosts.c.

2346{
2347 if (host == NULL || ip6 == NULL)
2348 return -1;
2349
2350 switch (gvm_host_type (host))
2351 {
2352 case HOST_TYPE_IPV6:
2353 memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
2354 return 0;
2355
2356 case HOST_TYPE_IPV4:
2357 ipv4_as_ipv6 (&host->addr, ip6);
2358 return 0;
2359
2360 case HOST_TYPE_NAME:
2361 {
2362 struct in_addr ip4;
2363
2364 /* Fail if IPv4 and IPv6 both don't resolve. */
2365 if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
2366 ipv4_as_ipv6 (&ip4, ip6);
2367 else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
2368 return -1;
2369 return 0;
2370 }
2371
2372 default:
2373 return -1;
2374 }
2375}
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:2324
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:243

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

2221{
2222 if (gvm_host_find_in_hosts (host, addr, hosts))
2223 return 1;
2224
2225 return 0;
2226}
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.
Definition: hosts.c:2127

References gvm_host_find_in_hosts().

Here is the call graph for this function:

◆ gvm_host_resolve()

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

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

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

Definition at line 2324 of file hosts.c.

2325{
2326 if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
2327 return -1;
2328
2329 return gvm_resolve (host->name, dst, family);
2330}
int gvm_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
Definition: networking.c:389

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

1815{
1816 int retry = 10;
1817 gchar hostname[NI_MAXHOST];
1818 void *addr;
1819 size_t addrlen;
1820 struct sockaddr_in sa;
1821 struct sockaddr_in6 sa6;
1822
1823 if (!host)
1824 return NULL;
1825
1826 if (host->type == HOST_TYPE_IPV4)
1827 {
1828 addr = &sa;
1829 addrlen = sizeof (sa);
1830 memset (addr, '\0', addrlen);
1831 sa.sin_addr = host->addr;
1832 sa.sin_family = AF_INET;
1833 }
1834 else if (host->type == HOST_TYPE_IPV6)
1835 {
1836 addr = &sa6;
1837 addrlen = sizeof (sa6);
1838 memset (&sa6, '\0', addrlen);
1839 memcpy (&sa6.sin6_addr, &host->addr6, 16);
1840 sa6.sin6_family = AF_INET6;
1841 }
1842 else
1843 return NULL;
1844
1845 while (retry--)
1846 {
1847 int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
1848 0, NI_NAMEREQD);
1849 if (!ret)
1850 return g_ascii_strdown (hostname, -1);
1851 if (ret != EAI_AGAIN)
1852 break;
1853 usleep (10000); // 10ms
1854 }
1855 return NULL;
1856}

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

Referenced by gvm_host_add_reverse_lookup(), gvm_hosts_reverse_lookup_only_excluded(), and gvm_hosts_reverse_lookup_unify_excluded().

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

2237{
2238 assert (host);
2239 return host->type;
2240}

References gvm_host::type.

Referenced by gvm_host_find_in_hosts(), and gvm_host_get_addr6().

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

2252{
2253 if (host == NULL)
2254 return NULL;
2255
2256 return host_type_str[host->type];
2257}
gchar * host_type_str[HOST_TYPE_MAX]
Definition: hosts.c:40

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

2268{
2269 if (host == NULL)
2270 return NULL;
2271
2272 switch (host->type)
2273 {
2274 case HOST_TYPE_NAME:
2275 return g_strdup (host->name);
2276 break;
2277 case HOST_TYPE_IPV4:
2278 case HOST_TYPE_IPV6:
2279 /* Handle both cases using inet_ntop(). */
2280 {
2281 int family, size;
2282 gchar *str;
2283 const void *srcaddr;
2284
2285 if (host->type == HOST_TYPE_IPV4)
2286 {
2287 family = AF_INET;
2288 size = INET_ADDRSTRLEN;
2289 srcaddr = &host->addr;
2290 }
2291 else
2292 {
2293 family = AF_INET6;
2294 size = INET6_ADDRSTRLEN;
2295 srcaddr = &host->addr6;
2296 }
2297
2298 str = g_malloc0 (size);
2299 if (inet_ntop (family, srcaddr, str, size) == NULL)
2300 {
2301 perror ("inet_ntop");
2302 g_free (str);
2303 return NULL;
2304 }
2305 return str;
2306 }
2307 default:
2308 return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
2309 }
2310}

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 Ensure(), gvm_host_find_in_hosts(), gvm_hosts_allowed_only(), gvm_hosts_deduplicate(), gvm_hosts_exclude_with_max(), and host_name_verify().

Here is the caller graph for this function:

◆ 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]hostsHosts in which to insert the host.
[in]hostHost to insert.

Definition at line 962 of file hosts.c.

963{
964 if (hosts->count == hosts->max_size)
965 {
966 hosts->max_size *= 4;
967 hosts->hosts =
968 g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
969 memset (hosts->hosts + hosts->count, '\0',
970 (hosts->max_size - hosts->count) * sizeof (gvm_host_t *));
971 }
972 hosts->hosts[hosts->count] = host;
973 hosts->count++;
974}
size_t max_size
Definition: hosts.h:91

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

Referenced by gvm_hosts_new_with_max(), gvm_hosts_resolve(), gvm_hosts_reverse_lookup_only_excluded(), and gvm_hosts_reverse_lookup_unify_excluded().

Here is the caller graph for this function:

◆ 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]hostsThe hosts collection from which to exclude.
[in]deny_hosts_strString of denied hosts. This hosts will be removed from the hosts list
[in]allow_hosts_strString 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.

Definition at line 1639 of file hosts.c.

1641{
1645 gvm_hosts_t *allowed_hosts, *denied_hosts;
1646 GHashTable *name_allow_table = NULL, *name_deny_table = NULL;
1647 GSList *removed = NULL;
1648 size_t excluded = 0, i;
1649
1650 if (hosts == NULL || (deny_hosts_str == NULL && allow_hosts_str == NULL))
1651 return NULL;
1652
1653 // Prepare list of denied and allowed hosts
1654 denied_hosts = gvm_hosts_new_with_max (deny_hosts_str, 0);
1655 allowed_hosts = gvm_hosts_new_with_max (allow_hosts_str, 0);
1656 if (denied_hosts == NULL && allowed_hosts == NULL)
1657 return NULL;
1658
1659 if (gvm_hosts_count (denied_hosts) == 0)
1660 gvm_hosts_free (denied_hosts);
1661 else
1662 {
1663 /* Hash host values from denied hosts list. */
1664 name_deny_table =
1665 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1666 for (i = 0; i < denied_hosts->count; i++)
1667 {
1668 gchar *name;
1669
1670 if ((name = gvm_host_value_str (denied_hosts->hosts[i])))
1671 g_hash_table_insert (name_deny_table, name, hosts);
1672 }
1673 }
1674 if (gvm_hosts_count (allowed_hosts) == 0)
1675 gvm_hosts_free (allowed_hosts);
1676 else
1677 {
1678 /* Hash host values from allowed hosts list. */
1679 name_allow_table =
1680 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1681 for (i = 0; i < allowed_hosts->count; i++)
1682 {
1683 gchar *name;
1684
1685 if ((name = gvm_host_value_str (allowed_hosts->hosts[i])))
1686 g_hash_table_insert (name_allow_table, name, hosts);
1687 }
1688 }
1689
1690 /* Check for authorized hosts in hash table and create a list of removed
1691 * hosts. */
1692 for (i = 0; i < hosts->count; i++)
1693 {
1694 gchar *name;
1695
1696 if ((name = gvm_host_value_str (hosts->hosts[i])))
1697 {
1698 if (denied_hosts != NULL
1699 && g_hash_table_lookup (name_deny_table, name))
1700 {
1701 gvm_host_free (hosts->hosts[i]);
1702 hosts->hosts[i] = NULL;
1703 excluded++;
1704 removed = g_slist_prepend (removed, name);
1705 continue;
1706 }
1707 else if (allowed_hosts != NULL
1708 && !g_hash_table_lookup (name_allow_table, name))
1709 {
1710 gvm_host_free (hosts->hosts[i]);
1711 hosts->hosts[i] = NULL;
1712 excluded++;
1713 removed = g_slist_prepend (removed, name);
1714 continue;
1715 }
1716 g_free (name);
1717 }
1718 }
1719
1720 /* Cleanup. */
1721 if (excluded)
1722 gvm_hosts_fill_gaps (hosts);
1723
1724 hosts->count -= excluded;
1725 hosts->removed += excluded;
1726 hosts->current = 0;
1727 if (name_allow_table != NULL)
1728 g_hash_table_destroy (name_allow_table);
1729 if (name_deny_table != NULL)
1730 g_hash_table_destroy (name_deny_table);
1731 if (allowed_hosts != NULL)
1732 gvm_hosts_free (allowed_hosts);
1733 if (denied_hosts != NULL)
1734 gvm_hosts_free (denied_hosts);
1735 return removed;
1736}
void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:941
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:1099
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:1002
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition: hosts.c:2083
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition: hosts.c:1358
The structure for Hosts collection.
Definition: hosts.h:88
size_t removed
Definition: hosts.h:94
size_t current
Definition: hosts.h:92

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 Ensure(), and main().

Here is the call graph for this function:
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 2083 of file hosts.c.

2084{
2085 return hosts ? hosts->count : 0;
2086}

References gvm_hosts::count.

Referenced by Ensure(), gvm_hosts_allowed_only(), and gvm_hosts_exclude_with_max().

Here is the caller graph for this function:

◆ 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]hostsThe hosts collection.
Returns
The number of duplicated values.

Definition at line 2111 of file hosts.c.

2112{
2113 return hosts ? hosts->duplicated : 0;
2114}
size_t duplicated
Definition: hosts.h:95

References gvm_hosts::duplicated.

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

1750{
1751 return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1752}
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:1563

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,out]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 1563 of file hosts.c.

1565{
1569 gvm_hosts_t *excluded_hosts;
1570 GHashTable *name_table;
1571 size_t excluded = 0, i;
1572
1573 if (hosts == NULL || excluded_str == NULL)
1574 return -1;
1575
1576 excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1577 if (excluded_hosts == NULL)
1578 return -1;
1579
1580 if (gvm_hosts_count (excluded_hosts) == 0)
1581 {
1582 gvm_hosts_free (excluded_hosts);
1583 return 0;
1584 }
1585
1586 /* Hash host values from excluded hosts list. */
1587 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1588 for (i = 0; i < excluded_hosts->count; i++)
1589 {
1590 gchar *name;
1591
1592 if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
1593 g_hash_table_insert (name_table, name, hosts);
1594 }
1595
1596 /* Check for hosts values in hash table. */
1597 for (i = 0; i < hosts->count; i++)
1598 {
1599 gchar *name;
1600
1601 if ((name = gvm_host_value_str (hosts->hosts[i])))
1602 {
1603 if (g_hash_table_lookup (name_table, name))
1604 {
1605 gvm_host_free (hosts->hosts[i]);
1606 hosts->hosts[i] = NULL;
1607 excluded++;
1608 g_free (name);
1609 continue;
1610 }
1611 g_free (name);
1612 }
1613 }
1614
1615 /* Cleanup. */
1616 if (excluded)
1617 gvm_hosts_fill_gaps (hosts);
1618 hosts->count -= excluded;
1619 hosts->removed += excluded;
1620 hosts->current = 0;
1621 g_hash_table_destroy (name_table);
1622 gvm_hosts_free (excluded_hosts);
1623 return excluded;
1624}

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

Referenced by gvm_hosts_exclude().

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

◆ gvm_hosts_free()

void gvm_hosts_free ( gvm_hosts_t hosts)

Frees memory occupied by an gvm_hosts_t structure.

Parameters
[in]hostsThe hosts collection to free.

Definition at line 1358 of file hosts.c.

1359{
1360 size_t i;
1361
1362 if (hosts == NULL)
1363 return;
1364
1365 if (hosts->orig_str)
1366 g_free (hosts->orig_str);
1367 for (i = 0; i < hosts->count; i++)
1368 gvm_host_free (hosts->hosts[i]);
1369 g_free (hosts->hosts);
1370 g_free (hosts);
1371 hosts = NULL;
1372}
gchar * orig_str
Definition: hosts.h:89

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

Referenced by Ensure(), gvm_hosts_allowed_only(), gvm_hosts_exclude_with_max(), gvm_hosts_new_with_max(), 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_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]hostsgvm_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.

Definition at line 1328 of file hosts.c.

1329{
1330 void *host_tmp;
1331 size_t i;
1332
1333 if (!hosts)
1334 return;
1335
1336 if (hosts->current == hosts->count)
1337 {
1338 hosts->current -= 1;
1339 return;
1340 }
1341
1342 hosts->current -= 1;
1343 host_tmp = hosts->hosts[hosts->current];
1344
1345 for (i = hosts->current; i < hosts->count; i++)
1346 hosts->hosts[i - 1] = hosts->hosts[i];
1347
1348 hosts->hosts[hosts->count - 1] = host_tmp;
1349}

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

Referenced by Ensure().

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

1297{
1298 return gvm_hosts_new_with_max (hosts_str, 0);
1299}

References gvm_hosts_new_with_max().

Referenced by Ensure(), gvm_hosts_reverse_lookup_only_excluded(), and gvm_hosts_reverse_lookup_unify_excluded().

Here is the call graph for this function:
Here is the caller 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 1099 of file hosts.c.

1100{
1101 gvm_hosts_t *hosts;
1102 gchar **host_element, **split;
1103 gchar *str;
1104
1105 if (hosts_str == NULL)
1106 return NULL;
1107
1108 /* Normalize separator: Transform newlines into commas. */
1109 hosts = gvm_hosts_init (hosts_str);
1110 str = hosts->orig_str;
1111 while (*str)
1112 {
1113 if (*str == '\n')
1114 *str = ',';
1115 str++;
1116 }
1117
1118 /* Split comma-separated list into single host-specifications */
1119 split = g_strsplit (hosts->orig_str, ",", 0);
1120
1121 /* first element of the split list */
1122 host_element = split;
1123 while (*host_element)
1124 {
1125 int host_type;
1126 gchar *stripped = g_strstrip (*host_element);
1127
1128 if (stripped == NULL || *stripped == '\0')
1129 {
1130 host_element++;
1131 continue;
1132 }
1133
1134 /* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ?
1135 */
1136 /* -1 if error. */
1137 host_type = gvm_get_host_type (stripped);
1138
1139 switch (host_type)
1140 {
1141 case HOST_TYPE_NAME:
1142 case HOST_TYPE_IPV4:
1143 case HOST_TYPE_IPV6:
1144 {
1145 /* New host. */
1146 gvm_host_t *host = gvm_host_new ();
1147 host->type = host_type;
1149 host->name = g_ascii_strdown (stripped, -1);
1150 else if (host_type == HOST_TYPE_IPV4)
1151 {
1152 if (inet_pton (AF_INET, stripped, &host->addr) != 1)
1153 break;
1154 }
1155 else if (host_type == HOST_TYPE_IPV6)
1156 {
1157 if (inet_pton (AF_INET6, stripped, &host->addr6) != 1)
1158 break;
1159 }
1160 gvm_hosts_add (hosts, host);
1161 break;
1162 }
1166 {
1167 struct in_addr first, last;
1168 uint32_t current;
1169 int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
1170
1172 ips_func = cidr_block_ips;
1173 else if (host_type == HOST_TYPE_RANGE_SHORT)
1174 ips_func = short_range_network_ips;
1175 else
1176 ips_func = long_range_network_ips;
1177
1178 if (ips_func (stripped, &first, &last) == -1)
1179 break;
1180
1181 /* Make sure that first actually comes before last */
1182 if (ntohl (first.s_addr) > ntohl (last.s_addr))
1183 break;
1184
1185 /* Add addresses from first to last as single hosts. */
1186 current = first.s_addr;
1187 while (ntohl (current) <= ntohl (last.s_addr))
1188 {
1189 gvm_host_t *host;
1190 if (max_hosts > 0 && hosts->count > max_hosts)
1191 {
1192 g_strfreev (split);
1193 gvm_hosts_free (hosts);
1194 return NULL;
1195 }
1196 host = gvm_host_new ();
1197 host->type = HOST_TYPE_IPV4;
1198 host->addr.s_addr = current;
1199 gvm_hosts_add (hosts, host);
1200 /* Next IP address. */
1201 current = htonl (ntohl (current) + 1);
1202 }
1203 break;
1204 }
1208 {
1209 struct in6_addr first, last;
1210 unsigned char current[16];
1211 int (*ips_func) (const char *, struct in6_addr *,
1212 struct in6_addr *);
1213
1215 ips_func = cidr6_block_ips;
1217 ips_func = short_range6_network_ips;
1218 else
1219 ips_func = long_range6_network_ips;
1220
1221 if (ips_func (stripped, &first, &last) == -1)
1222 break;
1223
1224 /* Make sure the first comes before the last. */
1225 if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1226 break;
1227
1228 /* Add addresses from first to last as single hosts. */
1229 memcpy (current, &first.s6_addr, 16);
1230 while (memcmp (current, &last.s6_addr, 16) <= 0)
1231 {
1232 int i;
1233 gvm_host_t *host;
1234
1235 if (max_hosts > 0 && hosts->count > max_hosts)
1236 {
1237 g_strfreev (split);
1238 gvm_hosts_free (hosts);
1239 return NULL;
1240 }
1241 host = gvm_host_new ();
1242 host->type = HOST_TYPE_IPV6;
1243 memcpy (host->addr6.s6_addr, current, 16);
1244 gvm_hosts_add (hosts, host);
1245 /* Next IPv6 address. */
1246 for (i = 15; i >= 0; --i)
1247 if (current[i] < 255)
1248 {
1249 current[i]++;
1250 break;
1251 }
1252 else
1253 current[i] = 0;
1254 }
1255 break;
1256 }
1257 case -1:
1258 default:
1259 /* Invalid host string. */
1260 g_strfreev (split);
1261 gvm_hosts_free (hosts);
1262 return NULL;
1263 }
1264 host_element++; /* move on to next element of split list */
1265 if (max_hosts > 0 && hosts->count > max_hosts)
1266 {
1267 g_strfreev (split);
1268 gvm_hosts_free (hosts);
1269 return NULL;
1270 }
1271 }
1272
1273 /* No need to check for duplicates when a hosts string contains a
1274 * single (IP/Hostname/Range/Subnetwork) entry. */
1275 if (g_strv_length (split) > 1)
1276 gvm_hosts_deduplicate (hosts);
1277
1278 g_strfreev (split);
1279#ifdef __GLIBC__
1280 malloc_trim (0);
1281#endif
1282 return hosts;
1283}
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:962
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:352
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:264
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:1040
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:764
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:569
static gvm_hosts_t * gvm_hosts_init(const char *hosts_str)
Creates a hosts collection from a hosts string.
Definition: hosts.c:984
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:676
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:198

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 Ensure(), gvm_hosts_allowed_only(), 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 1310 of file hosts.c.

1311{
1312 if (!hosts || hosts->current == hosts->count)
1313 return NULL;
1314
1315 return hosts->hosts[hosts->current++];
1316}

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

Referenced by Ensure().

Here is the caller graph for this function:

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

2098{
2099 return hosts ? hosts->removed : 0;
2100}

References gvm_hosts::removed.

◆ gvm_hosts_resolve()

GSList * gvm_hosts_resolve ( gvm_hosts_t hosts)

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

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

Definition at line 1440 of file hosts.c.

1441{
1442 size_t i, new_entries = 0, resolved = 0;
1443 GSList *unresolved = NULL;
1444
1445 for (i = 0; i < hosts->count; i++)
1446 {
1447 GSList *list, *tmp;
1448 gvm_host_t *host = hosts->hosts[i];
1449
1450 if (host->type != HOST_TYPE_NAME)
1451 continue;
1452
1453 list = tmp = gvm_resolve_list (host->name);
1454 while (tmp)
1455 {
1456 /* Create a new host for each IP address. */
1457 gvm_host_t *new;
1458 struct in6_addr *ip6 = tmp->data;
1459 gvm_vhost_t *vhost;
1460
1461 new = gvm_host_new ();
1462 if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1463 || ip6->s6_addr32[2] != htonl (0xffff))
1464 {
1465 new->type = HOST_TYPE_IPV6;
1466 memcpy (&new->addr6, ip6, sizeof (new->addr6));
1467 }
1468 else
1469 {
1470 new->type = HOST_TYPE_IPV4;
1471 memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1472 }
1473 vhost =
1474 gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1475 new->vhosts = g_slist_prepend (new->vhosts, vhost);
1476 gvm_hosts_add (hosts, new);
1477 tmp = tmp->next;
1478 new_entries = 1;
1479 }
1480 /* Remove hostname from list, as it was either replaced by IPs, or
1481 * is unresolvable. */
1482 hosts->hosts[i] = NULL;
1483 resolved++;
1484 if (!list)
1485 unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
1486 gvm_host_free (host);
1487 g_slist_free_full (list, g_free);
1488 }
1489 if (resolved)
1490 gvm_hosts_fill_gaps (hosts);
1491 hosts->count -= resolved;
1492 hosts->removed += resolved;
1493 if (new_entries)
1494 gvm_hosts_deduplicate (hosts);
1495 hosts->current = 0;
1496 return unresolved;
1497}
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition: networking.c:339

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

1415{
1416 size_t i, j;
1417 if (hosts == NULL)
1418 return;
1419
1420 for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1421 {
1422 gvm_host_t *tmp = hosts->hosts[i];
1423 hosts->hosts[i] = hosts->hosts[j];
1424 hosts->hosts[j] = tmp;
1425 }
1426 hosts->current = 0;
1427}

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

1983{
1984 gvm_hosts_t *excluded;
1985 int count = 0;
1986
1987 if (hosts == NULL)
1988 return -1;
1989
1990 excluded = gvm_hosts_reverse_lookup_only_excluded (hosts);
1991 count = excluded->count;
1992 gvm_hosts_free (excluded);
1993
1994 return count;
1995}
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 ove...
Definition: hosts.c:1941

References gvm_hosts::count, gvm_hosts_free(), and gvm_hosts_reverse_lookup_only_excluded().

Here is the call graph for this function:

◆ 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]hostsThe hosts collection to filter.
Returns
list of hosts removed, Null on error.

Definition at line 1941 of file hosts.c.

1942{
1943 size_t i, count = 0;
1944 gvm_hosts_t *excluded = gvm_hosts_new ("");
1945
1946 if (hosts == NULL)
1947 return NULL;
1948
1949 for (i = 0; i < hosts->count; i++)
1950 {
1951 gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1952
1953 if (name == NULL)
1954 {
1955 gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
1956 gvm_host_free (hosts->hosts[i]);
1957 hosts->hosts[i] = NULL;
1958 count++;
1959 }
1960 else
1961 g_free (name);
1962 }
1963
1964 if (count)
1965 gvm_hosts_fill_gaps (hosts);
1966 hosts->count -= count;
1967 hosts->removed += count;
1968 hosts->current = 0;
1969 return excluded;
1970}
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.
Definition: hosts.c:2176
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.
Definition: hosts.c:1296

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

Referenced by gvm_hosts_reverse_lookup_only().

Here is the call graph for this function:
Here is the caller 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.

Definition at line 2062 of file hosts.c.

2063{
2064 gvm_hosts_t *excluded = NULL;
2065 int count = 0;
2066 if (hosts == NULL)
2067 return -1;
2068
2069 excluded = gvm_hosts_reverse_lookup_unify_excluded (hosts);
2070 count = excluded->count;
2071 gvm_hosts_free (excluded);
2072
2073 return count;
2074}
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 t...
Definition: hosts.c:2007

References gvm_hosts::count, gvm_hosts_free(), and gvm_hosts_reverse_lookup_unify_excluded().

Here is the call graph for this function:

◆ 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]hostsThe 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.

Definition at line 2007 of file hosts.c.

2008{
2012 size_t i, count = 0;
2013 GHashTable *name_table;
2014 gvm_hosts_t *excluded = NULL;
2015
2016 if (hosts == NULL)
2017 return NULL;
2018
2019 excluded = gvm_hosts_new ("");
2020 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
2021 for (i = 0; i < hosts->count; i++)
2022 {
2023 gchar *name;
2024
2025 if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
2026 {
2027 if (g_hash_table_lookup (name_table, name))
2028 {
2029 gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
2030 gvm_host_free (hosts->hosts[i]);
2031 hosts->hosts[i] = NULL;
2032 count++;
2033 g_free (name);
2034 }
2035 else
2036 {
2037 /* Insert in the hash table. Value not important. */
2038 g_hash_table_insert (name_table, name, hosts);
2039 }
2040 }
2041 }
2042
2043 if (count)
2044 gvm_hosts_fill_gaps (hosts);
2045 g_hash_table_destroy (name_table);
2046 hosts->removed += count;
2047 hosts->count -= count;
2048 hosts->current = 0;
2049 return excluded;
2050}

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

Referenced by gvm_hosts_reverse_lookup_unify().

Here is the call graph for this function:
Here is the caller 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 1382 of file hosts.c.

1383{
1384 size_t i = 0;
1385 GRand *rand;
1386
1387 if (hosts == NULL)
1388 return;
1389
1390 /* Shuffle the array. */
1391 rand = g_rand_new ();
1392 for (i = 0; i < hosts->count; i++)
1393 {
1394 void *tmp;
1395 int j = g_rand_int_range (rand, 0, hosts->count);
1396
1397 tmp = hosts->hosts[i];
1398 hosts->hosts[i] = hosts->hosts[j];
1399 hosts->hosts[j] = tmp;
1400 }
1401
1402 hosts->current = 0;
1403 g_rand_free (rand);
1404}

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

◆ gvm_vhost_new()

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

Creates a new gvm_vhost_t object.

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

Definition at line 871 of file hosts.c.

872{
873 gvm_vhost_t *vhost;
874
875 vhost = g_malloc0 (sizeof (gvm_vhost_t));
876 vhost->value = value;
877 vhost->source = source;
878
879 return vhost;
880}
char * value
Definition: hosts.h:77
char * source
Definition: hosts.h:78

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

Referenced by gvm_duplicate_vhost(), 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 1508 of file hosts.c.

1509{
1510 GSList *vhost;
1511 char **excluded;
1512 int ret = 0;
1513
1514 if (!host || !excluded_str)
1515 return ret;
1516
1517 vhost = host->vhosts;
1518 excluded = g_strsplit (excluded_str, ",", 0);
1519 if (!excluded || !*excluded)
1520 {
1521 g_strfreev (excluded);
1522 return ret;
1523 }
1524 while (vhost)
1525 {
1526 char **tmp = excluded;
1527 char *value = ((gvm_vhost_t *) vhost->data)->value;
1528
1529 while (*tmp)
1530 {
1531 if (!strcasecmp (value, g_strstrip (*tmp)))
1532 {
1533 gvm_vhost_free (vhost->data);
1534 host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1535 ret++;
1536 break;
1537 }
1538 tmp++;
1539 if (!*tmp)
1540 {
1541 vhost = vhost->next;
1542 break;
1543 }
1544 }
1545 }
1546 g_strfreev (excluded);
1547
1548 return ret;
1549}

References gvm_vhost_free(), and gvm_host::vhosts.

Here is the call graph for this function: