Greenbone Vulnerability Management Libraries 22.35.9
hosts.c File Reference

Implementation of an API to handle Hosts objects. More...

#include "hosts.h"
#include "networking.h"
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <malloc.h>
#include <netdb.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
Include dependency graph for hosts.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "libgvm base"
 GLib log domain.

Functions

static int is_ipv4_address (const char *str)
 Checks if a buffer points to a valid IPv4 address. "192.168.11.1" is valid, "192.168.1.300" and "192.168.1.1e" are not.
static int is_ipv6_address (const char *str)
 Checks if a buffer points to a valid IPv6 address. "0:0:0:0:0:0:0:1", "::1" and "::FFFF:192.168.13.55" are valid "::1g" is not.
static int is_cidr_block (const char *str)
 Checks if a buffer points to an IPv4 CIDR-expressed block. "192.168.12.3/24" is valid, "192.168.1.3/31" is not.
static int cidr_get_block (const char *str, unsigned int *block)
 Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
static int cidr_get_ip (const char *str, struct in_addr *addr)
 Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".
static int cidr_block_ips (const char *str, struct in_addr *first, struct in_addr *last)
 Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24" would give 192.168.1.1 as first and 192.168.1.254 as last.
static int is_long_range_network (const char *str)
 Checks if a buffer points to a valid long range-expressed network. "192.168.12.1-192.168.13.50" is valid.
static int long_range_network_ips (const char *str, struct in_addr *first, struct in_addr *last)
 Gets the first and last IPv4 addresses from a long range-expressed network. eg. "192.168.1.1-192.168.2.40" would give 192.168.1.1 as first and 192.168.2.40 as last.
static int is_short_range_network (const char *str)
 Checks if a buffer points to a valid short range-expressed network. "192.168.11.1-50" is valid, "192.168.1.1-50e" and "192.168.1.1-300" are not.
static int short_range_network_ips (const char *str, struct in_addr *first, struct in_addr *last)
 Gets the first and last IPv4 addresses from a short range-expressed network. "192.168.1.1-40" would give 192.168.1.1 as first and 192.168.1.40 as last.
static int is_hostname (const char *str)
 Checks if a buffer points to a valid hostname.
static int is_cidr6_block (const char *str)
 Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid, "2620:0:2d0:200::7/129" is not.
static int cidr6_get_block (const char *str, unsigned int *block)
 Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
static int cidr6_get_ip (const char *str, struct in6_addr *addr6)
 Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".
static int cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
 Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24 would give 192.168.1.1 as first and 192.168.1.254 as last. Thus, it skips the network and broadcast addresses.
static int is_long_range6_network (const char *str)
 Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid.
static int long_range6_network_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
 Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:500" would give ::1:200:7 as first and ::1:205:500 as last.
static int is_short_range6_network (const char *str)
 Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid.
static int short_range6_network_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
 Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005" would give ::ffee:1:1001 as first and ::ffee:1:1005 as last.
int gvm_get_host_type (const gchar *str_stripped)
 Determines the host type in a buffer.
gvm_vhost_tgvm_vhost_new (char *value, char *source)
 Creates a new gvm_vhost_t object.
static void gvm_vhost_free (gpointer vhost)
 Frees the memory occupied by an gvm_vhost_t object.
gpointer gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
 Creates a deep copy of a gvm_vhost_t object.
static gvm_host_tgvm_host_new ()
 Creates a new gvm_host_t object.
void gvm_host_free (gpointer host)
 Frees the memory occupied by an gvm_host_t object.
void gvm_hosts_add (gvm_hosts_t *hosts, gvm_host_t *host)
 Inserts a host object at the end of a hosts collection.
static gvm_hosts_tgvm_hosts_init (const char *hosts_str)
 Creates a hosts collection from a hosts string.
static void gvm_hosts_fill_gaps (gvm_hosts_t *hosts)
 Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries.
static void gvm_hosts_deduplicate (gvm_hosts_t *hosts)
 Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current position.
gvm_hosts_tgvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
gvm_hosts_tgvm_hosts_new (const gchar *hosts_str)
 Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
gvm_host_tgvm_hosts_next (gvm_hosts_t *hosts)
 Gets the next gvm_host_t from a gvm_hosts_t structure. The state of iteration is kept internally within the gvm_hosts structure.
void gvm_hosts_move_current_host_to_end (gvm_hosts_t *hosts)
 Move the current gvm_host_t from a gvm_hosts_t structure to the end of the hosts list.
void gvm_hosts_free (gvm_hosts_t *hosts)
 Frees memory occupied by an gvm_hosts_t structure.
void gvm_hosts_shuffle (gvm_hosts_t *hosts)
 Randomizes the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator.
void gvm_hosts_reverse (gvm_hosts_t *hosts)
 Reverses the order of the hosts objects in the collection. Not to be used while iterating over the single hosts as it resets the iterator.
GSList * gvm_hosts_resolve (gvm_hosts_t *hosts)
 Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values. Not to be used while iterating over the single hosts as it resets the iterator.
int gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
 Exclude a list of vhosts from a host's vhosts list.
int gvm_hosts_exclude_with_max (gvm_hosts_t *hosts, const char *excluded_str, unsigned int max_hosts)
 Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
GSList * gvm_hosts_allowed_only (gvm_hosts_t *hosts, const char *deny_hosts_str, const char *allow_hosts_str)
 Returns a list of hosts after a host authorization check.
int gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str)
 Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
gvm_host_tgvm_host_from_str (const gchar *host_str)
 Creates a new gvm_host_t from a host string.
char * gvm_host_reverse_lookup (gvm_host_t *host)
 Checks for a host object reverse dns lookup existence.
static int host_name_verify (gvm_host_t *host, const char *value)
 Verifies that hostname value resolves to a host's IP.
void gvm_host_add_reverse_lookup (gvm_host_t *host)
 Add a host's reverse-lookup name to the vhosts list.
gvm_hosts_tgvm_hosts_reverse_lookup_only_excluded (gvm_hosts_t *hosts)
 Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
int gvm_hosts_reverse_lookup_only (gvm_hosts_t *hosts)
 Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating over the single hosts as it resets the iterator.
gvm_hosts_tgvm_hosts_reverse_lookup_unify_excluded (gvm_hosts_t *hosts)
 Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over the single hosts as it resets the iterator.
int gvm_hosts_reverse_lookup_unify (gvm_hosts_t *hosts)
 Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over the single hosts as it resets the iterator.
unsigned int gvm_hosts_count (const gvm_hosts_t *hosts)
 Gets the count of single hosts objects in a hosts collection.
unsigned int gvm_hosts_removed (const gvm_hosts_t *hosts)
 Gets the count of single values in hosts string that were removed (duplicates / excluded.).
unsigned int gvm_hosts_duplicated (const gvm_hosts_t *hosts)
 Gets the count of single values in hosts string that were duplicated and therefore removed from the list.
gvm_host_tgvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, const gvm_hosts_t *hosts)
 Find the gvm_host_t from a gvm_hosts_t structure.
gvm_host_tgvm_duplicate_host (gvm_host_t *host)
 Creates a deep copy of a host. gvm_host_free has to be called on it.
int gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr, const gvm_hosts_t *hosts)
 Returns whether a host has an equal host in a hosts collection. eg. 192.168.10.1 has an equal in list created from "192.168.10.1-5, 192.168.10.10-20" string while 192.168.10.7 doesn't.
enum host_type gvm_host_type (const gvm_host_t *host)
 Gets a host object's type.
gchar * gvm_host_type_str (const gvm_host_t *host)
 Gets a host's type in printable format.
gchar * gvm_host_value_str (const gvm_host_t *host)
 Gets a host's value in printable format.
int gvm_host_resolve (const gvm_host_t *host, void *dst, int family)
 Resolves a host object's name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAME.
int gvm_host_get_addr6 (const gvm_host_t *host, struct in6_addr *ip6)
 Gives a host object's value as an IPv6 address. If the host type is hostname, it resolves the IPv4 address then gives an IPv4-mapped IPv6 address (eg. ::ffff:192.168.1.1 .) If the host type is IPv4, it gives an IPv4-mapped IPv6 address. If the host's type is IPv6, it gives the value directly.

Variables

gchar * host_type_str [HOST_TYPE_MAX]

