Greenbone Vulnerability Management Libraries 22.40.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.
int gvm_is_cidr6_block (const char *)
 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.
int gvm_cidr6_get_block (const char *, unsigned int *)
 Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
int gvm_cidr6_get_ip (const char *, struct in6_addr *)
 Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".
int gvm_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.

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

int gvm_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.

Parameters
[in]strBuffer containing CIDR-expressed block.
[out]firstFirst IPv4 address in block.
[out]lastLast IPv4 address in block.
Returns
-1 if error, 0 else.

Definition at line 572 of file hosts.c.

574{
575 unsigned int block;
576 int i, j;
577
578 if (str == NULL || first == NULL || last == NULL)
579 return -1;
580
581 /* Get IP and block values. */
582 if (gvm_cidr6_get_block (str, &block) == -1)
583 return -1;
584 if (gvm_cidr6_get_ip (str, first) == -1)
585 return -1;
586 memcpy (&last->s6_addr, &first->s6_addr, 16);
587
588 /* /128 => Specified address is the first and last one. */
589 if (block == 128)
590 return 0;
591
592 /* First IP: And with mask and increment to skip network address. */
593 j = 15;
594 for (i = (128 - block) / 8; i > 0; i--)
595 {
596 first->s6_addr[j] = 0;
597 j--;
598 }
599 first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
600
601 /* Last IP: Broadcast address - 1. */
602 j = 15;
603 for (i = (128 - block) / 8; i > 0; i--)
604 {
605 last->s6_addr[j] = 0xff;
606 j--;
607 }
608 last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
609
610 /* /127 => Only two addresses. Don't skip network / broadcast addresses.*/
611 if (block == 127)
612 return 0;
613
614 /* Increment first IP. */
615 for (i = 15; i >= 0; --i)
616 if (first->s6_addr[i] < 255)
617 {
618 first->s6_addr[i]++;
619 break;
620 }
621 else
622 first->s6_addr[i] = 0;
623 /* Decrement last IP. */
624 for (i = 15; i >= 0; --i)
625 if (last->s6_addr[i] > 0)
626 {
627 last->s6_addr[i]--;
628 break;
629 }
630 else
631 last->s6_addr[i] = 0xff;
632
633 return 0;
634}
int gvm_cidr6_get_block(const char *str, unsigned int *block)
Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
Definition hosts.c:516
int gvm_cidr6_get_ip(const char *str, struct in6_addr *addr6)
Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192....
Definition hosts.c:537

References gvm_cidr6_get_block(), and gvm_cidr6_get_ip().

Referenced by gvm_hosts_new_with_max().

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

◆ gvm_cidr6_get_block()

int gvm_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.

Parameters
[in]strBuffer containing CIDR-expressed block.
[out]blockVariable to store block value.
Returns
-1 if error, 0 otherwise.

Definition at line 516 of file hosts.c.

517{
518 if (str == NULL || block == NULL)
519 return -1;
520
521 if (sscanf (str, "%*[0-9a-fA-F.:]/%3u", block) != 1)
522 return -1;
523
524 return 0;
525}

Referenced by gvm_cidr6_block_ips().

Here is the caller graph for this function:

◆ gvm_cidr6_get_ip()

int gvm_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".

Parameters
[in]strString containing CIDR-expressed block.
[out]addr6Variable to store the IPv4 address value.
Returns
-1 if error, 0 otherwise.

Definition at line 537 of file hosts.c.

538{
539 gchar *addr6_str, *tmp;
540
541 if (str == NULL || addr6 == NULL)
542 return -1;
543
544 addr6_str = g_strdup (str);
545 tmp = strchr (addr6_str, '/');
546 if (tmp == NULL)
547 {
548 g_free (addr6_str);
549 return -1;
550 }
551 *tmp = '\0';
552
553 if (inet_pton (AF_INET6, addr6_str, addr6) != 1)
554 return -1;
555
556 g_free (addr6_str);
557 return 0;
558}

Referenced by gvm_cidr6_block_ips().

Here is the caller graph for this function:

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

