OpenVAS Scanner  7.0.1~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 <stdio.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 42 of file hosts.c.

Function Documentation

◆ host_rm()

static void host_rm ( struct host h)
static

Definition at line 85 of file hosts.c.

86 {
87  if (h->pid != 0)
88  waitpid (h->pid, NULL, WNOHANG);
89 
90  if (!global_scan_stop)
91  {
92  char key[1024];
93  char *scan_id = kb_item_get_str (h->host_kb, "internal/scan_id");
94  snprintf (key, sizeof (key), "internal/%s", scan_id);
95  kb_item_set_str (h->host_kb, key, "finished", 0);
96 
97  host_set_time (h->host_kb, "internal/end_time");
98  kb_lnk_reset (h->host_kb);
99  g_free (scan_id);
100  }
101 
102  if (h->next != NULL)
103  h->next->prev = h->prev;
104 
105  if (h->prev != NULL)
106  h->prev->next = h->next;
107 
108  if (global_scan_stop == 1 && h->host_kb)
109  {
110  kb_delete (h->host_kb);
111  h->host_kb = NULL;
112  }
113 
114  g_free (h->name);
115  g_free (h->ip);
116  g_free (h);
117 }

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 66 of file hosts.c.

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

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 140 of file hosts.c.

141 {
142  struct host *h = hosts;
143  while (h != NULL)
144  {
145  if (strcmp (h->name, name) == 0)
146  return h;
147  h = h->next;
148  }
149  return NULL;
150 }

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 153 of file hosts.c.

154 {
155  g_max_hosts = max_hosts;
156  return 0;
157 }

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 160 of file hosts.c.

161 {
162  struct host *h;
163 
164  while (hosts_num () >= g_max_hosts)
165  {
166  if (hosts_read () < 0)
167  return -1;
168  }
169  if (global_scan_stop)
170  return 0;
171 
172  h = g_malloc0 (sizeof (struct host));
173  h->name = g_strdup (name);
174  h->pid = 0;
175  h->host_kb = kb;
176  if (hosts != NULL)
177  hosts->prev = h;
178  h->next = hosts;
179  h->prev = NULL;
180  hosts = h;
181  return 0;
182 }

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 125 of file hosts.c.

126 {
127  struct host *h = hosts;
128  int num;
129 
130  for (num = 0; h != NULL; num++, h = h->next)
131  ;
132 
133  return num;
134 }

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 271 of file hosts.c.

272 {
273  if (hosts == NULL)
274  return -1;
275 
276  hosts_read_data ();
277  usleep (500000);
278 
279  return 0;
280 }

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 226 of file hosts.c.

227 {
228  struct host *h = hosts;
229  int ret = 1;
230 
231  while (ret > 0)
232  {
233  ret = waitpid (-1, NULL, WNOHANG);
234  if (ret < 0)
235  g_debug ("waitpid() failed. %s)", strerror (errno));
236  }
237 
238  if (h == NULL)
239  return;
240 
241  while (h)
242  {
243  if (!h->ip)
244  {
245  /* Scan started. */
246  h->ip = kb_item_get_str (h->host_kb, "internal/ip");
247  if (h->ip)
248  host_set_time (h->host_kb, "internal/start_time");
249  }
250  if (h->ip)
251  {
252  if (kill (h->pid, 0) < 0) /* Process is dead */
253  {
254  if (!h->prev)
255  hosts = hosts->next;
256  host_rm (h);
257  h = hosts;
258  if (!h)
259  break;
260  }
261  }
262  h = h->next;
263  }
264 }

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 185 of file hosts.c.

186 {
187  struct host *h = hosts_get (name);
188  if (h == NULL)
189  {
190  g_debug ("host_set_pid() failed!\n");
191  return -1;
192  }
193 
194  h->pid = pid;
195  return 0;
196 }

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 211 of file hosts.c.

212 {
213  struct host *host = hosts;
214 
215  global_scan_stop = 1;
216  while (host)
217  {
219  host = host->next;
220  }
221 }

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

Referenced by handle_scan_stop_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 200 of file hosts.c.

201 {
202  if (h == NULL)
203  return -1;
204 
205  g_message ("Stopping host %s scan", h->name);
206  kill (h->pid, SIGUSR1);
207  return 0;
208 }

References 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 60 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 59 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:271
host::pid
pid_t pid
Definition: hosts.c:51
host::ip
char * ip
Definition: hosts.c:50
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:140
host_rm
static void host_rm(struct host *h)
Definition: hosts.c:85
hosts_read_data
static void hosts_read_data(void)
Definition: hosts.c:226
name
const char * name
Definition: nasl_init.c:377
g_max_hosts
static int g_max_hosts
Definition: hosts.c:60
host_set_time
static void host_set_time(kb_t kb, char *key)
Definition: hosts.c:66
global_scan_stop
int global_scan_stop
Definition: attack.c:224
host::prev
struct host * prev
Definition: hosts.c:54
hosts_stop_host
static int hosts_stop_host(struct host *h)
Definition: hosts.c:200
host::name
char * name
Definition: hosts.c:49
host
Host information, implemented as doubly linked list.
Definition: hosts.c:47
hosts_num
static int hosts_num(void)
Returns the number of entries in the global hosts list.
Definition: hosts.c:125
host::host_kb
kb_t host_kb
Definition: hosts.c:52
hosts
static struct host * hosts
Definition: hosts.c:59
host::next
struct host * next
Definition: hosts.c:53