OpenVAS Scanner 23.40.3
hosts.c File Reference

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

#include "hosts.h"
#include "../misc/network.h"
#include "../misc/plugutils.h"
#include "utils.h"
#include <errno.h>
#include <glib.h>
#include <gvm/base/networking.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.

Functions

void host_set_time (kb_t kb, char *ip, char *type)
 Add star_scan and end_scan results to the main kb.
static void host_rm (struct host *h)
static int hosts_num (void)
 Returns the number of entries in the global hosts list.
static struct hosthosts_get (char *name)
 Retrieves a host specified by its name from the global host list.
int hosts_init (int max_hosts)
int hosts_new (char *name, kb_t kb, kb_t main_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.
int host_is_currently_scanned (gvm_host_t *host_to_check)
 Returns 1 if the host is being scanned. 0 otherwise.

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

Function Documentation

◆ host_is_currently_scanned()

int host_is_currently_scanned ( gvm_host_t * host_to_check)

Returns 1 if the host is being scanned. 0 otherwise.

It checks not only the main IP of the host, but also the ips that a dns-lookup returns.

Definition at line 271 of file hosts.c.

272{
273 struct host *h = hosts;
274
275 GSList *list, *tmp;
276 char *vhost = NULL;
277
278 hosts_read ();
279
280 if (h == NULL)
281 return 0;
282
283 vhost = gvm_host_reverse_lookup (host_to_check);
284 if (!vhost)
285 return 0;
286
287 list = tmp = gvm_resolve_list (vhost);
288 g_free (vhost);
289 while (tmp)
290 {
291 h = hosts;
292 char buffer[INET6_ADDRSTRLEN];
293 addr6_to_str (tmp->data, buffer);
294
295 while (h != NULL)
296 {
297 if (!strcasecmp (h->name, buffer))
298 {
299 g_slist_free_full (list, g_free);
300 return 1;
301 }
302 h = h->next;
303 }
304 tmp = tmp->next;
305 }
306
307 g_slist_free_full (list, g_free);
308 return 0;
309}
static struct host * hosts
Definition hosts.c:49
int hosts_read(void)
Returns -1 if client asked to stop all tests or connection was lost or error. 0 otherwise.
Definition hosts.c:253
Host information, implemented as doubly linked list.
Definition hosts.c:37
struct host * next
Definition hosts.c:43
char * name
Definition hosts.c:38

References hosts, hosts_read(), host::name, and host::next.

Referenced by attack_network().

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

◆ host_rm()

void host_rm ( struct host * h)
static

Definition at line 88 of file hosts.c.

89{
90 if (h->pid != 0)
91 waitpid (h->pid, NULL, WNOHANG);
92
93 if (h->next != NULL)
94 h->next->prev = h->prev;
95
96 if (h->prev != NULL)
97 h->prev->next = h->next;
98
99 if (h->host_kb)
100 {
101 kb_delete (h->host_kb);
102 h->host_kb = NULL;
103 kb_lnk_reset (h->results_kb);
104 }
105
106 g_free (h->name);
107 g_free (h);
108}
struct host * prev
Definition hosts.c:44
kb_t host_kb
Definition hosts.c:41
pid_t pid
Definition hosts.c:40
kb_t results_kb
Definition hosts.c:42

References host::host_kb, host::name, host::next, host::pid, host::prev, and host::results_kb.

Referenced by hosts_read_data().

Here is the caller graph for this function:

◆ host_set_time()

void host_set_time ( kb_t kb,
char * ip,
char * type )

Add star_scan and end_scan results to the main kb.

Parameters
[in]kbMain KB where results are stored.
[in]ipList of vhosts to add new vhosts to.
[in]typeIf it is start or end message.

Definition at line 64 of file hosts.c.

65{
66 char *timestr;
67 char log_msg[1024];
68 time_t t;
69 int len;
70
71 t = time (NULL);
72 char ts[26];
73 char *ts_ptr = ts;
74 ctime_r (&t, ts_ptr);
75 timestr = g_strdup (ts_ptr);
76 len = strlen (timestr);
77 if (timestr[len - 1] == '\n')
78 timestr[len - 1] = '\0';
79
80 snprintf (log_msg, sizeof (log_msg), "%s|||%s||||||||| |||%s", type, ip,
81 timestr);
82 g_free (timestr);
83
84 kb_item_push_str_with_main_kb_check (kb, "internal/results", log_msg);
85}
uint8_t len
int kb_item_push_str_with_main_kb_check(kb_t kb, const char *name, const char *value)
Check if the current kb corresponds to the original scanid, if it matches it kb_item_push_str....
Definition plugutils.c:533

References host::ip, kb_item_push_str_with_main_kb_check(), and len.

Referenced by attack_host().

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

◆ hosts_get()

struct host * hosts_get ( char * name)
static

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

Definition at line 131 of file hosts.c.

132{
133 struct host *h = hosts;
134 while (h != NULL)
135 {
136 if (strcmp (h->name, name) == 0)
137 return h;
138 h = h->next;
139 }
140 return NULL;
141}
const char * name
Definition nasl_init.c:440

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

145{
146 g_max_hosts = max_hosts;
147 return 0;
148}
static int g_max_hosts
Definition hosts.c:50

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,
kb_t main_kb )

Definition at line 151 of file hosts.c.

152{
153 struct host *h;
154
155 while (hosts_num () >= g_max_hosts)
156 {
157 if (hosts_read () < 0)
158 return -1;
159 }
161 return 0;
162
163 h = g_malloc0 (sizeof (struct host));
164 h->name = g_strdup (name);
165 h->pid = 0;
166 h->host_kb = kb;
167 h->results_kb = main_kb;
168 if (hosts != NULL)
169 hosts->prev = h;
170 h->next = hosts;
171 h->prev = NULL;
172 hosts = h;
173 return 0;
174}
int global_scan_stop
Definition attack.c:261
static int hosts_num(void)
Returns the number of entries in the global hosts list.
Definition hosts.c:116
kb_t main_kb
Definition kb_cache.c:15

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

Referenced by attack_network().

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

◆ hosts_num()

int hosts_num ( void )
static

Returns the number of entries in the global hosts list.

Definition at line 116 of file hosts.c.

117{
118 struct host *h = hosts;
119 int num;
120
121 for (num = 0; h != NULL; num++, h = h->next)
122 ;
123
124 return num;
125}

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

254{
255 if (hosts == NULL)
256 return -1;
257
259 usleep (500000);
260
261 return 0;
262}
static void hosts_read_data(void)
Definition hosts.c:218

References hosts, and hosts_read_data().

Referenced by attack_network(), host_is_currently_scanned(), and hosts_new().

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

◆ hosts_read_data()

void hosts_read_data ( void )
static

Definition at line 218 of file hosts.c.

219{
220 struct host *h = hosts;
221 int ret = 1;
222
223 while (ret > 0)
224 {
225 ret = waitpid (-1, NULL, WNOHANG);
226 if (ret < 0)
227 g_debug ("waitpid() failed. %s)", strerror (errno));
228 }
229
230 if (h == NULL)
231 return;
232
233 while (h)
234 {
235 if (h->pid != 0 && kill (h->pid, 0) < 0) /* Process is dead */
236 {
237 if (!h->prev)
238 hosts = hosts->next;
239 host_rm (h);
240 h = hosts;
241 if (!h)
242 break;
243 }
244 h = h->next;
245 }
246}
static void host_rm(struct host *h)
Definition hosts.c:88

References host_rm(), hosts, 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 177 of file hosts.c.

178{
179 struct host *h = hosts_get (name);
180 if (h == NULL)
181 {
182 g_debug ("host_set_pid() failed!\n");
183 return -1;
184 }
185
186 h->pid = pid;
187 return 0;
188}
static struct host * hosts_get(char *name)
Retrieves a host specified by its name from the global host list.
Definition hosts.c:131
static pid_t pid

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

204{
205 struct host *host = hosts;
206
208 while (host)
209 {
211 host = host->next;
212 }
213}
static int hosts_stop_host(struct host *h)
Definition hosts.c:192

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

Referenced by scan_stop_cleanup().

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

◆ hosts_stop_host()

int hosts_stop_host ( struct host * h)
static

Definition at line 192 of file hosts.c.

193{
194 if (h == NULL)
195 return -1;
196
197 g_message ("Stopping host %s scan (pid: %d)", h->name, h->pid);
198 kill (h->pid, SIGUSR1);
199 return 0;
200}

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

Referenced by hosts_init(), and hosts_new().

◆ global_scan_stop

int global_scan_stop
extern

Definition at line 261 of file attack.c.

Referenced by handle_scan_stop_signal(), hosts_new(), hosts_stop_all(), and scan_is_stopped().

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

Referenced by apply_hosts_excluded(), apply_hosts_preferences_ordering(), apply_hosts_reverse_lookup_preferences(), attack_network(), host_is_currently_scanned(), hosts_get(), hosts_new(), hosts_num(), hosts_read(), hosts_read_data(), hosts_stop_all(), and main().