Simplify the plugin API
This commit is contained in:
parent
f9d6627456
commit
1f9f9b9a39
11
plugin-api.h
11
plugin-api.h
|
@ -47,22 +47,19 @@ struct service
|
||||||
/// Destroy the handle created for the scan
|
/// Destroy the handle created for the scan
|
||||||
void (*scan_free) (void *handle);
|
void (*scan_free) (void *handle);
|
||||||
|
|
||||||
// XXX: maybe force the service to store a reference to the unit?
|
|
||||||
|
|
||||||
/// We have received some data from the peer
|
/// We have received some data from the peer
|
||||||
// FIXME: the dependency on `struct str' is not very good
|
void (*on_data) (void *handle, const void *data, size_t len);
|
||||||
void (*on_data) (void *handle, struct unit *u, struct str *data);
|
|
||||||
|
|
||||||
/// Server has closed the connection
|
/// Server has closed the connection
|
||||||
void (*on_eof) (void *handle, struct unit *u);
|
void (*on_eof) (void *handle);
|
||||||
|
|
||||||
// XXX: do we need these at all? Is there any use for them?
|
// XXX: do we need these at all? Is there any use for them?
|
||||||
|
|
||||||
/// Network or other error has occured
|
/// Network or other error has occured
|
||||||
void (*on_error) (void *handle, struct unit *u);
|
void (*on_error) (void *handle);
|
||||||
|
|
||||||
/// The scan has been aborted
|
/// The scan has been aborted
|
||||||
void (*on_aborted) (void *handle, struct unit *u);
|
void (*on_aborted) (void *handle);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct plugin_api
|
struct plugin_api
|
||||||
|
|
|
@ -48,13 +48,13 @@ struct scan_data
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void
|
||||||
on_header_read (struct scan_data *data)
|
on_header_read (struct scan_data *scan)
|
||||||
{
|
{
|
||||||
if (!strcasecmp (data->field.str, "Server"))
|
if (!strcasecmp (scan->field.str, "Server"))
|
||||||
{
|
{
|
||||||
char *info = xstrdup_printf ("%s: %s",
|
char *info = xstrdup_printf ("%s: %s",
|
||||||
"server software", data->value.str);
|
"server software", scan->value.str);
|
||||||
g_data.api->unit_add_info (data->u, info);
|
g_data.api->unit_add_info (scan->u, info);
|
||||||
free (info);
|
free (info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,15 +62,15 @@ on_header_read (struct scan_data *data)
|
||||||
static int
|
static int
|
||||||
on_header_field (http_parser *parser, const char *at, size_t len)
|
on_header_field (http_parser *parser, const char *at, size_t len)
|
||||||
{
|
{
|
||||||
struct scan_data *data = parser->data;
|
struct scan_data *scan = parser->data;
|
||||||
if (data->state == STATE_VALUE)
|
if (scan->state == STATE_VALUE)
|
||||||
{
|
{
|
||||||
on_header_read (data);
|
on_header_read (scan);
|
||||||
str_reset (&data->field);
|
str_reset (&scan->field);
|
||||||
str_reset (&data->value);
|
str_reset (&scan->value);
|
||||||
}
|
}
|
||||||
str_append_data (&data->field, at, len);
|
str_append_data (&scan->field, at, len);
|
||||||
data->state = STATE_FIELD;
|
scan->state = STATE_FIELD;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,10 +86,10 @@ on_header_value (http_parser *parser, const char *at, size_t len)
|
||||||
static int
|
static int
|
||||||
on_headers_complete (http_parser *parser)
|
on_headers_complete (http_parser *parser)
|
||||||
{
|
{
|
||||||
struct scan_data *data = parser->data;
|
struct scan_data *scan = parser->data;
|
||||||
// We've got this far, this must be an HTTP server
|
// We've got this far, this must be an HTTP server
|
||||||
g_data.api->unit_set_success (data->u, true);
|
g_data.api->unit_set_success (scan->u, true);
|
||||||
g_data.api->unit_abort (data->u);
|
g_data.api->unit_abort (scan->u);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,7 +125,7 @@ scan_free (void *handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
on_data (void *handle, struct unit *u, struct str *data)
|
on_data (void *handle, const void *data, size_t len)
|
||||||
{
|
{
|
||||||
static const http_parser_settings http_settings =
|
static const http_parser_settings http_settings =
|
||||||
{
|
{
|
||||||
|
@ -136,26 +136,23 @@ on_data (void *handle, struct unit *u, struct str *data)
|
||||||
|
|
||||||
struct scan_data *scan = handle;
|
struct scan_data *scan = handle;
|
||||||
http_parser *parser = &scan->parser;
|
http_parser *parser = &scan->parser;
|
||||||
|
size_t n_parsed = http_parser_execute (parser, &http_settings, data, len);
|
||||||
size_t len = data ? data->len : 0;
|
|
||||||
const char *str = data ? data->str : NULL;
|
|
||||||
size_t n_parsed = http_parser_execute (parser, &http_settings, str, len);
|
|
||||||
|
|
||||||
if (parser->upgrade)
|
if (parser->upgrade)
|
||||||
{
|
{
|
||||||
// We should never get here though because `on_headers_complete'
|
// We should never get here though because `on_headers_complete'
|
||||||
// is called first and ends up aborting the unit.
|
// is called first and ends up aborting the unit.
|
||||||
g_data.api->unit_add_info (u, "upgrades to a different protocol");
|
g_data.api->unit_add_info (scan->u, "upgrades to a different protocol");
|
||||||
g_data.api->unit_abort (u);
|
g_data.api->unit_abort (scan->u);
|
||||||
}
|
}
|
||||||
else if (n_parsed != len && parser->http_errno != HPE_CB_headers_complete)
|
else if (n_parsed != len && parser->http_errno != HPE_CB_headers_complete)
|
||||||
g_data.api->unit_abort (u);
|
g_data.api->unit_abort (scan->u);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
on_eof (void *handle, struct unit *u)
|
on_eof (void *handle)
|
||||||
{
|
{
|
||||||
on_data (handle, u, NULL);
|
on_data (handle, NULL, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct service g_http_service =
|
static struct service g_http_service =
|
||||||
|
|
|
@ -279,12 +279,10 @@ on_irc_message (const struct irc_message *msg, const char *raw, void *user_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
on_data (void *handle, struct unit *u, struct str *data)
|
on_data (void *handle, const void *data, size_t len)
|
||||||
{
|
{
|
||||||
(void) u;
|
|
||||||
|
|
||||||
struct scan_data *scan = handle;
|
struct scan_data *scan = handle;
|
||||||
str_append_str (&scan->input, data);
|
str_append_data (&scan->input, data, len);
|
||||||
irc_process_buffer (&scan->input, on_irc_message, scan);
|
irc_process_buffer (&scan->input, on_irc_message, scan);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,16 +32,16 @@ g_data;
|
||||||
|
|
||||||
struct scan_data
|
struct scan_data
|
||||||
{
|
{
|
||||||
|
struct unit *u; ///< Scan unit
|
||||||
struct str input; ///< Input buffer
|
struct str input; ///< Input buffer
|
||||||
};
|
};
|
||||||
|
|
||||||
static void *
|
static void *
|
||||||
scan_init (struct unit *u)
|
scan_init (struct unit *u)
|
||||||
{
|
{
|
||||||
(void) u;
|
|
||||||
|
|
||||||
struct scan_data *scan = xcalloc (1, sizeof *scan);
|
struct scan_data *scan = xcalloc (1, sizeof *scan);
|
||||||
str_init (&scan->input);
|
str_init (&scan->input);
|
||||||
|
scan->u = u;
|
||||||
return scan;
|
return scan;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,14 +54,14 @@ scan_free (void *handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
on_data (void *handle, struct unit *u, struct str *data)
|
on_data (void *handle, const void *data, size_t len)
|
||||||
{
|
{
|
||||||
// See RFC 4253 -- we check for a valid SSH banner
|
// See RFC 4253 -- we check for a valid SSH banner
|
||||||
struct scan_data *scan = handle;
|
struct scan_data *scan = handle;
|
||||||
if (scan->input.len + data->len > 255)
|
if (scan->input.len + len > 255)
|
||||||
goto end_scan;
|
goto end_scan;
|
||||||
|
|
||||||
str_append_str (&scan->input, data);
|
str_append_data (&scan->input, data, len);
|
||||||
char *input = scan->input.str;
|
char *input = scan->input.str;
|
||||||
char *nl = strstr (input, "\r\n");
|
char *nl = strstr (input, "\r\n");
|
||||||
if (!nl)
|
if (!nl)
|
||||||
|
@ -71,11 +71,11 @@ on_data (void *handle, struct unit *u, struct str *data)
|
||||||
goto end_scan;
|
goto end_scan;
|
||||||
|
|
||||||
*nl = '\0';
|
*nl = '\0';
|
||||||
g_data.api->unit_add_info (u, input);
|
g_data.api->unit_add_info (scan->u, input);
|
||||||
g_data.api->unit_set_success (u, true);
|
g_data.api->unit_set_success (scan->u, true);
|
||||||
|
|
||||||
end_scan:
|
end_scan:
|
||||||
g_data.api->unit_abort (u);
|
g_data.api->unit_abort (scan->u);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct service g_ssh_service =
|
static struct service g_ssh_service =
|
||||||
|
|
|
@ -518,7 +518,7 @@ unit_abort (struct unit *u)
|
||||||
if (u->scan_started)
|
if (u->scan_started)
|
||||||
{
|
{
|
||||||
if (u->service->on_aborted)
|
if (u->service->on_aborted)
|
||||||
u->service->on_aborted (u->service_data, u);
|
u->service->on_aborted (u->service_data);
|
||||||
u->service->scan_free (u->service_data);
|
u->service->scan_free (u->service_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -581,7 +581,7 @@ on_unit_ready (const struct pollfd *pfd, struct unit *u)
|
||||||
if (u->read_buffer.len)
|
if (u->read_buffer.len)
|
||||||
{
|
{
|
||||||
struct str *buf = &u->read_buffer;
|
struct str *buf = &u->read_buffer;
|
||||||
service->on_data (u->service_data, u, buf);
|
service->on_data (u->service_data, buf->str, buf->len);
|
||||||
str_remove_slice (buf, 0, buf->len);
|
str_remove_slice (buf, 0, buf->len);
|
||||||
|
|
||||||
if (u->abortion_requested)
|
if (u->abortion_requested)
|
||||||
|
@ -595,7 +595,7 @@ on_unit_ready (const struct pollfd *pfd, struct unit *u)
|
||||||
if (got_eof)
|
if (got_eof)
|
||||||
{
|
{
|
||||||
if (service->on_eof)
|
if (service->on_eof)
|
||||||
service->on_eof (u->service_data, u);
|
service->on_eof (u->service_data);
|
||||||
if (u->abortion_requested || !u->write_buffer.len)
|
if (u->abortion_requested || !u->write_buffer.len)
|
||||||
goto abort;
|
goto abort;
|
||||||
}
|
}
|
||||||
|
@ -605,7 +605,7 @@ on_unit_ready (const struct pollfd *pfd, struct unit *u)
|
||||||
|
|
||||||
error:
|
error:
|
||||||
if (service->on_error)
|
if (service->on_error)
|
||||||
service->on_error (u->service_data, u);
|
service->on_error (u->service_data);
|
||||||
|
|
||||||
abort:
|
abort:
|
||||||
unit_abort (u);
|
unit_abort (u);
|
||||||
|
|
Loading…
Reference in New Issue