X-Git-Url: http://git.samba.org/?p=metze%2Fwireshark%2Fwip.git;a=blobdiff_plain;f=sharkd_session.c;h=010a543c236a83e3a60546260fa51c27d13095ca;hp=08cf739e8572e308072a3d8896e799aecc7c7661;hb=1774aafcf1700c7cd34fe66e2884b92b6acb823c;hpb=3c9f440ac60ba83f155a89a851f9a1d3cc947f8d diff --git a/sharkd_session.c b/sharkd_session.c index 08cf739e85..010a543c23 100644 --- a/sharkd_session.c +++ b/sharkd_session.c @@ -6,31 +6,20 @@ * By Gerald Combs * Copyright 1998 Gerald Combs * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * SPDX-License-Identifier: GPL-2.0-or-later */ #include #include #include +#include #include #include #include -#include +#include #include #include @@ -46,6 +35,7 @@ #include +#include #include #include #include @@ -61,6 +51,7 @@ #include #include #include +#include #include #include @@ -72,22 +63,21 @@ # include #endif /* HAVE_SPEEXDSP */ -#ifdef HAVE_GEOIP -# include -# include -# include -#endif +#include -#include +#include #include +#include "globals.h" + #include "sharkd.h" -static gboolean -json_unescape_str(char *input) +struct sharkd_filter_item { - return wsjsmn_unescape_json_string(input, input); -} + guint8 *filtered; /* can be NULL if all frames are matching for given filter. */ +}; + +static GHashTable *filter_table = NULL; static const char * json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr) @@ -107,32 +97,30 @@ json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char * } 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]); } } @@ -153,7 +141,7 @@ json_print_base64_step(const guint8 *data, int *state1, int *state2) if (wrote > 0) { buf[wrote] = '\0'; - printf("%s", buf); + fputs(buf, stdout); } } @@ -174,27 +162,116 @@ json_print_base64(const guint8 *data, size_t len) putchar('"'); } -struct filter_item +static void G_GNUC_PRINTF(3, 4) +sharkd_json_value_anyf(gboolean sepa, const char *key, const char *valuef, ...) { - struct filter_item *next; + if (sepa) + putchar(','); - char *filter; - guint8 *filtered; -}; + if (key) + { + putchar('"'); + fputs(key, stdout); + putchar('"'); + putchar(':'); + } -static struct filter_item *filter_list = NULL; + if (valuef && *valuef) + { + va_list ap; -static const guint8 * -sharkd_session_filter_data(const char *filter) + va_start(ap, valuef); + vprintf(valuef, ap); + va_end(ap); + } +} + +static void +sharkd_json_value_string(gboolean sepa, const char *key, const char *str) { - struct filter_item *l; + sharkd_json_value_anyf(sepa, key, NULL); + json_puts_string(str); +} - for (l = filter_list; l; l = l->next) - { - if (!strcmp(l->filter, filter)) - return l->filtered; - } +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) +{ + struct sharkd_filter_item *l = (struct sharkd_filter_item *) data; + + g_free(l->filtered); + g_free(l); +} + +static const struct sharkd_filter_item * +sharkd_session_filter_data(const char *filter) +{ + struct sharkd_filter_item *l; + + l = (struct sharkd_filter_item *) g_hash_table_lookup(filter_table, filter); + if (!l) { guint8 *filtered = NULL; @@ -203,55 +280,50 @@ sharkd_session_filter_data(const char *filter) if (ret == -1) return NULL; - l = (struct filter_item *) g_malloc(sizeof(struct filter_item)); - l->filter = g_strdup(filter); + l = (struct sharkd_filter_item *) g_malloc(sizeof(struct sharkd_filter_item)); l->filtered = filtered; - l->next = filter_list; - filter_list = l; - - return filtered; + g_hash_table_insert(filter_table, g_strdup(filter), l); } -} -struct sharkd_rtp_match -{ - guint32 addr_src, addr_dst; - address src_addr; - address dst_addr; - guint16 src_port; - guint16 dst_port; - guint32 ssrc; -}; + return l; +} static gboolean -sharkd_rtp_match_init(struct sharkd_rtp_match *req, const char *init_str) +sharkd_rtp_match_init(rtpstream_id_t *id, const char *init_str) { gboolean ret = FALSE; char **arr; + guint32 tmp_addr_src, tmp_addr_dst; + address tmp_src_addr, tmp_dst_addr; + + memset(id, 0, sizeof(*id)); arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */ if (g_strv_length(arr) != 5) goto fail; /* TODO, for now only IPv4 */ - if (!get_host_ipaddr(arr[0], &req->addr_src)) + if (!get_host_ipaddr(arr[0], &tmp_addr_src)) goto fail; - if (!ws_strtou16(arr[1], NULL, &req->src_port)) + if (!ws_strtou16(arr[1], NULL, &id->src_port)) goto fail; - if (!get_host_ipaddr(arr[2], &req->addr_dst)) + if (!get_host_ipaddr(arr[2], &tmp_addr_dst)) goto fail; - if (!ws_strtou16(arr[3], NULL, &req->dst_port)) + if (!ws_strtou16(arr[3], NULL, &id->dst_port)) goto fail; - if (!ws_hexstrtou32(arr[4], NULL, &req->ssrc)) + if (!ws_hexstrtou32(arr[4], NULL, &id->ssrc)) goto fail; - set_address(&req->src_addr, AT_IPv4, 4, &req->addr_src); - set_address(&req->dst_addr, AT_IPv4, 4, &req->addr_dst); + set_address(&tmp_src_addr, AT_IPv4, 4, &tmp_addr_src); + copy_address(&id->src_addr, &tmp_src_addr); + set_address(&tmp_dst_addr, AT_IPv4, 4, &tmp_addr_dst); + copy_address(&id->dst_addr, &tmp_dst_addr); + ret = TRUE; fail: @@ -259,31 +331,16 @@ fail: return ret; } -static gboolean -sharkd_rtp_match_check(const struct sharkd_rtp_match *req, const packet_info *pinfo, const struct _rtp_info *rtp_info) -{ - if (rtp_info->info_sync_src == req->ssrc && - pinfo->srcport == req->src_port && - pinfo->destport == req->dst_port && - addresses_equal(&pinfo->src, &req->src_addr) && - addresses_equal(&pinfo->dst, &req->dst_addr)) - { - return TRUE; - } - - return FALSE; -} - static gboolean sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata) { - stat_tap_table_ui *new_stat_tap = (stat_tap_table_ui *) value; + stat_tap_table_ui *stat_tap = (stat_tap_table_ui *) value; int *pi = (int *) userdata; - printf("%s{", (*pi) ? "," : ""); - printf("\"name\":\"%s\"", new_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; @@ -295,24 +352,24 @@ sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata struct register_ct *table = (struct register_ct *) value; int *pi = (int *) userdata; - const char *label = (const char*)key; + const char *label = (const char *) key; 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; } @@ -322,13 +379,13 @@ sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata static gboolean sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata) { - register_analysis_t *analysis = (register_analysis_t*) value; + 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; @@ -337,17 +394,17 @@ sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata) static gboolean sharkd_export_object_visit_cb(const void *key _U_, void *value, void *user_data) { - register_eo_t *eo = (register_eo_t*)value; + register_eo_t *eo = (register_eo_t *) value; int *pi = (int *) user_data; const int proto_id = get_eo_proto_id(eo); 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; @@ -363,10 +420,10 @@ sharkd_srt_visit_cb(const void *key _U_, void *value, void *user_data) 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; @@ -382,10 +439,10 @@ sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data) 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; @@ -394,17 +451,17 @@ sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data) static gboolean sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data) { - register_follow_t *follower = (register_follow_t*) value; + register_follow_t *follower = (register_follow_t *) value; int *pi = (int *) user_data; const int proto_id = get_follow_proto_id(follower); 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; @@ -416,6 +473,8 @@ sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data) * 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 @@ -444,110 +503,120 @@ sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data) * '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: * 'name' - tap name * 'tap' - sharkd tap-name * - * (m) follow - available followers, array of object with attributes: + * (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; - new_stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i); - printf("]"); + stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i); + 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(); } /** @@ -574,7 +643,7 @@ sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count) 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; } @@ -589,7 +658,7 @@ sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count) } ENDTRY; - printf("{\"err\":%d}\n", err); + sharkd_json_simple_reply(err, NULL); } /** @@ -606,28 +675,29 @@ sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count) 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); } - if (cfile.wth) + if (cfile.provider.wth) { - gint64 file_size = wtap_file_size(cfile.wth, NULL); + 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 @@ -664,11 +734,11 @@ sharkd_session_process_analyse_cb(epan_dissect_t *edt, proto_tree *tree, struct 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)); } } } @@ -696,20 +766,23 @@ sharkd_session_process_analyse(void) 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, &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); } @@ -734,13 +807,15 @@ sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok if (tok_column == NULL) break; + columns_custom[i] = NULL; + columns_occur[i] = 0; + if ((custom_sepa = strchr(tok_column, ':'))) { *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */ columns_fmt[i] = COL_CUSTOM; columns_custom[i] = tok_column; - columns_occur[i] = 0; if (!ws_strtoi16(custom_sepa + 1, NULL, &columns_occur[i])) return NULL; @@ -795,6 +870,7 @@ sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok * (o) filter - filter to be used * (o) skip=N - skip N frames * (o) limit=N - show only N frames + * (o) refs - list (comma separated) with sorted time reference frame numbers. * * Output array of frames with attributes: * (m) c - array of column data @@ -812,13 +888,15 @@ sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int coun const char *tok_column = json_find_attr(buf, tokens, count, "column0"); const char *tok_skip = json_find_attr(buf, tokens, count, "skip"); const char *tok_limit = json_find_attr(buf, tokens, count, "limit"); + const char *tok_refs = json_find_attr(buf, tokens, count, "refs"); const guint8 *filter_data = NULL; - const char *frame_sepa = ""; + gboolean frame_sepa = FALSE; int col; - guint32 framenum; + guint32 framenum, prev_dis_num = 0; + guint32 current_ref_frame = 0, next_ref_frame = G_MAXUINT32; guint32 skip; guint32 limit; @@ -835,9 +913,12 @@ sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int coun 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; @@ -854,10 +935,17 @@ sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int coun return; } - printf("["); + if (tok_refs) + { + if (!ws_strtou32(tok_refs, &tok_refs, &next_ref_frame)) + return; + } + + sharkd_json_array_open(FALSE, NULL); for (framenum = 1; framenum <= cfile.count; framenum++) { - frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum); + frame_data *fdata; + guint32 ref_frame = (framenum != 1) ? 1 : 0; if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8)))) continue; @@ -865,45 +953,84 @@ sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int coun if (skip) { skip--; + prev_dis_num = framenum; continue; } - sharkd_dissect_columns(framenum, cinfo, (fdata->color_filter == NULL)); + if (tok_refs) + { + if (framenum >= next_ref_frame) + { + current_ref_frame = next_ref_frame; + + if (*tok_refs != ',') + next_ref_frame = G_MAXUINT32; + + while (*tok_refs == ',' && framenum >= next_ref_frame) + { + current_ref_frame = next_ref_frame; + + if (!ws_strtou32(tok_refs + 1, &tok_refs, &next_ref_frame)) + { + fprintf(stderr, "sharkd_session_process_frames() wrong format for refs: %s\n", tok_refs); + break; + } + } + + if (*tok_refs == '\0' && framenum >= next_ref_frame) + { + current_ref_frame = next_ref_frame; + next_ref_frame = G_MAXUINT32; + } + } - printf("%s{\"c\":[", frame_sepa); + if (current_ref_frame) + ref_frame = current_ref_frame; + } + + fdata = sharkd_get_frame(framenum); + sharkd_dissect_columns(fdata, ref_frame, prev_dis_num, cinfo, (fdata->color_filter == NULL)); + + 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_phdr_comment) - printf(",\"ct\":true"); + sharkd_json_value_anyf(TRUE, "num", "%u", framenum); - if (fdata->flags.ignored) - printf(",\"i\":true"); + if (fdata->has_user_comment || fdata->has_phdr_comment) + { + if (!fdata->has_user_comment || sharkd_get_user_comment(fdata) != NULL) + sharkd_json_value_anyf(TRUE, "ct", "true"); + } - if (fdata->flags.marked) - printf(",\"m\":true"); + if (fdata->ignored) + sharkd_json_value_anyf(TRUE, "i", "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); @@ -913,48 +1040,60 @@ static void 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(); } /** @@ -983,11 +1122,17 @@ sharkd_session_process_tap_stats_cb(void *psp) { 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 @@ -1023,59 +1168,66 @@ sharkd_session_process_tap_expert_cb(void *tapdata) { 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 = (expert_info_t *) pointer; + const expert_info_t *ei = (const expert_info_t *) pointer; + expert_info_t *ei_copy; + + if (ei == NULL) + return TAP_PACKET_DONT_REDRAW; - ei = (expert_info_t *) g_memdup(ei, sizeof(*ei)); - ei->protocol = g_string_chunk_insert_const(etd->text, ei->protocol); - ei->summary = g_string_chunk_insert_const(etd->text, ei->summary); + ei_copy = g_new(expert_info_t, 1); + /* Note: this is a shallow copy */ + *ei_copy = *ei; - etd->details = g_slist_prepend(etd->details, ei); + /* ei->protocol, ei->summary might be allocated in packet scope, make a copy. */ + ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol); + ei_copy->summary = g_string_chunk_insert_const(etd->text, ei_copy->summary); - return TRUE; + etd->details = g_slist_prepend(etd->details, ei_copy); + + return TAP_PACKET_REDRAW; } static void @@ -1108,28 +1260,26 @@ sharkd_session_process_tap_flow_cb(void *tapdata) 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) { @@ -1140,25 +1290,22 @@ sharkd_session_process_tap_flow_cb(void *tapdata) 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 @@ -1177,126 +1324,78 @@ struct sharkd_conv_tap_data gboolean resolve_port; }; -static int +static gboolean sharkd_session_geoip_addr(address *addr, const char *suffix) { - int with_geoip = 0; - - (void) addr; - (void) suffix; + const mmdb_lookup_t *lookup = NULL; + gboolean with_geoip = FALSE; + char json_key[64]; -#ifdef HAVE_GEOIP if (addr->type == AT_IPv4) { - guint32 ip = pntoh32(addr->data); - - guint num_dbs = geoip_db_num_dbs(); - guint dbnum; - - for (dbnum = 0; dbnum < num_dbs; dbnum++) - { - const char *geoip_key = NULL; - char *geoip_val; - - int db_type = geoip_db_type(dbnum); - - switch (db_type) - { - case GEOIP_COUNTRY_EDITION: - geoip_key = "geoip_country"; - break; - - case GEOIP_CITY_EDITION_REV0: - case GEOIP_CITY_EDITION_REV1: - geoip_key = "geoip_city"; - break; + guint32 ip; - case GEOIP_ORG_EDITION: - geoip_key = "geoip_org"; - break; - - case GEOIP_ISP_EDITION: - geoip_key = "geoip_isp"; - break; - - case GEOIP_ASNUM_EDITION: - geoip_key = "geoip_as"; - break; - - case WS_LAT_FAKE_EDITION: - geoip_key = "geoip_lat"; - break; - - case WS_LON_FAKE_EDITION: - geoip_key = "geoip_lon"; - break; - } - - if (geoip_key && (geoip_val = geoip_db_lookup_ipv4(dbnum, ip, NULL))) - { - printf(",\"%s%s\":", geoip_key, suffix); - json_puts_string(geoip_val); - with_geoip = 1; - } - } + memcpy(&ip, addr->data, 4); + lookup = maxmind_db_lookup_ipv4(ip); } -#ifdef HAVE_GEOIP_V6 - if (addr->type == AT_IPv6) + else if (addr->type == AT_IPv6) { - const struct e_in6_addr *ip6 = (const struct e_in6_addr *) addr->data; + const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data; - guint num_dbs = geoip_db_num_dbs(); - guint dbnum; + lookup = maxmind_db_lookup_ipv6(ip6); + } - for (dbnum = 0; dbnum < num_dbs; dbnum++) - { - const char *geoip_key = NULL; - char *geoip_val; + if (!lookup || !lookup->found) + return FALSE; - int db_type = geoip_db_type(dbnum); + if (lookup->country) + { + snprintf(json_key, sizeof(json_key), "geoip_country%s", suffix); + sharkd_json_value_string(TRUE, json_key, lookup->country); + with_geoip = TRUE; + } - switch (db_type) - { - case GEOIP_COUNTRY_EDITION_V6: - geoip_key = "geoip_country"; - break; -#if NUM_DB_TYPES > 31 - case GEOIP_CITY_EDITION_REV0_V6: - case GEOIP_CITY_EDITION_REV1_V6: - geoip_key = "geoip_city"; - break; + if (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; + } - case GEOIP_ORG_EDITION_V6: - geoip_key = "geoip_org"; - break; + if (lookup->city) + { + snprintf(json_key, sizeof(json_key), "geoip_city%s", suffix); + sharkd_json_value_string(TRUE, json_key, lookup->city); + with_geoip = TRUE; + } - case GEOIP_ISP_EDITION_V6: - geoip_key = "geoip_isp"; - break; + if (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; + } - case GEOIP_ASNUM_EDITION_V6: - geoip_key = "geoip_as"; - break; -#endif /* DB_NUM_TYPES */ - case WS_LAT_FAKE_EDITION: - geoip_key = "geoip_lat"; - break; + if (lookup->as_number > 0) + { + snprintf(json_key, sizeof(json_key), "geoip_as%s", suffix); + sharkd_json_value_anyf(TRUE, json_key, "%u", lookup->as_number); + with_geoip = TRUE; + } - case WS_LON_FAKE_EDITION: - geoip_key = "geoip_lon"; - break; - } + if (lookup->latitude >= -90.0 && lookup->latitude <= 90.0) + { + snprintf(json_key, sizeof(json_key), "geoip_lat%s", suffix); + sharkd_json_value_anyf(TRUE, json_key, "%f", lookup->latitude); + with_geoip = TRUE; + } - if (geoip_key && (geoip_val = geoip_db_lookup_ipv6(dbnum, *ip6, NULL))) - { - printf(",\"%s%s\":", geoip_key, suffix); - json_puts_string(geoip_val); - with_geoip = 1; - } - } + if (lookup->longitude >= -180.0 && lookup->longitude <= 180.0) + { + snprintf(json_key, sizeof(json_key), "geoip_lon%s", suffix); + sharkd_json_value_anyf(TRUE, json_key, "%f", lookup->longitude); + with_geoip = TRUE; } -#endif /* HAVE_GEOIP_V6 */ -#endif /* HAVE_GEOIP */ return with_geoip; } @@ -1322,7 +1421,7 @@ struct sharkd_analyse_rtp_items struct sharkd_analyse_rtp { const char *tap_name; - struct sharkd_rtp_match rtp; + rtpstream_id_t id; GSList *packets; double start_time; @@ -1338,18 +1437,18 @@ sharkd_session_process_tap_rtp_free_cb(void *tapdata) 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; - const struct _rtp_info *rtpinfo = (const struct _rtp_info *) pointer; + const struct _rtp_info *rtp_info = (const struct _rtp_info *) pointer; - if (sharkd_rtp_match_check(&rtp_req->rtp, pinfo, rtpinfo)) + if (rtpstream_id_equal_pinfo_rtp_info(&rtp_req->id, pinfo, rtp_info)) { tap_rtp_stat_t *statinfo = &(rtp_req->statinfo); struct sharkd_analyse_rtp_items *item; - rtp_packet_analyse(statinfo, pinfo, rtpinfo); + rtppacket_analyse(statinfo, pinfo, rtp_info); item = (struct sharkd_analyse_rtp_items *) g_malloc(sizeof(struct sharkd_analyse_rtp_items)); @@ -1357,12 +1456,12 @@ sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts); item->frame_num = pinfo->num; - item->sequence_num = rtpinfo->info_seq_num; + item->sequence_num = rtp_info->info_seq_num; item->delta = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->delta; item->jitter = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->jitter; item->skew = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->skew; item->bandwidth = statinfo->bandwidth; - item->marker = rtpinfo->info_marker_set ? TRUE : FALSE; + item->marker = rtp_info->info_marker_set ? TRUE : FALSE; item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time; item->flags = statinfo->flags; @@ -1372,7 +1471,7 @@ sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan rtp_req->packets = g_slist_append(rtp_req->packets, item); } - return TRUE; + return TAP_PACKET_REDRAW; } /** @@ -1413,74 +1512,100 @@ sharkd_session_process_tap_rtp_analyse_cb(void *tapdata) 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->rtp.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(','); } /** @@ -1491,6 +1616,7 @@ sharkd_session_process_tap_rtp_analyse_cb(void *tapdata) * (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 @@ -1503,6 +1629,7 @@ sharkd_session_process_tap_rtp_analyse_cb(void *tapdata) * (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 @@ -1523,21 +1650,24 @@ sharkd_session_process_tap_conv_cb(void *arg) 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 = ""; } @@ -1552,33 +1682,33 @@ sharkd_session_process_tap_conv_cb(void *arg) 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->ptype, iu->resolve_port))); - printf(",\"dport\":\"%s\"", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->ptype, 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); } @@ -1590,7 +1720,7 @@ sharkd_session_process_tap_conv_cb(void *arg) 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)) @@ -1601,27 +1731,27 @@ sharkd_session_process_tap_conv_cb(void *arg) 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->ptype, 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); } @@ -1629,11 +1759,16 @@ sharkd_session_process_tap_conv_cb(void *arg) 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 @@ -1670,103 +1805,94 @@ sharkd_session_free_tap_conv_cb(void *arg) static void sharkd_session_process_tap_nstat_cb(void *arg) { - new_stat_data_t *stat_data = (new_stat_data_t *) arg; + 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; - field_data = new_stat_tap_get_field_data(table, j, 0); + field_data = stat_tap_get_field_data(table, j, 0); 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 = new_stat_tap_get_field_data(table, j, k); - - if (k) - printf(","); + field_data = stat_tap_get_field_data(table, j, k); 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.uint_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 sharkd_session_free_tap_nstat_cb(void *arg) { - new_stat_data_t *stat_data = (new_stat_data_t *) arg; + stat_data_t *stat_data = (stat_data_t *) arg; - free_stat_tables(stat_data->stat_tap_data, NULL, NULL); + free_stat_tables(stat_data->stat_tap_data); } /** @@ -1809,21 +1935,23 @@ sharkd_session_process_tap_rtd_cb(void *arg) * (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]; @@ -1835,36 +1963,38 @@ sharkd_session_process_tap_rtd_cb(void *arg) 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 @@ -1872,7 +2002,7 @@ sharkd_session_free_tap_rtd_cb(void *arg) { rtd_data_t *rtd_data = (rtd_data_t *) arg; - free_rtd_table(&rtd_data->stat_table, NULL, NULL); + free_rtd_table(&rtd_data->stat_table); g_free(rtd_data); } @@ -1905,42 +2035,35 @@ sharkd_session_process_tap_srt_cb(void *arg) 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 */ @@ -1949,27 +2072,30 @@ sharkd_session_process_tap_srt_cb(void *arg) 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 @@ -1978,7 +2104,7 @@ sharkd_session_free_tap_srt_cb(void *arg) srt_data_t *srt_data = (srt_data_t *) arg; register_srt_t *srt = (register_srt_t *) srt_data->user_data; - free_srt_table(srt, srt_data->srt_array, NULL, NULL); + free_srt_table(srt, srt_data->srt_array); g_array_free(srt_data->srt_array, TRUE); g_free(srt_data); } @@ -2012,50 +2138,46 @@ static void sharkd_session_process_tap_eo_cb(void *tapdata) { export_object_list_t *tap_object = (export_object_list_t *) tapdata; - struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list*) tap_object->gui_data; + struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) tap_object->gui_data; 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 @@ -2102,59 +2224,53 @@ sharkd_session_process_tap_rtp_cb(void *arg) 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) { - rtp_stream_info_t *streaminfo = (rtp_stream_info_t *) listx->data; - - char *src_addr, *dst_addr; - char *payload; - guint32 expected; + rtpstream_info_t *streaminfo = (rtpstream_info_t *) listx->data; + rtpstream_info_calc_t calc; - src_addr = address_to_display(NULL, &(streaminfo->src_addr)); - dst_addr = address_to_display(NULL, &(streaminfo->dest_addr)); - - if (streaminfo->payload_type_name != NULL) - payload = wmem_strdup(NULL, streaminfo->payload_type_name); - else - payload = val_to_str_ext_wmem(NULL, streaminfo->payload_type, &rtp_payload_type_short_vals_ext, "Unknown (%u)"); + rtpstream_info_calculate(streaminfo, &calc); - printf("%s{\"ssrc\":%u", sepa, streaminfo->ssrc); - printf(",\"payload\":\"%s\"", payload); + sharkd_json_object_open(sepa); - printf(",\"saddr\":\"%s\"", src_addr); - printf(",\"sport\":%u", streaminfo->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\"", dst_addr); - printf(",\"dport\":%u", streaminfo->dest_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", streaminfo->packet_count); + sharkd_json_value_anyf(TRUE, "pkts", "%u", calc.packet_count); - printf(",\"max_delta\":%f", streaminfo->rtp_stats.max_delta); - printf(",\"max_jitter\":%f", streaminfo->rtp_stats.max_jitter); - printf(",\"mean_jitter\":%f", streaminfo->rtp_stats.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); - expected = (streaminfo->rtp_stats.stop_seq_nr + streaminfo->rtp_stats.cycles * 65536) - streaminfo->rtp_stats.start_seq_nr + 1; - printf(",\"expectednr\":%u", expected); - printf(",\"totalnr\":%u", streaminfo->rtp_stats.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", streaminfo->problem ? "true" : "false"); + sharkd_json_value_anyf(TRUE, "problem", calc.problem ? "true" : "false"); /* for filter */ - printf(",\"ipver\":%d", (streaminfo->src_addr.type == AT_IPv6) ? 6 : 4); + sharkd_json_value_anyf(TRUE, "ipver", "%d", (streaminfo->id.src_addr.type == AT_IPv6) ? 6 : 4); - wmem_free(NULL, src_addr); - wmem_free(NULL, dst_addr); - wmem_free(NULL, payload); + rtpstream_info_calc_free(&calc); - printf("}"); - sepa = ","; + sharkd_json_object_close(); + sepa = TRUE; } - printf("]},"); + sharkd_json_array_close(); + + sharkd_json_object_close(); + putchar(','); } /** @@ -2194,7 +2310,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) int i; rtpstream_tapinfo_t rtp_tapinfo = - {NULL, NULL, NULL, NULL, 0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE}; + { NULL, NULL, NULL, NULL, 0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE }; for (i = 0; i < 16; i++) { @@ -2224,7 +2340,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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); @@ -2239,7 +2355,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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; @@ -2268,7 +2384,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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; @@ -2316,15 +2432,15 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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; } else if (!strncmp(tok_tap, "nstat:", 6)) { - stat_tap_table_ui *stat_tap = new_stat_tap_by_name(tok_tap + 6); - new_stat_data_t *stat_data; + stat_tap_table_ui *stat_tap = stat_tap_by_name(tok_tap + 6); + stat_data_t *stat_data; if (!stat_tap) { @@ -2332,13 +2448,13 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) continue; } - stat_tap->stat_tap_init_cb(stat_tap, NULL, NULL); + stat_tap->stat_tap_init_cb(stat_tap); - stat_data = g_new0(new_stat_data_t, 1); + stat_data = g_new0(stat_data_t, 1); 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; @@ -2367,7 +2483,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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; @@ -2395,9 +2511,9 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) srt_data = g_new0(srt_data_t, 1); srt_data->srt_array = g_array_new(FALSE, TRUE, sizeof(srt_stat_table *)); srt_data->user_data = srt; - srt_table_dissector_init(srt, srt_data->srt_array, NULL, NULL); + 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; @@ -2439,14 +2555,14 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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, 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; @@ -2456,8 +2572,9 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) struct sharkd_analyse_rtp *rtp_req; rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req)); - if (!sharkd_rtp_match_init(&rtp_req->rtp, tok_tap + 12)) + if (!sharkd_rtp_match_init(&rtp_req->id, tok_tap + 12)) { + rtpstream_id_free(&rtp_req->id); g_free(rtp_req); continue; } @@ -2466,7 +2583,7 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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; @@ -2495,9 +2612,19 @@ sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count) 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++) { @@ -2559,7 +2686,7 @@ sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count) 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); @@ -2570,76 +2697,69 @@ sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count) 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); - - printf("\"n\":%u", follow_record->packet_num); + sharkd_json_object_open(sepa); - 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); @@ -2647,24 +2767,22 @@ sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbu 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) @@ -2675,17 +2793,17 @@ sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbu { 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) @@ -2694,50 +2812,58 @@ sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbu 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 (((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 @@ -2757,9 +2883,10 @@ sharkd_follower_visit_layers_cb(const void *key _U_, void *value, void *user_dat 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); } @@ -2767,6 +2894,11 @@ sharkd_follower_visit_layers_cb(const void *key _U_, void *value, void *user_dat 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) { @@ -2774,27 +2906,25 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep 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_phdr_comment) - pkt_comment = pi->phdr->opt_comment; + if (fdata->has_user_comment) + pkt_comment = sharkd_get_user_comment(fdata); + 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 */) { @@ -2805,7 +2935,7 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep for (i = 0; i < count; i++) { - struct data_source *src = (struct data_source *) g_slist_nth_data((GSList *) data_src, i); + const struct data_source *src = (const struct data_source *) g_slist_nth_data((GSList *) data_src, i); tvbs[i] = get_data_source_tvb(src); } @@ -2813,7 +2943,8 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep 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); } @@ -2822,20 +2953,33 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep { 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; + struct data_source *src = (struct data_source *) data_src->data; + gboolean ds_sepa = FALSE; + gboolean ds_open = FALSE; tvbuff_t *tvb; guint length; @@ -2843,70 +2987,281 @@ sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct ep 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; + 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(); + + 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 */ + +struct sharkd_iograph +{ + /* config */ + int hf_index; + io_graph_item_unit_t calc_type; + guint32 interval; + + /* result */ + int space_items; + int num_items; + io_graph_item_t *items; + GString *error; +}; + +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 TAP_PACKET_DONT_REDRAW; + + if (idx + 1 > graph->num_items) + { + if (idx + 1 > graph->space_items) + { + int new_size = idx + 1024; + + graph->items = (io_graph_item_t *) g_realloc(graph->items, sizeof(io_graph_item_t) * new_size); + reset_io_graph_items(&graph->items[graph->space_items], new_size - graph->space_items); + + graph->space_items = new_size; + } + else if (graph->items == NULL) + { + graph->items = (io_graph_item_t *) g_malloc(sizeof(io_graph_item_t) * graph->space_items); + reset_io_graph_items(graph->items, graph->space_items); + } + + graph->num_items = idx + 1; + } + + 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; +} + +/** + * sharkd_session_process_iograph() + * + * Process iograph request + * + * Input: + * (o) interval - interval time in ms, if not specified: 1000ms + * (m) graph0 - First graph request + * (o) graph1...graph9 - Other graph requests + * (o) filter0 - First graph filter + * (o) filter1...filter9 - Other graph filters + * + * Graph requests can be one of: "packets", "bytes", "bits", "sum:", "frames:", "max:", "min:", "avg:", "load:", + * if you use variant with , you need to pass field name in filter request. + * + * Output object with attributes: + * (m) iograph - array of graph results with attributes: + * errmsg - graph cannot be constructed + * items - graph values, zeros are skipped, if value is not a number it's next index encoded as hex string + */ +static void +sharkd_session_process_iograph(char *buf, const jsmntok_t *tokens, int count) +{ + const char *tok_interval = json_find_attr(buf, tokens, count, "interval"); + struct sharkd_iograph graphs[10]; + gboolean is_any_ok = FALSE; + int graph_count; + + guint32 interval_ms = 1000; /* default: one per second */ + int i; + + if (tok_interval) + { + if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0) + { + fprintf(stderr, "Invalid interval parameter: %s.\n", tok_interval); + return; + } + } + + for (i = graph_count = 0; i < (int) G_N_ELEMENTS(graphs); i++) + { + struct sharkd_iograph *graph = &graphs[graph_count]; + + const char *tok_graph; + const char *tok_filter; + char tok_format_buf[32]; + const char *field_name; + + snprintf(tok_format_buf, sizeof(tok_format_buf), "graph%d", i); + tok_graph = json_find_attr(buf, tokens, count, tok_format_buf); + if (!tok_graph) + break; + + snprintf(tok_format_buf, sizeof(tok_format_buf), "filter%d", i); + tok_filter = json_find_attr(buf, tokens, count, tok_format_buf); + + if (!strcmp(tok_graph, "packets")) + graph->calc_type = IOG_ITEM_UNIT_PACKETS; + else if (!strcmp(tok_graph, "bytes")) + graph->calc_type = IOG_ITEM_UNIT_BYTES; + else if (!strcmp(tok_graph, "bits")) + graph->calc_type = IOG_ITEM_UNIT_BITS; + else if (g_str_has_prefix(tok_graph, "sum:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_SUM; + else if (g_str_has_prefix(tok_graph, "frames:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_FRAMES; + else if (g_str_has_prefix(tok_graph, "fields:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_FIELDS; + else if (g_str_has_prefix(tok_graph, "max:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_MAX; + else if (g_str_has_prefix(tok_graph, "min:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_MIN; + else if (g_str_has_prefix(tok_graph, "avg:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_AVERAGE; + else if (g_str_has_prefix(tok_graph, "load:")) + graph->calc_type = IOG_ITEM_UNIT_CALC_LOAD; + else + break; + + field_name = strchr(tok_graph, ':'); + if (field_name) + field_name = field_name + 1; + + graph->interval = interval_ms; + + graph->hf_index = -1; + graph->error = check_field_unit(field_name, &graph->hf_index, graph->calc_type); + + graph->space_items = 0; /* TODO, can avoid realloc()s in sharkd_iograph_packet() by calculating: capture_time / interval */ + graph->num_items = 0; + graph->items = NULL; + + if (!graph->error) + graph->error = register_tap_listener("frame", graph, tok_filter, TL_REQUIRES_PROTO_TREE, NULL, sharkd_iograph_packet, NULL, NULL); + + graph_count++; + + if (graph->error == NULL) + is_any_ok = TRUE; + } + + /* retap only if we have at least one ok */ + if (is_any_ok) + sharkd_retap(); + + sharkd_json_object_open(FALSE); + + sharkd_json_array_open(FALSE, "iograph"); + for (i = 0; i < graph_count; i++) + { + struct sharkd_iograph *graph = &graphs[i]; + + sharkd_json_object_open(i != 0); + + if (graph->error) + { + sharkd_json_value_string(FALSE, "errmsg", graph->error->str); + g_string_free(graph->error, TRUE); + } + else + { + int idx; + int next_idx = 0; + + sharkd_json_array_open(FALSE, "items"); + for (idx = 0; idx < graph->num_items; idx++) + { + double val; + + val = get_io_graph_item(graph->items, graph->calc_type, idx, graph->hf_index, &cfile, graph->interval, graph->num_items); + + /* if it's zero, don't display */ + if (val == 0.0) + continue; - printf("}\n"); + /* cause zeros are not printed, need to output index */ + if (next_idx != idx) + sharkd_json_value_stringf(next_idx != 0, NULL, "%x", idx); + + sharkd_json_value_anyf(idx != 0, NULL, "%f", val); + next_idx = idx + 1; + } + sharkd_json_array_close(); + } + sharkd_json_object_close(); + + remove_tap_listener(graph); + g_free(graph->items); + } + sharkd_json_array_close(); + + sharkd_json_object_close(); + sharkd_json_finish(); } /** @@ -2944,17 +3299,19 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) guint64 bytes; } st, st_total; - nstime_t *start_ts = NULL; + nstime_t *start_ts; 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_interval) { - if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0) { + if (tok_interval) + { + if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0) + { fprintf(stderr, "Invalid interval parameter: %s.\n", tok_interval); return; } @@ -2962,9 +3319,12 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) 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; @@ -2975,20 +3335,22 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) 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; for (framenum = 1; framenum <= cfile.count; framenum++) { - frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum); + frame_data *fdata; gint64 msec_rel; gint64 new_idx; - if (start_ts == NULL) - start_ts = &fdata->abs_ts; - if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8)))) continue; + fdata = sharkd_get_frame(framenum); + msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (gint64) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000; new_idx = msec_rel / interval_ms; @@ -2996,8 +3358,8 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) { 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; @@ -3017,11 +3379,17 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) 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(); } /** @@ -3031,9 +3399,13 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) * * Input: * (m) frame - requested frame number + * (o) ref_frame - time reference frame number + * (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 @@ -3050,6 +3422,8 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) * 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 @@ -3058,21 +3432,45 @@ sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count) * (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"); - 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); + 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"); - guint32 framenum; + 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; - sharkd_dissect_request(framenum, &sharkd_session_process_frame_cb, tok_bytes, tok_columns, tok_proto, NULL); + ref_frame_num = (framenum != 1) ? 1 : 0; + if (tok_ref_frame && (!ws_strtou32(tok_ref_frame, NULL, &ref_frame_num) || ref_frame_num > framenum)) + return; + + prev_dis_num = framenum - 1; + if (tok_prev_frame && (!ws_strtou32(tok_prev_frame, NULL, &prev_dis_num) || prev_dis_num >= framenum)) + return; + + 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); } /** @@ -3082,17 +3480,22 @@ sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count) * * 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; @@ -3102,21 +3505,29 @@ sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count) { 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; } @@ -3124,7 +3535,7 @@ struct sharkd_session_process_complete_pref_data { const char *module; const char *pref; - const char *sepa; + gboolean sepa; }; static guint @@ -3135,8 +3546,12 @@ sharkd_session_process_complete_pref_cb(module_t *module, gpointer d) 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; } @@ -3151,8 +3566,12 @@ sharkd_session_process_complete_pref_option_cb(pref_t *pref, gpointer d) 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 */ } @@ -3182,7 +3601,9 @@ sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count) 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); @@ -3191,9 +3612,9 @@ sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count) 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)) { @@ -3210,16 +3631,14 @@ sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count) 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) @@ -3232,26 +3651,24 @@ sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count) 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]) @@ -3261,10 +3678,9 @@ sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count) 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; @@ -3282,15 +3698,49 @@ sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count) { 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; } +/** + * sharkd_session_process_setcomment() + * + * Process setcomment request + * + * Input: + * (m) frame - frame number + * (o) comment - user comment + * + * Output object with attributes: + * (m) err - error code: 0 succeed + */ +static void +sharkd_session_process_setcomment(char *buf, const jsmntok_t *tokens, int count) +{ + const char *tok_frame = json_find_attr(buf, tokens, count, "frame"); + const char *tok_comment = json_find_attr(buf, tokens, count, "comment"); + + guint32 framenum; + frame_data *fdata; + int ret; + + if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0) + return; + + fdata = sharkd_get_frame(framenum); + if (!fdata) + return; + + ret = sharkd_set_user_comment(fdata, tok_comment); + + sharkd_json_simple_reply(ret, NULL); +} + /** * sharkd_session_process_setconf() * @@ -3319,21 +3769,15 @@ sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count) 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 @@ -3342,49 +3786,52 @@ sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d) 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; } @@ -3392,7 +3839,7 @@ sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d) 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; } @@ -3402,31 +3849,25 @@ sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d) 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; } @@ -3439,12 +3880,11 @@ sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d) } #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 */ } @@ -3491,11 +3931,17 @@ sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count) struct sharkd_session_process_dumpconf_data data; data.module = NULL; - 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_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data); - printf("}}\n"); + sharkd_json_object_close(); + + sharkd_json_object_close(); + sharkd_json_finish(); return; } @@ -3514,11 +3960,17 @@ sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count) 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; @@ -3530,17 +3982,23 @@ sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count) 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(); } } struct sharkd_download_rtp { - struct sharkd_rtp_match rtp; + rtpstream_id_t id; GSList *packets; double start_time; }; @@ -3692,7 +4150,7 @@ sharkd_rtp_download_decode(struct sharkd_download_rtp *req) 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; @@ -3700,9 +4158,9 @@ sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epa /* 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 (sharkd_rtp_match_check(&req_rtp->rtp, pinfo, rtp_info)) + if (rtpstream_id_equal_pinfo_rtp_info(&req_rtp->id, pinfo, rtp_info)) { rtp_packet_t *rtp_packet; @@ -3722,7 +4180,7 @@ sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epa req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet); } - return FALSE; + return TAP_PACKET_DONT_REDRAW; } /** @@ -3772,13 +4230,12 @@ sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count) 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, 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")) @@ -3790,13 +4247,12 @@ sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count) 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); } @@ -3806,13 +4262,13 @@ sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count) GString *tap_error; memset(&rtp_req, 0, sizeof(rtp_req)); - if (!sharkd_rtp_match_init(&rtp_req.rtp, tok_token + 4)) + if (!sharkd_rtp_match_init(&rtp_req.id, tok_token + 4)) { fprintf(stderr, "sharkd_session_process_download() rtp tokenizing error %s\n", tok_token); 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); @@ -3828,17 +4284,17 @@ sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count) 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); } @@ -3885,7 +4341,7 @@ sharkd_session_process(char *buf, const jsmntok_t *tokens, int count) 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; @@ -3919,10 +4375,14 @@ sharkd_session_process(char *buf, const jsmntok_t *tokens, int count) sharkd_session_process_tap(buf, tokens, count); else if (!strcmp(tok_req, "follow")) sharkd_session_process_follow(buf, tokens, count); + else if (!strcmp(tok_req, "iograph")) + sharkd_session_process_iograph(buf, tokens, count); else if (!strcmp(tok_req, "intervals")) sharkd_session_process_intervals(buf, tokens, count); else if (!strcmp(tok_req, "frame")) sharkd_session_process_frame(buf, tokens, count); + else if (!strcmp(tok_req, "setcomment")) + sharkd_session_process_setcomment(buf, tokens, count); else if (!strcmp(tok_req, "setconf")) sharkd_session_process_setconf(buf, tokens, count); else if (!strcmp(tok_req, "dumpconf")) @@ -3935,7 +4395,7 @@ sharkd_session_process(char *buf, const jsmntok_t *tokens, int count) 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 @@ -3964,12 +4424,19 @@ sharkd_session_main(void) fprintf(stderr, "Hello in child.\n"); + filter_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, sharkd_session_filter_free); + +#ifdef HAVE_MAXMINDDB + /* mmdbresolve was stopped before fork(), force starting it */ + uat_get_table_by_name("MaxMind Database Paths")->post_update_cb(); +#endif + while (fgets(buf, sizeof(buf), stdin)) { /* every command is line seperated JSON */ int ret; - ret = wsjsmn_parse(buf, NULL, 0); + ret = json_parse(buf, NULL, 0); if (ret < 0) { fprintf(stderr, "invalid JSON -> closing\n"); @@ -3987,16 +4454,21 @@ sharkd_session_main(void) memset(tokens, 0, ret * sizeof(jsmntok_t)); - ret = wsjsmn_parse(buf, tokens, ret); + ret = json_parse(buf, tokens, ret); if (ret < 0) { fprintf(stderr, "invalid JSON(2) -> closing\n"); return 2; } +#if defined(HAVE_C_ARES) || defined(HAVE_MAXMINDDB) + host_name_lookup_process(); +#endif + sharkd_session_process(buf, tokens, ret); } + g_hash_table_destroy(filter_table); g_free(tokens); return 0;