2200{
2201 gvm_host_t *ret = NULL;
2202
2203 if (host == NULL)
2204 return NULL;
2205
2206 ret = gvm_host_new ();
2207
2208 ret->type = host->type;
2209 switch (host->type)
2210 {
2211 case HOST_TYPE_NAME:
2212 ret->name = g_strdup (host->name);
2213 break;
2214 case HOST_TYPE_IPV4:
2215 ret->addr.s_addr = host->addr.s_addr;
2216 break;
2217 case HOST_TYPE_IPV6:
2218 ret->addr6.__in6_u = host->addr6.__in6_u;
2219 break;
2220 default:
2221 g_free (ret);
2222 return NULL;
2223 }
2224 ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);
2225
2226 return ret;
2227}
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition hosts.c:930
gpointer gvm_duplicate_vhost(gconstpointer vhost, gpointer data)
Creates a deep copy of a gvm_vhost_t object.
Definition hosts.c:910
struct gvm_host gvm_host_t
Definition hosts.h:48
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 910 of file hosts.c.

911{
912 (void) (data);
913 gvm_vhost_t *ret = NULL;
914
915 if (!vhost)
916 return NULL;
917
918 ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
919 g_strdup (((gvm_vhost_t *) vhost)->source));
920
921 return ret;
922}
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition hosts.c:875
struct gvm_vhost gvm_vhost_t
Definition hosts.h:49

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

815{
816 /*
817 * We have a single element with no leading or trailing
818 * white spaces. This element could represent different host
819 * definitions: single IPs, host names, CIDR-expressed blocks,
820 * range-expressed networks, IPv6 addresses.
821 */
822
823 /* Null or empty string. */
824 if (str_stripped == NULL || *str_stripped == '\0')
825 return -1;
826
827 /* Check for regular single IPv4 address. */
828 if (is_ipv4_address (str_stripped))
829 return HOST_TYPE_IPV4;
830
831 /* Check for regular single IPv6 address. */
832 if (is_ipv6_address (str_stripped))
833 return HOST_TYPE_IPV6;
834
835 /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
836 if (is_cidr_block (str_stripped))
838
839 /* Check for short range-expressed networks "192.168.12.5-40" */
840 if (is_short_range_network (str_stripped))
842
843 /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
844 if (is_long_range_network (str_stripped))
846
847 /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
848 if (gvm_is_cidr6_block (str_stripped))
850
851 /* Check for short range-expressed networks "::1-ef12" */
852 if (is_short_range6_network (str_stripped))
854
855 /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
856 if (is_long_range6_network (str_stripped))
858
859 /* Check for hostname. */
860 if (is_hostname (str_stripped))
861 return HOST_TYPE_NAME;
862
863 return -1;
864}
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:645
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_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:720
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
int gvm_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:470

References gvm_is_cidr6_block(), 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_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(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), Ensure(), 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 1921 of file hosts.c.

1922{
1923 GSList *vhosts;
1924 gvm_vhost_t *vhost;
1925 char *value;
1926
1927 if (!host || host->type == HOST_TYPE_NAME)
1928 return;
1929
1930 value = gvm_host_reverse_lookup (host);
1931 if (!value)
1932 return;
1933 if (host_name_verify (host, value))
1934 {
1935 g_free (value);
1936 return;
1937 }
1938 /* Don't add vhost, if already in the list. */
1939 vhosts = host->vhosts;
1940 while (vhosts)
1941 {
1942 if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1943 {
1944 g_free (value);
1945 return;
1946 }
1947 vhosts = vhosts->next;
1948 }
1949 vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1950 host->vhosts = g_slist_prepend (host->vhosts, vhost);
1951}
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition hosts.c:1836
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:1889

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

2152{
2153 char *host_str;
2154 size_t i;
2155
2156 if (host == NULL || hosts == NULL)
2157 return NULL;
2158
2159 host_str = gvm_host_value_str (host);
2160
2161 for (i = 0; i < hosts->count; i++)
2162 {
2163 gvm_host_t *current_host = hosts->hosts[i];
2164 char *tmp = gvm_host_value_str (current_host);
2165
2166 if (strcasecmp (host_str, tmp) == 0)
2167 {
2168 g_free (host_str);
2169 g_free (tmp);
2170 return current_host;
2171 }
2172 g_free (tmp);
2173
2174 /* Hostnames in hosts list shouldn't be resolved. */
2175 if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
2176 {
2177 struct in6_addr tmpaddr;
2178 gvm_host_get_addr6 (current_host, &tmpaddr);
2179
2180 if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
2181 {
2182 g_free (host_str);
2183 return current_host;
2184 }
2185 }
2186 }
2187
2188 g_free (host_str);
2189 return NULL;
2190}
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition hosts.c:2290
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:2368
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition hosts.c:2259
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 945 of file hosts.c.

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

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

