61 is_ipv4_address (
const char *str)
63 struct sockaddr_in sa;
65 return inet_pton(AF_INET, str, &(sa.sin_addr)) == 1;
77 is_ipv6_address (
const char *str)
79 struct sockaddr_in6 sa6;
81 return inet_pton(AF_INET6, str, &(sa6.sin6_addr)) == 1;
93 is_cidr_block (
const char *str)
96 char *addr_str, *block_str, *p;
98 addr_str = g_strdup (str);
99 block_str = strchr (addr_str,
'/');
100 if (block_str == NULL)
110 if (!is_ipv4_address (addr_str) || !isdigit (*block_str))
117 block = strtol (block_str, &p, 10);
120 if (*p || block <= 0 || block > 30)
136 cidr_get_block (
const char *str,
unsigned int *block)
138 if (str == NULL || block == NULL)
141 if (sscanf (str,
"%*[0-9.]/%2u", block)
158 cidr_get_ip (
const char *str,
struct in_addr *addr)
160 gchar *addr_str, *tmp;
162 if (str == NULL || addr == NULL)
165 addr_str = g_strdup (str);
166 tmp = strchr (addr_str,
'/');
174 if (inet_pton (AF_INET, addr_str, addr) != 1)
198 cidr_block_ips (
const char *str,
struct in_addr *first,
struct in_addr *last)
202 if (str == NULL || first == NULL || last == NULL)
206 if (cidr_get_block (str, &block) == -1)
208 if (cidr_get_ip (str, first) == -1)
212 first->s_addr &= htonl (0xffffffff ^ ((1 << (32 - block)) - 1));
213 first->s_addr = htonl (ntohl (first->s_addr) + 1);
216 last->s_addr = htonl (ntohl (first->s_addr) + (1 << (32 - block)) - 3);
229 is_long_range_network (
const char *str)
231 char *first_str, *second_str;
234 first_str = g_strdup (str);
235 second_str = strchr (first_str,
'-');
236 if (second_str == NULL)
246 ret = is_ipv4_address (first_str) && is_ipv4_address (second_str);
264 long_range_network_ips (
const char *str,
struct in_addr *first,
265 struct in_addr *last)
267 char *first_str, *last_str;
269 if (str == NULL || first == NULL || last == NULL)
272 first_str = g_strdup (str);
273 last_str = strchr (first_str,
'-');
274 if (last_str == NULL)
284 if (inet_pton (AF_INET, first_str, first) != 1
285 || inet_pton (AF_INET, last_str, last) != 1)
304 is_short_range_network (
const char *str)
307 char *ip_str, *end_str, *p;
309 ip_str = g_strdup (str);
310 end_str = strchr (ip_str,
'-');
321 if (!is_ipv4_address (ip_str) || !isdigit (*end_str))
328 end = strtol (end_str, &p, 10);
331 if (*p || end < 0 || end > 255)
349 short_range_network_ips (
const char *str,
struct in_addr *first,
350 struct in_addr *last)
352 char *first_str, *last_str;
355 if (str == NULL || first == NULL || last == NULL)
358 first_str = g_strdup (str);
359 last_str = strchr (first_str,
'-');
360 if (last_str == NULL)
369 end = atoi (last_str);
372 if (inet_pton (AF_INET, first_str, first) != 1)
379 last->s_addr = htonl ((ntohl (first->s_addr) & 0xffffff00) + end);
395 is_hostname (
const char *str)
399 while (*h && (isalnum (*h) || strchr (
"-_.", *h)))
403 if (*h ==
'\0' && h - str < 256)
418 is_cidr6_block (
const char *str)
421 char *addr6_str, *block_str, *p;
423 addr6_str = g_strdup (str);
424 block_str = strchr (addr6_str,
'/');
425 if (block_str == NULL)
435 if (!is_ipv6_address (addr6_str) || !isdigit (*block_str))
442 block = strtol (block_str, &p, 10);
445 if (*p || block <= 0 || block > 128)
461 cidr6_get_block (
const char *str,
unsigned int *block)
463 if (str == NULL || block == NULL)
466 if (sscanf (str,
"%*[0-9a-fA-F.:]/%3u", block)
483 cidr6_get_ip (
const char *str,
struct in6_addr *addr6)
485 gchar *addr6_str, *tmp;
487 if (str == NULL || addr6 == NULL)
490 addr6_str = g_strdup (str);
491 tmp = strchr (addr6_str,
'/');
499 if (inet_pton (AF_INET6, addr6_str, addr6) != 1)
518 cidr6_block_ips (
const char *str,
struct in6_addr *first,
struct in6_addr *last)
523 if (str == NULL || first == NULL || last == NULL)
527 if (cidr6_get_block (str, &block) == -1)
529 if (cidr6_get_ip (str, first) == -1)
531 memcpy (&last->s6_addr, &first->s6_addr, 16);
539 for (i = (128 - block) / 8; i > 0; i--)
541 first->s6_addr[j] = 0;
544 first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
548 for (i = (128 - block) / 8; i > 0; i--)
550 last->s6_addr[j] = 0xff;
553 last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
560 for (i = 15; i >= 0; --i)
561 if (first->s6_addr[i] < 255)
567 first->s6_addr[i] = 0;
569 for (i = 15; i >= 0; --i)
570 if (last->s6_addr[i] > 0)
576 last->s6_addr[i] = 0xff;
590 is_long_range6_network (
const char *str)
592 char *first_str, *second_str;
595 first_str = g_strdup (str);
596 second_str = strchr (first_str,
'-');
597 if (second_str == NULL)
607 ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
625 long_range6_network_ips (
const char *str,
struct in6_addr *first,
626 struct in6_addr *last)
628 char *first_str, *last_str;
630 if (str == NULL || first == NULL || last == NULL)
633 first_str = g_strdup (str);
634 last_str = strchr (first_str,
'-');
635 if (last_str == NULL)
645 if (inet_pton (AF_INET6, first_str, first) != 1
646 || inet_pton (AF_INET6, last_str, last) != 1)
665 is_short_range6_network (
const char *str)
667 char *ip_str, *end_str, *p;
669 ip_str = g_strdup (str);
670 end_str = strchr (ip_str,
'-');
681 if (!is_ipv6_address (ip_str) || *end_str ==
'\0')
689 while (isxdigit (*p) && p++);
690 if (*p || p - end_str > 4)
712 short_range6_network_ips (
const char *str,
struct in6_addr *first,
713 struct in6_addr *last)
715 char *first_str, *last_str;
718 if (str == NULL || first == NULL || last == NULL)
721 first_str = g_strdup (str);
722 last_str = strchr (first_str,
'-');
723 if (last_str == NULL)
733 if (inet_pton (AF_INET6, first_str, first) != 1)
740 memcpy (last, first,
sizeof (*last));
741 end = strtol (last_str, NULL, 16);
742 memcpy (&last->s6_addr[15], &end, 1);
743 memcpy (&last->s6_addr[14], ((
char *) &end) + 1, 1);
768 if (str_stripped == NULL || *str_stripped ==
'\0')
772 if (is_ipv4_address (str_stripped))
776 if (is_ipv6_address (str_stripped))
780 if (is_cidr_block (str_stripped))
784 if (is_short_range_network (str_stripped))
788 if (is_long_range_network (str_stripped))
792 if (is_cidr6_block (str_stripped))
796 if (is_short_range6_network (str_stripped))
800 if (is_long_range6_network (str_stripped))
804 if (is_hostname (str_stripped))
832 openvas_host_free (gpointer host)
858 GHashTable *name_table;
863 element = hosts->
hosts;
864 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
872 if (g_hash_table_lookup (name_table,
name))
877 element = element->next;
878 openvas_host_free (tmp->data);
879 hosts->
hosts = g_list_delete_link (hosts->
hosts, tmp);
886 g_hash_table_insert (name_table,
name, hosts);
887 element = element->next;
891 element = element->next;
894 g_hash_table_destroy (name_table);
895 hosts->
count -= duplicates;
915 gchar **host_element, **split;
918 if (hosts_str == NULL)
925 hosts->
orig_str = g_strdup (hosts_str);
930 if (*str ==
'\n') *str =
',';
935 split = g_strsplit (hosts->
orig_str,
",", 0);
938 host_element = split;
939 while (*host_element)
942 gchar *stripped = g_strstrip (*host_element);
944 if (stripped == NULL || *stripped ==
'\0')
964 host->
name = g_strdup (stripped);
966 inet_pton (AF_INET, stripped, &host->
addr);
968 inet_pton (AF_INET6, stripped, &host->
addr6);
970 hosts->
hosts = g_list_prepend (hosts->
hosts, host);
978 struct in_addr first, last;
980 int (*ips_func) (
const char *,
struct in_addr *,
struct in_addr *);
983 ips_func = cidr_block_ips;
985 ips_func = short_range_network_ips;
987 ips_func = long_range_network_ips;
991 if (ips_func (stripped, &first, &last) == -1)
995 if (ntohl (first.s_addr) > ntohl (last.s_addr))
999 current = first.s_addr;
1000 while (ntohl (current) <= ntohl (last.s_addr))
1004 host->
addr.s_addr = current;
1005 hosts->
hosts = g_list_prepend (hosts->
hosts, host);
1007 if (max_hosts > 0 && hosts->
count > max_hosts)
1014 current = htonl (ntohl (current) + 1);
1022 struct in6_addr first, last;
1023 unsigned char current[16];
1024 int (*ips_func) (
const char *,
struct in6_addr *,
struct in6_addr *);
1027 ips_func = cidr6_block_ips;
1029 ips_func = short_range6_network_ips;
1031 ips_func = long_range6_network_ips;
1035 if (ips_func (stripped, &first, &last) == -1)
1039 if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1043 memcpy (current, &first.s6_addr, 16);
1044 while (memcmp (current, &last.s6_addr, 16) <= 0)
1050 memcpy (host->
addr6.s6_addr, current, 16);
1051 hosts->
hosts = g_list_prepend (hosts->
hosts, host);
1053 if (max_hosts > 0 && hosts->
count > max_hosts)
1060 for (i = 15; i >= 0; --i)
1061 if (current[i] < 255)
1079 if (max_hosts > 0 && hosts->
count > max_hosts)
1088 hosts->
hosts = g_list_reverse (hosts->
hosts);
1091 openvas_hosts_deduplicate (hosts);
1129 if (hosts == NULL || hosts->
current == NULL)
1153 g_list_free_full (hosts->
hosts, openvas_host_free);
1178 rand = g_rand_new ();
1185 element = g_list_nth (hosts->
hosts, g_rand_int_range (rand, 0, count));
1187 hosts->
hosts = g_list_remove_link (hosts->
hosts, element);
1189 new_list = g_list_concat (element, new_list);
1192 hosts->
hosts = new_list;
1208 if (hosts == NULL || hosts->
hosts == NULL)
1211 hosts->
hosts = g_list_reverse (hosts->
hosts);
1229 element = element->next;
1230 openvas_host_free (tmp->data);
1231 hosts->
hosts = g_list_delete_link (hosts->
hosts, tmp);
1252 struct in_addr addr;
1259 g_free (host->
name);
1261 memcpy (&host->
addr, &addr, sizeof (host->
addr));
1288 GHashTable *name_table;
1291 if (hosts == NULL || excluded_str == NULL)
1295 if (excluded_hosts == NULL)
1308 element = excluded_hosts->
hosts;
1309 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1315 g_hash_table_insert (name_table,
name, hosts);
1316 element = element->next;
1320 element = hosts->
hosts;
1324 struct in_addr addr;
1329 if (g_hash_table_lookup (name_table,
name))
1331 element = openvas_hosts_remove_element (hosts, element);
1343 struct in6_addr addr6;
1347 if (g_hash_table_lookup (name_table,
name))
1349 element = openvas_hosts_remove_element (hosts, element);
1357 element = element->next;
1361 hosts->
count -= excluded;
1364 g_hash_table_destroy (name_table);
1385 return g_strdup (host->
name);
1388 struct sockaddr_in sa;
1390 gchar hostname[1000];
1392 bzero (&sa,
sizeof (
struct sockaddr));
1393 sa.sin_addr = host->
addr;
1394 sa.sin_family = AF_INET;
1397 int ret = getnameinfo ((
struct sockaddr *) &sa,
sizeof (sa), hostname,
1398 sizeof (hostname), NULL, 0, NI_NAMEREQD);
1400 return g_strdup (hostname);
1401 if (ret != EAI_AGAIN)
1408 struct sockaddr_in6 sa;
1409 char hostname[1000];
1411 bzero (&sa,
sizeof (
struct sockaddr));
1412 memcpy (&sa.sin6_addr, &host->
addr6, 16);
1413 sa.sin6_family = AF_INET6;
1415 if (getnameinfo ((
struct sockaddr *) &sa,
sizeof (sa), hostname,
1416 sizeof (hostname), NULL, 0, NI_NAMEREQD))
1419 return g_strdup (hostname);
1444 element = hosts->
hosts;
1451 element = openvas_hosts_remove_element (hosts, element);
1457 element = element->next;
1461 hosts->
count -= count;
1484 GHashTable *name_table;
1489 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1491 element = hosts->
hosts;
1498 if (g_hash_table_lookup (name_table,
name))
1500 element = openvas_hosts_remove_element (hosts, element);
1507 g_hash_table_insert (name_table,
name, hosts);
1508 element = element->next;
1512 element = element->next;
1515 g_hash_table_destroy (name_table);
1517 hosts->
count -= count;
1532 return hosts ? hosts->
count : 0;
1546 return hosts ? hosts->
removed : 0;
1568 if (host == NULL || hosts == NULL)
1573 element = hosts->
hosts;
1578 if (strcasecmp (host_str, tmp) == 0)
1589 struct in6_addr tmpaddr;
1592 if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
1599 element = element->next;
1653 return g_strdup (host->
name);
1661 const void *srcaddr;
1666 size = INET_ADDRSTRLEN;
1667 srcaddr = &host->
addr;
1672 size = INET6_ADDRSTRLEN;
1673 srcaddr = &host->
addr6;
1676 str = g_malloc0 (size);
1677 if (inet_ntop (family, srcaddr, str, size) == NULL)
1679 perror (
"inet_ntop");
1686 return g_strdup (
"Erroneous host type: Should be Hostname/IPv4/IPv6.");
1725 if (host == NULL || ip6 == NULL)
1731 memcpy (ip6, &host->
addr6, sizeof (
struct in6_addr));
openvas_hosts_t * openvas_hosts_new_with_max(const gchar *hosts_str, unsigned int max_hosts)
Creates a new openvas_hosts_t structure and the associated hosts objects from the provided hosts_str...
The structure for a single host object.
gchar * openvas_host_value_str(const openvas_host_t *host)
Gets a host's value in printable format.
Protos and data structures for Hosts collections and single hosts objects.
void openvas_hosts_free(openvas_hosts_t *hosts)
Frees memory occupied by an openvas_hosts_t structure.
int openvas_host_in_hosts(const openvas_host_t *host, const struct in6_addr *addr, const openvas_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...
int openvas_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
openvas_host_t * openvas_hosts_next(openvas_hosts_t *hosts)
Gets the next openvas_host_t from a openvas_hosts_t structure. The state of iteration is kept interna...
gchar * openvas_host_type_str(const openvas_host_t *host)
Gets a host's type in printable format.
int openvas_hosts_reverse_lookup_only(openvas_hosts_t *hosts)
Removes hosts that don't reverse-lookup from the hosts collection. Not to be used while iterating ove...
openvas_hosts_t * openvas_hosts_new(const gchar *hosts_str)
Creates a new openvas_hosts_t structure and the associated hosts objects from the provided hosts_str...
void openvas_hosts_resolve(openvas_hosts_t *hosts)
Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values...
char * addr6_as_str(const struct in6_addr *addr6)
int openvas_host_resolve(const openvas_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...
enum host_type openvas_host_type(const openvas_host_t *host)
Gets a host object's type.
gchar * host_type_str[HOST_TYPE_MAX]
unsigned int openvas_hosts_count(const openvas_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
unsigned int openvas_hosts_removed(const openvas_hosts_t *hosts)
Gets the count of single values in hosts string that were removed (duplicates / excluded.)
int openvas_hosts_reverse_lookup_unify(openvas_hosts_t *hosts)
Removes hosts duplicates that reverse-lookup to the same value. Not to be used while iterating over t...
The structure for Hosts collection.
int openvas_hosts_exclude(openvas_hosts_t *hosts, const char *excluded_str, int resolve)
Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating ...
void openvas_hosts_reverse(openvas_hosts_t *hosts)
Reverses the order of the hosts objects in the collection. Not to be used while iterating over the si...
int openvas_host_get_addr6(const openvas_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...
int openvas_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
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.168.10.20.
void openvas_hosts_shuffle(openvas_hosts_t *hosts)
Randomizes the order of the hosts objects in the collection. Not to be used while iterating over the ...
char * openvas_host_reverse_lookup(openvas_host_t *host)
Checks for a host object reverse dns lookup existence.