OpenVAS Libraries  9.0.3
test-hosts.c
Go to the documentation of this file.
1 /* openvas-libraries/base
2  * $Id$
3  * Description: Stand-alone tool to test module "openvas_hosts".
4  *
5  * Authors:
6  * Hani Benhabiles <hani.benhabiles@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2013 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
34 #include <stdio.h>
35 
36 #include "openvas_hosts.h"
37 
38 int
39 main (int argc, char **argv)
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.
Protos and data structures for Hosts collections and single hosts objects.
void openvas_hosts_free(openvas_hosts_t *hosts)
Frees memory occupied by an openvas_hosts_t structure.
int main(int argc, char **argv)
Definition: test-hosts.c:39
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