Greenbone Vulnerability Management Libraries  11.0.1
hosts.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2019 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
30 #include "hosts.h"
31 
32 #include "networking.h" /* for ipv4_as_ipv6, addr6_as_str, gvm_resolve */
33 
34 #include <arpa/inet.h> /* for inet_pton, inet_ntop */
35 #include <assert.h> /* for assert */
36 #include <ctype.h> /* for isdigit */
37 #include <malloc.h>
38 #include <netdb.h> /* for getnameinfo, NI_NAMEREQD */
39 #include <stdint.h> /* for uint8_t, uint32_t */
40 #include <stdio.h> /* for sscanf, perror */
41 #include <stdlib.h> /* for strtol, atoi */
42 #include <string.h> /* for strchr, memcpy, memcmp, bzero, strcasecmp */
43 #include <sys/socket.h> /* for AF_INET, AF_INET6, sockaddr */
44 
45 #undef G_LOG_DOMAIN
46 
49 #define G_LOG_DOMAIN "base hosts"
50 
51 /* Static variables */
52 
54  [HOST_TYPE_NAME] = "Hostname",
55  [HOST_TYPE_IPV4] = "IPv4",
56  [HOST_TYPE_IPV6] = "IPv6",
57  [HOST_TYPE_CIDR_BLOCK] = "IPv4 CIDR block",
58  [HOST_TYPE_RANGE_SHORT] = "IPv4 short range",
59  [HOST_TYPE_RANGE_LONG] = "IPv4 long range"};
60 
61 /* Function definitions */
62 
71 static int
72 is_ipv4_address (const char *str)
73 {
74  struct sockaddr_in sa;
75 
76  return inet_pton (AF_INET, str, &(sa.sin_addr)) == 1;
77 }
78 
87 static int
88 is_ipv6_address (const char *str)
89 {
90  struct sockaddr_in6 sa6;
91 
92  return inet_pton (AF_INET6, str, &(sa6.sin6_addr)) == 1;
93 }
94 
103 static int
104 is_cidr_block (const char *str)
105 {
106  long block;
107  char *addr_str, *block_str, *p;
108 
109  addr_str = g_strdup (str);
110  block_str = strchr (addr_str, '/');
111  if (block_str == NULL)
112  {
113  g_free (addr_str);
114  return 0;
115  }
116 
117  /* Separate the address from the block value. */
118  *block_str = '\0';
119  block_str++;
120 
121  if (!is_ipv4_address (addr_str) || !isdigit (*block_str))
122  {
123  g_free (addr_str);
124  return 0;
125  }
126 
127  p = NULL;
128  block = strtol (block_str, &p, 10);
129 
130  if (*p || block <= 0 || block > 30)
131  {
132  g_free (addr_str);
133  return 0;
134  }
135 
136  g_free (addr_str);
137  return 1;
138 }
139 
149 static int
150 cidr_get_block (const char *str, unsigned int *block)
151 {
152  if (str == NULL || block == NULL)
153  return -1;
154 
155  if (sscanf (str, "%*[0-9.]/%2u", block) != 1)
156  return -1;
157 
158  return 0;
159 }
160 
170 static int
171 cidr_get_ip (const char *str, struct in_addr *addr)
172 {
173  gchar *addr_str, *tmp;
174 
175  if (str == NULL || addr == NULL)
176  return -1;
177 
178  addr_str = g_strdup (str);
179  tmp = strchr (addr_str, '/');
180  if (tmp == NULL)
181  {
182  g_free (addr_str);
183  return -1;
184  }
185  *tmp = '\0';
186 
187  if (inet_pton (AF_INET, addr_str, addr) != 1)
188  return -1;
189 
190  g_free (addr_str);
191  return 0;
192 }
193 
210 static int
211 cidr_block_ips (const char *str, struct in_addr *first, struct in_addr *last)
212 {
213  unsigned int block;
214 
215  if (str == NULL || first == NULL || last == NULL)
216  return -1;
217 
218  /* Get IP and block values. */
219  if (cidr_get_block (str, &block) == -1)
220  return -1;
221  if (cidr_get_ip (str, first) == -1)
222  return -1;
223 
224  /* First IP: And with mask and increment. */
225  first->s_addr &= htonl (0xffffffff ^ ((1 << (32 - block)) - 1));
226  first->s_addr = htonl (ntohl (first->s_addr) + 1);
227 
228  /* Last IP: First IP + Number of usable hosts - 1. */
229  last->s_addr = htonl (ntohl (first->s_addr) + (1 << (32 - block)) - 3);
230  return 0;
231 }
232 
241 static int
242 is_long_range_network (const char *str)
243 {
244  char *first_str, *second_str;
245  int ret;
246 
247  first_str = g_strdup (str);
248  second_str = strchr (first_str, '-');
249  if (second_str == NULL)
250  {
251  g_free (first_str);
252  return 0;
253  }
254 
255  /* Separate the addresses. */
256  *second_str = '\0';
257  second_str++;
258 
259  ret = is_ipv4_address (first_str) && is_ipv4_address (second_str);
260  g_free (first_str);
261 
262  return ret;
263 }
264 
276 static int
277 long_range_network_ips (const char *str, struct in_addr *first,
278  struct in_addr *last)
279 {
280  char *first_str, *last_str;
281 
282  if (str == NULL || first == NULL || last == NULL)
283  return -1;
284 
285  first_str = g_strdup (str);
286  last_str = strchr (first_str, '-');
287  if (last_str == NULL)
288  {
289  g_free (first_str);
290  return -1;
291  }
292 
293  /* Separate the two IPs. */
294  *last_str = '\0';
295  last_str++;
296 
297  if (inet_pton (AF_INET, first_str, first) != 1
298  || inet_pton (AF_INET, last_str, last) != 1)
299  {
300  g_free (first_str);
301  return -1;
302  }
303 
304  g_free (first_str);
305  return 0;
306 }
307 
316 static int
317 is_short_range_network (const char *str)
318 {
319  long end;
320  char *ip_str, *end_str, *p;
321 
322  ip_str = g_strdup (str);
323  end_str = strchr (ip_str, '-');
324  if (end_str == NULL)
325  {
326  g_free (ip_str);
327  return 0;
328  }
329 
330  /* Separate the addresses. */
331  *end_str = '\0';
332  end_str++;
333 
334  if (!is_ipv4_address (ip_str) || !isdigit (*end_str))
335  {
336  g_free (ip_str);
337  return 0;
338  }
339 
340  p = NULL;
341  end = strtol (end_str, &p, 10);
342  g_free (ip_str);
343 
344  if (*p || end < 0 || end > 255)
345  return 0;
346 
347  return 1;
348 }
349 
361 static int
362 short_range_network_ips (const char *str, struct in_addr *first,
363  struct in_addr *last)
364 {
365  char *first_str, *last_str;
366  int end;
367 
368  if (str == NULL || first == NULL || last == NULL)
369  return -1;
370 
371  first_str = g_strdup (str);
372  last_str = strchr (first_str, '-');
373  if (last_str == NULL)
374  {
375  g_free (first_str);
376  return -1;
377  }
378 
379  /* Separate the two IPs. */
380  *last_str = '\0';
381  last_str++;
382  end = atoi (last_str);
383 
384  /* Get the first IP */
385  if (inet_pton (AF_INET, first_str, first) != 1)
386  {
387  g_free (first_str);
388  return -1;
389  }
390 
391  /* Get the last IP */
392  last->s_addr = htonl ((ntohl (first->s_addr) & 0xffffff00) + end);
393 
394  g_free (first_str);
395  return 0;
396 }
397 
407 static int
408 is_hostname (const char *str)
409 {
410  const char *h = str;
411 
412  while (*h && (isalnum (*h) || strchr ("-_.", *h)))
413  h++;
414 
415  /* Valid string if no other chars, and length is 255 at most. */
416  if (*h == '\0' && h - str < 256)
417  return 1;
418 
419  return 0;
420 }
421 
430 static int
431 is_cidr6_block (const char *str)
432 {
433  long block;
434  char *addr6_str, *block_str, *p;
435 
436  addr6_str = g_strdup (str);
437  block_str = strchr (addr6_str, '/');
438  if (block_str == NULL)
439  {
440  g_free (addr6_str);
441  return 0;
442  }
443 
444  /* Separate the address from the block value. */
445  *block_str = '\0';
446  block_str++;
447 
448  if (!is_ipv6_address (addr6_str) || !isdigit (*block_str))
449  {
450  g_free (addr6_str);
451  return 0;
452  }
453 
454  p = NULL;
455  block = strtol (block_str, &p, 10);
456  g_free (addr6_str);
457 
458  if (*p || block <= 0 || block > 128)
459  return 0;
460 
461  return 1;
462 }
463 
473 static int
474 cidr6_get_block (const char *str, unsigned int *block)
475 {
476  if (str == NULL || block == NULL)
477  return -1;
478 
479  if (sscanf (str, "%*[0-9a-fA-F.:]/%3u", block) != 1)
480  return -1;
481 
482  return 0;
483 }
484 
494 static int
495 cidr6_get_ip (const char *str, struct in6_addr *addr6)
496 {
497  gchar *addr6_str, *tmp;
498 
499  if (str == NULL || addr6 == NULL)
500  return -1;
501 
502  addr6_str = g_strdup (str);
503  tmp = strchr (addr6_str, '/');
504  if (tmp == NULL)
505  {
506  g_free (addr6_str);
507  return -1;
508  }
509  *tmp = '\0';
510 
511  if (inet_pton (AF_INET6, addr6_str, addr6) != 1)
512  return -1;
513 
514  g_free (addr6_str);
515  return 0;
516 }
517 
529 static int
530 cidr6_block_ips (const char *str, struct in6_addr *first, struct in6_addr *last)
531 {
532  unsigned int block;
533  int i, j;
534 
535  if (str == NULL || first == NULL || last == NULL)
536  return -1;
537 
538  /* Get IP and block values. */
539  if (cidr6_get_block (str, &block) == -1)
540  return -1;
541  if (cidr6_get_ip (str, first) == -1)
542  return -1;
543  memcpy (&last->s6_addr, &first->s6_addr, 16);
544 
545  /* /128 => Specified address is the first and last one. */
546  if (block == 128)
547  return 0;
548 
549  /* First IP: And with mask and increment to skip network address. */
550  j = 15;
551  for (i = (128 - block) / 8; i > 0; i--)
552  {
553  first->s6_addr[j] = 0;
554  j--;
555  }
556  first->s6_addr[j] &= 0xff ^ ((1 << ((128 - block) % 8)) - 1);
557 
558  /* Last IP: Broadcast address - 1. */
559  j = 15;
560  for (i = (128 - block) / 8; i > 0; i--)
561  {
562  last->s6_addr[j] = 0xff;
563  j--;
564  }
565  last->s6_addr[j] |= (1 << ((128 - block) % 8)) - 1;
566 
567  /* /127 => Only two addresses. Don't skip network / broadcast addresses.*/
568  if (block == 127)
569  return 0;
570 
571  /* Increment first IP. */
572  for (i = 15; i >= 0; --i)
573  if (first->s6_addr[i] < 255)
574  {
575  first->s6_addr[i]++;
576  break;
577  }
578  else
579  first->s6_addr[i] = 0;
580  /* Decrement last IP. */
581  for (i = 15; i >= 0; --i)
582  if (last->s6_addr[i] > 0)
583  {
584  last->s6_addr[i]--;
585  break;
586  }
587  else
588  last->s6_addr[i] = 0xff;
589 
590  return 0;
591 }
592 
601 static int
602 is_long_range6_network (const char *str)
603 {
604  char *first_str, *second_str;
605  int ret;
606 
607  first_str = g_strdup (str);
608  second_str = strchr (first_str, '-');
609  if (second_str == NULL)
610  {
611  g_free (first_str);
612  return 0;
613  }
614 
615  /* Separate the addresses. */
616  *second_str = '\0';
617  second_str++;
618 
619  ret = is_ipv6_address (first_str) && is_ipv6_address (second_str);
620  g_free (first_str);
621 
622  return ret;
623 }
624 
636 static int
637 long_range6_network_ips (const char *str, struct in6_addr *first,
638  struct in6_addr *last)
639 {
640  char *first_str, *last_str;
641 
642  if (str == NULL || first == NULL || last == NULL)
643  return -1;
644 
645  first_str = g_strdup (str);
646  last_str = strchr (first_str, '-');
647  if (last_str == NULL)
648  {
649  g_free (first_str);
650  return -1;
651  }
652 
653  /* Separate the two IPs. */
654  *last_str = '\0';
655  last_str++;
656 
657  if (inet_pton (AF_INET6, first_str, first) != 1
658  || inet_pton (AF_INET6, last_str, last) != 1)
659  {
660  g_free (first_str);
661  return -1;
662  }
663 
664  g_free (first_str);
665  return 0;
666 }
667 
676 static int
677 is_short_range6_network (const char *str)
678 {
679  char *ip_str, *end_str, *p;
680 
681  ip_str = g_strdup (str);
682  end_str = strchr (ip_str, '-');
683  if (end_str == NULL)
684  {
685  g_free (ip_str);
686  return 0;
687  }
688 
689  /* Separate the addresses. */
690  *end_str = '\0';
691  end_str++;
692 
693  if (!is_ipv6_address (ip_str) || *end_str == '\0')
694  {
695  g_free (ip_str);
696  return 0;
697  }
698 
699  p = end_str;
700  /* Check that the 2nd part is at most 4 hexadecimal characters. */
701  while (isxdigit (*p) && p++)
702  ;
703  if (*p || p - end_str > 4)
704  {
705  g_free (ip_str);
706  return 0;
707  }
708 
709  g_free (ip_str);
710  return 1;
711 }
712 
724 static int
725 short_range6_network_ips (const char *str, struct in6_addr *first,
726  struct in6_addr *last)
727 {
728  char *first_str, *last_str;
729  long int end;
730 
731  if (str == NULL || first == NULL || last == NULL)
732  return -1;
733 
734  first_str = g_strdup (str);
735  last_str = strchr (first_str, '-');
736  if (last_str == NULL)
737  {
738  g_free (first_str);
739  return -1;
740  }
741 
742  /* Separate the first IP. */
743  *last_str = '\0';
744  last_str++;
745 
746  if (inet_pton (AF_INET6, first_str, first) != 1)
747  {
748  g_free (first_str);
749  return -1;
750  }
751 
752  /* Calculate the last IP. */
753  memcpy (last, first, sizeof (*last));
754  end = strtol (last_str, NULL, 16);
755  memcpy (&last->s6_addr[15], &end, 1);
756  memcpy (&last->s6_addr[14], ((char *) &end) + 1, 1);
757 
758  g_free (first_str);
759  return 0;
760 }
761 
770 int
771 gvm_get_host_type (const gchar *str_stripped)
772 {
773  /*
774  * We have a single element with no leading or trailing
775  * white spaces. This element could represent different host
776  * definitions: single IPs, host names, CIDR-expressed blocks,
777  * range-expressed networks, IPv6 addresses.
778  */
779 
780  /* Null or empty string. */
781  if (str_stripped == NULL || *str_stripped == '\0')
782  return -1;
783 
784  /* Check for regular single IPv4 address. */
785  if (is_ipv4_address (str_stripped))
786  return HOST_TYPE_IPV4;
787 
788  /* Check for regular single IPv6 address. */
789  if (is_ipv6_address (str_stripped))
790  return HOST_TYPE_IPV6;
791 
792  /* Check for regular IPv4 CIDR-expressed block like "192.168.12.0/24" */
793  if (is_cidr_block (str_stripped))
794  return HOST_TYPE_CIDR_BLOCK;
795 
796  /* Check for short range-expressed networks "192.168.12.5-40" */
797  if (is_short_range_network (str_stripped))
798  return HOST_TYPE_RANGE_SHORT;
799 
800  /* Check for long range-expressed networks "192.168.1.0-192.168.3.44" */
801  if (is_long_range_network (str_stripped))
802  return HOST_TYPE_RANGE_LONG;
803 
804  /* Check for regular IPv6 CIDR-expressed block like "2620:0:2d0:200::7/120" */
805  if (is_cidr6_block (str_stripped))
806  return HOST_TYPE_CIDR6_BLOCK;
807 
808  /* Check for short range-expressed networks "::1-ef12" */
809  if (is_short_range6_network (str_stripped))
810  return HOST_TYPE_RANGE6_SHORT;
811 
812  /* Check for long IPv6 range-expressed networks like "::1:20:7-::1:25:3" */
813  if (is_long_range6_network (str_stripped))
814  return HOST_TYPE_RANGE6_LONG;
815 
816  /* Check for hostname. */
817  if (is_hostname (str_stripped))
818  return HOST_TYPE_NAME;
819 
820  return -1;
821 }
822 
831 gvm_vhost_t *
832 gvm_vhost_new (char *value, char *source)
833 {
834  gvm_vhost_t *vhost;
835 
836  vhost = g_malloc0 (sizeof (gvm_vhost_t));
837  vhost->value = value;
838  vhost->source = source;
839 
840  return vhost;
841 }
842 
848 static void
849 gvm_vhost_free (gpointer vhost)
850 {
851  if (vhost)
852  {
853  g_free (((gvm_vhost_t *) vhost)->value);
854  g_free (((gvm_vhost_t *) vhost)->source);
855  }
856  g_free (vhost);
857 }
858 
864 static gvm_host_t *
866 {
867  gvm_host_t *host;
868 
869  host = g_malloc0 (sizeof (gvm_host_t));
870 
871  return host;
872 }
873 
879 static void
880 gvm_host_free (gpointer host)
881 {
882  gvm_host_t *h = host;
883  if (h == NULL)
884  return;
885 
886  /* If host of type hostname, free the name buffer, first. */
887  if (h->type == HOST_TYPE_NAME)
888  g_free (h->name);
889 
890  g_slist_free_full (h->vhosts, gvm_vhost_free);
891  g_free (h);
892 }
893 
900 static void
902 {
903  if (hosts->count == hosts->max_size)
904  {
905  hosts->max_size *= 4;
906  hosts->hosts =
907  g_realloc_n (hosts->hosts, hosts->max_size, sizeof (*hosts->hosts));
908  }
909  hosts->hosts[hosts->count] = host;
910  hosts->count++;
911 }
912 
920 static gvm_hosts_t *
921 gvm_hosts_init (const char *hosts_str)
922 {
923  gvm_hosts_t *hosts;
924 
925  hosts = g_malloc0 (sizeof (gvm_hosts_t));
926  hosts->max_size = 1024;
927  hosts->hosts = g_malloc0_n (hosts->max_size, sizeof (gvm_host_t *));
928  hosts->orig_str = g_strdup (hosts_str);
929  return hosts;
930 }
931 
938 static void
940 {
941  size_t i;
942  if (!hosts)
943  return;
944 
945  for (i = 0; i < hosts->max_size; i++)
946  {
947  if (!hosts->hosts[i])
948  {
949  size_t j;
950 
951  /* Fill the gap with the closest host entry, in order to keep the
952  * sequential ordering. */
953  for (j = i + 1; j < hosts->max_size; j++)
954  {
955  if (hosts->hosts[j])
956  {
957  hosts->hosts[i] = hosts->hosts[j];
958  hosts->hosts[j] = NULL;
959  break;
960  }
961  }
962  /* No more entries left, ie. the empty space between count and
963  * max_size. */
964  if (!hosts->hosts[i])
965  return;
966  }
967  }
968 }
969 
976 static void
978 {
982  GHashTable *name_table;
983  size_t i, duplicates = 0;
984 
985  if (hosts == NULL)
986  return;
987  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
988 
989  for (i = 0; i < hosts->count; i++)
990  {
991  gchar *name;
992 
993  if ((name = gvm_host_value_str (hosts->hosts[i])))
994  {
995  gvm_host_t *host, *removed = hosts->hosts[i];
996 
997  host = g_hash_table_lookup (name_table, name);
998  if (host)
999  {
1000  /* Remove duplicate host. Add its vhosts to the original host. */
1001  host->vhosts = g_slist_concat (host->vhosts, removed->vhosts);
1002  removed->vhosts = NULL;
1003  gvm_host_free (removed);
1004  hosts->hosts[i] = NULL;
1005  duplicates++;
1006  g_free (name);
1007  }
1008  else
1009  g_hash_table_insert (name_table, name, hosts->hosts[i]);
1010  }
1011  }
1012 
1013  if (duplicates)
1014  gvm_hosts_fill_gaps (hosts);
1015  g_hash_table_destroy (name_table);
1016  hosts->count -= duplicates;
1017  hosts->removed += duplicates;
1018  hosts->current = 0;
1019  malloc_trim (0);
1020 }
1021 
1033 gvm_hosts_t *
1034 gvm_hosts_new_with_max (const gchar *hosts_str, unsigned int max_hosts)
1035 {
1036  gvm_hosts_t *hosts;
1037  gchar **host_element, **split;
1038  gchar *str;
1039 
1040  if (hosts_str == NULL)
1041  return NULL;
1042 
1043  /* Normalize separator: Transform newlines into commas. */
1044  hosts = gvm_hosts_init (hosts_str);
1045  str = hosts->orig_str;
1046  while (*str)
1047  {
1048  if (*str == '\n')
1049  *str = ',';
1050  str++;
1051  }
1052 
1053  /* Split comma-separated list into single host-specifications */
1054  split = g_strsplit (hosts->orig_str, ",", 0);
1055 
1056  /* first element of the split list */
1057  host_element = split;
1058  while (*host_element)
1059  {
1060  int host_type;
1061  gchar *stripped = g_strstrip (*host_element);
1062 
1063  if (stripped == NULL || *stripped == '\0')
1064  {
1065  host_element++;
1066  continue;
1067  }
1068 
1069  /* IPv4, hostname, IPv6, collection (short/long range, cidr block) etc,. ?
1070  */
1071  /* -1 if error. */
1072  host_type = gvm_get_host_type (stripped);
1073 
1074  switch (host_type)
1075  {
1076  case HOST_TYPE_NAME:
1077  case HOST_TYPE_IPV4:
1078  case HOST_TYPE_IPV6:
1079  {
1080  /* New host. */
1081  gvm_host_t *host = gvm_host_new ();
1082  host->type = host_type;
1083  if (host_type == HOST_TYPE_NAME)
1084  host->name = g_ascii_strdown (stripped, -1);
1085  else if (host_type == HOST_TYPE_IPV4)
1086  {
1087  if (inet_pton (AF_INET, stripped, &host->addr) != 1)
1088  break;
1089  }
1090  else if (host_type == HOST_TYPE_IPV6)
1091  {
1092  if (inet_pton (AF_INET6, stripped, &host->addr6) != 1)
1093  break;
1094  }
1095  gvm_hosts_add (hosts, host);
1096  break;
1097  }
1098  case HOST_TYPE_CIDR_BLOCK:
1099  case HOST_TYPE_RANGE_SHORT:
1100  case HOST_TYPE_RANGE_LONG:
1101  {
1102  struct in_addr first, last;
1103  uint32_t current;
1104  int (*ips_func) (const char *, struct in_addr *, struct in_addr *);
1105 
1107  ips_func = cidr_block_ips;
1108  else if (host_type == HOST_TYPE_RANGE_SHORT)
1109  ips_func = short_range_network_ips;
1110  else
1111  ips_func = long_range_network_ips;
1112 
1113  if (ips_func (stripped, &first, &last) == -1)
1114  break;
1115 
1116  /* Make sure that first actually comes before last */
1117  if (ntohl (first.s_addr) > ntohl (last.s_addr))
1118  break;
1119 
1120  /* Add addresses from first to last as single hosts. */
1121  current = first.s_addr;
1122  while (ntohl (current) <= ntohl (last.s_addr))
1123  {
1124  gvm_host_t *host;
1125  if (max_hosts > 0 && hosts->count > max_hosts)
1126  {
1127  g_strfreev (split);
1128  gvm_hosts_free (hosts);
1129  return NULL;
1130  }
1131  host = gvm_host_new ();
1132  host->type = HOST_TYPE_IPV4;
1133  host->addr.s_addr = current;
1134  gvm_hosts_add (hosts, host);
1135  /* Next IP address. */
1136  current = htonl (ntohl (current) + 1);
1137  }
1138  break;
1139  }
1140  case HOST_TYPE_CIDR6_BLOCK:
1141  case HOST_TYPE_RANGE6_LONG:
1143  {
1144  struct in6_addr first, last;
1145  unsigned char current[16];
1146  int (*ips_func) (const char *, struct in6_addr *,
1147  struct in6_addr *);
1148 
1150  ips_func = cidr6_block_ips;
1151  else if (host_type == HOST_TYPE_RANGE6_SHORT)
1152  ips_func = short_range6_network_ips;
1153  else
1154  ips_func = long_range6_network_ips;
1155 
1156  if (ips_func (stripped, &first, &last) == -1)
1157  break;
1158 
1159  /* Make sure the first comes before the last. */
1160  if (memcmp (&first.s6_addr, &last.s6_addr, 16) > 0)
1161  break;
1162 
1163  /* Add addresses from first to last as single hosts. */
1164  memcpy (current, &first.s6_addr, 16);
1165  while (memcmp (current, &last.s6_addr, 16) <= 0)
1166  {
1167  int i;
1168  gvm_host_t *host;
1169 
1170  if (max_hosts > 0 && hosts->count > max_hosts)
1171  {
1172  g_strfreev (split);
1173  gvm_hosts_free (hosts);
1174  return NULL;
1175  }
1176  host = gvm_host_new ();
1177  host->type = HOST_TYPE_IPV6;
1178  memcpy (host->addr6.s6_addr, current, 16);
1179  gvm_hosts_add (hosts, host);
1180  /* Next IPv6 address. */
1181  for (i = 15; i >= 0; --i)
1182  if (current[i] < 255)
1183  {
1184  current[i]++;
1185  break;
1186  }
1187  else
1188  current[i] = 0;
1189  }
1190  break;
1191  }
1192  case -1:
1193  default:
1194  /* Invalid host string. */
1195  g_strfreev (split);
1196  gvm_hosts_free (hosts);
1197  return NULL;
1198  }
1199  host_element++; /* move on to next element of split list */
1200  if (max_hosts > 0 && hosts->count > max_hosts)
1201  {
1202  g_strfreev (split);
1203  gvm_hosts_free (hosts);
1204  return NULL;
1205  }
1206  }
1207 
1208  /* No need to check for duplicates when a hosts string contains a
1209  * single (IP/Hostname/Range/Subnetwork) entry. */
1210  if (g_strv_length (split) > 1)
1211  gvm_hosts_deduplicate (hosts);
1212 
1213  g_strfreev (split);
1214  malloc_trim (0);
1215  return hosts;
1216 }
1217 
1228 gvm_hosts_t *
1229 gvm_hosts_new (const gchar *hosts_str)
1230 {
1231  return gvm_hosts_new_with_max (hosts_str, 0);
1232 }
1233 
1242 gvm_host_t *
1244 {
1245  if (!hosts || hosts->current == hosts->count)
1246  return NULL;
1247 
1248  return hosts->hosts[hosts->current++];
1249 }
1250 
1257 void
1259 {
1260  size_t i;
1261 
1262  if (hosts == NULL)
1263  return;
1264 
1265  if (hosts->orig_str)
1266  g_free (hosts->orig_str);
1267  for (i = 0; i < hosts->count; i++)
1268  gvm_host_free (hosts->hosts[i]);
1269  g_free (hosts->hosts);
1270  g_free (hosts);
1271 }
1272 
1280 void
1282 {
1283  size_t i = 0;
1284  GRand *rand;
1285 
1286  if (hosts == NULL)
1287  return;
1288 
1289  /* Shuffle the array. */
1290  rand = g_rand_new ();
1291  for (i = 0; i < hosts->count; i++)
1292  {
1293  void *tmp;
1294  int j = g_rand_int_range (rand, 0, hosts->count);
1295 
1296  tmp = hosts->hosts[i];
1297  hosts->hosts[i] = hosts->hosts[j];
1298  hosts->hosts[j] = tmp;
1299  }
1300 
1301  hosts->current = 0;
1302  g_rand_free (rand);
1303 }
1304 
1312 void
1314 {
1315  size_t i, j;
1316  if (hosts == NULL)
1317  return;
1318 
1319  for (i = 0, j = hosts->count - 1; i < j; i++, j--)
1320  {
1321  gvm_host_t *tmp = hosts->hosts[i];
1322  hosts->hosts[i] = hosts->hosts[j];
1323  hosts->hosts[j] = tmp;
1324  }
1325  hosts->current = 0;
1326 }
1327 
1338 GSList *
1340 {
1341  size_t i, new_entries = 0, resolved = 0;
1342  GSList *unresolved = NULL;
1343 
1344  for (i = 0; i < hosts->count; i++)
1345  {
1346  GSList *list, *tmp;
1347  gvm_host_t *host = hosts->hosts[i];
1348 
1349  if (host->type != HOST_TYPE_NAME)
1350  continue;
1351 
1352  list = tmp = gvm_resolve_list (host->name);
1353  while (tmp)
1354  {
1355  /* Create a new host for each IP address. */
1356  gvm_host_t *new;
1357  struct in6_addr *ip6 = tmp->data;
1358  gvm_vhost_t *vhost;
1359 
1360  new = gvm_host_new ();
1361  if (ip6->s6_addr32[0] != 0 || ip6->s6_addr32[1] != 0
1362  || ip6->s6_addr32[2] != htonl (0xffff))
1363  {
1364  new->type = HOST_TYPE_IPV6;
1365  memcpy (&new->addr6, ip6, sizeof (new->addr6));
1366  }
1367  else
1368  {
1369  new->type = HOST_TYPE_IPV4;
1370  memcpy (&new->addr6, &ip6->s6_addr32[3], sizeof (new->addr));
1371  }
1372  vhost =
1373  gvm_vhost_new (g_strdup (host->name), g_strdup ("Forward-DNS"));
1374  new->vhosts = g_slist_prepend (new->vhosts, vhost);
1375  gvm_hosts_add (hosts, new);
1376  tmp = tmp->next;
1377  new_entries = 1;
1378  }
1379  /* Remove hostname from list, as it was either replaced by IPs, or
1380  * is unresolvable. */
1381  hosts->hosts[i] = NULL;
1382  resolved++;
1383  if (!list)
1384  unresolved = g_slist_prepend (unresolved, g_strdup (host->name));
1385  gvm_host_free (host);
1386  g_slist_free_full (list, g_free);
1387  }
1388  if (resolved)
1389  gvm_hosts_fill_gaps (hosts);
1390  hosts->count -= resolved;
1391  hosts->removed += resolved;
1392  if (new_entries)
1393  gvm_hosts_deduplicate (hosts);
1394  hosts->current = 0;
1395  return unresolved;
1396 }
1397 
1406 int
1407 gvm_vhosts_exclude (gvm_host_t *host, const char *excluded_str)
1408 {
1409  GSList *vhost;
1410  char **excluded;
1411  int ret = 0;
1412 
1413  if (!host || !excluded_str)
1414  return ret;
1415 
1416  vhost = host->vhosts;
1417  excluded = g_strsplit (excluded_str, ",", 0);
1418  if (!excluded || !*excluded)
1419  {
1420  g_strfreev (excluded);
1421  return ret;
1422  }
1423  while (vhost)
1424  {
1425  char **tmp = excluded;
1426  char *value = ((gvm_vhost_t *) vhost->data)->value;
1427 
1428  while (*tmp)
1429  {
1430  if (!strcasecmp (value, g_strstrip (*tmp)))
1431  {
1432  gvm_vhost_free (vhost->data);
1433  host->vhosts = vhost = g_slist_delete_link (host->vhosts, vhost);
1434  ret++;
1435  break;
1436  }
1437  tmp++;
1438  if (!*tmp)
1439  {
1440  vhost = vhost->next;
1441  break;
1442  }
1443  }
1444  }
1445  g_strfreev (excluded);
1446 
1447  return ret;
1448 }
1449 
1461 int
1462 gvm_hosts_exclude_with_max (gvm_hosts_t *hosts, const char *excluded_str,
1463  unsigned int max_hosts)
1464 {
1468  gvm_hosts_t *excluded_hosts;
1469  GHashTable *name_table;
1470  size_t excluded = 0, i;
1471 
1472  if (hosts == NULL || excluded_str == NULL)
1473  return -1;
1474 
1475  excluded_hosts = gvm_hosts_new_with_max (excluded_str, max_hosts);
1476  if (excluded_hosts == NULL)
1477  return -1;
1478 
1479  if (gvm_hosts_count (excluded_hosts) == 0)
1480  {
1481  gvm_hosts_free (excluded_hosts);
1482  return 0;
1483  }
1484 
1485  /* Hash host values from excluded hosts list. */
1486  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1487  for (i = 0; i < excluded_hosts->count; i++)
1488  {
1489  gchar *name;
1490 
1491  if ((name = gvm_host_value_str (excluded_hosts->hosts[i])))
1492  g_hash_table_insert (name_table, name, hosts);
1493  }
1494 
1495  /* Check for hosts values in hash table. */
1496  for (i = 0; i < hosts->count; i++)
1497  {
1498  gchar *name;
1499 
1500  if ((name = gvm_host_value_str (hosts->hosts[i])))
1501  {
1502  if (g_hash_table_lookup (name_table, name))
1503  {
1504  gvm_host_free (hosts->hosts[i]);
1505  hosts->hosts[i] = NULL;
1506  excluded++;
1507  g_free (name);
1508  continue;
1509  }
1510  g_free (name);
1511  }
1512  }
1513 
1514  /* Cleanup. */
1515  if (excluded)
1516  gvm_hosts_fill_gaps (hosts);
1517  hosts->count -= excluded;
1518  hosts->removed += excluded;
1519  hosts->current = 0;
1520  g_hash_table_destroy (name_table);
1521  gvm_hosts_free (excluded_hosts);
1522  return excluded;
1523 }
1524 
1535 int
1536 gvm_hosts_exclude (gvm_hosts_t *hosts, const char *excluded_str)
1537 {
1538  return gvm_hosts_exclude_with_max (hosts, excluded_str, 0);
1539 }
1540 
1548 char *
1550 {
1551  int retry = 2;
1552  gchar hostname[NI_MAXHOST];
1553  void *addr;
1554  size_t addrlen;
1555  struct sockaddr_in sa;
1556  struct sockaddr_in6 sa6;
1557 
1558  if (!host)
1559  return NULL;
1560 
1561  if (host->type == HOST_TYPE_IPV4)
1562  {
1563  addr = &sa;
1564  addrlen = sizeof (sa);
1565  memset (addr, '\0', addrlen);
1566  sa.sin_addr = host->addr;
1567  sa.sin_family = AF_INET;
1568  }
1569  else if (host->type == HOST_TYPE_IPV6)
1570  {
1571  addr = &sa6;
1572  addrlen = sizeof (sa6);
1573  memset (&sa6, '\0', addrlen);
1574  memcpy (&sa6.sin6_addr, &host->addr6, 16);
1575  sa6.sin6_family = AF_INET6;
1576  }
1577  else
1578  return NULL;
1579 
1580  while (retry--)
1581  {
1582  int ret = getnameinfo (addr, addrlen, hostname, sizeof (hostname), NULL,
1583  0, NI_NAMEREQD);
1584  if (!ret)
1585  return g_ascii_strdown (hostname, -1);
1586  if (ret != EAI_AGAIN)
1587  break;
1588  }
1589  return NULL;
1590 }
1591 
1600 static int
1601 host_name_verify (gvm_host_t *host, const char *value)
1602 {
1603  GSList *list, *tmp;
1604  char *host_str;
1605  int ret = -1;
1606 
1607  assert (host);
1608  assert (value);
1609  host_str = gvm_host_value_str (host);
1610  list = tmp = gvm_resolve_list (value);
1611  while (tmp)
1612  {
1613  char buffer[INET6_ADDRSTRLEN];
1614  addr6_to_str (tmp->data, buffer);
1615  if (!strcasecmp (host_str, buffer))
1616  {
1617  ret = 0;
1618  break;
1619  }
1620  tmp = tmp->next;
1621  }
1622  g_free (host_str);
1623  g_slist_free_full (list, g_free);
1624  return ret;
1625 }
1626 
1632 void
1634 {
1635  GSList *vhosts;
1636  gvm_vhost_t *vhost;
1637  char *value;
1638 
1639  if (!host || host->type == HOST_TYPE_NAME)
1640  return;
1641 
1642  value = gvm_host_reverse_lookup (host);
1643  if (!value)
1644  return;
1645  if (host_name_verify (host, value))
1646  {
1647  g_free (value);
1648  return;
1649  }
1650  /* Don't add vhost, if already in the list. */
1651  vhosts = host->vhosts;
1652  while (vhosts)
1653  {
1654  if (!strcasecmp (((gvm_vhost_t *) vhosts->data)->value, value))
1655  {
1656  g_free (value);
1657  return;
1658  }
1659  vhosts = vhosts->next;
1660  }
1661  vhost = gvm_vhost_new (value, g_strdup ("Reverse-DNS"));
1662  host->vhosts = g_slist_prepend (host->vhosts, vhost);
1663 }
1664 
1674 int
1676 {
1677  size_t i, count = 0;
1678 
1679  if (hosts == NULL)
1680  return -1;
1681 
1682  for (i = 0; i < hosts->count; i++)
1683  {
1684  gchar *name = gvm_host_reverse_lookup (hosts->hosts[i]);
1685 
1686  if (name == NULL)
1687  {
1688  gvm_host_free (hosts->hosts[i]);
1689  hosts->hosts[i] = NULL;
1690  count++;
1691  }
1692  else
1693  g_free (name);
1694  }
1695 
1696  if (count)
1697  gvm_hosts_fill_gaps (hosts);
1698  hosts->count -= count;
1699  hosts->removed += count;
1700  hosts->current = 0;
1701  return count;
1702 }
1703 
1713 int
1715 {
1719  size_t i, count = 0;
1720  GHashTable *name_table;
1721 
1722  if (hosts == NULL)
1723  return -1;
1724 
1725  name_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
1726  for (i = 0; i < hosts->count; i++)
1727  {
1728  gchar *name;
1729 
1730  if ((name = gvm_host_reverse_lookup (hosts->hosts[i])))
1731  {
1732  if (g_hash_table_lookup (name_table, name))
1733  {
1734  gvm_host_free (hosts->hosts[i]);
1735  hosts->hosts[i] = NULL;
1736  count++;
1737  g_free (name);
1738  }
1739  else
1740  {
1741  /* Insert in the hash table. Value not important. */
1742  g_hash_table_insert (name_table, name, hosts);
1743  }
1744  }
1745  }
1746 
1747  if (count)
1748  gvm_hosts_fill_gaps (hosts);
1749  g_hash_table_destroy (name_table);
1750  hosts->removed += count;
1751  hosts->count -= count;
1752  hosts->current = 0;
1753  return count;
1754 }
1755 
1763 unsigned int
1765 {
1766  return hosts ? hosts->count : 0;
1767 }
1768 
1777 unsigned int
1779 {
1780  return hosts ? hosts->removed : 0;
1781 }
1782 
1795 int
1796 gvm_host_in_hosts (const gvm_host_t *host, const struct in6_addr *addr,
1797  const gvm_hosts_t *hosts)
1798 {
1799  char *host_str;
1800  size_t i;
1801 
1802  if (host == NULL || hosts == NULL)
1803  return 0;
1804 
1805  host_str = gvm_host_value_str (host);
1806 
1807  for (i = 0; i < hosts->count; i++)
1808  {
1809  gvm_host_t *current_host = hosts->hosts[i];
1810  char *tmp = gvm_host_value_str (current_host);
1811 
1812  if (strcasecmp (host_str, tmp) == 0)
1813  {
1814  g_free (host_str);
1815  g_free (tmp);
1816  return 1;
1817  }
1818  g_free (tmp);
1819 
1820  /* Hostnames in hosts list shouldn't be resolved. */
1821  if (addr && gvm_host_type (current_host) != HOST_TYPE_NAME)
1822  {
1823  struct in6_addr tmpaddr;
1824  gvm_host_get_addr6 (current_host, &tmpaddr);
1825 
1826  if (memcmp (addr->s6_addr, &tmpaddr.s6_addr, 16) == 0)
1827  {
1828  g_free (host_str);
1829  return 1;
1830  }
1831  }
1832  }
1833 
1834  g_free (host_str);
1835  return 0;
1836 }
1837 
1845 enum host_type
1847 {
1848  assert (host);
1849  return host->type;
1850 }
1851 
1860 gchar *
1862 {
1863  if (host == NULL)
1864  return NULL;
1865 
1866  return host_type_str[host->type];
1867 }
1868 
1876 gchar *
1878 {
1879  if (host == NULL)
1880  return NULL;
1881 
1882  switch (host->type)
1883  {
1884  case HOST_TYPE_NAME:
1885  return g_strdup (host->name);
1886  break;
1887  case HOST_TYPE_IPV4:
1888  case HOST_TYPE_IPV6:
1889  /* Handle both cases using inet_ntop(). */
1890  {
1891  int family, size;
1892  gchar *str;
1893  const void *srcaddr;
1894 
1895  if (host->type == HOST_TYPE_IPV4)
1896  {
1897  family = AF_INET;
1898  size = INET_ADDRSTRLEN;
1899  srcaddr = &host->addr;
1900  }
1901  else
1902  {
1903  family = AF_INET6;
1904  size = INET6_ADDRSTRLEN;
1905  srcaddr = &host->addr6;
1906  }
1907 
1908  str = g_malloc0 (size);
1909  if (inet_ntop (family, srcaddr, str, size) == NULL)
1910  {
1911  perror ("inet_ntop");
1912  g_free (str);
1913  return NULL;
1914  }
1915  return str;
1916  }
1917  default:
1918  return g_strdup ("Erroneous host type: Should be Hostname/IPv4/IPv6.");
1919  }
1920 }
1921 
1933 int
1934 gvm_host_resolve (const gvm_host_t *host, void *dst, int family)
1935 {
1936  if (host == NULL || dst == NULL || host->type != HOST_TYPE_NAME)
1937  return -1;
1938 
1939  return gvm_resolve (host->name, dst, family);
1940 }
1941 
1954 int
1955 gvm_host_get_addr6 (const gvm_host_t *host, struct in6_addr *ip6)
1956 {
1957  if (host == NULL || ip6 == NULL)
1958  return -1;
1959 
1960  switch (gvm_host_type (host))
1961  {
1962  case HOST_TYPE_IPV6:
1963  memcpy (ip6, &host->addr6, sizeof (struct in6_addr));
1964  return 0;
1965 
1966  case HOST_TYPE_IPV4:
1967  ipv4_as_ipv6 (&host->addr, ip6);
1968  return 0;
1969 
1970  case HOST_TYPE_NAME:
1971  {
1972  struct in_addr ip4;
1973 
1974  /* Fail if IPv4 and IPv6 both don't resolve. */
1975  if (gvm_host_resolve (host, &ip4, AF_INET) == 0)
1976  ipv4_as_ipv6 (&ip4, ip6);
1977  else if (gvm_host_resolve (host, ip6, AF_INET6) == -1)
1978  return -1;
1979  return 0;
1980  }
1981 
1982  default:
1983  return -1;
1984  }
1985 }
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:1877
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:637
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:1675
gvm_host::name
gchar * name
Definition: hosts.h:67
addr6_to_str
void addr6_to_str(const struct in6_addr *addr6, char *str)
Stringifies an IP address.
Definition: networking.c:260
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:530
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:1714
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:277
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:317
is_hostname
static int is_hostname(const char *str)
Checks if a buffer points to a valid hostname. Valid characters include: Alphanumerics,...
Definition: hosts.c:408
gvm_hosts::count
size_t count
Definition: hosts.h:96
gvm_hosts::removed
size_t removed
Definition: hosts.h:97
gvm_hosts_free
void gvm_hosts_free(gvm_hosts_t *hosts)
Frees memory occupied by an gvm_hosts_t structure.
Definition: hosts.c:1258
gvm_resolve_list
GSList * gvm_resolve_list(const char *name)
Returns a list of addresses that a hostname resolves to.
Definition: networking.c:338
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:242
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:921
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:431
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:242
HOST_TYPE_IPV6
@ HOST_TYPE_IPV6
Definition: hosts.h:43
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:1861
gvm_host_free
static void gvm_host_free(gpointer host)
Frees the memory occupied by an gvm_host_t object.
Definition: hosts.c:880
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:725
gvm_hosts::max_size
size_t max_size
Definition: hosts.h:94
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:1764
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:602
HOST_TYPE_RANGE_SHORT
@ HOST_TYPE_RANGE_SHORT
Definition: hosts.h:41
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:1034
gvm_host::type
enum host_type type
Definition: hosts.h:71
gvm_resolve
int gvm_resolve(const char *name, void *dst, int family)
Resolves a hostname to an IPv4 or IPv6 address.
Definition: networking.c:388
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:1955
gvm_vhost
The structure for a single vhost object.
Definition: hosts.h:78
HOST_TYPE_IPV4
@ HOST_TYPE_IPV4
Definition: hosts.h:39
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:495
gvm_hosts_add
static 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:901
gvm_hosts::hosts
gvm_host_t ** hosts
Definition: hosts.h:93
gvm_get_host_type
int gvm_get_host_type(const gchar *str_stripped)
Determines the host type in a buffer.
Definition: hosts.c:771
gvm_host
The structure for a single host object.
Definition: hosts.h:63
HOST_TYPE_CIDR6_BLOCK
@ HOST_TYPE_CIDR6_BLOCK
Definition: hosts.h:44
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:1536
gvm_hosts::current
size_t current
Definition: hosts.h:95
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:211
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:1229
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:1407
gvm_vhost::value
char * value
Definition: hosts.h:80
HOST_TYPE_RANGE_LONG
@ HOST_TYPE_RANGE_LONG
Definition: hosts.h:42
gvm_host::addr
struct in_addr addr
Definition: hosts.h:68
HOST_TYPE_RANGE6_LONG
@ HOST_TYPE_RANGE6_LONG
Definition: hosts.h:45
gvm_host_type
enum host_type gvm_host_type(const gvm_host_t *host)
Gets a host object's type.
Definition: hosts.c:1846
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:1796
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:104
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:1462
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:72
gvm_host_new
static gvm_host_t * gvm_host_new()
Creates a new gvm_host_t object.
Definition: hosts.c:865
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:171
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:1549
gvm_vhost::source
char * source
Definition: hosts.h:81
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:1243
HOST_TYPE_MAX
@ HOST_TYPE_MAX
Definition: hosts.h:47
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:677
gvm_vhost_free
static void gvm_vhost_free(gpointer vhost)
Frees the memory occupied by an gvm_vhost_t object.
Definition: hosts.c:849
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:1281
gvm_hosts::orig_str
gchar * orig_str
Definition: hosts.h:92
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:1601
host_type
host_type
Definition: hosts.h:36
host_type_str
gchar * host_type_str[HOST_TYPE_MAX]
Definition: hosts.c:53
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:150
gvm_host::addr6
struct in6_addr addr6
Definition: hosts.h:69
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:1313
gvm_vhost_new
gvm_vhost_t * gvm_vhost_new(char *value, char *source)
Creates a new gvm_vhost_t object.
Definition: hosts.c:832
HOST_TYPE_CIDR_BLOCK
@ HOST_TYPE_CIDR_BLOCK
Definition: hosts.h:40
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:1339
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:939
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:362
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:474
HOST_TYPE_RANGE6_SHORT
@ HOST_TYPE_RANGE6_SHORT
Definition: hosts.h:46
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:977
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:1633
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:88
HOST_TYPE_NAME
@ HOST_TYPE_NAME
Definition: hosts.h:38
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:1934
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:1778
gvm_host::vhosts
GSList * vhosts
Definition: hosts.h:72
gvm_hosts
The structure for Hosts collection.
Definition: hosts.h:90
hosts.h
Protos and data structures for Hosts collections and single hosts objects.