1788{
1789 int host_type;
1790
1791 if (host_str == NULL)
1792 return NULL;
1793
1794 /* IPv4, hostname, IPv6 */
1795 /* -1 if error. */
1796 host_type = gvm_get_host_type (host_str);
1797
1798 switch (host_type)
1799 {
1800 case HOST_TYPE_NAME:
1801 case HOST_TYPE_IPV4:
1802 case HOST_TYPE_IPV6:
1803 {
1804 /* New host. */
1805 gvm_host_t *host = gvm_host_new ();
1806 host->type = host_type;
1808 host->name = g_ascii_strdown (host_str, -1);
1809 else if (host_type == HOST_TYPE_IPV4)
1810 {
1811 if (inet_pton (AF_INET, host_str, &host->addr) != 1)
1812 break;
1813 }
1814 else if (host_type == HOST_TYPE_IPV6)
1815 {
1816 if (inet_pton (AF_INET6, host_str, &host->addr6) != 1)
1817 break;
1818 }
1819 return host;
1820 }
1821 case -1:
1822 default:
1823 return NULL;
1824 }
1825 return NULL;
1826}
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition hosts.c:814
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 2368 of file hosts.c.

2369{
2370 if (host == NULL || ip6 == NULL)
2371 return -1;
2372
2373 switch (gvm_host_type (host))
2374 {
2375 case HOST_TYPE_IPV6:
2376 memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
2377 return 0;
2378
2379 case HOST_TYPE_IPV4:
2380 ipv4_as_ipv6 (&host->addr, ip6);
2381 return 0;
2382
2383 case HOST_TYPE_NAME:
2384 {
2385 struct in_addr ip4;
2386
2387 /* Fail if IPv4 and IPv6 both don't resolve. */
2388 if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
2389 ipv4_as_ipv6 (&ip4, ip6);
2390 else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
2391 return -1;
2392 return 0;
2393 }
2394
2395 default:
2396 return -1;
2397 }
2398}
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:2347
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 2242 of file hosts.c.

2244{
2245 if (gvm_host_find_in_hosts (host, addr, hosts))
2246 return 1;
2247
2248 return 0;
2249}
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:2150

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

2348{
2349 if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
2350 return -1;
2351
2352 return gvm_resolve (host->name, dst, family);
2353}
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 1836 of file hosts.c.

1837{
1838 int retry = 10;
1839 gchar hostname[NI_MAXHOST];
1840 void *addr;
1841 size_t addrlen;
1842 struct sockaddr_in sa;
1843 struct sockaddr_in6 sa6;
1844
1845 if (!host)
1846 return NULL;
1847
1848 if (host->type == HOST_TYPE_IPV4)
1849 {
1850 addr = &sa;
1851 addrlen = sizeof (sa);
1852 memset (addr, '\0', addrlen);
1853 sa.sin_addr = host->addr;
1854 sa.sin_family = AF_INET;
1855 }
1856 else if (host->type == HOST_TYPE_IPV6)
1857 {
1858 addr = &sa6;
1859 addrlen = sizeof (sa6);
1860 memset (&sa6, '\0', addrlen);
1861 memcpy (&sa6.sin6_addr, &host->addr6, 16);
1862 sa6.sin6_family = AF_INET6;
1863 }
1864 else
1865 return NULL;
1866
1867 while (retry--)
1868 {
1869 int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
1870 0, NI_NAMEREQD);
1871 if (!ret)
1872 return g_ascii_strdown (hostname, -1);
1873 if (ret != EAI_AGAIN)
1874 break;
1875 usleep (10000); // 10ms
1876 }
1877 return NULL;
1878}

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

2260{
2261 assert (host);
2262 return host->type;
2263}

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

2275{
2276 if (host == NULL)
2277 return NULL;
2278
2279 return host_type_str[host->type];
2280}
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 2290 of file hosts.c.

2291{
2292 if (host == NULL)
2293 return NULL;
2294
2295 switch (host->type)
2296 {
2297 case HOST_TYPE_NAME:
2298 return g_strdup (host->name);
2299 break;
2300 case HOST_TYPE_IPV4:
2301 case HOST_TYPE_IPV6:
2302 /* Handle both cases using inet_ntop(). */
2303 {
2304 int family, size;
2305 gchar *str;
2306 const void *srcaddr;
2307
2308 if (host->type == HOST_TYPE_IPV4)
2309 {
2310 family = AF_INET;
2311 size = INET_ADDRSTRLEN;
2312 srcaddr = &host->addr;
2313 }
2314 else
2315 {
2316 family = AF_INET6;
2317 size = INET6_ADDRSTRLEN;
2318 srcaddr = &host->addr6;
2319 }
2320
2321 str = g_malloc0 (size);
2322 if (inet_ntop (family, srcaddr, str, size) == NULL)
2323 {
2324 perror ("inet_ntop");
2325 g_free (str);
2326 return NULL;
2327 }
2328 return str;
2329 }
2330 default:
2331 return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
2332 }
2333}

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(), host_name_verify(), and host_value_eq().

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

967{
968 if (hosts->count == hosts->max_size)
969 {
970 hosts->max_size *= 4;
971 hosts->hosts =
972 g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
973 memset (hosts->hosts + hosts->count, '\0',
974 (hosts->max_size - hosts->count) * sizeof (gvm_host_t *));
975 }
976 hosts->hosts[hosts->count] = host;
977 hosts->count++;
978}
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 1658 of file hosts.c.

1660{
1664 gvm_hosts_t *allowed_hosts, *denied_hosts;
1665 GHashTable *name_allow_table = NULL, *name_deny_table = NULL;
1666 GSList *removed = NULL;
1667 size_t excluded = 0, i;
1668
1669 if (hosts == NULL || (deny_hosts_str == NULL && allow_hosts_str == NULL))
1670 return NULL;
1671
1672 // Prepare list of denied and allowed hosts
1673 denied_hosts = gvm_hosts_new_with_max (deny_hosts_str, 0);
1674 allowed_hosts = gvm_hosts_new_with_max (allow_hosts_str, 0);
1675 if (denied_hosts == NULL && allowed_hosts == NULL)
1676 return NULL;
1677
1678 if (gvm_hosts_count (denied_hosts) == 0)
1679 gvm_hosts_free (denied_hosts);
1680 else
1681 {
1682 /* Hash host values from denied hosts list. */
1683 name_deny_table =
1684 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1685 for (i = 0; i < denied_hosts->count; i++)
1686 {
1687 gchar *name;
1688
1689 name = gvm_host_value_str (denied_hosts->hosts[i]);
1690 if (name)
1691 g_hash_table_insert (name_deny_table, name, hosts);
1692 }
1693 }
1694 if (gvm_hosts_count (allowed_hosts) == 0)
1695 gvm_hosts_free (allowed_hosts);
1696 else
1697 {
1698 /* Hash host values from allowed hosts list. */
1699 name_allow_table =
1700 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1701 for (i = 0; i < allowed_hosts->count; i++)
1702 {
1703 gchar *name;
1704
1705 name = gvm_host_value_str (allowed_hosts->hosts[i]);
1706 if (name)
1707 g_hash_table_insert (name_allow_table, name, hosts);
1708 }
1709 }
1710
1711 /* Check for authorized hosts in hash table and create a list of removed
1712 * hosts. */
1713 for (i = 0; i < hosts->count; i++)
1714 {
1715 gchar *name;
1716
1717 name = gvm_host_value_str (hosts->hosts[i]);
1718 if (name)
1719 {
1720 if (denied_hosts != NULL
1721 && g_hash_table_lookup (name_deny_table, name))
1722 {
1723 gvm_host_free (hosts->hosts[i]);
1724 hosts->hosts[i] = NULL;
1725 excluded++;
1726 removed = g_slist_prepend (removed, name);
1727 continue;
1728 }
1729 else if (allowed_hosts != NULL
1730 && !g_hash_table_lookup (name_allow_table, name))
1731 {
1732 gvm_host_free (hosts->hosts[i]);
1733 hosts->hosts[i] = NULL;
1734 excluded++;
1735 removed = g_slist_prepend (removed, name);
1736 continue;
1737 }
1738 g_free (name);
1739 }
1740 }
1741
1742 /* Cleanup. */
1743 if (excluded)
1744 gvm_hosts_fill_gaps (hosts);
1745
1746 hosts->count -= excluded;
1747 hosts->removed += excluded;
1748 hosts->current = 0;
1749 if (name_allow_table != NULL)
1750 g_hash_table_destroy (name_allow_table);
1751 if (name_deny_table != NULL)
1752 g_hash_table_destroy (name_deny_table);
1753 if (allowed_hosts != NULL)
1754 gvm_hosts_free (allowed_hosts);
1755 if (denied_hosts != NULL)
1756 gvm_hosts_free (denied_hosts);
1757 return removed;
1758}
void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition hosts.c:945
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:1104
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:1006
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition hosts.c:2106
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition hosts.c:1376
struct gvm_hosts gvm_hosts_t
Definition hosts.h:50
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 2106 of file hosts.c.

2107{
2108 return hosts ? hosts->count : 0;
2109}

References gvm_hosts::count.

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

2135{
2136 return hosts ? hosts->duplicated : 0;
2137}
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 1771 of file hosts.c.

1772{
1773 return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1774}
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:1580

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

1582{
1586 gvm_hosts_t *excluded_hosts;
1587 GHashTable *name_table;
1588 size_t excluded = 0, i;
1589
1590 if (hosts == NULL || excluded_str == NULL)
1591 return -1;
1592
1593 excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1594 if (excluded_hosts == NULL)
1595 return -1;
1596
1597 if (gvm_hosts_count (excluded_hosts) == 0)
1598 {
1599 gvm_hosts_free (excluded_hosts);
1600 return 0;
1601 }
1602
1603 /* Hash host values from excluded hosts list. */
1604 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1605 for (i = 0; i < excluded_hosts->count; i++)
1606 {
1607 gchar *name;
1608
1609 name = gvm_host_value_str (excluded_hosts->hosts[i]);
1610 if (name)
1611 g_hash_table_insert (name_table, name, hosts);
1612 }
1613
1614 /* Check for hosts values in hash table. */
1615 for (i = 0; i < hosts->count; i++)
1616 {
1617 gchar *name;
1618
1619 name = gvm_host_value_str (hosts->hosts[i]);
1620 if (name)
1621 {
1622 if (g_hash_table_lookup (name_table, name))
1623 {
1624 gvm_host_free (hosts->hosts[i]);
1625 hosts->hosts[i] = NULL;
1626 excluded++;
1627 g_free (name);
1628 continue;
1629 }
1630 g_free (name);
1631 }
1632 }
1633
1634 /* Cleanup. */
1635 if (excluded)
1636 gvm_hosts_fill_gaps (hosts);
1637 hosts->count -= excluded;
1638 hosts->removed += excluded;
1639 hosts->current = 0;
1640 g_hash_table_destroy (name_table);
1641 gvm_hosts_free (excluded_hosts);
1642 return excluded;
1643}

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

1377{
1378 size_t i;
1379
1380 if (hosts == NULL)
1381 return;
1382
1383 g_free (hosts->orig_str);
1384 for (i = 0; i < hosts->count; i++)
1385 gvm_host_free (hosts->hosts[i]);
1386 g_free (hosts->hosts);
1387 g_free (hosts);
1388 hosts = NULL;
1389}
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(), Ensure(), Ensure(), 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 host and gvm_hosts_next() must be called to get the next host in the list.

Definition at line 1333 of file hosts.c.

1334{
1335 void *host_tmp;
1336 size_t i;
1337
1338 if (!hosts)
1339 return;
1340
1341 // Keep in mind that gvm_hosts_next will return the current host and then
1342 // increment hosts->current.
1343
1344 if (hosts->current == hosts->count)
1345 {
1346 // We're on the last host, just make the previous host current.
1347 // TODO what happens when current is 0?
1348 hosts->current -= 1;
1349 return;
1350 }
1351
1352 // Make the previous host current. This makes sure that gvm_hosts_next will
1353 // return the host that has replaced the current host.
1354 // TODO what happens when current is 0?
1355 hosts->current -= 1;
1356 // Get the host to be moved.
1357 host_tmp = hosts->hosts[hosts->current];
1358
1359 // Shift all the others down. Start from current + 1 because we're assigning
1360 // into the previous slot (i - 1). It's safe to do this because we already
1361 // checked if current == count above.
1362 for (i = hosts->current + 1; i < hosts->count; i++)
1363 hosts->hosts[i - 1] = hosts->hosts[i];
1364
1365 // Put the moved host on the end.
1366 hosts->hosts[hosts->count - 1] = host_tmp;
1367}

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

