3 * Copyright (C) 2016 Jakub Zawadzki
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0+
21 #include <wsutil/wsjsmn.h>
22 #include <wsutil/ws_printf.h>
25 #include <epan/epan_dissect.h>
26 #include <epan/exceptions.h>
27 #include <epan/color_filters.h>
28 #include <epan/prefs.h>
29 #include <epan/prefs-int.h>
30 #include <epan/uat-int.h>
31 #include <wiretap/wtap.h>
33 #include <epan/column.h>
35 #include <ui/ssl_key_export.h>
37 #include <epan/stats_tree_priv.h>
38 #include <epan/stat_tap_ui.h>
39 #include <epan/conversation_table.h>
40 #include <epan/sequence_analysis.h>
41 #include <epan/expert.h>
42 #include <epan/export_object.h>
43 #include <epan/follow.h>
44 #include <epan/rtd_table.h>
45 #include <epan/srt_table.h>
47 #include <epan/dissectors/packet-h225.h>
48 #include <epan/rtp_pt.h>
49 #include <ui/voip_calls.h>
50 #include <ui/rtp_stream.h>
51 #include <ui/tap-rtp-common.h>
52 #include <epan/to_str.h>
54 #include <epan/addr_resolv.h>
55 #include <epan/dissectors/packet-rtp.h>
56 #include <ui/rtp_media.h>
58 # include <speex/speex_resampler.h>
60 # include <codecs/speex/speex_resampler.h>
61 #endif /* HAVE_SPEEXDSP */
65 # include <epan/geoip_db.h>
66 # include <wsutil/pint.h>
69 #include <wsutil/glib-compat.h>
70 #include <wsutil/strtoi.h>
76 struct sharkd_filter_item
81 static GHashTable *filter_table = NULL;
84 json_unescape_str(char *input)
86 return wsjsmn_unescape_json_string(input, input);
90 json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
94 for (i = 0; i < count; i += 2)
96 const char *tok_attr = &buf[tokens[i + 0].start];
97 const char *tok_value = &buf[tokens[i + 1].start];
99 if (!strcmp(tok_attr, attr))
107 json_puts_string(const char *str)
115 for (i = 0; str[i]; i++)
140 json_print_base64_step(const guint8 *data, int *state1, int *state2)
142 gchar buf[(1 / 3 + 1) * 4 + 4 + 1];
146 wrote = g_base64_encode_step(data, 1, FALSE, buf, state1, state2);
148 wrote = g_base64_encode_close(FALSE, buf, state1, state2);
158 json_print_base64(const guint8 *data, size_t len)
161 int base64_state1 = 0;
162 int base64_state2 = 0;
166 for (i = 0; i < len; i++)
167 json_print_base64_step(&data[i], &base64_state1, &base64_state2);
169 json_print_base64_step(NULL, &base64_state1, &base64_state2);
175 sharkd_session_filter_free(gpointer data)
177 struct sharkd_filter_item *l = (struct sharkd_filter_item *) data;
183 static const guint8 *
184 sharkd_session_filter_data(const char *filter)
186 struct sharkd_filter_item *l;
188 l = (struct sharkd_filter_item *) g_hash_table_lookup(filter_table, filter);
191 guint8 *filtered = NULL;
193 int ret = sharkd_filter(filter, &filtered);
198 l = (struct sharkd_filter_item *) g_malloc(sizeof(struct sharkd_filter_item));
199 l->filtered = filtered;
201 g_hash_table_insert(filter_table, g_strdup(filter), l);
207 struct sharkd_rtp_match
209 guint32 addr_src, addr_dst;
218 sharkd_rtp_match_init(struct sharkd_rtp_match *req, const char *init_str)
220 gboolean ret = FALSE;
223 arr = g_strsplit(init_str, "_", 7); /* pass larger value, so we'll catch incorrect input :) */
224 if (g_strv_length(arr) != 5)
227 /* TODO, for now only IPv4 */
228 if (!get_host_ipaddr(arr[0], &req->addr_src))
231 if (!ws_strtou16(arr[1], NULL, &req->src_port))
234 if (!get_host_ipaddr(arr[2], &req->addr_dst))
237 if (!ws_strtou16(arr[3], NULL, &req->dst_port))
240 if (!ws_hexstrtou32(arr[4], NULL, &req->ssrc))
243 set_address(&req->src_addr, AT_IPv4, 4, &req->addr_src);
244 set_address(&req->dst_addr, AT_IPv4, 4, &req->addr_dst);
253 sharkd_rtp_match_check(const struct sharkd_rtp_match *req, const packet_info *pinfo, const struct _rtp_info *rtp_info)
255 if (rtp_info->info_sync_src == req->ssrc &&
256 pinfo->srcport == req->src_port &&
257 pinfo->destport == req->dst_port &&
258 addresses_equal(&pinfo->src, &req->src_addr) &&
259 addresses_equal(&pinfo->dst, &req->dst_addr))
268 sharkd_session_process_info_nstat_cb(const void *key, void *value, void *userdata)
270 stat_tap_table_ui *new_stat_tap = (stat_tap_table_ui *) value;
271 int *pi = (int *) userdata;
273 printf("%s{", (*pi) ? "," : "");
274 printf("\"name\":\"%s\"", new_stat_tap->title);
275 printf(",\"tap\":\"nstat:%s\"", (const char *) key);
283 sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata)
285 struct register_ct *table = (struct register_ct *) value;
286 int *pi = (int *) userdata;
288 const char *label = (const char *) key;
290 if (get_conversation_packet_func(table))
292 printf("%s{", (*pi) ? "," : "");
293 printf("\"name\":\"Conversation List/%s\"", label);
294 printf(",\"tap\":\"conv:%s\"", label);
300 if (get_hostlist_packet_func(table))
302 printf("%s{", (*pi) ? "," : "");
303 printf("\"name\":\"Endpoint/%s\"", label);
304 printf(",\"tap\":\"endpt:%s\"", label);
313 sharkd_session_seq_analysis_cb(const void *key, void *value, void *userdata)
315 register_analysis_t *analysis = (register_analysis_t *) value;
316 int *pi = (int *) userdata;
318 printf("%s{", (*pi) ? "," : "");
319 printf("\"name\":\"%s\"", sequence_analysis_get_ui_name(analysis));
320 printf(",\"tap\":\"seqa:%s\"", (const char *) key);
328 sharkd_export_object_visit_cb(const void *key _U_, void *value, void *user_data)
330 register_eo_t *eo = (register_eo_t *) value;
331 int *pi = (int *) user_data;
333 const int proto_id = get_eo_proto_id(eo);
334 const char *filter = proto_get_protocol_filter_name(proto_id);
335 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
337 printf("%s{", (*pi) ? "," : "");
338 printf("\"name\":\"Export Object/%s\"", label);
339 printf(",\"tap\":\"eo:%s\"", filter);
347 sharkd_srt_visit_cb(const void *key _U_, void *value, void *user_data)
349 register_srt_t *srt = (register_srt_t *) value;
350 int *pi = (int *) user_data;
352 const int proto_id = get_srt_proto_id(srt);
353 const char *filter = proto_get_protocol_filter_name(proto_id);
354 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
356 printf("%s{", (*pi) ? "," : "");
357 printf("\"name\":\"Service Response Time/%s\"", label);
358 printf(",\"tap\":\"srt:%s\"", filter);
366 sharkd_rtd_visit_cb(const void *key _U_, void *value, void *user_data)
368 register_rtd_t *rtd = (register_rtd_t *) value;
369 int *pi = (int *) user_data;
371 const int proto_id = get_rtd_proto_id(rtd);
372 const char *filter = proto_get_protocol_filter_name(proto_id);
373 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
375 printf("%s{", (*pi) ? "," : "");
376 printf("\"name\":\"Response Time Delay/%s\"", label);
377 printf(",\"tap\":\"rtd:%s\"", filter);
385 sharkd_follower_visit_cb(const void *key _U_, void *value, void *user_data)
387 register_follow_t *follower = (register_follow_t *) value;
388 int *pi = (int *) user_data;
390 const int proto_id = get_follow_proto_id(follower);
391 const char *label = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
392 const char *filter = label; /* correct: get_follow_by_name() is registered by short name */
394 printf("%s{", (*pi) ? "," : "");
395 printf("\"name\":\"Follow/%s\"", label);
396 printf(",\"tap\":\"follow:%s\"", filter);
404 * sharkd_session_process_info()
406 * Process info request
408 * Output object with attributes:
409 * (m) columns - available column formats, array of object with attributes:
410 * 'name' - column name
411 * 'format' - column format-name
413 * (m) stats - available statistics, array of object with attributes:
414 * 'name' - statistic name
415 * 'tap' - sharkd tap-name for statistic
417 * (m) convs - available conversation list, array of object with attributes:
418 * 'name' - conversation name
419 * 'tap' - sharkd tap-name for conversation
421 * (m) eo - available export object list, array of object with attributes:
422 * 'name' - export object name
423 * 'tap' - sharkd tap-name for eo
425 * (m) srt - available service response time list, array of object with attributes:
426 * 'name' - service response time name
427 * 'tap' - sharkd tap-name for srt
429 * (m) rtd - available response time delay list, array of object with attributes:
430 * 'name' - response time delay name
431 * 'tap' - sharkd tap-name for rtd
433 * (m) seqa - available sequence analysis (flow) list, array of object with attributes:
434 * 'name' - sequence analysis name
435 * 'tap' - sharkd tap-name
437 * (m) taps - available taps, array of object with attributes:
439 * 'tap' - sharkd tap-name
441 * (m) follow - available followers, array of object with attributes:
443 * 'tap' - sharkd tap-name
445 * (m) ftypes - conversation table for FT_ number to string
448 sharkd_session_process_info(void)
452 printf("{\"columns\":[");
453 for (i = 0; i < NUM_COL_FMTS; i++)
455 const char *col_format = col_format_to_string(i);
456 const char *col_descr = col_format_desc(i);
458 printf("%s{", (i) ? "," : "");
459 printf("\"name\":\"%s\"", col_descr);
460 printf(",\"format\":\"%s\"", col_format);
465 printf(",\"stats\":[");
467 GList *cfg_list = stats_tree_get_cfg_list();
469 const char *sepa = "";
471 for (l = cfg_list; l; l = l->next)
473 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
476 printf("\"name\":\"%s\"", cfg->name);
477 printf(",\"tap\":\"stat:%s\"", cfg->abbr);
482 g_list_free(cfg_list);
486 printf(",\"ftypes\":[");
487 for (i = 0; i < FT_NUM_TYPES; i++)
491 json_puts_string(ftype_name((ftenum_t) i));
495 printf(",\"version\":");
496 json_puts_string(sharkd_version());
498 printf(",\"nstat\":[");
500 new_stat_tap_iterate_tables(sharkd_session_process_info_nstat_cb, &i);
503 printf(",\"convs\":[");
505 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
508 printf(",\"seqa\":[");
510 sequence_analysis_table_iterate_tables(sharkd_session_seq_analysis_cb, &i);
513 printf(",\"taps\":[");
515 printf("{\"name\":\"%s\",\"tap\":\"%s\"}", "RTP streams", "rtp-streams");
516 printf(",{\"name\":\"%s\",\"tap\":\"%s\"}", "Expert Information", "expert");
522 eo_iterate_tables(sharkd_export_object_visit_cb, &i);
525 printf(",\"srt\":[");
527 srt_table_iterate_tables(sharkd_srt_visit_cb, &i);
530 printf(",\"rtd\":[");
532 rtd_table_iterate_tables(sharkd_rtd_visit_cb, &i);
535 printf(",\"follow\":[");
537 follow_iterate_followers(sharkd_follower_visit_cb, &i);
544 * sharkd_session_process_load()
546 * Process load request
549 * (m) file - file to be loaded
551 * Output object with attributes:
552 * (m) err - error code
555 sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
557 const char *tok_file = json_find_attr(buf, tokens, count, "file");
560 fprintf(stderr, "load: filename=%s\n", tok_file);
565 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK)
567 printf("{\"err\":%d}\n", err);
573 err = sharkd_load_cap_file();
575 CATCH(OutOfMemoryError)
577 fprintf(stderr, "load: OutOfMemoryError\n");
582 printf("{\"err\":%d}\n", err);
586 * sharkd_session_process_status()
588 * Process status request
590 * Output object with attributes:
591 * (m) frames - count of currently loaded frames
592 * (m) duration - time difference between time of first frame, and last loaded frame
593 * (o) filename - capture filename
594 * (o) filesize - capture filesize
597 sharkd_session_process_status(void)
599 printf("{\"frames\":%u", cfile.count);
601 printf(",\"duration\":%.9f", nstime_to_sec(&cfile.elapsed_time));
605 char *name = g_path_get_basename(cfile.filename);
607 printf(",\"filename\":");
608 json_puts_string(name);
612 if (cfile.provider.wth)
614 gint64 file_size = wtap_file_size(cfile.provider.wth, NULL);
617 printf(",\"filesize\":%" G_GINT64_FORMAT, file_size);
623 struct sharkd_analyse_data
625 GHashTable *protocols_set;
626 nstime_t *first_time;
631 sharkd_session_process_analyse_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
633 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
634 packet_info *pi = &edt->pi;
635 frame_data *fdata = pi->fd;
641 if (analyser->first_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
642 analyser->first_time = &fdata->abs_ts;
644 if (analyser->last_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
645 analyser->last_time = &fdata->abs_ts;
649 wmem_list_frame_t *frame;
651 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
653 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame));
655 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id), NULL, NULL))
657 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id), GUINT_TO_POINTER(proto_id));
659 if (g_hash_table_size(analyser->protocols_set) != 1)
661 json_puts_string(proto_get_protocol_filter_name(proto_id));
669 * sharkd_session_process_status()
671 * Process analyse request
673 * Output object with attributes:
674 * (m) frames - count of currently loaded frames
675 * (m) protocols - protocol list
676 * (m) first - earliest frame time
677 * (m) last - latest frame time
680 sharkd_session_process_analyse(void)
682 unsigned int framenum;
683 struct sharkd_analyse_data analyser;
685 analyser.first_time = NULL;
686 analyser.last_time = NULL;
687 analyser.protocols_set = g_hash_table_new(NULL /* g_direct_hash() */, NULL /* g_direct_equal */);
689 printf("{\"frames\":%u", cfile.count);
691 printf(",\"protocols\":[");
692 for (framenum = 1; framenum <= cfile.count; framenum++)
693 sharkd_dissect_request(framenum, (framenum != 1) ? 1 : 0, framenum - 1, &sharkd_session_process_analyse_cb, 0, 0, 0, &analyser);
696 if (analyser.first_time)
697 printf(",\"first\":%.9f", nstime_to_sec(analyser.first_time));
699 if (analyser.last_time)
700 printf(",\"last\":%.9f", nstime_to_sec(analyser.last_time));
704 g_hash_table_destroy(analyser.protocols_set);
708 sharkd_session_create_columns(column_info *cinfo, const char *buf, const jsmntok_t *tokens, int count)
710 const char *columns_custom[32];
711 guint16 columns_fmt[32];
712 gint16 columns_occur[32];
716 for (i = 0; i < 32; i++)
718 const char *tok_column;
719 char tok_column_name[64];
722 ws_snprintf(tok_column_name, sizeof(tok_column_name), "column%d", i);
723 tok_column = json_find_attr(buf, tokens, count, tok_column_name);
724 if (tok_column == NULL)
727 columns_custom[i] = NULL;
728 columns_occur[i] = 0;
730 if ((custom_sepa = strchr(tok_column, ':')))
732 *custom_sepa = '\0'; /* XXX, C abuse: discarding-const */
734 columns_fmt[i] = COL_CUSTOM;
735 columns_custom[i] = tok_column;
737 if (!ws_strtoi16(custom_sepa + 1, NULL, &columns_occur[i]))
742 if (!ws_strtou16(tok_column, NULL, &columns_fmt[i]))
745 if (columns_fmt[i] >= NUM_COL_FMTS)
748 /* if custom, that it shouldn't be just custom number -> error */
749 if (columns_fmt[i] == COL_CUSTOM)
756 col_setup(cinfo, cols);
758 for (i = 0; i < cols; i++)
760 col_item_t *col_item = &cinfo->columns[i];
762 col_item->col_fmt = columns_fmt[i];
763 col_item->col_title = NULL; /* no need for title */
765 if (col_item->col_fmt == COL_CUSTOM)
767 col_item->col_custom_fields = g_strdup(columns_custom[i]);
768 col_item->col_custom_occurrence = columns_occur[i];
771 col_item->col_fence = 0;
780 * sharkd_session_process_frames()
782 * Process frames request
785 * (o) column0...columnXX - requested columns either number in range [0..NUM_COL_FMTS), or custom (syntax <dfilter>:<occurence>).
786 * If column0 is not specified default column set will be used.
787 * (o) filter - filter to be used
788 * (o) skip=N - skip N frames
789 * (o) limit=N - show only N frames
790 * (o) refs - list (comma separated) with sorted time reference frame numbers.
792 * Output array of frames with attributes:
793 * (m) c - array of column data
794 * (m) num - frame number
795 * (o) i - if frame is ignored
796 * (o) m - if frame is marked
797 * (o) ct - if frame is commented
798 * (o) bg - color filter - background color in hex
799 * (o) fg - color filter - foreground color in hex
802 sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
804 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
805 const char *tok_column = json_find_attr(buf, tokens, count, "column0");
806 const char *tok_skip = json_find_attr(buf, tokens, count, "skip");
807 const char *tok_limit = json_find_attr(buf, tokens, count, "limit");
808 const char *tok_refs = json_find_attr(buf, tokens, count, "refs");
810 const guint8 *filter_data = NULL;
812 const char *frame_sepa = "";
815 guint32 framenum, prev_dis_num = 0;
816 guint32 current_ref_frame = 0, next_ref_frame = G_MAXUINT32;
820 column_info *cinfo = &cfile.cinfo;
821 column_info user_cinfo;
825 memset(&user_cinfo, 0, sizeof(user_cinfo));
826 cinfo = sharkd_session_create_columns(&user_cinfo, buf, tokens, count);
833 filter_data = sharkd_session_filter_data(tok_filter);
841 if (!ws_strtou32(tok_skip, NULL, &skip))
848 if (!ws_strtou32(tok_limit, NULL, &limit))
854 if (!ws_strtou32(tok_refs, &tok_refs, &next_ref_frame))
859 for (framenum = 1; framenum <= cfile.count; framenum++)
862 guint32 ref_frame = (framenum != 1) ? 1 : 0;
864 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
870 prev_dis_num = framenum;
876 if (framenum >= next_ref_frame)
878 current_ref_frame = next_ref_frame;
880 if (*tok_refs != ',')
881 next_ref_frame = G_MAXUINT32;
883 while (*tok_refs == ',' && framenum >= next_ref_frame)
885 current_ref_frame = next_ref_frame;
887 if (!ws_strtou32(tok_refs + 1, &tok_refs, &next_ref_frame))
889 fprintf(stderr, "sharkd_session_process_frames() wrong format for refs: %s\n", tok_refs);
895 if (current_ref_frame)
896 ref_frame = current_ref_frame;
899 fdata = sharkd_get_frame(framenum);
900 sharkd_dissect_columns(fdata, ref_frame, prev_dis_num, cinfo, (fdata->color_filter == NULL));
902 printf("%s{\"c\":[", frame_sepa);
903 for (col = 0; col < cinfo->num_cols; ++col)
905 const col_item_t *col_item = &cinfo->columns[col];
910 json_puts_string(col_item->col_data);
912 printf("],\"num\":%u", framenum);
914 if (fdata->flags.has_user_comment || fdata->flags.has_phdr_comment)
916 if (!fdata->flags.has_user_comment || sharkd_get_user_comment(fdata) != NULL)
917 printf(",\"ct\":true");
920 if (fdata->flags.ignored)
921 printf(",\"i\":true");
923 if (fdata->flags.marked)
924 printf(",\"m\":true");
926 if (fdata->color_filter)
928 printf(",\"bg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->bg_color));
929 printf(",\"fg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->fg_color));
934 prev_dis_num = framenum;
936 if (limit && --limit == 0)
941 if (cinfo != &cfile.cinfo)
946 sharkd_session_process_tap_stats_node_cb(const stat_node *n)
949 const char *sepa = "";
952 for (node = n->children; node; node = node->next)
954 /* code based on stats_tree_get_values_from_node() */
955 printf("%s{\"name\":\"%s\"", sepa, node->name);
956 printf(",\"count\":%d", node->counter);
957 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE) || node->rng))
959 printf(",\"avg\":%.2f", ((float)node->total) / node->counter);
960 printf(",\"min\":%d", node->minvalue);
961 printf(",\"max\":%d", node->maxvalue);
964 if (node->st->elapsed)
965 printf(",\"rate\":%.4f",((float)node->counter) / node->st->elapsed);
967 if (node->parent && node->parent->counter)
968 printf(",\"perc\":%.2f", (node->counter * 100.0) / node->parent->counter);
969 else if (node->parent == &(node->st->root))
970 printf(",\"perc\":100");
972 if (prefs.st_enable_burstinfo && node->max_burst)
974 if (prefs.st_burst_showcount)
975 printf(",\"burstcount\":%d", node->max_burst);
977 printf(",\"burstrate\":%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
979 printf(",\"bursttime\":%.3f", ((double)node->burst_time / 1000.0));
985 sharkd_session_process_tap_stats_node_cb(node);
994 * sharkd_session_process_tap_stats_cb()
999 * (m) type:stats - tap output type
1000 * (m) name - stat name
1001 * (m) stats - array of object with attributes:
1002 * (m) name - stat item name
1003 * (m) count - stat item counter
1004 * (o) avg - stat item averange value
1005 * (o) min - stat item min value
1006 * (o) max - stat item max value
1007 * (o) rate - stat item rate value (ms)
1008 * (o) perc - stat item percentage
1009 * (o) burstrate - stat item burst rate
1010 * (o) burstcount - stat item burst count
1011 * (o) burstttme - stat item burst start
1012 * (o) sub - array of object with attributes like in stats node.
1015 sharkd_session_process_tap_stats_cb(void *psp)
1017 stats_tree *st = (stats_tree *) psp;
1019 printf("{\"tap\":\"stats:%s\",\"type\":\"stats\"", st->cfg->abbr);
1021 printf(",\"name\":\"%s\",\"stats\":", st->cfg->name);
1022 sharkd_session_process_tap_stats_node_cb(&st->root);
1027 sharkd_session_free_tap_stats_cb(void *psp)
1029 stats_tree *st = (stats_tree *) psp;
1031 stats_tree_free(st);
1034 struct sharkd_expert_tap
1041 * sharkd_session_process_tap_expert_cb()
1043 * Output expert tap:
1045 * (m) tap - tap name
1046 * (m) type:expert - tap output type
1047 * (m) details - array of object with attributes:
1048 * (m) f - frame number, which generated expert information
1051 * (m) m - expert message
1055 sharkd_session_process_tap_expert_cb(void *tapdata)
1057 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1059 const char *sepa = "";
1061 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "expert", "expert");
1063 printf(",\"details\":[");
1064 for (list = etd->details; list; list = list->next)
1066 expert_info_t *ei = (expert_info_t *) list->data;
1069 printf("%s{", sepa);
1071 printf("\"f\":%u,", ei->packet_num);
1073 tmp = try_val_to_str(ei->severity, expert_severity_vals);
1075 printf("\"s\":\"%s\",", tmp);
1077 tmp = try_val_to_str(ei->group, expert_group_vals);
1079 printf("\"g\":\"%s\",", tmp);
1082 json_puts_string(ei->summary);
1088 json_puts_string(ei->protocol);
1100 sharkd_session_packet_tap_expert_cb(void *tapdata, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *pointer)
1102 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1103 const expert_info_t *ei = (const expert_info_t *) pointer;
1104 expert_info_t *ei_copy;
1106 /* Note: this is a shallow copy */
1107 ei_copy = (expert_info_t *) g_memdup(ei, sizeof(*ei));
1108 if (ei_copy == NULL)
1111 ei_copy->protocol = g_string_chunk_insert_const(etd->text, ei_copy->protocol);
1112 ei_copy->summary = g_string_chunk_insert_const(etd->text, ei_copy->summary);
1114 etd->details = g_slist_prepend(etd->details, ei_copy);
1120 sharkd_session_free_tap_expert_cb(void *tapdata)
1122 struct sharkd_expert_tap *etd = (struct sharkd_expert_tap *) tapdata;
1124 g_slist_free_full(etd->details, g_free);
1125 g_string_chunk_free(etd->text);
1130 * sharkd_session_process_tap_flow_cb()
1133 * (m) tap - tap name
1134 * (m) type:flow - tap output type
1135 * (m) nodes - array of strings with node address
1136 * (m) flows - array of object with attributes:
1137 * (m) t - frame time string
1138 * (m) n - array of two numbers with source node index and destination node index
1139 * (m) pn - array of two numbers with source and destination port
1143 sharkd_session_process_tap_flow_cb(void *tapdata)
1145 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1149 const char *sepa = "";
1151 sequence_analysis_get_nodes(graph_analysis);
1153 printf("{\"tap\":\"seqa:%s\",\"type\":\"%s\"", graph_analysis->name, "flow");
1155 printf(",\"nodes\":[");
1156 for (i = 0; i < graph_analysis->num_nodes; i++)
1163 addr_str = address_to_display(NULL, &(graph_analysis->nodes[i]));
1164 json_puts_string(addr_str);
1165 wmem_free(NULL, addr_str);
1169 printf(",\"flows\":[");
1171 flow_list = g_queue_peek_nth_link(graph_analysis->items, 0);
1174 seq_analysis_item_t *sai = (seq_analysis_item_t *) flow_list->data;
1176 flow_list = g_list_next(flow_list);
1181 printf("%s{", sepa);
1183 printf("\"t\":\"%s\"", sai->time_str);
1184 printf(",\"n\":[%u,%u]", sai->src_node, sai->dst_node);
1185 printf(",\"pn\":[%u,%u]", sai->port_src, sai->port_dst);
1190 json_puts_string(sai->comment);
1203 sharkd_session_free_tap_flow_cb(void *tapdata)
1205 seq_analysis_info_t *graph_analysis = (seq_analysis_info_t *) tapdata;
1207 sequence_analysis_info_free(graph_analysis);
1210 struct sharkd_conv_tap_data
1214 gboolean resolve_name;
1215 gboolean resolve_port;
1219 sharkd_session_geoip_addr(address *addr, const char *suffix)
1227 if (addr->type == AT_IPv4)
1229 guint32 ip = pntoh32(addr->data);
1231 guint num_dbs = geoip_db_num_dbs();
1234 for (dbnum = 0; dbnum < num_dbs; dbnum++)
1236 const char *geoip_key = NULL;
1239 int db_type = geoip_db_type(dbnum);
1243 case GEOIP_COUNTRY_EDITION:
1244 geoip_key = "geoip_country";
1247 case GEOIP_CITY_EDITION_REV0:
1248 case GEOIP_CITY_EDITION_REV1:
1249 geoip_key = "geoip_city";
1252 case GEOIP_ORG_EDITION:
1253 geoip_key = "geoip_org";
1256 case GEOIP_ISP_EDITION:
1257 geoip_key = "geoip_isp";
1260 case GEOIP_ASNUM_EDITION:
1261 geoip_key = "geoip_as";
1264 case WS_LAT_FAKE_EDITION:
1265 geoip_key = "geoip_lat";
1268 case WS_LON_FAKE_EDITION:
1269 geoip_key = "geoip_lon";
1273 if (geoip_key && (geoip_val = geoip_db_lookup_ipv4(dbnum, ip, NULL)))
1275 printf(",\"%s%s\":", geoip_key, suffix);
1276 json_puts_string(geoip_val);
1281 #ifdef HAVE_GEOIP_V6
1282 if (addr->type == AT_IPv6)
1284 const ws_in6_addr *ip6 = (const ws_in6_addr *) addr->data;
1286 guint num_dbs = geoip_db_num_dbs();
1289 for (dbnum = 0; dbnum < num_dbs; dbnum++)
1291 const char *geoip_key = NULL;
1294 int db_type = geoip_db_type(dbnum);
1298 case GEOIP_COUNTRY_EDITION_V6:
1299 geoip_key = "geoip_country";
1301 #if NUM_DB_TYPES > 31
1302 case GEOIP_CITY_EDITION_REV0_V6:
1303 case GEOIP_CITY_EDITION_REV1_V6:
1304 geoip_key = "geoip_city";
1307 case GEOIP_ORG_EDITION_V6:
1308 geoip_key = "geoip_org";
1311 case GEOIP_ISP_EDITION_V6:
1312 geoip_key = "geoip_isp";
1315 case GEOIP_ASNUM_EDITION_V6:
1316 geoip_key = "geoip_as";
1318 #endif /* DB_NUM_TYPES */
1319 case WS_LAT_FAKE_EDITION:
1320 geoip_key = "geoip_lat";
1323 case WS_LON_FAKE_EDITION:
1324 geoip_key = "geoip_lon";
1328 if (geoip_key && (geoip_val = geoip_db_lookup_ipv6(dbnum, *ip6, NULL)))
1330 printf(",\"%s%s\":", geoip_key, suffix);
1331 json_puts_string(geoip_val);
1336 #endif /* HAVE_GEOIP_V6 */
1337 #endif /* HAVE_GEOIP */
1342 struct sharkd_analyse_rtp_items
1345 guint32 sequence_num;
1353 double arrive_offset;
1355 /* from tap_rtp_stat_t */
1360 struct sharkd_analyse_rtp
1362 const char *tap_name;
1363 struct sharkd_rtp_match rtp;
1367 tap_rtp_stat_t statinfo;
1371 sharkd_session_process_tap_rtp_free_cb(void *tapdata)
1373 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1375 g_slist_free_full(rtp_req->packets, g_free);
1380 sharkd_session_packet_tap_rtp_analyse_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pointer)
1382 struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1383 const struct _rtp_info *rtpinfo = (const struct _rtp_info *) pointer;
1385 if (sharkd_rtp_match_check(&rtp_req->rtp, pinfo, rtpinfo))
1387 tap_rtp_stat_t *statinfo = &(rtp_req->statinfo);
1388 struct sharkd_analyse_rtp_items *item;
1390 rtp_packet_analyse(statinfo, pinfo, rtpinfo);
1392 item = (struct sharkd_analyse_rtp_items *) g_malloc(sizeof(struct sharkd_analyse_rtp_items));
1394 if (!rtp_req->packets)
1395 rtp_req->start_time = nstime_to_sec(&pinfo->abs_ts);
1397 item->frame_num = pinfo->num;
1398 item->sequence_num = rtpinfo->info_seq_num;
1399 item->delta = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->delta;
1400 item->jitter = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->jitter;
1401 item->skew = (statinfo->flags & STAT_FLAG_FIRST) ? 0.0 : statinfo->skew;
1402 item->bandwidth = statinfo->bandwidth;
1403 item->marker = rtpinfo->info_marker_set ? TRUE : FALSE;
1404 item->arrive_offset= nstime_to_sec(&pinfo->abs_ts) - rtp_req->start_time;
1406 item->flags = statinfo->flags;
1407 item->pt = statinfo->pt;
1409 /* XXX, O(n) optimize */
1410 rtp_req->packets = g_slist_append(rtp_req->packets, item);
1417 * sharkd_session_process_tap_rtp_analyse_cb()
1419 * Output rtp analyse tap:
1420 * (m) tap - tap name
1421 * (m) type - tap output type
1422 * (m) ssrc - RTP SSRC
1423 * (m) max_delta - Max delta (ms)
1424 * (m) max_delta_nr - Max delta packet #
1425 * (m) max_jitter - Max jitter (ms)
1426 * (m) mean_jitter - Mean jitter (ms)
1427 * (m) max_skew - Max skew (ms)
1428 * (m) total_nr - Total number of RTP packets
1429 * (m) seq_err - Number of sequence errors
1430 * (m) duration - Duration (ms)
1431 * (m) items - array of object with attributes:
1432 * (m) f - frame number
1433 * (m) o - arrive offset
1434 * (m) sn - sequence number
1438 * (m) bw - bandwidth
1439 * (o) s - status string
1440 * (o) t - status type
1441 * (o) mark - rtp mark
1444 sharkd_session_process_tap_rtp_analyse_cb(void *tapdata)
1446 const int RTP_TYPE_CN = 1;
1447 const int RTP_TYPE_ERROR = 2;
1448 const int RTP_TYPE_WARN = 3;
1449 const int RTP_TYPE_PT_EVENT = 4;
1451 const struct sharkd_analyse_rtp *rtp_req = (struct sharkd_analyse_rtp *) tapdata;
1452 const tap_rtp_stat_t *statinfo = &rtp_req->statinfo;
1454 const char *sepa = "";
1457 printf("{\"tap\":\"%s\",\"type\":\"rtp-analyse\"", rtp_req->tap_name);
1459 printf(",\"ssrc\":%u", rtp_req->rtp.ssrc);
1461 printf(",\"max_delta\":%f", statinfo->max_delta);
1462 printf(",\"max_delta_nr\":%u", statinfo->max_nr);
1463 printf(",\"max_jitter\":%f", statinfo->max_jitter);
1464 printf(",\"mean_jitter\":%f", statinfo->mean_jitter);
1465 printf(",\"max_skew\":%f", statinfo->max_skew);
1466 printf(",\"total_nr\":%u", statinfo->total_nr);
1467 printf(",\"seq_err\":%u", statinfo->sequence);
1468 printf(",\"duration\":%f", statinfo->time - statinfo->start_time);
1470 printf(",\"items\":[");
1471 for (l = rtp_req->packets; l; l = l->next)
1473 struct sharkd_analyse_rtp_items *item = (struct sharkd_analyse_rtp_items *) l->data;
1475 printf("%s{", sepa);
1477 printf("\"f\":%u", item->frame_num);
1478 printf(",\"o\":%.9f", item->arrive_offset);
1479 printf(",\"sn\":%u", item->sequence_num);
1480 printf(",\"d\":%.2f", item->delta);
1481 printf(",\"j\":%.2f", item->jitter);
1482 printf(",\"sk\":%.2f", item->skew);
1483 printf(",\"bw\":%.2f", item->bandwidth);
1485 if (item->pt == PT_CN)
1486 printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=13, RFC 3389)", RTP_TYPE_CN);
1487 else if (item->pt == PT_CN_OLD)
1488 printf(",\"s\":\"%s\",\"t\":%d", "Comfort noise (PT=19, reserved)", RTP_TYPE_CN);
1489 else if (item->flags & STAT_FLAG_WRONG_SEQ)
1490 printf(",\"s\":\"%s\",\"t\":%d", "Wrong sequence number", RTP_TYPE_ERROR);
1491 else if (item->flags & STAT_FLAG_DUP_PKT)
1492 printf(",\"s\":\"%s\",\"t\":%d", "Suspected duplicate (MAC address) only delta time calculated", RTP_TYPE_WARN);
1493 else if (item->flags & STAT_FLAG_REG_PT_CHANGE)
1494 printf(",\"s\":\"Payload changed to PT=%u%s\",\"t\":%d",
1496 (item->flags & STAT_FLAG_PT_T_EVENT) ? " telephone/event" : "",
1498 else if (item->flags & STAT_FLAG_WRONG_TIMESTAMP)
1499 printf(",\"s\":\"%s\",\"t\":%d", "Incorrect timestamp", RTP_TYPE_WARN);
1500 else if ((item->flags & STAT_FLAG_PT_CHANGE)
1501 && !(item->flags & STAT_FLAG_FIRST)
1502 && !(item->flags & STAT_FLAG_PT_CN)
1503 && (item->flags & STAT_FLAG_FOLLOW_PT_CN)
1504 && !(item->flags & STAT_FLAG_MARKER))
1506 printf(",\"s\":\"%s\",\"t\":%d", "Marker missing?", RTP_TYPE_WARN);
1508 else if (item->flags & STAT_FLAG_PT_T_EVENT)
1509 printf(",\"s\":\"PT=%u telephone/event\",\"t\":%d", item->pt, RTP_TYPE_PT_EVENT);
1510 else if (item->flags & STAT_FLAG_MARKER)
1511 printf(",\"t\":%d", RTP_TYPE_WARN);
1514 printf(",\"mark\":1");
1525 * sharkd_session_process_tap_conv_cb()
1528 * (m) tap - tap name
1529 * (m) type - tap output type
1530 * (m) proto - protocol short name
1531 * (o) filter - filter string
1533 * (o) convs - array of object with attributes:
1534 * (m) saddr - source address
1535 * (m) daddr - destination address
1536 * (o) sport - source port
1537 * (o) dport - destination port
1538 * (m) txf - TX frame count
1539 * (m) txb - TX bytes
1540 * (m) rxf - RX frame count
1541 * (m) rxb - RX bytes
1542 * (m) start - (relative) first packet time
1543 * (m) stop - (relative) last packet time
1545 * (o) hosts - array of object with attributes:
1546 * (m) host - host address
1547 * (o) port - host port
1548 * (m) txf - TX frame count
1549 * (m) txb - TX bytes
1550 * (m) rxf - RX frame count
1551 * (m) rxb - RX bytes
1554 sharkd_session_process_tap_conv_cb(void *arg)
1556 conv_hash_t *hash = (conv_hash_t *) arg;
1557 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
1559 int proto_with_port;
1564 if (!strncmp(iu->type, "conv:", 5))
1566 printf("{\"tap\":\"%s\",\"type\":\"conv\"", iu->type);
1567 printf(",\"convs\":[");
1568 proto = iu->type + 5;
1570 else if (!strncmp(iu->type, "endpt:", 6))
1572 printf("{\"tap\":\"%s\",\"type\":\"host\"", iu->type);
1573 printf(",\"hosts\":[");
1574 proto = iu->type + 6;
1578 printf("{\"tap\":\"%s\",\"type\":\"err\"", iu->type);
1582 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
1584 if (iu->hash.conv_array != NULL && !strncmp(iu->type, "conv:", 5))
1586 for (i = 0; i < iu->hash.conv_array->len; i++)
1588 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
1589 char *src_addr, *dst_addr;
1590 char *src_port, *dst_port;
1593 printf("%s{", i ? "," : "");
1595 printf("\"saddr\":\"%s\"", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
1596 printf(",\"daddr\":\"%s\"", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
1598 if (proto_with_port)
1600 printf(",\"sport\":\"%s\"", (src_port = get_conversation_port(NULL, iui->src_port, iui->etype, iu->resolve_port)));
1601 printf(",\"dport\":\"%s\"", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->etype, iu->resolve_port)));
1603 wmem_free(NULL, src_port);
1604 wmem_free(NULL, dst_port);
1607 printf(",\"rxf\":%" G_GUINT64_FORMAT, iui->rx_frames);
1608 printf(",\"rxb\":%" G_GUINT64_FORMAT, iui->rx_bytes);
1610 printf(",\"txf\":%" G_GUINT64_FORMAT, iui->tx_frames);
1611 printf(",\"txb\":%" G_GUINT64_FORMAT, iui->tx_bytes);
1613 printf(",\"start\":%.9f", nstime_to_sec(&iui->start_time));
1614 printf(",\"stop\":%.9f", nstime_to_sec(&iui->stop_time));
1616 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
1619 printf(",\"filter\":\"%s\"", filter_str);
1623 wmem_free(NULL, src_addr);
1624 wmem_free(NULL, dst_addr);
1626 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
1628 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
1634 else if (iu->hash.conv_array != NULL && !strncmp(iu->type, "endpt:", 6))
1636 for (i = 0; i < iu->hash.conv_array->len; i++)
1638 hostlist_talker_t *host = &g_array_index(iu->hash.conv_array, hostlist_talker_t, i);
1639 char *host_str, *port_str;
1642 printf("%s{", i ? "," : "");
1644 printf("\"host\":\"%s\"", (host_str = get_conversation_address(NULL, &host->myaddress, iu->resolve_name)));
1646 if (proto_with_port)
1648 printf(",\"port\":\"%s\"", (port_str = get_conversation_port(NULL, host->port, host->etype, iu->resolve_port)));
1650 wmem_free(NULL, port_str);
1653 printf(",\"rxf\":%" G_GUINT64_FORMAT, host->rx_frames);
1654 printf(",\"rxb\":%" G_GUINT64_FORMAT, host->rx_bytes);
1656 printf(",\"txf\":%" G_GUINT64_FORMAT, host->tx_frames);
1657 printf(",\"txb\":%" G_GUINT64_FORMAT, host->tx_bytes);
1659 filter_str = get_hostlist_filter(host);
1662 printf(",\"filter\":\"%s\"", filter_str);
1666 wmem_free(NULL, host_str);
1668 if (sharkd_session_geoip_addr(&(host->myaddress), ""))
1674 printf("],\"proto\":\"%s\",\"geoip\":%s},", proto, with_geoip ? "true" : "false");
1678 sharkd_session_free_tap_conv_cb(void *arg)
1680 conv_hash_t *hash = (conv_hash_t *) arg;
1681 struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
1683 if (!strncmp(iu->type, "conv:", 5))
1685 reset_conversation_table_data(hash);
1687 else if (!strncmp(iu->type, "endpt:", 6))
1689 reset_hostlist_table_data(hash);
1696 * sharkd_session_process_tap_nstat_cb()
1699 * (m) tap - tap name
1700 * (m) type - tap output type
1701 * (m) fields: array of objects with attributes:
1704 * (m) tables: array of object with attributes:
1705 * (m) t - table title
1706 * (m) i - array of items
1709 sharkd_session_process_tap_nstat_cb(void *arg)
1711 new_stat_data_t *stat_data = (new_stat_data_t *) arg;
1714 printf("{\"tap\":\"nstat:%s\",\"type\":\"nstat\"", stat_data->stat_tap_data->cli_string);
1716 printf(",\"fields\":[");
1717 for (i = 0; i < stat_data->stat_tap_data->nfields; i++)
1719 stat_tap_table_item *field = &(stat_data->stat_tap_data->fields[i]);
1727 json_puts_string(field->column_name);
1733 printf(",\"tables\":[");
1734 for (i = 0; i < stat_data->stat_tap_data->tables->len; i++)
1736 stat_tap_table *table = g_array_index(stat_data->stat_tap_data->tables, stat_tap_table *, i);
1737 const char *sepa = "";
1745 printf("\"%s\"", table->title);
1748 for (j = 0; j < table->num_elements; j++)
1750 stat_tap_table_item_type *field_data;
1752 field_data = new_stat_tap_get_field_data(table, j, 0);
1753 if (field_data == NULL || field_data->type == TABLE_ITEM_NONE) /* Nothing for us here */
1756 printf("%s[", sepa);
1757 for (k = 0; k < table->num_fields; k++)
1759 field_data = new_stat_tap_get_field_data(table, j, k);
1764 switch (field_data->type)
1766 case TABLE_ITEM_UINT:
1767 printf("%u", field_data->value.uint_value);
1770 case TABLE_ITEM_INT:
1771 printf("%d", field_data->value.uint_value);
1774 case TABLE_ITEM_STRING:
1775 json_puts_string(field_data->value.string_value);
1778 case TABLE_ITEM_FLOAT:
1779 printf("%f", field_data->value.float_value);
1782 case TABLE_ITEM_ENUM:
1783 printf("%d", field_data->value.enum_value);
1786 case TABLE_ITEM_NONE:
1803 sharkd_session_free_tap_nstat_cb(void *arg)
1805 new_stat_data_t *stat_data = (new_stat_data_t *) arg;
1807 free_stat_tables(stat_data->stat_tap_data, NULL, NULL);
1811 * sharkd_session_process_tap_rtd_cb()
1814 * (m) tap - tap name
1815 * (m) type - tap output type
1816 * (m) stats - statistics rows - array object with attributes:
1817 * (m) type - statistic name
1818 * (m) num - number of messages
1819 * (m) min - minimum SRT time
1820 * (m) max - maximum SRT time
1821 * (m) tot - total SRT time
1822 * (m) min_frame - minimal SRT
1823 * (m) max_frame - maximum SRT
1824 * (o) open_req - Open Requests
1825 * (o) disc_rsp - Discarded Responses
1826 * (o) req_dup - Duplicated Requests
1827 * (o) rsp_dup - Duplicated Responses
1828 * (o) open_req - Open Requests
1829 * (o) disc_rsp - Discarded Responses
1830 * (o) req_dup - Duplicated Requests
1831 * (o) rsp_dup - Duplicated Responses
1834 sharkd_session_process_tap_rtd_cb(void *arg)
1836 rtd_data_t *rtd_data = (rtd_data_t *) arg;
1837 register_rtd_t *rtd = (register_rtd_t *) rtd_data->user_data;
1841 const char *filter = proto_get_protocol_filter_name(get_rtd_proto_id(rtd));
1843 /* XXX, some dissectors are having single table and multiple timestats (mgcp, megaco),
1844 * some multiple table and single timestat (radius, h225)
1845 * and it seems that value_string is used one for timestamp-ID, other one for table-ID
1846 * I wonder how it will gonna work with multiple timestats and multiple tables...
1847 * (for usage grep for: register_rtd_table)
1849 const value_string *vs = get_rtd_value_string(rtd);
1850 const char *sepa = "";
1852 printf("{\"tap\":\"rtd:%s\",\"type\":\"rtd\"", filter);
1854 if (rtd_data->stat_table.num_rtds == 1)
1856 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[0];
1858 printf(",\"open_req\":%u", ms->open_req_num);
1859 printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
1860 printf(",\"req_dup\":%u", ms->req_dup_num);
1861 printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
1864 printf(",\"stats\":[");
1865 for (i = 0; i < rtd_data->stat_table.num_rtds; i++)
1867 const rtd_timestat *ms = &rtd_data->stat_table.time_stats[i];
1869 for (j = 0; j < ms->num_timestat; j++)
1871 const char *type_str;
1873 if (ms->rtd[j].num == 0)
1876 printf("%s{", sepa);
1878 if (rtd_data->stat_table.num_rtds == 1)
1879 type_str = val_to_str_const(j, vs, "Other"); /* 1 table - description per row */
1881 type_str = val_to_str_const(i, vs, "Other"); /* multiple table - description per table */
1882 printf("\"type\":");
1883 json_puts_string(type_str);
1885 printf(",\"num\":%u", ms->rtd[j].num);
1886 printf(",\"min\":%.9f", nstime_to_sec(&(ms->rtd[j].min)));
1887 printf(",\"max\":%.9f", nstime_to_sec(&(ms->rtd[j].max)));
1888 printf(",\"tot\":%.9f", nstime_to_sec(&(ms->rtd[j].tot)));
1889 printf(",\"min_frame\":%u", ms->rtd[j].min_num);
1890 printf(",\"max_frame\":%u", ms->rtd[j].max_num);
1892 if (rtd_data->stat_table.num_rtds != 1)
1894 /* like in tshark, display it on every row */
1895 printf(",\"open_req\":%u", ms->open_req_num);
1896 printf(",\"disc_rsp\":%u", ms->disc_rsp_num);
1897 printf(",\"req_dup\":%u", ms->req_dup_num);
1898 printf(",\"rsp_dup\":%u", ms->rsp_dup_num);
1909 sharkd_session_free_tap_rtd_cb(void *arg)
1911 rtd_data_t *rtd_data = (rtd_data_t *) arg;
1913 free_rtd_table(&rtd_data->stat_table, NULL, NULL);
1918 * sharkd_session_process_tap_srt_cb()
1921 * (m) tap - tap name
1922 * (m) type - tap output type
1924 * (m) tables - array of object with attributes:
1925 * (m) n - table name
1926 * (m) f - table filter
1927 * (o) c - table column name
1928 * (m) r - table rows - array object with attributes:
1930 * (m) idx - procedure index
1931 * (m) num - number of events
1932 * (m) min - minimum SRT time
1933 * (m) max - maximum SRT time
1934 * (m) tot - total SRT time
1937 sharkd_session_process_tap_srt_cb(void *arg)
1939 srt_data_t *srt_data = (srt_data_t *) arg;
1940 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
1942 const char *filter = proto_get_protocol_filter_name(get_srt_proto_id(srt));
1946 printf("{\"tap\":\"srt:%s\",\"type\":\"srt\"", filter);
1948 printf(",\"tables\":[");
1949 for (i = 0; i < srt_data->srt_array->len; i++)
1952 srt_stat_table *rst = g_array_index(srt_data->srt_array, srt_stat_table *, i);
1953 const char *sepa = "";
1963 json_puts_string(rst->name);
1964 else if (rst->short_name)
1965 json_puts_string(rst->short_name);
1967 printf("\"table%u\"", i);
1969 if (rst->filter_string)
1972 json_puts_string(rst->filter_string);
1975 if (rst->proc_column_name)
1978 json_puts_string(rst->proc_column_name);
1982 for (j = 0; j < rst->num_procs; j++)
1985 srt_procedure_t *proc = &rst->procedures[j];
1987 if (proc->stats.num == 0)
1990 printf("%s{", sepa);
1993 json_puts_string(proc->procedure);
1995 if (rst->filter_string)
1996 printf(",\"idx\":%d", proc->proc_index);
1998 printf(",\"num\":%u", proc->stats.num);
2000 printf(",\"min\":%.9f", nstime_to_sec(&proc->stats.min));
2001 printf(",\"max\":%.9f", nstime_to_sec(&proc->stats.max));
2002 printf(",\"tot\":%.9f", nstime_to_sec(&proc->stats.tot));
2014 sharkd_session_free_tap_srt_cb(void *arg)
2016 srt_data_t *srt_data = (srt_data_t *) arg;
2017 register_srt_t *srt = (register_srt_t *) srt_data->user_data;
2019 free_srt_table(srt, srt_data->srt_array, NULL, NULL);
2020 g_array_free(srt_data->srt_array, TRUE);
2024 struct sharkd_export_object_list
2026 struct sharkd_export_object_list *next;
2033 static struct sharkd_export_object_list *sharkd_eo_list;
2036 * sharkd_session_process_tap_eo_cb()
2039 * (m) tap - tap name
2040 * (m) type - tap output type
2041 * (m) proto - protocol short name
2042 * (m) objects - array of object with attributes:
2043 * (m) pkt - packet number
2044 * (o) hostname - hostname
2045 * (o) type - content type
2046 * (o) filename - filename
2047 * (m) len - object length
2050 sharkd_session_process_tap_eo_cb(void *tapdata)
2052 export_object_list_t *tap_object = (export_object_list_t *) tapdata;
2053 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) tap_object->gui_data;
2057 printf("{\"tap\":\"%s\",\"type\":\"eo\"", object_list->type);
2058 printf(",\"proto\":\"%s\"", object_list->proto);
2059 printf(",\"objects\":[");
2061 for (slist = object_list->entries; slist; slist = slist->next)
2063 const export_object_entry_t *eo_entry = (export_object_entry_t *) slist->data;
2065 printf("%s{", i ? "," : "");
2067 printf("\"pkt\":%u", eo_entry->pkt_num);
2069 if (eo_entry->hostname)
2071 printf(",\"hostname\":");
2072 json_puts_string(eo_entry->hostname);
2075 if (eo_entry->content_type)
2077 printf(",\"type\":");
2078 json_puts_string(eo_entry->content_type);
2081 if (eo_entry->filename)
2083 printf(",\"filename\":");
2084 json_puts_string(eo_entry->filename);
2087 printf(",\"_download\":\"%s_%d\"", object_list->type, i);
2089 printf(",\"len\":%" G_GINT64_FORMAT, eo_entry->payload_len);
2100 sharkd_eo_object_list_add_entry(void *gui_data, export_object_entry_t *entry)
2102 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2104 object_list->entries = g_slist_append(object_list->entries, entry);
2107 static export_object_entry_t *
2108 sharkd_eo_object_list_get_entry(void *gui_data, int row)
2110 struct sharkd_export_object_list *object_list = (struct sharkd_export_object_list *) gui_data;
2112 return (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
2116 * sharkd_session_process_tap_rtp_cb()
2118 * Output RTP streams tap:
2119 * (m) tap - tap name
2120 * (m) type - tap output type
2121 * (m) streams - array of object with attributes:
2122 * (m) ssrc - RTP synchronization source identifier
2123 * (m) payload - stream payload
2124 * (m) saddr - source address
2125 * (m) sport - source port
2126 * (m) daddr - destination address
2127 * (m) dport - destination port
2128 * (m) pkts - packets count
2129 * (m) max_delta - max delta (ms)
2130 * (m) max_jitter - max jitter (ms)
2131 * (m) mean_jitter - mean jitter (ms)
2134 * (m) problem - if analyser found the problem
2135 * (m) ipver - address IP version (4 or 6)
2138 sharkd_session_process_tap_rtp_cb(void *arg)
2140 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
2143 const char *sepa = "";
2145 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "rtp-streams", "rtp-streams");
2147 printf(",\"streams\":[");
2148 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
2150 rtp_stream_info_t *streaminfo = (rtp_stream_info_t *) listx->data;
2152 char *src_addr, *dst_addr;
2156 src_addr = address_to_display(NULL, &(streaminfo->src_addr));
2157 dst_addr = address_to_display(NULL, &(streaminfo->dest_addr));
2159 if (streaminfo->payload_type_name != NULL)
2160 payload = wmem_strdup(NULL, streaminfo->payload_type_name);
2162 payload = val_to_str_ext_wmem(NULL, streaminfo->payload_type, &rtp_payload_type_short_vals_ext, "Unknown (%u)");
2164 printf("%s{\"ssrc\":%u", sepa, streaminfo->ssrc);
2165 printf(",\"payload\":\"%s\"", payload);
2167 printf(",\"saddr\":\"%s\"", src_addr);
2168 printf(",\"sport\":%u", streaminfo->src_port);
2170 printf(",\"daddr\":\"%s\"", dst_addr);
2171 printf(",\"dport\":%u", streaminfo->dest_port);
2173 printf(",\"pkts\":%u", streaminfo->packet_count);
2175 printf(",\"max_delta\":%f", streaminfo->rtp_stats.max_delta);
2176 printf(",\"max_jitter\":%f", streaminfo->rtp_stats.max_jitter);
2177 printf(",\"mean_jitter\":%f", streaminfo->rtp_stats.mean_jitter);
2179 expected = (streaminfo->rtp_stats.stop_seq_nr + streaminfo->rtp_stats.cycles * 65536) - streaminfo->rtp_stats.start_seq_nr + 1;
2180 printf(",\"expectednr\":%u", expected);
2181 printf(",\"totalnr\":%u", streaminfo->rtp_stats.total_nr);
2183 printf(",\"problem\":%s", streaminfo->problem ? "true" : "false");
2186 printf(",\"ipver\":%d", (streaminfo->src_addr.type == AT_IPv6) ? 6 : 4);
2188 wmem_free(NULL, src_addr);
2189 wmem_free(NULL, dst_addr);
2190 wmem_free(NULL, payload);
2199 * sharkd_session_process_tap()
2201 * Process tap request
2204 * (m) tap0 - First tap request
2205 * (o) tap1...tap15 - Other tap requests
2207 * Output object with attributes:
2208 * (m) taps - array of object with attributes:
2209 * (m) tap - tap name
2210 * (m) type - tap output type
2212 * for type:stats see sharkd_session_process_tap_stats_cb()
2213 * for type:nstat see sharkd_session_process_tap_nstat_cb()
2214 * for type:conv see sharkd_session_process_tap_conv_cb()
2215 * for type:host see sharkd_session_process_tap_conv_cb()
2216 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
2217 * for type:rtp-analyse see sharkd_session_process_tap_rtp_analyse_cb()
2218 * for type:eo see sharkd_session_process_tap_eo_cb()
2219 * for type:expert see sharkd_session_process_tap_expert_cb()
2220 * for type:rtd see sharkd_session_process_tap_rtd_cb()
2221 * for type:srt see sharkd_session_process_tap_srt_cb()
2222 * for type:flow see sharkd_session_process_tap_flow_cb()
2224 * (m) err - error code
2227 sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
2229 void *taps_data[16];
2230 GFreeFunc taps_free[16];
2234 rtpstream_tapinfo_t rtp_tapinfo =
2235 { NULL, NULL, NULL, NULL, 0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE };
2237 for (i = 0; i < 16; i++)
2240 const char *tok_tap;
2242 void *tap_data = NULL;
2243 GFreeFunc tap_free = NULL;
2244 const char *tap_filter = "";
2245 GString *tap_error = NULL;
2247 ws_snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
2248 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
2252 if (!strncmp(tok_tap, "stat:", 5))
2254 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
2259 fprintf(stderr, "sharkd_session_process_tap() stat %s not found\n", tok_tap + 5);
2263 st = stats_tree_new(cfg, NULL, tap_filter);
2265 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);
2267 if (!tap_error && cfg->init)
2271 tap_free = sharkd_session_free_tap_stats_cb;
2273 else if (!strcmp(tok_tap, "expert"))
2275 struct sharkd_expert_tap *expert_tap;
2277 expert_tap = g_new0(struct sharkd_expert_tap, 1);
2278 expert_tap->text = g_string_chunk_new(100);
2280 tap_error = register_tap_listener("expert", expert_tap, NULL, 0, NULL, sharkd_session_packet_tap_expert_cb, sharkd_session_process_tap_expert_cb);
2282 tap_data = expert_tap;
2283 tap_free = sharkd_session_free_tap_expert_cb;
2285 else if (!strncmp(tok_tap, "seqa:", 5))
2287 seq_analysis_info_t *graph_analysis;
2288 register_analysis_t *analysis;
2289 const char *tap_name;
2290 tap_packet_cb tap_func;
2293 analysis = sequence_analysis_find_by_name(tok_tap + 5);
2296 fprintf(stderr, "sharkd_session_process_tap() seq analysis %s not found\n", tok_tap + 5);
2300 graph_analysis = sequence_analysis_info_new();
2301 graph_analysis->name = tok_tap + 5;
2302 /* TODO, make configurable */
2303 graph_analysis->any_addr = FALSE;
2305 tap_name = sequence_analysis_get_tap_listener_name(analysis);
2306 tap_flags = sequence_analysis_get_tap_flags(analysis);
2307 tap_func = sequence_analysis_get_packet_func(analysis);
2309 tap_error = register_tap_listener(tap_name, graph_analysis, NULL, tap_flags, NULL, tap_func, sharkd_session_process_tap_flow_cb);
2311 tap_data = graph_analysis;
2312 tap_free = sharkd_session_free_tap_flow_cb;
2314 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
2316 struct register_ct *ct = NULL;
2317 const char *ct_tapname;
2318 struct sharkd_conv_tap_data *ct_data;
2319 tap_packet_cb tap_func = NULL;
2321 if (!strncmp(tok_tap, "conv:", 5))
2323 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 5));
2325 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
2327 fprintf(stderr, "sharkd_session_process_tap() conv %s not found\n", tok_tap + 5);
2331 else if (!strncmp(tok_tap, "endpt:", 6))
2333 ct = get_conversation_by_proto_id(proto_get_id_by_short_name(tok_tap + 6));
2335 if (!ct || !(tap_func = get_hostlist_packet_func(ct)))
2337 fprintf(stderr, "sharkd_session_process_tap() endpt %s not found\n", tok_tap + 6);
2343 fprintf(stderr, "sharkd_session_process_tap() conv/endpt(?): %s not found\n", tok_tap);
2347 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
2349 ct_data = (struct sharkd_conv_tap_data *) g_malloc0(sizeof(struct sharkd_conv_tap_data));
2350 ct_data->type = tok_tap;
2351 ct_data->hash.user_data = ct_data;
2353 /* XXX: make configurable */
2354 ct_data->resolve_name = TRUE;
2355 ct_data->resolve_port = TRUE;
2357 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb);
2359 tap_data = &ct_data->hash;
2360 tap_free = sharkd_session_free_tap_conv_cb;
2362 else if (!strncmp(tok_tap, "nstat:", 6))
2364 stat_tap_table_ui *stat_tap = new_stat_tap_by_name(tok_tap + 6);
2365 new_stat_data_t *stat_data;
2369 fprintf(stderr, "sharkd_session_process_tap() nstat=%s not found\n", tok_tap + 6);
2373 stat_tap->stat_tap_init_cb(stat_tap, NULL, NULL);
2375 stat_data = g_new0(new_stat_data_t, 1);
2376 stat_data->stat_tap_data = stat_tap;
2377 stat_data->user_data = NULL;
2379 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);
2381 tap_data = stat_data;
2382 tap_free = sharkd_session_free_tap_nstat_cb;
2384 else if (!strncmp(tok_tap, "rtd:", 4))
2386 register_rtd_t *rtd = get_rtd_table_by_name(tok_tap + 4);
2387 rtd_data_t *rtd_data;
2392 fprintf(stderr, "sharkd_session_process_tap() rtd=%s not found\n", tok_tap + 4);
2396 rtd_table_get_filter(rtd, "", &tap_filter, &err);
2399 fprintf(stderr, "sharkd_session_process_tap() rtd=%s err=%s\n", tok_tap + 4, err);
2404 rtd_data = g_new0(rtd_data_t, 1);
2405 rtd_data->user_data = rtd;
2406 rtd_table_dissector_init(rtd, &rtd_data->stat_table, NULL, NULL);
2408 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);
2410 tap_data = rtd_data;
2411 tap_free = sharkd_session_free_tap_rtd_cb;
2413 else if (!strncmp(tok_tap, "srt:", 4))
2415 register_srt_t *srt = get_srt_table_by_name(tok_tap + 4);
2416 srt_data_t *srt_data;
2421 fprintf(stderr, "sharkd_session_process_tap() srt=%s not found\n", tok_tap + 4);
2425 srt_table_get_filter(srt, "", &tap_filter, &err);
2428 fprintf(stderr, "sharkd_session_process_tap() srt=%s err=%s\n", tok_tap + 4, err);
2433 srt_data = g_new0(srt_data_t, 1);
2434 srt_data->srt_array = g_array_new(FALSE, TRUE, sizeof(srt_stat_table *));
2435 srt_data->user_data = srt;
2436 srt_table_dissector_init(srt, srt_data->srt_array, NULL, NULL);
2438 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);
2440 tap_data = srt_data;
2441 tap_free = sharkd_session_free_tap_srt_cb;
2443 else if (!strncmp(tok_tap, "eo:", 3))
2445 register_eo_t *eo = get_eo_by_name(tok_tap + 3);
2446 export_object_list_t *eo_object;
2447 struct sharkd_export_object_list *object_list;
2451 fprintf(stderr, "sharkd_session_process_tap() eo=%s not found\n", tok_tap + 3);
2455 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
2457 if (!strcmp(object_list->type, tok_tap))
2459 g_slist_free_full(object_list->entries, (GDestroyNotify) eo_free_entry);
2460 object_list->entries = NULL;
2467 object_list = g_new(struct sharkd_export_object_list, 1);
2468 object_list->type = g_strdup(tok_tap);
2469 object_list->proto = proto_get_protocol_short_name(find_protocol_by_id(get_eo_proto_id(eo)));
2470 object_list->entries = NULL;
2471 object_list->next = sharkd_eo_list;
2472 sharkd_eo_list = object_list;
2475 eo_object = g_new0(export_object_list_t, 1);
2476 eo_object->add_entry = sharkd_eo_object_list_add_entry;
2477 eo_object->get_entry = sharkd_eo_object_list_get_entry;
2478 eo_object->gui_data = (void *) object_list;
2480 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);
2482 tap_data = eo_object;
2483 tap_free = g_free; /* need to free only eo_object, object_list need to be kept for potential download */
2485 else if (!strcmp(tok_tap, "rtp-streams"))
2487 tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet, sharkd_session_process_tap_rtp_cb);
2489 tap_data = &rtp_tapinfo;
2490 tap_free = rtpstream_reset_cb;
2492 else if (!strncmp(tok_tap, "rtp-analyse:", 12))
2494 struct sharkd_analyse_rtp *rtp_req;
2496 rtp_req = (struct sharkd_analyse_rtp *) g_malloc0(sizeof(*rtp_req));
2497 if (!sharkd_rtp_match_init(&rtp_req->rtp, tok_tap + 12))
2503 rtp_req->tap_name = tok_tap;
2504 rtp_req->statinfo.first_packet = TRUE;
2505 rtp_req->statinfo.reg_pt = PT_UNDEFINED;
2507 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);
2510 tap_free = sharkd_session_process_tap_rtp_free_cb;
2514 fprintf(stderr, "sharkd_session_process_tap() %s not recognized\n", tok_tap);
2520 fprintf(stderr, "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str);
2521 g_string_free(tap_error, TRUE);
2527 taps_data[taps_count] = tap_data;
2528 taps_free[taps_count] = tap_free;
2532 fprintf(stderr, "sharkd_session_process_tap() count=%d\n", taps_count);
2533 if (taps_count == 0)
2536 printf("{\"taps\":[");
2538 printf("null],\"err\":0}\n");
2540 for (i = 0; i < taps_count; i++)
2543 remove_tap_listener(taps_data[i]);
2546 taps_free[i](taps_data[i]);
2551 * sharkd_session_process_follow()
2553 * Process follow request
2556 * (m) follow - follow protocol request (e.g. HTTP)
2557 * (m) filter - filter request (e.g. tcp.stream == 1)
2559 * Output object with attributes:
2561 * (m) err - error code
2562 * (m) shost - server host
2563 * (m) sport - server port
2564 * (m) sbytes - server send bytes count
2565 * (m) chost - client host
2566 * (m) cport - client port
2567 * (m) cbytes - client send bytes count
2568 * (o) payloads - array of object with attributes:
2569 * (o) s - set if server sent, else client
2570 * (m) n - packet number
2571 * (m) d - data base64 encoded
2574 sharkd_session_process_follow(char *buf, const jsmntok_t *tokens, int count)
2576 const char *tok_follow = json_find_attr(buf, tokens, count, "follow");
2577 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2579 register_follow_t *follower;
2582 follow_info_t *follow_info;
2586 if (!tok_follow || !tok_filter)
2589 follower = get_follow_by_name(tok_follow);
2592 fprintf(stderr, "sharkd_session_process_follow() follower=%s not found\n", tok_follow);
2596 /* follow_reset_stream ? */
2597 follow_info = g_new0(follow_info_t, 1);
2598 /* gui_data, filter_out_filter not set, but not used by dissector */
2600 tap_error = register_tap_listener(get_follow_tap_string(follower), follow_info, tok_filter, 0, NULL, get_follow_tap_handler(follower), NULL);
2603 fprintf(stderr, "sharkd_session_process_follow() name=%s error=%s", tok_follow, tap_error->str);
2604 g_string_free(tap_error, TRUE);
2605 g_free(follow_info);
2613 printf("\"err\":0");
2615 /* Server information: hostname, port, bytes sent */
2616 host = address_to_name(&follow_info->server_ip);
2617 printf(",\"shost\":");
2618 json_puts_string(host);
2620 port = get_follow_port_to_display(follower)(NULL, follow_info->server_port);
2621 printf(",\"sport\":");
2622 json_puts_string(port);
2623 wmem_free(NULL, port);
2625 printf(",\"sbytes\":%u", follow_info->bytes_written[0]);
2627 /* Client information: hostname, port, bytes sent */
2628 host = address_to_name(&follow_info->client_ip);
2629 printf(",\"chost\":");
2630 json_puts_string(host);
2632 port = get_follow_port_to_display(follower)(NULL, follow_info->client_port);
2633 printf(",\"cport\":");
2634 json_puts_string(port);
2635 wmem_free(NULL, port);
2637 printf(",\"cbytes\":%u", follow_info->bytes_written[1]);
2639 if (follow_info->payload)
2641 follow_record_t *follow_record;
2643 const char *sepa = "";
2645 printf(",\"payloads\":[");
2647 for (cur = follow_info->payload; cur; cur = g_list_next(cur))
2649 follow_record = (follow_record_t *) cur->data;
2651 printf("%s{", sepa);
2653 printf("\"n\":%u", follow_record->packet_num);
2656 json_print_base64(follow_record->data->data, follow_record->data->len);
2658 if (follow_record->is_server)
2659 printf(",\"s\":%d", 1);
2670 remove_tap_listener(follow_info);
2671 follow_info_free(follow_info);
2675 sharkd_session_process_frame_cb_tree(epan_dissect_t *edt, proto_tree *tree, tvbuff_t **tvbs)
2678 const char *sepa = "";
2681 for (node = tree->first_child; node; node = node->next)
2683 field_info *finfo = PNODE_FINFO(node);
2688 /* XXX, for now always skip hidden */
2689 if (FI_GET_FLAG(finfo, FI_HIDDEN))
2692 printf("%s{", sepa);
2697 char label_str[ITEM_LABEL_LENGTH];
2699 label_str[0] = '\0';
2700 proto_item_fill_label(finfo, label_str);
2701 json_puts_string(label_str);
2705 json_puts_string(finfo->rep->representation);
2708 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
2712 for (idx = 1; tvbs[idx]; idx++)
2714 if (tvbs[idx] == finfo->ds_tvb)
2716 printf(",\"ds\":%d", idx);
2722 if (finfo->start >= 0 && finfo->length > 0)
2723 printf(",\"h\":[%d,%d]", finfo->start, finfo->length);
2725 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
2726 printf(",\"i\":[%d,%d]", finfo->appendix_start, finfo->appendix_length);
2733 if (finfo->hfinfo->type == FT_PROTOCOL)
2735 printf(",\"t\":\"proto\"");
2737 else if (finfo->hfinfo->type == FT_FRAMENUM)
2739 printf(",\"t\":\"framenum\",\"fnum\":%u", finfo->value.value.uinteger);
2741 else if (FI_GET_FLAG(finfo, FI_URL) && IS_FT_STRING(finfo->hfinfo->type))
2743 char *url = fvalue_to_string_repr(NULL, &finfo->value, FTREPR_DISPLAY, finfo->hfinfo->display);
2745 printf(",\"t\":\"url\",\"url\":");
2746 json_puts_string(url);
2747 wmem_free(NULL, url);
2750 filter = proto_construct_match_selected_string(finfo, edt);
2754 json_puts_string(filter);
2755 wmem_free(NULL, filter);
2759 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
2761 const char *severity = try_val_to_str(FI_GET_FLAG(finfo, PI_SEVERITY_MASK), expert_severity_vals);
2763 g_assert(severity != NULL);
2765 printf(",\"s\":\"%s\"", severity);
2768 if (((proto_tree *) node)->first_child)
2770 if (finfo->tree_type != -1)
2771 printf(",\"e\":%d", finfo->tree_type);
2773 sharkd_session_process_frame_cb_tree(edt, (proto_tree *) node, tvbs);
2783 sharkd_follower_visit_layers_cb(const void *key _U_, void *value, void *user_data)
2785 register_follow_t *follower = (register_follow_t *) value;
2786 packet_info *pi = (packet_info *) user_data;
2788 const int proto_id = get_follow_proto_id(follower);
2790 guint32 ignore_stream;
2792 if (proto_is_frame_protocol(pi->layers, proto_get_protocol_filter_name(proto_id)))
2794 const char *layer_proto = proto_get_protocol_short_name(find_protocol_by_id(proto_id));
2795 char *follow_filter;
2797 follow_filter = get_follow_conv_func(follower)(pi, &ignore_stream);
2799 printf(",[\"%s\",", layer_proto);
2800 json_puts_string(follow_filter);
2803 g_free(follow_filter);
2810 sharkd_session_process_frame_cb(epan_dissect_t *edt, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
2812 packet_info *pi = &edt->pi;
2813 frame_data *fdata = pi->fd;
2814 const char *pkt_comment = NULL;
2820 printf("\"err\":0");
2822 if (fdata->flags.has_user_comment)
2823 pkt_comment = sharkd_get_user_comment(fdata);
2824 else if (fdata->flags.has_phdr_comment)
2825 pkt_comment = pi->phdr->opt_comment;
2829 printf(",\"comment\":");
2830 json_puts_string(pkt_comment);
2835 tvbuff_t **tvbs = NULL;
2837 printf(",\"tree\":");
2839 /* arrayize data src, to speedup searching for ds_tvb index */
2840 if (data_src && data_src->next /* only needed if there are more than one data source */)
2842 guint count = g_slist_length((GSList *) data_src);
2845 tvbs = (tvbuff_t **) g_malloc((count + 1) * sizeof(*tvbs));
2847 for (i = 0; i < count; i++)
2849 struct data_source *src = (struct data_source *) g_slist_nth_data((GSList *) data_src, i);
2851 tvbs[i] = get_data_source_tvb(src);
2857 sharkd_session_process_frame_cb_tree(edt, tree, tvbs);
2866 printf(",\"col\":[");
2867 for (col = 0; col < cinfo->num_cols; ++col)
2869 const col_item_t *col_item = &cinfo->columns[col];
2871 printf("%s\"%s\"", (col) ? "," : "", col_item->col_data);
2878 struct data_source *src = (struct data_source *) data_src->data;
2879 const char *ds_sepa = NULL;
2884 tvb = get_data_source_tvb(src);
2885 length = tvb_captured_length(tvb);
2887 printf(",\"bytes\":");
2890 const guchar *cp = tvb_get_ptr(tvb, 0, length);
2892 /* XXX pi.fd->flags.encoding */
2893 json_print_base64(cp, length);
2897 json_print_base64("", 0);
2900 data_src = data_src->next;
2903 printf(",\"ds\":[");
2909 src = (struct data_source *) data_src->data;
2912 char *src_name = get_data_source_name(src);
2914 printf("%s{\"name\":", ds_sepa);
2915 json_puts_string(src_name);
2916 wmem_free(NULL, src_name);
2919 tvb = get_data_source_tvb(src);
2920 length = tvb_captured_length(tvb);
2922 printf(",\"bytes\":");
2925 const guchar *cp = tvb_get_ptr(tvb, 0, length);
2927 /* XXX pi.fd->flags.encoding */
2928 json_print_base64(cp, length);
2932 json_print_base64("", 0);
2938 data_src = data_src->next;
2941 /* close ds, only if was opened */
2942 if (ds_sepa != NULL)
2946 printf(",\"fol\":[0");
2947 follow_iterate_followers(sharkd_follower_visit_layers_cb, pi);
2954 * sharkd_session_process_intervals()
2956 * Process intervals request - generate basic capture file statistics per requested interval.
2959 * (o) interval - interval time in ms, if not specified: 1000ms
2960 * (o) filter - filter for generating interval request
2962 * Output object with attributes:
2963 * (m) intervals - array of intervals, with indexes:
2964 * [0] - index of interval,
2965 * [1] - number of frames during interval,
2966 * [2] - number of bytes during interval.
2968 * (m) last - last interval number.
2969 * (m) frames - total number of frames
2970 * (m) bytes - total number of bytes
2972 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
2975 sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
2977 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
2978 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
2980 const guint8 *filter_data = NULL;
2984 unsigned int frames;
2990 guint32 interval_ms = 1000; /* default: one per second */
2992 const char *sepa = "";
2993 unsigned int framenum;
2999 if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0)
3001 fprintf(stderr, "Invalid interval parameter: %s.\n", tok_interval);
3008 filter_data = sharkd_session_filter_data(tok_filter);
3013 st_total.frames = 0;
3021 printf("{\"intervals\":[");
3023 start_ts = (cfile.count >= 1) ? &(sharkd_get_frame(1)->abs_ts) : NULL;
3025 for (framenum = 1; framenum <= cfile.count; framenum++)
3031 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
3034 fdata = sharkd_get_frame(framenum);
3036 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * (gint64) 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
3037 new_idx = msec_rel / interval_ms;
3043 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
3056 st.bytes += fdata->pkt_len;
3058 st_total.frames += 1;
3059 st_total.bytes += fdata->pkt_len;
3064 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
3068 printf("],\"last\":%" G_GINT64_FORMAT ",\"frames\":%u,\"bytes\":%" G_GUINT64_FORMAT "}\n", max_idx, st_total.frames, st_total.bytes);
3072 * sharkd_session_process_frame()
3074 * Process frame request
3077 * (m) frame - requested frame number
3078 * (o) ref_frame - time reference frame number
3079 * (o) prev_frame - previously displayed frame number
3080 * (o) proto - set if output frame tree
3081 * (o) columns - set if output frame columns
3082 * (o) bytes - set if output frame bytes
3084 * Output object with attributes:
3085 * (m) err - 0 if succeed
3086 * (o) tree - array of frame nodes with attributes:
3088 * t: 'proto', 'framenum', 'url' - type of node
3091 * e - subtree ett index
3092 * n - array of subtree nodes
3093 * h - two item array: (item start, item length)
3094 * i - two item array: (appendix start, appendix length)
3095 * p - [RESERVED] two item array: (protocol start, protocol length)
3096 * ds- data src index
3097 * url - only for t:'url', url
3098 * fnum - only for t:'framenum', frame number
3100 * (o) col - array of column data
3101 * (o) bytes - base64 of frame bytes
3102 * (o) ds - array of other data srcs
3103 * (o) comment - frame comment
3104 * (o) fol - array of follow filters:
3106 * [1] - filter string
3109 sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
3111 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
3112 const char *tok_ref_frame = json_find_attr(buf, tokens, count, "ref_frame");
3113 const char *tok_prev_frame = json_find_attr(buf, tokens, count, "prev_frame");
3114 int tok_proto = (json_find_attr(buf, tokens, count, "proto") != NULL);
3115 int tok_bytes = (json_find_attr(buf, tokens, count, "bytes") != NULL);
3116 int tok_columns = (json_find_attr(buf, tokens, count, "columns") != NULL);
3118 guint32 framenum, ref_frame_num, prev_dis_num;
3120 if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
3123 ref_frame_num = (framenum != 1) ? 1 : 0;
3124 if (tok_ref_frame && (!ws_strtou32(tok_ref_frame, NULL, &ref_frame_num) || ref_frame_num > framenum))
3127 prev_dis_num = framenum - 1;
3128 if (tok_prev_frame && (!ws_strtou32(tok_prev_frame, NULL, &prev_dis_num) || prev_dis_num >= framenum))
3131 sharkd_dissect_request(framenum, ref_frame_num, prev_dis_num, &sharkd_session_process_frame_cb, tok_bytes, tok_columns, tok_proto, NULL);
3135 * sharkd_session_process_check()
3137 * Process check request.
3140 * (o) filter - filter to be checked
3142 * Output object with attributes:
3143 * (m) err - always 0
3144 * (o) filter - 'ok', 'warn' or error message
3147 sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
3149 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
3151 printf("{\"err\":0");
3152 if (tok_filter != NULL)
3154 char *err_msg = NULL;
3157 if (dfilter_compile(tok_filter, &dfp, &err_msg))
3159 const char *s = "ok";
3161 if (dfilter_deprecated_tokens(dfp))
3164 printf(",\"filter\":\"%s\"", s);
3169 printf(",\"filter\":");
3170 json_puts_string(err_msg);
3179 struct sharkd_session_process_complete_pref_data
3187 sharkd_session_process_complete_pref_cb(module_t *module, gpointer d)
3189 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
3191 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
3194 printf("%s{\"f\":\"%s\",\"d\":\"%s\"}", data->sepa, module->name, module->title);
3201 sharkd_session_process_complete_pref_option_cb(pref_t *pref, gpointer d)
3203 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
3204 const char *pref_name = prefs_get_name(pref);
3205 const char *pref_title = prefs_get_title(pref);
3207 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
3210 printf("%s{\"f\":\"%s.%s\",\"d\":\"%s\"}", data->sepa, data->module, pref_name, pref_title);
3213 return 0; /* continue */
3217 * sharkd_session_process_complete()
3219 * Process complete request
3222 * (o) field - field to be completed
3223 * (o) pref - preference to be completed
3225 * Output object with attributes:
3226 * (m) err - always 0
3227 * (o) field - array of object with attributes:
3228 * (m) f - field text
3229 * (o) t - field type (FT_ number)
3230 * (o) n - field name
3231 * (o) pref - array of object with attributes:
3233 * (o) d - pref description
3236 sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
3238 const char *tok_field = json_find_attr(buf, tokens, count, "field");
3239 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
3241 printf("{\"err\":0");
3242 if (tok_field != NULL && tok_field[0])
3244 const size_t filter_length = strlen(tok_field);
3245 const int filter_with_dot = !!strchr(tok_field, '.');
3250 const char *sepa = "";
3252 printf(",\"field\":[");
3254 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
3256 protocol_t *protocol = find_protocol_by_id(proto_id);
3257 const char *protocol_filter;
3258 const char *protocol_name;
3259 header_field_info *hfinfo;
3261 if (!proto_is_protocol_enabled(protocol))
3264 protocol_name = proto_get_protocol_long_name(protocol);
3265 protocol_filter = proto_get_protocol_filter_name(proto_id);
3267 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
3269 printf("%s{", sepa);
3272 json_puts_string(protocol_filter);
3273 printf(",\"t\":%d", FT_PROTOCOL);
3275 json_puts_string(protocol_name);
3281 if (!filter_with_dot)
3284 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL; hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
3286 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
3289 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
3291 printf("%s{", sepa);
3294 json_puts_string(hfinfo->abbrev);
3296 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
3297 if (hfinfo->same_name_next == NULL)
3299 printf(",\"t\":%d", hfinfo->type);
3301 json_puts_string(hfinfo->name);
3313 if (tok_pref != NULL && tok_pref[0])
3315 struct sharkd_session_process_complete_pref_data data;
3318 data.module = tok_pref;
3319 data.pref = tok_pref;
3322 printf(",\"pref\":[");
3324 if ((dot_sepa = strchr(tok_pref, '.')))
3328 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
3329 data.pref = dot_sepa + 1;
3331 pref_mod = prefs_find_module(data.module);
3333 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
3339 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
3351 * sharkd_session_process_setcomment()
3353 * Process setcomment request
3356 * (m) frame - frame number
3357 * (o) comment - user comment
3359 * Output object with attributes:
3360 * (m) err - error code: 0 succeed
3363 sharkd_session_process_setcomment(char *buf, const jsmntok_t *tokens, int count)
3365 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
3366 const char *tok_comment = json_find_attr(buf, tokens, count, "comment");
3372 if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
3375 fdata = sharkd_get_frame(framenum);
3379 ret = sharkd_set_user_comment(fdata, tok_comment);
3380 printf("{\"err\":%d}\n", ret);
3384 * sharkd_session_process_setconf()
3386 * Process setconf request
3389 * (m) name - preference name
3390 * (m) value - preference value
3392 * Output object with attributes:
3393 * (m) err - error code: 0 succeed
3396 sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
3398 const char *tok_name = json_find_attr(buf, tokens, count, "name");
3399 const char *tok_value = json_find_attr(buf, tokens, count, "value");
3401 char *errmsg = NULL;
3403 prefs_set_pref_e ret;
3405 if (!tok_name || tok_name[0] == '\0' || !tok_value)
3408 ws_snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
3410 ret = prefs_set_pref(pref, &errmsg);
3411 printf("{\"err\":%d", ret);
3414 /* Add error message for some syntax errors. */
3415 printf(",\"errmsg\":");
3416 json_puts_string(errmsg);
3422 struct sharkd_session_process_dumpconf_data
3429 sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d)
3431 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
3432 const char *pref_name = prefs_get_name(pref);
3434 printf("%s\"%s.%s\":{", data->sepa, data->module->name, pref_name);
3436 switch (prefs_get_type(pref))
3439 case PREF_DECODE_AS_UINT:
3440 printf("\"u\":%u", prefs_get_uint_value_real(pref, pref_current));
3441 if (prefs_get_uint_base(pref) != 10)
3442 printf(",\"ub\":%u", prefs_get_uint_base(pref));
3446 printf("\"b\":%s", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
3450 case PREF_SAVE_FILENAME:
3451 case PREF_OPEN_FILENAME:
3454 json_puts_string(prefs_get_string_value(pref, pref_current));
3459 const enum_val_t *enums;
3460 const char *enum_sepa = "";
3463 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
3465 printf("%s{\"v\":%d", enum_sepa, enums->value);
3467 if (enums->value == prefs_get_enum_value(pref, pref_current))
3471 json_puts_string(enums->description);
3481 case PREF_DECODE_AS_RANGE:
3483 char *range_str = range_convert_range(NULL, prefs_get_range_value_real(pref, pref_current));
3484 printf("\"r\":\"%s\"", range_str);
3485 wmem_free(NULL, range_str);
3491 uat_t *uat = prefs_get_uat_value(pref);
3495 for (idx = 0; idx < uat->raw_data->len; idx++)
3497 void *rec = UAT_INDEX_PTR(uat, idx);
3504 for (colnum = 0; colnum < uat->ncols; colnum++)
3506 char *str = uat_fld_tostr(rec, &(uat->fields[colnum]));
3511 json_puts_string(str);
3524 case PREF_STATIC_TEXT:
3532 json_puts_string(prefs_get_title(pref));
3538 return 0; /* continue */
3542 sharkd_session_process_dumpconf_mod_cb(module_t *module, gpointer d)
3544 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
3546 data->module = module;
3547 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
3553 * sharkd_session_process_dumpconf()
3555 * Process dumpconf request
3558 * (o) pref - module, or preference, NULL for all
3560 * Output object with attributes:
3561 * (o) prefs - object with module preferences
3562 * (m) [KEY] - preference name
3563 * (o) u - preference value (for PREF_UINT, PREF_DECODE_AS_UINT)
3564 * (o) ub - preference value suggested base for display (for PREF_UINT, PREF_DECODE_AS_UINT) and if different than 10
3565 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
3566 * (o) s - preference value (for PREF_STRING, PREF_SAVE_FILENAME, PREF_OPEN_FILENAME, PREF_DIRNAME)
3567 * (o) e - preference possible values (only for PREF_ENUM)
3568 * (o) r - preference value (for PREF_RANGE, PREF_DECODE_AS_RANGE)
3569 * (o) t - preference value (only for PREF_UAT)
3572 sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
3574 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
3580 struct sharkd_session_process_dumpconf_data data;
3585 printf("{\"prefs\":{");
3586 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
3591 if ((dot_sepa = strchr(tok_pref, '.')))
3593 pref_t *pref = NULL;
3595 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
3596 pref_mod = prefs_find_module(tok_pref);
3598 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
3603 struct sharkd_session_process_dumpconf_data data;
3605 data.module = pref_mod;
3608 printf("{\"prefs\":{");
3609 sharkd_session_process_dumpconf_cb(pref, &data);
3616 pref_mod = prefs_find_module(tok_pref);
3619 struct sharkd_session_process_dumpconf_data data;
3621 data.module = pref_mod;
3624 printf("{\"prefs\":{");
3625 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
3630 struct sharkd_download_rtp
3632 struct sharkd_rtp_match rtp;
3638 sharkd_rtp_download_free_items(void *ptr)
3640 rtp_packet_t *rtp_packet = (rtp_packet_t *) ptr;
3642 g_free(rtp_packet->info);
3643 g_free(rtp_packet->payload_data);
3648 sharkd_rtp_download_decode(struct sharkd_download_rtp *req)
3650 /* based on RtpAudioStream::decode() 6e29d874f8b5e6ebc59f661a0bb0dab8e56f122a */
3651 /* TODO, for now only without silence (timing_mode_ = Uninterrupted) */
3653 static const int sample_bytes_ = sizeof(SAMPLE) / sizeof(char);
3655 guint32 audio_out_rate_ = 0;
3656 struct _GHashTable *decoders_hash_ = rtp_decoder_hash_table_new();
3657 struct SpeexResamplerState_ *audio_resampler_ = NULL;
3659 gsize resample_buff_len = 0x1000;
3660 SAMPLE *resample_buff = (SAMPLE *) g_malloc(resample_buff_len);
3661 spx_uint32_t cur_in_rate = 0;
3662 char *write_buff = NULL;
3663 gint64 write_bytes = 0;
3664 unsigned channels = 0;
3665 unsigned sample_rate = 0;
3668 int base64_state1 = 0;
3669 int base64_state2 = 0;
3673 for (l = req->packets; l; l = l->next)
3675 rtp_packet_t *rtp_packet = (rtp_packet_t *) l->data;
3677 SAMPLE *decode_buff = NULL;
3678 size_t decoded_bytes;
3680 decoded_bytes = decode_rtp_packet(rtp_packet, &decode_buff, decoders_hash_, &channels, &sample_rate);
3681 if (decoded_bytes == 0 || sample_rate == 0)
3683 /* We didn't decode anything. Clean up and prep for the next packet. */
3684 g_free(decode_buff);
3688 if (audio_out_rate_ == 0)
3694 /* First non-zero wins */
3695 audio_out_rate_ = sample_rate;
3697 RTP_STREAM_DEBUG("Audio sample rate is %u", audio_out_rate_);
3699 /* write WAVE header */
3700 memset(&wav_hdr, 0, sizeof(wav_hdr));
3701 memcpy(&wav_hdr[0], "RIFF", 4);
3702 memcpy(&wav_hdr[4], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
3703 memcpy(&wav_hdr[8], "WAVE", 4);
3705 memcpy(&wav_hdr[12], "fmt ", 4);
3706 memcpy(&wav_hdr[16], "\x10\x00\x00\x00", 4); /* PCM */
3707 memcpy(&wav_hdr[20], "\x01\x00", 2); /* PCM */
3710 memcpy(&wav_hdr[22], &tmp16, 2);
3712 tmp32 = sample_rate;
3713 memcpy(&wav_hdr[24], &tmp32, 4);
3715 tmp32 = sample_rate * channels * sample_bytes_;
3716 memcpy(&wav_hdr[28], &tmp32, 4);
3718 tmp16 = channels * sample_bytes_;
3719 memcpy(&wav_hdr[32], &tmp16, 2);
3720 /* bits per sample */
3721 tmp16 = 8 * sample_bytes_;
3722 memcpy(&wav_hdr[34], &tmp16, 2);
3724 memcpy(&wav_hdr[36], "data", 4);
3725 memcpy(&wav_hdr[40], "\xFF\xFF\xFF\xFF", 4); /* XXX, unknown */
3727 for (i = 0; i < (int) sizeof(wav_hdr); i++)
3728 json_print_base64_step(&wav_hdr[i], &base64_state1, &base64_state2);
3731 // Write samples to our file.
3732 write_buff = (char *) decode_buff;
3733 write_bytes = decoded_bytes;
3735 if (audio_out_rate_ != sample_rate)
3737 spx_uint32_t in_len, out_len;
3739 /* Resample the audio to match our previous output rate. */
3740 if (!audio_resampler_)
3742 audio_resampler_ = speex_resampler_init(1, sample_rate, audio_out_rate_, 10, NULL);
3743 speex_resampler_skip_zeros(audio_resampler_);
3744 RTP_STREAM_DEBUG("Started resampling from %u to (out) %u Hz.", sample_rate, audio_out_rate_);
3748 spx_uint32_t audio_out_rate;
3749 speex_resampler_get_rate(audio_resampler_, &cur_in_rate, &audio_out_rate);
3751 if (sample_rate != cur_in_rate)
3753 speex_resampler_set_rate(audio_resampler_, sample_rate, audio_out_rate);
3754 RTP_STREAM_DEBUG("Changed input rate from %u to %u Hz. Out is %u.", cur_in_rate, sample_rate, audio_out_rate_);
3757 in_len = (spx_uint32_t)rtp_packet->info->info_payload_len;
3758 out_len = (audio_out_rate_ * (spx_uint32_t)rtp_packet->info->info_payload_len / sample_rate) + (audio_out_rate_ % sample_rate != 0);
3759 if (out_len * sample_bytes_ > resample_buff_len)
3761 while ((out_len * sample_bytes_ > resample_buff_len))
3762 resample_buff_len *= 2;
3763 resample_buff = (SAMPLE *) g_realloc(resample_buff, resample_buff_len);
3766 speex_resampler_process_int(audio_resampler_, 0, decode_buff, &in_len, resample_buff, &out_len);
3767 write_buff = (char *) resample_buff;
3768 write_bytes = out_len * sample_bytes_;
3771 /* Write the decoded, possibly-resampled audio */
3772 for (i = 0; i < write_bytes; i++)
3773 json_print_base64_step(&write_buff[i], &base64_state1, &base64_state2);
3775 g_free(decode_buff);
3778 json_print_base64_step(NULL, &base64_state1, &base64_state2);
3780 g_free(resample_buff);
3781 g_hash_table_destroy(decoders_hash_);
3785 sharkd_session_packet_download_tap_rtp_cb(void *tapdata, packet_info *pinfo, epan_dissect_t *edt _U_, const void *data)
3787 const struct _rtp_info *rtp_info = (const struct _rtp_info *) data;
3788 struct sharkd_download_rtp *req_rtp = (struct sharkd_download_rtp *) tapdata;
3790 /* do not consider RTP packets without a setup frame */
3791 if (rtp_info->info_setup_frame_num == 0)
3794 if (sharkd_rtp_match_check(&req_rtp->rtp, pinfo, rtp_info))
3796 rtp_packet_t *rtp_packet;
3798 rtp_packet = g_new0(rtp_packet_t, 1);
3799 rtp_packet->info = (struct _rtp_info *) g_memdup(rtp_info, sizeof(struct _rtp_info));
3801 if (rtp_info->info_all_data_present && rtp_info->info_payload_len != 0)
3802 rtp_packet->payload_data = (guint8 *) g_memdup(&(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
3804 if (!req_rtp->packets)
3805 req_rtp->start_time = nstime_to_sec(&pinfo->abs_ts);
3807 rtp_packet->frame_num = pinfo->num;
3808 rtp_packet->arrive_offset = nstime_to_sec(&pinfo->abs_ts) - req_rtp->start_time;
3810 /* XXX, O(n) optimize */
3811 req_rtp->packets = g_slist_append(req_rtp->packets, rtp_packet);
3818 * sharkd_session_process_download()
3820 * Process download request
3823 * (m) token - token to download
3825 * Output object with attributes:
3826 * (o) file - suggested name of file
3827 * (o) mime - suggested content type
3828 * (o) data - payload base64 encoded
3831 sharkd_session_process_download(char *buf, const jsmntok_t *tokens, int count)
3833 const char *tok_token = json_find_attr(buf, tokens, count, "token");
3838 if (!strncmp(tok_token, "eo:", 3))
3840 struct sharkd_export_object_list *object_list;
3841 const export_object_entry_t *eo_entry = NULL;
3843 for (object_list = sharkd_eo_list; object_list; object_list = object_list->next)
3845 size_t eo_type_len = strlen(object_list->type);
3847 if (!strncmp(tok_token, object_list->type, eo_type_len) && tok_token[eo_type_len] == '_')
3851 if (sscanf(&tok_token[eo_type_len + 1], "%d", &row) != 1)
3854 eo_entry = (export_object_entry_t *) g_slist_nth_data(object_list->entries, row);
3861 const char *mime = (eo_entry->content_type) ? eo_entry->content_type : "application/octet-stream";
3862 const char *filename = (eo_entry->filename) ? eo_entry->filename : tok_token;
3864 printf("{\"file\":");
3865 json_puts_string(filename);
3866 printf(",\"mime\":");
3867 json_puts_string(mime);
3868 printf(",\"data\":");
3869 json_print_base64(eo_entry->payload_data, (size_t)(eo_entry->payload_len));
3873 else if (!strcmp(tok_token, "ssl-secrets"))
3875 char *str = ssl_export_sessions();
3879 const char *mime = "text/plain";
3880 const char *filename = "keylog.txt";
3882 printf("{\"file\":");
3883 json_puts_string(filename);
3884 printf(",\"mime\":");
3885 json_puts_string(mime);
3886 printf(",\"data\":");
3887 json_print_base64(str, strlen(str));
3892 else if (!strncmp(tok_token, "rtp:", 4))
3894 struct sharkd_download_rtp rtp_req;
3897 memset(&rtp_req, 0, sizeof(rtp_req));
3898 if (!sharkd_rtp_match_init(&rtp_req.rtp, tok_token + 4))
3900 fprintf(stderr, "sharkd_session_process_download() rtp tokenizing error %s\n", tok_token);
3904 tap_error = register_tap_listener("rtp", &rtp_req, NULL, 0, NULL, sharkd_session_packet_download_tap_rtp_cb, NULL);
3907 fprintf(stderr, "sharkd_session_process_download() rtp error=%s", tap_error->str);
3908 g_string_free(tap_error, TRUE);
3913 remove_tap_listener(&rtp_req);
3915 if (rtp_req.packets)
3917 const char *mime = "audio/x-wav";
3918 const char *filename = tok_token;
3920 printf("{\"file\":");
3921 json_puts_string(filename);
3922 printf(",\"mime\":");
3923 json_puts_string(mime);
3925 printf(",\"data\":");
3927 sharkd_rtp_download_decode(&rtp_req);
3932 g_slist_free_full(rtp_req.packets, sharkd_rtp_download_free_items);
3938 sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
3942 /* sanity check, and split strings */
3943 if (count < 1 || tokens[0].type != JSMN_OBJECT)
3945 fprintf(stderr, "sanity check(1): [0] not object\n");
3949 /* don't need [0] token */
3955 fprintf(stderr, "sanity check(2): %d not even\n", count);
3959 for (i = 0; i < count; i += 2)
3961 if (tokens[i].type != JSMN_STRING)
3963 fprintf(stderr, "sanity check(3): [%d] not string\n", i);
3967 if (tokens[i + 1].type != JSMN_STRING && tokens[i + 1].type != JSMN_PRIMITIVE)
3969 fprintf(stderr, "sanity check(3a): [%d] wrong type\n", i + 1);
3973 buf[tokens[i + 0].end] = '\0';
3974 buf[tokens[i + 1].end] = '\0';
3976 /* unescape only value, as keys are simple strings */
3977 if (tokens[i + 1].type == JSMN_STRING && !json_unescape_str(&buf[tokens[i + 1].start]))
3979 fprintf(stderr, "sanity check(3b): [%d] cannot unescape string\n", i + 1);
3985 const char *tok_req = json_find_attr(buf, tokens, count, "req");
3989 fprintf(stderr, "sanity check(4): no \"req\".\n");
3993 if (!strcmp(tok_req, "load"))
3994 sharkd_session_process_load(buf, tokens, count);
3995 else if (!strcmp(tok_req, "status"))
3996 sharkd_session_process_status();
3997 else if (!strcmp(tok_req, "analyse"))
3998 sharkd_session_process_analyse();
3999 else if (!strcmp(tok_req, "info"))
4000 sharkd_session_process_info();
4001 else if (!strcmp(tok_req, "check"))
4002 sharkd_session_process_check(buf, tokens, count);
4003 else if (!strcmp(tok_req, "complete"))
4004 sharkd_session_process_complete(buf, tokens, count);
4005 else if (!strcmp(tok_req, "frames"))
4006 sharkd_session_process_frames(buf, tokens, count);
4007 else if (!strcmp(tok_req, "tap"))
4008 sharkd_session_process_tap(buf, tokens, count);
4009 else if (!strcmp(tok_req, "follow"))
4010 sharkd_session_process_follow(buf, tokens, count);
4011 else if (!strcmp(tok_req, "intervals"))
4012 sharkd_session_process_intervals(buf, tokens, count);
4013 else if (!strcmp(tok_req, "frame"))
4014 sharkd_session_process_frame(buf, tokens, count);
4015 else if (!strcmp(tok_req, "setcomment"))
4016 sharkd_session_process_setcomment(buf, tokens, count);
4017 else if (!strcmp(tok_req, "setconf"))
4018 sharkd_session_process_setconf(buf, tokens, count);
4019 else if (!strcmp(tok_req, "dumpconf"))
4020 sharkd_session_process_dumpconf(buf, tokens, count);
4021 else if (!strcmp(tok_req, "download"))
4022 sharkd_session_process_download(buf, tokens, count);
4023 else if (!strcmp(tok_req, "bye"))
4026 fprintf(stderr, "::: req = %s\n", tok_req);
4028 /* reply for every command are 0+ lines of JSON reply (outputed above), finished by empty new line */
4032 * We do an explicit fflush after every line, because
4033 * we want output to be written to the socket as soon
4034 * as the line is complete.
4036 * The stream is fully-buffered by default, so it's
4037 * only flushed when the buffer fills or the FILE *
4038 * is closed. On UN*X, we could set it to be line
4039 * buffered, but the MSVC standard I/O routines don't
4040 * support line buffering - they only support *byte*
4041 * buffering, doing a write for every byte written,
4042 * which is too inefficient, and full buffering,
4043 * which is what you get if you request line buffering.
4050 sharkd_session_main(void)
4053 jsmntok_t *tokens = NULL;
4054 int tokens_max = -1;
4056 fprintf(stderr, "Hello in child.\n");
4058 filter_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, sharkd_session_filter_free);
4060 while (fgets(buf, sizeof(buf), stdin))
4062 /* every command is line seperated JSON */
4065 ret = wsjsmn_parse(buf, NULL, 0);
4068 fprintf(stderr, "invalid JSON -> closing\n");
4072 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
4075 if (tokens == NULL || tokens_max < ret)
4078 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
4081 memset(tokens, 0, ret * sizeof(jsmntok_t));
4083 ret = wsjsmn_parse(buf, tokens, ret);
4086 fprintf(stderr, "invalid JSON(2) -> closing\n");
4090 sharkd_session_process(buf, tokens, ret);
4093 g_hash_table_destroy(filter_table);
4100 * Editor modelines - http://www.wireshark.org/tools/modelines.html
4105 * indent-tabs-mode: t
4108 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
4109 * :indentSize=8:tabSize=8:noTabs=false: