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/epan_dissect.h>
38 #include <epan/exceptions.h>
39 #include <epan/color_filters.h>
40 #include <epan/prefs.h>
41 #include <epan/prefs-int.h>
42 #include <epan/uat-int.h>
43 #include <wiretap/wtap.h>
45 #include <epan/column.h>
47 #include <ui/ssl_key_export.h>
49 #include <epan/stats_tree_priv.h>
50 #include <epan/stat_tap_ui.h>
51 #include <epan/conversation_table.h>
52 #include <epan/sequence_analysis.h>
53 #include <epan/expert.h>
54 #include <epan/export_object.h>
55 #include <epan/follow.h>
56 #include <epan/rtd_table.h>
57 #include <epan/srt_table.h>
59 #include <epan/dissectors/packet-h225.h>
60 #include <epan/rtp_pt.h>
61 #include <ui/voip_calls.h>
62 #include <ui/rtp_stream.h>
63 #include <ui/tap-rtp-common.h>
64 #include <epan/to_str.h>
66 #include <epan/addr_resolv.h>
67 #include <epan/dissectors/packet-rtp.h>
68 #include <ui/rtp_media.h>
70 # include <speex/speex_resampler.h>
72 # include <codecs/speex/speex_resampler.h>
73 #endif /* HAVE_SPEEXDSP */
77 # include <epan/geoip_db.h>
78 # include <wsutil/pint.h>
81 #include <wsutil/glib-compat.h>
82 #include <wsutil/strtoi.h>
87 json_unescape_str(char *input)
89 return wsjsmn_unescape_json_string(input, input);
93 json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
97 for (i = 0; i < count; i += 2)
99 const char *tok_attr = &buf[tokens[i + 0].start];
100 const char *tok_value = &buf[tokens[i + 1].start];
102 if (!strcmp(tok_attr, attr))
110 json_puts_string(const char *str)
118 for (i = 0; str[i]; i++)
143 json_print_base64_step(const guint8 *data, int *state1, int *state2)
145 gchar buf[(1 / 3 + 1) * 4 + 4 + 1];
149 wrote = g_base64_encode_step(data, 1, FALSE, buf, state1, state2);
151 wrote = g_base64_encode_close(FALSE, buf, state1, state2);
161 json_print_base64(const guint8 *data, size_t len)
164 int base64_state1 = 0;
165 int base64_state2 = 0;
169 for (i = 0; i < len; i++)
170 json_print_base64_step(&data[i], &base64_state1, &base64_state2);
172 json_print_base64_step(NULL, &base64_state1, &base64_state2);
179 struct filter_item *next;
185 static struct filter_item *filter_list = NULL;
187 static const guint8 *
188 sharkd_session_filter_data(const char *filter)
190 struct filter_item *l;
192 for (l = filter_list; l; l = l->next)
194 if (!strcmp(l->filter, filter))
199 guint8 *filtered = NULL;
201 int ret = sharkd_filter(filter, &filtered);
206 l = (struct filter_item *) g_malloc(sizeof(struct filter_item));
207 l->filter = g_strdup(filter);
208 l->filtered = filtered;
210 l->next = filter_list;
217 struct sharkd_rtp_match
219 guint32 addr_src, addr_dst;
228 sharkd_rtp_match_init(struct sharkd_rtp_match *req, const char *init_str)
230 gboolean ret = FALSE;
233 arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
234 if (g_strv_length(arr) != 5)
237 /* TODO, for now only IPv4 */
238 if (!get_host_ipaddr(arr[0], &req->addr_src))
241 if (!ws_strtou16(arr[1], NULL, &req->src_port))
244 if (!get_host_ipaddr(arr[2], &req->addr_dst))
247 if (!ws_strtou16(arr[3], NULL, &req->dst_port))
250 if (!ws_hexstrtou32(arr[4], NULL, &req->ssrc))
253 set_address(&req->src_addr, AT_IPv4, 4, &req->addr_src);
254 set_address(&req->dst_addr, AT_IPv4, 4, &req->addr_dst);
263 sharkd_rtp_match_check(const struct sharkd_rtp_match *req, const packet_info *pinfo, const struct _rtp_info *rtp_info)
265 if (rtp_info->info_sync_src == req->ssrc &&
266 pinfo->srcport == req->src_port &&
267 pinfo->destport == req->dst_port &&
268 addresses_equal(&pinfo->src, &req->src_addr) &&
269 addresses_equal(&pinfo->dst, &req->dst_addr))
278 sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata)
280 stat_tap_table_ui *new_stat_tap = (stat_tap_table_ui *) value;
281 int *pi = (int *) userdata;
283 printf("%s{", (*pi) ? "," : "");
284 printf("\"name\":\"%s\"", new_stat_tap->title);
285 printf(",\"tap\":\"nstat:%s\"", (const char *) key);
293 sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata)
295 struct register_ct *table = (struct register_ct *) value;
296 int *pi = (int *) userdata;
298 const char *label = (const char*)key;
300 if (get_conversation_packet_func(table))
302 printf("%s{", (*pi) ? "," : "");
303 printf("\"name\":\"Conversation List/%s\"", label);
304 printf(",\"tap\":\"conv:%s\"", label);
310 if (get_hostlist_packet_func(table))
312 printf("%s{", (*pi) ? "," : "");
313 printf("\"name\":\"Endpoint/%s\"", label);
314 printf(",\"tap\":\"endpt:%s\"", label);
323 sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata)
325 register_analysis_t *analysis = (register_analysis_t*) value;
326 int *pi = (int *) userdata;
328 printf("%s{", (*pi) ? "," : "");
329 printf("\"name\":\"%s\"", sequence_analysis_get_ui_name(analysis));
330 printf(",\"tap\":\"seqa:%s\"", (const char *) key);
338 sharkd_export_object_visit_cb(const void *key _U_, void *value, void *user_data)
340 register_eo_t *eo = (register_eo_t*)value;
341 int *pi = (int *) user_data;
343 const int proto_id = get_eo_proto_id(eo);
344 const char *filter = proto_get_protocol_filter_name(proto_id);
345 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
347 printf("%s{", (*pi) ? "," : "");
348 printf("\"name\":\"Export Object/%s\"", label);
349 printf(",\"tap\":\"eo:%s\"", filter);
357 sharkd_srt_visit_cb(const void *key _U_, void *value, void *user_data)
359 register_srt_t *srt = (register_srt_t *) value;
360 int *pi = (int *) user_data;
362 const int proto_id = get_srt_proto_id(srt);
363 const char *filter = proto_get_protocol_filter_name(proto_id);
364 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
366 printf("%s{", (*pi) ? "," : "");
367 printf("\"name\":\"Service Response Time/%s\"", label);
368 printf(",\"tap\":\"srt:%s\"", filter);
376 sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data)
378 register_rtd_t *rtd = (register_rtd_t *) value;
379 int *pi = (int *) user_data;
381 const int proto_id = get_rtd_proto_id(rtd);
382 const char *filter = proto_get_protocol_filter_name(proto_id);
383 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
385 printf("%s{", (*pi) ? "," : "");
386 printf("\"name\":\"Response Time Delay/%s\"", label);
387 printf(",\"tap\":\"rtd:%s\"", filter);
395 sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data)
397 register_follow_t *follower = (register_follow_t*) value;
398 int *pi = (int *) user_data;
400 const int proto_id = get_follow_proto_id(follower);
401 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
402 const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
404 printf("%s{", (*pi) ? "," : "");
405 printf("\"name\":\"Follow/%s\"", label);
406 printf(",\"tap\":\"follow:%s\"", filter);
414 * sharkd_session_process_info()
416 * Process info request
418 * Output object with attributes:
419 * (m) columns - available column formats, array of object with attributes:
420 * 'name' - column name
421 * 'format' - column format-name
423 * (m) stats - available statistics, array of object with attributes:
424 * 'name' - statistic name
425 * 'tap' - sharkd tap-name for statistic
427 * (m) convs - available conversation list, array of object with attributes:
428 * 'name' - conversation name
429 * 'tap' - sharkd tap-name for conversation
431 * (m) eo - available export object list, array of object with attributes:
432 * 'name' - export object name
433 * 'tap' - sharkd tap-name for eo
435 * (m) srt - available service response time list, array of object with attributes:
436 * 'name' - service response time name
437 * 'tap' - sharkd tap-name for srt
439 * (m) rtd - available response time delay list, array of object with attributes:
440 * 'name' - response time delay name
441 * 'tap' - sharkd tap-name for rtd
443 * (m) seqa - available sequence analysis (flow) list, array of object with attributes:
444 * 'name' - sequence analysis name
445 * 'tap' - sharkd tap-name
447 * (m) taps - available taps, array of object with attributes:
449 * 'tap' - sharkd tap-name
451 * (m) follow - available followers, array of object with attributes:
453 * 'tap' - sharkd tap-name
455 * (m) ftypes - conversation table for FT_ number to string
458 sharkd_session_process_info(void)
462 printf("{\"columns\":[");
463 for (i = 0; i < NUM_COL_FMTS; i++)
465 const char *col_format = col_format_to_string(i);
466 const char *col_descr = col_format_desc(i);
468 printf("%s{", (i) ? "," : "");
469 printf("\"name\":\"%s\"", col_descr);
470 printf(",\"format\":\"%s\"", col_format);
475 printf(",\"stats\":[");
477 GList *cfg_list = stats_tree_get_cfg_list();
479 const char *sepa = "";
481 for (l = cfg_list; l; l = l->next)
483 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
486 printf("\"name\":\"%s\"", cfg->name);
487 printf(",\"tap\":\"stat:%s\"", cfg->abbr);
492 g_list_free(cfg_list);
496 printf(",\"ftypes\":[");
497 for (i = 0; i < FT_NUM_TYPES; i++)
501 json_puts_string(ftype_name((ftenum_t) i));
505 printf(",\"version\":");
506 json_puts_string(sharkd_version());
508 printf(",\"nstat\":[");
510 new_stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
513 printf(",\"convs\":[");
515 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
518 printf(",\"seqa\":[");
520 sequence_analysis_table_iterate_tables(sharkd_session_seq_analysis_cb, &i);
523 printf(",\"taps\":[");
525 printf("{\"name\":\"%s\",\"tap\":\"%s\"}", "RTP streams", "rtp-streams");
526 printf(",{\"name\":\"%s\",\"tap\":\"%s\"}", "Expert Information", "expert");
532 eo_iterate_tables(sharkd_export_object_visit_cb, &i);
535 printf(",\"srt\":[");
537 srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
540 printf(",\"rtd\":[");
542 rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
545 printf(",\"follow\":[");
547 follow_iterate_followers(sharkd_follower_visit_cb, &i);
554 * sharkd_session_process_load()
556 * Process load request
559 * (m) file - file to be loaded
561 * Output object with attributes:
562 * (m) err - error code
565 sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
567 const char *tok_file = json_find_attr(buf, tokens, count, "file");
570 fprintf(stderr, "load: filename=%s\n", tok_file);
575 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK)
577 printf("{\"err\":%d}\n", err);
583 err = sharkd_load_cap_file();
585 CATCH(OutOfMemoryError)
587 fprintf(stderr, "load: OutOfMemoryError\n");
592 printf("{\"err\":%d}\n", err);
596 * sharkd_session_process_status()
598 * Process status request
600 * Output object with attributes:
601 * (m) frames - count of currently loaded frames
602 * (m) duration - time difference between time of first frame, and last loaded frame
603 * (o) filename - capture filename
604 * (o) filesize - capture filesize
607 sharkd_session_process_status(void)
609 printf("{\"frames\":%u", cfile.count);
611 printf(",\"duration\":%.9f", nstime_to_sec(&cfile.elapsed_time));
615 char *name = g_path_get_basename(cfile.filename);
617 printf(",\"filename\":");
618 json_puts_string(name);
624 gint64 file_size = wtap_file_size(cfile.wth, NULL);
627 printf(",\"filesize\":%" G_GINT64_FORMAT, file_size);
633 struct sharkd_analyse_data
635 GHashTable *protocols_set;
636 nstime_t *first_time;
641 sharkd_session_process_analyse_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
643 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
644 packet_info *pi = &edt->pi;
645 frame_data *fdata = pi->fd;
651 if (analyser->first_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
652 analyser->first_time = &fdata->abs_ts;
654 if (analyser->last_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
655 analyser->last_time = &fdata->abs_ts;
659 wmem_list_frame_t *frame;
661 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
663 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame));
665 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id), NULL, NULL))
667 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id), GUINT_TO_POINTER(proto_id));
669 if (g_hash_table_size(analyser->protocols_set) != 1)
671 json_puts_string(proto_get_protocol_filter_name(proto_id));
679 * sharkd_session_process_status()
681 * Process analyse request
683 * Output object with attributes:
684 * (m) frames - count of currently loaded frames
685 * (m) protocols - protocol list
686 * (m) first - earliest frame time
687 * (m) last - latest frame time
690 sharkd_session_process_analyse(void)
692 unsigned int framenum;
693 struct sharkd_analyse_data analyser;
695 analyser.first_time = NULL;
696 analyser.last_time = NULL;
697 analyser.protocols_set = g_hash_table_new(NULL /* g_direct_hash() */, NULL /* g_direct_equal */);
699 printf("{\"frames\":%u", cfile.count);
701 printf(",\"protocols\":[");
702 for (framenum = 1; framenum <= cfile.count; framenum++)
703 sharkd_dissect_request(framenum, &sharkd_session_process_analyse_cb, 0, 0, 0, &analyser);
706 if (analyser.first_time)
707 printf(",\"first\":%.9f", nstime_to_sec(analyser.first_time));
709 if (analyser.last_time)
710 printf(",\"last\":%.9f", nstime_to_sec(analyser.last_time));
714 g_hash_table_destroy(analyser.protocols_set);
718 sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok_t *tokens, int count)
720 const char *columns_custom[32];
721 guint16 columns_fmt[32];
722 gint16 columns_occur[32];
726 for (i = 0; i < 32; i++)
728 const char *tok_column;
729 char tok_column_name[64];
732 ws_snprintf(tok_column_name, sizeof(tok_column_name), "column%d", i);
733 tok_column = json_find_attr(buf, tokens, count, tok_column_name);
734 if (tok_column == NULL)
737 if ((custom_sepa = strchr(tok_column, ':')))
739 *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
741 columns_fmt[i] = COL_CUSTOM;
742 columns_custom[i] = tok_column;
743 columns_occur[i] = 0;
745 if (!ws_strtoi16(custom_sepa + 1, NULL, &columns_occur[i]))
750 if (!ws_strtou16(tok_column, NULL, &columns_fmt[i]))
753 if (columns_fmt[i] >= NUM_COL_FMTS)
756 /* if custom, that it shouldn't be just custom number -> error */
757 if (columns_fmt[i] == COL_CUSTOM)
764 col_setup(cinfo, cols);
766 for (i = 0; i < cols; i++)
768 col_item_t *col_item = &cinfo->columns[i];
770 col_item->col_fmt = columns_fmt[i];
771 col_item->col_title = NULL; /* no need for title */
773 if (col_item->col_fmt == COL_CUSTOM)
775 col_item->col_custom_fields = g_strdup(columns_custom[i]);
776 col_item->col_custom_occurrence = columns_occur[i];
779 col_item->col_fence = 0;
788 * sharkd_session_process_frames()
790 * Process frames request
793 * (o) column0...columnXX - requested columns either number in range [0..NUM_COL_FMTS), or custom (syntax <dfilter>:<occurence>).
794 * If column0 is not specified default column set will be used.
795 * (o) filter - filter to be used
796 * (o) skip=N - skip N frames
797 * (o) limit=N - show only N frames
799 * Output array of frames with attributes:
800 * (m) c - array of column data
801 * (m) num - frame number
802 * (o) i - if frame is ignored
803 * (o) m - if frame is marked
804 * (o) ct - if frame is commented
805 * (o) bg - color filter - background color in hex
806 * (o) fg - color filter - foreground color in hex
809 sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
811 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
812 const char *tok_column = json_find_attr(buf, tokens, count, "column0");
813 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
814 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
816 const guint8 *filter_data = NULL;
818 const char *frame_sepa = "";
825 column_info *cinfo = &cfile.cinfo;
826 column_info user_cinfo;
830 memset(&user_cinfo, 0, sizeof(user_cinfo));
831 cinfo = sharkd_session_create_columns(&user_cinfo, buf, tokens, count);
838 filter_data = sharkd_session_filter_data(tok_filter);
846 if (!ws_strtou32(tok_skip, NULL, &skip))
853 if (!ws_strtou32(tok_limit, NULL, &limit))
858 for (framenum = 1; framenum <= cfile.count; framenum++)
860 frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum);
862 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
871 sharkd_dissect_columns(framenum, cinfo, (fdata->color_filter == NULL));
873 printf("%s{\"c\":[", frame_sepa);
874 for (col = 0; col < cinfo->num_cols; ++col)
876 const col_item_t *col_item = &cinfo->columns[col];
881 json_puts_string(col_item->col_data);
883 printf("],\"num\":%u", framenum);
885 if (fdata->flags.has_phdr_comment)
886 printf(",\"ct\":true");
888 if (fdata->flags.ignored)
889 printf(",\"i\":true");
891 if (fdata->flags.marked)
892 printf(",\"m\":true");
894 if (fdata->color_filter)
896 printf(",\"bg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->bg_color));
897 printf(",\"fg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->fg_color));
903 if (limit && --limit == 0)
908 if (cinfo != &cfile.cinfo)
913 sharkd_session_process_tap_stats_node_cb(const stat_node *n)
916 const char *sepa = "";
919 for (node = n->children; node; node = node->next)
921 /* code based on stats_tree_get_values_from_node() */
922 printf("%s{\"name\":\"%s\"", sepa, node->name);
923 printf(",\"count\":%d", node->counter);
924 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE) || node->rng))
926 printf(",\"avg\":%.2f", ((float)node->total) / node->counter);
927 printf(",\"min\":%d", node->minvalue);
928 printf(",\"max\":%d", node->maxvalue);
931 if (node->st->elapsed)
932 printf(",\"rate\":%.4f",((float)node->counter) / node->st->elapsed);
934 if (node->parent && node->parent->counter)
935 printf(",\"perc\":%.2f", (node->counter * 100.0) / node->parent->counter);
936 else if (node->parent == &(node->st->root))
937 printf(",\"perc\":100");
939 if (prefs.st_enable_burstinfo && node->max_burst)
941 if (prefs.st_burst_showcount)
942 printf(",\"burstcount\":%d", node->max_burst);
944 printf(",\"burstrate\":%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
946 printf(",\"bursttime\":%.3f", ((double)node->burst_time / 1000.0));
952 sharkd_session_process_tap_stats_node_cb(node);
961 * sharkd_session_process_tap_stats_cb()
966 * (m) type:stats - tap output type
967 * (m) name - stat name
968 * (m) stats - array of object with attributes:
969 * (m) name - stat item name
970 * (m) count - stat item counter
971 * (o) avg - stat item averange value
972 * (o) min - stat item min value
973 * (o) max - stat item max value
974 * (o) rate - stat item rate value (ms)
975 * (o) perc - stat item percentage
976 * (o) burstrate - stat item burst rate
977 * (o) burstcount - stat item burst count
978 * (o) burstttme - stat item burst start
979 * (o) sub - array of object with attributes like in stats node.
982 sharkd_session_process_tap_stats_cb(void *psp)
984 stats_tree *st = (stats_tree *) psp;
986 printf("{\"tap\":\"stats:%s\",\"type\":\"stats\"", st->cfg->abbr);
988 printf(",\"name\":\"%s\",\"stats\":", st->cfg->name);
989 sharkd_session_process_tap_stats_node_cb(&st->root);
994 sharkd_session_free_tap_stats_cb(void *psp)
996 stats_tree *st = (stats_tree *) psp;
1001 struct sharkd_expert_tap
1008 * sharkd_session_process_tap_expert_cb()
1010 * Output expert tap:
1012 * (m) tap - tap name
1013 * (m) type:expert - tap output type
1014 * (m) details - array of object with attributes:
1015 * (m) f - frame number, which generated expert information
1018 * (m) m - expert message
1022 sharkd_session_process_tap_expert_cb(void *tapdata)
1024 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1026 const char *sepa = "";
1028 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "expert", "expert");
1030 printf(",\"details\":[");
1031 for (list = etd->details; list; list = list->next)
1033 expert_info_t *ei = (expert_info_t *) list->data;
1036 printf("%s{", sepa);
1038 printf("\"f\":%u,", ei->packet_num);
1040 tmp = try_val_to_str(ei->severity, expert_severity_vals);
1042 printf("\"s\":\"%s\",", tmp);
1044 tmp = try_val_to_str(ei->group, expert_group_vals);
1046 printf("\"g\":\"%s\",", tmp);
1049 json_puts_string(ei->summary);
1055 json_puts_string(ei->protocol);
1067 sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pointer)
1069 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1070 expert_info_t *ei = (expert_info_t *) pointer;
1072 ei = (expert_info_t *) g_memdup(ei, sizeof(*ei));
1073 ei->protocol = g_string_chunk_insert_const(etd->text, ei->protocol);
1074 ei->summary = g_string_chunk_insert_const(etd->text, ei->summary);
1076 etd->details = g_slist_prepend(etd->details, ei);
1082 sharkd_session_free_tap_expert_cb(void *tapdata)
1084 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1086 g_slist_free_full(etd->details, g_free);
1087 g_string_chunk_free(etd->text);
1092 * sharkd_session_process_tap_flow_cb()
1095 * (m) tap - tap name
1096 * (m) type:flow - tap output type
1097 * (m) nodes - array of strings with node address
1098 * (m) flows - array of object with attributes:
1099 * (m) t - frame time string
1100 * (m) n - array of two numbers with source node index and destination node index
1101 * (m) pn - array of two numbers with source and destination port
1105 sharkd_session_process_tap_flow_cb(void *tapdata)
1107 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1111 const char *sepa = "";
1113 sequence_analysis_get_nodes(graph_analysis);
1115 printf("{\"tap\":\"seqa:%s\",\"type\":\"%s\"", graph_analysis->name, "flow");
1117 printf(",\"nodes\":[");
1118 for (i = 0; i < graph_analysis->num_nodes; i++)
1125 addr_str = address_to_display(NULL, &(graph_analysis->nodes[i]));
1126 json_puts_string(addr_str);
1127 wmem_free(NULL, addr_str);
1131 printf(",\"flows\":[");
1133 flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
1136 seq_analysis_item_t *sai = (seq_analysis_item_t *) flow_list->data;
1138 flow_list = g_list_next(flow_list);
1143 printf("%s{", sepa);
1145 printf("\"t\":\"%s\"", sai->time_str);
1146 printf(",\"n\":[%u,%u]", sai->src_node, sai->dst_node);
1147 printf(",\"pn\":[%u,%u]", sai->port_src, sai->port_dst);
1152 json_puts_string(sai->comment);
1165 sharkd_session_free_tap_flow_cb(void *tapdata)
1167 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1169 sequence_analysis_info_free(graph_analysis);
1172 struct sharkd_conv_tap_data
1176 gboolean resolve_name;
1177 gboolean resolve_port;
1181 sharkd_session_geoip_addr(address *addr, const char *suffix)
1189 if (addr->type == AT_IPv4)
1191 guint32 ip = pntoh32(addr->data);
1193 guint num_dbs = geoip_db_num_dbs();
1196 for (dbnum = 0; dbnum < num_dbs; dbnum++)
1198 const char *geoip_key = NULL;
1201 int db_type = geoip_db_type(dbnum);
1205 case GEOIP_COUNTRY_EDITION:
1206 geoip_key = "geoip_country";
1209 case GEOIP_CITY_EDITION_REV0:
1210 case GEOIP_CITY_EDITION_REV1:
1211 geoip_key = "geoip_city";
1214 case GEOIP_ORG_EDITION:
1215 geoip_key = "geoip_org";
1218 case GEOIP_ISP_EDITION:
1219 geoip_key = "geoip_isp";
1222 case GEOIP_ASNUM_EDITION:
1223 geoip_key = "geoip_as";
1226 case WS_LAT_FAKE_EDITION:
1227 geoip_key = "geoip_lat";
1230 case WS_LON_FAKE_EDITION:
1231 geoip_key = "geoip_lon";
1235 if (geoip_key && (geoip_val = geoip_db_lookup_ipv4(dbnum, ip, NULL)))
1237 printf(",\"%s%s\":", geoip_key, suffix);
1238 json_puts_string(geoip_val);
1243 #ifdef HAVE_GEOIP_V6
1244 if (addr->type == AT_IPv6)
1246 const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
1248 guint num_dbs = geoip_db_num_dbs();
1251 for (dbnum = 0; dbnum < num_dbs; dbnum++)
1253 const char *geoip_key = NULL;
1256 int db_type = geoip_db_type(dbnum);
1260 case GEOIP_COUNTRY_EDITION_V6:
1261 geoip_key = "geoip_country";
1263 #if NUM_DB_TYPES > 31
1264 case GEOIP_CITY_EDITION_REV0_V6:
1265 case GEOIP_CITY_EDITION_REV1_V6:
1266 geoip_key = "geoip_city";
1269 case GEOIP_ORG_EDITION_V6:
1270 geoip_key = "geoip_org";
1273 case GEOIP_ISP_EDITION_V6:
1274 geoip_key = "geoip_isp";
1277 case GEOIP_ASNUM_EDITION_V6:
1278 geoip_key = "geoip_as";
1280 #endif /* DB_NUM_TYPES */
1281 case WS_LAT_FAKE_EDITION:
1282 geoip_key = "geoip_lat";
1285 case WS_LON_FAKE_EDITION:
1286 geoip_key = "geoip_lon";
1290 if (geoip_key && (geoip_val = geoip_db_lookup_ipv6(dbnum, *ip6, NULL)))
1292 printf(",\"%s%s\":", geoip_key, suffix);
1293 json_puts_string(geoip_val);
1298 #endif /* HAVE_GEOIP_V6 */
1299 #endif /* HAVE_GEOIP */
1304 struct sharkd_analyse_rtp_items
1307 guint32 sequence_num;
1315 double arrive_offset;
1317 /* from tap_rtp_stat_t */
1322 struct sharkd_analyse_rtp
1324 const char *tap_name;
1325 struct sharkd_rtp_match rtp;
1329 tap_rtp_stat_t statinfo;
1333 sharkd_session_process_tap_rtp_free_cb(void *tapdata)
1335 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1337 g_slist_free_full(rtp_req->packets, g_free);
1342 sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pointer)
1344 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1345 const struct _rtp_info *rtpinfo = (const struct _rtp_info *) pointer;
1347 if (sharkd_rtp_match_check(&rtp_req->rtp, pinfo, rtpinfo))
1349 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
1350 struct sharkd_analyse_rtp_items *item;
1352 rtp_packet_analyse(statinfo, pinfo, rtpinfo);
1354 item = (struct sharkd_analyse_rtp_items *) g_malloc(sizeof(struct sharkd_analyse_rtp_items));
1356 if (!rtp_req->packets)
1357 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
1359 item->frame_num = pinfo->num;
1360 item->sequence_num = rtpinfo->info_seq_num;
1361 item->delta = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->delta;
1362 item->jitter = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->jitter;
1363 item->skew = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->skew;
1364 item->bandwidth = statinfo->bandwidth;
1365 item->marker = rtpinfo->info_marker_set ? TRUE : FALSE;
1366 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
1368 item->flags = statinfo->flags;
1369 item->pt = statinfo->pt;
1371 /* XXX, O(n) optimize */
1372 rtp_req->packets = g_slist_append(rtp_req->packets, item);
1379 * sharkd_session_process_tap_rtp_analyse_cb()
1381 * Output rtp analyse tap:
1382 * (m) tap - tap name
1383 * (m) type - tap output type
1384 * (m) ssrc - RTP SSRC
1385 * (m) max_delta - Max delta (ms)
1386 * (m) max_delta_nr - Max delta packet #
1387 * (m) max_jitter - Max jitter (ms)
1388 * (m) mean_jitter - Mean jitter (ms)
1389 * (m) max_skew - Max skew (ms)
1390 * (m) total_nr - Total number of RTP packets
1391 * (m) seq_err - Number of sequence errors
1392 * (m) duration - Duration (ms)
1393 * (m) items - array of object with attributes:
1394 * (m) f - frame number
1395 * (m) o - arrive offset
1396 * (m) sn - sequence number
1400 * (m) bw - bandwidth
1401 * (o) s - status string
1402 * (o) t - status type
1403 * (o) mark - rtp mark
1406 sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
1408 const int RTP_TYPE_CN = 1;
1409 const int RTP_TYPE_ERROR = 2;
1410 const int RTP_TYPE_WARN = 3;
1411 const int RTP_TYPE_PT_EVENT = 4;
1413 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1414 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
1416 const char *sepa = "";
1419 printf("{\"tap\":\"%s\",\"type\":\"rtp-analyse\"", rtp_req->tap_name);
1421 printf(",\"ssrc\":%u", rtp_req->rtp.ssrc);
1423 printf(",\"max_delta\":%f", statinfo->max_delta);
1424 printf(",\"max_delta_nr\":%u", statinfo->max_nr);
1425 printf(",\"max_jitter\":%f", statinfo->max_jitter);
1426 printf(",\"mean_jitter\":%f", statinfo->mean_jitter);
1427 printf(",\"max_skew\":%f", statinfo->max_skew);
1428 printf(",\"total_nr\":%u", statinfo->total_nr);
1429 printf(",\"seq_err\":%u", statinfo->sequence);
1430 printf(",\"duration\":%f", statinfo->time - statinfo->start_time);
1432 printf(",\"items\":[");
1433 for (l = rtp_req->packets; l; l = l->next)
1435 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
1437 printf("%s{", sepa);
1439 printf("\"f\":%u", item->frame_num);
1440 printf(",\"o\":%.9f", item->arrive_offset);
1441 printf(",\"sn\":%u", item->sequence_num);
1442 printf(",\"d\":%.2f", item->delta);
1443 printf(",\"j\":%.2f", item->jitter);
1444 printf(",\"sk\":%.2f", item->skew);
1445 printf(",\"bw\":%.2f", item->bandwidth);
1447 if (item->pt == PT_CN)
1448 printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=13, RFC 3389)", RTP_TYPE_CN);
1449 else if (item->pt == PT_CN_OLD)
1450 printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=19, reserved)", RTP_TYPE_CN);
1451 else if (item->flags & STAT_FLAG_WRONG_SEQ)
1452 printf(",\"s\":\"%s\",\"t\":%d", "Wrong sequence number", RTP_TYPE_ERROR);
1453 else if (item->flags & STAT_FLAG_DUP_PKT)
1454 printf(",\"s\":\"%s\",\"t\":%d", "Suspected duplicate (MAC address) only delta time calculated", RTP_TYPE_WARN);
1455 else if (item->flags & STAT_FLAG_REG_PT_CHANGE)
1456 printf(",\"s\":\"Payload changed to PT=%u%s\",\"t\":%d",
1458 (item->flags & STAT_FLAG_PT_T_EVENT) ? " telephone/event" : "",
1460 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP)
1461 printf(",\"s\":\"%s\",\"t\":%d", "Incorrect timestamp", RTP_TYPE_WARN);
1462 else if ((item->flags & STAT_FLAG_PT_CHANGE)
1463 && !(item->flags & STAT_FLAG_FIRST)
1464 && !(item->flags & STAT_FLAG_PT_CN)
1465 && (item->flags & STAT_FLAG_FOLLOW_PT_CN)
1466 && !(item->flags & STAT_FLAG_MARKER))
1468 printf(",\"s\":\"%s\",\"t\":%d", "Marker missing?", RTP_TYPE_WARN);
1470 else if (item->flags & STAT_FLAG_PT_T_EVENT)
1471 printf(",\"s\":\"PT=%u telephone/event\",\"t\":%d", item->pt, RTP_TYPE_PT_EVENT);
1472 else if (item->flags & STAT_FLAG_MARKER)
1473 printf(",\"t\":%d", RTP_TYPE_WARN);
1476 printf(",\"mark\":1");
1487 * sharkd_session_process_tap_conv_cb()
1490 * (m) tap - tap name
1491 * (m) type - tap output type
1492 * (m) proto - protocol short name
1493 * (o) filter - filter string
1495 * (o) convs - array of object with attributes:
1496 * (m) saddr - source address
1497 * (m) daddr - destination address
1498 * (o) sport - source port
1499 * (o) dport - destination port
1500 * (m) txf - TX frame count
1501 * (m) txb - TX bytes
1502 * (m) rxf - RX frame count
1503 * (m) rxb - RX bytes
1504 * (m) start - (relative) first packet time
1505 * (m) stop - (relative) last packet time
1507 * (o) hosts - array of object with attributes:
1508 * (m) host - host address
1509 * (o) port - host port
1510 * (m) txf - TX frame count
1511 * (m) txb - TX bytes
1512 * (m) rxf - RX frame count
1513 * (m) rxb - RX bytes
1516 sharkd_session_process_tap_conv_cb(void *arg)
1518 conv_hash_t *hash = (conv_hash_t *) arg;
1519 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
1521 int proto_with_port;
1526 if (!strncmp(iu->type, "conv:", 5))
1528 printf("{\"tap\":\"%s\",\"type\":\"conv\"", iu->type);
1529 printf(",\"convs\":[");
1530 proto = iu->type + 5;
1532 else if (!strncmp(iu->type, "endpt:", 6))
1534 printf("{\"tap\":\"%s\",\"type\":\"host\"", iu->type);
1535 printf(",\"hosts\":[");
1536 proto = iu->type + 6;
1540 printf("{\"tap\":\"%s\",\"type\":\"err\"", iu->type);
1544 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
1546 if (iu->hash.conv_array != NULL && !strncmp(iu->type, "conv:", 5))
1548 for (i = 0; i < iu->hash.conv_array->len; i++)
1550 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
1551 char *src_addr, *dst_addr;
1552 char *src_port, *dst_port;
1555 printf("%s{", i ? "," : "");
1557 printf("\"saddr\":\"%s\"", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
1558 printf(",\"daddr\":\"%s\"", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
1560 if (proto_with_port)
1562 printf(",\"sport\":\"%s\"", (src_port = get_conversation_port(NULL, iui->src_port, iui->ptype, iu->resolve_port)));
1563 printf(",\"dport\":\"%s\"", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->ptype, iu->resolve_port)));
1565 wmem_free(NULL, src_port);
1566 wmem_free(NULL, dst_port);
1569 printf(",\"rxf\":%" G_GUINT64_FORMAT, iui->rx_frames);
1570 printf(",\"rxb\":%" G_GUINT64_FORMAT, iui->rx_bytes);
1572 printf(",\"txf\":%" G_GUINT64_FORMAT, iui->tx_frames);
1573 printf(",\"txb\":%" G_GUINT64_FORMAT, iui->tx_bytes);
1575 printf(",\"start\":%.9f", nstime_to_sec(&iui->start_time));
1576 printf(",\"stop\":%.9f", nstime_to_sec(&iui->stop_time));
1578 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
1581 printf(",\"filter\":\"%s\"", filter_str);
1585 wmem_free(NULL, src_addr);
1586 wmem_free(NULL, dst_addr);
1588 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
1590 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
1596 else if (iu->hash.conv_array != NULL && !strncmp(iu->type, "endpt:", 6))
1598 for (i = 0; i < iu->hash.conv_array->len; i++)
1600 hostlist_talker_t *host = &g_array_index(iu->hash.conv_array, hostlist_talker_t, i);
1601 char *host_str, *port_str;
1604 printf("%s{", i ? "," : "");
1606 printf("\"host\":\"%s\"", (host_str = get_conversation_address(NULL, &host->myaddress, iu->resolve_name)));
1608 if (proto_with_port)
1610 printf(",\"port\":\"%s\"", (port_str = get_conversation_port(NULL, host->port, host->ptype, iu->resolve_port)));
1612 wmem_free(NULL, port_str);
1615 printf(",\"rxf\":%" G_GUINT64_FORMAT, host->rx_frames);
1616 printf(",\"rxb\":%" G_GUINT64_FORMAT, host->rx_bytes);
1618 printf(",\"txf\":%" G_GUINT64_FORMAT, host->tx_frames);
1619 printf(",\"txb\":%" G_GUINT64_FORMAT, host->tx_bytes);
1621 filter_str = get_hostlist_filter(host);
1624 printf(",\"filter\":\"%s\"", filter_str);
1628 wmem_free(NULL, host_str);
1630 if (sharkd_session_geoip_addr(&(host->myaddress), ""))
1636 printf("],\"proto\":\"%s\",\"geoip\":%s},", proto, with_geoip ? "true" : "false");
1640 sharkd_session_free_tap_conv_cb(void *arg)
1642 conv_hash_t *hash = (conv_hash_t *) arg;
1643 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
1645 if (!strncmp(iu->type, "conv:", 5))
1647 reset_conversation_table_data(hash);
1649 else if (!strncmp(iu->type, "endpt:", 6))
1651 reset_hostlist_table_data(hash);
1658 * sharkd_session_process_tap_nstat_cb()
1661 * (m) tap - tap name
1662 * (m) type - tap output type
1663 * (m) fields: array of objects with attributes:
1666 * (m) tables: array of object with attributes:
1667 * (m) t - table title
1668 * (m) i - array of items
1671 sharkd_session_process_tap_nstat_cb(void *arg)
1673 new_stat_data_t *stat_data = (new_stat_data_t *) arg;
1676 printf("{\"tap\":\"nstat:%s\",\"type\":\"nstat\"", stat_data->stat_tap_data->cli_string);
1678 printf(",\"fields\":[");
1679 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
1681 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
1689 json_puts_string(field->column_name);
1695 printf(",\"tables\":[");
1696 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
1698 stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i);
1699 const char *sepa = "";
1707 printf("\"%s\"", table->title);
1710 for (j = 0; j < table->num_elements; j++)
1712 stat_tap_table_item_type *field_data;
1714 field_data = new_stat_tap_get_field_data(table, j, 0);
1715 if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
1718 printf("%s[", sepa);
1719 for (k = 0; k < table->num_fields; k++)
1721 field_data = new_stat_tap_get_field_data(table, j, k);
1726 switch (field_data->type)
1728 case TABLE_ITEM_UINT:
1729 printf("%u", field_data->value.uint_value);
1732 case TABLE_ITEM_INT:
1733 printf("%d", field_data->value.uint_value);
1736 case TABLE_ITEM_STRING:
1737 json_puts_string(field_data->value.string_value);
1740 case TABLE_ITEM_FLOAT:
1741 printf("%f", field_data->value.float_value);
1744 case TABLE_ITEM_ENUM:
1745 printf("%d", field_data->value.enum_value);
1748 case TABLE_ITEM_NONE:
1765 sharkd_session_free_tap_nstat_cb(void *arg)
1767 new_stat_data_t *stat_data = (new_stat_data_t *) arg;
1769 free_stat_tables(stat_data->stat_tap_data, NULL, NULL);
1773 * sharkd_session_process_tap_rtd_cb()
1776 * (m) tap - tap name
1777 * (m) type - tap output type
1778 * (m) stats - statistics rows - array object with attributes:
1779 * (m) type - statistic name
1780 * (m) num - number of messages
1781 * (m) min - minimum SRT time
1782 * (m) max - maximum SRT time
1783 * (m) tot - total SRT time
1784 * (m) min_frame - minimal SRT
1785 * (m) max_frame - maximum SRT
1786 * (o) open_req - Open Requests
1787 * (o) disc_rsp - Discarded Responses
1788 * (o) req_dup - Duplicated Requests
1789 * (o) rsp_dup - Duplicated Responses
1790 * (o) open_req - Open Requests
1791 * (o) disc_rsp - Discarded Responses
1792 * (o) req_dup - Duplicated Requests
1793 * (o) rsp_dup - Duplicated Responses
1796 sharkd_session_process_tap_rtd_cb(void *arg)
1798 rtd_data_t *rtd_data = (rtd_data_t *) arg;
1799 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
1803 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
1805 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
1806 * some multiple table and single timestat (radius, h225)
1807 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
1808 * I wonder how it will gonna work with multiple timestats and multiple tables...
1809 * (for usage grep for: register_rtd_table)
1811 const value_string *vs = get_rtd_value_string(rtd);
1812 const char *sepa = "";
1814 printf("{\"tap\":\"rtd:%s\",\"type\":\"rtd\"", filter);
1816 if (rtd_data->stat_table.num_rtds == 1)
1818 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
1820 printf(",\"open_req\":%u", ms->open_req_num);
1821 printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
1822 printf(",\"req_dup\":%u", ms->req_dup_num);
1823 printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
1826 printf(",\"stats\":[");
1827 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
1829 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
1831 for (j = 0; j < ms->num_timestat; j++)
1833 const char *type_str;
1835 if (ms->rtd[j].num == 0)
1838 printf("%s{", sepa);
1840 if (rtd_data->stat_table.num_rtds == 1)
1841 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
1843 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
1844 printf("\"type\":");
1845 json_puts_string(type_str);
1847 printf(",\"num\":%u", ms->rtd[j].num);
1848 printf(",\"min\":%.9f", nstime_to_sec(&(ms->rtd[j].min)));
1849 printf(",\"max\":%.9f", nstime_to_sec(&(ms->rtd[j].max)));
1850 printf(",\"tot\":%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
1851 printf(",\"min_frame\":%u", ms->rtd[j].min_num);
1852 printf(",\"max_frame\":%u", ms->rtd[j].max_num);
1854 if (rtd_data->stat_table.num_rtds != 1)
1856 /* like in tshark, display it on every row */
1857 printf(",\"open_req\":%u", ms->open_req_num);
1858 printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
1859 printf(",\"req_dup\":%u", ms->req_dup_num);
1860 printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
1871 sharkd_session_free_tap_rtd_cb(void *arg)
1873 rtd_data_t *rtd_data = (rtd_data_t *) arg;
1875 free_rtd_table(&rtd_data->stat_table, NULL, NULL);
1880 * sharkd_session_process_tap_srt_cb()
1883 * (m) tap - tap name
1884 * (m) type - tap output type
1886 * (m) tables - array of object with attributes:
1887 * (m) n - table name
1888 * (m) f - table filter
1889 * (o) c - table column name
1890 * (m) r - table rows - array object with attributes:
1892 * (m) idx - procedure index
1893 * (m) num - number of events
1894 * (m) min - minimum SRT time
1895 * (m) max - maximum SRT time
1896 * (m) tot - total SRT time
1899 sharkd_session_process_tap_srt_cb(void *arg)
1901 srt_data_t *srt_data = (srt_data_t *) arg;
1902 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
1904 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
1908 printf("{\"tap\":\"srt:%s\",\"type\":\"srt\"", filter);
1910 printf(",\"tables\":[");
1911 for (i = 0; i < srt_data->srt_array->len; i++)
1914 srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i);
1915 const char *sepa = "";
1925 json_puts_string(rst->name);
1926 else if (rst->short_name)
1927 json_puts_string(rst->short_name);
1929 printf("\"table%u\"", i);
1931 if (rst->filter_string)
1934 json_puts_string(rst->filter_string);
1937 if (rst->proc_column_name)
1940 json_puts_string(rst->proc_column_name);
1944 for (j = 0; j < rst->num_procs; j++)
1947 srt_procedure_t *proc = &rst->procedures[j];
1949 if (proc->stats.num == 0)
1952 printf("%s{", sepa);
1955 json_puts_string(proc->procedure);
1957 if (rst->filter_string)
1958 printf(",\"idx\":%d", proc->proc_index);
1960 printf(",\"num\":%u", proc->stats.num);
1962 printf(",\"min\":%.9f", nstime_to_sec(&proc->stats.min));
1963 printf(",\"max\":%.9f", nstime_to_sec(&proc->stats.max));
1964 printf(",\"tot\":%.9f", nstime_to_sec(&proc->stats.tot));
1976 sharkd_session_free_tap_srt_cb(void *arg)
1978 srt_data_t *srt_data = (srt_data_t *) arg;
1979 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
1981 free_srt_table(srt, srt_data->srt_array, NULL, NULL);
1982 g_array_free(srt_data->srt_array, TRUE);
1986 struct sharkd_export_object_list
1988 struct sharkd_export_object_list *next;
1995 static struct sharkd_export_object_list *sharkd_eo_list;
1998 * sharkd_session_process_tap_eo_cb()
2001 * (m) tap - tap name
2002 * (m) type - tap output type
2003 * (m) proto - protocol short name
2004 * (m) objects - array of object with attributes:
2005 * (m) pkt - packet number
2006 * (o) hostname - hostname
2007 * (o) type - content type
2008 * (o) filename - filename
2009 * (m) len - object length
2012 sharkd_session_process_tap_eo_cb(void *tapdata)
2014 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
2015 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list*) tap_object->gui_data;
2019 printf("{\"tap\":\"%s\",\"type\":\"eo\"", object_list->type);
2020 printf(",\"proto\":\"%s\"", object_list->proto);
2021 printf(",\"objects\":[");
2023 for (slist = object_list->entries; slist; slist = slist->next)
2025 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
2027 printf("%s{", i ? "," : "");
2029 printf("\"pkt\":%u", eo_entry->pkt_num);
2031 if (eo_entry->hostname)
2033 printf(",\"hostname\":");
2034 json_puts_string(eo_entry->hostname);
2037 if (eo_entry->content_type)
2039 printf(",\"type\":");
2040 json_puts_string(eo_entry->content_type);
2043 if (eo_entry->filename)
2045 printf(",\"filename\":");
2046 json_puts_string(eo_entry->filename);
2049 printf(",\"_download\":\"%s_%d\"", object_list->type, i);
2051 printf(",\"len\":%" G_GINT64_FORMAT, eo_entry->payload_len);
2062 sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
2064 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2066 object_list->entries = g_slist_append(object_list->entries, entry);
2069 static export_object_entry_t *
2070 sharkd_eo_object_list_get_entry(void *gui_data, int row)
2072 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2074 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
2078 * sharkd_session_process_tap_rtp_cb()
2080 * Output RTP streams tap:
2081 * (m) tap - tap name
2082 * (m) type - tap output type
2083 * (m) streams - array of object with attributes:
2084 * (m) ssrc - RTP synchronization source identifier
2085 * (m) payload - stream payload
2086 * (m) saddr - source address
2087 * (m) sport - source port
2088 * (m) daddr - destination address
2089 * (m) dport - destination port
2090 * (m) pkts - packets count
2091 * (m) max_delta - max delta (ms)
2092 * (m) max_jitter - max jitter (ms)
2093 * (m) mean_jitter - mean jitter (ms)
2096 * (m) problem - if analyser found the problem
2097 * (m) ipver - address IP version (4 or 6)
2100 sharkd_session_process_tap_rtp_cb(void *arg)
2102 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
2105 const char *sepa = "";
2107 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "rtp-streams", "rtp-streams");
2109 printf(",\"streams\":[");
2110 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
2112 rtp_stream_info_t *streaminfo = (rtp_stream_info_t *) listx->data;
2114 char *src_addr, *dst_addr;
2118 src_addr = address_to_display(NULL, &(streaminfo->src_addr));
2119 dst_addr = address_to_display(NULL, &(streaminfo->dest_addr));
2121 if (streaminfo->payload_type_name != NULL)
2122 payload = wmem_strdup(NULL, streaminfo->payload_type_name);
2124 payload = val_to_str_ext_wmem(NULL, streaminfo->payload_type, &rtp_payload_type_short_vals_ext, "Unknown (%u)");
2126 printf("%s{\"ssrc\":%u", sepa, streaminfo->ssrc);
2127 printf(",\"payload\":\"%s\"", payload);
2129 printf(",\"saddr\":\"%s\"", src_addr);
2130 printf(",\"sport\":%u", streaminfo->src_port);
2132 printf(",\"daddr\":\"%s\"", dst_addr);
2133 printf(",\"dport\":%u", streaminfo->dest_port);
2135 printf(",\"pkts\":%u", streaminfo->packet_count);
2137 printf(",\"max_delta\":%f", streaminfo->rtp_stats.max_delta);
2138 printf(",\"max_jitter\":%f", streaminfo->rtp_stats.max_jitter);
2139 printf(",\"mean_jitter\":%f", streaminfo->rtp_stats.mean_jitter);
2141 expected = (streaminfo->rtp_stats.stop_seq_nr + streaminfo->rtp_stats.cycles * 65536) - streaminfo->rtp_stats.start_seq_nr + 1;
2142 printf(",\"expectednr\":%u", expected);
2143 printf(",\"totalnr\":%u", streaminfo->rtp_stats.total_nr);
2145 printf(",\"problem\":%s", streaminfo->problem ? "true" : "false");
2148 printf(",\"ipver\":%d", (streaminfo->src_addr.type == AT_IPv6) ? 6 : 4);
2150 wmem_free(NULL, src_addr);
2151 wmem_free(NULL, dst_addr);
2152 wmem_free(NULL, payload);
2161 * sharkd_session_process_tap()
2163 * Process tap request
2166 * (m) tap0 - First tap request
2167 * (o) tap1...tap15 - Other tap requests
2169 * Output object with attributes:
2170 * (m) taps - array of object with attributes:
2171 * (m) tap - tap name
2172 * (m) type - tap output type
2174 * for type:stats see sharkd_session_process_tap_stats_cb()
2175 * for type:nstat see sharkd_session_process_tap_nstat_cb()
2176 * for type:conv see sharkd_session_process_tap_conv_cb()
2177 * for type:host see sharkd_session_process_tap_conv_cb()
2178 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
2179 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
2180 * for type:eo see sharkd_session_process_tap_eo_cb()
2181 * for type:expert see sharkd_session_process_tap_expert_cb()
2182 * for type:rtd see sharkd_session_process_tap_rtd_cb()
2183 * for type:srt see sharkd_session_process_tap_srt_cb()
2184 * for type:flow see sharkd_session_process_tap_flow_cb()
2186 * (m) err - error code
2189 sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
2191 void *taps_data[16];
2192 GFreeFunc taps_free[16];
2196 rtpstream_tapinfo_t rtp_tapinfo =
2197 {NULL, NULL, NULL, NULL, 0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE};
2199 for (i = 0; i < 16; i++)
2202 const char *tok_tap;
2204 void *tap_data = NULL;
2205 GFreeFunc tap_free = NULL;
2206 const char *tap_filter = "";
2207 GString *tap_error = NULL;
2209 ws_snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
2210 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
2214 if (!strncmp(tok_tap, "stat:", 5))
2216 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
2221 fprintf(stderr, "sharkd_session_process_tap() stat %s not found\n", tok_tap + 5);
2225 st = stats_tree_new(cfg, NULL, tap_filter);
2227 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);
2229 if (!tap_error && cfg->init)
2233 tap_free = sharkd_session_free_tap_stats_cb;
2235 else if (!strcmp(tok_tap, "expert"))
2237 struct sharkd_expert_tap *expert_tap;
2239 expert_tap = g_new0(struct sharkd_expert_tap, 1);
2240 expert_tap->text = g_string_chunk_new(100);
2242 tap_error = register_tap_listener("expert", expert_tap, NULL, 0, NULL, sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb);
2244 tap_data = expert_tap;
2245 tap_free = sharkd_session_free_tap_expert_cb;
2247 else if (!strncmp(tok_tap, "seqa:", 5))
2249 seq_analysis_info_t *graph_analysis;
2250 register_analysis_t *analysis;
2251 const char *tap_name;
2252 tap_packet_cb tap_func;
2255 analysis = sequence_analysis_find_by_name(tok_tap + 5);
2258 fprintf(stderr, "sharkd_session_process_tap() seq analysis %s not found\n", tok_tap + 5);
2262 graph_analysis = sequence_analysis_info_new();
2263 graph_analysis->name = tok_tap + 5;
2264 /* TODO, make configurable */
2265 graph_analysis->any_addr = FALSE;
2267 tap_name = sequence_analysis_get_tap_listener_name(analysis);
2268 tap_flags = sequence_analysis_get_tap_flags(analysis);
2269 tap_func = sequence_analysis_get_packet_func(analysis);
2271 tap_error = register_tap_listener(tap_name, graph_analysis, NULL, tap_flags, NULL, tap_func, sharkd_session_process_tap_flow_cb);
2273 tap_data = graph_analysis;
2274 tap_free = sharkd_session_free_tap_flow_cb;
2276 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
2278 struct register_ct *ct = NULL;
2279 const char *ct_tapname;
2280 struct sharkd_conv_tap_data *ct_data;
2281 tap_packet_cb tap_func = NULL;
2283 if (!strncmp(tok_tap, "conv:", 5))
2285 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
2287 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
2289 fprintf(stderr, "sharkd_session_process_tap() conv %s not found\n", tok_tap + 5);
2293 else if (!strncmp(tok_tap, "endpt:", 6))
2295 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
2297 if (!ct || !(tap_func = get_hostlist_packet_func(ct)))
2299 fprintf(stderr, "sharkd_session_process_tap() endpt %s not found\n", tok_tap + 6);
2305 fprintf(stderr, "sharkd_session_process_tap() conv/endpt(?): %s not found\n", tok_tap);
2309 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
2311 ct_data = (struct sharkd_conv_tap_data *) g_malloc0(sizeof(struct sharkd_conv_tap_data));
2312 ct_data->type = tok_tap;
2313 ct_data->hash.user_data = ct_data;
2315 /* XXX: make configurable */
2316 ct_data->resolve_name = TRUE;
2317 ct_data->resolve_port = TRUE;
2319 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb);
2321 tap_data = &ct_data->hash;
2322 tap_free = sharkd_session_free_tap_conv_cb;
2324 else if (!strncmp(tok_tap, "nstat:", 6))
2326 stat_tap_table_ui *stat_tap = new_stat_tap_by_name(tok_tap + 6);
2327 new_stat_data_t *stat_data;
2331 fprintf(stderr, "sharkd_session_process_tap() nstat=%s not found\n", tok_tap + 6);
2335 stat_tap->stat_tap_init_cb(stat_tap, NULL, NULL);
2337 stat_data = g_new0(new_stat_data_t, 1);
2338 stat_data->stat_tap_data = stat_tap;
2339 stat_data->user_data = NULL;
2341 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);
2343 tap_data = stat_data;
2344 tap_free = sharkd_session_free_tap_nstat_cb;
2346 else if (!strncmp(tok_tap, "rtd:", 4))
2348 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
2349 rtd_data_t *rtd_data;
2354 fprintf(stderr, "sharkd_session_process_tap() rtd=%s not found\n", tok_tap + 4);
2358 rtd_table_get_filter(rtd, "", &tap_filter, &err);
2361 fprintf(stderr, "sharkd_session_process_tap() rtd=%s err=%s\n", tok_tap + 4, err);
2366 rtd_data = g_new0(rtd_data_t, 1);
2367 rtd_data->user_data = rtd;
2368 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL, NULL);
2370 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);
2372 tap_data = rtd_data;
2373 tap_free = sharkd_session_free_tap_rtd_cb;
2375 else if (!strncmp(tok_tap, "srt:", 4))
2377 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
2378 srt_data_t *srt_data;
2383 fprintf(stderr, "sharkd_session_process_tap() srt=%s not found\n", tok_tap + 4);
2387 srt_table_get_filter(srt, "", &tap_filter, &err);
2390 fprintf(stderr, "sharkd_session_process_tap() srt=%s err=%s\n", tok_tap + 4, err);
2395 srt_data = g_new0(srt_data_t, 1);
2396 srt_data->srt_array = g_array_new(FALSE, TRUE, sizeof(srt_stat_table *));
2397 srt_data->user_data = srt;
2398 srt_table_dissector_init(srt, srt_data->srt_array, NULL, NULL);
2400 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);
2402 tap_data = srt_data;
2403 tap_free = sharkd_session_free_tap_srt_cb;
2405 else if (!strncmp(tok_tap, "eo:", 3))
2407 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
2408 export_object_list_t *eo_object;
2409 struct sharkd_export_object_list *object_list;
2413 fprintf(stderr, "sharkd_session_process_tap() eo=%s not found\n", tok_tap + 3);
2417 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
2419 if (!strcmp(object_list->type, tok_tap))
2421 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
2422 object_list->entries = NULL;
2429 object_list = g_new(struct sharkd_export_object_list, 1);
2430 object_list->type = g_strdup(tok_tap);
2431 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
2432 object_list->entries = NULL;
2433 object_list->next = sharkd_eo_list;
2434 sharkd_eo_list = object_list;
2437 eo_object = g_new0(export_object_list_t, 1);
2438 eo_object->add_entry = sharkd_eo_object_list_add_entry;
2439 eo_object->get_entry = sharkd_eo_object_list_get_entry;
2440 eo_object->gui_data = (void *) object_list;
2442 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);
2444 tap_data = eo_object;
2445 tap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
2447 else if (!strcmp(tok_tap, "rtp-streams"))
2449 tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet, sharkd_session_process_tap_rtp_cb);
2451 tap_data = &rtp_tapinfo;
2452 tap_free = rtpstream_reset_cb;
2454 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
2456 struct sharkd_analyse_rtp *rtp_req;
2458 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
2459 if (!sharkd_rtp_match_init(&rtp_req->rtp, tok_tap + 12))
2465 rtp_req->tap_name = tok_tap;
2466 rtp_req->statinfo.first_packet = TRUE;
2467 rtp_req->statinfo.reg_pt = PT_UNDEFINED;
2469 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);
2472 tap_free = sharkd_session_process_tap_rtp_free_cb;
2476 fprintf(stderr, "sharkd_session_process_tap() %s not recognized\n", tok_tap);
2482 fprintf(stderr, "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str);
2483 g_string_free(tap_error, TRUE);
2489 taps_data[taps_count] = tap_data;
2490 taps_free[taps_count] = tap_free;
2494 fprintf(stderr, "sharkd_session_process_tap() count=%d\n", taps_count);
2495 if (taps_count == 0)
2498 printf("{\"taps\":[");
2500 printf("null],\"err\":0}\n");
2502 for (i = 0; i < taps_count; i++)
2505 remove_tap_listener(taps_data[i]);
2508 taps_free[i](taps_data[i]);
2513 * sharkd_session_process_follow()
2515 * Process follow request
2518 * (m) follow - follow protocol request (e.g. HTTP)
2519 * (m) filter - filter request (e.g. tcp.stream == 1)
2521 * Output object with attributes:
2523 * (m) err - error code
2524 * (m) shost - server host
2525 * (m) sport - server port
2526 * (m) sbytes - server send bytes count
2527 * (m) chost - client host
2528 * (m) cport - client port
2529 * (m) cbytes - client send bytes count
2530 * (o) payloads - array of object with attributes:
2531 * (o) s - set if server sent, else client
2532 * (m) n - packet number
2533 * (m) d - data base64 encoded
2536 sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
2538 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
2539 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2541 register_follow_t *follower;
2544 follow_info_t *follow_info;
2548 if (!tok_follow || !tok_filter)
2551 follower = get_follow_by_name(tok_follow);
2554 fprintf(stderr, "sharkd_session_process_follow() follower=%s not found\n", tok_follow);
2558 /* follow_reset_stream ? */
2559 follow_info = g_new0(follow_info_t, 1);
2560 /* gui_data, filter_out_filter not set, but not used by dissector */
2562 tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL, get_follow_tap_handler(follower), NULL);
2565 fprintf(stderr, "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str);
2566 g_string_free(tap_error, TRUE);
2567 g_free(follow_info);
2575 printf("\"err\":0");
2577 /* Server information: hostname, port, bytes sent */
2578 host = address_to_name(&follow_info->server_ip);
2579 printf(",\"shost\":");
2580 json_puts_string(host);
2582 port = get_follow_port_to_display(follower)(NULL, follow_info->server_port);
2583 printf(",\"sport\":");
2584 json_puts_string(port);
2585 wmem_free(NULL, port);
2587 printf(",\"sbytes\":%u", follow_info->bytes_written[0]);
2589 /* Client information: hostname, port, bytes sent */
2590 host = address_to_name(&follow_info->client_ip);
2591 printf(",\"chost\":");
2592 json_puts_string(host);
2594 port = get_follow_port_to_display(follower)(NULL, follow_info->client_port);
2595 printf(",\"cport\":");
2596 json_puts_string(port);
2597 wmem_free(NULL, port);
2599 printf(",\"cbytes\":%u", follow_info->bytes_written[1]);
2601 if (follow_info->payload)
2603 follow_record_t *follow_record;
2605 const char *sepa = "";
2607 printf(",\"payloads\":[");
2609 for (cur = follow_info->payload; cur; cur = g_list_next(cur))
2611 follow_record = (follow_record_t *) cur->data;
2613 printf("%s{", sepa);
2615 printf("\"n\":%u", follow_record->packet_num);
2618 json_print_base64(follow_record->data->data, follow_record->data->len);
2620 if (follow_record->is_server)
2621 printf(",\"s\":%d", 1);
2632 remove_tap_listener(follow_info);
2633 follow_info_free(follow_info);
2637 sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs)
2640 const char *sepa = "";
2643 for (node = tree->first_child; node; node = node->next)
2645 field_info *finfo = PNODE_FINFO(node);
2650 /* XXX, for now always skip hidden */
2651 if (FI_GET_FLAG(finfo, FI_HIDDEN))
2654 printf("%s{", sepa);
2659 char label_str[ITEM_LABEL_LENGTH];
2661 label_str[0] = '\0';
2662 proto_item_fill_label(finfo, label_str);
2663 json_puts_string(label_str);
2667 json_puts_string(finfo->rep->representation);
2670 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
2674 for (idx = 1; tvbs[idx]; idx++)
2676 if (tvbs[idx] == finfo->ds_tvb)
2678 printf(",\"ds\":%d", idx);
2684 if (finfo->start >= 0 && finfo->length > 0)
2685 printf(",\"h\":[%d,%d]", finfo->start, finfo->length);
2687 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
2688 printf(",\"i\":[%d,%d]", finfo->appendix_start, finfo->appendix_length);
2695 if (finfo->hfinfo->type == FT_PROTOCOL)
2697 printf(",\"t\":\"proto\"");
2699 else if (finfo->hfinfo->type == FT_FRAMENUM)
2701 printf(",\"t\":\"framenum\",\"fnum\":%u", finfo->value.value.uinteger);
2703 else if (FI_GET_FLAG(finfo, FI_URL) && IS_FT_STRING(finfo->hfinfo->type))
2705 char *url = fvalue_to_string_repr(NULL, &finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
2707 printf(",\"t\":\"url\",\"url\":");
2708 json_puts_string(url);
2709 wmem_free(NULL, url);
2712 filter = proto_construct_match_selected_string(finfo, edt);
2716 json_puts_string(filter);
2717 wmem_free(NULL, filter);
2721 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
2723 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK), expert_severity_vals);
2725 g_assert(severity != NULL);
2727 printf(",\"s\":\"%s\"", severity);
2730 if (((proto_tree *) node)->first_child) {
2731 if (finfo->tree_type != -1)
2732 printf(",\"e\":%d", finfo->tree_type);
2734 sharkd_session_process_frame_cb_tree(edt, (proto_tree *) node, tvbs);
2744 sharkd_follower_visit_layers_cb(const void *key _U_, void *value, void *user_data)
2746 register_follow_t *follower = (register_follow_t *) value;
2747 packet_info *pi = (packet_info *) user_data;
2749 const int proto_id = get_follow_proto_id(follower);
2751 guint32 ignore_stream;
2753 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
2755 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
2756 char *follow_filter;
2758 follow_filter = get_follow_conv_func(follower)(pi, &ignore_stream);
2760 printf(",[\"%s\",", layer_proto);
2761 json_puts_string(follow_filter);
2764 g_free(follow_filter);
2771 sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
2773 packet_info *pi = &edt->pi;
2774 frame_data *fdata = pi->fd;
2775 const char *pkt_comment = NULL;
2781 printf("\"err\":0");
2783 if (fdata->flags.has_phdr_comment)
2784 pkt_comment = pi->phdr->opt_comment;
2788 printf(",\"comment\":");
2789 json_puts_string(pkt_comment);
2794 tvbuff_t **tvbs = NULL;
2796 printf(",\"tree\":");
2798 /* arrayize data src, to speedup searching for ds_tvb index */
2799 if (data_src && data_src->next /* only needed if there are more than one data source */)
2801 guint count = g_slist_length((GSList *) data_src);
2804 tvbs = (tvbuff_t **) g_malloc((count + 1) * sizeof(*tvbs));
2806 for (i = 0; i < count; i++)
2808 struct data_source *src = (struct data_source *) g_slist_nth_data((GSList *) data_src, i);
2810 tvbs[i] = get_data_source_tvb(src);
2816 sharkd_session_process_frame_cb_tree(edt, tree, tvbs);
2825 printf(",\"col\":[");
2826 for (col = 0; col < cinfo->num_cols; ++col)
2828 const col_item_t *col_item = &cinfo->columns[col];
2830 printf("%s\"%s\"", (col) ? "," : "", col_item->col_data);
2837 struct data_source *src = (struct data_source *)data_src->data;
2838 const char *ds_sepa = NULL;
2843 tvb = get_data_source_tvb(src);
2844 length = tvb_captured_length(tvb);
2846 printf(",\"bytes\":");
2849 const guchar *cp = tvb_get_ptr(tvb, 0, length);
2851 /* XXX pi.fd->flags.encoding */
2852 json_print_base64(cp, length);
2856 json_print_base64("", 0);
2859 data_src = data_src->next;
2862 printf(",\"ds\":[");
2868 src = (struct data_source *)data_src->data;
2871 char *src_name = get_data_source_name(src);
2873 printf("%s{\"name\":", ds_sepa);
2874 json_puts_string(src_name);
2875 wmem_free(NULL, src_name);
2878 tvb = get_data_source_tvb(src);
2879 length = tvb_captured_length(tvb);
2881 printf(",\"bytes\":");
2884 const guchar *cp = tvb_get_ptr(tvb, 0, length);
2886 /* XXX pi.fd->flags.encoding */
2887 json_print_base64(cp, length);
2891 json_print_base64("", 0);
2897 data_src = data_src->next;
2900 /* close ds, only if was opened */
2901 if (ds_sepa != NULL)
2905 printf(",\"fol\":[0");
2906 follow_iterate_followers(sharkd_follower_visit_layers_cb, pi);
2913 * sharkd_session_process_intervals()
2915 * Process intervals request - generate basic capture file statistics per requested interval.
2918 * (o) interval - interval time in ms, if not specified: 1000ms
2919 * (o) filter - filter for generating interval request
2921 * Output object with attributes:
2922 * (m) intervals - array of intervals, with indexes:
2923 * [0] - index of interval,
2924 * [1] - number of frames during interval,
2925 * [2] - number of bytes during interval.
2927 * (m) last - last interval number.
2928 * (m) frames - total number of frames
2929 * (m) bytes - total number of bytes
2931 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
2934 sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
2936 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
2937 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2939 const guint8 *filter_data = NULL;
2943 unsigned int frames;
2947 nstime_t *start_ts = NULL;
2949 guint32 interval_ms = 1000; /* default: one per second */
2951 const char *sepa = "";
2952 unsigned int framenum;
2957 if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0) {
2958 fprintf(stderr, "Invalid interval parameter: %s.\n", tok_interval);
2965 filter_data = sharkd_session_filter_data(tok_filter);
2970 st_total.frames = 0;
2978 printf("{\"intervals\":[");
2980 for (framenum = 1; framenum <= cfile.count; framenum++)
2982 frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum);
2986 if (start_ts == NULL)
2987 start_ts = &fdata->abs_ts;
2989 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
2992 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (gint64) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
2993 new_idx = msec_rel / interval_ms;
2999 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
3012 st.bytes += fdata->pkt_len;
3014 st_total.frames += 1;
3015 st_total.bytes += fdata->pkt_len;
3020 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
3024 printf("],\"last\":%" G_GINT64_FORMAT ",\"frames\":%u,\"bytes\":%" G_GUINT64_FORMAT "}\n", max_idx, st_total.frames, st_total.bytes);
3028 * sharkd_session_process_frame()
3030 * Process frame request
3033 * (m) frame - requested frame number
3034 * (o) proto - set if output frame tree
3035 * (o) columns - set if output frame columns
3036 * (o) bytes - set if output frame bytes
3038 * Output object with attributes:
3039 * (m) err - 0 if succeed
3040 * (o) tree - array of frame nodes with attributes:
3042 * t: 'proto', 'framenum', 'url' - type of node
3045 * e - subtree ett index
3046 * n - array of subtree nodes
3047 * h - two item array: (item start, item length)
3048 * i - two item array: (appendix start, appendix length)
3049 * p - [RESERVED] two item array: (protocol start, protocol length)
3050 * ds- data src index
3051 * url - only for t:'url', url
3052 * fnum - only for t:'framenum', frame number
3054 * (o) col - array of column data
3055 * (o) bytes - base64 of frame bytes
3056 * (o) ds - array of other data srcs
3057 * (o) comment - frame comment
3058 * (o) fol - array of follow filters:
3060 * [1] - filter string
3063 sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
3065 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
3066 int tok_proto = (json_find_attr(buf, tokens, count, "proto") != NULL);
3067 int tok_bytes = (json_find_attr(buf, tokens, count, "bytes") != NULL);
3068 int tok_columns = (json_find_attr(buf, tokens, count, "columns") != NULL);
3072 if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
3075 sharkd_dissect_request(framenum, &sharkd_session_process_frame_cb, tok_bytes, tok_columns, tok_proto, NULL);
3079 * sharkd_session_process_check()
3081 * Process check request.
3084 * (o) filter - filter to be checked
3086 * Output object with attributes:
3087 * (m) err - always 0
3088 * (o) filter - 'ok', 'warn' or error message
3091 sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
3093 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
3095 printf("{\"err\":0");
3096 if (tok_filter != NULL)
3098 char *err_msg = NULL;
3101 if (dfilter_compile(tok_filter, &dfp, &err_msg))
3103 const char *s = "ok";
3105 if (dfilter_deprecated_tokens(dfp))
3108 printf(",\"filter\":\"%s\"", s);
3113 printf(",\"filter\":");
3114 json_puts_string(err_msg);
3123 struct sharkd_session_process_complete_pref_data
3131 sharkd_session_process_complete_pref_cb(module_t *module, gpointer d)
3133 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
3135 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
3138 printf("%s{\"f\":\"%s\",\"d\":\"%s\"}", data->sepa, module->name, module->title);
3145 sharkd_session_process_complete_pref_option_cb(pref_t *pref, gpointer d)
3147 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
3148 const char *pref_name = prefs_get_name(pref);
3149 const char *pref_title = prefs_get_title(pref);
3151 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
3154 printf("%s{\"f\":\"%s.%s\",\"d\":\"%s\"}", data->sepa, data->module, pref_name, pref_title);
3157 return 0; /* continue */
3161 * sharkd_session_process_complete()
3163 * Process complete request
3166 * (o) field - field to be completed
3167 * (o) pref - preference to be completed
3169 * Output object with attributes:
3170 * (m) err - always 0
3171 * (o) field - array of object with attributes:
3172 * (m) f - field text
3173 * (o) t - field type (FT_ number)
3174 * (o) n - field name
3175 * (o) pref - array of object with attributes:
3177 * (o) d - pref description
3180 sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
3182 const char *tok_field = json_find_attr(buf, tokens, count, "field");
3183 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
3185 printf("{\"err\":0");
3186 if (tok_field != NULL && tok_field[0])
3188 const size_t filter_length = strlen(tok_field);
3189 const int filter_with_dot = !!strchr(tok_field, '.');
3194 const char *sepa = "";
3196 printf(",\"field\":[");
3198 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
3200 protocol_t *protocol = find_protocol_by_id(proto_id);
3201 const char *protocol_filter;
3202 const char *protocol_name;
3203 header_field_info *hfinfo;
3205 if (!proto_is_protocol_enabled(protocol))
3208 protocol_name = proto_get_protocol_long_name(protocol);
3209 protocol_filter = proto_get_protocol_filter_name(proto_id);
3211 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
3213 printf("%s{", sepa);
3216 json_puts_string(protocol_filter);
3217 printf(",\"t\":%d", FT_PROTOCOL);
3219 json_puts_string(protocol_name);
3225 if (!filter_with_dot)
3228 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL; hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
3230 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
3233 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
3235 printf("%s{", sepa);
3238 json_puts_string(hfinfo->abbrev);
3240 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
3241 if (hfinfo->same_name_next == NULL)
3243 printf(",\"t\":%d", hfinfo->type);
3245 json_puts_string(hfinfo->name);
3257 if (tok_pref != NULL && tok_pref[0])
3259 struct sharkd_session_process_complete_pref_data data;
3262 data.module = tok_pref;
3263 data.pref = tok_pref;
3266 printf(",\"pref\":[");
3268 if ((dot_sepa = strchr(tok_pref, '.')))
3272 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
3273 data.pref = dot_sepa + 1;
3275 pref_mod = prefs_find_module(data.module);
3277 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
3283 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
3295 * sharkd_session_process_setconf()
3297 * Process setconf request
3300 * (m) name - preference name
3301 * (m) value - preference value
3303 * Output object with attributes:
3304 * (m) err - error code: 0 succeed
3307 sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
3309 const char *tok_name = json_find_attr(buf, tokens, count, "name");
3310 const char *tok_value = json_find_attr(buf, tokens, count, "value");
3312 char *errmsg = NULL;
3314 prefs_set_pref_e ret;
3316 if (!tok_name || tok_name[0] == '\0' || !tok_value)
3319 ws_snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
3321 ret = prefs_set_pref(pref, &errmsg);
3322 printf("{\"err\":%d", ret);
3325 /* Add error message for some syntax errors. */
3326 printf(",\"errmsg\":");
3327 json_puts_string(errmsg);
3333 struct sharkd_session_process_dumpconf_data
3340 sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d)
3342 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
3343 const char *pref_name = prefs_get_name(pref);
3345 printf("%s\"%s.%s\":{", data->sepa, data->module->name, pref_name);
3347 switch (prefs_get_type(pref))
3350 case PREF_DECODE_AS_UINT:
3351 printf("\"u\":%u", prefs_get_uint_value_real(pref, pref_current));
3352 if (prefs_get_uint_base(pref) != 10)
3353 printf(",\"ub\":%u", prefs_get_uint_base(pref));
3357 printf("\"b\":%s", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
3361 case PREF_SAVE_FILENAME:
3362 case PREF_OPEN_FILENAME:
3365 json_puts_string(prefs_get_string_value(pref, pref_current));
3370 const enum_val_t *enums;
3371 const char *enum_sepa = "";
3374 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
3376 printf("%s{\"v\":%d", enum_sepa, enums->value);
3378 if (enums->value == prefs_get_enum_value(pref, pref_current))
3382 json_puts_string(enums->description);
3392 case PREF_DECODE_AS_RANGE:
3394 char *range_str = range_convert_range(NULL, prefs_get_range_value_real(pref, pref_current));
3395 printf("\"r\":\"%s\"", range_str);
3396 wmem_free(NULL, range_str);
3402 uat_t *uat = prefs_get_uat_value(pref);
3406 for (idx = 0; idx < uat->raw_data->len; idx++)
3408 void *rec = UAT_INDEX_PTR(uat, idx);
3415 for (colnum = 0; colnum < uat->ncols; colnum++)
3417 char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
3422 json_puts_string(str);
3435 case PREF_STATIC_TEXT:
3443 json_puts_string(prefs_get_title(pref));
3449 return 0; /* continue */
3453 sharkd_session_process_dumpconf_mod_cb(module_t *module, gpointer d)
3455 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
3457 data->module = module;
3458 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
3464 * sharkd_session_process_dumpconf()
3466 * Process dumpconf request
3469 * (o) pref - module, or preference, NULL for all
3471 * Output object with attributes:
3472 * (o) prefs - object with module preferences
3473 * (m) [KEY] - preference name
3474 * (o) u - preference value (for PREF_UINT, PREF_DECODE_AS_UINT)
3475 * (o) ub - preference value suggested base for display (for PREF_UINT, PREF_DECODE_AS_UINT) and if different than 10
3476 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
3477 * (o) s - preference value (for PREF_STRING, PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME)
3478 * (o) e - preference possible values (only for PREF_ENUM)
3479 * (o) r - preference value (for PREF_RANGE, PREF_DECODE_AS_RANGE)
3480 * (o) t - preference value (only for PREF_UAT)
3483 sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
3485 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
3491 struct sharkd_session_process_dumpconf_data data;
3496 printf("{\"prefs\":{");
3497 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
3502 if ((dot_sepa = strchr(tok_pref, '.')))
3504 pref_t *pref = NULL;
3506 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
3507 pref_mod = prefs_find_module(tok_pref);
3509 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
3514 struct sharkd_session_process_dumpconf_data data;
3516 data.module = pref_mod;
3519 printf("{\"prefs\":{");
3520 sharkd_session_process_dumpconf_cb(pref, &data);
3527 pref_mod = prefs_find_module(tok_pref);
3530 struct sharkd_session_process_dumpconf_data data;
3532 data.module = pref_mod;
3535 printf("{\"prefs\":{");
3536 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
3541 struct sharkd_download_rtp
3543 struct sharkd_rtp_match rtp;
3549 sharkd_rtp_download_free_items(void *ptr)
3551 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
3553 g_free(rtp_packet->info);
3554 g_free(rtp_packet->payload_data);
3559 sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
3561 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
3562 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
3564 static const int sample_bytes_ = sizeof(SAMPLE) / sizeof(char);
3566 guint32 audio_out_rate_ = 0;
3567 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
3568 struct SpeexResamplerState_ *audio_resampler_ = NULL;
3570 gsize resample_buff_len = 0x1000;
3571 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
3572 spx_uint32_t cur_in_rate = 0;
3573 char *write_buff = NULL;
3574 gint64 write_bytes = 0;
3575 unsigned channels = 0;
3576 unsigned sample_rate = 0;
3579 int base64_state1 = 0;
3580 int base64_state2 = 0;
3584 for (l = req->packets; l; l = l->next)
3586 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
3588 SAMPLE *decode_buff = NULL;
3589 size_t decoded_bytes;
3591 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
3592 if (decoded_bytes == 0 || sample_rate == 0)
3594 /* We didn't decode anything. Clean up and prep for the next packet. */
3595 g_free(decode_buff);
3599 if (audio_out_rate_ == 0)
3605 /* First non-zero wins */
3606 audio_out_rate_ = sample_rate;
3608 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
3610 /* write WAVE header */
3611 memset(&wav_hdr, 0, sizeof(wav_hdr));
3612 memcpy(&wav_hdr[0], "RIFF", 4);
3613 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
3614 memcpy(&wav_hdr[8], "WAVE", 4);
3616 memcpy(&wav_hdr[12], "fmt ", 4);
3617 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
3618 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
3621 memcpy(&wav_hdr[22], &tmp16, 2);
3623 tmp32 = sample_rate;
3624 memcpy(&wav_hdr[24], &tmp32, 4);
3626 tmp32 = sample_rate * channels * sample_bytes_;
3627 memcpy(&wav_hdr[28], &tmp32, 4);
3629 tmp16 = channels * sample_bytes_;
3630 memcpy(&wav_hdr[32], &tmp16, 2);
3631 /* bits per sample */
3632 tmp16 = 8 * sample_bytes_;
3633 memcpy(&wav_hdr[34], &tmp16, 2);
3635 memcpy(&wav_hdr[36], "data", 4);
3636 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
3638 for (i = 0; i < (int) sizeof(wav_hdr); i++)
3639 json_print_base64_step(&wav_hdr[i], &base64_state1, &base64_state2);
3642 // Write samples to our file.
3643 write_buff = (char *) decode_buff;
3644 write_bytes = decoded_bytes;
3646 if (audio_out_rate_ != sample_rate)
3648 spx_uint32_t in_len, out_len;
3650 /* Resample the audio to match our previous output rate. */
3651 if (!audio_resampler_)
3653 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL);
3654 speex_resampler_skip_zeros(audio_resampler_);
3655 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
3659 spx_uint32_t audio_out_rate;
3660 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
3662 if (sample_rate != cur_in_rate)
3664 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
3665 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
3668 in_len = (spx_uint32_t)rtp_packet->info->info_payload_len;
3669 out_len = (audio_out_rate_ * (spx_uint32_t)rtp_packet->info->info_payload_len / sample_rate) + (audio_out_rate_ % sample_rate != 0);
3670 if (out_len * sample_bytes_ > resample_buff_len)
3672 while ((out_len * sample_bytes_ > resample_buff_len))
3673 resample_buff_len *= 2;
3674 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
3677 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
3678 write_buff = (char *) resample_buff;
3679 write_bytes = out_len * sample_bytes_;
3682 /* Write the decoded, possibly-resampled audio */
3683 for (i = 0; i < write_bytes; i++)
3684 json_print_base64_step(&write_buff[i], &base64_state1, &base64_state2);
3686 g_free(decode_buff);
3689 json_print_base64_step(NULL, &base64_state1, &base64_state2);
3691 g_free(resample_buff);
3692 g_hash_table_destroy(decoders_hash_);
3696 sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
3698 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
3699 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
3701 /* do not consider RTP packets without a setup frame */
3702 if (rtp_info->info_setup_frame_num == 0)
3705 if (sharkd_rtp_match_check(&req_rtp->rtp, pinfo, rtp_info))
3707 rtp_packet_t *rtp_packet;
3709 rtp_packet = g_new0(rtp_packet_t, 1);
3710 rtp_packet->info = (struct _rtp_info *) g_memdup(rtp_info, sizeof(struct _rtp_info));
3712 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
3713 rtp_packet->payload_data = (guint8 *) g_memdup(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
3715 if (!req_rtp->packets)
3716 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
3718 rtp_packet->frame_num = pinfo->num;
3719 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
3721 /* XXX, O(n) optimize */
3722 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
3729 * sharkd_session_process_download()
3731 * Process download request
3734 * (m) token - token to download
3736 * Output object with attributes:
3737 * (o) file - suggested name of file
3738 * (o) mime - suggested content type
3739 * (o) data - payload base64 encoded
3742 sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
3744 const char *tok_token = json_find_attr(buf, tokens, count, "token");
3749 if (!strncmp(tok_token, "eo:", 3))
3751 struct sharkd_export_object_list *object_list;
3752 const export_object_entry_t *eo_entry = NULL;
3754 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
3756 size_t eo_type_len = strlen(object_list->type);
3758 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
3762 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
3765 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
3772 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
3773 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
3775 printf("{\"file\":");
3776 json_puts_string(filename);
3777 printf(",\"mime\":");
3778 json_puts_string(mime);
3779 printf(",\"data\":");
3780 json_print_base64(eo_entry->payload_data, eo_entry->payload_len);
3784 else if (!strcmp(tok_token, "ssl-secrets"))
3786 char *str = ssl_export_sessions();
3790 const char *mime = "text/plain";
3791 const char *filename = "keylog.txt";
3793 printf("{\"file\":");
3794 json_puts_string(filename);
3795 printf(",\"mime\":");
3796 json_puts_string(mime);
3797 printf(",\"data\":");
3798 json_print_base64(str, strlen(str));
3803 else if (!strncmp(tok_token, "rtp:", 4))
3805 struct sharkd_download_rtp rtp_req;
3808 memset(&rtp_req, 0, sizeof(rtp_req));
3809 if (!sharkd_rtp_match_init(&rtp_req.rtp, tok_token + 4))
3811 fprintf(stderr, "sharkd_session_process_download() rtp tokenizing error %s\n", tok_token);
3815 tap_error = register_tap_listener("rtp", &rtp_req, NULL, 0, NULL, sharkd_session_packet_download_tap_rtp_cb, NULL);
3818 fprintf(stderr, "sharkd_session_process_download() rtp error=%s", tap_error->str);
3819 g_string_free(tap_error, TRUE);
3824 remove_tap_listener(&rtp_req);
3826 if (rtp_req.packets)
3828 const char *mime = "audio/x-wav";
3829 const char *filename = tok_token;
3831 printf("{\"file\":");
3832 json_puts_string(filename);
3833 printf(",\"mime\":");
3834 json_puts_string(mime);
3836 printf(",\"data\":");
3838 sharkd_rtp_download_decode(&rtp_req);
3843 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
3849 sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
3853 /* sanity check, and split strings */
3854 if (count < 1 || tokens[0].type != JSMN_OBJECT)
3856 fprintf(stderr, "sanity check(1): [0] not object\n");
3860 /* don't need [0] token */
3866 fprintf(stderr, "sanity check(2): %d not even\n", count);
3870 for (i = 0; i < count; i += 2)
3872 if (tokens[i].type != JSMN_STRING)
3874 fprintf(stderr, "sanity check(3): [%d] not string\n", i);
3878 if (tokens[i + 1].type != JSMN_STRING && tokens[i + 1].type != JSMN_PRIMITIVE)
3880 fprintf(stderr, "sanity check(3a): [%d] wrong type\n", i + 1);
3884 buf[tokens[i + 0].end] = '\0';
3885 buf[tokens[i + 1].end] = '\0';
3887 /* unescape only value, as keys are simple strings */
3888 if (tokens[i + 1].type == JSMN_STRING && !json_unescape_str(&buf[tokens[i + 1].start]))
3890 fprintf(stderr, "sanity check(3b): [%d] cannot unescape string\n", i + 1);
3896 const char *tok_req = json_find_attr(buf, tokens, count, "req");
3900 fprintf(stderr, "sanity check(4): no \"req\".\n");
3904 if (!strcmp(tok_req, "load"))
3905 sharkd_session_process_load(buf, tokens, count);
3906 else if (!strcmp(tok_req, "status"))
3907 sharkd_session_process_status();
3908 else if (!strcmp(tok_req, "analyse"))
3909 sharkd_session_process_analyse();
3910 else if (!strcmp(tok_req, "info"))
3911 sharkd_session_process_info();
3912 else if (!strcmp(tok_req, "check"))
3913 sharkd_session_process_check(buf, tokens, count);
3914 else if (!strcmp(tok_req, "complete"))
3915 sharkd_session_process_complete(buf, tokens, count);
3916 else if (!strcmp(tok_req, "frames"))
3917 sharkd_session_process_frames(buf, tokens, count);
3918 else if (!strcmp(tok_req, "tap"))
3919 sharkd_session_process_tap(buf, tokens, count);
3920 else if (!strcmp(tok_req, "follow"))
3921 sharkd_session_process_follow(buf, tokens, count);
3922 else if (!strcmp(tok_req, "intervals"))
3923 sharkd_session_process_intervals(buf, tokens, count);
3924 else if (!strcmp(tok_req, "frame"))
3925 sharkd_session_process_frame(buf, tokens, count);
3926 else if (!strcmp(tok_req, "setconf"))
3927 sharkd_session_process_setconf(buf, tokens, count);
3928 else if (!strcmp(tok_req, "dumpconf"))
3929 sharkd_session_process_dumpconf(buf, tokens, count);
3930 else if (!strcmp(tok_req, "download"))
3931 sharkd_session_process_download(buf, tokens, count);
3932 else if (!strcmp(tok_req, "bye"))
3935 fprintf(stderr, "::: req = %s\n", tok_req);
3937 /* reply for every command are 0+ lines of JSON reply (outputed above), finished by empty new line */
3941 * We do an explicit fflush after every line, because
3942 * we want output to be written to the socket as soon
3943 * as the line is complete.
3945 * The stream is fully-buffered by default, so it's
3946 * only flushed when the buffer fills or the FILE *
3947 * is closed. On UN*X, we could set it to be line
3948 * buffered, but the MSVC standard I/O routines don't
3949 * support line buffering - they only support *byte*
3950 * buffering, doing a write for every byte written,
3951 * which is too inefficient, and full buffering,
3952 * which is what you get if you request line buffering.
3959 sharkd_session_main(void)
3962 jsmntok_t *tokens = NULL;
3963 int tokens_max = -1;
3965 fprintf(stderr, "Hello in child.\n");
3967 while (fgets(buf, sizeof(buf), stdin))
3969 /* every command is line seperated JSON */
3972 ret = wsjsmn_parse(buf, NULL, 0);
3975 fprintf(stderr, "invalid JSON -> closing\n");
3979 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
3982 if (tokens == NULL || tokens_max < ret)
3985 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
3988 memset(tokens, 0, ret * sizeof(jsmntok_t));
3990 ret = wsjsmn_parse(buf, tokens, ret);
3993 fprintf(stderr, "invalid JSON(2) -> closing\n");
3997 sharkd_session_process(buf, tokens, ret);
4006 * Editor modelines - http://www.wireshark.org/tools/modelines.html
4011 * indent-tabs-mode: t
4014 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
4015 * :indentSize=8:tabSize=8:noTabs=false: