OpenVAS Scanner  7.0.0~git
hosts.c File Reference

Basically creates a new process for each tested host. More...

#include "hosts.h"
#include "../misc/network.h"
#include "utils.h"
#include <errno.h>
#include <glib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
Include dependency graph for hosts.c:

Go to the source code of this file.

Data Structures

struct  host
 Host information, implemented as doubly linked list. More...
 

Macros

#define G_LOG_DOMAIN   "sd main"
 GLib log domain. More...
 

Functions

static void host_set_time (kb_t kb, char *key)
 
static void host_rm (struct host *h)
 
static int hosts_num (void)
 Returns the number of entries in the global hosts list. More...
 
static struct hosthosts_get (char *name)
 Retrieves a host specified by its name from the global host list. More...
 
int hosts_init (int max_hosts)
 
int hosts_new (char *name, kb_t kb)
 
int hosts_set_pid (char *name, pid_t pid)
 
static int hosts_stop_host (struct host *h)
 
void hosts_stop_all (void)
 
static void hosts_read_data (void)
 
int hosts_read (void)
 Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise. More...
 

Variables

static struct hosthosts = NULL
 
static int g_max_hosts = 15
 
int global_scan_stop
 

Detailed Description

Basically creates a new process for each tested host.

Definition in file hosts.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "sd main"

GLib log domain.

Definition at line 41 of file hosts.c.

Function Documentation

◆ host_rm()

static void host_rm ( struct host h)
static

Definition at line 84 of file hosts.c.

85 {
86  if (h->pid != 0)
87  waitpid (h->pid, NULL, WNOHANG);
88 
89  if (!global_scan_stop)
90  host_set_time (h->host_kb, "internal/end_time");
91  if (h->next != NULL)
92  h->next->prev = h->prev;
93 
94  if (h->prev != NULL)
95  h->prev->next = h->next;
96 
97  kb_lnk_reset (h->host_kb);
98  g_free (h->name);
99  g_free (h->ip);
100  g_free (h);
101 }

References global_scan_stop, host::host_kb, host_set_time(), host::ip, host::name, host::next, host::pid, and host::prev.

Referenced by hosts_read_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_set_time()

static void host_set_time ( kb_t  kb,
char *  key 
)
static

Definition at line 65 of file hosts.c.

66 {
67  char timestr[1024];
68  char *tmp;
69  time_t t;
70  int len;
71 
72  t = time (NULL);
73  tmp = ctime (&t);
74  timestr[sizeof (timestr) - 1] = '\0';
75  strncpy (timestr, tmp, sizeof (timestr) - 1);
76  len = strlen (timestr);
77  if (timestr[len - 1] == '\n')
78  timestr[len - 1] = '\0';
79 
80  kb_item_push_str (kb, key, timestr);
81 }

Referenced by host_rm(), and hosts_read_data().

Here is the caller graph for this function:

◆ hosts_get()

static struct host* hosts_get ( char *  name)
static

Retrieves a host specified by its name from the global host list.

Definition at line 124 of file hosts.c.

125 {
126  struct host *h = hosts;
127  while (h != NULL)
128  {
129  if (strcmp (h->name, name) == 0)
130  return h;
131  h = h->next;
132  }
133  return NULL;
134 }

References hosts, host::name, name, and host::next.

Referenced by hosts_set_pid().

Here is the caller graph for this function:

◆ hosts_init()

int hosts_init ( int  max_hosts)

Definition at line 137 of file hosts.c.

138 {
139  g_max_hosts = max_hosts;
140  return 0;
141 }

References g_max_hosts.

Referenced by attack_network().

Here is the caller graph for this function:

◆ hosts_new()

int hosts_new ( char *  name,
kb_t  kb 
)

Definition at line 144 of file hosts.c.

145 {
146  struct host *h;
147 
148  while (hosts_num () >= g_max_hosts)
149  {
150  if (hosts_read () < 0)
151  return -1;
152  }
153  if (global_scan_stop)
154  return 0;
155 
156  h = g_malloc0 (sizeof (struct host));
157  h->name = g_strdup (name);
158  h->pid = 0;
159  h->host_kb = kb;
160  if (hosts != NULL)
161  hosts->prev = h;
162  h->next = hosts;
163  h->prev = NULL;
164  hosts = h;
165  return 0;
166 }

References g_max_hosts, global_scan_stop, host::host_kb, hosts, hosts_num(), hosts_read(), host::name, name, host::next, host::pid, and host::prev.

Referenced by attack_network().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hosts_num()

static int hosts_num ( void  )
static

Returns the number of entries in the global hosts list.

Definition at line 109 of file hosts.c.

110 {
111  struct host *h = hosts;
112  int num;
113 
114  for (num = 0; h != NULL; num++, h = h->next)
115  ;
116 
117  return num;
118 }

References hosts, and host::next.

Referenced by hosts_new().

Here is the caller graph for this function:

◆ hosts_read()

int hosts_read ( void  )

Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise.

Definition at line 250 of file hosts.c.

251 {
252  if (hosts == NULL)
253  return -1;
254 
255  hosts_read_data ();
256  usleep (500000);
257 
258  return 0;
259 }

References hosts, and hosts_read_data().

Referenced by attack_network(), and hosts_new().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hosts_read_data()

static void hosts_read_data ( void  )
static

Definition at line 211 of file hosts.c.

212 {
213  struct host *h = hosts;
214 
215  waitpid (-1, NULL, WNOHANG);
216 
217  if (h == NULL)
218  return;
219 
220  while (h)
221  {
222  if (!h->ip)
223  {
224  /* Scan started. */
225  h->ip = kb_item_get_str (h->host_kb, "internal/ip");
226  if (h->ip)
227  host_set_time (h->host_kb, "internal/start_time");
228  }
229  if (h->ip)
230  {
231  if (kill (h->pid, 0) < 0) /* Process is dead */
232  {
233  if (!h->prev)
234  hosts = hosts->next;
235  host_rm (h);
236  h = hosts;
237  if (!h)
238  break;
239  }
240  }
241  h = h->next;
242  }
243 }

References host::host_kb, host_rm(), host_set_time(), hosts, host::ip, host::next, host::pid, and host::prev.

Referenced by hosts_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hosts_set_pid()

int hosts_set_pid ( char *  name,
pid_t  pid 
)

Definition at line 169 of file hosts.c.

170 {
171  struct host *h = hosts_get (name);
172  if (h == NULL)
173  {
174  g_debug ("host_set_pid() failed!\n");
175  return -1;
176  }
177 
178  h->pid = pid;
179  return 0;
180 }

References hosts_get(), name, host::pid, and pid.

Referenced by attack_network().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hosts_stop_all()

void hosts_stop_all ( void  )

Definition at line 196 of file hosts.c.

197 {
198  struct host *host = hosts;
199 
200  global_scan_stop = 1;
201  while (host)
202  {
204  host = host->next;
205  }
206 }

References global_scan_stop, hosts, hosts_stop_host(), and host::next.

Referenced by handle_stop_all_scans_signal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hosts_stop_host()

static int hosts_stop_host ( struct host h)
static

Definition at line 184 of file hosts.c.

185 {
186  if (h == NULL)
187  return -1;
188 
189  g_message ("Stopping host %s scan", h->name);
190  kill (h->pid, SIGUSR1);
191  kb_delete (h->host_kb);
192  return 0;
193 }

References host::host_kb, host::name, and host::pid.

Referenced by hosts_stop_all().

Here is the caller graph for this function:

Variable Documentation

◆ g_max_hosts

int g_max_hosts = 15
static

Definition at line 59 of file hosts.c.

Referenced by hosts_init(), and hosts_new().

◆ global_scan_stop

int global_scan_stop

◆ hosts

struct host* hosts = NULL
static
Todo:
struct hosts could be stripped down and put in a g_list, or, as a g_hash_table (name -> [soc,pid]), see hosts_get.

Definition at line 58 of file hosts.c.

Referenced by apply_hosts_preferences(), attack_network(), hosts_get(), hosts_new(), hosts_num(), hosts_read(), hosts_read_data(), hosts_stop_all(), and main().

hosts_read
int hosts_read(void)
Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise.
Definition: hosts.c:250
host::pid
pid_t pid
Definition: hosts.c:50
host::ip
char * ip
Definition: hosts.c:49
pid
static pid_t pid
Definition: nasl_builtin_nmap.c:499
hosts_get
static struct host * hosts_get(char *name)
Retrieves a host specified by its name from the global host list.
Definition: hosts.c:124
host_rm
static void host_rm(struct host *h)
Definition: hosts.c:84
hosts_read_data
static void hosts_read_data(void)
Definition: hosts.c:211
name
const char * name
Definition: nasl_init.c:377
g_max_hosts
static int g_max_hosts
Definition: hosts.c:59
host_set_time
static void host_set_time(kb_t kb, char *key)
Definition: hosts.c:65
global_scan_stop
int global_scan_stop
Definition: attack.c:224
host::prev
struct host * prev
Definition: hosts.c:53
hosts_stop_host
static int hosts_stop_host(struct host *h)
Definition: hosts.c:184
host::name
char * name
Definition: hosts.c:48
host
Host information, implemented as doubly linked list.
Definition: hosts.c:46
hosts_num
static int hosts_num(void)
Returns the number of entries in the global hosts list.
Definition: hosts.c:109
host::host_kb
kb_t host_kb
Definition: hosts.c:51
hosts
static struct host * hosts
Definition: hosts.c:58
host::next
struct host * next
Definition: hosts.c:52