3 * Copyright (C) 2016 Jakub Zawadzki
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <wsutil/wsjsmn.h>
34 #include <wsutil/ws_printf.h>
37 #include <epan/exceptions.h>
38 #include <epan/color_filters.h>
39 #include <epan/prefs.h>
40 #include <epan/prefs-int.h>
41 #include <epan/uat-int.h>
42 #include <wiretap/wtap.h>
44 #include <epan/column.h>
46 #include <ui/ssl_key_export.h>
48 #include <epan/stats_tree_priv.h>
49 #include <epan/stat_tap_ui.h>
50 #include <epan/conversation_table.h>
51 #include <epan/expert.h>
52 #include <epan/export_object.h>
53 #include <epan/follow.h>
54 #include <epan/rtd_table.h>
55 #include <epan/srt_table.h>
57 #include <epan/dissectors/packet-h225.h>
58 #include <epan/rtp_pt.h>
59 #include <ui/voip_calls.h>
60 #include <ui/rtp_stream.h>
61 #include <ui/tap-rtp-common.h>
62 #include <epan/to_str.h>
64 #include <epan/addr_resolv.h>
65 #include <epan/dissectors/packet-rtp.h>
66 #include <ui/rtp_media.h>
67 #include <codecs/speex/speex_resampler.h>
71 # include <epan/geoip_db.h>
72 # include <wsutil/pint.h>
75 #include <wsutil/glib-compat.h>
76 #include <wsutil/strtoi.h>
81 json_unescape_str(char *input)
91 /* TODO, add more escaping rules */
103 json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
107 for (i = 0; i < count; i += 2)
109 const char *tok_attr = &buf[tokens[i + 0].start];
110 const char *tok_value = &buf[tokens[i + 1].start];
112 if (!strcmp(tok_attr, attr))
120 json_puts_string(const char *str)
128 for (i = 0; str[i]; i++)
153 json_print_base64_step(const guint8 *data, int *state1, int *state2)
155 gchar buf[(1 / 3 + 1) * 4 + 4 + 1];
159 wrote = g_base64_encode_step(data, 1, FALSE, buf, state1, state2);
161 wrote = g_base64_encode_close(FALSE, buf, state1, state2);
171 json_print_base64(const guint8 *data, size_t len)
174 int base64_state1 = 0;
175 int base64_state2 = 0;
179 for (i = 0; i < len; i++)
180 json_print_base64_step(&data[i], &base64_state1, &base64_state2);
182 json_print_base64_step(NULL, &base64_state1, &base64_state2);
189 struct filter_item *next;
195 static struct filter_item *filter_list = NULL;
197 static const guint8 *
198 sharkd_session_filter_data(const char *filter)
200 struct filter_item *l;
202 for (l = filter_list; l; l = l->next)
204 if (!strcmp(l->filter, filter))
209 guint8 *filtered = NULL;
211 int ret = sharkd_filter(filter, &filtered);
216 l = (struct filter_item *) g_malloc(sizeof(struct filter_item));
217 l->filter = g_strdup(filter);
218 l->filtered = filtered;
220 l->next = filter_list;
227 struct sharkd_rtp_match
229 guint32 addr_src, addr_dst;
238 sharkd_rtp_match_init(struct sharkd_rtp_match *req, const char *init_str)
240 gboolean ret = FALSE;
243 arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
244 if (g_strv_length(arr) != 5)
247 /* TODO, for now only IPv4 */
248 if (!get_host_ipaddr(arr[0], &req->addr_src))
251 if (!ws_strtou16(arr[1], NULL, &req->src_port))
254 if (!get_host_ipaddr(arr[2], &req->addr_dst))
257 if (!ws_strtou16(arr[3], NULL, &req->dst_port))
260 if (!ws_hexstrtou32(arr[4], NULL, &req->ssrc))
263 set_address(&req->src_addr, AT_IPv4, 4, &req->addr_src);
264 set_address(&req->dst_addr, AT_IPv4, 4, &req->addr_dst);
273 sharkd_rtp_match_check(const struct sharkd_rtp_match *req, const packet_info *pinfo, const struct _rtp_info *rtp_info)
275 if (rtp_info->info_sync_src == req->ssrc &&
276 pinfo->srcport == req->src_port &&
277 pinfo->destport == req->dst_port &&
278 addresses_equal(&pinfo->src, &req->src_addr) &&
279 addresses_equal(&pinfo->dst, &req->dst_addr))
288 sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata)
290 stat_tap_table_ui *new_stat_tap = (stat_tap_table_ui *) value;
291 int *pi = (int *) userdata;
293 printf("%s{", (*pi) ? "," : "");
294 printf("\"name\":\"%s\"", new_stat_tap->title);
295 printf(",\"tap\":\"nstat:%s\"", (const char *) key);
303 sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata)
305 struct register_ct *table = (struct register_ct *) value;
306 int *pi = (int *) userdata;
308 const char *label = (const char*)key;
310 if (get_conversation_packet_func(table))
312 printf("%s{", (*pi) ? "," : "");
313 printf("\"name\":\"Conversation List/%s\"", label);
314 printf(",\"tap\":\"conv:%s\"", label);
320 if (get_hostlist_packet_func(table))
322 printf("%s{", (*pi) ? "," : "");
323 printf("\"name\":\"Endpoint/%s\"", label);
324 printf(",\"tap\":\"endpt:%s\"", label);
333 sharkd_export_object_visit_cb(const void *key _U_, void *value, void *user_data)
335 register_eo_t *eo = (register_eo_t*)value;
336 int *pi = (int *) user_data;
338 const int proto_id = get_eo_proto_id(eo);
339 const char *filter = proto_get_protocol_filter_name(proto_id);
340 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
342 printf("%s{", (*pi) ? "," : "");
343 printf("\"name\":\"Export Object/%s\"", label);
344 printf(",\"tap\":\"eo:%s\"", filter);
352 sharkd_srt_visit_cb(const void *key _U_, void *value, void *user_data)
354 register_srt_t *srt = (register_srt_t *) value;
355 int *pi = (int *) user_data;
357 const int proto_id = get_srt_proto_id(srt);
358 const char *filter = proto_get_protocol_filter_name(proto_id);
359 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
361 printf("%s{", (*pi) ? "," : "");
362 printf("\"name\":\"Service Response Time/%s\"", label);
363 printf(",\"tap\":\"srt:%s\"", filter);
371 sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data)
373 register_rtd_t *rtd = (register_rtd_t *) value;
374 int *pi = (int *) user_data;
376 const int proto_id = get_rtd_proto_id(rtd);
377 const char *filter = proto_get_protocol_filter_name(proto_id);
378 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
380 printf("%s{", (*pi) ? "," : "");
381 printf("\"name\":\"Response Time Delay/%s\"", label);
382 printf(",\"tap\":\"rtd:%s\"", filter);
390 sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data)
392 register_follow_t *follower = (register_follow_t*) value;
393 int *pi = (int *) user_data;
395 const int proto_id = get_follow_proto_id(follower);
396 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
397 const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
399 printf("%s{", (*pi) ? "," : "");
400 printf("\"name\":\"Follow/%s\"", label);
401 printf(",\"tap\":\"follow:%s\"", filter);
409 * sharkd_session_process_info()
411 * Process info request
413 * Output object with attributes:
414 * (m) columns - available column formats, array of object with attributes:
415 * 'name' - column name
416 * 'format' - column format-name
418 * (m) stats - available statistics, array of object with attributes:
419 * 'name' - statistic name
420 * 'tap' - sharkd tap-name for statistic
422 * (m) convs - available conversation list, array of object with attributes:
423 * 'name' - conversation name
424 * 'tap' - sharkd tap-name for conversation
426 * (m) eo - available export object list, array of object with attributes:
427 * 'name' - export object name
428 * 'tap' - sharkd tap-name for eo
430 * (m) srt - available service response time list, array of object with attributes:
431 * 'name' - service response time name
432 * 'tap' - sharkd tap-name for srt
434 * (m) rtd - available response time delay list, array of object with attributes:
435 * 'name' - response time delay name
436 * 'tap' - sharkd tap-name for rtd
438 * (m) taps - available taps, array of object with attributes:
440 * 'tap' - sharkd tap-name
442 * (m) follow - available followers, array of object with attributes:
444 * 'tap' - sharkd tap-name
446 * (m) ftypes - conversation table for FT_ number to string
449 sharkd_session_process_info(void)
453 printf("{\"columns\":[");
454 for (i = 0; i < NUM_COL_FMTS; i++)
456 const char *col_format = col_format_to_string(i);
457 const char *col_descr = col_format_desc(i);
459 printf("%s{", (i) ? "," : "");
460 printf("\"name\":\"%s\"", col_descr);
461 printf(",\"format\":\"%s\"", col_format);
466 printf(",\"stats\":[");
468 GList *cfg_list = stats_tree_get_cfg_list();
470 const char *sepa = "";
472 for (l = cfg_list; l; l = l->next)
474 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
477 printf("\"name\":\"%s\"", cfg->name);
478 printf(",\"tap\":\"stat:%s\"", cfg->abbr);
483 g_list_free(cfg_list);
487 printf(",\"ftypes\":[");
488 for (i = 0; i < FT_NUM_TYPES; i++)
492 json_puts_string(ftype_name((ftenum_t) i));
496 printf(",\"version\":");
497 json_puts_string(sharkd_version());
499 printf(",\"nstat\":[");
501 new_stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
504 printf(",\"convs\":[");
506 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
509 printf(",\"taps\":[");
511 printf("{\"name\":\"%s\",\"tap\":\"%s\"}", "RTP streams", "rtp-streams");
512 printf(",{\"name\":\"%s\",\"tap\":\"%s\"}", "Expert Information", "expert");
518 eo_iterate_tables(sharkd_export_object_visit_cb, &i);
521 printf(",\"srt\":[");
523 srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
526 printf(",\"rtd\":[");
528 rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
531 printf(",\"follow\":[");
533 follow_iterate_followers(sharkd_follower_visit_cb, &i);
540 * sharkd_session_process_load()
542 * Process load request
545 * (m) file - file to be loaded
547 * Output object with attributes:
548 * (m) err - error code
551 sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
553 const char *tok_file = json_find_attr(buf, tokens, count, "file");
556 fprintf(stderr, "load: filename=%s\n", tok_file);
561 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK)
563 printf("{\"err\":%d}\n", err);
569 err = sharkd_load_cap_file();
571 CATCH(OutOfMemoryError)
573 fprintf(stderr, "load: OutOfMemoryError\n");
578 printf("{\"err\":%d}\n", err);
582 * sharkd_session_process_status()
584 * Process status request
586 * Output object with attributes:
587 * (m) frames - count of currently loaded frames
590 sharkd_session_process_status(void)
592 printf("{\"frames\":%u", cfile.count);
597 struct sharkd_analyse_data
599 GHashTable *protocols_set;
600 nstime_t *first_time;
605 sharkd_session_process_analyse_cb(packet_info *pi, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
607 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
608 frame_data *fdata = pi->fd;
614 if (analyser->first_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
615 analyser->first_time = &fdata->abs_ts;
617 if (analyser->last_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
618 analyser->last_time = &fdata->abs_ts;
622 wmem_list_frame_t *frame;
624 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
626 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame));
628 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id), NULL, NULL))
630 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id), GUINT_TO_POINTER(proto_id));
632 if (g_hash_table_size(analyser->protocols_set) != 1)
634 json_puts_string(proto_get_protocol_filter_name(proto_id));
642 * sharkd_session_process_status()
644 * Process analyse request
646 * Output object with attributes:
647 * (m) frames - count of currently loaded frames
648 * (m) protocols - protocol list
649 * (m) first - earliest frame time
650 * (m) last - latest frame time
653 sharkd_session_process_analyse(void)
655 unsigned int framenum;
656 struct sharkd_analyse_data analyser;
658 analyser.first_time = NULL;
659 analyser.last_time = NULL;
660 analyser.protocols_set = g_hash_table_new(NULL /* g_direct_hash() */, NULL /* g_direct_equal */);
662 printf("{\"frames\":%u", cfile.count);
664 printf(",\"protocols\":[");
665 for (framenum = 1; framenum <= cfile.count; framenum++)
666 sharkd_dissect_request(framenum, &sharkd_session_process_analyse_cb, 0, 0, 0, &analyser);
669 if (analyser.first_time)
670 printf(",\"first\":%.9f", nstime_to_sec(analyser.first_time));
672 if (analyser.last_time)
673 printf(",\"last\":%.9f", nstime_to_sec(analyser.last_time));
677 g_hash_table_destroy(analyser.protocols_set);
681 * sharkd_session_process_frames()
683 * Process frames request
686 * (o) filter - filter to be used
687 * (o) skip=N - skip N frames
688 * (o) limit=N - show only N frames
690 * Output array of frames with attributes:
691 * (m) c - array of column data
692 * (m) num - frame number
693 * (m) i - if frame is ignored
694 * (m) m - if frame is marked
695 * (m) bg - color filter - background color in hex
696 * (m) fg - color filter - foreground color in hex
699 sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
701 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
702 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
703 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
705 const guint8 *filter_data = NULL;
707 const char *frame_sepa = "";
714 column_info *cinfo = &cfile.cinfo;
718 filter_data = sharkd_session_filter_data(tok_filter);
726 if (!ws_strtou32(tok_skip, NULL, &skip))
733 if (!ws_strtou32(tok_limit, NULL, &limit))
738 for (framenum = 1; framenum <= cfile.count; framenum++)
740 frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum);
742 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
751 sharkd_dissect_columns(framenum, cinfo, (fdata->color_filter == NULL));
753 printf("%s{\"c\":[", frame_sepa);
754 for (col = 0; col < cinfo->num_cols; ++col)
756 const col_item_t *col_item = &cinfo->columns[col];
761 json_puts_string(col_item->col_data);
763 printf("],\"num\":%u", framenum);
765 if (fdata->flags.ignored)
766 printf(",\"i\":true");
768 if (fdata->flags.marked)
769 printf(",\"m\":true");
771 if (fdata->color_filter)
773 printf(",\"bg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->bg_color));
774 printf(",\"fg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->fg_color));
780 if (limit && --limit == 0)
785 if (cinfo != &cfile.cinfo)
790 sharkd_session_process_tap_stats_node_cb(const stat_node *n)
793 const char *sepa = "";
796 for (node = n->children; node; node = node->next)
798 /* code based on stats_tree_get_values_from_node() */
799 printf("%s{\"name\":\"%s\"", sepa, node->name);
800 printf(",\"count\":%d", node->counter);
801 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE) || node->rng))
803 printf(",\"avg\":%.2f", ((float)node->total) / node->counter);
804 printf(",\"min\":%d", node->minvalue);
805 printf(",\"max\":%d", node->maxvalue);
808 if (node->st->elapsed)
809 printf(",\"rate\":%.4f",((float)node->counter) / node->st->elapsed);
811 if (node->parent && node->parent->counter)
812 printf(",\"perc\":%.2f", (node->counter * 100.0) / node->parent->counter);
813 else if (node->parent == &(node->st->root))
814 printf(",\"perc\":100");
816 if (prefs.st_enable_burstinfo && node->max_burst)
818 if (prefs.st_burst_showcount)
819 printf(",\"burstcount\":%d", node->max_burst);
821 printf(",\"burstrate\":%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
823 printf(",\"bursttime\":%.3f", ((double)node->burst_time / 1000.0));
829 sharkd_session_process_tap_stats_node_cb(node);
838 * sharkd_session_process_tap_stats_cb()
843 * (m) type:stats - tap output type
844 * (m) name - stat name
845 * (m) stats - array of object with attributes:
846 * (m) name - stat item name
847 * (m) count - stat item counter
848 * (o) avg - stat item averange value
849 * (o) min - stat item min value
850 * (o) max - stat item max value
851 * (o) rate - stat item rate value (ms)
852 * (o) perc - stat item percentage
853 * (o) burstrate - stat item burst rate
854 * (o) burstcount - stat item burst count
855 * (o) burstttme - stat item burst start
856 * (o) sub - array of object with attributes like in stats node.
859 sharkd_session_process_tap_stats_cb(void *psp)
861 stats_tree *st = (stats_tree *) psp;
863 printf("{\"tap\":\"stats:%s\",\"type\":\"stats\"", st->cfg->abbr);
865 printf(",\"name\":\"%s\",\"stats\":", st->cfg->name);
866 sharkd_session_process_tap_stats_node_cb(&st->root);
871 sharkd_session_free_tap_stats_cb(void *psp)
873 stats_tree *st = (stats_tree *) psp;
878 struct sharkd_expert_tap
885 * sharkd_session_process_tap_expert_cb()
890 * (m) type:expert - tap output type
891 * (m) details - array of object with attributes:
892 * (m) f - frame number, which generated expert information
895 * (m) m - expert message
899 sharkd_session_process_tap_expert_cb(void *tapdata)
901 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
903 const char *sepa = "";
905 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "expert", "expert");
907 printf(",\"details\":[");
908 for (list = etd->details; list; list = list->next)
910 expert_info_t *ei = (expert_info_t *) list->data;
915 printf("\"f\":%u,", ei->packet_num);
917 tmp = try_val_to_str(ei->severity, expert_severity_vals);
919 printf("\"s\":\"%s\",", tmp);
921 tmp = try_val_to_str(ei->group, expert_group_vals);
923 printf("\"g\":\"%s\",", tmp);
926 json_puts_string(ei->summary);
932 json_puts_string(ei->protocol);
944 sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pointer)
946 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
947 expert_info_t *ei = (expert_info_t *) pointer;
949 ei = (expert_info_t *) g_memdup(ei, sizeof(*ei));
950 ei->protocol = g_string_chunk_insert_const(etd->text, ei->protocol);
951 ei->summary = g_string_chunk_insert_const(etd->text, ei->summary);
953 etd->details = g_slist_prepend(etd->details, ei);
959 sharkd_session_free_tap_expert_cb(void *tapdata)
961 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
963 g_slist_free_full(etd->details, g_free);
964 g_string_chunk_free(etd->text);
968 struct sharkd_conv_tap_data
972 gboolean resolve_name;
973 gboolean resolve_port;
977 sharkd_session_geoip_addr(address *addr, const char *suffix)
985 if (addr->type == AT_IPv4)
987 guint32 ip = pntoh32(addr->data);
989 guint num_dbs = geoip_db_num_dbs();
992 for (dbnum = 0; dbnum < num_dbs; dbnum++)
994 const char *geoip_key = NULL;
997 int db_type = geoip_db_type(dbnum);
1001 case GEOIP_COUNTRY_EDITION:
1002 geoip_key = "geoip_country";
1005 case GEOIP_CITY_EDITION_REV0:
1006 case GEOIP_CITY_EDITION_REV1:
1007 geoip_key = "geoip_city";
1010 case GEOIP_ORG_EDITION:
1011 geoip_key = "geoip_org";
1014 case GEOIP_ISP_EDITION:
1015 geoip_key = "geoip_isp";
1018 case GEOIP_ASNUM_EDITION:
1019 geoip_key = "geoip_as";
1022 case WS_LAT_FAKE_EDITION:
1023 geoip_key = "geoip_lat";
1026 case WS_LON_FAKE_EDITION:
1027 geoip_key = "geoip_lon";
1031 if (geoip_key && (geoip_val = geoip_db_lookup_ipv4(dbnum, ip, NULL)))
1033 printf(",\"%s%s\":", geoip_key, suffix);
1034 json_puts_string(geoip_val);
1039 #ifdef HAVE_GEOIP_V6
1040 if (addr->type == AT_IPv6)
1042 const struct e_in6_addr *ip6 = (const struct e_in6_addr *) addr->data;
1044 guint num_dbs = geoip_db_num_dbs();
1047 for (dbnum = 0; dbnum < num_dbs; dbnum++)
1049 const char *geoip_key = NULL;
1052 int db_type = geoip_db_type(dbnum);
1056 case GEOIP_COUNTRY_EDITION_V6:
1057 geoip_key = "geoip_country";
1059 #if NUM_DB_TYPES > 31
1060 case GEOIP_CITY_EDITION_REV0_V6:
1061 case GEOIP_CITY_EDITION_REV1_V6:
1062 geoip_key = "geoip_city";
1065 case GEOIP_ORG_EDITION_V6:
1066 geoip_key = "geoip_org";
1069 case GEOIP_ISP_EDITION_V6:
1070 geoip_key = "geoip_isp";
1073 case GEOIP_ASNUM_EDITION_V6:
1074 geoip_key = "geoip_as";
1076 #endif /* DB_NUM_TYPES */
1077 case WS_LAT_FAKE_EDITION:
1078 geoip_key = "geoip_lat";
1081 case WS_LON_FAKE_EDITION:
1082 geoip_key = "geoip_lon";
1086 if (geoip_key && (geoip_val = geoip_db_lookup_ipv6(dbnum, *ip6, NULL)))
1088 printf(",\"%s%s\":", geoip_key, suffix);
1089 json_puts_string(geoip_val);
1094 #endif /* HAVE_GEOIP_V6 */
1095 #endif /* HAVE_GEOIP */
1100 struct sharkd_analyse_rtp_items
1103 guint32 sequence_num;
1111 double arrive_offset;
1113 /* from tap_rtp_stat_t */
1118 struct sharkd_analyse_rtp
1120 const char *tap_name;
1121 struct sharkd_rtp_match rtp;
1125 tap_rtp_stat_t statinfo;
1129 sharkd_session_process_tap_rtp_free_cb(void *tapdata)
1131 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1133 g_slist_free_full(rtp_req->packets, g_free);
1138 sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pointer)
1140 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1141 const struct _rtp_info *rtpinfo = (const struct _rtp_info *) pointer;
1143 if (sharkd_rtp_match_check(&rtp_req->rtp, pinfo, rtpinfo))
1145 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
1146 struct sharkd_analyse_rtp_items *item;
1148 rtp_packet_analyse(statinfo, pinfo, rtpinfo);
1150 item = (struct sharkd_analyse_rtp_items *) g_malloc(sizeof(struct sharkd_analyse_rtp_items));
1152 if (!rtp_req->packets)
1153 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
1155 item->frame_num = pinfo->num;
1156 item->sequence_num = rtpinfo->info_seq_num;
1157 item->delta = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->delta;
1158 item->jitter = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->jitter;
1159 item->skew = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->skew;
1160 item->bandwidth = statinfo->bandwidth;
1161 item->marker = rtpinfo->info_marker_set ? TRUE : FALSE;
1162 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
1164 item->flags = statinfo->flags;
1165 item->pt = statinfo->pt;
1167 /* XXX, O(n) optimize */
1168 rtp_req->packets = g_slist_append(rtp_req->packets, item);
1175 * sharkd_session_process_tap_rtp_analyse_cb()
1177 * Output rtp analyse tap:
1178 * (m) tap - tap name
1179 * (m) type - tap output type
1180 * (m) ssrc - RTP SSRC
1181 * (m) max_delta - Max delta (ms)
1182 * (m) max_delta_nr - Max delta packet #
1183 * (m) max_jitter - Max jitter (ms)
1184 * (m) mean_jitter - Mean jitter (ms)
1185 * (m) max_skew - Max skew (ms)
1186 * (m) total_nr - Total number of RTP packets
1187 * (m) seq_err - Number of sequence errors
1188 * (m) duration - Duration (ms)
1189 * (m) items - array of object with attributes:
1190 * (m) f - frame number
1191 * (m) o - arrive offset
1192 * (m) sn - sequence number
1196 * (m) bw - bandwidth
1197 * (o) s - status string
1198 * (o) t - status type
1199 * (o) mark - rtp mark
1202 sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
1204 const int RTP_TYPE_CN = 1;
1205 const int RTP_TYPE_ERROR = 2;
1206 const int RTP_TYPE_WARN = 3;
1207 const int RTP_TYPE_PT_EVENT = 4;
1209 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1210 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
1212 const char *sepa = "";
1215 printf("{\"tap\":\"%s\",\"type\":\"rtp-analyse\"", rtp_req->tap_name);
1217 printf(",\"ssrc\":%u", rtp_req->rtp.ssrc);
1219 printf(",\"max_delta\":%f", statinfo->max_delta);
1220 printf(",\"max_delta_nr\":%u", statinfo->max_nr);
1221 printf(",\"max_jitter\":%f", statinfo->max_jitter);
1222 printf(",\"mean_jitter\":%f", statinfo->mean_jitter);
1223 printf(",\"max_skew\":%f", statinfo->max_skew);
1224 printf(",\"total_nr\":%u", statinfo->total_nr);
1225 printf(",\"seq_err\":%u", statinfo->sequence);
1226 printf(",\"duration\":%f", statinfo->time - statinfo->start_time);
1228 printf(",\"items\":[");
1229 for (l = rtp_req->packets; l; l = l->next)
1231 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
1233 printf("%s{", sepa);
1235 printf("\"f\":%u", item->frame_num);
1236 printf(",\"o\":%.9f", item->arrive_offset);
1237 printf(",\"sn\":%u", item->sequence_num);
1238 printf(",\"d\":%.2f", item->delta);
1239 printf(",\"j\":%.2f", item->jitter);
1240 printf(",\"sk\":%.2f", item->skew);
1241 printf(",\"bw\":%.2f", item->bandwidth);
1243 if (item->pt == PT_CN)
1244 printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=13, RFC 3389)", RTP_TYPE_CN);
1245 else if (item->pt == PT_CN_OLD)
1246 printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=19, reserved)", RTP_TYPE_CN);
1247 else if (item->flags & STAT_FLAG_WRONG_SEQ)
1248 printf(",\"s\":\"%s\",\"t\":%d", "Wrong sequence number", RTP_TYPE_ERROR);
1249 else if (item->flags & STAT_FLAG_DUP_PKT)
1250 printf(",\"s\":\"%s\",\"t\":%d", "Suspected duplicate (MAC address) only delta time calculated", RTP_TYPE_WARN);
1251 else if (item->flags & STAT_FLAG_REG_PT_CHANGE)
1252 printf(",\"s\":\"Payload changed to PT=%u%s\",\"t\":%d",
1254 (item->flags & STAT_FLAG_PT_T_EVENT) ? " telephone/event" : "",
1256 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP)
1257 printf(",\"s\":\"%s\",\"t\":%d", "Incorrect timestamp", RTP_TYPE_WARN);
1258 else if ((item->flags & STAT_FLAG_PT_CHANGE)
1259 && !(item->flags & STAT_FLAG_FIRST)
1260 && !(item->flags & STAT_FLAG_PT_CN)
1261 && (item->flags & STAT_FLAG_FOLLOW_PT_CN)
1262 && !(item->flags & STAT_FLAG_MARKER))
1264 printf(",\"s\":\"%s\",\"t\":%d", "Marker missing?", RTP_TYPE_WARN);
1266 else if (item->flags & STAT_FLAG_PT_T_EVENT)
1267 printf(",\"s\":\"PT=%u telephone/event\",\"t\":%d", item->pt, RTP_TYPE_PT_EVENT);
1268 else if (item->flags & STAT_FLAG_MARKER)
1269 printf(",\"t\":%d", RTP_TYPE_WARN);
1272 printf(",\"mark\":1");
1283 * sharkd_session_process_tap_conv_cb()
1286 * (m) tap - tap name
1287 * (m) type - tap output type
1288 * (m) proto - protocol short name
1289 * (o) filter - filter string
1291 * (o) convs - array of object with attributes:
1292 * (m) saddr - source address
1293 * (m) daddr - destination address
1294 * (o) sport - source port
1295 * (o) dport - destination port
1296 * (m) txf - TX frame count
1297 * (m) txb - TX bytes
1298 * (m) rxf - RX frame count
1299 * (m) rxb - RX bytes
1300 * (m) start - (relative) first packet time
1301 * (m) stop - (relative) last packet time
1303 * (o) hosts - array of object with attributes:
1304 * (m) host - host address
1305 * (o) port - host port
1306 * (m) txf - TX frame count
1307 * (m) txb - TX bytes
1308 * (m) rxf - RX frame count
1309 * (m) rxb - RX bytes
1312 sharkd_session_process_tap_conv_cb(void *arg)
1314 conv_hash_t *hash = (conv_hash_t *) arg;
1315 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
1317 int proto_with_port;
1322 if (!strncmp(iu->type, "conv:", 5))
1324 printf("{\"tap\":\"%s\",\"type\":\"conv\"", iu->type);
1325 printf(",\"convs\":[");
1326 proto = iu->type + 5;
1328 else if (!strncmp(iu->type, "endpt:", 6))
1330 printf("{\"tap\":\"%s\",\"type\":\"host\"", iu->type);
1331 printf(",\"hosts\":[");
1332 proto = iu->type + 6;
1336 printf("{\"tap\":\"%s\",\"type\":\"err\"", iu->type);
1340 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
1342 if (iu->hash.conv_array != NULL && !strncmp(iu->type, "conv:", 5))
1344 for (i = 0; i < iu->hash.conv_array->len; i++)
1346 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
1347 char *src_addr, *dst_addr;
1348 char *src_port, *dst_port;
1351 printf("%s{", i ? "," : "");
1353 printf("\"saddr\":\"%s\"", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
1354 printf(",\"daddr\":\"%s\"", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
1356 if (proto_with_port)
1358 printf(",\"sport\":\"%s\"", (src_port = get_conversation_port(NULL, iui->src_port, iui->ptype, iu->resolve_port)));
1359 printf(",\"dport\":\"%s\"", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->ptype, iu->resolve_port)));
1361 wmem_free(NULL, src_port);
1362 wmem_free(NULL, dst_port);
1365 printf(",\"rxf\":%" G_GUINT64_FORMAT, iui->rx_frames);
1366 printf(",\"rxb\":%" G_GUINT64_FORMAT, iui->rx_bytes);
1368 printf(",\"txf\":%" G_GUINT64_FORMAT, iui->tx_frames);
1369 printf(",\"txb\":%" G_GUINT64_FORMAT, iui->tx_bytes);
1371 printf(",\"start\":%.9f", nstime_to_sec(&iui->start_time));
1372 printf(",\"stop\":%.9f", nstime_to_sec(&iui->stop_time));
1374 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
1377 printf(",\"filter\":\"%s\"", filter_str);
1381 wmem_free(NULL, src_addr);
1382 wmem_free(NULL, dst_addr);
1384 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
1386 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
1392 else if (iu->hash.conv_array != NULL && !strncmp(iu->type, "endpt:", 6))
1394 for (i = 0; i < iu->hash.conv_array->len; i++)
1396 hostlist_talker_t *host = &g_array_index(iu->hash.conv_array, hostlist_talker_t, i);
1397 char *host_str, *port_str;
1400 printf("%s{", i ? "," : "");
1402 printf("\"host\":\"%s\"", (host_str = get_conversation_address(NULL, &host->myaddress, iu->resolve_name)));
1404 if (proto_with_port)
1406 printf(",\"port\":\"%s\"", (port_str = get_conversation_port(NULL, host->port, host->ptype, iu->resolve_port)));
1408 wmem_free(NULL, port_str);
1411 printf(",\"rxf\":%" G_GUINT64_FORMAT, host->rx_frames);
1412 printf(",\"rxb\":%" G_GUINT64_FORMAT, host->rx_bytes);
1414 printf(",\"txf\":%" G_GUINT64_FORMAT, host->tx_frames);
1415 printf(",\"txb\":%" G_GUINT64_FORMAT, host->tx_bytes);
1417 filter_str = get_hostlist_filter(host);
1420 printf(",\"filter\":\"%s\"", filter_str);
1424 wmem_free(NULL, host_str);
1426 if (sharkd_session_geoip_addr(&(host->myaddress), ""))
1432 printf("],\"proto\":\"%s\",\"geoip\":%s},", proto, with_geoip ? "true" : "false");
1436 sharkd_session_free_tap_conv_cb(void *arg)
1438 conv_hash_t *hash = (conv_hash_t *) arg;
1439 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
1441 if (!strncmp(iu->type, "conv:", 5))
1443 reset_conversation_table_data(hash);
1445 else if (!strncmp(iu->type, "endpt:", 6))
1447 reset_hostlist_table_data(hash);
1454 * sharkd_session_process_tap_nstat_cb()
1457 * (m) tap - tap name
1458 * (m) type - tap output type
1459 * (m) fields: array of objects with attributes:
1462 * (m) tables: array of object with attributes:
1463 * (m) t - table title
1464 * (m) i - array of items
1467 sharkd_session_process_tap_nstat_cb(void *arg)
1469 new_stat_data_t *stat_data = (new_stat_data_t *) arg;
1472 printf("{\"tap\":\"nstat:%s\",\"type\":\"nstat\"", stat_data->stat_tap_data->cli_string);
1474 printf(",\"fields\":[");
1475 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
1477 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
1485 json_puts_string(field->column_name);
1491 printf(",\"tables\":[");
1492 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
1494 stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i);
1495 const char *sepa = "";
1503 printf("\"%s\"", table->title);
1506 for (j = 0; j < table->num_elements; j++)
1508 stat_tap_table_item_type *field_data;
1510 field_data = new_stat_tap_get_field_data(table, j, 0);
1511 if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
1514 printf("%s[", sepa);
1515 for (k = 0; k < table->num_fields; k++)
1517 field_data = new_stat_tap_get_field_data(table, j, k);
1522 switch (field_data->type)
1524 case TABLE_ITEM_UINT:
1525 printf("%u", field_data->value.uint_value);
1528 case TABLE_ITEM_INT:
1529 printf("%d", field_data->value.uint_value);
1532 case TABLE_ITEM_STRING:
1533 json_puts_string(field_data->value.string_value);
1536 case TABLE_ITEM_FLOAT:
1537 printf("%f", field_data->value.float_value);
1540 case TABLE_ITEM_ENUM:
1541 printf("%d", field_data->value.enum_value);
1544 case TABLE_ITEM_NONE:
1561 sharkd_session_free_tap_nstat_cb(void *arg)
1563 new_stat_data_t *stat_data = (new_stat_data_t *) arg;
1565 free_stat_tables(stat_data->stat_tap_data, NULL, NULL);
1569 * sharkd_session_process_tap_rtd_cb()
1572 * (m) tap - tap name
1573 * (m) type - tap output type
1574 * (m) stats - statistics rows - array object with attributes:
1575 * (m) type - statistic name
1576 * (m) num - number of messages
1577 * (m) min - minimum SRT time
1578 * (m) max - maximum SRT time
1579 * (m) tot - total SRT time
1580 * (m) min_frame - minimal SRT
1581 * (m) max_frame - maximum SRT
1582 * (o) open_req - Open Requests
1583 * (o) disc_rsp - Discarded Responses
1584 * (o) req_dup - Duplicated Requests
1585 * (o) rsp_dup - Duplicated Responses
1586 * (o) open_req - Open Requests
1587 * (o) disc_rsp - Discarded Responses
1588 * (o) req_dup - Duplicated Requests
1589 * (o) rsp_dup - Duplicated Responses
1592 sharkd_session_process_tap_rtd_cb(void *arg)
1594 rtd_data_t *rtd_data = (rtd_data_t *) arg;
1595 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
1599 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
1601 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
1602 * some multiple table and single timestat (radius, h225)
1603 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
1604 * I wonder how it will gonna work with multiple timestats and multiple timestat...
1605 * (for usage grep for: register_rtd_table)
1607 const value_string *vs = get_rtd_value_string(rtd);
1608 const char *sepa = "";
1610 printf("{\"tap\":\"rtd:%s\",\"type\":\"rtd\"", filter);
1612 if (rtd_data->stat_table.num_rtds == 1)
1614 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
1616 printf(",\"open_req\":%u", ms->open_req_num);
1617 printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
1618 printf(",\"req_dup\":%u", ms->req_dup_num);
1619 printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
1622 printf(",\"stats\":[");
1623 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
1625 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
1627 for (j = 0; j < ms->num_timestat; j++)
1629 const char *type_str;
1631 if (ms->rtd[j].num == 0)
1634 printf("%s{", sepa);
1636 if (rtd_data->stat_table.num_rtds == 1)
1637 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
1639 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
1640 printf("\"type\":");
1641 json_puts_string(type_str);
1643 printf(",\"num\":%u", ms->rtd[j].num);
1644 printf(",\"min\":%.9f", nstime_to_sec(&(ms->rtd[j].min)));
1645 printf(",\"max\":%.9f", nstime_to_sec(&(ms->rtd[j].max)));
1646 printf(",\"tot\":%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
1647 printf(",\"min_frame\":%u", ms->rtd[j].min_num);
1648 printf(",\"max_frame\":%u", ms->rtd[j].max_num);
1650 if (rtd_data->stat_table.num_rtds != 1)
1652 /* like in tshark, display it on every row */
1653 printf(",\"open_req\":%u", ms->open_req_num);
1654 printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
1655 printf(",\"req_dup\":%u", ms->req_dup_num);
1656 printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
1667 sharkd_session_free_tap_rtd_cb(void *arg)
1669 rtd_data_t *rtd_data = (rtd_data_t *) arg;
1671 free_rtd_table(&rtd_data->stat_table, NULL, NULL);
1676 * sharkd_session_process_tap_srt_cb()
1679 * (m) tap - tap name
1680 * (m) type - tap output type
1682 * (m) tables - array of object with attributes:
1683 * (m) n - table name
1684 * (m) f - table filter
1685 * (o) c - table column name
1686 * (m) r - table rows - array object with attributes:
1688 * (m) idx - procedure index
1689 * (m) num - number of events
1690 * (m) min - minimum SRT time
1691 * (m) max - maximum SRT time
1692 * (m) tot - total SRT time
1695 sharkd_session_process_tap_srt_cb(void *arg)
1697 srt_data_t *srt_data = (srt_data_t *) arg;
1698 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
1700 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
1704 printf("{\"tap\":\"srt:%s\",\"type\":\"srt\"", filter);
1706 printf(",\"tables\":[");
1707 for (i = 0; i < srt_data->srt_array->len; i++)
1710 srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i);
1711 const char *sepa = "";
1721 json_puts_string(rst->name);
1722 else if (rst->short_name)
1723 json_puts_string(rst->short_name);
1725 printf("\"table%u\"", i);
1727 if (rst->filter_string)
1730 json_puts_string(rst->filter_string);
1733 if (rst->proc_column_name)
1736 json_puts_string(rst->proc_column_name);
1740 for (j = 0; j < rst->num_procs; j++)
1743 srt_procedure_t *proc = &rst->procedures[j];
1745 if (proc->stats.num == 0)
1748 printf("%s{", sepa);
1751 json_puts_string(proc->procedure);
1753 if (rst->filter_string)
1754 printf(",\"idx\":%d", proc->proc_index);
1756 printf(",\"num\":%u", proc->stats.num);
1758 printf(",\"min\":%.9f", nstime_to_sec(&proc->stats.min));
1759 printf(",\"max\":%.9f", nstime_to_sec(&proc->stats.max));
1760 printf(",\"tot\":%.9f", nstime_to_sec(&proc->stats.tot));
1772 sharkd_session_free_tap_srt_cb(void *arg)
1774 srt_data_t *srt_data = (srt_data_t *) arg;
1775 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
1777 free_srt_table(srt, srt_data->srt_array, NULL, NULL);
1778 g_array_free(srt_data->srt_array, TRUE);
1782 struct sharkd_export_object_list
1784 struct sharkd_export_object_list *next;
1791 static struct sharkd_export_object_list *sharkd_eo_list;
1794 * sharkd_session_process_tap_eo_cb()
1797 * (m) tap - tap name
1798 * (m) type - tap output type
1799 * (m) proto - protocol short name
1800 * (m) objects - array of object with attributes:
1801 * (m) pkt - packet number
1802 * (o) hostname - hostname
1803 * (o) type - content type
1804 * (o) filename - filename
1805 * (m) len - object length
1808 sharkd_session_process_tap_eo_cb(void *tapdata)
1810 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
1811 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list*) tap_object->gui_data;
1815 printf("{\"tap\":\"%s\",\"type\":\"eo\"", object_list->type);
1816 printf(",\"proto\":\"%s\"", object_list->proto);
1817 printf(",\"objects\":[");
1819 for (slist = object_list->entries; slist; slist = slist->next)
1821 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
1823 printf("%s{", i ? "," : "");
1825 printf("\"pkt\":%u", eo_entry->pkt_num);
1827 if (eo_entry->hostname)
1829 printf(",\"hostname\":");
1830 json_puts_string(eo_entry->hostname);
1833 if (eo_entry->content_type)
1835 printf(",\"type\":");
1836 json_puts_string(eo_entry->content_type);
1839 if (eo_entry->filename)
1841 printf(",\"filename\":");
1842 json_puts_string(eo_entry->filename);
1845 printf(",\"_download\":\"%s_%d\"", object_list->type, i);
1847 printf(",\"len\":%" G_GINT64_FORMAT, eo_entry->payload_len);
1858 sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
1860 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
1862 object_list->entries = g_slist_append(object_list->entries, entry);
1865 static export_object_entry_t *
1866 sharkd_eo_object_list_get_entry(void *gui_data, int row)
1868 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
1870 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
1874 * sharkd_session_process_tap_rtp_cb()
1876 * Output RTP streams tap:
1877 * (m) tap - tap name
1878 * (m) type - tap output type
1879 * (m) streams - array of object with attributes:
1880 * (m) ssrc - RTP synchronization source identifier
1881 * (m) payload - stream payload
1882 * (m) saddr - source address
1883 * (m) sport - source port
1884 * (m) daddr - destination address
1885 * (m) dport - destination port
1886 * (m) pkts - packets count
1887 * (m) max_delta - max delta (ms)
1888 * (m) max_jitter - max jitter (ms)
1889 * (m) mean_jitter - mean jitter (ms)
1892 * (m) problem - if analyser found the problem
1893 * (m) ipver - address IP version (4 or 6)
1896 sharkd_session_process_tap_rtp_cb(void *arg)
1898 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
1901 const char *sepa = "";
1903 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "rtp-streams", "rtp-streams");
1905 printf(",\"streams\":[");
1906 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
1908 rtp_stream_info_t *streaminfo = (rtp_stream_info_t *) listx->data;
1910 char *src_addr, *dst_addr;
1914 src_addr = address_to_display(NULL, &(streaminfo->src_addr));
1915 dst_addr = address_to_display(NULL, &(streaminfo->dest_addr));
1917 if (streaminfo->payload_type_name != NULL)
1918 payload = wmem_strdup(NULL, streaminfo->payload_type_name);
1920 payload = val_to_str_ext_wmem(NULL, streaminfo->payload_type, &rtp_payload_type_short_vals_ext, "Unknown (%u)");
1922 printf("%s{\"ssrc\":%u", sepa, streaminfo->ssrc);
1923 printf(",\"payload\":\"%s\"", payload);
1925 printf(",\"saddr\":\"%s\"", src_addr);
1926 printf(",\"sport\":%u", streaminfo->src_port);
1928 printf(",\"daddr\":\"%s\"", dst_addr);
1929 printf(",\"dport\":%u", streaminfo->dest_port);
1931 printf(",\"pkts\":%u", streaminfo->packet_count);
1933 printf(",\"max_delta\":%f", streaminfo->rtp_stats.max_delta);
1934 printf(",\"max_jitter\":%f", streaminfo->rtp_stats.max_jitter);
1935 printf(",\"mean_jitter\":%f", streaminfo->rtp_stats.mean_jitter);
1937 expected = (streaminfo->rtp_stats.stop_seq_nr + streaminfo->rtp_stats.cycles * 65536) - streaminfo->rtp_stats.start_seq_nr + 1;
1938 printf(",\"expectednr\":%u", expected);
1939 printf(",\"totalnr\":%u", streaminfo->rtp_stats.total_nr);
1941 printf(",\"problem\":%s", streaminfo->problem ? "true" : "false");
1944 printf(",\"ipver\":%d", (streaminfo->src_addr.type == AT_IPv6) ? 6 : 4);
1946 wmem_free(NULL, src_addr);
1947 wmem_free(NULL, dst_addr);
1948 wmem_free(NULL, payload);
1957 * sharkd_session_process_tap()
1959 * Process tap request
1962 * (m) tap0 - First tap request
1963 * (o) tap1...tap15 - Other tap requests
1965 * Output object with attributes:
1966 * (m) taps - array of object with attributes:
1967 * (m) tap - tap name
1968 * (m) type - tap output type
1970 * for type:stats see sharkd_session_process_tap_stats_cb()
1971 * for type:nstat see sharkd_session_process_tap_nstat_cb()
1972 * for type:conv see sharkd_session_process_tap_conv_cb()
1973 * for type:host see sharkd_session_process_tap_conv_cb()
1974 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
1975 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
1976 * for type:eo see sharkd_session_process_tap_eo_cb()
1977 * for type:expert see sharkd_session_process_tap_expert_cb()
1978 * for type:rtd see sharkd_session_process_tap_rtd_cb()
1979 * for type:srt see sharkd_session_process_tap_srt_cb()
1981 * (m) err - error code
1984 sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
1986 void *taps_data[16];
1987 GFreeFunc taps_free[16];
1991 rtpstream_tapinfo_t rtp_tapinfo =
1992 {NULL, NULL, NULL, NULL, 0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE};
1994 for (i = 0; i < 16; i++)
1997 const char *tok_tap;
1999 void *tap_data = NULL;
2000 GFreeFunc tap_free = NULL;
2001 const char *tap_filter = "";
2002 GString *tap_error = NULL;
2004 ws_snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
2005 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
2009 if (!strncmp(tok_tap, "stat:", 5))
2011 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
2016 fprintf(stderr, "sharkd_session_process_tap() stat %s not found\n", tok_tap + 5);
2020 st = stats_tree_new(cfg, NULL, tap_filter);
2022 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);
2024 if (!tap_error && cfg->init)
2028 tap_free = sharkd_session_free_tap_stats_cb;
2030 else if (!strcmp(tok_tap, "expert"))
2032 struct sharkd_expert_tap *expert_tap;
2034 expert_tap = g_new0(struct sharkd_expert_tap, 1);
2035 expert_tap->text = g_string_chunk_new(100);
2037 tap_error = register_tap_listener("expert", expert_tap, NULL, 0, NULL, sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb);
2039 tap_data = expert_tap;
2040 tap_free = sharkd_session_free_tap_expert_cb;
2042 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
2044 struct register_ct *ct = NULL;
2045 const char *ct_tapname;
2046 struct sharkd_conv_tap_data *ct_data;
2047 tap_packet_cb tap_func = NULL;
2049 if (!strncmp(tok_tap, "conv:", 5))
2051 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
2053 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
2055 fprintf(stderr, "sharkd_session_process_tap() conv %s not found\n", tok_tap + 5);
2059 else if (!strncmp(tok_tap, "endpt:", 6))
2061 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
2063 if (!ct || !(tap_func = get_hostlist_packet_func(ct)))
2065 fprintf(stderr, "sharkd_session_process_tap() endpt %s not found\n", tok_tap + 6);
2071 fprintf(stderr, "sharkd_session_process_tap() conv/endpt(?): %s not found\n", tok_tap);
2075 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
2077 ct_data = (struct sharkd_conv_tap_data *) g_malloc0(sizeof(struct sharkd_conv_tap_data));
2078 ct_data->type = tok_tap;
2079 ct_data->hash.user_data = ct_data;
2081 /* XXX: make configurable */
2082 ct_data->resolve_name = TRUE;
2083 ct_data->resolve_port = TRUE;
2085 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb);
2087 tap_data = &ct_data->hash;
2088 tap_free = sharkd_session_free_tap_conv_cb;
2090 else if (!strncmp(tok_tap, "nstat:", 6))
2092 stat_tap_table_ui *stat = new_stat_tap_by_name(tok_tap + 6);
2093 new_stat_data_t *stat_data;
2097 fprintf(stderr, "sharkd_session_process_tap() nstat=%s not found\n", tok_tap + 6);
2101 stat->stat_tap_init_cb(stat, NULL, NULL);
2103 stat_data = g_new0(new_stat_data_t, 1);
2104 stat_data->stat_tap_data = stat;
2105 stat_data->user_data = NULL;
2107 tap_error = register_tap_listener(stat->tap_name, stat_data, tap_filter, 0, NULL, stat->packet_func, sharkd_session_process_tap_nstat_cb);
2109 tap_data = stat_data;
2110 tap_free = sharkd_session_free_tap_nstat_cb;
2112 else if (!strncmp(tok_tap, "rtd:", 4))
2114 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
2115 rtd_data_t *rtd_data;
2120 fprintf(stderr, "sharkd_session_process_tap() rtd=%s not found\n", tok_tap + 4);
2124 rtd_table_get_filter(rtd, "", &tap_filter, &err);
2127 fprintf(stderr, "sharkd_session_process_tap() rtd=%s err=%s\n", tok_tap + 4, err);
2132 rtd_data = g_new0(rtd_data_t, 1);
2133 rtd_data->user_data = rtd;
2134 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL, NULL);
2136 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);
2138 tap_data = rtd_data;
2139 tap_free = sharkd_session_free_tap_rtd_cb;
2141 else if (!strncmp(tok_tap, "srt:", 4))
2143 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
2144 srt_data_t *srt_data;
2149 fprintf(stderr, "sharkd_session_process_tap() srt=%s not found\n", tok_tap + 4);
2153 srt_table_get_filter(srt, "", &tap_filter, &err);
2156 fprintf(stderr, "sharkd_session_process_tap() srt=%s err=%s\n", tok_tap + 4, err);
2161 srt_data = g_new0(srt_data_t, 1);
2162 srt_data->srt_array = g_array_new(FALSE, TRUE, sizeof(srt_stat_table *));
2163 srt_data->user_data = srt;
2164 srt_table_dissector_init(srt, srt_data->srt_array, NULL, NULL);
2166 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);
2168 tap_data = srt_data;
2169 tap_free = sharkd_session_free_tap_srt_cb;
2171 else if (!strncmp(tok_tap, "eo:", 3))
2173 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
2174 export_object_list_t *eo_object;
2175 struct sharkd_export_object_list *object_list;
2179 fprintf(stderr, "sharkd_session_process_tap() eo=%s not found\n", tok_tap + 3);
2183 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
2185 if (!strcmp(object_list->type, tok_tap))
2187 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
2188 object_list->entries = NULL;
2195 object_list = g_new(struct sharkd_export_object_list, 1);
2196 object_list->type = g_strdup(tok_tap);
2197 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
2198 object_list->entries = NULL;
2199 object_list->next = sharkd_eo_list;
2200 sharkd_eo_list = object_list;
2203 eo_object = g_new0(export_object_list_t, 1);
2204 eo_object->add_entry = sharkd_eo_object_list_add_entry;
2205 eo_object->get_entry = sharkd_eo_object_list_get_entry;
2206 eo_object->gui_data = (void *) object_list;
2208 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);
2210 tap_data = eo_object;
2211 tap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
2213 else if (!strcmp(tok_tap, "rtp-streams"))
2215 tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet, sharkd_session_process_tap_rtp_cb);
2217 tap_data = &rtp_tapinfo;
2218 tap_free = rtpstream_reset_cb;
2220 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
2222 struct sharkd_analyse_rtp *rtp_req;
2224 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
2225 if (!sharkd_rtp_match_init(&rtp_req->rtp, tok_tap + 12))
2231 rtp_req->tap_name = tok_tap;
2232 rtp_req->statinfo.first_packet = TRUE;
2233 rtp_req->statinfo.reg_pt = PT_UNDEFINED;
2235 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);
2238 tap_free = sharkd_session_process_tap_rtp_free_cb;
2242 fprintf(stderr, "sharkd_session_process_tap() %s not recognized\n", tok_tap);
2248 fprintf(stderr, "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str);
2249 g_string_free(tap_error, TRUE);
2255 taps_data[taps_count] = tap_data;
2256 taps_free[taps_count] = tap_free;
2260 fprintf(stderr, "sharkd_session_process_tap() count=%d\n", taps_count);
2261 if (taps_count == 0)
2264 printf("{\"taps\":[");
2266 printf("null],\"err\":0}\n");
2268 for (i = 0; i < taps_count; i++)
2271 remove_tap_listener(taps_data[i]);
2274 taps_free[i](taps_data[i]);
2279 * sharkd_session_process_follow()
2281 * Process follow request
2284 * (m) follow - follow protocol request (e.g. HTTP)
2285 * (m) filter - filter request (e.g. tcp.stream == 1)
2287 * Output object with attributes:
2289 * (m) err - error code
2290 * (m) shost - server host
2291 * (m) sport - server port
2292 * (m) sbytes - server send bytes count
2293 * (m) chost - client host
2294 * (m) cport - client port
2295 * (m) cbytes - client send bytes count
2296 * (o) payloads - array of object with attributes:
2297 * (o) s - set if server sent, else client
2298 * (m) n - packet number
2299 * (m) d - data base64 encoded
2302 sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
2304 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
2305 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2307 register_follow_t *follower;
2310 follow_info_t *follow_info;
2314 if (!tok_follow || !tok_filter)
2317 follower = get_follow_by_name(tok_follow);
2320 fprintf(stderr, "sharkd_session_process_follow() follower=%s not found\n", tok_follow);
2324 /* follow_reset_stream ? */
2325 follow_info = g_new0(follow_info_t, 1);
2326 /* gui_data, filter_out_filter not set, but not used by dissector */
2328 tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL, get_follow_tap_handler(follower), NULL);
2331 fprintf(stderr, "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str);
2332 g_string_free(tap_error, TRUE);
2333 g_free(follow_info);
2341 printf("\"err\":0");
2343 /* Server information: hostname, port, bytes sent */
2344 host = address_to_name(&follow_info->server_ip);
2345 printf(",\"shost\":");
2346 json_puts_string(host);
2348 port = get_follow_port_to_display(follower)(NULL, follow_info->server_port);
2349 printf(",\"sport\":");
2350 json_puts_string(port);
2351 wmem_free(NULL, port);
2353 printf(",\"sbytes\":%u", follow_info->bytes_written[0]);
2355 /* Client information: hostname, port, bytes sent */
2356 host = address_to_name(&follow_info->client_ip);
2357 printf(",\"chost\":");
2358 json_puts_string(host);
2360 port = get_follow_port_to_display(follower)(NULL, follow_info->client_port);
2361 printf(",\"cport\":");
2362 json_puts_string(port);
2363 wmem_free(NULL, port);
2365 printf(",\"cbytes\":%u", follow_info->bytes_written[1]);
2367 if (follow_info->payload)
2369 follow_record_t *follow_record;
2371 const char *sepa = "";
2373 printf(",\"payloads\":[");
2375 for (cur = follow_info->payload; cur; cur = g_list_next(cur))
2377 follow_record = (follow_record_t *) cur->data;
2379 printf("%s{", sepa);
2381 printf("\"n\":%u", follow_record->packet_num);
2384 json_print_base64(follow_record->data->data, follow_record->data->len);
2386 if (follow_record->is_server)
2387 printf(",\"s\":%d", 1);
2398 remove_tap_listener(follow_info);
2399 follow_info_free(follow_info);
2403 sharkd_session_process_frame_cb_tree(proto_tree *tree, tvbuff_t **tvbs)
2406 const char *sepa = "";
2409 for (node = tree->first_child; node; node = node->next)
2411 field_info *finfo = PNODE_FINFO(node);
2416 /* XXX, for now always skip hidden */
2417 if (FI_GET_FLAG(finfo, FI_HIDDEN))
2420 printf("%s{", sepa);
2425 char label_str[ITEM_LABEL_LENGTH];
2427 label_str[0] = '\0';
2428 proto_item_fill_label(finfo, label_str);
2429 json_puts_string(label_str);
2433 json_puts_string(finfo->rep->representation);
2436 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
2440 for (idx = 1; tvbs[idx]; idx++)
2442 if (tvbs[idx] == finfo->ds_tvb)
2444 printf(",\"ds\":%d", idx);
2450 if (finfo->start >= 0 && finfo->length > 0)
2451 printf(",\"h\":[%d,%d]", finfo->start, finfo->length);
2453 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
2454 printf(",\"i\":[%d,%d]", finfo->appendix_start, finfo->appendix_length);
2459 if (finfo->hfinfo->type == FT_PROTOCOL)
2461 printf(",\"t\":\"proto\"");
2463 else if (finfo->hfinfo->type == FT_FRAMENUM)
2465 printf(",\"t\":\"framenum\",\"fnum\":%u", finfo->value.value.uinteger);
2467 else if (FI_GET_FLAG(finfo, FI_URL) && IS_FT_STRING(finfo->hfinfo->type))
2469 char *url = fvalue_to_string_repr(NULL, &finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
2471 printf(",\"t\":\"url\",\"url\":");
2472 json_puts_string(url);
2473 wmem_free(NULL, url);
2477 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
2479 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK), expert_severity_vals);
2481 g_assert(severity != NULL);
2483 printf(",\"s\":\"%s\"", severity);
2486 if (((proto_tree *) node)->first_child) {
2487 if (finfo->tree_type != -1)
2488 printf(",\"e\":%d", finfo->tree_type);
2490 sharkd_session_process_frame_cb_tree((proto_tree *) node, tvbs);
2500 sharkd_follower_visit_layers_cb(const void *key _U_, void *value, void *user_data)
2502 register_follow_t *follower = (register_follow_t *) value;
2503 packet_info *pi = (packet_info *) user_data;
2505 const int proto_id = get_follow_proto_id(follower);
2507 guint32 ignore_stream;
2509 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
2511 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
2512 char *follow_filter;
2514 follow_filter = get_follow_conv_func(follower)(pi, &ignore_stream);
2516 printf(",[\"%s\",", layer_proto);
2517 json_puts_string(follow_filter);
2520 g_free(follow_filter);
2527 sharkd_session_process_frame_cb(packet_info *pi, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
2534 printf("\"err\":0");
2538 tvbuff_t **tvbs = NULL;
2540 printf(",\"tree\":");
2542 /* arrayize data src, to speedup searching for ds_tvb index */
2543 if (data_src && data_src->next /* only needed if there are more than one data source */)
2545 guint count = g_slist_length((GSList *) data_src);
2548 tvbs = (tvbuff_t **) g_malloc((count + 1) * sizeof(*tvbs));
2550 for (i = 0; i < count; i++)
2552 struct data_source *src = (struct data_source *) g_slist_nth_data((GSList *) data_src, i);
2554 tvbs[i] = get_data_source_tvb(src);
2560 sharkd_session_process_frame_cb_tree(tree, tvbs);
2569 printf(",\"col\":[");
2570 for (col = 0; col < cinfo->num_cols; ++col)
2572 const col_item_t *col_item = &cinfo->columns[col];
2574 printf("%s\"%s\"", (col) ? "," : "", col_item->col_data);
2581 struct data_source *src = (struct data_source *)data_src->data;
2582 const char *ds_sepa = NULL;
2587 tvb = get_data_source_tvb(src);
2588 length = tvb_captured_length(tvb);
2590 printf(",\"bytes\":");
2593 const guchar *cp = tvb_get_ptr(tvb, 0, length);
2595 /* XXX pi.fd->flags.encoding */
2596 json_print_base64(cp, length);
2600 json_print_base64("", 0);
2603 data_src = data_src->next;
2606 printf(",\"ds\":[");
2612 src = (struct data_source *)data_src->data;
2615 char *src_name = get_data_source_name(src);
2617 printf("%s{\"name\":", ds_sepa);
2618 json_puts_string(src_name);
2619 wmem_free(NULL, src_name);
2622 tvb = get_data_source_tvb(src);
2623 length = tvb_captured_length(tvb);
2625 printf(",\"bytes\":");
2628 const guchar *cp = tvb_get_ptr(tvb, 0, length);
2630 /* XXX pi.fd->flags.encoding */
2631 json_print_base64(cp, length);
2635 json_print_base64("", 0);
2641 data_src = data_src->next;
2644 /* close ds, only if was opened */
2645 if (ds_sepa != NULL)
2649 printf(",\"fol\":[0");
2650 follow_iterate_followers(sharkd_follower_visit_layers_cb, pi);
2657 * sharkd_session_process_intervals()
2659 * Process intervals request - generate basic capture file statistics per requested interval.
2662 * (o) interval - interval time in ms, if not specified: 1000ms
2663 * (o) filter - filter for generating interval request
2665 * Output object with attributes:
2666 * (m) intervals - array of intervals, with indexes:
2667 * [0] - index of interval,
2668 * [1] - number of frames during interval,
2669 * [2] - number of bytes during interval.
2671 * (m) last - last interval number.
2672 * (m) frames - total number of frames
2673 * (m) bytes - total number of bytes
2675 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
2678 sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
2680 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
2681 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2683 const guint8 *filter_data = NULL;
2687 unsigned int frames;
2691 nstime_t *start_ts = NULL;
2693 guint32 interval_ms = 1000; /* default: one per second */
2695 const char *sepa = "";
2696 unsigned int framenum;
2701 if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0) {
2702 fprintf(stderr, "Invalid interval parameter: %s.\n", tok_interval);
2709 filter_data = sharkd_session_filter_data(tok_filter);
2714 st_total.frames = 0;
2722 printf("{\"intervals\":[");
2724 for (framenum = 1; framenum <= cfile.count; framenum++)
2726 frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum);
2730 if (start_ts == NULL)
2731 start_ts = &fdata->abs_ts;
2733 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
2736 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (gint64) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
2737 new_idx = msec_rel / interval_ms;
2743 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
2756 st.bytes += fdata->pkt_len;
2758 st_total.frames += 1;
2759 st_total.bytes += fdata->pkt_len;
2764 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
2768 printf("],\"last\":%" G_GINT64_FORMAT ",\"frames\":%u,\"bytes\":%" G_GUINT64_FORMAT "}\n", max_idx, st_total.frames, st_total.bytes);
2772 * sharkd_session_process_frame()
2774 * Process frame request
2777 * (m) frame - requested frame number
2778 * (o) proto - set if output frame tree
2779 * (o) columns - set if output frame columns
2780 * (o) bytes - set if output frame bytes
2782 * Output object with attributes:
2783 * (m) err - 0 if succeed
2784 * (o) tree - array of frame nodes with attributes:
2786 * t: 'proto', 'framenum', 'url' - type of node
2788 * e - subtree ett index
2789 * n - array of subtree nodes
2790 * h - two item array: (item start, item length)
2791 * i - two item array: (appendix start, appendix length)
2792 * p - [RESERVED] two item array: (protocol start, protocol length)
2793 * ds- data src index
2794 * url - only for t:'url', url
2795 * fnum - only for t:'framenum', frame number
2797 * (o) col - array of column data
2798 * (o) bytes - base64 of frame bytes
2799 * (o) ds - array of other data srcs
2800 * (o) fol - array of follow filters:
2802 * [1] - filter string
2805 sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
2807 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
2808 int tok_proto = (json_find_attr(buf, tokens, count, "proto") != NULL);
2809 int tok_bytes = (json_find_attr(buf, tokens, count, "bytes") != NULL);
2810 int tok_columns = (json_find_attr(buf, tokens, count, "columns") != NULL);
2814 if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
2817 sharkd_dissect_request(framenum, &sharkd_session_process_frame_cb, tok_bytes, tok_columns, tok_proto, NULL);
2821 * sharkd_session_process_check()
2823 * Process check request.
2826 * (o) filter - filter to be checked
2828 * Output object with attributes:
2829 * (m) err - always 0
2830 * (o) filter - 'ok', 'warn' or error message
2833 sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
2835 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2837 printf("{\"err\":0");
2838 if (tok_filter != NULL)
2840 char *err_msg = NULL;
2843 if (dfilter_compile(tok_filter, &dfp, &err_msg))
2845 const char *s = "ok";
2847 if (dfilter_deprecated_tokens(dfp))
2850 printf(",\"filter\":\"%s\"", s);
2855 printf(",\"filter\":");
2856 json_puts_string(err_msg);
2865 struct sharkd_session_process_complete_pref_data
2873 sharkd_session_process_complete_pref_cb(module_t *module, gpointer d)
2875 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
2877 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
2880 printf("%s{\"f\":\"%s\",\"d\":\"%s\"}", data->sepa, module->name, module->title);
2887 sharkd_session_process_complete_pref_option_cb(pref_t *pref, gpointer d)
2889 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
2890 const char *pref_name = prefs_get_name(pref);
2891 const char *pref_title = prefs_get_title(pref);
2893 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
2896 printf("%s{\"f\":\"%s.%s\",\"d\":\"%s\"}", data->sepa, data->module, pref_name, pref_title);
2899 return 0; /* continue */
2903 * sharkd_session_process_complete()
2905 * Process complete request
2908 * (o) field - field to be completed
2909 * (o) pref - preference to be completed
2911 * Output object with attributes:
2912 * (m) err - always 0
2913 * (o) field - array of object with attributes:
2914 * (m) f - field text
2915 * (o) t - field type (FT_ number)
2916 * (o) n - field name
2917 * (o) pref - array of object with attributes:
2919 * (o) d - pref description
2922 sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
2924 const char *tok_field = json_find_attr(buf, tokens, count, "field");
2925 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
2927 printf("{\"err\":0");
2928 if (tok_field != NULL && tok_field[0])
2930 const size_t filter_length = strlen(tok_field);
2931 const int filter_with_dot = !!strchr(tok_field, '.');
2936 const char *sepa = "";
2938 printf(",\"field\":[");
2940 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
2942 protocol_t *protocol = find_protocol_by_id(proto_id);
2943 const char *protocol_filter;
2944 const char *protocol_name;
2945 header_field_info *hfinfo;
2947 if (!proto_is_protocol_enabled(protocol))
2950 protocol_name = proto_get_protocol_long_name(protocol);
2951 protocol_filter = proto_get_protocol_filter_name(proto_id);
2953 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
2955 printf("%s{", sepa);
2958 json_puts_string(protocol_filter);
2959 printf(",\"t\":%d", FT_PROTOCOL);
2961 json_puts_string(protocol_name);
2967 if (!filter_with_dot)
2970 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL; hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
2972 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
2975 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
2977 printf("%s{", sepa);
2980 json_puts_string(hfinfo->abbrev);
2982 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
2983 if (hfinfo->same_name_next == NULL)
2985 printf(",\"t\":%d", hfinfo->type);
2987 json_puts_string(hfinfo->name);
2999 if (tok_pref != NULL && tok_pref[0])
3001 struct sharkd_session_process_complete_pref_data data;
3004 data.module = tok_pref;
3005 data.pref = tok_pref;
3008 printf(",\"pref\":[");
3010 if ((dot_sepa = strchr(tok_pref, '.')))
3014 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
3015 data.pref = dot_sepa + 1;
3017 pref_mod = prefs_find_module(data.module);
3019 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
3025 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
3037 * sharkd_session_process_setconf()
3039 * Process setconf request
3042 * (m) name - preference name
3043 * (m) value - preference value
3045 * Output object with attributes:
3046 * (m) err - error code: 0 succeed
3049 sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
3051 const char *tok_name = json_find_attr(buf, tokens, count, "name");
3052 const char *tok_value = json_find_attr(buf, tokens, count, "value");
3055 prefs_set_pref_e ret;
3057 if (!tok_name || tok_name[0] == '\0' || !tok_value)
3060 ws_snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
3062 ret = prefs_set_pref(pref);
3063 printf("{\"err\":%d}\n", ret);
3066 struct sharkd_session_process_dumpconf_data
3073 sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d)
3075 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
3076 const char *pref_name = prefs_get_name(pref);
3078 printf("%s\"%s.%s\":{", data->sepa, data->module->name, pref_name);
3080 switch (prefs_get_type(pref))
3083 case PREF_DECODE_AS_UINT:
3084 printf("\"u\":%u", prefs_get_uint_value_real(pref, pref_current));
3085 if (prefs_get_uint_base(pref) != 10)
3086 printf(",\"ub\":%u", prefs_get_uint_base(pref));
3090 printf("\"b\":%s", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
3095 json_puts_string(prefs_get_string_value(pref, pref_current));
3100 const enum_val_t *enums;
3101 const char *enum_sepa = "";
3104 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
3106 printf("%s{\"v\":%d", enum_sepa, enums->value);
3108 if (enums->value == prefs_get_enum_value(pref, pref_current))
3112 json_puts_string(enums->description);
3122 case PREF_DECODE_AS_RANGE:
3124 char *range_str = range_convert_range(NULL, prefs_get_range_value_real(pref, pref_current));
3125 printf("\"r\":\"%s\"", range_str);
3126 wmem_free(NULL, range_str);
3133 case PREF_STATIC_TEXT:
3141 json_puts_string(prefs_get_title(pref));
3147 return 0; /* continue */
3151 sharkd_session_process_dumpconf_mod_cb(module_t *module, gpointer d)
3153 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
3155 data->module = module;
3156 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
3162 * sharkd_session_process_dumpconf()
3164 * Process dumpconf request
3167 * (o) pref - module, or preference, NULL for all
3169 * Output object with attributes:
3170 * (o) prefs - object with module preferences
3171 * (m) [KEY] - preference name
3172 * (o) u - preference value (only for PREF_UINT)
3173 * (o) ub - preference value suggested base for display (only for PREF_UINT) and if different than 10
3174 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
3175 * (o) s - preference value (only for PREF_STRING)
3176 * (o) e - preference possible values (only for PREF_ENUM)
3177 * (o) r - preference value (only for PREF_RANGE)
3178 * (o) t - preference value (only for PREF_UAT)
3181 sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
3183 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
3189 struct sharkd_session_process_dumpconf_data data;
3194 printf("{\"prefs\":{");
3195 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
3200 if ((dot_sepa = strchr(tok_pref, '.')))
3202 pref_t *pref = NULL;
3204 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
3205 pref_mod = prefs_find_module(tok_pref);
3207 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
3212 struct sharkd_session_process_dumpconf_data data;
3214 data.module = pref_mod;
3217 printf("{\"prefs\":{");
3218 sharkd_session_process_dumpconf_cb(pref, &data);
3225 pref_mod = prefs_find_module(tok_pref);
3228 struct sharkd_session_process_dumpconf_data data;
3230 data.module = pref_mod;
3233 printf("{\"prefs\":{");
3234 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
3239 struct sharkd_download_rtp
3241 struct sharkd_rtp_match rtp;
3247 sharkd_rtp_download_free_items(void *ptr)
3249 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
3251 g_free(rtp_packet->info);
3252 g_free(rtp_packet->payload_data);
3257 sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
3259 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
3260 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
3262 static const int sample_bytes_ = sizeof(SAMPLE) / sizeof(char);
3264 guint32 audio_out_rate_ = 0;
3265 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
3266 struct SpeexResamplerState_ *audio_resampler_ = NULL;
3268 gsize resample_buff_len = 0x1000;
3269 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
3270 spx_uint32_t cur_in_rate = 0;
3271 char *write_buff = NULL;
3272 gint64 write_bytes = 0;
3273 unsigned channels = 0;
3274 unsigned sample_rate = 0;
3277 int base64_state1 = 0;
3278 int base64_state2 = 0;
3282 for (l = req->packets; l; l = l->next)
3284 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
3286 SAMPLE *decode_buff = NULL;
3287 size_t decoded_bytes;
3289 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
3290 if (decoded_bytes == 0 || sample_rate == 0)
3292 /* We didn't decode anything. Clean up and prep for the next packet. */
3293 g_free(decode_buff);
3297 if (audio_out_rate_ == 0)
3303 /* First non-zero wins */
3304 audio_out_rate_ = sample_rate;
3306 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
3308 /* write WAVE header */
3309 memset(&wav_hdr, 0, sizeof(wav_hdr));
3310 memcpy(&wav_hdr[0], "RIFF", 4);
3311 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
3312 memcpy(&wav_hdr[8], "WAVE", 4);
3314 memcpy(&wav_hdr[12], "fmt ", 4);
3315 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
3316 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
3319 memcpy(&wav_hdr[22], &tmp16, 2);
3321 tmp32 = sample_rate;
3322 memcpy(&wav_hdr[24], &tmp32, 4);
3324 tmp32 = sample_rate * channels * sample_bytes_;
3325 memcpy(&wav_hdr[28], &tmp32, 4);
3327 tmp16 = channels * sample_bytes_;
3328 memcpy(&wav_hdr[32], &tmp16, 2);
3329 /* bits per sample */
3330 tmp16 = 8 * sample_bytes_;
3331 memcpy(&wav_hdr[34], &tmp16, 2);
3333 memcpy(&wav_hdr[36], "data", 4);
3334 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
3336 for (i = 0; i < (int) sizeof(wav_hdr); i++)
3337 json_print_base64_step(&wav_hdr[i], &base64_state1, &base64_state2);
3340 // Write samples to our file.
3341 write_buff = (char *) decode_buff;
3342 write_bytes = decoded_bytes;
3344 if (audio_out_rate_ != sample_rate)
3346 spx_uint32_t in_len, out_len;
3348 /* Resample the audio to match our previous output rate. */
3349 if (!audio_resampler_)
3351 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL);
3352 speex_resampler_skip_zeros(audio_resampler_);
3353 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
3357 spx_uint32_t audio_out_rate;
3358 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
3360 if (sample_rate != cur_in_rate)
3362 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
3363 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
3366 in_len = (spx_uint32_t)rtp_packet->info->info_payload_len;
3367 out_len = (audio_out_rate_ * (spx_uint32_t)rtp_packet->info->info_payload_len / sample_rate) + (audio_out_rate_ % sample_rate != 0);
3368 if (out_len * sample_bytes_ > resample_buff_len)
3370 while ((out_len * sample_bytes_ > resample_buff_len))
3371 resample_buff_len *= 2;
3372 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
3375 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
3376 write_buff = (char *) resample_buff;
3377 write_bytes = out_len * sample_bytes_;
3380 /* Write the decoded, possibly-resampled audio */
3381 for (i = 0; i < write_bytes; i++)
3382 json_print_base64_step(&write_buff[i], &base64_state1, &base64_state2);
3384 g_free(decode_buff);
3387 json_print_base64_step(NULL, &base64_state1, &base64_state2);
3389 g_free(resample_buff);
3390 g_hash_table_destroy(decoders_hash_);
3394 sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
3396 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
3397 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
3399 /* do not consider RTP packets without a setup frame */
3400 if (rtp_info->info_setup_frame_num == 0)
3403 if (sharkd_rtp_match_check(&req_rtp->rtp, pinfo, rtp_info))
3405 rtp_packet_t *rtp_packet;
3407 rtp_packet = g_new0(rtp_packet_t, 1);
3408 rtp_packet->info = (struct _rtp_info *) g_memdup(rtp_info, sizeof(struct _rtp_info));
3410 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
3411 rtp_packet->payload_data = (guint8 *) g_memdup(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
3413 if (!req_rtp->packets)
3414 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
3416 rtp_packet->frame_num = pinfo->num;
3417 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
3419 /* XXX, O(n) optimize */
3420 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
3427 * sharkd_session_process_download()
3429 * Process download request
3432 * (m) token - token to download
3434 * Output object with attributes:
3435 * (o) file - suggested name of file
3436 * (o) mime - suggested content type
3437 * (o) data - payload base64 encoded
3440 sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
3442 const char *tok_token = json_find_attr(buf, tokens, count, "token");
3447 if (!strncmp(tok_token, "eo:", 3))
3449 struct sharkd_export_object_list *object_list;
3450 const export_object_entry_t *eo_entry = NULL;
3452 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
3454 size_t eo_type_len = strlen(object_list->type);
3456 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
3460 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
3463 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
3470 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
3471 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
3473 printf("{\"file\":");
3474 json_puts_string(filename);
3475 printf(",\"mime\":");
3476 json_puts_string(mime);
3477 printf(",\"data\":");
3478 json_print_base64(eo_entry->payload_data, (int) eo_entry->payload_len); /* XXX, export object will be truncated if >= 2^31 */
3482 else if (!strcmp(tok_token, "ssl-secrets"))
3484 char *str = ssl_export_sessions();
3488 const char *mime = "text/plain";
3489 const char *filename = "keylog.txt";
3491 printf("{\"file\":");
3492 json_puts_string(filename);
3493 printf(",\"mime\":");
3494 json_puts_string(mime);
3495 printf(",\"data\":");
3496 json_print_base64(str, strlen(str));
3501 else if (!strncmp(tok_token, "rtp:", 4))
3503 struct sharkd_download_rtp rtp_req;
3506 memset(&rtp_req, 0, sizeof(rtp_req));
3507 if (!sharkd_rtp_match_init(&rtp_req.rtp, tok_token + 4))
3509 fprintf(stderr, "sharkd_session_process_download() rtp tokenizing error %s\n", tok_token);
3513 tap_error = register_tap_listener("rtp", &rtp_req, NULL, 0, NULL, sharkd_session_packet_download_tap_rtp_cb, NULL);
3516 fprintf(stderr, "sharkd_session_process_download() rtp error=%s", tap_error->str);
3517 g_string_free(tap_error, TRUE);
3522 remove_tap_listener(&rtp_req);
3524 if (rtp_req.packets)
3526 const char *mime = "audio/x-wav";
3527 const char *filename = tok_token;
3529 printf("{\"file\":");
3530 json_puts_string(filename);
3531 printf(",\"mime\":");
3532 json_puts_string(mime);
3534 printf(",\"data\":");
3536 sharkd_rtp_download_decode(&rtp_req);
3541 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
3547 sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
3551 /* sanity check, and split strings */
3552 if (count < 1 || tokens[0].type != JSMN_OBJECT)
3554 fprintf(stderr, "sanity check(1): [0] not object\n");
3558 /* don't need [0] token */
3564 fprintf(stderr, "sanity check(2): %d not even\n", count);
3568 for (i = 0; i < count; i += 2)
3570 if (tokens[i].type != JSMN_STRING)
3572 fprintf(stderr, "sanity check(3): [%d] not string\n", i);
3576 buf[tokens[i + 0].end] = '\0';
3577 buf[tokens[i + 1].end] = '\0';
3579 json_unescape_str(&buf[tokens[i + 0].start]);
3580 json_unescape_str(&buf[tokens[i + 1].start]);
3584 const char *tok_req = json_find_attr(buf, tokens, count, "req");
3588 fprintf(stderr, "sanity check(4): no \"req\".\n");
3592 if (!strcmp(tok_req, "load"))
3593 sharkd_session_process_load(buf, tokens, count);
3594 else if (!strcmp(tok_req, "status"))
3595 sharkd_session_process_status();
3596 else if (!strcmp(tok_req, "analyse"))
3597 sharkd_session_process_analyse();
3598 else if (!strcmp(tok_req, "info"))
3599 sharkd_session_process_info();
3600 else if (!strcmp(tok_req, "check"))
3601 sharkd_session_process_check(buf, tokens, count);
3602 else if (!strcmp(tok_req, "complete"))
3603 sharkd_session_process_complete(buf, tokens, count);
3604 else if (!strcmp(tok_req, "frames"))
3605 sharkd_session_process_frames(buf, tokens, count);
3606 else if (!strcmp(tok_req, "tap"))
3607 sharkd_session_process_tap(buf, tokens, count);
3608 else if (!strcmp(tok_req, "follow"))
3609 sharkd_session_process_follow(buf, tokens, count);
3610 else if (!strcmp(tok_req, "intervals"))
3611 sharkd_session_process_intervals(buf, tokens, count);
3612 else if (!strcmp(tok_req, "frame"))
3613 sharkd_session_process_frame(buf, tokens, count);
3614 else if (!strcmp(tok_req, "setconf"))
3615 sharkd_session_process_setconf(buf, tokens, count);
3616 else if (!strcmp(tok_req, "dumpconf"))
3617 sharkd_session_process_dumpconf(buf, tokens, count);
3618 else if (!strcmp(tok_req, "download"))
3619 sharkd_session_process_download(buf, tokens, count);
3620 else if (!strcmp(tok_req, "bye"))
3623 fprintf(stderr, "::: req = %s\n", tok_req);
3625 /* reply for every command are 0+ lines of JSON reply (outputed above), finished by empty new line */
3629 * We do an explicit fflush after every line, because
3630 * we want output to be written to the socket as soon
3631 * as the line is complete.
3633 * The stream is fully-buffered by default, so it's
3634 * only flushed when the buffer fills or the FILE *
3635 * is closed. On UN*X, we could set it to be line
3636 * buffered, but the MSVC standard I/O routines don't
3637 * support line buffering - they only support *byte*
3638 * buffering, doing a write for every byte written,
3639 * which is too inefficient, and full buffering,
3640 * which is what you get if you request line buffering.
3647 sharkd_session_main(void)
3650 jsmntok_t *tokens = NULL;
3651 int tokens_max = -1;
3653 fprintf(stderr, "Hello in child.\n");
3655 while (fgets(buf, sizeof(buf), stdin))
3657 /* every command is line seperated JSON */
3660 ret = wsjsmn_parse(buf, NULL, 0);
3663 fprintf(stderr, "invalid JSON -> closing\n");
3667 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
3670 if (tokens == NULL || tokens_max < ret)
3673 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
3676 memset(tokens, 0, ret * sizeof(jsmntok_t));
3678 ret = wsjsmn_parse(buf, tokens, ret);
3681 fprintf(stderr, "invalid JSON(2) -> closing\n");
3685 sharkd_session_process(buf, tokens, ret);
3694 * Editor modelines - http://www.wireshark.org/tools/modelines.html
3699 * indent-tabs-mode: t
3702 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
3703 * :indentSize=8:tabSize=8:noTabs=false: