Greenbone Vulnerability Management Libraries 23.16.0
hosts.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
15
16#include "hosts.h"
17
18#include "networking.h" /* for ipv4_as_ipv6, addr6_as_str, gvm_resolve */
19
20#include <arpa/inet.h> /* for inet_pton, inet_ntop */
21#include <assert.h> /* for assert */
22#include <ctype.h> /* for isdigit */
23#include <malloc.h>
24#include <netdb.h> /* for getnameinfo, NI_NAMEREQD */
25#include <stdint.h> /* for uint8_t, uint32_t */
26#include <stdio.h> /* for sscanf, perror */
27#include <stdlib.h> /* for strtol, atoi */
28#include <string.h> /* for strchr, memcpy, memcmp, bzero, strcasecmp */
29#include <sys/socket.h> /* for AF_INET, AF_INET6, sockaddr */
30#include <unistd.h> /* for usleep() */
31
32#undef G_LOG_DOMAIN
36#define G_LOG_DOMAIN "libgvm base"
37
38/* Static variables */
39
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"};
47
48/* Function definitions */
49
58static int
59is_ipv4_address (const char *str)
60{
61 struct sockaddr_in sa;
62
63 return inet_pton (AF_INET, str, &(sa.sin_addr)) == 1;
64}
65
74static int
75is_ipv6_address (const char *str)
76{
77 struct sockaddr_in6 sa6;
78
79 return inet_pton (AF_INET6, str, &(sa6.sin6_addr)) == 1;
80}
81
90static int
91is_cidr_block (const char *str)
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}
126
136static int
137cidr_get_block (const char *str, unsigned int *block)
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}
147
157static int
158cidr_get_ip (const char *str, struct in_addr *addr)
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}
180
197static int
198cidr_block_ips (const char *str, struct in_addr *first, struct in_addr *last)
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}
219
228static int
229is_long_range_network (const char *str)
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}
251
263static int
264long_range_network_ips (const char *str, struct in_addr *first,
265 struct in_addr *last)
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}
294
303static int
304is_short_range_network (const char *str)
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}
339
351static int
352short_range_network_ips (const char *str, struct in_addr *first,
353 struct in_addr *last)
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}
387
395static int
396is_hostname (const char *str)
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}
460
469int
470gvm_is_cidr6_block (const char *str)
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}
505
515int
516gvm_cidr6_get_block (const char *str, unsigned int *block)
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}
526
536int
537gvm_cidr6_get_ip (const char *str, struct in6_addr *addr6)
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}
559
571int
572gvm_cidr6_block_ips (const char *str, struct in6_addr *first,
573 struct in6_addr *last)
574{
575 unsigned int block;
576 int i, j;
577
578 if (str == NULL || first == NULL || last == NULL)
579 return -1;
580
581 /* Get IP and block values. */
582 if (gvm_cidr6_get_block (str, &block) == -1)
583 return -1;
584 if (gvm_cidr6_get_ip (str, first) == -1)
585 return -1;
586 memcpy (&last->s6_addr, &first->s6_addr, 16);
587
588 /* /128 => Specified address is the first and last one. */
589 if (block == 128)
590 return 0;
591
592 /* First IP: And with mask and increment to skip network address. */
593 j = 15;
594 for (i = (128 - block) / 8; i > 0; i--)
595 {
596 first->s6_addr[j] = 0;
597 j--;
598 }
599 first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
600
601 /* Last IP: Broadcast address - 1. */
602 j = 15;
603 for (i = (128 - block) / 8; i > 0; i--)
604 {
605 last->s6_addr[j] = 0xff;
606 j--;
607 }
608 last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
609
610 /* /127 => Only two addresses. Don't skip network / broadcast addresses.*/
611 if (block == 127)
612 return 0;
613
614 /* Increment first IP. */
615 for (i = 15; i >= 0; --i)
616 if (first->s6_addr[i] < 255)
617 {
618 first->s6_addr[i]++;
619 break;
620 }
621 else
622 first->s6_addr[i] = 0;
623 /* Decrement last IP. */
624 for (i = 15; i >= 0; --i)
625 if (last->s6_addr[i] > 0)
626 {
627 last->s6_addr[i]--;
628 break;
629 }
630 else
631 last->s6_addr[i] = 0xff;
632
633 return 0;
634}
635
644static int
645is_long_range6_network (const char *str)
646{
647 char *first_str, *second_str;
648 int ret;
649
650 first_str = g_strdup (str);
651 second_str = strchr (first_str, '-');
652 if (second_str == NULL)
653 {
654 g_free (first_str);
655 return 0;
656 }
657
658 /* Separate the addresses. */
659 *second_str = '\0';
660 second_str++;
661
662 ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
663 g_free (first_str);
664
665 return ret;
666}
667
679static int
680long_range6_network_ips (const char *str, struct in6_addr *first,
681 struct in6_addr *last)
682{
683 char *first_str, *last_str;
684
685 if (str == NULL || first == NULL || last == NULL)
686 return -1;
687
688 first_str = g_strdup (str);
689 last_str = strchr (first_str, '-');
690 if (last_str == NULL)
691 {
692 g_free (first_str);
693 return -1;
694 }
695
696 /* Separate the two IPs. */
697 *last_str = '\0';
698 last_str++;
699
700 if (inet_pton (AF_INET6, first_str, first) != 1
701 || inet_pton (AF_INET6, last_str, last) != 1)
702 {
703 g_free (first_str);
704 return -1;
705 }
706
707 g_free (first_str);
708 return 0;
709}
710
719static int
720is_short_range6_network (const char *str)
721{
722 char *ip_str, *end_str, *p;
723
724 ip_str = g_strdup (str);
725 end_str = strchr (ip_str, '-');
726 if (end_str == NULL)
727 {
728 g_free (ip_str);
729 return 0;
730 }
731
732 /* Separate the addresses. */
733 *end_str = '\0';
734 end_str++;
735
736 if (!is_ipv6_address (ip_str) || *end_str == '\0')
737 {
738 g_free (ip_str);
739 return 0;
740 }
741
742 p = end_str;
743 /* Check that the 2nd part is at most 4 hexadecimal characters. */
744 while (isxdigit (*p) && p++)
745 ;
746 if (*p || p - end_str > 4)
747 {
748 g_free (ip_str);
749 return 0;
750 }
751
752 g_free (ip_str);
753 return 1;
754}
755
767static int
768short_range6_network_ips (const char *str, struct in6_addr *first,
769 struct in6_addr *last)
770{
771 char *first_str, *last_str;
772 long int end;
773
774 if (str == NULL || first == NULL || last == NULL)
775 return -1;
776
777 first_str = g_strdup (str);
778 last_str = strchr (first_str, '-');
779 if (last_str == NULL)
780 {
781 g_free (first_str);
782 return -1;
783 }
784
785 /* Separate the first IP. */
786 *last_str = '\0';
787 last_str++;
788
789 if (inet_pton (AF_INET6, first_str, first) != 1)
790 {
791 g_free (first_str);
792 return -1;
793 }
794
795 /* Calculate the last IP. */
796 memcpy (last, first, sizeof (*last));
797 end = strtol (last_str, NULL, 16);
798 memcpy (&last->s6_addr[15], &end, 1);
799 memcpy (&last->s6_addr[14], ((char *) &end) + 1, 1);
800
801 g_free (first_str);
802 return 0;
803}
804
813int
814gvm_get_host_type (const gchar *str_stripped)
815{
816 /*
817 * We have a single element with no leading or trailing
818 * white spaces. This element could represent different host
819 * definitions: single IPs, host names, CIDR-expressed blocks,
820 * range-expressed networks, IPv6 addresses.
821 */
822
823 /* Null or empty string. */
824 if (str_stripped == NULL || *str_stripped == '\0')
825 return -1;
826
827 /* Check for regular single IPv4 address. */
828 if (is_ipv4_address (str_stripped))
829 return HOST_TYPE_IPV4;
830
831 /* Check for regular single IPv6 address. */
832 if (is_ipv6_address (str_stripped))
833 return HOST_TYPE_IPV6;
834
835 /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
836 if (is_cidr_block (str_stripped))
838
839 /* Check for short range-expressed networks "192.168.12.5-40" */
840 if (is_short_range_network (str_stripped))
842
843 /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
844 if (is_long_range_network (str_stripped))
846
847 /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
848 if (gvm_is_cidr6_block (str_stripped))
850
851 /* Check for short range-expressed networks "::1-ef12" */
852 if (is_short_range6_network (str_stripped))
854
855 /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
856 if (is_long_range6_network (str_stripped))
858
859 /* Check for hostname. */
860 if (is_hostname (str_stripped))
861 return HOST_TYPE_NAME;
862
863 return -1;
864}
865
875gvm_vhost_new (char *value, char *source)
876{
877 gvm_vhost_t *vhost;
878
879 vhost = g_malloc0 (sizeof (gvm_vhost_t));
880 vhost->value = value;
881 vhost->source = source;
882
883 return vhost;
884}
885
891static void
892gvm_vhost_free (gpointer vhost)
893{
894 if (vhost)
895 {
896 g_free (((gvm_vhost_t *) vhost)->value);
897 g_free (((gvm_vhost_t *) vhost)->source);
898 }
899 g_free (vhost);
900}
901
909gpointer
910gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
911{
912 (void) (data);
913 gvm_vhost_t *ret = NULL;
914
915 if (!vhost)
916 return NULL;
917
918 ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
919 g_strdup (((gvm_vhost_t *) vhost)->source));
920
921 return ret;
922}
923
929static gvm_host_t *
931{
932 gvm_host_t *host;
933
934 host = g_malloc0 (sizeof (gvm_host_t));
935
936 return host;
937}
938
944void
945gvm_host_free (gpointer host)
946{
947 gvm_host_t *h = host;
948 if (h == NULL)
949 return;
950
951 /* If host of type hostname, free the name buffer, first. */
952 if (h->type == HOST_TYPE_NAME)
953 g_free (h->name);
954
955 g_slist_free_full (h->vhosts, gvm_vhost_free);
956 g_free (h);
957}
958
965void
967{
968 if (hosts->count == hosts->max_size)
969 {
970 hosts->max_size *= 4;
971 hosts->hosts =
972 g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
973 memset (hosts->hosts + hosts->count, '\0',
974 (hosts->max_size - hosts->count) * sizeof (gvm_host_t *));
975 }
976 hosts->hosts[hosts->count] = host;
977 hosts->count++;
978}
979
987static gvm_hosts_t *
988gvm_hosts_init (const char *hosts_str)
989{
990 gvm_hosts_t *hosts;
991
992 hosts = g_malloc0 (sizeof (gvm_hosts_t));
993 hosts->max_size = 1024;
994 hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
995 hosts->orig_str = g_strdup (hosts_str);
996 return hosts;
997}
998
1005static void
1007{
1008 size_t i;
1009 if (!hosts)
1010 return;
1011
1012 for (i = 0; i < hosts->max_size; i++)
1013 {
1014 if (!hosts->hosts[i])
1015 {
1016 size_t j;
1017
1018 /* Fill the gap with the closest host entry, in order to keep the
1019 * sequential ordering. */
1020 for (j = i + 1; j < hosts->max_size; j++)
1021 {
1022 if (hosts->hosts[j])
1023 {
1024 hosts->hosts[i] = hosts->hosts[j];
1025 hosts->hosts[j] = NULL;
1026 break;
1027 }
1028 }
1029 /* No more entries left, ie. the empty space between count and
1030 * max_size. */
1031 if (!hosts->hosts[i])
1032 return;
1033 }
1034 }
1035}
1036
1043static void
1045{
1049 GHashTable *name_table;
1050 size_t i, duplicates = 0;
1051
1052 if (hosts == NULL)
1053 return;
1054 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1055
1056 for (i = 0; i < hosts->count; i++)
1057 {
1058 gchar *name;
1059
1060 name = gvm_host_value_str (hosts->hosts[i]);
1061 if (name)
1062 {
1063 gvm_host_t *host, *removed = hosts->hosts[i];
1064
1065 host = g_hash_table_lookup (name_table, name);
1066 if (host)
1067 {
1068 /* Remove duplicate host. Add its vhosts to the original host. */
1069 host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
1070 removed->vhosts = NULL;
1071 gvm_host_free (removed);
1072 hosts->hosts[i] = NULL;
1073 duplicates++;
1074 g_free (name);
1075 }
1076 else
1077 g_hash_table_insert (name_table, name, hosts->hosts[i]);
1078 }
1079 }
1080
1081 if (duplicates)
1082 gvm_hosts_fill_gaps (hosts);
1083 g_hash_table_destroy (name_table);
1084 hosts->count -= duplicates;
1085 hosts->duplicated += duplicates;
1086 hosts->current = 0;
1087#ifdef __GLIBC__
1088 malloc_trim (0);
1089#endif
1090}
1091
1104gvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
1105{
1106 gvm_hosts_t *hosts;
1107 gchar **host_element, **split;
1108 gchar *str;
1109
1110 if (hosts_str == NULL)
1111 return NULL;
1112
1113 /* Normalize separator: Transform newlines into commas. */
1114 hosts = gvm_hosts_init (hosts_str);
1115 str = hosts->orig_str;
1116 while (*str)
1117 {
1118 if (*str == '\n')
1119 *str = ',';
1120 str++;
1121 }
1122
1123 /* Split comma-separated list into single host-specifications */
1124 split = g_strsplit (hosts->orig_str, ",", 0);
1125
1126 /* first element of the split list */
1127 host_element = split;
1128 while (*host_element)
1129 {
1130 int host_type;
1131 gchar *stripped = g_strstrip (*host_element);
1132
1133 if (stripped == NULL || *stripped == '\0')
1134 {
1135 host_element++;
1136 continue;
1137 }
1138
1139 /* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ?
1140 */
1141 /* -1 if error. */
1142 host_type = gvm_get_host_type (stripped);
1143
1144 switch (host_type)
1145 {
1146 case HOST_TYPE_NAME:
1147 case HOST_TYPE_IPV4:
1148 case HOST_TYPE_IPV6:
1149 {
1150 /* New host. */
1151 gvm_host_t *host = gvm_host_new ();
1152 host->type = host_type;
1154 host->name = g_ascii_strdown (stripped, -1);
1155 else if (host_type == HOST_TYPE_IPV4)
1156 {
1157 if (inet_pton (AF_INET, stripped, &host->addr) != 1)
1158 break;
1159 }
1160 else if (host_type == HOST_TYPE_IPV6)
1161 {
1162 if (inet_pton (AF_INET6, stripped, &host->addr6) != 1)
1163 break;
1164 }
1165 gvm_hosts_add (hosts, host);
1166 break;
1167 }
1171 {
1172 struct in_addr first, last;
1173 uint32_t current;
1174 int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
1175
1177 ips_func = cidr_block_ips;
1178 else if (host_type == HOST_TYPE_RANGE_SHORT)
1179 ips_func = short_range_network_ips;
1180 else
1181 ips_func = long_range_network_ips;
1182
1183 if (ips_func (stripped, &first, &last) == -1)
1184 break;
1185
1186 /* Make sure that first actually comes before last */
1187 if (ntohl (first.s_addr) > ntohl (last.s_addr))
1188 break;
1189
1190 /* Add addresses from first to last as single hosts. */
1191 current = first.s_addr;
1192 while (1)
1193 {
1194 gvm_host_t *host;
1195 if (max_hosts > 0 && hosts->count > max_hosts)
1196 {
1197 g_strfreev (split);
1198 gvm_hosts_free (hosts);
1199 return NULL;
1200 }
1201 host = gvm_host_new ();
1202 host->type = HOST_TYPE_IPV4;
1203 host->addr.s_addr = current;
1204 gvm_hosts_add (hosts, host);
1205 /* Exit loop if range end has been reached.
1206 * Check is done before increment to avoid possible overflow.
1207 */
1208 if (ntohl (current) >= ntohl (last.s_addr))
1209 break;
1210 /* Next IP address. */
1211 current = htonl (ntohl (current) + 1);
1212 }
1213 break;
1214 }
1218 {
1219 struct in6_addr first, last;
1220 unsigned char current[16];
1221 int (*ips_func) (const char *, struct in6_addr *,
1222 struct in6_addr *);
1223
1225 ips_func = gvm_cidr6_block_ips;
1227 ips_func = short_range6_network_ips;
1228 else
1229 ips_func = long_range6_network_ips;
1230
1231 if (ips_func (stripped, &first, &last) == -1)
1232 break;
1233
1234 /* Make sure the first comes before the last. */
1235 if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1236 break;
1237
1238 /* Add addresses from first to last as single hosts. */
1239 memcpy (current, &first.s6_addr, 16);
1240 while (1)
1241 {
1242 int i;
1243 gvm_host_t *host;
1244
1245 if (max_hosts > 0 && hosts->count > max_hosts)
1246 {
1247 g_strfreev (split);
1248 gvm_hosts_free (hosts);
1249 return NULL;
1250 }
1251 host = gvm_host_new ();
1252 host->type = HOST_TYPE_IPV6;
1253 memcpy (host->addr6.s6_addr, current, 16);
1254 gvm_hosts_add (hosts, host);
1255
1256 /* Exit loop if range end has been reached.
1257 * Check is done before increment to avoid possible overflow.
1258 */
1259 if (memcmp (current, &last.s6_addr, 16) >= 0)
1260 break;
1261
1262 /* Next IPv6 address. */
1263 for (i = 15; i >= 0; --i)
1264 if (current[i] < 255)
1265 {
1266 current[i]++;
1267 break;
1268 }
1269 else
1270 current[i] = 0;
1271 }
1272 break;
1273 }
1274 case -1:
1275 default:
1276 /* Invalid host string. */
1277 g_strfreev (split);
1278 gvm_hosts_free (hosts);
1279 return NULL;
1280 }
1281 host_element++; /* move on to next element of split list */
1282 if (max_hosts > 0 && hosts->count > max_hosts)
1283 {
1284 g_strfreev (split);
1285 gvm_hosts_free (hosts);
1286 return NULL;
1287 }
1288 }
1289
1290 /* No need to check for duplicates when a hosts string contains a
1291 * single (IP/Hostname/Range/Subnetwork) entry. */
1292 if (g_strv_length (split) > 1)
1293 gvm_hosts_deduplicate (hosts);
1294
1295 g_strfreev (split);
1296#ifdef __GLIBC__
1297 malloc_trim (0);
1298#endif
1299 return hosts;
1300}
1301
1313gvm_hosts_new (const gchar *hosts_str)
1314{
1315 return gvm_hosts_new_with_max (hosts_str, 0);
1316}
1317
1326gvm_host_t *
1328{
1329 if (!hosts || hosts->current == hosts->count)
1330 return NULL;
1331
1332 return hosts->hosts[hosts->current++];
1333}
1334
1344void
1346{
1347 void *host_tmp;
1348 size_t i;
1349
1350 if (!hosts)
1351 return;
1352
1353 // Keep in mind that gvm_hosts_next will return the current host and then
1354 // increment hosts->current.
1355
1356 if (hosts->current == hosts->count)
1357 {
1358 // We're on the last host, just make the previous host current.
1359 if (hosts->current == 0)
1360 return;
1361 hosts->current -= 1;
1362 return;
1363 }
1364
1365 // Make the previous host current. This makes sure that gvm_hosts_next will
1366 // return the host that has replaced the current host.
1367 if (hosts->current == 0)
1368 return;
1369 hosts->current -= 1;
1370 // Get the host to be moved.
1371 host_tmp = hosts->hosts[hosts->current];
1372
1373 // Shift all the others down. Start from current + 1 because we're assigning
1374 // into the previous slot (i - 1). It's safe to do this because we already
1375 // checked if current == count above.
1376 for (i = hosts->current + 1; i < hosts->count; i++)
1377 hosts->hosts[i - 1] = hosts->hosts[i];
1378
1379 // Put the moved host on the end.
1380 hosts->hosts[hosts->count - 1] = host_tmp;
1381}
1382
1389void
1391{
1392 size_t i;
1393
1394 if (hosts == NULL)
1395 return;
1396
1397 g_free (hosts->orig_str);
1398 for (i = 0; i < hosts->count; i++)
1399 gvm_host_free (hosts->hosts[i]);
1400 g_free (hosts->hosts);
1401 g_free (hosts);
1402 hosts = NULL;
1403}
1404
1412void
1414{
1415 size_t i = 0;
1416 GRand *rand;
1417
1418 if (hosts == NULL)
1419 return;
1420
1421 /* Shuffle the array. */
1422 rand = g_rand_new ();
1423 for (i = 0; i < hosts->count; i++)
1424 {
1425 void *tmp;
1426 int j = g_rand_int_range (rand, 0, hosts->count);
1427
1428 tmp = hosts->hosts[i];
1429 hosts->hosts[i] = hosts->hosts[j];
1430 hosts->hosts[j] = tmp;
1431 }
1432
1433 hosts->current = 0;
1434 g_rand_free (rand);
1435}
1436
1444void
1446{
1447 size_t i, j;
1448 if (hosts == NULL || hosts->count < 2)
1449 return;
1450
1451 for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1452 {
1453 gvm_host_t *tmp = hosts->hosts[i];
1454 hosts->hosts[i] = hosts->hosts[j];
1455 hosts->hosts[j] = tmp;
1456 }
1457 hosts->current = 0;
1458}
1459
1470GSList *
1472{
1473 size_t i, new_entries = 0, resolved = 0;
1474 GSList *unresolved = NULL;
1475
1476 for (i = 0; i < hosts->count; i++)
1477 {
1478 GSList *list, *tmp;
1479 gvm_host_t *host = hosts->hosts[i];
1480
1481 if (host->type != HOST_TYPE_NAME)
1482 continue;
1483
1484 list = tmp = gvm_resolve_list (host->name);
1485 while (tmp)
1486 {
1487 /* Create a new host for each IP address. */
1488 gvm_host_t *new;
1489 struct in6_addr *ip6 = tmp->data;
1490 gvm_vhost_t *vhost;
1491
1492 new = gvm_host_new ();
1493 if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1494 || ip6->s6_addr32[2] != htonl (0xffff))
1495 {
1496 new->type = HOST_TYPE_IPV6;
1497 memcpy (&new->addr6, ip6, sizeof (new->addr6));
1498 }
1499 else
1500 {
1501 new->type = HOST_TYPE_IPV4;
1502 memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1503 }
1504 vhost =
1505 gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1506 new->vhosts = g_slist_prepend (new->vhosts, vhost);
1507 gvm_hosts_add (hosts, new);
1508 tmp = tmp->next;
1509 new_entries = 1;
1510 }
1511 /* Remove hostname from list, as it was either replaced by IPs, or
1512 * is unresolvable. */
1513 hosts->hosts[i] = NULL;
1514 resolved++;
1515 if (!list)
1516 unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
1517 gvm_host_free (host);
1518 g_slist_free_full (list, g_free);
1519 }
1520 if (resolved)
1521 gvm_hosts_fill_gaps (hosts);
1522 hosts->count -= resolved;
1523 hosts->removed += resolved;
1524 if (new_entries)
1525 gvm_hosts_deduplicate (hosts);
1526 hosts->current = 0;
1527 return unresolved;
1528}
1529
1538int
1539gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
1540{
1541 GSList *vhost;
1542 char **excluded;
1543 int ret = 0;
1544
1545 if (!host || !excluded_str)
1546 return ret;
1547
1548 vhost = host->vhosts;
1549 excluded = g_strsplit (excluded_str, ",", 0);
1550 if (!excluded || !*excluded)
1551 {
1552 g_strfreev (excluded);
1553 return ret;
1554 }
1555 while (vhost)
1556 {
1557 char **tmp = excluded;
1558 char *value = ((gvm_vhost_t *) vhost->data)->value;
1559
1560 while (*tmp)
1561 {
1562 if (!strcasecmp (value, g_strstrip (*tmp)))
1563 {
1564 gvm_vhost_free (vhost->data);
1565 host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1566 ret++;
1567 break;
1568 }
1569 tmp++;
1570 if (!*tmp)
1571 {
1572 vhost = vhost->next;
1573 break;
1574 }
1575 }
1576 }
1577 g_strfreev (excluded);
1578
1579 return ret;
1580}
1581
1593int
1594gvm_hosts_exclude_with_max (gvm_hosts_t *hosts, const char *excluded_str,
1595 unsigned int max_hosts)
1596{
1600 gvm_hosts_t *excluded_hosts;
1601 GHashTable *name_table;
1602 size_t excluded = 0, i;
1603
1604 if (hosts == NULL || excluded_str == NULL)
1605 return -1;
1606
1607 excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1608 if (excluded_hosts == NULL)
1609 return -1;
1610
1611 if (gvm_hosts_count (excluded_hosts) == 0)
1612 {
1613 gvm_hosts_free (excluded_hosts);
1614 return 0;
1615 }
1616
1617 /* Hash host values from excluded hosts list. */
1618 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1619 for (i = 0; i < excluded_hosts->count; i++)
1620 {
1621 gchar *name;
1622
1623 name = gvm_host_value_str (excluded_hosts->hosts[i]);
1624 if (name)
1625 g_hash_table_insert (name_table, name, hosts);
1626 }
1627
1628 /* Check for hosts values in hash table. */
1629 for (i = 0; i < hosts->count; i++)
1630 {
1631 gchar *name;
1632
1633 name = gvm_host_value_str (hosts->hosts[i]);
1634 if (name)
1635 {
1636 if (g_hash_table_lookup (name_table, name))
1637 {
1638 gvm_host_free (hosts->hosts[i]);
1639 hosts->hosts[i] = NULL;
1640 excluded++;
1641 g_free (name);
1642 continue;
1643 }
1644 g_free (name);
1645 }
1646 }
1647
1648 /* Cleanup. */
1649 if (excluded)
1650 gvm_hosts_fill_gaps (hosts);
1651 hosts->count -= excluded;
1652 hosts->removed += excluded;
1653 hosts->current = 0;
1654 g_hash_table_destroy (name_table);
1655 gvm_hosts_free (excluded_hosts);
1656 return excluded;
1657}
1658
1671GSList *
1672gvm_hosts_allowed_only (gvm_hosts_t *hosts, const char *deny_hosts_str,
1673 const char *allow_hosts_str)
1674{
1678 gvm_hosts_t *allowed_hosts, *denied_hosts;
1679 GHashTable *name_allow_table = NULL, *name_deny_table = NULL;
1680 GSList *removed = NULL;
1681 size_t excluded = 0, i;
1682
1683 if (hosts == NULL || (deny_hosts_str == NULL && allow_hosts_str == NULL))
1684 return NULL;
1685
1686 // Prepare list of denied and allowed hosts
1687 denied_hosts = gvm_hosts_new_with_max (deny_hosts_str, 0);
1688 allowed_hosts = gvm_hosts_new_with_max (allow_hosts_str, 0);
1689
1690 if (gvm_hosts_count (denied_hosts) == 0)
1691 {
1692 gvm_hosts_free (denied_hosts);
1693 denied_hosts = NULL;
1694 }
1695
1696 if (gvm_hosts_count (allowed_hosts) == 0)
1697 {
1698 gvm_hosts_free (allowed_hosts);
1699 allowed_hosts = NULL;
1700 }
1701
1702 if (denied_hosts == NULL && allowed_hosts == NULL)
1703 return NULL;
1704
1705 if (denied_hosts)
1706 {
1707 /* Hash host values from denied hosts list. */
1708 name_deny_table =
1709 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1710 for (i = 0; i < denied_hosts->count; i++)
1711 {
1712 gchar *name;
1713
1714 name = gvm_host_value_str (denied_hosts->hosts[i]);
1715 if (name)
1716 g_hash_table_insert (name_deny_table, name, hosts);
1717 }
1718 }
1719
1720 if (allowed_hosts)
1721 {
1722 /* Hash host values from allowed hosts list. */
1723 name_allow_table =
1724 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1725 for (i = 0; i < allowed_hosts->count; i++)
1726 {
1727 gchar *name;
1728
1729 name = gvm_host_value_str (allowed_hosts->hosts[i]);
1730 if (name)
1731 g_hash_table_insert (name_allow_table, name, hosts);
1732 }
1733 }
1734
1735 /* Check for authorized hosts in hash table and create a list of removed
1736 * hosts. */
1737 for (i = 0; i < hosts->count; i++)
1738 {
1739 gchar *name;
1740
1741 name = gvm_host_value_str (hosts->hosts[i]);
1742 if (name)
1743 {
1744 if (name_deny_table != NULL
1745 && g_hash_table_lookup (name_deny_table, name))
1746 {
1747 gvm_host_free (hosts->hosts[i]);
1748 hosts->hosts[i] = NULL;
1749 excluded++;
1750 removed = g_slist_prepend (removed, name);
1751 continue;
1752 }
1753 else if (name_allow_table != NULL
1754 && !g_hash_table_lookup (name_allow_table, name))
1755 {
1756 gvm_host_free (hosts->hosts[i]);
1757 hosts->hosts[i] = NULL;
1758 excluded++;
1759 removed = g_slist_prepend (removed, name);
1760 continue;
1761 }
1762 g_free (name);
1763 }
1764 }
1765
1766 /* Cleanup. */
1767 if (excluded)
1768 gvm_hosts_fill_gaps (hosts);
1769
1770 hosts->count -= excluded;
1771 hosts->removed += excluded;
1772 hosts->current = 0;
1773 if (name_allow_table != NULL)
1774 g_hash_table_destroy (name_allow_table);
1775 if (name_deny_table != NULL)
1776 g_hash_table_destroy (name_deny_table);
1777 if (allowed_hosts != NULL)
1778 gvm_hosts_free (allowed_hosts);
1779 if (denied_hosts != NULL)
1780 gvm_hosts_free (denied_hosts);
1781 return removed;
1782}
1783
1794int
1795gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str)
1796{
1797 return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1798}
1799
1810gvm_host_t *
1811gvm_host_from_str (const gchar *host_str)
1812{
1813 int host_type;
1814
1815 if (host_str == NULL)
1816 return NULL;
1817
1818 /* IPv4, hostname, IPv6 */
1819 /* -1 if error. */
1820 host_type = gvm_get_host_type (host_str);
1821
1822 switch (host_type)
1823 {
1824 case HOST_TYPE_NAME:
1825 case HOST_TYPE_IPV4:
1826 case HOST_TYPE_IPV6:
1827 {
1828 /* New host. */
1829 gvm_host_t *host = gvm_host_new ();
1830 host->type = host_type;
1832 host->name = g_ascii_strdown (host_str, -1);
1833 else if (host_type == HOST_TYPE_IPV4)
1834 {
1835 if (inet_pton (AF_INET, host_str, &host->addr) != 1)
1836 break;
1837 }
1838 else if (host_type == HOST_TYPE_IPV6)
1839 {
1840 if (inet_pton (AF_INET6, host_str, &host->addr6) != 1)
1841 break;
1842 }
1843 return host;
1844 }
1845 case -1:
1846 default:
1847 return NULL;
1848 }
1849 return NULL;
1850}
1851
1859char *
1861{
1862 int retry = 10;
1863 gchar hostname[NI_MAXHOST];
1864 void *addr;
1865 size_t addrlen;
1866 struct sockaddr_in sa;
1867 struct sockaddr_in6 sa6;
1868
1869 if (!host)
1870 return NULL;
1871
1872 if (host->type == HOST_TYPE_IPV4)
1873 {
1874 addr = &sa;
1875 addrlen = sizeof (sa);
1876 memset (addr, '\0', addrlen);
1877 sa.sin_addr = host->addr;
1878 sa.sin_family = AF_INET;
1879 }
1880 else if (host->type == HOST_TYPE_IPV6)
1881 {
1882 addr = &sa6;
1883 addrlen = sizeof (sa6);
1884 memset (&sa6, '\0', addrlen);
1885 memcpy (&sa6.sin6_addr, &host->addr6, 16);
1886 sa6.sin6_family = AF_INET6;
1887 }
1888 else
1889 return NULL;
1890
1891 while (retry--)
1892 {
1893 int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
1894 0, NI_NAMEREQD);
1895 if (!ret)
1896 return g_ascii_strdown (hostname, -1);
1897 if (ret != EAI_AGAIN)
1898 break;
1899 usleep (10000); // 10ms
1900 }
1901 return NULL;
1902}
1903
1912static int
1913host_name_verify (gvm_host_t *host, const char *value)
1914{
1915 GSList *list, *tmp;
1916 char *host_str;
1917 int ret = -1;
1918
1919 assert (host);
1920 assert (value);
1921 host_str = gvm_host_value_str (host);
1922 list = tmp = gvm_resolve_list (value);
1923 while (tmp)
1924 {
1925 char buffer[INET6_ADDRSTRLEN];
1926 addr6_to_str (tmp->data, buffer);
1927 if (!strcasecmp (host_str, buffer))
1928 {
1929 ret = 0;
1930 break;
1931 }
1932 tmp = tmp->next;
1933 }
1934 g_free (host_str);
1935 g_slist_free_full (list, g_free);
1936 return ret;
1937}
1938
1944void
1946{
1947 GSList *vhosts;
1948 gvm_vhost_t *vhost;
1949 char *value;
1950
1951 if (!host || host->type == HOST_TYPE_NAME)
1952 return;
1953
1954 value = gvm_host_reverse_lookup (host);
1955 if (!value)
1956 return;
1957 if (host_name_verify (host, value))
1958 {
1959 g_free (value);
1960 return;
1961 }
1962 /* Don't add vhost, if already in the list. */
1963 vhosts = host->vhosts;
1964 while (vhosts)
1965 {
1966 if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1967 {
1968 g_free (value);
1969 return;
1970 }
1971 vhosts = vhosts->next;
1972 }
1973 vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1974 host->vhosts = g_slist_prepend (host->vhosts, vhost);
1975}
1976
1988{
1989 size_t i, count = 0;
1990 gvm_hosts_t *excluded = gvm_hosts_new ("");
1991
1992 if (hosts == NULL)
1993 return NULL;
1994
1995 for (i = 0; i < hosts->count; i++)
1996 {
1997 gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1998
1999 if (name == NULL)
2000 {
2001 gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
2002 gvm_host_free (hosts->hosts[i]);
2003 hosts->hosts[i] = NULL;
2004 count++;
2005 }
2006 else
2007 g_free (name);
2008 }
2009
2010 if (count)
2011 gvm_hosts_fill_gaps (hosts);
2012 hosts->count -= count;
2013 hosts->removed += count;
2014 hosts->current = 0;
2015 return excluded;
2016}
2017
2027int
2029{
2030 gvm_hosts_t *excluded;
2031 int count = 0;
2032
2033 if (hosts == NULL)
2034 return -1;
2035
2036 excluded = gvm_hosts_reverse_lookup_only_excluded (hosts);
2037 count = excluded->count;
2038 gvm_hosts_free (excluded);
2039
2040 return count;
2041}
2042
2054{
2058 size_t i, count = 0;
2059 GHashTable *name_table;
2060 gvm_hosts_t *excluded = NULL;
2061
2062 if (hosts == NULL)
2063 return NULL;
2064
2065 excluded = gvm_hosts_new ("");
2066 name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
2067 for (i = 0; i < hosts->count; i++)
2068 {
2069 gchar *name;
2070
2071 name = gvm_host_reverse_lookup (hosts->hosts[i]);
2072 if (name)
2073 {
2074 if (g_hash_table_lookup (name_table, name))
2075 {
2076 gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
2077 gvm_host_free (hosts->hosts[i]);
2078 hosts->hosts[i] = NULL;
2079 count++;
2080 g_free (name);
2081 }
2082 else
2083 {
2084 /* Insert in the hash table. Value not important. */
2085 g_hash_table_insert (name_table, name, hosts);
2086 }
2087 }
2088 }
2089
2090 if (count)
2091 gvm_hosts_fill_gaps (hosts);
2092 g_hash_table_destroy (name_table);
2093 hosts->removed += count;
2094 hosts->count -= count;
2095 hosts->current = 0;
2096 return excluded;
2097}
2098
2108int
2110{
2111 gvm_hosts_t *excluded = NULL;
2112 int count = 0;
2113 if (hosts == NULL)
2114 return -1;
2115
2116 excluded = gvm_hosts_reverse_lookup_unify_excluded (hosts);
2117 count = excluded->count;
2118 gvm_hosts_free (excluded);
2119
2120 return count;
2121}
2122
2129unsigned int
2131{
2132 return hosts ? hosts->count : 0;
2133}
2134
2143unsigned int
2145{
2146 return hosts ? hosts->removed : 0;
2147}
2148
2157unsigned int
2159{
2160 return hosts ? hosts->duplicated : 0;
2161}
2162
2173gvm_host_t *
2174gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
2175 const gvm_hosts_t *hosts)
2176{
2177 char *host_str;
2178 size_t i;
2179
2180 if (host == NULL || hosts == NULL)
2181 return NULL;
2182
2183 host_str = gvm_host_value_str (host);
2184
2185 for (i = 0; i < hosts->count; i++)
2186 {
2187 gvm_host_t *current_host = hosts->hosts[i];
2188 char *tmp = gvm_host_value_str (current_host);
2189
2190 if (strcasecmp (host_str, tmp) == 0)
2191 {
2192 g_free (host_str);
2193 g_free (tmp);
2194 return current_host;
2195 }
2196 g_free (tmp);
2197
2198 /* Hostnames in hosts list shouldn't be resolved. */
2199 if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
2200 {
2201 struct in6_addr tmpaddr;
2202 gvm_host_get_addr6 (current_host, &tmpaddr);
2203
2204 if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
2205 {
2206 g_free (host_str);
2207 return current_host;
2208 }
2209 }
2210 }
2211
2212 g_free (host_str);
2213 return NULL;
2214}
2215
2222gvm_host_t *
2224{
2225 gvm_host_t *ret = NULL;
2226
2227 if (host == NULL)
2228 return NULL;
2229
2230 ret = gvm_host_new ();
2231
2232 ret->type = host->type;
2233 switch (host->type)
2234 {
2235 case HOST_TYPE_NAME:
2236 ret->name = g_strdup (host->name);
2237 break;
2238 case HOST_TYPE_IPV4:
2239 ret->addr = host->addr;
2240 break;
2241 case HOST_TYPE_IPV6:
2242 ret->addr6 = host->addr6;
2243 break;
2244 default:
2245 g_free (ret);
2246 return NULL;
2247 }
2248 ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);
2249
2250 return ret;
2251}
2252
2265int
2266gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
2267 const gvm_hosts_t *hosts)
2268{
2269 if (gvm_host_find_in_hosts (host, addr, hosts))
2270 return 1;
2271
2272 return 0;
2273}
2274
2282enum host_type
2284{
2285 assert (host);
2286 return host->type;
2287}
2288
2297gchar *
2299{
2300 if (host == NULL)
2301 return NULL;
2302
2303 return host_type_str[host->type];
2304}
2305
2313gchar *
2315{
2316 if (host == NULL)
2317 return NULL;
2318
2319 switch (host->type)
2320 {
2321 case HOST_TYPE_NAME:
2322 return g_strdup (host->name);
2323 break;
2324 case HOST_TYPE_IPV4:
2325 case HOST_TYPE_IPV6:
2326 /* Handle both cases using inet_ntop(). */
2327 {
2328 int family, size;
2329 gchar *str;
2330 const void *srcaddr;
2331
2332 if (host->type == HOST_TYPE_IPV4)
2333 {
2334 family = AF_INET;
2335 size = INET_ADDRSTRLEN;
2336 srcaddr = &host->addr;
2337 }
2338 else
2339 {
2340 family = AF_INET6;
2341 size = INET6_ADDRSTRLEN;
2342 srcaddr = &host->addr6;
2343 }
2344
2345 str = g_malloc0 (size);
2346 if (inet_ntop (family, srcaddr, str, size) == NULL)
2347 {
2348 perror ("inet_ntop");
2349 g_free (str);
2350 return NULL;
2351 }
2352 return str;
2353 }
2354 default:
2355 return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
2356 }
2357}
2358
2370int
2371gvm_host_resolve (const gvm_host_t *host, void *dst, int family)
2372{
2373 if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
2374 return -1;
2375
2376 return gvm_resolve (host->name, dst, family);
2377}
2378
2391int
2392gvm_host_get_addr6 (const gvm_host_t *host, struct in6_addr *ip6)
2393{
2394 if (host == NULL || ip6 == NULL)
2395 return -1;
2396
2397 switch (gvm_host_type (host))
2398 {
2399 case HOST_TYPE_IPV6:
2400 memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
2401 return 0;
2402
2403 case HOST_TYPE_IPV4:
2404 ipv4_as_ipv6 (&host->addr, ip6);
2405 return 0;
2406
2407 case HOST_TYPE_NAME:
2408 {
2409 struct in_addr ip4;
2410
2411 /* Fail if IPv4 and IPv6 both don't resolve. */
2412 if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
2413 ipv4_as_ipv6 (&ip4, ip6);
2414 else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
2415 return -1;
2416 return 0;
2417 }
2418
2419 default:
2420 return -1;
2421 }
2422}
void gvm_hosts_add(gvm_hosts_t *hosts, gvm_host_t *host)
Inserts a host object at the end of a hosts collection.
Definition hosts.c:966
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...
Definition hosts.c:2266
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
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:2053
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
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 ove...
Definition hosts.c:2028
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition hosts.c:2314
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
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:2371
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:1987
gchar * host_type_str[HOST_TYPE_MAX]
Definition hosts.c:40
static int is_long_range6_network(const char *str)
Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid.
Definition hosts.c:645
static int 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
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition hosts.c:1860
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
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:2223
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
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition hosts.c:875
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:1313
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 with...
Definition hosts.c:1327
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.
Definition hosts.c:1672
void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition hosts.c:945
unsigned int gvm_hosts_removed(const gvm_hosts_t *hosts)
Gets the count of single values in hosts string that were removed (duplicates / excluded....
Definition hosts.c:2144
gchar * gvm_host_type_str(const gvm_host_t *host)
Gets a host's type in printable format.
Definition hosts.c:2298
void gvm_host_add_reverse_lookup(gvm_host_t *host)
Add a host's reverse-lookup name to the vhosts list.
Definition hosts.c:1945
gvm_hosts_t * gvm_hosts_new_with_max(const gchar *hosts_str, unsigned int max_hosts)
Creates a new gvm_hosts_t structure and the associated hosts objects from the provided hosts_str.
Definition hosts.c:1104
int gvm_cidr6_block_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192....
Definition hosts.c:572
static int is_hostname(const char *str)
Checks if a buffer points to a valid hostname.
Definition hosts.c:396
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition hosts.c:930
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 si...
Definition hosts.c:1445
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 ...
Definition hosts.c:1413
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 l...
Definition hosts.c:2158
static int is_short_range6_network(const char *str)
Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid.
Definition hosts.c:720
gvm_host_t * gvm_host_from_str(const gchar *host_str)
Creates a new gvm_host_t from a host string.
Definition hosts.c:1811
static void gvm_hosts_deduplicate(gvm_hosts_t *hosts)
Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current positi...
Definition hosts.c:1044
int gvm_cidr6_get_block(const char *str, unsigned int *block)
Gets the network block value from a CIDR-expressed block string. For "192.168.1.1/24" it is 24.
Definition hosts.c:516
static int short_range6_network_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005...
Definition hosts.c:768
int gvm_cidr6_get_ip(const char *str, struct in6_addr *addr6)
Gets the IPv4 value from a CIDR-expressed block. eg. For "192.168.1.10/24" it is "192....
Definition hosts.c:537
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 gvm_hosts_t * gvm_hosts_init(const char *hosts_str)
Creates a hosts collection from a hosts string.
Definition hosts.c:988
static int long_range6_network_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:50...
Definition hosts.c:680
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:2392
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
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.
Definition hosts.c:1345
int gvm_vhosts_exclude(gvm_host_t *host, const char *excluded_str)
Exclude a list of vhosts from a host's vhosts list.
Definition hosts.c:1539
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:1594
GSList * gvm_hosts_resolve(gvm_hosts_t *hosts)
Resolves host objects of type name in a hosts collection, replacing hostnames with IPv4 values....
Definition hosts.c:1471
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition hosts.c:2283
gpointer gvm_duplicate_vhost(gconstpointer vhost, gpointer data)
Creates a deep copy of a gvm_vhost_t object.
Definition hosts.c:910
static void gvm_hosts_fill_gaps(gvm_hosts_t *hosts)
Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries.
Definition hosts.c:1006
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:1913
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:2174
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition hosts.c:814
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
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition hosts.c:2130
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
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition hosts.c:892
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition hosts.c:1390
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 ...
Definition hosts.c:1795
int gvm_is_cidr6_block(const char *str)
Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid,...
Definition hosts.c:470
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 t...
Definition hosts.c:2109
Protos and data structures for Hosts collections and single hosts objects.
host_type
Definition hosts.h:34
@ HOST_TYPE_RANGE_SHORT
Definition hosts.h:38
@ HOST_TYPE_RANGE6_SHORT
Definition hosts.h:43
@ HOST_TYPE_RANGE_LONG
Definition hosts.h:39
@ HOST_TYPE_RANGE6_LONG
Definition hosts.h:42
@ HOST_TYPE_NAME
Definition hosts.h:35
@ HOST_TYPE_IPV6
Definition hosts.h:40
@ HOST_TYPE_CIDR6_BLOCK
Definition hosts.h:41
@ HOST_TYPE_MAX
Definition hosts.h:44
@ HOST_TYPE_IPV4
Definition hosts.h:36
@ HOST_TYPE_CIDR_BLOCK
Definition hosts.h:37
struct gvm_host gvm_host_t
Definition hosts.h:48
struct gvm_vhost gvm_vhost_t
Definition hosts.h:49
struct gvm_hosts gvm_hosts_t
Definition hosts.h:50
void addr6_to_str(const struct in6_addr *addr6, char *str)
Stringifies an IP address.
Definition networking.c:261
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
int gvm_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
Definition networking.c:389
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition networking.c:339
GVM Networking related API.
GSList * vhosts
Definition hosts.h:69
enum host_type type
Definition hosts.h:68
size_t max_size
Definition hosts.h:91
size_t removed
Definition hosts.h:94
gchar * orig_str
Definition hosts.h:89
size_t duplicated
Definition hosts.h:95
size_t current
Definition hosts.h:92
size_t count
Definition hosts.h:93
gvm_host_t ** hosts
Definition hosts.h:90
char * value
Definition hosts.h:77
char * source
Definition hosts.h:78