#include <stdio.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <string.h>
#include <errno.h>
struct sharkd_filter_item
{
- guint8 *filtered;
+ guint8 *filtered; /* can be NULL if all frames are matching for given filter. */
};
static GHashTable *filter_table = NULL;
-static gboolean
-json_unescape_str(char *input)
-{
- return wsjson_unescape_json_string(input, input);
-}
-
static const char *
json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
{
}
static void
-json_puts_string(const char *str)
+json_puts_string(const char *s)
{
- int i;
+ static const char json_cntrl[0x20][6] =
+ {
+ "u0000", "u0001", "u0002", "u0003", "u0004", "u0005", "u0006", "u0007", "b", "t", "n", "u000b", "f", "r", "u000e", "u000f",
+ "u0010", "u0011", "u0012", "u0013", "u0014", "u0015", "u0016", "u0017", "u0018", "u0019", "u001a", "u001b", "u001c", "u001d", "u001e", "u001f"
+ };
- if (str == NULL)
- str = "";
+ const unsigned char *str = (const unsigned char *) s ? s : "";
+ int i;
putchar('"');
for (i = 0; str[i]; i++)
{
- switch (str[i])
+ if (str[i] < 0x20)
{
- case '\\':
- case '"':
- putchar('\\');
- putchar(str[i]);
- break;
-
- case '\n':
+ putchar('\\');
+ fputs(json_cntrl[(guint)str[i]], stdout);
+ }
+ else
+ {
+ if (str[i] == '\\' || str[i] == '"')
putchar('\\');
- putchar('n');
- break;
-
- default:
- putchar(str[i]);
- break;
+ putchar(str[i]);
}
}
if (wrote > 0)
{
buf[wrote] = '\0';
- printf("%s", buf);
+ fputs(buf, stdout);
}
}
putchar('"');
}
+static void G_GNUC_PRINTF(3, 4)
+sharkd_json_value_anyf(gboolean sepa, const char *key, const char *valuef, ...)
+{
+ if (sepa)
+ putchar(',');
+
+ if (key)
+ {
+ putchar('"');
+ fputs(key, stdout);
+ putchar('"');
+ putchar(':');
+ }
+
+ if (valuef && *valuef)
+ {
+ va_list ap;
+
+ va_start(ap, valuef);
+ vprintf(valuef, ap);
+ va_end(ap);
+ }
+}
+
+static void
+sharkd_json_value_string(gboolean sepa, const char *key, const char *str)
+{
+ sharkd_json_value_anyf(sepa, key, NULL);
+ json_puts_string(str);
+}
+
+static void
+sharkd_json_value_base64(gboolean sepa, const char *key, const guint8 *data, size_t len)
+{
+ sharkd_json_value_anyf(sepa, key, NULL);
+ json_print_base64(data, len);
+}
+
+static void G_GNUC_PRINTF(3, 4)
+sharkd_json_value_stringf(gboolean sepa, const char *key, const char *format, ...)
+{
+ va_list ap;
+
+ sharkd_json_value_anyf(sepa, key, NULL);
+
+ va_start(ap, format);
+ putchar('"');
+ vprintf(format, ap);
+ putchar('"');
+ va_end(ap);
+}
+
+static void
+sharkd_json_array_open(gboolean sepa, const char *key)
+{
+ sharkd_json_value_anyf(sepa, key, "[");
+}
+
+static void
+sharkd_json_array_close(void)
+{
+ putchar(']');
+}
+
+static void
+sharkd_json_object_open(gboolean sepa)
+{
+ sharkd_json_value_anyf(sepa, NULL, "{");
+}
+
+static void
+sharkd_json_object_close(void)
+{
+ putchar('}');
+}
+
+static void
+sharkd_json_finish(void)
+{
+ putchar('\n');
+}
+
+static void
+sharkd_json_simple_reply(int err, const char *errmsg)
+{
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_anyf(FALSE, "err", "%d", err);
+ if (errmsg)
+ sharkd_json_value_string(TRUE, "errmsg", errmsg);
+
+ sharkd_json_object_close();
+ sharkd_json_finish();
+}
+
static void
sharkd_session_filter_free(gpointer data)
{
g_free(l);
}
-static const guint8 *
+static const struct sharkd_filter_item *
sharkd_session_filter_data(const char *filter)
{
struct sharkd_filter_item *l;
g_hash_table_insert(filter_table, g_strdup(filter), l);
}
- return l->filtered;
+ return l;
}
static gboolean
stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value;
int *pi = (int *) userdata;
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"%s\"", stat_tap->title);
- printf(",\"tap\":\"nstat:%s\"", (const char *) key);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_string(FALSE, "name", stat_tap->title);
+ sharkd_json_value_stringf(TRUE, "tap", "nstat:%s", (const char *) key);
+ sharkd_json_object_close();
*pi = *pi + 1;
return FALSE;
if (get_conversation_packet_func(table))
{
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"Conversation List/%s\"", label);
- printf(",\"tap\":\"conv:%s\"", label);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_stringf(FALSE, "name", "Conversation List/%s", label);
+ sharkd_json_value_stringf(TRUE, "tap", "conv:%s", label);
+ sharkd_json_object_close();
*pi = *pi + 1;
}
if (get_hostlist_packet_func(table))
{
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"Endpoint/%s\"", label);
- printf(",\"tap\":\"endpt:%s\"", label);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_stringf(FALSE, "name", "Endpoint/%s", label);
+ sharkd_json_value_stringf(TRUE, "tap", "endpt:%s", label);
+ sharkd_json_object_close();
*pi = *pi + 1;
}
register_analysis_t *analysis = (register_analysis_t *) value;
int *pi = (int *) userdata;
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"%s\"", sequence_analysis_get_ui_name(analysis));
- printf(",\"tap\":\"seqa:%s\"", (const char *) key);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_string(FALSE, "name", sequence_analysis_get_ui_name(analysis));
+ sharkd_json_value_stringf(TRUE, "tap", "seqa:%s", (const char *) key);
+ sharkd_json_object_close();
*pi = *pi + 1;
return FALSE;
const char *filter = proto_get_protocol_filter_name(proto_id);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"Export Object/%s\"", label);
- printf(",\"tap\":\"eo:%s\"", filter);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_stringf(FALSE, "name", "Export Object/%s", label);
+ sharkd_json_value_stringf(TRUE, "tap", "eo:%s", filter);
+ sharkd_json_object_close();
*pi = *pi + 1;
return FALSE;
const char *filter = proto_get_protocol_filter_name(proto_id);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"Service Response Time/%s\"", label);
- printf(",\"tap\":\"srt:%s\"", filter);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_stringf(FALSE, "name", "Service Response Time/%s", label);
+ sharkd_json_value_stringf(TRUE, "tap", "srt:%s", filter);
+ sharkd_json_object_close();
*pi = *pi + 1;
return FALSE;
const char *filter = proto_get_protocol_filter_name(proto_id);
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"Response Time Delay/%s\"", label);
- printf(",\"tap\":\"rtd:%s\"", filter);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_stringf(FALSE, "name", "Response Time Delay/%s", label);
+ sharkd_json_value_stringf(TRUE, "tap", "rtd:%s", filter);
+ sharkd_json_object_close();
*pi = *pi + 1;
return FALSE;
const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
- printf("%s{", (*pi) ? "," : "");
- printf("\"name\":\"Follow/%s\"", label);
- printf(",\"tap\":\"follow:%s\"", filter);
- printf("}");
+ sharkd_json_object_open(*pi != 0);
+ sharkd_json_value_stringf(FALSE, "name", "Follow/%s", label);
+ sharkd_json_value_stringf(TRUE, "tap", "follow:%s", filter);
+ sharkd_json_object_close();
*pi = *pi + 1;
return FALSE;
* Process info request
*
* Output object with attributes:
+ * (m) version - version number
+ *
* (m) columns - available column formats, array of object with attributes:
* 'name' - column name
* 'format' - column format-name
* 'name' - sequence analysis name
* 'tap' - sharkd tap-name
*
- * (m) taps - available taps, array of object with attributes:
+ * (m) taps - available taps, array of object with attributes:
* 'name' - tap name
* 'tap' - sharkd tap-name
*
- * (m) follow - available followers, array of object with attributes:
+ * (m) follow - available followers, array of object with attributes:
+ * 'name' - tap name
+ * 'tap' - sharkd tap-name
+ *
+ * (m) ftypes - conversation table for FT_ number to string, array of FT_xxx strings.
+ *
+ * (m) nstat - available table-based taps, array of object with attributes:
* 'name' - tap name
* 'tap' - sharkd tap-name
*
- * (m) ftypes - conversation table for FT_ number to string
*/
static void
sharkd_session_process_info(void)
{
int i;
- printf("{\"columns\":[");
+ sharkd_json_object_open(FALSE);
+
+ sharkd_json_array_open(FALSE, "columns");
for (i = 0; i < NUM_COL_FMTS; i++)
{
const char *col_format = col_format_to_string(i);
const char *col_descr = col_format_desc(i);
- printf("%s{", (i) ? "," : "");
- printf("\"name\":\"%s\"", col_descr);
- printf(",\"format\":\"%s\"", col_format);
- printf("}");
+ sharkd_json_object_open(i != 0);
+ sharkd_json_value_string(FALSE, "name", col_descr);
+ sharkd_json_value_string(TRUE, "format", col_format);
+ sharkd_json_object_close();
}
- printf("]");
+ sharkd_json_array_close();
- printf(",\"stats\":[");
+ sharkd_json_array_open(TRUE, "stats");
{
GList *cfg_list = stats_tree_get_cfg_list();
GList *l;
- const char *sepa = "";
+ gboolean sepa = FALSE;
for (l = cfg_list; l; l = l->next)
{
stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
- printf("%s{", sepa);
- printf("\"name\":\"%s\"", cfg->name);
- printf(",\"tap\":\"stat:%s\"", cfg->abbr);
- printf("}");
- sepa = ",";
+ sharkd_json_object_open(sepa);
+ sharkd_json_value_string(FALSE, "name", cfg->name);
+ sharkd_json_value_stringf(TRUE, "tap", "stat:%s", cfg->abbr);
+ sharkd_json_object_close();
+ sepa = TRUE;
}
g_list_free(cfg_list);
}
- printf("]");
+ sharkd_json_array_close();
- printf(",\"ftypes\":[");
+ sharkd_json_array_open(TRUE, "ftypes");
for (i = 0; i < FT_NUM_TYPES; i++)
- {
- if (i)
- printf(",");
- json_puts_string(ftype_name((ftenum_t) i));
- }
- printf("]");
+ sharkd_json_value_string(i != 0, NULL, ftype_name((ftenum_t) i));
+ sharkd_json_array_close();
- printf(",\"version\":");
- json_puts_string(sharkd_version());
+ sharkd_json_value_string(TRUE, "version", sharkd_version());
- printf(",\"nstat\":[");
+ sharkd_json_array_open(TRUE, "nstat");
i = 0;
stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf(",\"convs\":[");
+ sharkd_json_array_open(TRUE, "convs");
i = 0;
conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf(",\"seqa\":[");
+ sharkd_json_array_open(TRUE, "seqa");
i = 0;
sequence_analysis_table_iterate_tables(sharkd_session_seq_analysis_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf(",\"taps\":[");
+ sharkd_json_array_open(TRUE, "taps");
{
- printf("{\"name\":\"%s\",\"tap\":\"%s\"}", "RTP streams", "rtp-streams");
- printf(",{\"name\":\"%s\",\"tap\":\"%s\"}", "Expert Information", "expert");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "name", "RTP streams");
+ sharkd_json_value_string(TRUE, "tap", "rtp-streams");
+ sharkd_json_object_close();
+
+ sharkd_json_object_open(TRUE);
+ sharkd_json_value_string(FALSE, "name", "Expert Information");
+ sharkd_json_value_string(TRUE, "tap", "expert");
+ sharkd_json_object_close();
}
- printf("]");
+ sharkd_json_array_close();
- printf(",\"eo\":[");
+ sharkd_json_array_open(TRUE, "eo");
i = 0;
eo_iterate_tables(sharkd_export_object_visit_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf(",\"srt\":[");
+ sharkd_json_array_open(TRUE, "srt");
i = 0;
srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf(",\"rtd\":[");
+ sharkd_json_array_open(TRUE, "rtd");
i = 0;
rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf(",\"follow\":[");
+ sharkd_json_array_open(TRUE, "follow");
i = 0;
follow_iterate_followers(sharkd_follower_visit_cb, &i);
- printf("]");
+ sharkd_json_array_close();
- printf("}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
/**
if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK)
{
- printf("{\"err\":%d}\n", err);
+ sharkd_json_simple_reply(err, NULL);
return;
}
}
ENDTRY;
- printf("{\"err\":%d}\n", err);
+ sharkd_json_simple_reply(err, NULL);
}
/**
static void
sharkd_session_process_status(void)
{
- printf("{\"frames\":%u", cfile.count);
+ sharkd_json_object_open(FALSE);
- printf(",\"duration\":%.9f", nstime_to_sec(&cfile.elapsed_time));
+ sharkd_json_value_anyf(FALSE, "frames", "%u", cfile.count);
+ sharkd_json_value_anyf(TRUE, "duration", "%.9f", nstime_to_sec(&cfile.elapsed_time));
if (cfile.filename)
{
char *name = g_path_get_basename(cfile.filename);
- printf(",\"filename\":");
- json_puts_string(name);
+ sharkd_json_value_string(TRUE, "filename", name);
g_free(name);
}
gint64 file_size = wtap_file_size(cfile.provider.wth, NULL);
if (file_size > 0)
- printf(",\"filesize\":%" G_GINT64_FORMAT, file_size);
+ sharkd_json_value_anyf(TRUE, "filesize", "%" G_GINT64_FORMAT, file_size);
}
- printf("}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
struct sharkd_analyse_data
if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id), NULL, NULL))
{
+ guint proto_count = g_hash_table_size(analyser->protocols_set);
+
g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id), GUINT_TO_POINTER(proto_id));
- if (g_hash_table_size(analyser->protocols_set) != 1)
- printf(",");
- json_puts_string(proto_get_protocol_filter_name(proto_id));
+ sharkd_json_value_string(proto_count != 0, NULL, proto_get_protocol_filter_name(proto_id));
}
}
}
analyser.last_time = NULL;
analyser.protocols_set = g_hash_table_new(NULL /* g_direct_hash() */, NULL /* g_direct_equal */);
- printf("{\"frames\":%u", cfile.count);
+ sharkd_json_object_open(FALSE);
+
+ sharkd_json_value_anyf(FALSE, "frames", "%u", cfile.count);
- printf(",\"protocols\":[");
+ sharkd_json_array_open(TRUE, "protocols");
for (framenum = 1; framenum <= cfile.count; framenum++)
- sharkd_dissect_request(framenum, (framenum != 1) ? 1 : 0, framenum - 1, &sharkd_session_process_analyse_cb, 0, 0, 0, &analyser);
- printf("]");
+ sharkd_dissect_request(framenum, (framenum != 1) ? 1 : 0, framenum - 1, &sharkd_session_process_analyse_cb, SHARKD_DISSECT_FLAG_NULL, &analyser);
+ sharkd_json_array_close();
if (analyser.first_time)
- printf(",\"first\":%.9f", nstime_to_sec(analyser.first_time));
+ sharkd_json_value_anyf(TRUE, "first", "%.9f", nstime_to_sec(analyser.first_time));
if (analyser.last_time)
- printf(",\"last\":%.9f", nstime_to_sec(analyser.last_time));
+ sharkd_json_value_anyf(TRUE, "last", "%.9f", nstime_to_sec(analyser.last_time));
- printf("}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
g_hash_table_destroy(analyser.protocols_set);
}
const guint8 *filter_data = NULL;
- const char *frame_sepa = "";
+ gboolean frame_sepa = FALSE;
int col;
guint32 framenum, prev_dis_num = 0;
if (tok_filter)
{
- filter_data = sharkd_session_filter_data(tok_filter);
- if (!filter_data)
+ const struct sharkd_filter_item *filter_item;
+
+ filter_item = sharkd_session_filter_data(tok_filter);
+ if (!filter_item)
return;
+ filter_data = filter_item->filtered;
}
skip = 0;
return;
}
- printf("[");
+ sharkd_json_array_open(FALSE, NULL);
for (framenum = 1; framenum <= cfile.count; framenum++)
{
frame_data *fdata;
break;
}
}
+
+ if (*tok_refs == '\0' && framenum >= next_ref_frame)
+ {
+ current_ref_frame = next_ref_frame;
+ next_ref_frame = G_MAXUINT32;
+ }
}
if (current_ref_frame)
fdata = sharkd_get_frame(framenum);
sharkd_dissect_columns(fdata, ref_frame, prev_dis_num, cinfo, (fdata->color_filter == NULL));
- printf("%s{\"c\":[", frame_sepa);
+ sharkd_json_object_open(frame_sepa);
+
+ sharkd_json_array_open(FALSE, "c");
for (col = 0; col < cinfo->num_cols; ++col)
{
const col_item_t *col_item = &cinfo->columns[col];
- if (col)
- printf(",");
-
- json_puts_string(col_item->col_data);
+ sharkd_json_value_string(col != 0, NULL, col_item->col_data);
}
- printf("],\"num\":%u", framenum);
+ sharkd_json_array_close();
- if (fdata->flags.has_user_comment || fdata->flags.has_phdr_comment)
+ sharkd_json_value_anyf(TRUE, "num", "%u", framenum);
+
+ if (fdata->has_user_comment || fdata->has_phdr_comment)
{
- if (!fdata->flags.has_user_comment || sharkd_get_user_comment(fdata) != NULL)
- printf(",\"ct\":true");
+ if (!fdata->has_user_comment || sharkd_get_user_comment(fdata) != NULL)
+ sharkd_json_value_anyf(TRUE, "ct", "true");
}
- if (fdata->flags.ignored)
- printf(",\"i\":true");
+ if (fdata->ignored)
+ sharkd_json_value_anyf(TRUE, "i", "true");
- if (fdata->flags.marked)
- printf(",\"m\":true");
+ if (fdata->marked)
+ sharkd_json_value_anyf(TRUE, "m", "true");
if (fdata->color_filter)
{
- printf(",\"bg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->bg_color));
- printf(",\"fg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->fg_color));
+ sharkd_json_value_stringf(TRUE, "bg", "%x", color_t_to_rgb(&fdata->color_filter->bg_color));
+ sharkd_json_value_stringf(TRUE, "fg", "%x", color_t_to_rgb(&fdata->color_filter->fg_color));
}
- printf("}");
- frame_sepa = ",";
+ sharkd_json_object_close();
+ frame_sepa = TRUE;
prev_dis_num = framenum;
if (limit && --limit == 0)
break;
}
- printf("]\n");
+ sharkd_json_array_close();
+ sharkd_json_finish();
if (cinfo != &cfile.cinfo)
col_cleanup(cinfo);
sharkd_session_process_tap_stats_node_cb(const stat_node *n)
{
stat_node *node;
- const char *sepa = "";
+ gboolean sepa = FALSE;
- printf("[");
+ sharkd_json_array_open(FALSE, NULL);
for (node = n->children; node; node = node->next)
{
+ sharkd_json_object_open(sepa);
+
/* code based on stats_tree_get_values_from_node() */
- printf("%s{\"name\":\"%s\"", sepa, node->name);
- printf(",\"count\":%d", node->counter);
+ sharkd_json_value_string(FALSE, "name", node->name);
+ sharkd_json_value_anyf(TRUE, "count", "%d", node->counter);
if (node->counter && ((node->st_flags & ST_FLG_AVERAGE) || node->rng))
{
- printf(",\"avg\":%.2f", ((float)node->total) / node->counter);
- printf(",\"min\":%d", node->minvalue);
- printf(",\"max\":%d", node->maxvalue);
+ switch(node->datatype)
+ {
+ case STAT_DT_INT:
+ sharkd_json_value_anyf(TRUE, "avg", "%.2f", ((float)node->total.int_total) / node->counter);
+ sharkd_json_value_anyf(TRUE, "min", "%d", node->minvalue.int_min);
+ sharkd_json_value_anyf(TRUE, "max", "%d", node->maxvalue.int_max);
+ break;
+ case STAT_DT_FLOAT:
+ sharkd_json_value_anyf(TRUE, "avg", "%.2f", node->total.float_total / node->counter);
+ sharkd_json_value_anyf(TRUE, "min", "%f", node->minvalue.float_min);
+ sharkd_json_value_anyf(TRUE, "max", "%f", node->maxvalue.float_max);
+ break;
+ }
}
if (node->st->elapsed)
- printf(",\"rate\":%.4f",((float)node->counter) / node->st->elapsed);
+ sharkd_json_value_anyf(TRUE, "rate", "%.4f", ((float)node->counter) / node->st->elapsed);
if (node->parent && node->parent->counter)
- printf(",\"perc\":%.2f", (node->counter * 100.0) / node->parent->counter);
+ sharkd_json_value_anyf(TRUE, "perc", "%.2f", (node->counter * 100.0) / node->parent->counter);
else if (node->parent == &(node->st->root))
- printf(",\"perc\":100");
+ sharkd_json_value_anyf(TRUE, "perc", "100");
if (prefs.st_enable_burstinfo && node->max_burst)
{
if (prefs.st_burst_showcount)
- printf(",\"burstcount\":%d", node->max_burst);
+ sharkd_json_value_anyf(TRUE, "burstcount", "%d", node->max_burst);
else
- printf(",\"burstrate\":%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
+ sharkd_json_value_anyf(TRUE, "burstrate", "%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
- printf(",\"bursttime\":%.3f", ((double)node->burst_time / 1000.0));
+ sharkd_json_value_anyf(TRUE, "bursttime", "%.3f", (node->burst_time / 1000.0));
}
if (node->children)
{
- printf(",\"sub\":");
+ sharkd_json_value_anyf(TRUE, "sub", NULL);
sharkd_session_process_tap_stats_node_cb(node);
}
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
- printf("]");
+ sharkd_json_array_close();
}
/**
{
stats_tree *st = (stats_tree *) psp;
- printf("{\"tap\":\"stats:%s\",\"type\":\"stats\"", st->cfg->abbr);
+ sharkd_json_object_open(FALSE);
- printf(",\"name\":\"%s\",\"stats\":", st->cfg->name);
+ sharkd_json_value_stringf(FALSE, "tap", "stats:%s", st->cfg->abbr);
+ sharkd_json_value_string(TRUE, "type", "stats");
+ sharkd_json_value_string(TRUE, "name", st->cfg->name);
+
+ sharkd_json_value_anyf(TRUE, "stats", NULL);
sharkd_session_process_tap_stats_node_cb(&st->root);
- printf("},");
+
+ sharkd_json_object_close();
+ putchar(',');
}
static void
{
struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
GSList *list;
- const char *sepa = "";
+ gboolean sepa = FALSE;
- printf("{\"tap\":\"%s\",\"type\":\"%s\"", "expert", "expert");
+ sharkd_json_object_open(FALSE);
- printf(",\"details\":[");
+ sharkd_json_value_string(FALSE, "tap", "expert");
+ sharkd_json_value_string(TRUE, "type", "expert");
+
+ sharkd_json_array_open(TRUE, "details");
for (list = etd->details; list; list = list->next)
{
expert_info_t *ei = (expert_info_t *) list->data;
const char *tmp;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
- printf("\"f\":%u,", ei->packet_num);
+ sharkd_json_value_anyf(FALSE, "f", "%u", ei->packet_num);
tmp = try_val_to_str(ei->severity, expert_severity_vals);
if (tmp)
- printf("\"s\":\"%s\",", tmp);
+ sharkd_json_value_string(TRUE, "s", tmp);
tmp = try_val_to_str(ei->group, expert_group_vals);
if (tmp)
- printf("\"g\":\"%s\",", tmp);
+ sharkd_json_value_string(TRUE, "g", tmp);
- printf("\"m\":");
- json_puts_string(ei->summary);
- printf(",");
+ sharkd_json_value_string(TRUE, "m", ei->summary);
if (ei->protocol)
- {
- printf("\"p\":");
- json_puts_string(ei->protocol);
- }
+ sharkd_json_value_string(TRUE, "p", ei->protocol);
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
- printf("]");
+ sharkd_json_array_close();
- printf("},");
+ sharkd_json_object_close();
+ putchar(',');
}
-static gboolean
+static tap_packet_status
sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pointer)
{
struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
expert_info_t *ei_copy;
if (ei == NULL)
- return FALSE;
+ return TAP_PACKET_DONT_REDRAW;
ei_copy = g_new(expert_info_t, 1);
/* Note: this is a shallow copy */
etd->details = g_slist_prepend(etd->details, ei_copy);
- return TRUE;
+ return TAP_PACKET_REDRAW;
}
static void
GList *flow_list;
guint i;
- const char *sepa = "";
+ gboolean sepa = FALSE;
sequence_analysis_get_nodes(graph_analysis);
- printf("{\"tap\":\"seqa:%s\",\"type\":\"%s\"", graph_analysis->name, "flow");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_stringf(FALSE, "tap", "seqa:%s", graph_analysis->name);
+ sharkd_json_value_string(TRUE, "type", "flow");
- printf(",\"nodes\":[");
+ sharkd_json_array_open(TRUE, "nodes");
for (i = 0; i < graph_analysis->num_nodes; i++)
{
char *addr_str;
- if (i)
- printf(",");
-
addr_str = address_to_display(NULL, &(graph_analysis->nodes[i]));
- json_puts_string(addr_str);
+ sharkd_json_value_string(i != 0, NULL, addr_str);
wmem_free(NULL, addr_str);
}
- printf("]");
-
- printf(",\"flows\":[");
+ sharkd_json_array_close();
+ sharkd_json_array_open(TRUE, "flows");
flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
while (flow_list)
{
if (!sai->display)
continue;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
- printf("\"t\":\"%s\"", sai->time_str);
- printf(",\"n\":[%u,%u]", sai->src_node, sai->dst_node);
- printf(",\"pn\":[%u,%u]", sai->port_src, sai->port_dst);
+ sharkd_json_value_string(FALSE, "t", sai->time_str);
+ sharkd_json_value_anyf(TRUE, "n", "[%u,%u]", sai->src_node, sai->dst_node);
+ sharkd_json_value_anyf(TRUE, "pn", "[%u,%u]", sai->port_src, sai->port_dst);
if (sai->comment)
- {
- printf(",\"c\":");
- json_puts_string(sai->comment);
- }
+ sharkd_json_value_string(TRUE, "c", sai->comment);
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
+ sharkd_json_array_close();
- printf("]");
-
- printf("},");
+ sharkd_json_object_close();
+ putchar(',');
}
static void
{
const mmdb_lookup_t *lookup = NULL;
gboolean with_geoip = FALSE;
+ char json_key[64];
if (addr->type == AT_IPv4)
{
if (lookup->country)
{
- printf(",\"geoip_country%s\":", suffix);
- json_puts_string(lookup->country);
+ snprintf(json_key, sizeof(json_key), "geoip_country%s", suffix);
+ sharkd_json_value_string(TRUE, json_key, lookup->country);
with_geoip = TRUE;
}
if (lookup->country_iso)
{
- printf(",\"geoip_country_iso%s\":", suffix);
- json_puts_string(lookup->country_iso);
+ snprintf(json_key, sizeof(json_key), "geoip_country_iso%s", suffix);
+ sharkd_json_value_string(TRUE, json_key, lookup->country_iso);
with_geoip = TRUE;
}
if (lookup->city)
{
- printf(",\"geoip_city%s\":", suffix);
- json_puts_string(lookup->city);
+ snprintf(json_key, sizeof(json_key), "geoip_city%s", suffix);
+ sharkd_json_value_string(TRUE, json_key, lookup->city);
with_geoip = TRUE;
}
if (lookup->as_org)
{
- printf(",\"geoip_as_org%s\":", suffix);
- json_puts_string(lookup->as_org);
+ snprintf(json_key, sizeof(json_key), "geoip_as_org%s", suffix);
+ sharkd_json_value_string(TRUE, json_key, lookup->as_org);
with_geoip = TRUE;
}
if (lookup->as_number > 0)
{
- printf(",\"geoip_as%s\":%u", suffix, lookup->as_number);
+ snprintf(json_key, sizeof(json_key), "geoip_as%s", suffix);
+ sharkd_json_value_anyf(TRUE, json_key, "%u", lookup->as_number);
with_geoip = TRUE;
}
if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0)
{
- printf(",\"geoip_lat%s\":%f", suffix, lookup->latitude);
+ snprintf(json_key, sizeof(json_key), "geoip_lat%s", suffix);
+ sharkd_json_value_anyf(TRUE, json_key, "%f", lookup->latitude);
with_geoip = TRUE;
}
if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0)
{
- printf(",\"geoip_lon%s\":%f", suffix, lookup->longitude);
+ snprintf(json_key, sizeof(json_key), "geoip_lon%s", suffix);
+ sharkd_json_value_anyf(TRUE, json_key, "%f", lookup->longitude);
with_geoip = TRUE;
}
g_free(rtp_req);
}
-static gboolean
+static tap_packet_status
sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pointer)
{
struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
rtp_req->packets = g_slist_append(rtp_req->packets, item);
}
- return TRUE;
+ return TAP_PACKET_REDRAW;
}
/**
const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
- const char *sepa = "";
+ gboolean sepa = FALSE;
GSList *l;
- printf("{\"tap\":\"%s\",\"type\":\"rtp-analyse\"", rtp_req->tap_name);
+ sharkd_json_object_open(FALSE);
- printf(",\"ssrc\":%u", rtp_req->id.ssrc);
+ sharkd_json_value_string(FALSE, "tap", rtp_req->tap_name);
+ sharkd_json_value_string(TRUE, "type", "rtp-analyse");
+ sharkd_json_value_anyf(TRUE, "ssrc", "%u", rtp_req->id.ssrc);
- printf(",\"max_delta\":%f", statinfo->max_delta);
- printf(",\"max_delta_nr\":%u", statinfo->max_nr);
- printf(",\"max_jitter\":%f", statinfo->max_jitter);
- printf(",\"mean_jitter\":%f", statinfo->mean_jitter);
- printf(",\"max_skew\":%f", statinfo->max_skew);
- printf(",\"total_nr\":%u", statinfo->total_nr);
- printf(",\"seq_err\":%u", statinfo->sequence);
- printf(",\"duration\":%f", statinfo->time - statinfo->start_time);
+ sharkd_json_value_anyf(TRUE, "max_delta", "%f", statinfo->max_delta);
+ sharkd_json_value_anyf(TRUE, "max_delta_nr", "%u", statinfo->max_nr);
+ sharkd_json_value_anyf(TRUE, "max_jitter", "%f", statinfo->max_jitter);
+ sharkd_json_value_anyf(TRUE, "mean_jitter", "%f", statinfo->mean_jitter);
+ sharkd_json_value_anyf(TRUE, "max_skew", "%f", statinfo->max_skew);
+ sharkd_json_value_anyf(TRUE, "total_nr", "%u", statinfo->total_nr);
+ sharkd_json_value_anyf(TRUE, "seq_err", "%u", statinfo->sequence);
+ sharkd_json_value_anyf(TRUE, "duration", "%f", statinfo->time - statinfo->start_time);
- printf(",\"items\":[");
+ sharkd_json_array_open(TRUE, "items");
for (l = rtp_req->packets; l; l = l->next)
{
struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
- printf("\"f\":%u", item->frame_num);
- printf(",\"o\":%.9f", item->arrive_offset);
- printf(",\"sn\":%u", item->sequence_num);
- printf(",\"d\":%.2f", item->delta);
- printf(",\"j\":%.2f", item->jitter);
- printf(",\"sk\":%.2f", item->skew);
- printf(",\"bw\":%.2f", item->bandwidth);
+ sharkd_json_value_anyf(FALSE, "f", "%u", item->frame_num);
+ sharkd_json_value_anyf(TRUE, "o", "%.9f", item->arrive_offset);
+ sharkd_json_value_anyf(TRUE, "sn", "%u", item->sequence_num);
+ sharkd_json_value_anyf(TRUE, "d", "%.2f", item->delta);
+ sharkd_json_value_anyf(TRUE, "j", "%.2f", item->jitter);
+ sharkd_json_value_anyf(TRUE, "sk", "%.2f", item->skew);
+ sharkd_json_value_anyf(TRUE, "bw", "%.2f", item->bandwidth);
if (item->pt == PT_CN)
- printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=13, RFC 3389)", RTP_TYPE_CN);
+ {
+ sharkd_json_value_string(TRUE, "s", "Comfort noise (PT=13, RFC 3389)");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_CN);
+ }
else if (item->pt == PT_CN_OLD)
- printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=19, reserved)", RTP_TYPE_CN);
+ {
+ sharkd_json_value_string(TRUE, "s", "Comfort noise (PT=19, reserved)");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_CN);
+ }
else if (item->flags & STAT_FLAG_WRONG_SEQ)
- printf(",\"s\":\"%s\",\"t\":%d", "Wrong sequence number", RTP_TYPE_ERROR);
+ {
+ sharkd_json_value_string(TRUE, "s", "Wrong sequence number");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_ERROR);
+ }
else if (item->flags & STAT_FLAG_DUP_PKT)
- printf(",\"s\":\"%s\",\"t\":%d", "Suspected duplicate (MAC address) only delta time calculated", RTP_TYPE_WARN);
+ {
+ sharkd_json_value_string(TRUE, "s", "Suspected duplicate (MAC address) only delta time calculated");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_WARN);
+ }
else if (item->flags & STAT_FLAG_REG_PT_CHANGE)
- printf(",\"s\":\"Payload changed to PT=%u%s\",\"t\":%d",
+ {
+ sharkd_json_value_stringf(TRUE, "s", "Payload changed to PT=%u%s",
item->pt,
- (item->flags & STAT_FLAG_PT_T_EVENT) ? " telephone/event" : "",
- RTP_TYPE_WARN);
+ (item->flags & STAT_FLAG_PT_T_EVENT) ? " telephone/event" : "");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_WARN);
+ }
else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP)
- printf(",\"s\":\"%s\",\"t\":%d", "Incorrect timestamp", RTP_TYPE_WARN);
+ {
+ sharkd_json_value_string(TRUE, "s", "Incorrect timestamp");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_WARN);
+ }
else if ((item->flags & STAT_FLAG_PT_CHANGE)
&& !(item->flags & STAT_FLAG_FIRST)
&& !(item->flags & STAT_FLAG_PT_CN)
&& (item->flags & STAT_FLAG_FOLLOW_PT_CN)
&& !(item->flags & STAT_FLAG_MARKER))
{
- printf(",\"s\":\"%s\",\"t\":%d", "Marker missing?", RTP_TYPE_WARN);
+ sharkd_json_value_string(TRUE, "s", "Marker missing?");
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_WARN);
}
else if (item->flags & STAT_FLAG_PT_T_EVENT)
- printf(",\"s\":\"PT=%u telephone/event\",\"t\":%d", item->pt, RTP_TYPE_PT_EVENT);
+ {
+ sharkd_json_value_stringf(TRUE, "s", "PT=%u telephone/event", item->pt);
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_PT_EVENT);
+ }
else if (item->flags & STAT_FLAG_MARKER)
- printf(",\"t\":%d", RTP_TYPE_WARN);
+ {
+ sharkd_json_value_anyf(TRUE, "t", "%d", RTP_TYPE_WARN);
+ }
if (item->marker)
- printf(",\"mark\":1");
+ sharkd_json_value_anyf(TRUE, "mark", "1");
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
- printf("]");
+ sharkd_json_array_close();
- printf("},");
+ sharkd_json_object_close();
+ putchar(',');
}
/**
* (m) type - tap output type
* (m) proto - protocol short name
* (o) filter - filter string
+ * (o) geoip - whether GeoIP information is available, boolean
*
* (o) convs - array of object with attributes:
* (m) saddr - source address
* (m) rxb - RX bytes
* (m) start - (relative) first packet time
* (m) stop - (relative) last packet time
+ * (o) filter - conversation filter
*
* (o) hosts - array of object with attributes:
* (m) host - host address
int with_geoip = 0;
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "tap", iu->type);
+
if (!strncmp(iu->type, "conv:", 5))
{
- printf("{\"tap\":\"%s\",\"type\":\"conv\"", iu->type);
- printf(",\"convs\":[");
+ sharkd_json_value_string(TRUE, "type", "conv");
+ sharkd_json_array_open(TRUE, "convs");
proto = iu->type + 5;
}
else if (!strncmp(iu->type, "endpt:", 6))
{
- printf("{\"tap\":\"%s\",\"type\":\"host\"", iu->type);
- printf(",\"hosts\":[");
+ sharkd_json_value_string(TRUE, "type", "host");
+ sharkd_json_array_open(TRUE, "hosts");
proto = iu->type + 6;
}
else
{
- printf("{\"tap\":\"%s\",\"type\":\"err\"", iu->type);
+ sharkd_json_value_string(TRUE, "type", "err");
proto = "";
}
char *src_port, *dst_port;
char *filter_str;
- printf("%s{", i ? "," : "");
+ sharkd_json_object_open(i != 0);
- printf("\"saddr\":\"%s\"", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
- printf(",\"daddr\":\"%s\"", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
+ sharkd_json_value_string(FALSE, "saddr", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
+ sharkd_json_value_string(TRUE, "daddr", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
if (proto_with_port)
{
- printf(",\"sport\":\"%s\"", (src_port = get_conversation_port(NULL, iui->src_port, iui->etype, iu->resolve_port)));
- printf(",\"dport\":\"%s\"", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->etype, iu->resolve_port)));
+ sharkd_json_value_string(TRUE, "sport", (src_port = get_conversation_port(NULL, iui->src_port, iui->etype, iu->resolve_port)));
+ sharkd_json_value_string(TRUE, "dport", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->etype, iu->resolve_port)));
wmem_free(NULL, src_port);
wmem_free(NULL, dst_port);
}
- printf(",\"rxf\":%" G_GUINT64_FORMAT, iui->rx_frames);
- printf(",\"rxb\":%" G_GUINT64_FORMAT, iui->rx_bytes);
+ sharkd_json_value_anyf(TRUE, "rxf", "%" G_GUINT64_FORMAT, iui->rx_frames);
+ sharkd_json_value_anyf(TRUE, "rxb", "%" G_GUINT64_FORMAT, iui->rx_bytes);
- printf(",\"txf\":%" G_GUINT64_FORMAT, iui->tx_frames);
- printf(",\"txb\":%" G_GUINT64_FORMAT, iui->tx_bytes);
+ sharkd_json_value_anyf(TRUE, "txf", "%" G_GUINT64_FORMAT, iui->tx_frames);
+ sharkd_json_value_anyf(TRUE, "txb", "%" G_GUINT64_FORMAT, iui->tx_bytes);
- printf(",\"start\":%.9f", nstime_to_sec(&iui->start_time));
- printf(",\"stop\":%.9f", nstime_to_sec(&iui->stop_time));
+ sharkd_json_value_anyf(TRUE, "start", "%.9f", nstime_to_sec(&iui->start_time));
+ sharkd_json_value_anyf(TRUE, "stop", "%.9f", nstime_to_sec(&iui->stop_time));
filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
if (filter_str)
{
- printf(",\"filter\":\"%s\"", filter_str);
+ sharkd_json_value_string(TRUE, "filter", filter_str);
g_free(filter_str);
}
if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
with_geoip = 1;
- printf("}");
+ sharkd_json_object_close();
}
}
else if (iu->hash.conv_array != NULL && !strncmp(iu->type, "endpt:", 6))
char *host_str, *port_str;
char *filter_str;
- printf("%s{", i ? "," : "");
+ sharkd_json_object_open(i != 0);
- printf("\"host\":\"%s\"", (host_str = get_conversation_address(NULL, &host->myaddress, iu->resolve_name)));
+ sharkd_json_value_string(FALSE, "host", (host_str = get_conversation_address(NULL, &host->myaddress, iu->resolve_name)));
if (proto_with_port)
{
- printf(",\"port\":\"%s\"", (port_str = get_conversation_port(NULL, host->port, host->etype, iu->resolve_port)));
+ sharkd_json_value_string(TRUE, "port", (port_str = get_conversation_port(NULL, host->port, host->etype, iu->resolve_port)));
wmem_free(NULL, port_str);
}
- printf(",\"rxf\":%" G_GUINT64_FORMAT, host->rx_frames);
- printf(",\"rxb\":%" G_GUINT64_FORMAT, host->rx_bytes);
+ sharkd_json_value_anyf(TRUE, "rxf", "%" G_GUINT64_FORMAT, host->rx_frames);
+ sharkd_json_value_anyf(TRUE, "rxb", "%" G_GUINT64_FORMAT, host->rx_bytes);
- printf(",\"txf\":%" G_GUINT64_FORMAT, host->tx_frames);
- printf(",\"txb\":%" G_GUINT64_FORMAT, host->tx_bytes);
+ sharkd_json_value_anyf(TRUE, "txf", "%" G_GUINT64_FORMAT, host->tx_frames);
+ sharkd_json_value_anyf(TRUE, "txb", "%" G_GUINT64_FORMAT, host->tx_bytes);
filter_str = get_hostlist_filter(host);
if (filter_str)
{
- printf(",\"filter\":\"%s\"", filter_str);
+ sharkd_json_value_string(TRUE, "filter", filter_str);
g_free(filter_str);
}
if (sharkd_session_geoip_addr(&(host->myaddress), ""))
with_geoip = 1;
- printf("}");
+ sharkd_json_object_close();
}
}
+ sharkd_json_array_close();
- printf("],\"proto\":\"%s\",\"geoip\":%s},", proto, with_geoip ? "true" : "false");
+ sharkd_json_value_string(TRUE, "proto", proto);
+ sharkd_json_value_anyf(TRUE, "geoip", with_geoip ? "true" : "false");
+
+ sharkd_json_object_close();
+ putchar(',');
}
static void
stat_data_t *stat_data = (stat_data_t *) arg;
guint i, j, k;
- printf("{\"tap\":\"nstat:%s\",\"type\":\"nstat\"", stat_data->stat_tap_data->cli_string);
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_stringf(FALSE, "tap", "nstat:%s", stat_data->stat_tap_data->cli_string);
+ sharkd_json_value_string(TRUE, "type", "nstat");
- printf(",\"fields\":[");
+ sharkd_json_array_open(TRUE, "fields");
for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
{
stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
- if (i)
- printf(",");
-
- printf("{");
-
- printf("\"c\":");
- json_puts_string(field->column_name);
-
- printf("}");
+ sharkd_json_object_open(i != 0);
+ sharkd_json_value_string(FALSE, "c", field->column_name);
+ sharkd_json_object_close();
}
- printf("]");
+ sharkd_json_array_close();
- printf(",\"tables\":[");
+ sharkd_json_array_open(TRUE, "tables");
for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
{
stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i);
- const char *sepa = "";
-
- if (i)
- printf(",");
+ gboolean sepa = FALSE;
- printf("{");
+ sharkd_json_object_open(i != 0);
- printf("\"t\":");
- printf("\"%s\"", table->title);
+ sharkd_json_value_string(FALSE, "t", table->title);
- printf(",\"i\":[");
+ sharkd_json_array_open(TRUE, "i");
for (j = 0; j < table->num_elements; j++)
{
stat_tap_table_item_type *field_data;
if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
continue;
- printf("%s[", sepa);
+ sharkd_json_array_open(sepa, NULL);
for (k = 0; k < table->num_fields; k++)
{
field_data = stat_tap_get_field_data(table, j, k);
- if (k)
- printf(",");
-
switch (field_data->type)
{
case TABLE_ITEM_UINT:
- printf("%u", field_data->value.uint_value);
+ sharkd_json_value_anyf(k != 0, NULL, "%u", field_data->value.uint_value);
break;
case TABLE_ITEM_INT:
- printf("%d", field_data->value.int_value);
+ sharkd_json_value_anyf(k != 0, NULL, "%d", field_data->value.int_value);
break;
case TABLE_ITEM_STRING:
- json_puts_string(field_data->value.string_value);
+ sharkd_json_value_string(k != 0, NULL, field_data->value.string_value);
break;
case TABLE_ITEM_FLOAT:
- printf("%f", field_data->value.float_value);
+ sharkd_json_value_anyf(k != 0, NULL, "%f", field_data->value.float_value);
break;
case TABLE_ITEM_ENUM:
- printf("%d", field_data->value.enum_value);
+ sharkd_json_value_anyf(k != 0, NULL, "%d", field_data->value.enum_value);
break;
case TABLE_ITEM_NONE:
- printf("null");
+ sharkd_json_value_anyf(k != 0, NULL, "null");
break;
}
}
- printf("]");
- sepa = ",";
+ sharkd_json_array_close();
+ sepa = TRUE;
}
- printf("]");
- printf("}");
+ sharkd_json_array_close();
+ sharkd_json_object_close();
}
+ sharkd_json_array_close();
- printf("]},");
+ sharkd_json_object_close();
+ putchar(',');
}
static void
* (for usage grep for: register_rtd_table)
*/
const value_string *vs = get_rtd_value_string(rtd);
- const char *sepa = "";
+ gboolean sepa = FALSE;
- printf("{\"tap\":\"rtd:%s\",\"type\":\"rtd\"", filter);
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_stringf(FALSE, "tap", "rtd:%s", filter);
+ sharkd_json_value_string(TRUE, "type", "rtd");
if (rtd_data->stat_table.num_rtds == 1)
{
const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
- printf(",\"open_req\":%u", ms->open_req_num);
- printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
- printf(",\"req_dup\":%u", ms->req_dup_num);
- printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
+ sharkd_json_value_anyf(TRUE, "open_req", "%u", ms->open_req_num);
+ sharkd_json_value_anyf(TRUE, "disc_rsp", "%u", ms->disc_rsp_num);
+ sharkd_json_value_anyf(TRUE, "req_dup", "%u", ms->req_dup_num);
+ sharkd_json_value_anyf(TRUE, "rsp_dup", "%u", ms->rsp_dup_num);
}
- printf(",\"stats\":[");
+ sharkd_json_array_open(TRUE, "stats");
for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
{
const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
if (ms->rtd[j].num == 0)
continue;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
if (rtd_data->stat_table.num_rtds == 1)
type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
else
type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
- printf("\"type\":");
- json_puts_string(type_str);
+ sharkd_json_value_string(FALSE, "type", type_str);
- printf(",\"num\":%u", ms->rtd[j].num);
- printf(",\"min\":%.9f", nstime_to_sec(&(ms->rtd[j].min)));
- printf(",\"max\":%.9f", nstime_to_sec(&(ms->rtd[j].max)));
- printf(",\"tot\":%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
- printf(",\"min_frame\":%u", ms->rtd[j].min_num);
- printf(",\"max_frame\":%u", ms->rtd[j].max_num);
+ sharkd_json_value_anyf(TRUE, "num", "%u", ms->rtd[j].num);
+ sharkd_json_value_anyf(TRUE, "min", "%.9f", nstime_to_sec(&(ms->rtd[j].min)));
+ sharkd_json_value_anyf(TRUE, "max", "%.9f", nstime_to_sec(&(ms->rtd[j].max)));
+ sharkd_json_value_anyf(TRUE, "tot", "%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
+ sharkd_json_value_anyf(TRUE, "min_frame", "%u", ms->rtd[j].min_num);
+ sharkd_json_value_anyf(TRUE, "max_frame", "%u", ms->rtd[j].max_num);
if (rtd_data->stat_table.num_rtds != 1)
{
/* like in tshark, display it on every row */
- printf(",\"open_req\":%u", ms->open_req_num);
- printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
- printf(",\"req_dup\":%u", ms->req_dup_num);
- printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
+ sharkd_json_value_anyf(TRUE, "open_req", "%u", ms->open_req_num);
+ sharkd_json_value_anyf(TRUE, "disc_rsp", "%u", ms->disc_rsp_num);
+ sharkd_json_value_anyf(TRUE, "req_dup", "%u", ms->req_dup_num);
+ sharkd_json_value_anyf(TRUE, "rsp_dup", "%u", ms->rsp_dup_num);
}
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
}
- printf("]},");
+ sharkd_json_array_close();
+
+ sharkd_json_object_close();
+ putchar(',');
}
static void
guint i;
- printf("{\"tap\":\"srt:%s\",\"type\":\"srt\"", filter);
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_stringf(FALSE, "tap", "srt:%s", filter);
+ sharkd_json_value_string(TRUE, "type", "srt");
- printf(",\"tables\":[");
+ sharkd_json_array_open(TRUE, "tables");
for (i = 0; i < srt_data->srt_array->len; i++)
{
/* SRT table */
srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i);
- const char *sepa = "";
+ gboolean sepa = FALSE;
int j;
- if (i)
- printf(",");
- printf("{");
+ sharkd_json_object_open(i != 0);
- printf("\"n\":");
if (rst->name)
- json_puts_string(rst->name);
+ sharkd_json_value_string(FALSE, "n", rst->name);
else if (rst->short_name)
- json_puts_string(rst->short_name);
+ sharkd_json_value_string(FALSE, "n", rst->short_name);
else
- printf("\"table%u\"", i);
+ sharkd_json_value_stringf(FALSE, "n", "table%u", i);
if (rst->filter_string)
- {
- printf(",\"f\":");
- json_puts_string(rst->filter_string);
- }
+ sharkd_json_value_string(TRUE, "f", rst->filter_string);
if (rst->proc_column_name)
- {
- printf(",\"c\":");
- json_puts_string(rst->proc_column_name);
- }
+ sharkd_json_value_string(TRUE, "c", rst->proc_column_name);
- printf(",\"r\":[");
+ sharkd_json_array_open(TRUE, "r");
for (j = 0; j < rst->num_procs; j++)
{
/* SRT row */
if (proc->stats.num == 0)
continue;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
- printf("\"n\":");
- json_puts_string(proc->procedure);
+ sharkd_json_value_string(FALSE, "n", proc->procedure);
if (rst->filter_string)
- printf(",\"idx\":%d", proc->proc_index);
+ sharkd_json_value_anyf(TRUE, "idx", "%d", proc->proc_index);
- printf(",\"num\":%u", proc->stats.num);
+ sharkd_json_value_anyf(TRUE, "num", "%u", proc->stats.num);
- printf(",\"min\":%.9f", nstime_to_sec(&proc->stats.min));
- printf(",\"max\":%.9f", nstime_to_sec(&proc->stats.max));
- printf(",\"tot\":%.9f", nstime_to_sec(&proc->stats.tot));
+ sharkd_json_value_anyf(TRUE, "min", "%.9f", nstime_to_sec(&proc->stats.min));
+ sharkd_json_value_anyf(TRUE, "max", "%.9f", nstime_to_sec(&proc->stats.max));
+ sharkd_json_value_anyf(TRUE, "tot", "%.9f", nstime_to_sec(&proc->stats.tot));
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
- printf("]}");
+ sharkd_json_array_close();
+
+ sharkd_json_object_close();
}
+ sharkd_json_array_close();
- printf("]},");
+ sharkd_json_object_close();
+ putchar(',');
}
static void
GSList *slist;
int i = 0;
- printf("{\"tap\":\"%s\",\"type\":\"eo\"", object_list->type);
- printf(",\"proto\":\"%s\"", object_list->proto);
- printf(",\"objects\":[");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "tap", object_list->type);
+ sharkd_json_value_string(TRUE, "type", "eo");
+
+ sharkd_json_value_string(TRUE, "proto", object_list->proto);
+ sharkd_json_array_open(TRUE, "objects");
for (slist = object_list->entries; slist; slist = slist->next)
{
const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
- printf("%s{", i ? "," : "");
+ sharkd_json_object_open(i != 0);
- printf("\"pkt\":%u", eo_entry->pkt_num);
+ sharkd_json_value_anyf(FALSE, "pkt", "%u", eo_entry->pkt_num);
if (eo_entry->hostname)
- {
- printf(",\"hostname\":");
- json_puts_string(eo_entry->hostname);
- }
+ sharkd_json_value_string(TRUE, "hostname", eo_entry->hostname);
if (eo_entry->content_type)
- {
- printf(",\"type\":");
- json_puts_string(eo_entry->content_type);
- }
+ sharkd_json_value_string(TRUE, "type", eo_entry->content_type);
if (eo_entry->filename)
- {
- printf(",\"filename\":");
- json_puts_string(eo_entry->filename);
- }
+ sharkd_json_value_string(TRUE, "filename", eo_entry->filename);
- printf(",\"_download\":\"%s_%d\"", object_list->type, i);
+ sharkd_json_value_stringf(TRUE, "_download", "%s_%d", object_list->type, i);
- printf(",\"len\":%" G_GINT64_FORMAT, eo_entry->payload_len);
+ sharkd_json_value_anyf(TRUE, "len", "%" G_GINT64_FORMAT, eo_entry->payload_len);
- printf("}");
+ sharkd_json_object_close();
i++;
}
+ sharkd_json_array_close();
- printf("]},");
+ sharkd_json_object_close();
+ putchar(',');
}
static void
rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
GList *listx;
- const char *sepa = "";
+ gboolean sepa = FALSE;
- printf("{\"tap\":\"%s\",\"type\":\"%s\"", "rtp-streams", "rtp-streams");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "tap", "rtp-streams");
+ sharkd_json_value_string(TRUE, "type", "rtp-streams");
- printf(",\"streams\":[");
+ sharkd_json_array_open(TRUE, "streams");
for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
{
- rtpstream_info_calc_t calc;
rtpstream_info_t *streaminfo = (rtpstream_info_t *) listx->data;
+ rtpstream_info_calc_t calc;
rtpstream_info_calculate(streaminfo, &calc);
- printf("%s{\"ssrc\":%u", sepa, calc.ssrc);
- printf(",\"payload\":\"%s\"", calc.all_payload_type_names);
+ sharkd_json_object_open(sepa);
- printf(",\"saddr\":\"%s\"", calc.src_addr_str);
- printf(",\"sport\":%u", calc.src_port);
+ sharkd_json_value_anyf(FALSE, "ssrc", "%u", calc.ssrc);
+ sharkd_json_value_string(TRUE, "payload", calc.all_payload_type_names);
- printf(",\"daddr\":\"%s\"", calc.dst_addr_str);
- printf(",\"dport\":%u", calc.dst_port);
+ sharkd_json_value_string(TRUE, "saddr", calc.src_addr_str);
+ sharkd_json_value_anyf(TRUE, "sport", "%u", calc.src_port);
+ sharkd_json_value_string(TRUE, "daddr", calc.dst_addr_str);
+ sharkd_json_value_anyf(TRUE, "dport", "%u", calc.dst_port);
- printf(",\"pkts\":%u", calc.packet_count);
+ sharkd_json_value_anyf(TRUE, "pkts", "%u", calc.packet_count);
- printf(",\"max_delta\":%f",calc.max_delta);
- printf(",\"max_jitter\":%f", calc.max_jitter);
- printf(",\"mean_jitter\":%f", calc.mean_jitter);
+ sharkd_json_value_anyf(TRUE, "max_delta", "%f",calc.max_delta);
+ sharkd_json_value_anyf(TRUE, "max_jitter", "%f", calc.max_jitter);
+ sharkd_json_value_anyf(TRUE, "mean_jitter", "%f", calc.mean_jitter);
- printf(",\"expectednr\":%u", calc.packet_expected);
- printf(",\"totalnr\":%u", calc.total_nr);
+ sharkd_json_value_anyf(TRUE, "expectednr", "%u", calc.packet_expected);
+ sharkd_json_value_anyf(TRUE, "totalnr", "%u", calc.total_nr);
- printf(",\"problem\":%s", calc.problem? "true" : "false");
+ sharkd_json_value_anyf(TRUE, "problem", calc.problem ? "true" : "false");
/* for filter */
- printf(",\"ipver\":%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4);
+ sharkd_json_value_anyf(TRUE, "ipver", "%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4);
rtpstream_info_calc_free(&calc);
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
- printf("]},");
+ sharkd_json_array_close();
+
+ sharkd_json_object_close();
+ putchar(',');
}
/**
st = stats_tree_new(cfg, NULL, tap_filter);
- tap_error = register_tap_listener(st->cfg->tapname, st, st->filter, st->cfg->flags, stats_tree_reset, stats_tree_packet, sharkd_session_process_tap_stats_cb);
+ tap_error = register_tap_listener(st->cfg->tapname, st, st->filter, st->cfg->flags, stats_tree_reset, stats_tree_packet, sharkd_session_process_tap_stats_cb, NULL);
if (!tap_error && cfg->init)
cfg->init(st);
expert_tap = g_new0(struct sharkd_expert_tap, 1);
expert_tap->text = g_string_chunk_new(100);
- tap_error = register_tap_listener("expert", expert_tap, NULL, 0, NULL, sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb);
+ tap_error = register_tap_listener("expert", expert_tap, NULL, 0, NULL, sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb, NULL);
tap_data = expert_tap;
tap_free = sharkd_session_free_tap_expert_cb;
tap_flags = sequence_analysis_get_tap_flags(analysis);
tap_func = sequence_analysis_get_packet_func(analysis);
- tap_error = register_tap_listener(tap_name, graph_analysis, NULL, tap_flags, NULL, tap_func, sharkd_session_process_tap_flow_cb);
+ tap_error = register_tap_listener(tap_name, graph_analysis, NULL, tap_flags, NULL, tap_func, sharkd_session_process_tap_flow_cb, NULL);
tap_data = graph_analysis;
tap_free = sharkd_session_free_tap_flow_cb;
ct_data->resolve_name = TRUE;
ct_data->resolve_port = TRUE;
- tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb);
+ tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb, NULL);
tap_data = &ct_data->hash;
tap_free = sharkd_session_free_tap_conv_cb;
stat_data->stat_tap_data = stat_tap;
stat_data->user_data = NULL;
- tap_error = register_tap_listener(stat_tap->tap_name, stat_data, tap_filter, 0, NULL, stat_tap->packet_func, sharkd_session_process_tap_nstat_cb);
+ tap_error = register_tap_listener(stat_tap->tap_name, stat_data, tap_filter, 0, NULL, stat_tap->packet_func, sharkd_session_process_tap_nstat_cb, NULL);
tap_data = stat_data;
tap_free = sharkd_session_free_tap_nstat_cb;
rtd_data->user_data = rtd;
rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL, NULL);
- tap_error = register_tap_listener(get_rtd_tap_listener_name(rtd), rtd_data, tap_filter, 0, NULL, get_rtd_packet_func(rtd), sharkd_session_process_tap_rtd_cb);
+ tap_error = register_tap_listener(get_rtd_tap_listener_name(rtd), rtd_data, tap_filter, 0, NULL, get_rtd_packet_func(rtd), sharkd_session_process_tap_rtd_cb, NULL);
tap_data = rtd_data;
tap_free = sharkd_session_free_tap_rtd_cb;
srt_data->user_data = srt;
srt_table_dissector_init(srt, srt_data->srt_array);
- tap_error = register_tap_listener(get_srt_tap_listener_name(srt), srt_data, tap_filter, 0, NULL, get_srt_packet_func(srt), sharkd_session_process_tap_srt_cb);
+ tap_error = register_tap_listener(get_srt_tap_listener_name(srt), srt_data, tap_filter, 0, NULL, get_srt_packet_func(srt), sharkd_session_process_tap_srt_cb, NULL);
tap_data = srt_data;
tap_free = sharkd_session_free_tap_srt_cb;
eo_object->get_entry = sharkd_eo_object_list_get_entry;
eo_object->gui_data = (void *) object_list;
- tap_error = register_tap_listener(get_eo_tap_listener_name(eo), eo_object, NULL, 0, NULL, get_eo_packet_func(eo), sharkd_session_process_tap_eo_cb);
+ tap_error = register_tap_listener(get_eo_tap_listener_name(eo), eo_object, NULL, 0, NULL, get_eo_packet_func(eo), sharkd_session_process_tap_eo_cb, NULL);
tap_data = eo_object;
tap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
}
else if (!strcmp(tok_tap, "rtp-streams"))
{
- tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet_cb, sharkd_session_process_tap_rtp_cb);
+ tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet_cb, sharkd_session_process_tap_rtp_cb, NULL);
tap_data = &rtp_tapinfo;
tap_free = rtpstream_reset_cb;
rtp_req->statinfo.first_packet = TRUE;
rtp_req->statinfo.reg_pt = PT_UNDEFINED;
- tap_error = register_tap_listener("rtp", rtp_req, tap_filter, 0, NULL, sharkd_session_packet_tap_rtp_analyse_cb, sharkd_session_process_tap_rtp_analyse_cb);
+ tap_error = register_tap_listener("rtp", rtp_req, tap_filter, 0, NULL, sharkd_session_packet_tap_rtp_analyse_cb, sharkd_session_process_tap_rtp_analyse_cb, NULL);
tap_data = rtp_req;
tap_free = sharkd_session_process_tap_rtp_free_cb;
if (taps_count == 0)
return;
- printf("{\"taps\":[");
+ sharkd_json_object_open(FALSE);
+
+ sharkd_json_array_open(FALSE, "taps");
sharkd_retap();
- printf("null],\"err\":0}\n");
+ // This dummy value exists to permit unconditionally adding ',' in the taps callback.
+ // XXX convert to json_dumper and remove this.
+ sharkd_json_value_anyf(FALSE, NULL, "null");
+ sharkd_json_array_close();
+
+ sharkd_json_value_anyf(TRUE, "err", "0");
+
+ sharkd_json_object_close();
+ sharkd_json_finish();
for (i = 0; i < taps_count; i++)
{
follow_info = g_new0(follow_info_t, 1);
/* gui_data, filter_out_filter not set, but not used by dissector */
- tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL, get_follow_tap_handler(follower), NULL);
+ tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL, get_follow_tap_handler(follower), NULL, NULL);
if (tap_error)
{
fprintf(stderr, "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str);
sharkd_retap();
- printf("{");
+ sharkd_json_object_open(FALSE);
- printf("\"err\":0");
+ sharkd_json_value_anyf(FALSE, "err", "0");
/* Server information: hostname, port, bytes sent */
host = address_to_name(&follow_info->server_ip);
- printf(",\"shost\":");
- json_puts_string(host);
+ sharkd_json_value_string(TRUE, "shost", host);
port = get_follow_port_to_display(follower)(NULL, follow_info->server_port);
- printf(",\"sport\":");
- json_puts_string(port);
+ sharkd_json_value_string(TRUE, "sport", port);
wmem_free(NULL, port);
- printf(",\"sbytes\":%u", follow_info->bytes_written[0]);
+ sharkd_json_value_anyf(TRUE, "sbytes", "%u", follow_info->bytes_written[0]);
/* Client information: hostname, port, bytes sent */
host = address_to_name(&follow_info->client_ip);
- printf(",\"chost\":");
- json_puts_string(host);
+ sharkd_json_value_string(TRUE, "chost", host);
port = get_follow_port_to_display(follower)(NULL, follow_info->client_port);
- printf(",\"cport\":");
- json_puts_string(port);
+ sharkd_json_value_string(TRUE, "cport", port);
wmem_free(NULL, port);
- printf(",\"cbytes\":%u", follow_info->bytes_written[1]);
+ sharkd_json_value_anyf(TRUE, "cbytes", "%u", follow_info->bytes_written[1]);
if (follow_info->payload)
{
follow_record_t *follow_record;
GList *cur;
- const char *sepa = "";
-
- printf(",\"payloads\":[");
+ gboolean sepa = FALSE;
- for (cur = follow_info->payload; cur; cur = g_list_next(cur))
+ sharkd_json_array_open(TRUE, "payloads");
+ for (cur = g_list_last(follow_info->payload); cur; cur = g_list_previous(cur))
{
follow_record = (follow_record_t *) cur->data;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
- printf("\"n\":%u", follow_record->packet_num);
-
- printf(",\"d\":");
- json_print_base64(follow_record->data->data, follow_record->data->len);
+ sharkd_json_value_anyf(FALSE, "n", "%u", follow_record->packet_num);
+ sharkd_json_value_base64(TRUE, "d", follow_record->data->data, follow_record->data->len);
if (follow_record->is_server)
- printf(",\"s\":%d", 1);
+ sharkd_json_value_anyf(TRUE, "s", "%d", 1);
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
-
- printf("]");
+ sharkd_json_array_close();
}
- printf("}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
remove_tap_listener(follow_info);
follow_info_free(follow_info);
}
static void
-sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs)
+sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs, gboolean display_hidden)
{
proto_node *node;
- const char *sepa = "";
+ gboolean sepa = FALSE;
- printf("[");
+ sharkd_json_array_open(FALSE, NULL);
for (node = tree->first_child; node; node = node->next)
{
field_info *finfo = PNODE_FINFO(node);
if (!finfo)
continue;
- /* XXX, for now always skip hidden */
- if (FI_GET_FLAG(finfo, FI_HIDDEN))
+ if (!display_hidden && FI_GET_FLAG(finfo, FI_HIDDEN))
continue;
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
- printf("\"l\":");
if (!finfo->rep)
{
char label_str[ITEM_LABEL_LENGTH];
label_str[0] = '\0';
proto_item_fill_label(finfo, label_str);
- json_puts_string(label_str);
+ sharkd_json_value_string(FALSE, "l", label_str);
}
else
{
- json_puts_string(finfo->rep->representation);
+ sharkd_json_value_string(FALSE, "l", finfo->rep->representation);
}
if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
{
if (tvbs[idx] == finfo->ds_tvb)
{
- printf(",\"ds\":%d", idx);
+ sharkd_json_value_anyf(TRUE, "ds", "%d", idx);
break;
}
}
}
if (finfo->start >= 0 && finfo->length > 0)
- printf(",\"h\":[%d,%d]", finfo->start, finfo->length);
+ sharkd_json_value_anyf(TRUE, "h", "[%d,%d]", finfo->start, finfo->length);
if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
- printf(",\"i\":[%d,%d]", finfo->appendix_start, finfo->appendix_length);
+ sharkd_json_value_anyf(TRUE, "i", "[%d,%d]", finfo->appendix_start, finfo->appendix_length);
if (finfo->hfinfo)
if (finfo->hfinfo->type == FT_PROTOCOL)
{
- printf(",\"t\":\"proto\"");
+ sharkd_json_value_string(TRUE, "t", "proto");
}
else if (finfo->hfinfo->type == FT_FRAMENUM)
{
- printf(",\"t\":\"framenum\",\"fnum\":%u", finfo->value.value.uinteger);
+ sharkd_json_value_string(TRUE, "t", "framenum");
+ sharkd_json_value_anyf(TRUE, "fnum", "%u", finfo->value.value.uinteger);
}
else if (FI_GET_FLAG(finfo, FI_URL) && IS_FT_STRING(finfo->hfinfo->type))
{
char *url = fvalue_to_string_repr(NULL, &finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
- printf(",\"t\":\"url\",\"url\":");
- json_puts_string(url);
+ sharkd_json_value_string(TRUE, "t", "url");
+ sharkd_json_value_string(TRUE, "url", url);
wmem_free(NULL, url);
}
filter = proto_construct_match_selected_string(finfo, edt);
if (filter)
{
- printf(",\"f\":");
- json_puts_string(filter);
+ sharkd_json_value_string(TRUE, "f", filter);
wmem_free(NULL, filter);
}
}
+ if (FI_GET_FLAG(finfo, FI_GENERATED))
+ sharkd_json_value_anyf(TRUE, "g", "true");
+
+ if (FI_GET_FLAG(finfo, FI_HIDDEN))
+ sharkd_json_value_anyf(TRUE, "v", "true");
+
if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
{
const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK), expert_severity_vals);
g_assert(severity != NULL);
- printf(",\"s\":\"%s\"", severity);
+ sharkd_json_value_string(TRUE, "s", severity);
}
if (((proto_tree *) node)->first_child)
{
if (finfo->tree_type != -1)
- printf(",\"e\":%d", finfo->tree_type);
- printf(",\"n\":");
- sharkd_session_process_frame_cb_tree(edt, (proto_tree *) node, tvbs);
+ sharkd_json_value_anyf(TRUE, "e", "%d", finfo->tree_type);
+
+ sharkd_json_value_anyf(TRUE, "n", NULL);
+ sharkd_session_process_frame_cb_tree(edt, (proto_tree *) node, tvbs, display_hidden);
}
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
- printf("]");
+ sharkd_json_array_close();
}
static gboolean
follow_filter = get_follow_conv_func(follower)(pi, &ignore_stream);
- printf(",[\"%s\",", layer_proto);
- json_puts_string(follow_filter);
- printf("]");
+ sharkd_json_array_open(TRUE, NULL);
+ sharkd_json_value_string(FALSE, NULL, layer_proto);
+ sharkd_json_value_string(TRUE, NULL, follow_filter);
+ sharkd_json_array_close();
g_free(follow_filter);
}
return FALSE;
}
+struct sharkd_frame_request_data
+{
+ gboolean display_hidden;
+};
+
static void
sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
{
frame_data *fdata = pi->fd;
const char *pkt_comment = NULL;
- (void) data;
+ const struct sharkd_frame_request_data * const req_data = (const struct sharkd_frame_request_data * const) data;
+ const gboolean display_hidden = (req_data) ? req_data->display_hidden : FALSE;
- printf("{");
+ sharkd_json_object_open(FALSE);
- printf("\"err\":0");
+ sharkd_json_value_anyf(FALSE, "err", "0");
- if (fdata->flags.has_user_comment)
+ if (fdata->has_user_comment)
pkt_comment = sharkd_get_user_comment(fdata);
- else if (fdata->flags.has_phdr_comment)
+ else if (fdata->has_phdr_comment)
pkt_comment = pi->rec->opt_comment;
if (pkt_comment)
- {
- printf(",\"comment\":");
- json_puts_string(pkt_comment);
- }
+ sharkd_json_value_string(TRUE, "comment", pkt_comment);
if (tree)
{
tvbuff_t **tvbs = NULL;
- printf(",\"tree\":");
-
/* arrayize data src, to speedup searching for ds_tvb index */
if (data_src && data_src->next /* only needed if there are more than one data source */)
{
tvbs[count] = NULL;
}
- sharkd_session_process_frame_cb_tree(edt, tree, tvbs);
+ sharkd_json_value_anyf(TRUE, "tree", NULL);
+ sharkd_session_process_frame_cb_tree(edt, tree, tvbs, display_hidden);
g_free(tvbs);
}
{
int col;
- printf(",\"col\":[");
+ sharkd_json_array_open(TRUE, "col");
for (col = 0; col < cinfo->num_cols; ++col)
{
const col_item_t *col_item = &cinfo->columns[col];
- printf("%s\"%s\"", (col) ? "," : "", col_item->col_data);
+ sharkd_json_value_string(col != 0, NULL, col_item->col_data);
}
- printf("]");
+ sharkd_json_array_close();
+ }
+
+ if (fdata->ignored)
+ sharkd_json_value_anyf(TRUE, "i", "true");
+
+ if (fdata->marked)
+ sharkd_json_value_anyf(TRUE, "m", "true");
+
+ if (fdata->color_filter)
+ {
+ sharkd_json_value_stringf(TRUE, "bg", "%x", color_t_to_rgb(&fdata->color_filter->bg_color));
+ sharkd_json_value_stringf(TRUE, "fg", "%x", color_t_to_rgb(&fdata->color_filter->fg_color));
}
if (data_src)
{
struct data_source *src = (struct data_source *) data_src->data;
- const char *ds_sepa = NULL;
+ gboolean ds_sepa = FALSE;
+ gboolean ds_open = FALSE;
tvbuff_t *tvb;
guint length;
tvb = get_data_source_tvb(src);
length = tvb_captured_length(tvb);
- printf(",\"bytes\":");
if (length != 0)
{
const guchar *cp = tvb_get_ptr(tvb, 0, length);
- /* XXX pi.fd->flags.encoding */
- json_print_base64(cp, length);
+ /* XXX pi.fd->encoding */
+ sharkd_json_value_base64(TRUE, "bytes", cp, length);
}
else
{
- json_print_base64("", 0);
+ sharkd_json_value_base64(TRUE, "bytes", "", 0);
}
data_src = data_src->next;
if (data_src)
{
- printf(",\"ds\":[");
- ds_sepa = "";
+ sharkd_json_array_open(TRUE, "ds");
+ ds_open = TRUE;
}
while (data_src)
{
src = (struct data_source *) data_src->data;
+ sharkd_json_object_open(ds_sepa);
+
{
char *src_name = get_data_source_name(src);
- printf("%s{\"name\":", ds_sepa);
- json_puts_string(src_name);
+ sharkd_json_value_string(FALSE, "name", src_name);
wmem_free(NULL, src_name);
}
tvb = get_data_source_tvb(src);
length = tvb_captured_length(tvb);
- printf(",\"bytes\":");
if (length != 0)
{
const guchar *cp = tvb_get_ptr(tvb, 0, length);
- /* XXX pi.fd->flags.encoding */
- json_print_base64(cp, length);
+ /* XXX pi.fd->encoding */
+ sharkd_json_value_base64(TRUE, "bytes", cp, length);
}
else
{
- json_print_base64("", 0);
+ sharkd_json_value_base64(TRUE, "bytes", "", 0);
}
- printf("}");
- ds_sepa = ",";
+ sharkd_json_object_close();
+ ds_sepa = TRUE;
data_src = data_src->next;
}
/* close ds, only if was opened */
- if (ds_sepa != NULL)
- printf("]");
+ if (ds_open)
+ sharkd_json_array_close();
}
- printf(",\"fol\":[0");
+ sharkd_json_array_open(TRUE, "fol");
+ // This dummy entry allows sharkd_follower_visit_layers_cb() to always insert ',' before dumping item.
+ // XXX convert to json_dumper and remove the dummy entry.
+ sharkd_json_value_anyf(FALSE, NULL, "0");
follow_iterate_followers(sharkd_follower_visit_layers_cb, pi);
- printf("]");
+ sharkd_json_array_close();
- printf("}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
#define SHARKD_IOGRAPH_MAX_ITEMS 250000 /* 250k limit of items is taken from wireshark-qt, on x86_64 sizeof(io_graph_item_t) is 152, so single graph can take max 36 MB */
GString *error;
};
-static gboolean
+static tap_packet_status
sharkd_iograph_packet(void *g, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
{
struct sharkd_iograph *graph = (struct sharkd_iograph *) g;
int idx;
+ gboolean update_succeeded;
idx = get_io_graph_index(pinfo, graph->interval);
if (idx < 0 || idx >= SHARKD_IOGRAPH_MAX_ITEMS)
- return FALSE;
+ return TAP_PACKET_DONT_REDRAW;
if (idx + 1 > graph->num_items)
{
graph->num_items = idx + 1;
}
- return update_io_graph_item(graph->items, idx, pinfo, edt, graph->hf_index, graph->calc_type, graph->interval);
+ update_succeeded = update_io_graph_item(graph->items, idx, pinfo, edt, graph->hf_index, graph->calc_type, graph->interval);
+ /* XXX - TAP_PACKET_FAILED if the item couldn't be updated, with an error message? */
+ return update_succeeded ? TAP_PACKET_REDRAW : TAP_PACKET_DONT_REDRAW;
}
/**
graph->items = NULL;
if (!graph->error)
- graph->error = register_tap_listener("frame", graph, tok_filter, TL_REQUIRES_PROTO_TREE, NULL, sharkd_iograph_packet, NULL);
+ graph->error = register_tap_listener("frame", graph, tok_filter, TL_REQUIRES_PROTO_TREE, NULL, sharkd_iograph_packet, NULL, NULL);
graph_count++;
if (is_any_ok)
sharkd_retap();
- printf("{\"iograph\":[");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_array_open(FALSE, "iograph");
for (i = 0; i < graph_count; i++)
{
struct sharkd_iograph *graph = &graphs[i];
- if (i)
- printf(",");
- printf("{");
+ sharkd_json_object_open(i != 0);
if (graph->error)
{
- printf("\"errmsg\":");
- json_puts_string(graph->error->str);
+ sharkd_json_value_string(FALSE, "errmsg", graph->error->str);
g_string_free(graph->error, TRUE);
}
else
{
int idx;
int next_idx = 0;
- const char *sepa = "";
- printf("\"items\":[");
+ sharkd_json_array_open(FALSE, "items");
for (idx = 0; idx < graph->num_items; idx++)
{
double val;
if (val == 0.0)
continue;
- printf("%s", sepa);
-
/* cause zeros are not printed, need to output index */
if (next_idx != idx)
- printf("\"%x\",", idx);
+ sharkd_json_value_stringf(next_idx != 0, NULL, "%x", idx);
- printf("%f", val);
+ sharkd_json_value_anyf(idx != 0, NULL, "%f", val);
next_idx = idx + 1;
- sepa = ",";
}
- printf("]");
+ sharkd_json_array_close();
}
- printf("}");
+ sharkd_json_object_close();
remove_tap_listener(graph);
g_free(graph->items);
}
+ sharkd_json_array_close();
- printf("]}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
/**
guint32 interval_ms = 1000; /* default: one per second */
- const char *sepa = "";
+ gboolean sepa = FALSE;
unsigned int framenum;
gint64 idx;
gint64 max_idx = 0;
if (tok_filter)
{
- filter_data = sharkd_session_filter_data(tok_filter);
- if (!filter_data)
+ const struct sharkd_filter_item *filter_item;
+
+ filter_item = sharkd_session_filter_data(tok_filter);
+ if (!filter_item)
return;
+ filter_data = filter_item->filtered;
}
st_total.frames = 0;
idx = 0;
- printf("{\"intervals\":[");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_array_open(FALSE, "intervals");
start_ts = (cfile.count >= 1) ? &(sharkd_get_frame(1)->abs_ts) : NULL;
{
if (st.frames != 0)
{
- printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
- sepa = ",";
+ sharkd_json_value_anyf(sepa, NULL, "[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", idx, st.frames, st.bytes);
+ sepa = TRUE;
}
idx = new_idx;
if (st.frames != 0)
{
- printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
- /* sepa = ","; */
+ sharkd_json_value_anyf(sepa, NULL, "[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", idx, st.frames, st.bytes);
+ /* sepa = TRUE; */
}
+ sharkd_json_array_close();
+
+ sharkd_json_value_anyf(TRUE, "last", "%" G_GINT64_FORMAT, max_idx);
+ sharkd_json_value_anyf(TRUE, "frames", "%u", st_total.frames);
+ sharkd_json_value_anyf(TRUE, "bytes", "%" G_GUINT64_FORMAT, st_total.bytes);
- printf("],\"last\":%" G_GINT64_FORMAT ",\"frames\":%u,\"bytes\":%" G_GUINT64_FORMAT "}\n", max_idx, st_total.frames, st_total.bytes);
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
/**
* (o) prev_frame - previously displayed frame number
* (o) proto - set if output frame tree
* (o) columns - set if output frame columns
+ * (o) color - set if output color-filter bg/fg
* (o) bytes - set if output frame bytes
+ * (o) hidden - set if output hidden tree fields
*
* Output object with attributes:
* (m) err - 0 if succeed
* ds- data src index
* url - only for t:'url', url
* fnum - only for t:'framenum', frame number
+ * g - if field is generated by Wireshark
+ * v - if field is hidden
*
* (o) col - array of column data
* (o) bytes - base64 of frame bytes
* (o) fol - array of follow filters:
* [0] - protocol
* [1] - filter string
+ * (o) i - if frame is ignored
+ * (o) m - if frame is marked
+ * (o) bg - color filter - background color in hex
+ * (o) fg - color filter - foreground color in hex
*/
static void
sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
const char *tok_ref_frame = json_find_attr(buf, tokens, count, "ref_frame");
const char *tok_prev_frame = json_find_attr(buf, tokens, count, "prev_frame");
- int tok_proto = (json_find_attr(buf, tokens, count, "proto") != NULL);
- int tok_bytes = (json_find_attr(buf, tokens, count, "bytes") != NULL);
- int tok_columns = (json_find_attr(buf, tokens, count, "columns") != NULL);
guint32 framenum, ref_frame_num, prev_dis_num;
+ guint32 dissect_flags = SHARKD_DISSECT_FLAG_NULL;
+ struct sharkd_frame_request_data req_data;
if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
return;
if (tok_prev_frame && (!ws_strtou32(tok_prev_frame, NULL, &prev_dis_num) || prev_dis_num >= framenum))
return;
- sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num, &sharkd_session_process_frame_cb, tok_bytes, tok_columns, tok_proto, NULL);
+ if (json_find_attr(buf, tokens, count, "proto") != NULL)
+ dissect_flags |= SHARKD_DISSECT_FLAG_PROTO_TREE;
+ if (json_find_attr(buf, tokens, count, "bytes") != NULL)
+ dissect_flags |= SHARKD_DISSECT_FLAG_BYTES;
+ if (json_find_attr(buf, tokens, count, "columns") != NULL)
+ dissect_flags |= SHARKD_DISSECT_FLAG_COLUMNS;
+ if (json_find_attr(buf, tokens, count, "color") != NULL)
+ dissect_flags |= SHARKD_DISSECT_FLAG_COLOR;
+
+ req_data.display_hidden = (json_find_attr(buf, tokens, count, "v") != NULL);
+
+ sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num, &sharkd_session_process_frame_cb, dissect_flags, &req_data);
}
/**
*
* Input:
* (o) filter - filter to be checked
+ * (o) field - field to be checked
*
* Output object with attributes:
* (m) err - always 0
* (o) filter - 'ok', 'warn' or error message
+ * (o) field - 'ok', or 'notfound'
*/
static int
sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
{
const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
+ const char *tok_field = json_find_attr(buf, tokens, count, "field");
+
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_anyf(FALSE, "err", "0");
- printf("{\"err\":0");
if (tok_filter != NULL)
{
char *err_msg = NULL;
{
const char *s = "ok";
- if (dfilter_deprecated_tokens(dfp))
+ if (dfp && dfilter_deprecated_tokens(dfp))
s = "warn";
- printf(",\"filter\":\"%s\"", s);
+ sharkd_json_value_string(TRUE, "filter", s);
dfilter_free(dfp);
}
else
{
- printf(",\"filter\":");
- json_puts_string(err_msg);
+ sharkd_json_value_string(TRUE, "filter", err_msg);
g_free(err_msg);
}
}
- printf("}\n");
+ if (tok_field != NULL)
+ {
+ header_field_info *hfi = proto_registrar_get_byname(tok_field);
+
+ sharkd_json_value_string(TRUE, "field", (hfi) ? "ok" : "notfound");
+ }
+
+ sharkd_json_object_close();
+ sharkd_json_finish();
+
return 0;
}
{
const char *module;
const char *pref;
- const char *sepa;
+ gboolean sepa;
};
static guint
if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
return 0;
- printf("%s{\"f\":\"%s\",\"d\":\"%s\"}", data->sepa, module->name, module->title);
- data->sepa = ",";
+ sharkd_json_object_open(data->sepa);
+ sharkd_json_value_string(FALSE, "f", module->name);
+ sharkd_json_value_string(TRUE, "d", module->title);
+ sharkd_json_object_close();
+
+ data->sepa = TRUE;
return 0;
}
if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
return 0;
- printf("%s{\"f\":\"%s.%s\",\"d\":\"%s\"}", data->sepa, data->module, pref_name, pref_title);
- data->sepa = ",";
+ sharkd_json_object_open(data->sepa);
+ sharkd_json_value_stringf(FALSE, "f", "%s.%s", data->module, pref_name);
+ sharkd_json_value_string(TRUE, "d", pref_title);
+ sharkd_json_object_close();
+
+ data->sepa = TRUE;
return 0; /* continue */
}
const char *tok_field = json_find_attr(buf, tokens, count, "field");
const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
- printf("{\"err\":0");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_anyf(FALSE, "err", "0");
+
if (tok_field != NULL && tok_field[0])
{
const size_t filter_length = strlen(tok_field);
void *proto_cookie;
void *field_cookie;
int proto_id;
- const char *sepa = "";
+ gboolean sepa = FALSE;
- printf(",\"field\":[");
+ sharkd_json_array_open(TRUE, "field");
for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
{
if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
{
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
{
- printf("\"f\":");
- json_puts_string(protocol_filter);
- printf(",\"t\":%d", FT_PROTOCOL);
- printf(",\"n\":");
- json_puts_string(protocol_name);
+ sharkd_json_value_string(FALSE, "f", protocol_filter);
+ sharkd_json_value_anyf(TRUE, "t", "%d", FT_PROTOCOL);
+ sharkd_json_value_string(TRUE, "n", protocol_name);
}
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
if (!filter_with_dot)
if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
{
- printf("%s{", sepa);
+ sharkd_json_object_open(sepa);
{
- printf("\"f\":");
- json_puts_string(hfinfo->abbrev);
+ sharkd_json_value_string(FALSE, "f", hfinfo->abbrev);
/* XXX, skip displaying name, if there are multiple (to not confuse user) */
if (hfinfo->same_name_next == NULL)
{
- printf(",\"t\":%d", hfinfo->type);
- printf(",\"n\":");
- json_puts_string(hfinfo->name);
+ sharkd_json_value_anyf(TRUE, "t", "%d", hfinfo->type);
+ sharkd_json_value_string(TRUE, "n", hfinfo->name);
}
}
- printf("}");
- sepa = ",";
+ sharkd_json_object_close();
+ sepa = TRUE;
}
}
}
- printf("]");
+ sharkd_json_array_close();
}
if (tok_pref != NULL && tok_pref[0])
data.module = tok_pref;
data.pref = tok_pref;
- data.sepa = "";
-
- printf(",\"pref\":[");
+ data.sepa = FALSE;
+ sharkd_json_array_open(TRUE, "pref");
if ((dot_sepa = strchr(tok_pref, '.')))
{
module_t *pref_mod;
{
prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
}
-
- printf("]");
+ sharkd_json_array_close();
}
+ sharkd_json_object_close();
+ sharkd_json_finish();
- printf("}\n");
return 0;
}
return;
ret = sharkd_set_user_comment(fdata, tok_comment);
- printf("{\"err\":%d}\n", ret);
+
+ sharkd_json_simple_reply(ret, NULL);
}
/**
ws_snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
ret = prefs_set_pref(pref, &errmsg);
- printf("{\"err\":%d", ret);
- if (errmsg)
- {
- /* Add error message for some syntax errors. */
- printf(",\"errmsg\":");
- json_puts_string(errmsg);
- }
- printf("}\n");
+
+ sharkd_json_simple_reply(ret, errmsg);
g_free(errmsg);
}
struct sharkd_session_process_dumpconf_data
{
module_t *module;
- const char *sepa;
+ gboolean sepa;
};
static guint
struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
const char *pref_name = prefs_get_name(pref);
- printf("%s\"%s.%s\":{", data->sepa, data->module->name, pref_name);
+ char json_pref_key[512];
+
+ snprintf(json_pref_key, sizeof(json_pref_key), "%s.%s", data->module->name, pref_name);
+ sharkd_json_value_anyf(data->sepa, json_pref_key, "{");
switch (prefs_get_type(pref))
{
case PREF_UINT:
case PREF_DECODE_AS_UINT:
- printf("\"u\":%u", prefs_get_uint_value_real(pref, pref_current));
+ sharkd_json_value_anyf(FALSE, "u", "%u", prefs_get_uint_value_real(pref, pref_current));
if (prefs_get_uint_base(pref) != 10)
- printf(",\"ub\":%u", prefs_get_uint_base(pref));
+ sharkd_json_value_anyf(TRUE, "ub", "%u", prefs_get_uint_base(pref));
break;
case PREF_BOOL:
- printf("\"b\":%s", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
+ sharkd_json_value_anyf(FALSE, "b", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
break;
case PREF_STRING:
case PREF_SAVE_FILENAME:
case PREF_OPEN_FILENAME:
case PREF_DIRNAME:
- printf("\"s\":");
- json_puts_string(prefs_get_string_value(pref, pref_current));
+ sharkd_json_value_string(FALSE, "s", prefs_get_string_value(pref, pref_current));
break;
case PREF_ENUM:
{
const enum_val_t *enums;
- const char *enum_sepa = "";
+ gboolean enum_sepa = FALSE;
- printf("\"e\":[");
+ sharkd_json_array_open(FALSE, "e");
for (enums = prefs_get_enumvals(pref); enums->name; enums++)
{
- printf("%s{\"v\":%d", enum_sepa, enums->value);
+ sharkd_json_object_open(enum_sepa);
+
+ sharkd_json_value_anyf(FALSE, "v", "%d", enums->value);
if (enums->value == prefs_get_enum_value(pref, pref_current))
- printf(",\"s\":1");
+ sharkd_json_value_anyf(TRUE, "s", "1");
- printf(",\"d\":");
- json_puts_string(enums->description);
+ sharkd_json_value_string(TRUE, "d", enums->description);
- printf("}");
- enum_sepa = ",";
+ sharkd_json_object_close();
+ enum_sepa = TRUE;
}
- printf("]");
+ sharkd_json_array_close();
break;
}
case PREF_DECODE_AS_RANGE:
{
char *range_str = range_convert_range(NULL, prefs_get_range_value_real(pref, pref_current));
- printf("\"r\":\"%s\"", range_str);
+ sharkd_json_value_string(FALSE, "r", range_str);
wmem_free(NULL, range_str);
break;
}
uat_t *uat = prefs_get_uat_value(pref);
guint idx;
- printf("\"t\":[");
+ sharkd_json_array_open(FALSE, "t");
for (idx = 0; idx < uat->raw_data->len; idx++)
{
void *rec = UAT_INDEX_PTR(uat, idx);
guint colnum;
- if (idx)
- printf(",");
-
- printf("[");
+ sharkd_json_array_open(idx != 0, NULL);
for (colnum = 0; colnum < uat->ncols; colnum++)
{
char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
- if (colnum)
- printf(",");
-
- json_puts_string(str);
+ sharkd_json_value_string(colnum != 0, NULL, str);
g_free(str);
}
- printf("]");
+ sharkd_json_array_close();
}
- printf("]");
+ sharkd_json_array_close();
break;
}
}
#if 0
- printf(",\"t\":");
- json_puts_string(prefs_get_title(pref));
+ sharkd_json_value_string(TRUE, "t", prefs_get_title(pref));
#endif
- printf("}");
- data->sepa = ",";
+ sharkd_json_object_close();
+ data->sepa = TRUE;
return 0; /* continue */
}
struct sharkd_session_process_dumpconf_data data;
data.module = NULL;
- data.sepa = "";
+ data.sepa = FALSE;
- printf("{\"prefs\":{");
+ sharkd_json_object_open(FALSE);
+
+ sharkd_json_value_anyf(FALSE, "prefs", NULL);
+ sharkd_json_object_open(FALSE);
prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
- printf("}}\n");
+ sharkd_json_object_close();
+
+ sharkd_json_object_close();
+ sharkd_json_finish();
return;
}
struct sharkd_session_process_dumpconf_data data;
data.module = pref_mod;
- data.sepa = "";
+ data.sepa = FALSE;
+
+ sharkd_json_object_open(FALSE);
- printf("{\"prefs\":{");
+ sharkd_json_value_anyf(FALSE, "prefs", NULL);
+ sharkd_json_object_open(FALSE);
sharkd_session_process_dumpconf_cb(pref, &data);
- printf("}}\n");
+ sharkd_json_object_close();
+
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
return;
struct sharkd_session_process_dumpconf_data data;
data.module = pref_mod;
- data.sepa = "";
+ data.sepa = FALSE;
+
+ sharkd_json_object_open(FALSE);
- printf("{\"prefs\":{");
+ sharkd_json_value_anyf(FALSE, "prefs", NULL);
+ sharkd_json_object_open(FALSE);
prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
- printf("}}\n");
+ sharkd_json_object_close();
+
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
}
g_hash_table_destroy(decoders_hash_);
}
-static gboolean
+static tap_packet_status
sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
{
const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
/* do not consider RTP packets without a setup frame */
if (rtp_info->info_setup_frame_num == 0)
- return FALSE;
+ return TAP_PACKET_DONT_REDRAW;
if (rtpstream_id_equal_pinfo_rtp_info(&req_rtp->id, pinfo, rtp_info))
{
req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
}
- return FALSE;
+ return TAP_PACKET_DONT_REDRAW;
}
/**
const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
- printf("{\"file\":");
- json_puts_string(filename);
- printf(",\"mime\":");
- json_puts_string(mime);
- printf(",\"data\":");
- json_print_base64(eo_entry->payload_data, (size_t)(eo_entry->payload_len));
- printf("}\n");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "file", filename);
+ sharkd_json_value_string(TRUE, "mime", mime);
+ sharkd_json_value_base64(TRUE, "data", eo_entry->payload_data, (size_t) eo_entry->payload_len);
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
}
else if (!strcmp(tok_token, "ssl-secrets"))
const char *mime = "text/plain";
const char *filename = "keylog.txt";
- printf("{\"file\":");
- json_puts_string(filename);
- printf(",\"mime\":");
- json_puts_string(mime);
- printf(",\"data\":");
- json_print_base64(str, strlen(str));
- printf("}\n");
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "file", filename);
+ sharkd_json_value_string(TRUE, "mime", mime);
+ sharkd_json_value_base64(TRUE, "data", str, strlen(str));
+ sharkd_json_object_close();
+ sharkd_json_finish();
}
g_free(str);
}
return;
}
- tap_error = register_tap_listener("rtp", &rtp_req, NULL, 0, NULL, sharkd_session_packet_download_tap_rtp_cb, NULL);
+ tap_error = register_tap_listener("rtp", &rtp_req, NULL, 0, NULL, sharkd_session_packet_download_tap_rtp_cb, NULL, NULL);
if (tap_error)
{
fprintf(stderr, "sharkd_session_process_download() rtp error=%s", tap_error->str);
const char *mime = "audio/x-wav";
const char *filename = tok_token;
- printf("{\"file\":");
- json_puts_string(filename);
- printf(",\"mime\":");
- json_puts_string(mime);
+ sharkd_json_object_open(FALSE);
+ sharkd_json_value_string(FALSE, "file", filename);
+ sharkd_json_value_string(TRUE, "mime", mime);
- printf(",\"data\":");
+ sharkd_json_value_anyf(TRUE, "data", NULL);
putchar('"');
sharkd_rtp_download_decode(&rtp_req);
putchar('"');
- printf("}\n");
+ sharkd_json_object_close();
+ sharkd_json_finish();
g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
}
buf[tokens[i + 1].end] = '\0';
/* unescape only value, as keys are simple strings */
- if (tokens[i + 1].type == JSMN_STRING && !json_unescape_str(&buf[tokens[i + 1].start]))
+ if (tokens[i + 1].type == JSMN_STRING && !json_decode_string_inplace(&buf[tokens[i + 1].start]))
{
fprintf(stderr, "sanity check(3b): [%d] cannot unescape string\n", i + 1);
return;
fprintf(stderr, "::: req = %s\n", tok_req);
/* reply for every command are 0+ lines of JSON reply (outputed above), finished by empty new line */
- printf("\n");
+ sharkd_json_finish();
/*
* We do an explicit fflush after every line, because
/* every command is line seperated JSON */
int ret;
- ret = wsjson_parse(buf, NULL, 0);
+ ret = json_parse(buf, NULL, 0);
if (ret < 0)
{
fprintf(stderr, "invalid JSON -> closing\n");
memset(tokens, 0, ret * sizeof(jsmntok_t));
- ret = wsjson_parse(buf, tokens, ret);
+ ret = json_parse(buf, tokens, ret);
if (ret < 0)
{
fprintf(stderr, "invalid JSON(2) -> closing\n");