From 9b65afa8b0a504f606d9d8e5dfdc8fa3e904c547 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 6 Oct 2020 08:01:12 -0500 Subject: [PATCH 01/10] Add argument to select the position in the queue in which a new element is append --- util/kb.c | 8 +++++--- util/kb.h | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/util/kb.c b/util/kb.c index 38c9b4345..a556d750d 100644 --- a/util/kb.c +++ b/util/kb.c @@ -1112,11 +1112,13 @@ 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 +1137,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 +1147,7 @@ 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..8f99a4bd5 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,18 @@ 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); } /** From d869c23c8b3b216a624a3666284bef0e9130114b Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 6 Oct 2020 08:36:44 -0500 Subject: [PATCH 02/10] Make put_host_on_queue() a public function --- boreas/boreas_io.c | 2 +- boreas/boreas_io.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index 9c03deac8..a13fdf784 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 diff --git a/boreas/boreas_io.h b/boreas/boreas_io.h index 671bd6c29..cf4bc90f6 100644 --- a/boreas/boreas_io.h +++ b/boreas/boreas_io.h @@ -28,6 +28,9 @@ 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 *); From 19f4b744f62713b9822c5727d93a6d0b573d4895 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Tue, 6 Oct 2020 08:38:58 -0500 Subject: [PATCH 03/10] Add function to reallocate the finish flag at the of the queue. --- boreas/boreas_io.c | 21 +++++++++++++++++++++ boreas/boreas_io.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index a13fdf784..d84ed470c 100644 --- a/boreas/boreas_io.c +++ b/boreas/boreas_io.c @@ -238,6 +238,27 @@ put_host_on_queue (kb_t kb, char *addr_str) __func__, addr_str); } +/** + * @brief Reallocate finish signal at the end of 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; + + pos = 1; // Append the item at the end of the queue. + 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 cf4bc90f6..73c64fe54 100644 --- a/boreas/boreas_io.h +++ b/boreas/boreas_io.h @@ -34,6 +34,9 @@ put_host_on_queue (kb_t, char *); void put_finish_signal_on_queue (void *); +void +realloc_finish_signal_on_queue(kb_t); + void send_dead_hosts_to_ospd_openvas (int); From 004d37cbce8426adde9755bde908c6bab1d0e7ec Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 7 Oct 2020 03:46:22 -0500 Subject: [PATCH 04/10] Add function to check if the finish flag has been already set --- boreas/boreas_io.c | 27 ++++++++++++++++++++++++++- boreas/boreas_io.h | 3 +++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index d84ed470c..3a12589b1 100644 --- a/boreas/boreas_io.c +++ b/boreas/boreas_io.c @@ -238,13 +238,38 @@ put_host_on_queue (kb_t kb, char *addr_str) __func__, addr_str); } +/** + * @brief Checks if the finish signal is at the end of alive detection + * 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) + && (!strcmp (last_queue_item->v_str, ALIVE_DETECTION_FINISHED))) + ret = 1; + + kb_item_free (last_queue_item); + return ret; +} /** * @brief Reallocate finish signal at the end of alive detection queue. * * @param main_kb kb to use */ void -realloc_finish_signal_on_queue(kb_t main_kb) +realloc_finish_signal_on_queue (kb_t main_kb) { int kb_item_push_str_err, pos; diff --git a/boreas/boreas_io.h b/boreas/boreas_io.h index 73c64fe54..a48b8f232 100644 --- a/boreas/boreas_io.h +++ b/boreas/boreas_io.h @@ -37,6 +37,9 @@ 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); From e577438be689e759af93bb09b8c8f9d214d5f3ee Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 7 Oct 2020 04:09:59 -0500 Subject: [PATCH 05/10] Make gvm_host_free() a public function --- base/hosts.c | 2 +- base/hosts.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) 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..9b0ab1cd4 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -180,6 +180,9 @@ 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 * From 17447df358b65251e57878016b69506a2168c1b0 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 7 Oct 2020 09:58:56 -0500 Subject: [PATCH 06/10] Fix format --- base/hosts.h | 3 +-- boreas/boreas_io.c | 15 ++++++--------- boreas/boreas_io.h | 6 ++---- boreas/util.c | 1 - util/kb.c | 6 ++++-- util/kb.h | 3 ++- util/xmlutils.c | 9 +++++---- 7 files changed, 20 insertions(+), 23 deletions(-) diff --git a/base/hosts.h b/base/hosts.h index 9b0ab1cd4..dd6654282 100644 --- a/base/hosts.h +++ b/base/hosts.h @@ -180,8 +180,7 @@ 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); +void gvm_host_free (gpointer); /* Miscellaneous functions */ diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index 3a12589b1..287aefbd4 100644 --- a/boreas/boreas_io.c +++ b/boreas/boreas_io.c @@ -246,17 +246,16 @@ put_host_on_queue (kb_t kb, char *addr_str) * @return 1 if it is already set. 0 otherwise. */ int -finish_signal_on_queue(kb_t main_kb) +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); + 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) + if (last_queue_item && (last_queue_item->type == KB_TYPE_STR) && (!strcmp (last_queue_item->v_str, ALIVE_DETECTION_FINISHED))) ret = 1; @@ -274,10 +273,8 @@ realloc_finish_signal_on_queue (kb_t main_kb) int kb_item_push_str_err, pos; pos = 1; // Append the item at the end of the queue. - kb_item_push_str_err = kb_item_add_str_unique (main_kb, - ALIVE_DETECTION_QUEUE, - ALIVE_DETECTION_FINISHED, - 0, pos); + 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.", diff --git a/boreas/boreas_io.h b/boreas/boreas_io.h index a48b8f232..4163bbc69 100644 --- a/boreas/boreas_io.h +++ b/boreas/boreas_io.h @@ -34,11 +34,9 @@ put_host_on_queue (kb_t, char *); void put_finish_signal_on_queue (void *); -void -realloc_finish_signal_on_queue(kb_t); +void realloc_finish_signal_on_queue (kb_t); -int -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 a556d750d..b562b13f1 100644 --- a/util/kb.c +++ b/util/kb.c @@ -1118,7 +1118,8 @@ redis_del_items (kb_t kb, const char *name) * @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, int pos) +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; @@ -1147,7 +1148,8 @@ redis_add_str_unique (kb_t kb, const char *name, const char *str, size_t len, in else { redisAppendCommand (ctx, "LREM %s 1 %b", name, str, len); - redisAppendCommand (ctx, "%s %s %b", pos ? "LPUSH" : "RPUSH", 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 8f99a4bd5..34463eca3 100644 --- a/util/kb.h +++ b/util/kb.h @@ -463,7 +463,8 @@ kb_item_add_str (kb_t kb, const char *name, const char *str, size_t len) * @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, int pos) +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); 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; } } From 01f27046d83855ef0c2790fda14fc6c0de910851 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Wed, 7 Oct 2020 10:04:40 -0500 Subject: [PATCH 07/10] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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) From 102a039bea79dd7b79c739dd270bc53848ea23eb Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 8 Oct 2020 01:38:13 -0500 Subject: [PATCH 08/10] Use g_strcmp0 instead of strcmp. --- boreas/boreas_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index 287aefbd4..e9bf4ca4d 100644 --- a/boreas/boreas_io.c +++ b/boreas/boreas_io.c @@ -256,7 +256,7 @@ finish_signal_on_queue (kb_t main_kb) kb_item_get_single (main_kb, ALIVE_DETECTION_QUEUE, KB_TYPE_STR); if (last_queue_item && (last_queue_item->type == KB_TYPE_STR) - && (!strcmp (last_queue_item->v_str, ALIVE_DETECTION_FINISHED))) + && (!g_strcmp0 (last_queue_item->v_str, ALIVE_DETECTION_FINISHED))) ret = 1; kb_item_free (last_queue_item); From c043d3e0323e0cada6babdbeffaa08a0f7484f1e Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 8 Oct 2020 01:39:16 -0500 Subject: [PATCH 09/10] Improve docstring --- boreas/boreas_io.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/boreas/boreas_io.c b/boreas/boreas_io.c index e9bf4ca4d..5a3c81e55 100644 --- a/boreas/boreas_io.c +++ b/boreas/boreas_io.c @@ -239,8 +239,7 @@ put_host_on_queue (kb_t kb, char *addr_str) } /** - * @brief Checks if the finish signal is at the end of alive detection - * queue. + * @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. @@ -262,8 +261,10 @@ finish_signal_on_queue (kb_t main_kb) kb_item_free (last_queue_item); return ret; } + /** - * @brief Reallocate finish signal at the end of alive detection queue. + * @brief Reallocate finish signal in last position of the alive detection + * queue. * * @param main_kb kb to use */ @@ -272,7 +273,10 @@ realloc_finish_signal_on_queue (kb_t main_kb) { int kb_item_push_str_err, pos; - pos = 1; // Append the item at the end of the queue. + /* 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) From 206263c8521780e505efd14738ab918f67159340 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Thu, 8 Oct 2020 01:42:46 -0500 Subject: [PATCH 10/10] fix format 2 --- boreas/boreas_io.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/boreas/boreas_io.h b/boreas/boreas_io.h index 4163bbc69..b149683fc 100644 --- a/boreas/boreas_io.h +++ b/boreas/boreas_io.h @@ -34,9 +34,11 @@ put_host_on_queue (kb_t, char *); void put_finish_signal_on_queue (void *); -void realloc_finish_signal_on_queue (kb_t); +void +realloc_finish_signal_on_queue (kb_t); -int finish_signal_on_queue (kb_t); +int +finish_signal_on_queue (kb_t); void send_dead_hosts_to_ospd_openvas (int);