1302{
1303 return gvm_hosts_new_with_max (hosts_str, 0);
1304}

References gvm_hosts_new_with_max().

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

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

1316{
1317 if (!hosts || hosts->current == hosts->count)
1318 return NULL;
1319
1320 return hosts->hosts[hosts->current++];
1321}

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

Referenced by Ensure(), and 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 2120 of file hosts.c.

2121{
2122 return hosts ? hosts->removed : 0;
2123}

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

1458{
1459 size_t i, new_entries = 0, resolved = 0;
1460 GSList *unresolved = NULL;
1461
1462 for (i = 0; i < hosts->count; i++)
1463 {
1464 GSList *list, *tmp;
1465 gvm_host_t *host = hosts->hosts[i];
1466
1467 if (host->type != HOST_TYPE_NAME)
1468 continue;
1469
1470 list = tmp = gvm_resolve_list (host->name);
1471 while (tmp)
1472 {
1473 /* Create a new host for each IP address. */
1474 gvm_host_t *new;
1475 struct in6_addr *ip6 = tmp->data;
1476 gvm_vhost_t *vhost;
1477
1478 new = gvm_host_new ();
1479 if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1480 || ip6->s6_addr32[2] != htonl (0xffff))
1481 {
1482 new->type = HOST_TYPE_IPV6;
1483 memcpy (&new->addr6, ip6, sizeof (new->addr6));
1484 }
1485 else
1486 {
1487 new->type = HOST_TYPE_IPV4;
1488 memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1489 }
1490 vhost =
1491 gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1492 new->vhosts = g_slist_prepend (new->vhosts, vhost);
1493 gvm_hosts_add (hosts, new);
1494 tmp = tmp->next;
1495 new_entries = 1;
1496 }
1497 /* Remove hostname from list, as it was either replaced by IPs, or
1498 * is unresolvable. */
1499 hosts->hosts[i] = NULL;
1500 resolved++;
1501 if (!list)
1502 unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
1503 gvm_host_free (host);
1504 g_slist_free_full (list, g_free);
1505 }
1506 if (resolved)
1507 gvm_hosts_fill_gaps (hosts);
1508 hosts->count -= resolved;
1509 hosts->removed += resolved;
1510 if (new_entries)
1511 gvm_hosts_deduplicate (hosts);
1512 hosts->current = 0;
1513 return unresolved;
1514}
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 1431 of file hosts.c.

1432{
1433 size_t i, j;
1434 if (hosts == NULL)
1435 return;
1436
1437 for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1438 {
1439 gvm_host_t *tmp = hosts->hosts[i];
1440 hosts->hosts[i] = hosts->hosts[j];
1441 hosts->hosts[j] = tmp;
1442 }
1443 hosts->current = 0;
1444}

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

2005{
2006 gvm_hosts_t *excluded;
2007 int count = 0;
2008
2009 if (hosts == NULL)
2010 return -1;
2011
2012 excluded = gvm_hosts_reverse_lookup_only_excluded (hosts);
2013 count = excluded->count;
2014 gvm_hosts_free (excluded);
2015
2016 return count;
2017}
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:1963

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

1964{
1965 size_t i, count = 0;
1966 gvm_hosts_t *excluded = gvm_hosts_new ("");
1967
1968 if (hosts == NULL)
1969 return NULL;
1970
1971 for (i = 0; i < hosts->count; i++)
1972 {
1973 gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1974
1975 if (name == NULL)
1976 {
1977 gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
1978 gvm_host_free (hosts->hosts[i]);
1979 hosts->hosts[i] = NULL;
1980 count++;
1981 }
1982 else
1983 g_free (name);
1984 }
1985
1986 if (count)
1987 gvm_hosts_fill_gaps (hosts);
1988 hosts->count -= count;
1989 hosts->removed += count;
1990 hosts->current = 0;
1991 return excluded;
1992}
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:2199
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:1301

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

2086{
2087 gvm_hosts_t *excluded = NULL;
2088 int count = 0;
2089 if (hosts == NULL)
2090 return -1;
2091
2092 excluded = gvm_hosts_reverse_lookup_unify_excluded (hosts);
2093 count = excluded->count;
2094 gvm_hosts_free (excluded);
2095
2096 return count;
2097}
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:2029

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