Detailed Description

Implementation of an API to handle Hosts objects.

This file contains all methods to handle Hosts collections (gvm_hosts_t) and single hosts objects (gvm_host_t.)

The module consequently uses glib datatypes.

Definition in file hosts.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm base"

GLib log domain.

Definition at line 36 of file hosts.c.

Function Documentation

◆ cidr6_block_ips()

int cidr6_block_ips ( const char * str,
struct in6_addr * first,
struct in6_addr * last )
static

Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24 would give 192.168.1.1 as first and 192.168.1.254 as last. Thus, it skips the network and broadcast addresses.

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

Definition at line 572 of file hosts.c.

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

References cidr6_get_block(), and cidr6_get_ip().

Referenced by gvm_hosts_new_with_max().

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

◆ cidr6_get_block()

int cidr6_get_block ( const char * str,
unsigned int * block )
static

Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.

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

Definition at line 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 cidr6_block_ips().

Here is the caller graph for this function:

◆ cidr6_get_ip()

int cidr6_get_ip ( const char * str,
struct in6_addr * addr6 )
static

Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".

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

Definition at line 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 cidr6_block_ips().

Here is the caller graph for this function:

◆ cidr_block_ips()

int cidr_block_ips ( const char * str,
struct in_addr * first,
struct in_addr * last )
static

Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192.168.1.0/24" would give 192.168.1.1 as first and 192.168.1.254 as last.

Both network and broadcast addresses are skipped:

  • They are never used as a host address. Not being included is the expected behaviour from users.
  • When needed, short/long ranges (eg. 192.168.1.0-255) are available.
Parameters
[in]strBuffer containing CIDR-expressed block.
[out]firstFirst IPv4 address in block.
[out]lastLast IPv4 address in block.
Returns
-1 if error, 0 else.

Definition at line 198 of file hosts.c.

199{
200 unsigned int block;
201
202 if (str == NULL || first == NULL || last == NULL)
203 return -1;
204
205 /* Get IP and block values. */
206 if (cidr_get_block (str, &block) == -1)
207 return -1;
208 if (cidr_get_ip (str, first) == -1)
209 return -1;
210
211 /* First IP: And with mask and increment. */
212 first->s_addr &= htonl (0xffffffff ^ ((1 << (32 - block)) - 1));
213 first->s_addr = htonl (ntohl (first->s_addr) + 1);
214
215 /* Last IP: First IP + Number of usable hosts - 1. */
216 last->s_addr = htonl (ntohl (first->s_addr) + (1 << (32 - block)) - 3);
217 return 0;
218}
static int cidr_get_ip(const char *str, struct in_addr *addr)
Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192....
Definition hosts.c:158
static int cidr_get_block(const char *str, unsigned int *block)
Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
Definition hosts.c:137

References cidr_get_block(), and cidr_get_ip().

Referenced by gvm_hosts_new_with_max().

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

◆ cidr_get_block()

int cidr_get_block ( const char * str,
unsigned int * block )
static

Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.

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

Definition at line 137 of file hosts.c.

138{
139 if (str == NULL || block == NULL)
140 return -1;
141
142 if (sscanf (str, "%*[0-9.]/%2u", block) != 1)
143 return -1;
144
145 return 0;
146}

Referenced by cidr_block_ips().

Here is the caller graph for this function:

◆ cidr_get_ip()

int cidr_get_ip ( const char * str,
struct in_addr * addr )
static

Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192.168.1.10".

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

Definition at line 158 of file hosts.c.

159{
160 gchar *addr_str, *tmp;
161
162 if (str == NULL || addr == NULL)
163 return -1;
164
165 addr_str = g_strdup (str);
166 tmp = strchr (addr_str, '/');
167 if (tmp == NULL)
168 {
169 g_free (addr_str);
170 return -1;
171 }
172 *tmp = '\0';
173
174 if (inet_pton (AF_INET, addr_str, addr) != 1)
175 return -1;
176
177 g_free (addr_str);
178 return 0;
179}

Referenced by cidr_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 2198 of file hosts.c.

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

910{
911 (void) (data);
912 gvm_vhost_t *ret = NULL;
913
914 if (!vhost)
915 return NULL;
916
917 ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
918 g_strdup (((gvm_vhost_t *) vhost)->source));
919
920 return ret;
921}
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition hosts.c:874
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 813 of file hosts.c.

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

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

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

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

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

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

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

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

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

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

References gvm_host_find_in_hosts().

Here is the call graph for this function:

◆ gvm_host_new()

gvm_host_t * gvm_host_new ( )
static

Creates a new gvm_host_t object.

Returns
Pointer to new host object, NULL if creation fails.

Definition at line 929 of file hosts.c.

930{
931 gvm_host_t *host;
932
933 host = g_malloc0 (sizeof (gvm_host_t));
934
935 return host;
936}

Referenced by gvm_duplicate_host(), gvm_host_from_str(), gvm_hosts_new_with_max(), and gvm_hosts_resolve().

Here is the caller graph for this function:

◆ gvm_host_resolve()

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

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

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

Definition at line 2346 of file hosts.c.

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

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

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

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

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

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

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

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

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

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

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

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

void gvm_hosts_deduplicate ( gvm_hosts_t * hosts)
static

Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current position.

Parameters
[in]hostshosts collection from which to remove duplicates.

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

Definition at line 1043 of file hosts.c.

1044{
1048 GHashTable *name_table;
1049 size_t i, duplicates = 0;
1050
1051 if (hosts == NULL)
1052 return;
1053 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1054
1055 for (i = 0; i < hosts->count; i++)
1056 {
1057 gchar *name;
1058
1059 name = gvm_host_value_str (hosts->hosts[i]);
1060 if (name)
1061 {
1062 gvm_host_t *host, *removed = hosts->hosts[i];
1063
1064 host = g_hash_table_lookup (name_table, name);
1065 if (host)
1066 {
1067 /* Remove duplicate host. Add its vhosts to the original host. */
1068 host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
1069 removed->vhosts = NULL;
1070 gvm_host_free (removed);
1071 hosts->hosts[i] = NULL;
1072 duplicates++;
1073 g_free (name);
1074 }
1075 else
1076 g_hash_table_insert (name_table, name, hosts->hosts[i]);
1077 }
1078 }
1079
1080 if (duplicates)
1081 gvm_hosts_fill_gaps (hosts);
1082 g_hash_table_destroy (name_table);
1083 hosts->count -= duplicates;
1084 hosts->duplicated += duplicates;
1085 hosts->current = 0;
1086#ifdef __GLIBC__
1087 malloc_trim (0);
1088#endif
1089}
size_t duplicated
Definition hosts.h:95

References gvm_hosts::count, gvm_hosts::current, gvm_hosts::duplicated, gvm_host_free(), gvm_host_value_str(), gvm_hosts_fill_gaps(), gvm_hosts::hosts, and gvm_host::vhosts.

Referenced by gvm_hosts_new_with_max(), and gvm_hosts_resolve().

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

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

2134{
2135 return hosts ? hosts->duplicated : 0;
2136}

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

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

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

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

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

Referenced by gvm_hosts_exclude().

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

◆ gvm_hosts_fill_gaps()

void gvm_hosts_fill_gaps ( gvm_hosts_t * hosts)
static

Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries.

Parameters
[in]hostsHosts collection to fill gaps in.

Definition at line 1005 of file hosts.c.

1006{
1007 size_t i;
1008 if (!hosts)
1009 return;
1010
1011 for (i = 0; i < hosts->max_size; i++)
1012 {
1013 if (!hosts->hosts[i])
1014 {
1015 size_t j;
1016
1017 /* Fill the gap with the closest host entry, in order to keep the
1018 * sequential ordering. */
1019 for (j = i + 1; j < hosts->max_size; j++)
1020 {
1021 if (hosts->hosts[j])
1022 {
1023 hosts->hosts[i] = hosts->hosts[j];
1024 hosts->hosts[j] = NULL;
1025 break;
1026 }
1027 }
1028 /* No more entries left, ie. the empty space between count and
1029 * max_size. */
1030 if (!hosts->hosts[i])
1031 return;
1032 }
1033 }
1034}

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

Referenced by gvm_hosts_allowed_only(), gvm_hosts_deduplicate(), gvm_hosts_exclude_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_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 1375 of file hosts.c.

