OpenVAS Libraries  9.0.3
test-hosts.c File Reference

Stand-alone tool to test module "openvas_hosts". More...

#include <stdio.h>
#include "openvas_hosts.h"
Include dependency graph for test-hosts.c:

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Stand-alone tool to test module "openvas_hosts".

This file offers a command line interface to test the functionalities of the hosts object.

Definition in file test-hosts.c.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 39 of file test-hosts.c.

References HOST_TYPE_NAME, openvas_host::name, name, openvas_host_resolve(), openvas_host_type(), openvas_host_type_str(), openvas_host_value_str(), openvas_hosts_count(), openvas_hosts_exclude(), openvas_hosts_free(), openvas_hosts_new(), openvas_hosts_next(), and openvas_hosts_removed().

40 {
41  openvas_hosts_t *hosts;
42  openvas_host_t *host;
43  int i;
44 
45  if (argc < 2)
46  return 1;
47  hosts = openvas_hosts_new (argv[1]);
48  if (hosts == NULL)
49  return 1;
50  if (argv[2])
51  {
52  if (openvas_hosts_exclude (hosts, argv[2], 1) == -1)
53  return 2;
54  }
55 
56  printf ("Count: %u\n", openvas_hosts_count (hosts));
57  printf ("Removed: %u\n", openvas_hosts_removed (hosts));
58 
59  i = 1;
60  while ((host = openvas_hosts_next (hosts)))
61  {
62  char *str;
63 
64  str = openvas_host_value_str (host);
65  if (openvas_host_type (host) == HOST_TYPE_NAME)
66  {
67  char name[INET_ADDRSTRLEN], name6[INET6_ADDRSTRLEN];
68  struct in_addr addr;
69  struct in6_addr addr6;
70 
71  if (openvas_host_resolve (host, &addr, AF_INET) == -1)
72  {
73  fprintf (stderr, "ERROR - %s: Couldn't resolve IPv4 address.\n",
74  host->name);
75  printf ("#%d %s %s\n", i, openvas_host_type_str (host), str);
76  i++;
77  g_free (str);
78  continue;
79  }
80  if (inet_ntop (AF_INET, &addr, name, sizeof (name)) == NULL)
81  {
82  printf ("inet_ntop() error.\n");
83  break;
84  }
85 
86  if (openvas_host_resolve (host, &addr6, AF_INET6) == -1)
87  {
88  fprintf (stderr, "ERROR - %s: Couldn't resolve IPv6 address.\n",
89  host->name);
90  printf ("#%d %s %s (%s)\n", i, openvas_host_type_str (host),
91  str, name);
92  i++;
93  g_free (str);
94  continue;
95  }
96  if (inet_ntop (AF_INET6, &addr6, name6, sizeof (name6)) == NULL)
97  {
98  printf ("inet_ntop() error.\n");
99  break;
100  }
101 
102  printf ("#%d %s %s (%s / %s)\n", i, openvas_host_type_str (host), str,
103  name, name6);
104  }
105  else
106  printf ("#%d %s %s\n", i, openvas_host_type_str (host), str);
107 
108  i++;
109  g_free (str);
110  }
111 
112  openvas_hosts_free (hosts);
113  return 0;
114 }
The structure for a single host object.
Definition: openvas_hosts.h:76
gchar * openvas_host_value_str(const openvas_host_t *host)
Gets a host&#39;s value in printable format.
void openvas_hosts_free(openvas_hosts_t *hosts)
Frees memory occupied by an openvas_hosts_t structure.
openvas_host_t * openvas_hosts_next(openvas_hosts_t *hosts)
Gets the next openvas_host_t from a openvas_hosts_t structure. The state of iteration is kept interna...
gchar * openvas_host_type_str(const openvas_host_t *host)
Gets a host&#39;s type in printable format.
openvas_hosts_t * openvas_hosts_new(const gchar *hosts_str)
Creates a new openvas_hosts_t structure and the associated hosts objects from the provided hosts_str...
int openvas_host_resolve(const openvas_host_t *host, void *dst, int family)
Resolves a host object&#39;s name to an IPv4 or IPv6 address. Host object should be of type HOST_TYPE_NAM...
enum host_type openvas_host_type(const openvas_host_t *host)
Gets a host object&#39;s type.
const char * name
Definition: nasl_init.c:524
unsigned int openvas_hosts_count(const openvas_hosts_t *hosts)
Gets the count of single hosts objects in a hosts collection.
unsigned int openvas_hosts_removed(const openvas_hosts_t *hosts)
Gets the count of single values in hosts string that were removed (duplicates / excluded.)
The structure for Hosts collection.
Definition: openvas_hosts.h:92
int openvas_hosts_exclude(openvas_hosts_t *hosts, const char *excluded_str, int resolve)
Excludes a set of hosts provided as a string from a hosts collection. Not to be used while iterating ...
gchar * name
Definition: openvas_hosts.h:79
Here is the call graph for this function: