Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnicola committed Oct 9, 2024
1 parent a035b05 commit d1fa858
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
63 changes: 33 additions & 30 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/multi.h>
#include <gnutls/gnutls.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -516,29 +515,34 @@ openvasd_get_version (openvasd_connector_t *conn)
g_free (resp.ptr);
return response;

Check warning on line 516 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L515-L516

Added lines #L515 - L516 were not covered by tests
}

struct curl_handlers
{
CURLM *mhnd;
CURL *hnd;
};

/**
* @brief Wrapps a CURLM * handler
*/
static curl_handler_t *
curlm_handler_new (void)
curlm_t
openvasd_curlm_handler_new (void)

Check warning on line 522 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L522

Added line #L522 was not covered by tests
{
curl_handler_t *handlers = g_malloc0 (sizeof (curl_handler_t));
return handlers;
CURLM *h = NULL;
return h;

Check warning on line 525 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L524-L525

Added lines #L524 - L525 were not covered by tests
}

void
openvasd_curl_handler_close (curl_handler_t *h)
openvasd_curl_handler_close (curlm_t *h)

Check warning on line 529 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L529

Added line #L529 was not covered by tests
{
curl_multi_remove_handle (h->mhnd, h->hnd);
curl_easy_cleanup (h->hnd);
curl_multi_cleanup (h->mhnd);
int queued = 0;

Check warning on line 531 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L531

Added line #L531 was not covered by tests

/* when an easy handle has completed, remove it */
CURLMsg *msg = curl_multi_info_read (h, &queued);

Check warning on line 534 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L534

Added line #L534 was not covered by tests
if (msg)
{
if (msg->msg == CURLMSG_DONE)
{
curl_multi_remove_handle (h, msg->easy_handle);
curl_easy_cleanup (msg->easy_handle);
curl_multi_cleanup (h);
return;

Check warning on line 542 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L539-L542

Added lines #L539 - L542 were not covered by tests
}
g_warning ("%s: Not possible to clean up the curl handler", __func__);

Check warning on line 544 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L544

Added line #L544 was not covered by tests
}
}

/**
Expand All @@ -552,15 +556,14 @@ openvasd_curl_handler_close (curl_handler_t *h)
* @return The response. Null on error.
*/
openvasd_resp_t
openvasd_get_vts_stream_init (openvasd_connector_t *conn, curl_handler_t **h,
openvasd_get_vts_stream_init (openvasd_connector_t *conn, curlm_t *mhnd,

Check warning on line 559 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L559

Added line #L559 was not covered by tests
stringstream *resp)
{
GString *path;
openvasd_resp_t response = NULL;
char *err = NULL;
CURL *hnd = NULL;

*h = curlm_handler_new ();
CURLM *h = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));

Check warning on line 567 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L563-L567

Added lines #L563 - L567 were not covered by tests
if (response == NULL)
return NULL;

Check warning on line 569 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L569

Added line #L569 was not covered by tests
Expand All @@ -576,9 +579,9 @@ openvasd_get_vts_stream_init (openvasd_connector_t *conn, curl_handler_t **h,
}
g_string_free (path, TRUE);

Check warning on line 580 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L580

Added line #L580 was not covered by tests

(*h)->mhnd = curl_multi_init ();
curl_multi_add_handle ((*h)->mhnd, hnd);
(*h)->hnd = hnd;
h = curl_multi_init ();
curl_multi_add_handle (h, hnd);
*mhnd = h;

Check warning on line 584 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L582-L584

Added lines #L582 - L584 were not covered by tests

response->code = RESP_CODE_OK;
return response;

Check warning on line 587 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L586-L587