1376{
1377 size_t i;
1378
1379 if (hosts == NULL)
1380 return;
1381
1382 g_free (hosts->orig_str);
1383 for (i = 0; i < hosts->count; i++)
1384 gvm_host_free (hosts->hosts[i]);
1385 g_free (hosts->hosts);
1386 g_free (hosts);
1387 hosts = NULL;
1388}
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_init()

gvm_hosts_t * gvm_hosts_init ( const char * hosts_str)
static

Creates a hosts collection from a hosts string.

Parameters
[in]hosts_strString of hosts.
Returns
Hosts collection.

Definition at line 987 of file hosts.c.

988{
989 gvm_hosts_t *hosts;
990
991 hosts = g_malloc0 (sizeof (gvm_hosts_t));
992 hosts->max_size = 1024;
993 hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
994 hosts->orig_str = g_strdup (hosts_str);
995 return hosts;
996}

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

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

◆ gvm_vhost_free()

void gvm_vhost_free ( gpointer vhost)
static

Frees the memory occupied by an gvm_vhost_t object.

Parameters
[in]vhostVhost to free.

Definition at line 891 of file hosts.c.

892{
893 if (vhost)
894 {
895 g_free (((gvm_vhost_t *) vhost)->value);
896 g_free (((gvm_vhost_t *) vhost)->source);
897 }
898 g_free (vhost);
899}

Referenced by gvm_host_free(), and gvm_vhosts_exclude().

Here is the caller graph for this function:

◆ gvm_vhost_new()

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

Creates a new gvm_vhost_t object.

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

Definition at line 874 of file hosts.c.

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

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

References gvm_vhost_free(), and gvm_host::vhosts.

Here is the call graph for this function:

◆ host_name_verify()

int host_name_verify ( gvm_host_t * host,
const char * value )
static

Verifies that hostname value resolves to a host's IP.

Parameters
[in]hostThe host whose IP is to be checked against.
[in]valueHostname value to verify.
Returns
0 if hostname resolves to host's IP, -1 otherwise.

Definition at line 1888 of file hosts.c.

1889{
1890 GSList *list, *tmp;
1891 char *host_str;
1892 int ret = -1;
1893
1894 assert (host);
1895 assert (value);
1896 host_str = gvm_host_value_str (host);
1897 list = tmp = gvm_resolve_list (value);
1898 while (tmp)
1899 {
1900 char buffer[INET6_ADDRSTRLEN];
1901 addr6_to_str (tmp->data, buffer);
1902 if (!strcasecmp (host_str, buffer))
1903 {
1904 ret = 0;
1905 break;
1906 }
1907 tmp = tmp->next;
1908 }
1909 g_free (host_str);
1910 g_slist_free_full (list, g_free);
1911 return ret;
1912}
void addr6_to_str(const struct in6_addr *addr6, char *str)
Stringifies an IP address.
Definition networking.c:261

References addr6_to_str(), gvm_host_value_str(), and gvm_resolve_list().

Referenced by gvm_host_add_reverse_lookup().

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

◆ is_cidr6_block()

int is_cidr6_block ( const char * str)
static

Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid, "2620:0:2d0:200::7/129" is not.

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

Definition at line 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:

◆ is_cidr_block()

int is_cidr_block ( const char * str)
static

Checks if a buffer points to an IPv4 CIDR-expressed block. "192.168.12.3/24" is valid, "192.168.1.3/31" is not.

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

Definition at line 91 of file hosts.c.

92{
93 long block;
94 char *addr_str, *block_str, *p;
95
96 addr_str = g_strdup (str);
97 block_str = strchr (addr_str, '/');
98 if (block_str == NULL)
99 {
100 g_free (addr_str);
101 return 0;
102 }
103
104 /* Separate the address from the block value. */
105 *block_str = '\0';
106 block_str++;
107
108 if (!is_ipv4_address (addr_str) || !isdigit (*block_str))
109 {
110 g_free (addr_str);
111 return 0;
112 }
113
114 p = NULL;
115 block = strtol (block_str, &p, 10);
116
117 if (*p || block <= 0 || block > 30)
118 {
119 g_free (addr_str);
120 return 0;
121 }
122
123 g_free (addr_str);
124 return 1;
125}

