Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to re allocate the finish flag in the hosts queue for alive tests. #407

Merged
merged 10 commits into from
Oct 12, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Added
- Use dedicated port list for alive detection (Boreas only) if supplied via OSP. [#391](https://github.com/greenbone/gvm-libs/pull/391)
- Allow to re allocate the finish flag in the host queue for alive tests. [#407](https://github.com/greenbone/gvm-libs/pull/407)

### Changed
- Add separators for a new (ip address) field in ERRMSG and DEADHOST messages. [#376](https://github.com/greenbone/gvm-libs/pull/376)
Expand Down
2 changes: 1 addition & 1 deletion base/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ gvm_host_new ()
*
* @param[in] host Host to free.
*/
static void
void
gvm_host_free (gpointer host)
{
gvm_host_t *h = host;
Expand Down
2 changes: 2 additions & 0 deletions base/hosts.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ gvm_host_get_addr6 (const gvm_host_t *, struct in6_addr *);
void
gvm_host_add_reverse_lookup (gvm_host_t *);

void gvm_host_free (gpointer);
mattmundell marked this conversation as resolved.
Show resolved Hide resolved

/* Miscellaneous functions */

gvm_vhost_t *
Expand Down
49 changes: 48 additions & 1 deletion boreas/boreas_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ get_host_from_queue (kb_t alive_hosts_kb, gboolean *alive_deteciton_finished)
* @param kb KB to use.
* @param addr_str IP addr in str representation to put on queue.
*/
static void
void
put_host_on_queue (kb_t kb, char *addr_str)
{
/* Print host on command line if no kb is available. No kb available could
Expand All @@ -238,6 +238,53 @@ put_host_on_queue (kb_t kb, char *addr_str)
__func__, addr_str);
}

/**
* @brief Checks if the finish signal is already set as last item in the queue.
*
* @param main_kb kb to use
* @return 1 if it is already set. 0 otherwise.
*/
int
finish_signal_on_queue (kb_t main_kb)
{
struct kb_item *last_queue_item;
int ret;

ret = 0;
last_queue_item =
kb_item_get_single (main_kb, ALIVE_DETECTION_QUEUE, KB_TYPE_STR);

if (last_queue_item && (last_queue_item->type == KB_TYPE_STR)
&& (!g_strcmp0 (last_queue_item->v_str, ALIVE_DETECTION_FINISHED)))
ret = 1;

kb_item_free (last_queue_item);
return ret;
}

/**
* @brief Reallocate finish signal in last position of the alive detection
* queue.
*
* @param main_kb kb to use
*/
void
realloc_finish_signal_on_queue (kb_t main_kb)
{
int kb_item_push_str_err, pos;

/* The alive test queue is a FIFO queue. Alive hosts are taken from the
* right side of the queue. Therefore the finish signal is put in the
* left end of queue, being the last item to be fetch.*/
pos = 1;
kb_item_push_str_err = kb_item_add_str_unique (
main_kb, ALIVE_DETECTION_QUEUE, ALIVE_DETECTION_FINISHED, 0, pos);
if (kb_item_push_str_err)
g_debug ("%s: Could not push the Boreas finish signal on the alive "
"detection Queue.",
__func__);
}

/**
* @brief Put finish signal on alive detection queue.
*
Expand Down
9 changes: 9 additions & 0 deletions boreas/boreas_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@
gvm_host_t *
get_host_from_queue (kb_t, gboolean *);

void
put_host_on_queue (kb_t, char *);

void
put_finish_signal_on_queue (void *);

void
realloc_finish_signal_on_queue (kb_t);

int
finish_signal_on_queue (kb_t);

void
send_dead_hosts_to_ospd_openvas (int);

Expand Down
1 change: 0 additions & 1 deletion boreas/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ fill_ports_array (gpointer range, gpointer ports_array)
}
else
{

for (i = range_start; i <= range_end; i++)
{
port_sized = (uint16_t) i;
Expand Down
10 changes: 7 additions & 3 deletions util/kb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1112,11 +1112,14 @@ redis_del_items (kb_t kb, const char *name)
* @param[in] name Item name.
* @param[in] str Item value.
* @param[in] len Value length. Used for blobs.
* @param[in] pos Which position the value is appended to. 0 for right,
* 1 for left position in the list.
*
* @return 0 on success, non-null on error.
*/
static int
redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len,
int pos)
{
struct kb_redis *kbr;
redisReply *rep = NULL;
Expand All @@ -1135,7 +1138,7 @@ redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
if (len == 0)
{
redisAppendCommand (ctx, "LREM %s 1 %s", name, str);
redisAppendCommand (ctx, "RPUSH %s %s", name, str);
redisAppendCommand (ctx, "%s %s %s", pos ? "LPUSH" : "RPUSH", name, str);
redisGetReply (ctx, (void **) &rep);
if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
g_debug ("Key '%s' already contained value '%s'", name, str);
Expand All @@ -1145,7 +1148,8 @@ redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
else
{
redisAppendCommand (ctx, "LREM %s 1 %b", name, str, len);
redisAppendCommand (ctx, "RPUSH %s %b", name, str, len);
redisAppendCommand (ctx, "%s %s %b", pos ? "LPUSH" : "RPUSH", name, str,
len);
redisGetReply (ctx, (void **) &rep);
if (rep && rep->type == REDIS_REPLY_INTEGER && rep->integer == 1)
g_debug ("Key '%s' already contained string '%s'", name, str);
Expand Down
9 changes: 6 additions & 3 deletions util/kb.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ struct kb_operations
* Function provided by an implementation to insert (append) a new
* unique entry under a given name.
*/
int (*kb_add_str_unique) (kb_t, const char *, const char *, size_t);
int (*kb_add_str_unique) (kb_t, const char *, const char *, size_t, int);
/**
* Function provided by an implementation to get (replace) a new entry
* under a given name.
Expand Down Expand Up @@ -458,16 +458,19 @@ kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len)
* @param[in] name Item name.
* @param[in] str Item value.
* @param[in] len Value length. Used for blobs.
* @param[in] pos Which position the value is appended to. 0 for right,
* 1 for left position in the list.
* @return 0 on success, non-null on error.
*/
static inline int
kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len)
kb_item_add_str_unique (kb_t kb, const char *name, const char *str, size_t len,
int pos)
{
assert (kb);
assert (kb->kb_ops);
assert (kb->kb_ops->kb_add_str_unique);

return kb->kb_ops->kb_add_str_unique (kb, name, str, len);
return kb->kb_ops->kb_add_str_unique (kb, name, str, len, pos);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions util/xmlutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,13 @@ try_read_entity_and_string (gnutls_session_t *session, int timeout,
else if ((timeout == 0) && (count == GNUTLS_E_AGAIN))
{
/* Server still busy, try read again.
If there is no timeout set and the server is still not ready,
it will try up to 10 times before closing the socket.*/
If there is no timeout set and the server is still not
ready, it will try up to 10 times before closing the
socket.*/
if (retries > 0)
{
retries = retries - 1;
continue;
retries = retries - 1;
continue;
}
}

Expand Down