Added lines #L586 - L587 were not covered by tests
Expand All @@ -596,19 +599,19 @@ openvasd_get_vts_stream_init (openvasd_connector_t *conn, curl_handler_t **h,
* transmision finished. -1 on error
*/
int
openvasd_get_vts_stream (curl_handler_t *h)
openvasd_get_vts_stream (curlm_t mhnd)

Check warning on line 602 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L602

Added line #L602 was not covered by tests
{
static int running = 0;

if (!(h->mhnd))
CURLM *h = mhnd;

Check warning on line 605 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L605

Added line #L605 was not covered by tests
if (!(h))
{
return -1;

Check warning on line 608 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L608

Added line #L608 was not covered by tests
}

CURLMcode mc = curl_multi_perform (h->mhnd, &running);
CURLMcode mc = curl_multi_perform (h, &running);

Check warning on line 611 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L611

Added line #L611 was not covered by tests
if (!mc && running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll (h->mhnd, NULL, 0, 5000, NULL);
mc = curl_multi_poll (h, NULL, 0, 5000, NULL);

Check warning on line 614 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L614

Added line #L614 was not covered by tests
if (mc != CURLM_OK)
{
g_warning ("%s: error on curl_multi_poll(): %d\n", __func__, mc);
Expand Down Expand Up @@ -1000,12 +1003,12 @@ openvasd_parsed_results (openvasd_connector_t *conn, unsigned long first,
const char *err = NULL;
openvasd_resp_t resp = NULL;
openvasd_result_t result = NULL;
unsigned long id;
unsigned long id = 0;
gchar *type = NULL;
gchar *ip_address = NULL;
gchar *hostname = NULL;
gchar *oid = NULL;
int port;
int port = 0;
gchar *protocol = NULL;
gchar *message = NULL;
gchar *detail_name = NULL;
Expand Down Expand Up @@ -1701,7 +1704,7 @@ openvasd_parsed_scans_preferences (openvasd_connector_t *conn, GSList **params)

cJSON_ArrayForEach (param_obj, parser)

Check warning on line 1705 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1705

Added line #L1705 was not covered by tests
{
const char *id, *name, *desc;
const char *id = NULL, *name = NULL, *desc = NULL;
char *defval = NULL, *param_type = NULL;
openvasd_param_t *param = NULL;
int val, mandatory = 0;

Check warning on line 1710 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1707-L1710

Added lines #L1707 - L1710 were not covered by tests
Expand Down
16 changes: 9 additions & 7 deletions openvasd/openvasd.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ openvasd_build_scan_config_json (openvasd_target_t *, GHashTable *, GSList *);

/* Curl multiperform wrapper */

typedef struct curl_handlers curl_handler_t;

void
openvasd_curl_handler_close (curl_handler_t *);
typedef void *curlm_t;

/** @brief Define a string struct for storing the response.
*/
Expand All @@ -288,11 +285,16 @@ typedef struct string
void
init_stringstream (stringstream *s);

curlm_t
openvasd_curlm_handler_new (void);

void
openvasd_curl_handler_close (curlm_t *);

openvasd_resp_t
openvasd_get_vts_stream_init (openvasd_connector_t *, curl_handler_t **,
openvasd_get_vts_stream_init (openvasd_connector_t *, curlm_t *,
stringstream *);

int
openvasd_get_vts_stream (curl_handler_t *);
int openvasd_get_vts_stream (curlm_t);

#endif
6 changes: 4 additions & 2 deletions openvasd/vtparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -323,17 +323,19 @@ openvasd_parse_vt (gvm_json_pull_parser_t *parser, gvm_json_pull_event_t *event)
g_debug ("%s: Start parsing feed", __func__);

Check warning on line 323 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L322-L323

Added lines #L322 - L323 were not covered by tests
}
else if (!g_strcmp0 (path, "$")
&& event->type == GVM_JSON_PULL_EVENT_ARRAY_END)
&& (event->type == GVM_JSON_PULL_EVENT_ARRAY_END
|| event->type == GVM_JSON_PULL_EVENT_EOF))

Check warning on line 327 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L325-L327

Added lines #L325 - L327 were not covered by tests
{
g_debug ("%s: Finish parsing feed", __func__);
g_free (path);
return NULL;

Check warning on line 331 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L329-L331

Added lines #L329 - L331 were not covered by tests
}
g_free (path);

Check warning on line 333 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L333

Added line #L333 was not covered by tests

// It is an NVT object
if (event->type != GVM_JSON_PULL_EVENT_OBJECT_START)
{
g_message ("%s: Error reading VT object", __func__);
g_warning ("%s: Error reading VT object", __func__);
return NULL;

Check warning on line 339 in openvasd/vtparser.c

View check run for this annotation

Codecov / codecov/patch

openvasd/vtparser.c#L338-L339

Added lines #L338 - L339 were not covered by tests
}

Expand Down
2 changes: 1 addition & 1 deletion util/jsonpull.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ gvm_json_pull_parser_next (gvm_json_pull_parser_t *parser,
return;
}
}

event->path = parser->path;

// Delayed addition to path after a container start element
Expand Down

0 comments on commit d1fa858

Please sign in to comment.