summaryrefslogtreecommitdiff
path: root/nDPId.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-07-07 23:59:14 +0200
committerToni Uhlig <matzeton@googlemail.com>2020-07-07 23:59:14 +0200
commit1fe7832b7a04fe32cd30ebd97c60f6b2a01fb50f (patch)
treeb4fb7ea53971be354a0fed9d85b0f27647f56b25 /nDPId.c
parentf700457d9b3debc738559e5b656eb8c9b9b4cb33 (diff)
jsonize_basic_event // jsonize_basic_event_error
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'nDPId.c')
-rw-r--r--nDPId.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/nDPId.c b/nDPId.c
index 2baf8e599..567708b65 100644
--- a/nDPId.c
+++ b/nDPId.c
@@ -10,6 +10,7 @@
#include <pcap/pcap.h>
#include <pthread.h>
#include <signal.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
@@ -663,6 +664,116 @@ static void jsonize_flow_event(struct nDPId_reader_thread * const reader_thread,
}
ndpi_reset_serializer(&workflow->ndpi_serializer);
}
+
+static void jsonize_basic_event_error(struct nDPId_reader_thread * const reader_thread, uint32_t format_index)
+{
+ char * out;
+ uint32_t out_size = 0;
+
+ ndpi_serialize_string_string(&reader_thread->workflow->ndpi_serializer,
+ "serializer-error", "format");
+ ndpi_serialize_string_uint32(&reader_thread->workflow->ndpi_serializer,
+ "serializer-format-index", format_index);
+ out = ndpi_serializer_get_buffer(&reader_thread->workflow->ndpi_serializer, &out_size);
+ if (out != NULL && out_size > 0) {
+ printf("ERR: %s\n", out);
+ }
+ ndpi_reset_serializer(&reader_thread->workflow->ndpi_serializer);
+}
+
+static void jsonize_basic_event(struct nDPId_reader_thread * const reader_thread,
+ char const * format, ...)
+{
+ va_list ap;
+ uint8_t got_jsonkey = 0;
+ char json_key[BUFSIZ];
+ uint32_t format_index = 0;
+
+ (void)reader_thread;
+ va_start(ap, format);
+ while (*format) {
+ switch (*format++) {
+ case 's': {
+ format_index++;
+ char * value = va_arg(ap, char *);
+ if (got_jsonkey == 0) {
+ snprintf(json_key, sizeof(json_key), "%s", value);
+ got_jsonkey = 1;
+ } else {
+ ndpi_serialize_string_string(&reader_thread->workflow->ndpi_serializer,
+ json_key, value);
+ got_jsonkey = 0;
+ }
+ break;
+ }
+ case 'f': {
+ format_index++;
+ if (got_jsonkey == 1) {
+ float value = va_arg(ap, double);
+ ndpi_serialize_string_float(&reader_thread->workflow->ndpi_serializer,
+ json_key, value, "%.2f");
+ got_jsonkey = 0;
+ } else {
+ jsonize_basic_event_error(reader_thread, format_index);
+ return;
+ }
+ break;
+ }
+ case 'l':
+ format_index++;
+ if (got_jsonkey != 1) {
+ jsonize_basic_event_error(reader_thread, format_index);
+ return;
+ }
+ if (*(format++) == 'd') {
+ long long int value = va_arg(ap, long long int);
+ ndpi_serialize_string_int64(&reader_thread->workflow->ndpi_serializer,
+ json_key, value);
+ got_jsonkey = 0;
+ } else if (*(format++) == 'u') {
+ unsigned long long int value = va_arg(ap, unsigned long long int);
+ ndpi_serialize_string_uint64(&reader_thread->workflow->ndpi_serializer,
+ json_key, value);
+ got_jsonkey = 0;
+ } else {
+ jsonize_basic_event_error(reader_thread, format_index);
+ return;
+ }
+ break;
+ case 'u':
+ format_index++;
+ if (got_jsonkey == 1) {
+ unsigned int value = va_arg(ap, unsigned int);
+ ndpi_serialize_string_uint32(&reader_thread->workflow->ndpi_serializer,
+ json_key, value);
+ got_jsonkey = 0;
+ } else {
+ jsonize_basic_event_error(reader_thread, format_index);
+ return;
+ }
+ break;
+ case 'd':
+ format_index++;
+ if (got_jsonkey == 1) {
+ int value = va_arg(ap, int);
+ ndpi_serialize_string_int32(&reader_thread->workflow->ndpi_serializer,
+ json_key, value);
+ got_jsonkey = 0;
+ } else {
+ jsonize_basic_event_error(reader_thread, format_index);
+ return;
+ }
+ break;
+ case '%':
+ format_index++;
+ break;
+ default:
+ jsonize_basic_event_error(reader_thread, format_index);
+ return;
+ }
+ }
+ va_end(ap);
+}
#endif
static void ndpi_process_packet(uint8_t * const args,