File: test-hosts.c

package info (click to toggle)
gvm-libs 22.38.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,028 kB
  • sloc: ansic: 40,126; makefile: 26
file content (114 lines) | stat: -rw-r--r-- 3,046 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* SPDX-FileCopyrightText: 2013-2023 Greenbone AG
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

/**
 * @file
 * @brief Stand-alone tool to test module "hosts".
 *
 * This file offers a command line interface to test the functionalities
 * of the hosts object.
 */

#include "../base/hosts.h" /* for gvm_host_type_str, gvm_host_resolve, gvm_... */

#include <arpa/inet.h>     /* for inet_ntop */
#include <glib.h>          /* for g_free */
#include <netinet/in.h>    /* for INET6_ADDRSTRLEN, INET_ADDRSTRLEN, in6_addr */
#include <stdio.h>         /* for printf, fprintf, NULL, stderr */
#include <sys/socket.h>    /* for AF_INET, AF_INET6 */

static void
print_vhosts (gvm_host_t *host)
{
  GSList *tmp = host->vhosts;
  while (tmp)
    {
      gvm_vhost_t *vhost = tmp->data;
      printf (" %s [%s]", vhost->value, vhost->source);
      tmp = tmp->next;
    }
  if (host->vhosts)
    printf ("\n");
}
int
main (int argc, char **argv)
{
  gvm_hosts_t *hosts;
  gvm_host_t *host;
  int i;

  if (argc < 2)
    return 1;
  hosts = gvm_hosts_new (argv[1]);
  if (hosts == NULL)
    return 1;
  if (argv[2])
    {
      if (gvm_hosts_exclude (hosts, argv[2]) == -1)
        return 2;
    }
  gvm_hosts_resolve (hosts);

  printf ("Count: %u\n", gvm_hosts_count (hosts));
  printf ("Removed: %u\n", gvm_hosts_removed (hosts));

  i = 1;
  while ((host = gvm_hosts_next (hosts)))
    {
      char *str;

      gvm_host_add_reverse_lookup (host);
      str = gvm_host_value_str (host);
      if (gvm_host_type (host) == HOST_TYPE_NAME)
        {
          char name[INET_ADDRSTRLEN], name6[INET6_ADDRSTRLEN];
          struct in_addr addr;
          struct in6_addr addr6;

          if (gvm_host_resolve (host, &addr, AF_INET) == -1)
            {
              fprintf (stderr, "ERROR - %s: Couldn't resolve IPv4 address.\n",
                       host->name);
              printf ("#%d %s %s\n", i, gvm_host_type_str (host), str);
              i++;
              g_free (str);
              continue;
            }
          if (inet_ntop (AF_INET, &addr, name, sizeof (name)) == NULL)
            {
              printf ("inet_ntop() error.\n");
              break;
            }

          if (gvm_host_resolve (host, &addr6, AF_INET6) == -1)
            {
              fprintf (stderr, "ERROR - %s: Couldn't resolve IPv6 address.\n",
                       host->name);
              printf ("#%d %s %s (%s)\n", i, gvm_host_type_str (host), str,
                      name);
              i++;
              g_free (str);
              continue;
            }
          if (inet_ntop (AF_INET6, &addr6, name6, sizeof (name6)) == NULL)
            {
              printf ("inet_ntop() error.\n");
              break;
            }

          printf ("#%d %s %s (%s / %s)\n", i, gvm_host_type_str (host), str,
                  name, name6);
        }
      else
        printf ("#%d %s %s\n", i, gvm_host_type_str (host), str);
      print_vhosts (host);

      i++;
      g_free (str);
    }

  gvm_hosts_free (hosts);
  return 0;
}