References is_ipv4_address().

Referenced by gvm_get_host_type().

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

◆ is_hostname()

int is_hostname ( const char * str)
static

Checks if a buffer points to a valid hostname.

Parameters
[in]strString to check.
Returns
1 if valid hostname, 0 otherwise.

Definition at line 396 of file hosts.c.

397{
398 gchar *copy, **point, **split;
399
400 /* From
401 * https://stackoverflow.com/questions/2532053/validate-a-hostname-string. */
402
403 /* Remove one dot from the end. */
404
405 copy = g_strdup (str);
406 if (copy[strlen (copy) - 1] == '.')
407 copy[strlen (copy) - 1] = '\0';
408
409 /* Check length. */
410
411 if (strlen (copy) == 0 || strlen (copy) > 253)
412 {
413 g_free (copy);
414 return 0;
415 }
416
417 /* Split on dots. */
418
419 point = split = g_strsplit (copy, ".", 0);
420 g_free (copy);
421
422 /* Last part (TLD) may not be an integer. */
423
424 if (*point)
425 {
426 gchar *last;
427
428 while (*(point + 1))
429 point++;
430 last = *point;
431 if (strlen (last))
432 {
433 while (*last && isdigit (*last))
434 last++;
435 if (*last == '\0')
436 {
437 g_strfreev (split);
438 return 0;
439 }
440 }
441 }
442
443 /* Check each part. */
444
445 point = split;
446 while (*point)
447 if (g_regex_match_simple ("^(?!-)[a-z0-9_-]{1,63}(?<!-)$", *point,
448 G_REGEX_CASELESS, 0)
449 == 0)
450 {
451 g_strfreev (split);
452 return 0;
453 }
454 else
455 point++;
456
457 g_strfreev (split);
458 return 1;
459}

Referenced by gvm_get_host_type().

Here is the caller graph for this function:

◆ is_ipv4_address()

int is_ipv4_address ( const char * str)
static

Checks if a buffer points to a valid IPv4 address. "192.168.11.1" is valid, "192.168.1.300" and "192.168.1.1e" are not.

Parameters
[in]strBuffer to check in.
Returns
1 if valid IPv4 address, 0 otherwise.

Definition at line 59 of file hosts.c.

60{
61 struct sockaddr_in sa;
62
63 return inet_pton (AF_INET, str, &(sa.sin_addr)) == 1;
64}

Referenced by gvm_get_host_type(), is_cidr_block(), is_long_range_network(), and is_short_range_network().

Here is the caller graph for this function:

◆ is_ipv6_address()

int is_ipv6_address ( const char * str)
static

Checks if a buffer points to a valid IPv6 address. "0:0:0:0:0:0:0:1", "::1" and "::FFFF:192.168.13.55" are valid "::1g" is not.

Parameters
[in]strBuffer to check in.
Returns
1 if valid IPv6 address, 0 otherwise.

Definition at line 75 of file hosts.c.

76{
77 struct sockaddr_in6 sa6;
78
79 return inet_pton (AF_INET6, str, &(sa6.sin6_addr)) == 1;
80}

Referenced by gvm_get_host_type(), is_cidr6_block(), is_long_range6_network(), and is_short_range6_network().

Here is the caller graph for this function:

◆ is_long_range6_network()

int is_long_range6_network ( const char * str)
static

Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid.

Parameters
[in]strBuffer to check in.
Returns
1 if valid long range-expressed network, 0 otherwise.

Definition at line 644 of file hosts.c.

645{
646 char *first_str, *second_str;
647 int ret;
648
649 first_str = g_strdup (str);
650 second_str = strchr (first_str, '-');
651 if (second_str == NULL)
652 {
653 g_free (first_str);
654 return 0;
655 }
656
657 /* Separate the addresses. */
658 *second_str = '\0';
659 second_str++;
660
661 ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
662 g_free (first_str);
663
664 return ret;
665}

References is_ipv6_address().

Referenced by gvm_get_host_type().

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

◆ is_long_range_network()

int is_long_range_network ( const char * str)
static

Checks if a buffer points to a valid long range-expressed network. "192.168.12.1-192.168.13.50" is valid.

Parameters
[in]strBuffer to check in.
Returns
1 if valid long range-expressed network, 0 otherwise.

Definition at line 229 of file hosts.c.

230{
231 char *first_str, *second_str;
232 int ret;
233
234 first_str = g_strdup (str);
235 second_str = strchr (first_str, '-');
236 if (second_str == NULL)
237 {
238 g_free (first_str);
239 return 0;
240 }
241
242 /* Separate the addresses. */
243 *second_str = '\0';
244 second_str++;
245
246 ret = is_ipv4_address (first_str) && is_ipv4_address (second_str);
247 g_free (first_str);
248
249 return ret;
250}

References is_ipv4_address().

Referenced by gvm_get_host_type().

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

◆ is_short_range6_network()

int is_short_range6_network ( const char * str)
static

Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid.

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

Definition at line 719 of file hosts.c.

720{
721 char *ip_str, *end_str, *p;
722
723 ip_str = g_strdup (str);
724 end_str = strchr (ip_str, '-');
725 if (end_str == NULL)
726 {
727 g_free (ip_str);
728 return 0;
729 }
730
731 /* Separate the addresses. */
732 *end_str = '\0';
733 end_str++;
734
735 if (!is_ipv6_address (ip_str) || *end_str == '\0')
736 {
737 g_free (ip_str);
738 return 0;
739 }
740
741 p = end_str;
742 /* Check that the 2nd part is at most 4 hexadecimal characters. */
743 while (isxdigit (*p) && p++)
744 ;
745 if (*p || p - end_str > 4)
746 {
747 g_free (ip_str);
748 return 0;
749 }
750
751 g_free (ip_str);
752 return 1;
753}

References is_ipv6_address().

Referenced by gvm_get_host_type().

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

◆ is_short_range_network()

int is_short_range_network ( const char * str)
static

Checks if a buffer points to a valid short range-expressed network. "192.168.11.1-50" is valid, "192.168.1.1-50e" and "192.168.1.1-300" are not.

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

Definition at line 304 of file hosts.c.

305{
306 long end;
307 char *ip_str, *end_str, *p;
308
309 ip_str = g_strdup (str);
310 end_str = strchr (ip_str, '-');
311 if (end_str == NULL)
312 {
313 g_free (ip_str);
314 return 0;
315 }
316
317 /* Separate the addresses. */
318 *end_str = '\0';
319 end_str++;
320
321 if (!is_ipv4_address (ip_str) || !isdigit (*end_str))
322 {
323 g_free (ip_str);
324 return 0;
325 }
326
327 p = NULL;
328 end = strtol (end_str, &p, 10);
329
330 if (*p || end < 0 || end > 255)
331 {
332 g_free (ip_str);
333 return 0;
334 }
335
336 g_free (ip_str);
337 return 1;
338}

References is_ipv4_address().

Referenced by gvm_get_host_type().

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

◆ long_range6_network_ips()

int long_range6_network_ips ( const char * str,
struct in6_addr * first,
struct in6_addr * last )
static

Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:500" would give ::1:200:7 as first and ::1:205:500 as last.

Parameters
[in]strString containing long IPv6 range-expressed network.
[out]firstFirst IPv6 address in range.
[out]lastLast IPv6 address in range.
Returns
-1 if error, 0 else.

Definition at line 679 of file hosts.c.

681{
682 char *first_str, *last_str;
683
684 if (str == NULL || first == NULL || last == NULL)
685 return -1;
686
687 first_str = g_strdup (str);
688 last_str = strchr (first_str, '-');
689 if (last_str == NULL)
690 {
691 g_free (first_str);
692 return -1;
693 }
694
695 /* Separate the two IPs. */
696 *last_str = '\0';
697 last_str++;
698
699 if (inet_pton (AF_INET6, first_str, first) != 1
700 || inet_pton (AF_INET6, last_str, last) != 1)
701 {
702 g_free (first_str);
703 return -1;
704 }
705
706 g_free (first_str);
707 return 0;
708}

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ long_range_network_ips()

