diff --git a/CHANGELOG.md b/CHANGELOG.md index d81005820..dfbbf9c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/base/hosts.c b/base/hosts.c index d55d291d1..cca3e2b48 100644 --- a/base/hosts.c +++ b/base/hosts.c @@ -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; diff --git a/base/hosts.h b/base/hosts.h index 3fadb02b7..dd6654282 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -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); + /* Miscellaneous functions */ gvm_vhost_t * diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index 9c03deac8..5a3c81e55 100644 --- a/boreas/boreas_io.c +++ b/boreas/boreas_io.c @@ -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 @@ -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. * diff --git a/boreas/boreas_io.h b/boreas/boreas_io.h index 671bd6c29..b149683fc 100644 --- a/boreas/boreas_io.h +++ b/boreas/boreas_io.h @@ -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); diff --git a/boreas/util.c b/boreas/util.c index 33a0852fc..2d081a936 100644 --- a/boreas/util.c +++ b/boreas/util.c @@ -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; diff --git a/util/kb.c b/util/kb.c index 38c9b4345..b562b13f1 100644 --- a/util/kb.c +++ b/util/kb.c @@ -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; @@ -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); @@ -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); diff --git a/util/kb.h b/util/kb.h index 8e1d75f17..34463eca3 100644 --- a/util/kb.h +++ b/util/kb.h @@ -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. @@ -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); } /** diff --git a/util/xmlutils.c b/util/xmlutils.c index 6456ccd0e..b841ff96b 100644 --- a/util/xmlutils.c +++ b/util/xmlutils.c @@ -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; } }