From 1b159ff163de728491f6daf57865e9bc4df03d12 Mon Sep 17 00:00:00 2001 From: Juan Jose Nicola Date: Fri, 8 Nov 2024 09:11:23 -0300 Subject: [PATCH] Apply suggested changes --- openvasd/openvasd.c | 70 +++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/openvasd/openvasd.c b/openvasd/openvasd.c index 5fac76e8..e129aa5d 100644 --- a/openvasd/openvasd.c +++ b/openvasd/openvasd.c @@ -152,20 +152,20 @@ openvasd_curlm_handler_new (void) * @param h Openvasd curl handler to clean */ static void -openvasd_curlm_handler_close (openvasd_curlm_t **h) +openvasd_curlm_handler_close (openvasd_curlm_t *h) { int queued = 0; /* when an easy handle has completed, remove it */ - CURLMsg *msg = curl_multi_info_read ((*h)->h, &queued); + CURLMsg *msg = curl_multi_info_read (h->h, &queued); if (msg) { if (msg->msg == CURLMSG_DONE) { - curl_multi_remove_handle ((*h)->h, msg->easy_handle); + curl_multi_remove_handle (h->h, msg->easy_handle); curl_easy_cleanup (msg->easy_handle); - curl_slist_free_all ((*h)->customheader); - curl_multi_cleanup ((*h)->h); + curl_slist_free_all (h->customheader); + curl_multi_cleanup (h->h); return; } g_warning ("%s: Not possible to clean up the curl handler", __func__); @@ -194,13 +194,16 @@ openvasd_vt_stream_new (void) * @param s The string struct to be freed */ static void -openvasd_vt_stream_cleanup (openvasd_vt_stream_t s) +openvasd_vt_stream_free (openvasd_vt_stream_t s) { - if (s) - g_free (s->ptr); + if (s == NULL) + return; + g_free (s->ptr); if (s->curl_hnd) - openvasd_curlm_handler_close (&(s->curl_hnd)); + openvasd_curlm_handler_close (s->curl_hnd); + + g_free (s); } /** @brief Reinitialize the string struct to hold the response @@ -312,7 +315,7 @@ openvasd_connector_free (openvasd_connector_t conn) g_free (conn->server); g_free (conn->host); g_free (conn->scan_id); - openvasd_vt_stream_cleanup (conn->stream_resp); + openvasd_vt_stream_free (conn->stream_resp); g_free (conn); conn = NULL; @@ -600,7 +603,8 @@ openvasd_get_version (openvasd_connector_t conn) response = g_malloc0 (sizeof (struct openvasd_response)); customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, HEAD, "/", NULL, customheader, &err)) == NULL) + hnd = handler (conn, HEAD, "/", NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); response->code = RESP_CODE_ERR; @@ -644,7 +648,8 @@ openvasd_get_vt_stream_init (openvasd_connector_t conn) path = g_string_new ("/vts?information=1"); customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, path->str, NULL, customheader, &err)) == NULL) + hnd = handler (conn, GET, path->str, NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -740,7 +745,8 @@ openvasd_get_vts (openvasd_connector_t conn) path = g_string_new ("/vts?information=1"); customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, path->str, NULL, customheader, &err)) == NULL) + hnd = handler (conn, GET, path->str, NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -780,7 +786,8 @@ openvasd_start_scan (openvasd_connector_t conn, gchar *data) response = g_malloc0 (sizeof (struct openvasd_response)); customheader = init_customheader (conn->apikey, TRUE); - if ((hnd = handler (conn, POST, "/scans", data, customheader, &err)) == NULL) + hnd = handler (conn, POST, "/scans", data, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); response->code = RESP_CODE_ERR; @@ -844,9 +851,9 @@ openvasd_start_scan (openvasd_connector_t conn, gchar *data) openvasd_reset_vt_stream (conn); customheader = init_customheader (conn->apikey, TRUE); - if ((hnd = handler (conn, POST, path->str, "{\"action\": \"start\"}", - customheader, &err)) - == NULL) + hnd = handler (conn, POST, path->str, "{\"action\": \"start\"}", customheader, + &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -901,9 +908,9 @@ openvasd_stop_scan (openvasd_connector_t conn) } customheader = init_customheader (conn->apikey, TRUE); - if ((hnd = handler (conn, POST, path->str, "{\"action\": \"stop\"}", - customheader, &err)) - == NULL) + hnd = handler (conn, POST, path->str, "{\"action\": \"stop\"}", customheader, + &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -955,7 +962,8 @@ openvasd_get_scan_results (openvasd_connector_t conn, long first, long last) } customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, path->str, NULL, customheader, &err)) == NULL) + hnd = handler (conn, GET, path->str, NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -1236,7 +1244,8 @@ openvasd_get_scan_status (openvasd_connector_t conn) } customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, path->str, NULL, customheader, &err)) == NULL) + hnd = handler (conn, GET, path->str, NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -1480,8 +1489,8 @@ openvasd_delete_scan (openvasd_connector_t conn) } customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, DELETE, path->str, NULL, customheader, &err)) - == NULL) + hnd = handler (conn, DELETE, path->str, NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); g_string_free (path, TRUE); @@ -1517,8 +1526,8 @@ openvasd_get_health_alive (openvasd_connector_t conn) response = g_malloc0 (sizeof (struct openvasd_response)); customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, "/health/alive", NULL, customheader, &err)) - == NULL) + hnd = handler (conn, GET, "/health/alive", NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); response->code = RESP_CODE_ERR; @@ -1551,10 +1560,9 @@ openvasd_get_health_ready (openvasd_connector_t conn) response = g_malloc0 (sizeof (struct openvasd_response)); - // openvasd_vt_stream_init (conn->vt_stream); customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, "/health/ready", NULL, customheader, &err)) - == NULL) + hnd = handler (conn, GET, "/health/ready", NULL, customheader, &err); + if (hnd == NULL) { response->code = RESP_CODE_ERR; response->body = err; @@ -1587,8 +1595,8 @@ openvasd_get_health_started (openvasd_connector_t conn) response = g_malloc0 (sizeof (struct openvasd_response)); customheader = init_customheader (conn->apikey, FALSE); - if ((hnd = handler (conn, GET, "/health/started", NULL, customheader, &err)) - == NULL) + hnd = handler (conn, GET, "/health/started", NULL, customheader, &err); + if (hnd == NULL) { curl_slist_free_all (customheader); response->code = RESP_CODE_ERR;