2030{
2034 size_t i, count = 0;
2035 GHashTable *name_table;
2036 gvm_hosts_t *excluded = NULL;
2037
2038 if (hosts == NULL)
2039 return NULL;
2040
2041 excluded = gvm_hosts_new ("");
2042 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
2043 for (i = 0; i < hosts->count; i++)
2044 {
2045 gchar *name;
2046
2047 name = gvm_host_reverse_lookup (hosts->hosts[i]);
2048 if (name)
2049 {
2050 if (g_hash_table_lookup (name_table, name))
2051 {
2052 gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
2053 gvm_host_free (hosts->hosts[i]);
2054 hosts->hosts[i] = NULL;
2055 count++;
2056 g_free (name);
2057 }
2058 else
2059 {
2060 /* Insert in the hash table. Value not important. */
2061 g_hash_table_insert (name_table, name, hosts);
2062 }
2063 }
2064 }
2065
2066 if (count)
2067 gvm_hosts_fill_gaps (hosts);
2068 g_hash_table_destroy (name_table);
2069 hosts->removed += count;
2070 hosts->count -= count;
2071 hosts->current = 0;
2072 return excluded;
2073}

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

1400{
1401 size_t i = 0;
1402 GRand *rand;
1403
1404 if (hosts == NULL)
1405 return;
1406
1407 /* Shuffle the array. */
1408 rand = g_rand_new ();
1409 for (i = 0; i < hosts->count; i++)
1410 {
1411 void *tmp;
1412 int j = g_rand_int_range (rand, 0, hosts->count);
1413
1414 tmp = hosts->hosts[i];
1415 hosts->hosts[i] = hosts->hosts[j];
1416 hosts->hosts[j] = tmp;
1417 }
1418
1419 hosts->current = 0;
1420 g_rand_free (rand);
1421}

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

◆ gvm_is_cidr6_block()

int gvm_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.

Parameters
[in]strBuffer to check in.
Returns
1 if valid IPv6 CIDR-expressed block, 0 otherwise.

Definition at line 470 of file hosts.c.

471{
472 long block;
473 char *addr6_str, *block_str, *p;
474
475 addr6_str = g_strdup (str);
476 block_str = strchr (addr6_str, '/');
477 if (block_str == NULL)
478 {
479 g_free (addr6_str);
480 return 0;
481 }
482
483 /* Separate the address from the block value. */
484 *block_str = '\0';
485 block_str++;
486
487 if (!is_ipv6_address (addr6_str) || !isdigit (*block_str))
488 {
489 g_free (addr6_str);
490 return 0;
491 }
492
493 p = NULL;
494 block = strtol (block_str, &p, 10);
495
496 if (*p || block <= 0 || block > 128)
497 {
498 g_free (addr6_str);
499 return 0;
500 }
501
502 g_free (addr6_str);
503 return 1;
504}

References is_ipv6_address().

Referenced by gvm_get_host_type().

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

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

876{
877 gvm_vhost_t *vhost;
878
879 vhost = g_malloc0 (sizeof (gvm_vhost_t));
880 vhost->value = value;
881 vhost->source = source;
882
883 return vhost;
884}
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 1525 of file hosts.c.

1526{
1527 GSList *vhost;
1528 char **excluded;
1529 int ret = 0;
1530
1531 if (!host || !excluded_str)
1532 return ret;
1533
1534 vhost = host->vhosts;
1535 excluded = g_strsplit (excluded_str, ",", 0);
1536 if (!excluded || !*excluded)
1537 {
1538 g_strfreev (excluded);
1539 return ret;
1540 }
1541 while (vhost)
1542 {
1543 char **tmp = excluded;
1544 char *value = ((gvm_vhost_t *) vhost->data)->value;
1545
1546 while (*tmp)
1547 {
1548 if (!strcasecmp (value, g_strstrip (*tmp)))
1549 {
1550 gvm_vhost_free (vhost->data);
1551 host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1552 ret++;
1553 break;
1554 }
1555 tmp++;
1556 if (!*tmp)
1557 {
1558 vhost = vhost->next;
1559 break;
1560 }
1561 }
1562 }
1563 g_strfreev (excluded);
1564
1565 return ret;
1566}

References gvm_vhost_free(), and gvm_host::vhosts.

Here is the call graph for this function: