Greenbone Vulnerability Management Libraries  22.8.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 
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
33 
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 
58 static int
59 is_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 
74 static int
75 is_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 
90 static int
91 is_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 
136 static int
137 cidr_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 
157 static int
158 cidr_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 
197 static int
198 cidr_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 
228 static int
229 is_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 
263 static int
264 long_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 
303 static int
304 is_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 
351 static int
352 short_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 
395 static int
396 is_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  return 0;
437  }
438  }
439 
440  /* Check each part. */
441 
442  point = split;
443  while (*point)
444  if (g_regex_match_simple ("^(?!-)[a-z0-9_-]{1,63}(?<!-)$", *point,
445  G_REGEX_CASELESS, 0)
446  == 0)
447  {
448  g_strfreev (split);
449  return 0;
450  }
451  else
452  point++;
453 
454  g_strfreev (split);
455  return 1;
456 }
457 
466 static int
467 is_cidr6_block (const char *str)
468 {
469  long block;
470  char *addr6_str, *block_str, *p;
471 
472  addr6_str = g_strdup (str);
473  block_str = strchr (addr6_str, '/');
474  if (block_str == NULL)
475  {
476  g_free (addr6_str);
477  return 0;
478  }
479 
480  /* Separate the address from the block value. */
481  *block_str = '\0';
482  block_str++;
483 
484  if (!is_ipv6_address (addr6_str) || !isdigit (*block_str))
485  {
486  g_free (addr6_str);
487  return 0;
488  }
489 
490  p = NULL;
491  block = strtol (block_str, &p, 10);
492 
493  if (*p || block <= 0 || block > 128)
494  {
495  g_free (addr6_str);
496  return 0;
497  }
498 
499  g_free (addr6_str);
500  return 1;
501 }
502 
512 static int
513 cidr6_get_block (const char *str, unsigned int *block)
514 {
515  if (str == NULL || block == NULL)
516  return -1;
517 
518  if (sscanf (str, "%*[0-9a-fA-F.:]/%3u", block) != 1)
519  return -1;
520 
521  return 0;
522 }
523 
533 static int
534 cidr6_get_ip (const char *str, struct in6_addr *addr6)
535 {
536  gchar *addr6_str, *tmp;
537 
538  if (str == NULL || addr6 == NULL)
539  return -1;
540 
541  addr6_str = g_strdup (str);
542  tmp = strchr (addr6_str, '/');
543  if (tmp == NULL)
544  {
545  g_free (addr6_str);
546  return -1;
547  }
548  *tmp = '\0';
549 
550  if (inet_pton (AF_INET6, addr6_str, addr6) != 1)
551  return -1;
552 
553  g_free (addr6_str);
554  return 0;
555 }
556 
568 static int
569 cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
570 {
571  unsigned int block;
572  int i, j;
573 
574  if (str == NULL || first == NULL || last == NULL)
575  return -1;
576 
577  /* Get IP and block values. */
578  if (cidr6_get_block (str, &block) == -1)
579  return -1;
580  if (cidr6_get_ip (str, first) == -1)
581  return -1;
582  memcpy (&last->s6_addr, &first->s6_addr, 16);
583 
584  /* /128 => Specified address is the first and last one. */
585  if (block == 128)
586  return 0;
587 
588  /* First IP: And with mask and increment to skip network address. */
589  j = 15;
590  for (i = (128 - block) / 8; i > 0; i--)
591  {
592  first->s6_addr[j] = 0;
593  j--;
594  }
595  first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
596 
597  /* Last IP: Broadcast address - 1. */
598  j = 15;
599  for (i = (128 - block) / 8; i > 0; i--)
600  {
601  last->s6_addr[j] = 0xff;
602  j--;
603  }
604  last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
605 
606  /* /127 => Only two addresses. Don't skip network / broadcast addresses.*/
607  if (block == 127)
608  return 0;
609 
610  /* Increment first IP. */
611  for (i = 15; i >= 0; --i)
612  if (first->s6_addr[i] < 255)
613  {
614  first->s6_addr[i]++;
615  break;
616  }
617  else
618  first->s6_addr[i] = 0;
619  /* Decrement last IP. */
620  for (i = 15; i >= 0; --i)
621  if (last->s6_addr[i] > 0)
622  {
623  last->s6_addr[i]--;
624  break;
625  }
626  else
627  last->s6_addr[i] = 0xff;
628 
629  return 0;
630 }
631 
640 static int
641 is_long_range6_network (const char *str)
642 {
643  char *first_str, *second_str;
644  int ret;
645 
646  first_str = g_strdup (str);
647  second_str = strchr (first_str, '-');
648  if (second_str == NULL)
649  {
650  g_free (first_str);
651  return 0;
652  }
653 
654  /* Separate the addresses. */
655  *second_str = '\0';
656  second_str++;
657 
658  ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
659  g_free (first_str);
660 
661  return ret;
662 }
663 
675 static int
676 long_range6_network_ips (const char *str, struct in6_addr *first,
677  struct in6_addr *last)
678 {
679  char *first_str, *last_str;
680 
681  if (str == NULL || first == NULL || last == NULL)
682  return -1;
683 
684  first_str = g_strdup (str);
685  last_str = strchr (first_str, '-');
686  if (last_str == NULL)
687  {
688  g_free (first_str);
689  return -1;
690  }
691 
692  /* Separate the two IPs. */
693  *last_str = '\0';
694  last_str++;
695 
696  if (inet_pton (AF_INET6, first_str, first) != 1
697  || inet_pton (AF_INET6, last_str, last) != 1)
698  {
699  g_free (first_str);
700  return -1;
701  }
702 
703  g_free (first_str);
704  return 0;
705 }
706 
715 static int
716 is_short_range6_network (const char *str)
717 {
718  char *ip_str, *end_str, *p;
719 
720  ip_str = g_strdup (str);
721  end_str = strchr (ip_str, '-');
722  if (end_str == NULL)
723  {
724  g_free (ip_str);
725  return 0;
726  }
727 
728  /* Separate the addresses. */
729  *end_str = '\0';
730  end_str++;
731 
732  if (!is_ipv6_address (ip_str) || *end_str == '\0')
733  {
734  g_free (ip_str);
735  return 0;
736  }
737 
738  p = end_str;
739  /* Check that the 2nd part is at most 4 hexadecimal characters. */
740  while (isxdigit (*p) && p++)
741  ;
742  if (*p || p - end_str > 4)
743  {
744  g_free (ip_str);
745  return 0;
746  }
747 
748  g_free (ip_str);
749  return 1;
750 }
751 
763 static int
764 short_range6_network_ips (const char *str, struct in6_addr *first,
765  struct in6_addr *last)
766 {
767  char *first_str, *last_str;
768  long int end;
769 
770  if (str == NULL || first == NULL || last == NULL)
771  return -1;
772 
773  first_str = g_strdup (str);
774  last_str = strchr (first_str, '-');
775  if (last_str == NULL)
776  {
777  g_free (first_str);
778  return -1;
779  }
780 
781  /* Separate the first IP. */
782  *last_str = '\0';
783  last_str++;
784 
785  if (inet_pton (AF_INET6, first_str, first) != 1)
786  {
787  g_free (first_str);
788  return -1;
789  }
790 
791  /* Calculate the last IP. */
792  memcpy (last, first, sizeof (*last));
793  end = strtol (last_str, NULL, 16);
794  memcpy (&last->s6_addr[15], &end, 1);
795  memcpy (&last->s6_addr[14], ((char *) &end) + 1, 1);
796 
797  g_free (first_str);
798  return 0;
799 }
800 
809 int
810 gvm_get_host_type (const gchar *str_stripped)
811 {
812  /*
813  * We have a single element with no leading or trailing
814  * white spaces. This element could represent different host
815  * definitions: single IPs, host names, CIDR-expressed blocks,
816  * range-expressed networks, IPv6 addresses.
817  */
818 
819  /* Null or empty string. */
820  if (str_stripped == NULL || *str_stripped == '\0')
821  return -1;
822 
823  /* Check for regular single IPv4 address. */
824  if (is_ipv4_address (str_stripped))
825  return HOST_TYPE_IPV4;
826 
827  /* Check for regular single IPv6 address. */
828  if (is_ipv6_address (str_stripped))
829  return HOST_TYPE_IPV6;
830 
831  /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
832  if (is_cidr_block (str_stripped))
833  return HOST_TYPE_CIDR_BLOCK;
834 
835  /* Check for short range-expressed networks "192.168.12.5-40" */
836  if (is_short_range_network (str_stripped))
837  return HOST_TYPE_RANGE_SHORT;
838 
839  /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
840  if (is_long_range_network (str_stripped))
841  return HOST_TYPE_RANGE_LONG;
842 
843  /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
844  if (is_cidr6_block (str_stripped))
845  return HOST_TYPE_CIDR6_BLOCK;
846 
847  /* Check for short range-expressed networks "::1-ef12" */
848  if (is_short_range6_network (str_stripped))
849  return HOST_TYPE_RANGE6_SHORT;
850 
851  /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
852  if (is_long_range6_network (str_stripped))
853  return HOST_TYPE_RANGE6_LONG;
854 
855  /* Check for hostname. */
856  if (is_hostname (str_stripped))
857  return HOST_TYPE_NAME;
858 
859  return -1;
860 }
861 
870 gvm_vhost_t *
871 gvm_vhost_new (char *value, char *source)
872 {
873  gvm_vhost_t *vhost;
874 
875  vhost = g_malloc0 (sizeof (gvm_vhost_t));
876  vhost->value = value;
877  vhost->source = source;
878 
879  return vhost;
880 }
881 
887 static void
888 gvm_vhost_free (gpointer vhost)
889 {
890  if (vhost)
891  {
892  g_free (((gvm_vhost_t *) vhost)->value);
893  g_free (((gvm_vhost_t *) vhost)->source);
894  }
895  g_free (vhost);
896 }
897 
905 gpointer
906 gvm_duplicate_vhost (gconstpointer vhost, gpointer data)
907 {
908  (void) (data);
909  gvm_vhost_t *ret = NULL;
910 
911  if (!vhost)
912  return NULL;
913 
914  ret = gvm_vhost_new (g_strdup (((gvm_vhost_t *) vhost)->value),
915  g_strdup (((gvm_vhost_t *) vhost)->source));
916 
917  return ret;
918 }
919 
925 static gvm_host_t *
927 {
928  gvm_host_t *host;
929 
930  host = g_malloc0 (sizeof (gvm_host_t));
931 
932  return host;
933 }
934 
940 void
941 gvm_host_free (gpointer host)
942 {
943  gvm_host_t *h = host;
944  if (h == NULL)
945  return;
946 
947  /* If host of type hostname, free the name buffer, first. */
948  if (h->type == HOST_TYPE_NAME)
949  g_free (h->name);
950 
951  g_slist_free_full (h->vhosts, gvm_vhost_free);
952  g_free (h);
953 }
954 
961 void
963 {
964  if (hosts->count == hosts->max_size)
965  {
966  hosts->max_size *= 4;
967  hosts->hosts =
968  g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
969  memset (hosts->hosts + hosts->count, '\0',
970  (hosts->max_size - hosts->count) * sizeof (gvm_host_t *));
971  }
972  hosts->hosts[hosts->count] = host;
973  hosts->count++;
974 }
975 
983 static gvm_hosts_t *
984 gvm_hosts_init (const char *hosts_str)
985 {
986  gvm_hosts_t *hosts;
987 
988  hosts = g_malloc0 (sizeof (gvm_hosts_t));
989  hosts->max_size = 1024;
990  hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
991  hosts->orig_str = g_strdup (hosts_str);
992  return hosts;
993 }
994 
1001 static void
1003 {
1004  size_t i;
1005  if (!hosts)
1006  return;
1007 
1008  for (i = 0; i < hosts->max_size; i++)
1009  {
1010  if (!hosts->hosts[i])
1011  {
1012  size_t j;
1013 
1014  /* Fill the gap with the closest host entry, in order to keep the
1015  * sequential ordering. */
1016  for (j = i + 1; j < hosts->max_size; j++)
1017  {
1018  if (hosts->hosts[j])
1019  {
1020  hosts->hosts[i] = hosts->hosts[j];
1021  hosts->hosts[j] = NULL;
1022  break;
1023  }
1024  }
1025  /* No more entries left, ie. the empty space between count and
1026  * max_size. */
1027  if (!hosts->hosts[i])
1028  return;
1029  }
1030  }
1031 }
1032 
1039 static void
1041 {
1045  GHashTable *name_table;
1046  size_t i, duplicates = 0;
1047 
1048  if (hosts == NULL)
1049  return;
1050  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1051 
1052  for (i = 0; i < hosts->count; i++)
1053  {
1054  gchar *name;
1055 
1056  if ((name = gvm_host_value_str (hosts->hosts[i])))
1057  {
1058  gvm_host_t *host, *removed = hosts->hosts[i];
1059 
1060  host = g_hash_table_lookup (name_table, name);
1061  if (host)
1062  {
1063  /* Remove duplicate host. Add its vhosts to the original host. */
1064  host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
1065  removed->vhosts = NULL;
1066  gvm_host_free (removed);
1067  hosts->hosts[i] = NULL;
1068  duplicates++;
1069  g_free (name);
1070  }
1071  else
1072  g_hash_table_insert (name_table, name, hosts->hosts[i]);
1073  }
1074  }
1075 
1076  if (duplicates)
1077  gvm_hosts_fill_gaps (hosts);
1078  g_hash_table_destroy (name_table);
1079  hosts->count -= duplicates;
1080  hosts->duplicated += duplicates;
1081  hosts->current = 0;
1082 #ifdef __GLIBC__
1083  malloc_trim (0);
1084 #endif
1085 }
1086 
1098 gvm_hosts_t *
1099 gvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
1100 {
1101  gvm_hosts_t *hosts;
1102  gchar **host_element, **split;
1103  gchar *str;
1104 
1105  if (hosts_str == NULL)
1106  return NULL;
1107 
1108  /* Normalize separator: Transform newlines into commas. */
1109  hosts = gvm_hosts_init (hosts_str);
1110  str = hosts->orig_str;
1111  while (*str)
1112  {
1113  if (*str == '\n')
1114  *str = ',';
1115  str++;
1116  }
1117 
1118  /* Split comma-separated list into single host-specifications */
1119  split = g_strsplit (hosts->orig_str, ",", 0);
1120 
1121  /* first element of the split list */
1122  host_element = split;
1123  while (*host_element)
1124  {
1125  int host_type;
1126  gchar *stripped = g_strstrip (*host_element);
1127 
1128  if (stripped == NULL || *stripped == '\0')
1129  {
1130  host_element++;
1131  continue;
1132  }
1133 
1134  /* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ?
1135  */
1136  /* -1 if error. */
1137  host_type = gvm_get_host_type (stripped);
1138 
1139  switch (host_type)
1140  {
1141  case HOST_TYPE_NAME:
1142  case HOST_TYPE_IPV4:
1143  case HOST_TYPE_IPV6:
1144  {
1145  /* New host. */
1146  gvm_host_t *host = gvm_host_new ();
1147  host->type = host_type;
1148  if (host_type == HOST_TYPE_NAME)
1149  host->name = g_ascii_strdown (stripped, -1);
1150  else if (host_type == HOST_TYPE_IPV4)
1151  {
1152  if (inet_pton (AF_INET, stripped, &host->addr) != 1)
1153  break;
1154  }
1155  else if (host_type == HOST_TYPE_IPV6)
1156  {
1157  if (inet_pton (AF_INET6, stripped, &host->addr6) != 1)
1158  break;
1159  }
1160  gvm_hosts_add (hosts, host);
1161  break;
1162  }
1163  case HOST_TYPE_CIDR_BLOCK:
1164  case HOST_TYPE_RANGE_SHORT:
1165  case HOST_TYPE_RANGE_LONG:
1166  {
1167  struct in_addr first, last;
1168  uint32_t current;
1169  int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
1170 
1172  ips_func = cidr_block_ips;
1173  else if (host_type == HOST_TYPE_RANGE_SHORT)
1174  ips_func = short_range_network_ips;
1175  else
1176  ips_func = long_range_network_ips;
1177 
1178  if (ips_func (stripped, &first, &last) == -1)
1179  break;
1180 
1181  /* Make sure that first actually comes before last */
1182  if (ntohl (first.s_addr) > ntohl (last.s_addr))
1183  break;
1184 
1185  /* Add addresses from first to last as single hosts. */
1186  current = first.s_addr;
1187  while (ntohl (current) <= ntohl (last.s_addr))
1188  {
1189  gvm_host_t *host;
1190  if (max_hosts > 0 && hosts->count > max_hosts)
1191  {
1192  g_strfreev (split);
1193  gvm_hosts_free (hosts);
1194  return NULL;
1195  }
1196  host = gvm_host_new ();
1197  host->type = HOST_TYPE_IPV4;
1198  host->addr.s_addr = current;
1199  gvm_hosts_add (hosts, host);
1200  /* Next IP address. */
1201  current = htonl (ntohl (current) + 1);
1202  }
1203  break;
1204  }
1205  case HOST_TYPE_CIDR6_BLOCK:
1206  case HOST_TYPE_RANGE6_LONG:
1208  {
1209  struct in6_addr first, last;
1210  unsigned char current[16];
1211  int (*ips_func) (const char *, struct in6_addr *,
1212  struct in6_addr *);
1213 
1215  ips_func = cidr6_block_ips;
1216  else if (host_type == HOST_TYPE_RANGE6_SHORT)
1217  ips_func = short_range6_network_ips;
1218  else
1219  ips_func = long_range6_network_ips;
1220 
1221  if (ips_func (stripped, &first, &last) == -1)
1222  break;
1223 
1224  /* Make sure the first comes before the last. */
1225  if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1226  break;
1227 
1228  /* Add addresses from first to last as single hosts. */
1229  memcpy (current, &first.s6_addr, 16);
1230  while (memcmp (current, &last.s6_addr, 16) <= 0)
1231  {
1232  int i;
1233  gvm_host_t *host;
1234 
1235  if (max_hosts > 0 && hosts->count > max_hosts)
1236  {
1237  g_strfreev (split);
1238  gvm_hosts_free (hosts);
1239  return NULL;
1240  }
1241  host = gvm_host_new ();
1242  host->type = HOST_TYPE_IPV6;
1243  memcpy (host->addr6.s6_addr, current, 16);
1244  gvm_hosts_add (hosts, host);
1245  /* Next IPv6 address. */
1246  for (i = 15; i >= 0; --i)
1247  if (current[i] < 255)
1248  {
1249  current[i]++;
1250  break;
1251  }
1252  else
1253  current[i] = 0;
1254  }
1255  break;
1256  }
1257  case -1:
1258  default:
1259  /* Invalid host string. */
1260  g_strfreev (split);
1261  gvm_hosts_free (hosts);
1262  return NULL;
1263  }
1264  host_element++; /* move on to next element of split list */
1265  if (max_hosts > 0 && hosts->count > max_hosts)
1266  {
1267  g_strfreev (split);
1268  gvm_hosts_free (hosts);
1269  return NULL;
1270  }
1271  }
1272 
1273  /* No need to check for duplicates when a hosts string contains a
1274  * single (IP/Hostname/Range/Subnetwork) entry. */
1275  if (g_strv_length (split) > 1)
1276  gvm_hosts_deduplicate (hosts);
1277 
1278  g_strfreev (split);
1279 #ifdef __GLIBC__
1280  malloc_trim (0);
1281 #endif
1282  return hosts;
1283 }
1284 
1295 gvm_hosts_t *
1296 gvm_hosts_new (const gchar *hosts_str)
1297 {
1298  return gvm_hosts_new_with_max (hosts_str, 0);
1299 }
1300 
1309 gvm_host_t *
1311 {
1312  if (!hosts || hosts->current == hosts->count)
1313  return NULL;
1314 
1315  return hosts->hosts[hosts->current++];
1316 }
1317 
1327 void
1329 {
1330  void *host_tmp;
1331  size_t i;
1332 
1333  if (!hosts)
1334  return;
1335 
1336  if (hosts->current == hosts->count)
1337  {
1338  hosts->current -= 1;
1339  return;
1340  }
1341 
1342  hosts->current -= 1;
1343  host_tmp = hosts->hosts[hosts->current];
1344 
1345  for (i = hosts->current; i < hosts->count; i++)
1346  hosts->hosts[i - 1] = hosts->hosts[i];
1347 
1348  hosts->hosts[hosts->count - 1] = host_tmp;
1349 }
1350 
1357 void
1359 {
1360  size_t i;
1361 
1362  if (hosts == NULL)
1363  return;
1364 
1365  if (hosts->orig_str)
1366  g_free (hosts->orig_str);
1367  for (i = 0; i < hosts->count; i++)
1368  gvm_host_free (hosts->hosts[i]);
1369  g_free (hosts->hosts);
1370  g_free (hosts);
1371  hosts = NULL;
1372 }
1373 
1381 void
1383 {
1384  size_t i = 0;
1385  GRand *rand;
1386 
1387  if (hosts == NULL)
1388  return;
1389 
1390  /* Shuffle the array. */
1391  rand = g_rand_new ();
1392  for (i = 0; i < hosts->count; i++)
1393  {
1394  void *tmp;
1395  int j = g_rand_int_range (rand, 0, hosts->count);
1396 
1397  tmp = hosts->hosts[i];
1398  hosts->hosts[i] = hosts->hosts[j];
1399  hosts->hosts[j] = tmp;
1400  }
1401 
1402  hosts->current = 0;
1403  g_rand_free (rand);
1404 }
1405 
1413 void
1415 {
1416  size_t i, j;
1417  if (hosts == NULL)
1418  return;
1419 
1420  for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1421  {
1422  gvm_host_t *tmp = hosts->hosts[i];
1423  hosts->hosts[i] = hosts->hosts[j];
1424  hosts->hosts[j] = tmp;
1425  }
1426  hosts->current = 0;
1427 }
1428 
1439 GSList *
1441 {
1442  size_t i, new_entries = 0, resolved = 0;
1443  GSList *unresolved = NULL;
1444 
1445  for (i = 0; i < hosts->count; i++)
1446  {
1447  GSList *list, *tmp;
1448  gvm_host_t *host = hosts->hosts[i];
1449 
1450  if (host->type != HOST_TYPE_NAME)
1451  continue;
1452 
1453  list = tmp = gvm_resolve_list (host->name);
1454  while (tmp)
1455  {
1456  /* Create a new host for each IP address. */
1457  gvm_host_t *new;
1458  struct in6_addr *ip6 = tmp->data;
1459  gvm_vhost_t *vhost;
1460 
1461  new = gvm_host_new ();
1462  if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1463  || ip6->s6_addr32[2] != htonl (0xffff))
1464  {
1465  new->type = HOST_TYPE_IPV6;
1466  memcpy (&new->addr6, ip6, sizeof (new->addr6));
1467  }
1468  else
1469  {
1470  new->type = HOST_TYPE_IPV4;
1471  memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1472  }
1473  vhost =
1474  gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1475  new->vhosts = g_slist_prepend (new->vhosts, vhost);
1476  gvm_hosts_add (hosts, new);
1477  tmp = tmp->next;
1478  new_entries = 1;
1479  }
1480  /* Remove hostname from list, as it was either replaced by IPs, or
1481  * is unresolvable. */
1482  hosts->hosts[i] = NULL;
1483  resolved++;
1484  if (!list)
1485  unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
1486  gvm_host_free (host);
1487  g_slist_free_full (list, g_free);
1488  }
1489  if (resolved)
1490  gvm_hosts_fill_gaps (hosts);
1491  hosts->count -= resolved;
1492  hosts->removed += resolved;
1493  if (new_entries)
1494  gvm_hosts_deduplicate (hosts);
1495  hosts->current = 0;
1496  return unresolved;
1497 }
1498 
1507 int
1508 gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
1509 {
1510  GSList *vhost;
1511  char **excluded;
1512  int ret = 0;
1513 
1514  if (!host || !excluded_str)
1515  return ret;
1516 
1517  vhost = host->vhosts;
1518  excluded = g_strsplit (excluded_str, ",", 0);
1519  if (!excluded || !*excluded)
1520  {
1521  g_strfreev (excluded);
1522  return ret;
1523  }
1524  while (vhost)
1525  {
1526  char **tmp = excluded;
1527  char *value = ((gvm_vhost_t *) vhost->data)->value;
1528 
1529  while (*tmp)
1530  {
1531  if (!strcasecmp (value, g_strstrip (*tmp)))
1532  {
1533  gvm_vhost_free (vhost->data);
1534  host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1535  ret++;
1536  break;
1537  }
1538  tmp++;
1539  if (!*tmp)
1540  {
1541  vhost = vhost->next;
1542  break;
1543  }
1544  }
1545  }
1546  g_strfreev (excluded);
1547 
1548  return ret;
1549 }
1550 
1562 int
1563 gvm_hosts_exclude_with_max (gvm_hosts_t *hosts, const char *excluded_str,
1564  unsigned int max_hosts)
1565 {
1569  gvm_hosts_t *excluded_hosts;
1570  GHashTable *name_table;
1571  size_t excluded = 0, i;
1572 
1573  if (hosts == NULL || excluded_str == NULL)
1574  return -1;
1575 
1576  excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1577  if (excluded_hosts == NULL)
1578  return -1;
1579 
1580  if (gvm_hosts_count (excluded_hosts) == 0)
1581  {
1582  gvm_hosts_free (excluded_hosts);
1583  return 0;
1584  }
1585 
1586  /* Hash host values from excluded hosts list. */
1587  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1588  for (i = 0; i < excluded_hosts->count; i++)
1589  {
1590  gchar *name;
1591 
1592  if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
1593  g_hash_table_insert (name_table, name, hosts);
1594  }
1595 
1596  /* Check for hosts values in hash table. */
1597  for (i = 0; i < hosts->count; i++)
1598  {
1599  gchar *name;
1600 
1601  if ((name = gvm_host_value_str (hosts->hosts[i])))
1602  {
1603  if (g_hash_table_lookup (name_table, name))
1604  {
1605  gvm_host_free (hosts->hosts[i]);
1606  hosts->hosts[i] = NULL;
1607  excluded++;
1608  g_free (name);
1609  continue;
1610  }
1611  g_free (name);
1612  }
1613  }
1614 
1615  /* Cleanup. */
1616  if (excluded)
1617  gvm_hosts_fill_gaps (hosts);
1618  hosts->count -= excluded;
1619  hosts->removed += excluded;
1620  hosts->current = 0;
1621  g_hash_table_destroy (name_table);
1622  gvm_hosts_free (excluded_hosts);
1623  return excluded;
1624 }
1625 
1638 GSList *
1639 gvm_hosts_allowed_only (gvm_hosts_t *hosts, const char *deny_hosts_str,
1640  const char *allow_hosts_str)
1641 {
1645  gvm_hosts_t *allowed_hosts, *denied_hosts;
1646  GHashTable *name_allow_table = NULL, *name_deny_table = NULL;
1647  GSList *removed = NULL;
1648  size_t excluded = 0, i;
1649 
1650  if (hosts == NULL || (deny_hosts_str == NULL && allow_hosts_str == NULL))
1651  return NULL;
1652 
1653  // Prepare list of denied and allowed hosts
1654  denied_hosts = gvm_hosts_new_with_max (deny_hosts_str, 0);
1655  allowed_hosts = gvm_hosts_new_with_max (allow_hosts_str, 0);
1656  if (denied_hosts == NULL && allowed_hosts == NULL)
1657  return NULL;
1658 
1659  if (gvm_hosts_count (denied_hosts) == 0)
1660  gvm_hosts_free (denied_hosts);
1661  else
1662  {
1663  /* Hash host values from denied hosts list. */
1664  name_deny_table =
1665  g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1666  for (i = 0; i < denied_hosts->count; i++)
1667  {
1668  gchar *name;
1669 
1670  if ((name = gvm_host_value_str (denied_hosts->hosts[i])))
1671  g_hash_table_insert (name_deny_table, name, hosts);
1672  }
1673  }
1674  if (gvm_hosts_count (allowed_hosts) == 0)
1675  gvm_hosts_free (allowed_hosts);
1676  else
1677  {
1678  /* Hash host values from allowed hosts list. */
1679  name_allow_table =
1680  g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1681  for (i = 0; i < allowed_hosts->count; i++)
1682  {
1683  gchar *name;
1684 
1685  if ((name = gvm_host_value_str (allowed_hosts->hosts[i])))
1686  g_hash_table_insert (name_allow_table, name, hosts);
1687  }
1688  }
1689 
1690  /* Check for authorized hosts in hash table and create a list of removed
1691  * hosts. */
1692  for (i = 0; i < hosts->count; i++)
1693  {
1694  gchar *name;
1695 
1696  if ((name = gvm_host_value_str (hosts->hosts[i])))
1697  {
1698  if (denied_hosts != NULL
1699  && g_hash_table_lookup (name_deny_table, name))
1700  {
1701  gvm_host_free (hosts->hosts[i]);
1702  hosts->hosts[i] = NULL;
1703  excluded++;
1704  removed = g_slist_prepend (removed, name);
1705  continue;
1706  }
1707  else if (allowed_hosts != NULL
1708  && !g_hash_table_lookup (name_allow_table, name))
1709  {
1710  gvm_host_free (hosts->hosts[i]);
1711  hosts->hosts[i] = NULL;
1712  excluded++;
1713  removed = g_slist_prepend (removed, name);
1714  continue;
1715  }
1716  g_free (name);
1717  }
1718  }
1719 
1720  /* Cleanup. */
1721  if (excluded)
1722  gvm_hosts_fill_gaps (hosts);
1723 
1724  hosts->count -= excluded;
1725  hosts->removed += excluded;
1726  hosts->current = 0;
1727  if (name_allow_table != NULL)
1728  g_hash_table_destroy (name_allow_table);
1729  if (name_deny_table != NULL)
1730  g_hash_table_destroy (name_deny_table);
1731  if (allowed_hosts != NULL)
1732  gvm_hosts_free (allowed_hosts);
1733  if (denied_hosts != NULL)
1734  gvm_hosts_free (denied_hosts);
1735  return removed;
1736 }
1737 
1748 int
1749 gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str)
1750 {
1751  return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1752 }
1753 
1764 gvm_host_t *
1765 gvm_host_from_str (const gchar *host_str)
1766 {
1767  int host_type;
1768 
1769  if (host_str == NULL)
1770  return NULL;
1771 
1772  /* IPv4, hostname, IPv6 */
1773  /* -1 if error. */
1774  host_type = gvm_get_host_type (host_str);
1775 
1776  switch (host_type)
1777  {
1778  case HOST_TYPE_NAME:
1779  case HOST_TYPE_IPV4:
1780  case HOST_TYPE_IPV6:
1781  {
1782  /* New host. */
1783  gvm_host_t *host = gvm_host_new ();
1784  host->type = host_type;
1785  if (host_type == HOST_TYPE_NAME)
1786  host->name = g_ascii_strdown (host_str, -1);
1787  else if (host_type == HOST_TYPE_IPV4)
1788  {
1789  if (inet_pton (AF_INET, host_str, &host->addr) != 1)
1790  break;
1791  }
1792  else if (host_type == HOST_TYPE_IPV6)
1793  {
1794  if (inet_pton (AF_INET6, host_str, &host->addr6) != 1)
1795  break;
1796  }
1797  return host;
1798  }
1799  case -1:
1800  default:
1801  return NULL;
1802  }
1803  return NULL;
1804 }
1805 
1813 char *
1815 {
1816  int retry = 10;
1817  gchar hostname[NI_MAXHOST];
1818  void *addr;
1819  size_t addrlen;
1820  struct sockaddr_in sa;
1821  struct sockaddr_in6 sa6;
1822 
1823  if (!host)
1824  return NULL;
1825 
1826  if (host->type == HOST_TYPE_IPV4)
1827  {
1828  addr = &sa;
1829  addrlen = sizeof (sa);
1830  memset (addr, '\0', addrlen);
1831  sa.sin_addr = host->addr;
1832  sa.sin_family = AF_INET;
1833  }
1834  else if (host->type == HOST_TYPE_IPV6)
1835  {
1836  addr = &sa6;
1837  addrlen = sizeof (sa6);
1838  memset (&sa6, '\0', addrlen);
1839  memcpy (&sa6.sin6_addr, &host->addr6, 16);
1840  sa6.sin6_family = AF_INET6;
1841  }
1842  else
1843  return NULL;
1844 
1845  while (retry--)
1846  {
1847  int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
1848  0, NI_NAMEREQD);
1849  if (!ret)
1850  return g_ascii_strdown (hostname, -1);
1851  if (ret != EAI_AGAIN)
1852  break;
1853  usleep (10000); // 10ms
1854  }
1855  return NULL;
1856 }
1857 
1866 static int
1867 host_name_verify (gvm_host_t *host, const char *value)
1868 {
1869  GSList *list, *tmp;
1870  char *host_str;
1871  int ret = -1;
1872 
1873  assert (host);
1874  assert (value);
1875  host_str = gvm_host_value_str (host);
1876  list = tmp = gvm_resolve_list (value);
1877  while (tmp)
1878  {
1879  char buffer[INET6_ADDRSTRLEN];
1880  addr6_to_str (tmp->data, buffer);
1881  if (!strcasecmp (host_str, buffer))
1882  {
1883  ret = 0;
1884  break;
1885  }
1886  tmp = tmp->next;
1887  }
1888  g_free (host_str);
1889  g_slist_free_full (list, g_free);
1890  return ret;
1891 }
1892 
1898 void
1900 {
1901  GSList *vhosts;
1902  gvm_vhost_t *vhost;
1903  char *value;
1904 
1905  if (!host || host->type == HOST_TYPE_NAME)
1906  return;
1907 
1908  value = gvm_host_reverse_lookup (host);
1909  if (!value)
1910  return;
1911  if (host_name_verify (host, value))
1912  {
1913  g_free (value);
1914  return;
1915  }
1916  /* Don't add vhost, if already in the list. */
1917  vhosts = host->vhosts;
1918  while (vhosts)
1919  {
1920  if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1921  {
1922  g_free (value);
1923  return;
1924  }
1925  vhosts = vhosts->next;
1926  }
1927  vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1928  host->vhosts = g_slist_prepend (host->vhosts, vhost);
1929 }
1930 
1940 gvm_hosts_t *
1942 {
1943  size_t i, count = 0;
1944  gvm_hosts_t *excluded = gvm_hosts_new ("");
1945 
1946  if (hosts == NULL)
1947  return NULL;
1948 
1949  for (i = 0; i < hosts->count; i++)
1950  {
1951  gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1952 
1953  if (name == NULL)
1954  {
1955  gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
1956  gvm_host_free (hosts->hosts[i]);
1957  hosts->hosts[i] = NULL;
1958  count++;
1959  }
1960  else
1961  g_free (name);
1962  }
1963 
1964  if (count)
1965  gvm_hosts_fill_gaps (hosts);
1966  hosts->count -= count;
1967  hosts->removed += count;
1968  hosts->current = 0;
1969  return excluded;
1970 }
1971 
1981 int
1983 {
1984  gvm_hosts_t *excluded;
1985  int count = 0;
1986 
1987  if (hosts == NULL)
1988  return -1;
1989 
1990  excluded = gvm_hosts_reverse_lookup_only_excluded (hosts);
1991  count = excluded->count;
1992  gvm_hosts_free (excluded);
1993 
1994  return count;
1995 }
1996 
2006 gvm_hosts_t *
2008 {
2012  size_t i, count = 0;
2013  GHashTable *name_table;
2014  gvm_hosts_t *excluded = NULL;
2015 
2016  if (hosts == NULL)
2017  return NULL;
2018 
2019  excluded = gvm_hosts_new ("");
2020  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
2021  for (i = 0; i < hosts->count; i++)
2022  {
2023  gchar *name;
2024 
2025  if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
2026  {
2027  if (g_hash_table_lookup (name_table, name))
2028  {
2029  gvm_hosts_add (excluded, gvm_duplicate_host (hosts->hosts[i]));
2030  gvm_host_free (hosts->hosts[i]);
2031  hosts->hosts[i] = NULL;
2032  count++;
2033  g_free (name);
2034  }
2035  else
2036  {
2037  /* Insert in the hash table. Value not important. */
2038  g_hash_table_insert (name_table, name, hosts);
2039  }
2040  }
2041  }
2042 
2043  if (count)
2044  gvm_hosts_fill_gaps (hosts);
2045  g_hash_table_destroy (name_table);
2046  hosts->removed += count;
2047  hosts->count -= count;
2048  hosts->current = 0;
2049  return excluded;
2050 }
2051 
2061 int
2063 {
2064  gvm_hosts_t *excluded = NULL;
2065  int count = 0;
2066  if (hosts == NULL)
2067  return -1;
2068 
2069  excluded = gvm_hosts_reverse_lookup_unify_excluded (hosts);
2070  count = excluded->count;
2071  gvm_hosts_free (excluded);
2072 
2073  return count;
2074 }
2082 unsigned int
2084 {
2085  return hosts ? hosts->count : 0;
2086 }
2087 
2096 unsigned int
2098 {
2099  return hosts ? hosts->removed : 0;
2100 }
2101 
2110 unsigned int
2112 {
2113  return hosts ? hosts->duplicated : 0;
2114 }
2115 
2126 gvm_host_t *
2127 gvm_host_find_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
2128  const gvm_hosts_t *hosts)
2129 {
2130  char *host_str;
2131  size_t i;
2132 
2133  if (host == NULL || hosts == NULL)
2134  return NULL;
2135 
2136  host_str = gvm_host_value_str (host);
2137 
2138  for (i = 0; i < hosts->count; i++)
2139  {
2140  gvm_host_t *current_host = hosts->hosts[i];
2141  char *tmp = gvm_host_value_str (current_host);
2142 
2143  if (strcasecmp (host_str, tmp) == 0)
2144  {
2145  g_free (host_str);
2146  g_free (tmp);
2147  return current_host;
2148  }
2149  g_free (tmp);
2150 
2151  /* Hostnames in hosts list shouldn't be resolved. */
2152  if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
2153  {
2154  struct in6_addr tmpaddr;
2155  gvm_host_get_addr6 (current_host, &tmpaddr);
2156 
2157  if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
2158  {
2159  g_free (host_str);
2160  return current_host;
2161  }
2162  }
2163  }
2164 
2165  g_free (host_str);
2166  return NULL;
2167 }
2168 
2175 gvm_host_t *
2177 {
2178  gvm_host_t *ret = NULL;
2179 
2180  if (host == NULL)
2181  return NULL;
2182 
2183  ret = gvm_host_new ();
2184 
2185  ret->type = host->type;
2186  switch (host->type)
2187  {
2188  case HOST_TYPE_NAME:
2189  ret->name = g_strdup (host->name);
2190  break;
2191  case HOST_TYPE_IPV4:
2192  ret->addr.s_addr = host->addr.s_addr;
2193  break;
2194  case HOST_TYPE_IPV6:
2195  ret->addr6.__in6_u = host->addr6.__in6_u;
2196  break;
2197  default:
2198  g_free (ret);
2199  return NULL;
2200  }
2201  ret->vhosts = g_slist_copy_deep (host->vhosts, gvm_duplicate_vhost, NULL);
2202 
2203  return ret;
2204 }
2205 
2218 int
2219 gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
2220  const gvm_hosts_t *hosts)
2221 {
2222  if (gvm_host_find_in_hosts (host, addr, hosts))
2223  return 1;
2224 
2225  return 0;
2226 }
2227 
2235 enum host_type
2236 gvm_host_type (const gvm_host_t *host)
2237 {
2238  assert (host);
2239  return host->type;
2240 }
2241 
2250 gchar *
2252 {
2253  if (host == NULL)
2254  return NULL;
2255 
2256  return host_type_str[host->type];
2257 }
2258 
2266 gchar *
2268 {
2269  if (host == NULL)
2270  return NULL;
2271 
2272  switch (host->type)
2273  {
2274  case HOST_TYPE_NAME:
2275  return g_strdup (host->name);
2276  break;
2277  case HOST_TYPE_IPV4:
2278  case HOST_TYPE_IPV6:
2279  /* Handle both cases using inet_ntop(). */
2280  {
2281  int family, size;
2282  gchar *str;
2283  const void *srcaddr;
2284 
2285  if (host->type == HOST_TYPE_IPV4)
2286  {
2287  family = AF_INET;
2288  size = INET_ADDRSTRLEN;
2289  srcaddr = &host->addr;
2290  }
2291  else
2292  {
2293  family = AF_INET6;
2294  size = INET6_ADDRSTRLEN;
2295  srcaddr = &host->addr6;
2296  }
2297 
2298  str = g_malloc0 (size);
2299  if (inet_ntop (family, srcaddr, str, size) == NULL)
2300  {
2301  perror ("inet_ntop");
2302  g_free (str);
2303  return NULL;
2304  }
2305  return str;
2306  }
2307  default:
2308  return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
2309  }
2310 }
2311 
2323 int
2324 gvm_host_resolve (const gvm_host_t *host, void *dst, int family)
2325 {
2326  if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
2327  return -1;
2328 
2329  return gvm_resolve (host->name, dst, family);
2330 }
2331 
2344 int
2345 gvm_host_get_addr6 (const gvm_host_t *host, struct in6_addr *ip6)
2346 {
2347  if (host == NULL || ip6 == NULL)
2348  return -1;
2349 
2350  switch (gvm_host_type (host))
2351  {
2352  case HOST_TYPE_IPV6:
2353  memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
2354  return 0;
2355 
2356  case HOST_TYPE_IPV4:
2357  ipv4_as_ipv6 (&host->addr, ip6);
2358  return 0;
2359 
2360  case HOST_TYPE_NAME:
2361  {
2362  struct in_addr ip4;
2363 
2364  /* Fail if IPv4 and IPv6 both don't resolve. */
2365  if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
2366  ipv4_as_ipv6 (&ip4, ip6);
2367  else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
2368  return -1;
2369  return 0;
2370  }
2371 
2372  default:
2373  return -1;
2374  }
2375 }
gvm_host_value_str
gchar * gvm_host_value_str(const gvm_host_t *host)
Gets a host's value in printable format.
Definition: hosts.c:2267
long_range6_network_ips
static int long_range6_network_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last IPv6 addresses from a long range-expressed network. eg. "::1:200:7-::1:205:50...
Definition: hosts.c:676
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 ove...
Definition: hosts.c:1982
gvm_host::name
gchar * name
Definition: hosts.h:64
addr6_to_str
void addr6_to_str(const struct in6_addr *addr6, char *str)
Stringifies an IP address.
Definition: networking.c:261
networking.h
GVM Networking related API.
cidr6_block_ips
static int cidr6_block_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last usable IPv4 addresses from a CIDR-expressed block. eg. "192....
Definition: hosts.c:569
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 t...
Definition: hosts.c:2062
long_range_network_ips
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
is_short_range_network
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_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.
Definition: hosts.c:1639
is_hostname
static int is_hostname(const char *str)
Checks if a buffer points to a valid hostname.
Definition: hosts.c:396
gvm_hosts::count
size_t count
Definition: hosts.h:93
gvm_hosts::removed
size_t removed
Definition: hosts.h:94
gvm_hosts_free
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition: hosts.c:1358
gvm_resolve_list
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition: networking.c:339
is_long_range_network
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_init
static gvm_hosts_t * gvm_hosts_init(const char *hosts_str)
Creates a hosts collection from a hosts string.
Definition: hosts.c:984
is_cidr6_block
static int is_cidr6_block(const char *str)
Checks if a buffer points to an IPv6 CIDR-expressed block. "2620:0:2d0:200::7/120" is valid,...
Definition: hosts.c:467
ipv4_as_ipv6
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
HOST_TYPE_IPV6
@ HOST_TYPE_IPV6
Definition: hosts.h:40
gvm_host_type_str
gchar * gvm_host_type_str(const gvm_host_t *host)
Gets a host's type in printable format.
Definition: hosts.c:2251
gvm_hosts::duplicated
size_t duplicated
Definition: hosts.h:95
short_range6_network_ips
static int short_range6_network_ips(const char *str, struct in6_addr *first, struct in6_addr *last)
Gets the first and last IPv6 addresses from a short range-expressed network. eg. "\::ffee:1:1001-1005...
Definition: hosts.c:764
gvm_hosts::max_size
size_t max_size
Definition: hosts.h:91
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.
Definition: hosts.c:962
gvm_hosts_count
unsigned int gvm_hosts_count(const gvm_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
Definition: hosts.c:2083
is_long_range6_network
static int is_long_range6_network(const char *str)
Checks if a buffer points to a valid long IPv6 range-expressed network. "::fee5-::1:530" is valid.
Definition: hosts.c:641
HOST_TYPE_RANGE_SHORT
@ HOST_TYPE_RANGE_SHORT
Definition: hosts.h:38
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.
Definition: hosts.c:1099
gvm_host::type
enum host_type type
Definition: hosts.h:68
gvm_resolve
int gvm_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
Definition: networking.c:389
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 ad...
Definition: hosts.c:2345
gvm_vhost
The structure for a single vhost object.
Definition: hosts.h:76
HOST_TYPE_IPV4
@ HOST_TYPE_IPV4
Definition: hosts.h:36
cidr6_get_ip
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:534
gvm_duplicate_vhost
gpointer gvm_duplicate_vhost(gconstpointer vhost, gpointer data)
Creates a deep copy of a gvm_vhost_t object.
Definition: hosts.c:906
gvm_hosts::hosts
gvm_host_t ** hosts
Definition: hosts.h:90
gvm_get_host_type
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition: hosts.c:810
gvm_host
The structure for a single host object.
Definition: hosts.h:61
HOST_TYPE_CIDR6_BLOCK
@ HOST_TYPE_CIDR6_BLOCK
Definition: hosts.h:41
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 ...
Definition: hosts.c:1749
gvm_hosts::current
size_t current
Definition: hosts.h:92
cidr_block_ips
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
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.
Definition: hosts.c:1296
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.
Definition: hosts.c:1508
gvm_vhost::value
char * value
Definition: hosts.h:77
gvm_host_free
void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:941
HOST_TYPE_RANGE_LONG
@ HOST_TYPE_RANGE_LONG
Definition: hosts.h:39
gvm_host::addr
struct in_addr addr
Definition: hosts.h:65
HOST_TYPE_RANGE6_LONG
@ HOST_TYPE_RANGE6_LONG
Definition: hosts.h:42
gvm_host_type
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition: hosts.c:2236
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...
Definition: hosts.c:2219
is_cidr_block
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_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 ...
Definition: hosts.c:1563
is_ipv4_address
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
gvm_host_new
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition: hosts.c:926
cidr_get_ip
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
gvm_host_reverse_lookup
char * gvm_host_reverse_lookup(gvm_host_t *host)
Checks for a host object reverse dns lookup existence.
Definition: hosts.c:1814
gvm_vhost::source
char * source
Definition: hosts.h:78
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 with...
Definition: hosts.c:1310
HOST_TYPE_MAX
@ HOST_TYPE_MAX
Definition: hosts.h:44
is_short_range6_network
static int is_short_range6_network(const char *str)
Checks if a buffer points to a valid short IPv6 range-expressed network. "::200:ff:1-fee5" is valid.
Definition: hosts.c:716
gvm_vhost_free
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition: hosts.c:888
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 ...
Definition: hosts.c:1382
gvm_hosts::orig_str
gchar * orig_str
Definition: hosts.h:89
host_name_verify
static int host_name_verify(gvm_host_t *host, const char *value)
Verifies that hostname value resolves to a host's IP.
Definition: hosts.c:1867
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.
Definition: hosts.c:1328
host_type
host_type
Definition: hosts.h:34
host_type_str
gchar * host_type_str[HOST_TYPE_MAX]
Definition: hosts.c:40
cidr_get_block
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
gvm_host::addr6
struct in6_addr addr6
Definition: hosts.h:66
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 si...
Definition: hosts.c:1414
gvm_vhost_new
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition: hosts.c:871
HOST_TYPE_CIDR_BLOCK
@ HOST_TYPE_CIDR_BLOCK
Definition: hosts.h:37
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....
Definition: hosts.c:1440
gvm_hosts_fill_gaps
static void gvm_hosts_fill_gaps(gvm_hosts_t *hosts)
Fill the gaps in the array of a hosts collection, which are caused by the removal of host entries.
Definition: hosts.c:1002
short_range_network_ips
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
cidr6_get_block
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:513
HOST_TYPE_RANGE6_SHORT
@ HOST_TYPE_RANGE6_SHORT
Definition: hosts.h:43
gvm_hosts_deduplicate
static void gvm_hosts_deduplicate(gvm_hosts_t *hosts)
Removes duplicate hosts values from an gvm_hosts_t structure. Also resets the iterator current positi...
Definition: hosts.c:1040
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 l...
Definition: hosts.c:2111
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.
Definition: hosts.c:1899
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.
Definition: hosts.c:2127
is_ipv6_address
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
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.
Definition: hosts.c:2176
HOST_TYPE_NAME
@ HOST_TYPE_NAME
Definition: hosts.h:35
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_NAM...
Definition: hosts.c:2324
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 ove...
Definition: hosts.c:1941
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....
Definition: hosts.c:2097
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.
Definition: hosts.c:1765
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 t...
Definition: hosts.c:2007
gvm_host::vhosts
GSList * vhosts
Definition: hosts.h:69
gvm_hosts
The structure for Hosts collection.
Definition: hosts.h:88
hosts.h
Protos and data structures for Hosts collections and single hosts objects.