int long_range_network_ips ( const char * str,
struct in_addr * first,
struct in_addr * last )
static

Gets the first and last IPv4 addresses from a long range-expressed network. eg. "192.168.1.1-192.168.2.40" would give 192.168.1.1 as first and 192.168.2.40 as last.

Parameters
[in]strString containing long range-expressed network.
[out]firstFirst IP address in block.
[out]lastLast IP address in block.
Returns
-1 if error, 0 else.

Definition at line 264 of file hosts.c.

266{
267 char *first_str, *last_str;
268
269 if (str == NULL || first == NULL || last == NULL)
270 return -1;
271
272 first_str = g_strdup (str);
273 last_str = strchr (first_str, '-');
274 if (last_str == NULL)
275 {
276 g_free (first_str);
277 return -1;
278 }
279
280 /* Separate the two IPs. */
281 *last_str = '\0';
282 last_str++;
283
284 if (inet_pton (AF_INET, first_str, first) != 1
285 || inet_pton (AF_INET, last_str, last) != 1)
286 {
287 g_free (first_str);
288 return -1;
289 }
290
291 g_free (first_str);
292 return 0;
293}

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ short_range6_network_ips()

int short_range6_network_ips ( const char * str,
struct in6_addr * first,
struct in6_addr * last )
static

Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005" would give ::ffee:1:1001 as first and ::ffee:1:1005 as last.

Parameters
[in]strString containing short IPv6 range-expressed network.
[out]firstFirst IPv6 address in range.
[out]lastLast IPv6 address in range.
Returns
-1 if error, 0 else.

Definition at line 767 of file hosts.c.

769{
770 char *first_str, *last_str;
771 long int end;
772
773 if (str == NULL || first == NULL || last == NULL)
774 return -1;
775
776 first_str = g_strdup (str);
777 last_str = strchr (first_str, '-');
778 if (last_str == NULL)
779 {
780 g_free (first_str);
781 return -1;
782 }
783
784 /* Separate the first IP. */
785 *last_str = '\0';
786 last_str++;
787
788 if (inet_pton (AF_INET6, first_str, first) != 1)
789 {
790 g_free (first_str);
791 return -1;
792 }
793
794 /* Calculate the last IP. */
795 memcpy (last, first, sizeof (*last));
796 end = strtol (last_str, NULL, 16);
797 memcpy (&last->s6_addr[15], &end, 1);
798 memcpy (&last->s6_addr[14], ((char *) &end) + 1, 1);
799
800 g_free (first_str);
801 return 0;
802}

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

◆ short_range_network_ips()

int short_range_network_ips ( const char * str,
struct in_addr * first,
struct in_addr * last )
static

Gets the first and last IPv4 addresses from a short range-expressed network. "192.168.1.1-40" would give 192.168.1.1 as first and 192.168.1.40 as last.

Parameters
[in]strString containing short range-expressed network.
[out]firstFirst IP address in block.
[out]lastLast IP address in block.
Returns
-1 if error, 0 else.

Definition at line 352 of file hosts.c.

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

Referenced by gvm_hosts_new_with_max().

Here is the caller graph for this function:

Variable Documentation

◆ host_type_str

gchar* host_type_str[HOST_TYPE_MAX]
Initial value:
= {
[HOST_TYPE_NAME] = "Hostname",
[HOST_TYPE_IPV4] = "IPv4",
[HOST_TYPE_IPV6] = "IPv6",
[HOST_TYPE_CIDR_BLOCK] = "IPv4 CIDR block",
[HOST_TYPE_RANGE_SHORT] = "IPv4 short range",
[HOST_TYPE_RANGE_LONG] = "IPv4 long range"}

Definition at line 40 of file hosts.c.

40 {
41 [HOST_TYPE_NAME] = "Hostname",
42 [HOST_TYPE_IPV4] = "IPv4",
43 [HOST_TYPE_IPV6] = "IPv6",
44 [HOST_TYPE_CIDR_BLOCK] = "IPv4 CIDR block",
45 [HOST_TYPE_RANGE_SHORT] = "IPv4 short range",
46 [HOST_TYPE_RANGE_LONG] = "IPv4 long range"};

Referenced by gvm_host_type_str().