3 * Copyright (C) 2016 Jakub Zawadzki
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33 #include <wsutil/wsjsmn.h>
34 #include <wsutil/ws_printf.h>
37 #include <epan/exceptions.h>
38 #include <epan/color_filters.h>
39 #include <epan/prefs.h>
40 #include <epan/prefs-int.h>
41 #include <epan/uat-int.h>
42 #include <wiretap/wtap.h>
44 #include <epan/column.h>
46 #include <epan/stats_tree_priv.h>
47 #include <epan/stat_tap_ui.h>
48 #include <epan/conversation_table.h>
50 #include <epan/dissectors/packet-h225.h>
51 #include <epan/rtp_pt.h>
52 #include <ui/voip_calls.h>
53 #include <ui/rtp_stream.h>
54 #include <ui/tap-rtp-common.h>
55 #include <epan/to_str.h>
59 # include <epan/geoip_db.h>
60 # include <wsutil/pint.h>
63 #include <wsutil/strtoi.h>
67 static struct register_ct *
68 _get_conversation_table_by_name(const char *name)
70 return get_conversation_by_proto_id(proto_get_id_by_short_name(name));
74 json_unescape_str(char *input)
84 /* TODO, add more escaping rules */
96 json_find_attr(const char *buf, const jsmntok_t *tokens, int count, const char *attr)
100 for (i = 0; i < count; i += 2)
102 const char *tok_attr = &buf[tokens[i + 0].start];
103 const char *tok_value = &buf[tokens[i + 1].start];
105 if (!strcmp(tok_attr, attr))
113 json_puts_string(const char *str)
121 for (i = 0; str[i]; i++)
146 json_print_base64(const guint8 *data, int len)
149 int base64_state1 = 0;
150 int base64_state2 = 0;
152 gchar buf[(1 / 3 + 1) * 4 + 4 + 1];
156 for (i = 0; i < len; i++)
158 wrote = g_base64_encode_step(&data[i], 1, FALSE, buf, &base64_state1, &base64_state2);
166 wrote = g_base64_encode_close(FALSE, buf, &base64_state1, &base64_state2);
178 struct filter_item *next;
184 static struct filter_item *filter_list = NULL;
186 static const guint8 *
187 sharkd_session_filter_data(const char *filter)
189 struct filter_item *l;
191 for (l = filter_list; l; l = l->next)
193 if (!strcmp(l->filter, filter))
198 guint8 *filtered = NULL;
200 int ret = sharkd_filter(filter, &filtered);
205 l = (struct filter_item *) g_malloc(sizeof(struct filter_item));
206 l->filter = g_strdup(filter);
207 l->filtered = filtered;
209 l->next = filter_list;
217 sharkd_session_process_info_conv_cb(const void* key, void* value, void* userdata)
219 struct register_ct *table = (struct register_ct *) value;
220 int *pi = (int *) userdata;
222 const char *label = (const char*)key;
224 if (get_conversation_packet_func(table))
226 printf("%s{", (*pi) ? "," : "");
227 printf("\"name\":\"Conversation List/%s\"", label);
228 printf(",\"tap\":\"conv:%s\"", label);
234 if (get_hostlist_packet_func(table))
236 printf("%s{", (*pi) ? "," : "");
237 printf("\"name\":\"Endpoint/%s\"", label);
238 printf(",\"tap\":\"endpt:%s\"", label);
247 * sharkd_session_process_info()
249 * Process info request
251 * Output object with attributes:
252 * (m) columns - available column formats, array of object with attributes:
253 * 'name' - column name
254 * 'format' - column format-name
256 * (m) stats - available statistics, array of object with attributes:
257 * 'name' - statistic name
258 * 'tap' - sharkd tap-name for statistic
260 * (m) convs - available conversation list, array of object with attributes:
261 * 'name' - conversation name
262 * 'tap' - sharkd tap-name for conversation
264 * (m) taps - available taps, array of object with attributes:
266 * 'tap' - sharkd tap-name
268 * (m) ftypes - conversation table for FT_ number to string
271 sharkd_session_process_info(void)
275 printf("{\"columns\":[");
276 for (i = 0; i < NUM_COL_FMTS; i++)
278 const char *col_format = col_format_to_string(i);
279 const char *col_descr = col_format_desc(i);
281 printf("%s{", (i) ? "," : "");
282 printf("\"name\":\"%s\"", col_descr);
283 printf(",\"format\":\"%s\"", col_format);
288 printf(",\"stats\":[");
290 GList *cfg_list = stats_tree_get_cfg_list();
292 const char *sepa = "";
294 for (l = cfg_list; l; l = l->next)
296 stats_tree_cfg *cfg = (stats_tree_cfg *) l->data;
299 printf("\"name\":\"%s\"", cfg->name);
300 printf(",\"tap\":\"stat:%s\"", cfg->abbr);
305 g_list_free(cfg_list);
309 printf(",\"ftypes\":[");
310 for (i = 0; i < FT_NUM_TYPES; i++)
314 json_puts_string(ftype_name((ftenum_t) i));
318 printf(",\"version\":");
319 json_puts_string(sharkd_version());
321 printf(",\"convs\":[");
323 conversation_table_iterate_tables(sharkd_session_process_info_conv_cb, &i);
326 printf(",\"taps\":[");
328 printf("{\"name\":\"%s\",\"tap\":\"%s\"}", "RTP streams", "rtp-streams");
336 * sharkd_session_process_load()
338 * Process load request
341 * (m) file - file to be loaded
343 * Output object with attributes:
344 * (m) err - error code
347 sharkd_session_process_load(const char *buf, const jsmntok_t *tokens, int count)
349 const char *tok_file = json_find_attr(buf, tokens, count, "file");
352 fprintf(stderr, "load: filename=%s\n", tok_file);
357 if (sharkd_cf_open(tok_file, WTAP_TYPE_AUTO, FALSE, &err) != CF_OK)
359 printf("{\"err\":%d}\n", err);
365 err = sharkd_load_cap_file();
367 CATCH(OutOfMemoryError)
369 fprintf(stderr, "load: OutOfMemoryError\n");
374 printf("{\"err\":%d}\n", err);
378 * sharkd_session_process_status()
380 * Process status request
382 * Output object with attributes:
383 * (m) frames - count of currently loaded frames
386 sharkd_session_process_status(void)
388 printf("{\"frames\":%d", cfile.count);
393 struct sharkd_analyse_data
395 GHashTable *protocols_set;
396 nstime_t *first_time;
401 sharkd_session_process_analyse_cb(packet_info *pi, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
403 struct sharkd_analyse_data *analyser = (struct sharkd_analyse_data *) data;
404 frame_data *fdata = pi->fd;
410 if (analyser->first_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->first_time) < 0)
411 analyser->first_time = &fdata->abs_ts;
413 if (analyser->last_time == NULL || nstime_cmp(&fdata->abs_ts, analyser->last_time) > 0)
414 analyser->last_time = &fdata->abs_ts;
418 wmem_list_frame_t *frame;
420 for (frame = wmem_list_head(pi->layers); frame; frame = wmem_list_frame_next(frame))
422 int proto_id = GPOINTER_TO_UINT(wmem_list_frame_data(frame));
424 if (!g_hash_table_lookup_extended(analyser->protocols_set, GUINT_TO_POINTER(proto_id), NULL, NULL))
426 g_hash_table_insert(analyser->protocols_set, GUINT_TO_POINTER(proto_id), GUINT_TO_POINTER(proto_id));
428 if (g_hash_table_size(analyser->protocols_set) != 1)
430 json_puts_string(proto_get_protocol_filter_name(proto_id));
438 * sharkd_session_process_status()
440 * Process analyse request
442 * Output object with attributes:
443 * (m) frames - count of currently loaded frames
444 * (m) protocols - protocol list
445 * (m) first - earliest frame time
446 * (m) last - latest frame time
449 sharkd_session_process_analyse(void)
451 unsigned int framenum;
452 struct sharkd_analyse_data analyser;
454 analyser.first_time = NULL;
455 analyser.last_time = NULL;
456 analyser.protocols_set = g_hash_table_new(NULL /* g_direct_hash() */, NULL /* g_direct_equal */);
458 printf("{\"frames\":%d", cfile.count);
460 printf(",\"protocols\":[");
461 for (framenum = 1; framenum <= cfile.count; framenum++)
462 sharkd_dissect_request(framenum, &sharkd_session_process_analyse_cb, 0, 0, 0, &analyser);
465 if (analyser.first_time)
466 printf(",\"first\":%.9f", nstime_to_sec(analyser.first_time));
468 if (analyser.last_time)
469 printf(",\"last\":%.9f", nstime_to_sec(analyser.last_time));
473 g_hash_table_destroy(analyser.protocols_set);
477 * sharkd_session_process_frames()
479 * Process frames request
482 * (o) filter - filter to be used
483 * (o) range - packet range to be used [TODO]
485 * Output array of frames with attributes:
486 * (m) c - array of column data
487 * (m) num - frame number
488 * (m) i - if frame is ignored
489 * (m) m - if frame is marked
490 * (m) bg - color filter - background color in hex
491 * (m) fg - color filter - foreground color in hex
494 sharkd_session_process_frames(const char *buf, const jsmntok_t *tokens, int count)
496 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
498 const guint8 *filter_data = NULL;
500 const char *frame_sepa = "";
501 unsigned int framenum;
504 column_info *cinfo = &cfile.cinfo;
508 filter_data = sharkd_session_filter_data(tok_filter);
514 for (framenum = 1; framenum <= cfile.count; framenum++)
516 frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum);
518 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
521 sharkd_dissect_columns(framenum, cinfo, (fdata->color_filter == NULL));
523 printf("%s{\"c\":[", frame_sepa);
524 for (col = 0; col < cinfo->num_cols; ++col)
526 const col_item_t *col_item = &cinfo->columns[col];
531 json_puts_string(col_item->col_data);
533 printf("],\"num\":%u", framenum);
535 if (fdata->flags.ignored)
536 printf(",\"i\":true");
538 if (fdata->flags.marked)
539 printf(",\"m\":true");
541 if (fdata->color_filter)
543 printf(",\"bg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->bg_color));
544 printf(",\"fg\":\"%x\"", color_t_to_rgb(&fdata->color_filter->fg_color));
552 if (cinfo != &cfile.cinfo)
557 sharkd_session_process_tap_stats_node_cb(const stat_node *n)
560 const char *sepa = "";
563 for (node = n->children; node; node = node->next)
565 /* code based on stats_tree_get_values_from_node() */
566 printf("%s{\"name\":\"%s\"", sepa, node->name);
567 printf(",\"count\":%u", node->counter);
568 if (node->counter && ((node->st_flags & ST_FLG_AVERAGE) || node->rng))
570 printf(",\"avg\":%.2f", ((float)node->total) / node->counter);
571 printf(",\"min\":%u", node->minvalue);
572 printf(",\"max\":%u", node->maxvalue);
575 if (node->st->elapsed)
576 printf(",\"rate\":%.4f",((float)node->counter) / node->st->elapsed);
578 if (node->parent && node->parent->counter)
579 printf(",\"perc\":%.2f", (node->counter * 100.0) / node->parent->counter);
580 else if (node->parent == &(node->st->root))
581 printf(",\"perc\":100");
583 if (prefs.st_enable_burstinfo && node->max_burst)
585 if (prefs.st_burst_showcount)
586 printf(",\"burstcount\":%d", node->max_burst);
588 printf(",\"burstrate\":%.4f", ((double)node->max_burst) / prefs.st_burst_windowlen);
590 printf(",\"bursttime\":%.3f", ((double)node->burst_time / 1000.0));
596 sharkd_session_process_tap_stats_node_cb(node);
605 * sharkd_session_process_tap_stats_cb()
610 * (m) type:stats - tap output type
611 * (m) name - stat name
612 * (m) stats - array of object with attributes:
613 * (m) name - stat item name
614 * (m) count - stat item counter
615 * (o) avg - stat item averange value
616 * (o) min - stat item min value
617 * (o) max - stat item max value
618 * (o) rate - stat item rate value (ms)
619 * (o) perc - stat item percentage
620 * (o) burstrate - stat item burst rate
621 * (o) burstcount - stat item burst count
622 * (o) burstttme - stat item burst start
623 * (o) sub - array of object with attributes like in stats node.
626 sharkd_session_process_tap_stats_cb(void *psp)
628 stats_tree *st = (stats_tree *)psp;
630 printf("{\"tap\":\"stats:%s\",\"type\":\"stats\"", st->cfg->abbr);
632 printf(",\"name\":\"%s\",\"stats\":", st->cfg->name);
633 sharkd_session_process_tap_stats_node_cb(&st->root);
637 struct sharkd_conv_tap_data
641 gboolean resolve_name;
642 gboolean resolve_port;
646 sharkd_session_geoip_addr(address *addr, const char *suffix)
654 if (addr->type == AT_IPv4)
656 guint32 ip = pntoh32(addr->data);
658 guint num_dbs = geoip_db_num_dbs();
661 for (dbnum = 0; dbnum < num_dbs; dbnum++)
663 const char *geoip_key = NULL;
666 int db_type = geoip_db_type(dbnum);
670 case GEOIP_COUNTRY_EDITION:
671 geoip_key = "geoip_country";
674 case GEOIP_CITY_EDITION_REV0:
675 case GEOIP_CITY_EDITION_REV1:
676 geoip_key = "geoip_city";
679 case GEOIP_ORG_EDITION:
680 geoip_key = "geoip_org";
683 case GEOIP_ISP_EDITION:
684 geoip_key = "geoip_isp";
687 case GEOIP_ASNUM_EDITION:
688 geoip_key = "geoip_as";
691 case WS_LAT_FAKE_EDITION:
692 geoip_key = "geoip_lat";
695 case WS_LON_FAKE_EDITION:
696 geoip_key = "geoip_lon";
700 if (geoip_key && (geoip_val = geoip_db_lookup_ipv4(dbnum, ip, NULL)))
702 printf(",\"%s%s\":", geoip_key, suffix);
703 json_puts_string(geoip_val);
709 if (addr->type == AT_IPv6)
711 const struct e_in6_addr *ip6 = (const struct e_in6_addr *) addr->data;
713 guint num_dbs = geoip_db_num_dbs();
716 for (dbnum = 0; dbnum < num_dbs; dbnum++)
718 const char *geoip_key = NULL;
721 int db_type = geoip_db_type(dbnum);
725 case GEOIP_COUNTRY_EDITION_V6:
726 geoip_key = "geoip_country";
728 #if NUM_DB_TYPES > 31
729 case GEOIP_CITY_EDITION_REV0_V6:
730 case GEOIP_CITY_EDITION_REV1_V6:
731 geoip_key = "geoip_city";
734 case GEOIP_ORG_EDITION_V6:
735 geoip_key = "geoip_org";
738 case GEOIP_ISP_EDITION_V6:
739 geoip_key = "geoip_isp";
742 case GEOIP_ASNUM_EDITION_V6:
743 geoip_key = "geoip_as";
745 #endif /* DB_NUM_TYPES */
746 case WS_LAT_FAKE_EDITION:
747 geoip_key = "geoip_lat";
750 case WS_LON_FAKE_EDITION:
751 geoip_key = "geoip_lon";
755 if (geoip_key && (geoip_val = geoip_db_lookup_ipv6(dbnum, *ip6, NULL)))
757 printf(",\"%s%s\":", geoip_key, suffix);
758 json_puts_string(geoip_val);
769 * sharkd_session_process_tap_conv_cb()
773 * (m) type - tap output type
774 * (m) proto - protocol short name
775 * (o) filter - filter string
777 * (o) convs - array of object with attributes:
778 * (m) saddr - source address
779 * (m) daddr - destination address
780 * (o) sport - source port
781 * (o) dport - destination port
782 * (m) txf - TX frame count
784 * (m) rxf - RX frame count
786 * (m) start - (relative) first packet time
787 * (m) stop - (relative) last packet time
789 * (o) hosts - array of object with attributes:
790 * (m) host - host address
791 * (o) port - host port
792 * (m) txf - TX frame count
794 * (m) rxf - RX frame count
798 sharkd_session_process_tap_conv_cb(void *arg)
800 conv_hash_t *hash = (conv_hash_t *) arg;
801 const struct sharkd_conv_tap_data *iu = (struct sharkd_conv_tap_data *) hash->user_data;
808 if (!strncmp(iu->type, "conv:", 5))
810 printf("{\"tap\":\"%s\",\"type\":\"conv\"", iu->type);
811 printf(",\"convs\":[");
812 proto = iu->type + 5;
814 else if (!strncmp(iu->type, "endpt:", 6))
816 printf("{\"tap\":\"%s\",\"type\":\"host\"", iu->type);
817 printf(",\"hosts\":[");
818 proto = iu->type + 6;
822 printf("{\"tap\":\"%s\",\"type\":\"err\"", iu->type);
826 proto_with_port = (!strcmp(proto, "TCP") || !strcmp(proto, "UDP") || !strcmp(proto, "SCTP"));
828 if (iu->hash.conv_array != NULL && !strncmp(iu->type, "conv:", 5))
830 for (i = 0; i < iu->hash.conv_array->len; i++)
832 conv_item_t *iui = &g_array_index(iu->hash.conv_array, conv_item_t, i);
833 char *src_addr, *dst_addr;
834 char *src_port, *dst_port;
837 printf("%s{", i ? "," : "");
839 printf("\"saddr\":\"%s\"", (src_addr = get_conversation_address(NULL, &iui->src_address, iu->resolve_name)));
840 printf(",\"daddr\":\"%s\"", (dst_addr = get_conversation_address(NULL, &iui->dst_address, iu->resolve_name)));
844 printf(",\"sport\":\"%s\"", (src_port = get_conversation_port(NULL, iui->src_port, iui->ptype, iu->resolve_port)));
845 printf(",\"dport\":\"%s\"", (dst_port = get_conversation_port(NULL, iui->dst_port, iui->ptype, iu->resolve_port)));
847 wmem_free(NULL, src_port);
848 wmem_free(NULL, dst_port);
851 printf(",\"rxf\":%" G_GUINT64_FORMAT, iui->rx_frames);
852 printf(",\"rxb\":%" G_GUINT64_FORMAT, iui->rx_bytes);
854 printf(",\"txf\":%" G_GUINT64_FORMAT, iui->tx_frames);
855 printf(",\"txb\":%" G_GUINT64_FORMAT, iui->tx_bytes);
857 printf(",\"start\":%.9f", nstime_to_sec(&iui->start_time));
858 printf(",\"stop\":%.9f", nstime_to_sec(&iui->stop_time));
860 filter_str = get_conversation_filter(iui, CONV_DIR_A_TO_FROM_B);
863 printf(",\"filter\":\"%s\"", filter_str);
867 wmem_free(NULL, src_addr);
868 wmem_free(NULL, dst_addr);
870 if (sharkd_session_geoip_addr(&(iui->src_address), "1"))
872 if (sharkd_session_geoip_addr(&(iui->dst_address), "2"))
878 else if (iu->hash.conv_array != NULL && !strncmp(iu->type, "endpt:", 6))
880 for (i = 0; i < iu->hash.conv_array->len; i++)
882 hostlist_talker_t *host = &g_array_index(iu->hash.conv_array, hostlist_talker_t, i);
883 char *host_str, *port_str;
886 printf("%s{", i ? "," : "");
888 printf("\"host\":\"%s\"", (host_str = get_conversation_address(NULL, &host->myaddress, iu->resolve_name)));
892 printf(",\"port\":\"%s\"", (port_str = get_conversation_port(NULL, host->port, host->ptype, iu->resolve_port)));
894 wmem_free(NULL, port_str);
897 printf(",\"rxf\":%" G_GUINT64_FORMAT, host->rx_frames);
898 printf(",\"rxb\":%" G_GUINT64_FORMAT, host->rx_bytes);
900 printf(",\"txf\":%" G_GUINT64_FORMAT, host->tx_frames);
901 printf(",\"txb\":%" G_GUINT64_FORMAT, host->tx_bytes);
903 filter_str = get_hostlist_filter(host);
906 printf(",\"filter\":\"%s\"", filter_str);
910 wmem_free(NULL, host_str);
912 if (sharkd_session_geoip_addr(&(host->myaddress), ""))
918 printf("],\"proto\":\"%s\",\"geoip\":%s},", proto, with_geoip ? "true" : "false");
922 * sharkd_session_process_tap_rtp_cb()
924 * Output RTP streams tap:
926 * (m) type - tap output type
927 * (m) streams - array of object with attributes:
928 * (m) ssrc - RTP synchronization source identifier
929 * (m) payload - stream payload
930 * (m) saddr - source address
931 * (m) sport - source port
932 * (m) daddr - destination address
933 * (m) dport - destination port
934 * (m) pkts - packets count
935 * (m) max_delta - max delta (ms)
936 * (m) max_jitter - max jitter (ms)
937 * (m) mean_jitter - mean jitter (ms)
940 * (m) problem - if analyser found the problem
941 * (m) ipver - address IP version (4 or 6)
944 sharkd_session_process_tap_rtp_cb(void *arg)
946 rtpstream_tapinfo_t *rtp_tapinfo = (rtpstream_tapinfo_t *) arg;
949 const char *sepa = "";
951 printf("{\"tap\":\"%s\",\"type\":\"%s\"", "rtp-streams", "rtp-streams");
953 printf(",\"streams\":[");
954 for (listx = g_list_first(rtp_tapinfo->strinfo_list); listx; listx = listx->next)
956 rtp_stream_info_t *streaminfo = (rtp_stream_info_t *) listx->data;
958 char *src_addr, *dst_addr;
962 src_addr = address_to_display(NULL, &(streaminfo->src_addr));
963 dst_addr = address_to_display(NULL, &(streaminfo->dest_addr));
965 if (streaminfo->payload_type_name != NULL)
966 payload = wmem_strdup(NULL, streaminfo->payload_type_name);
968 payload = val_to_str_ext_wmem(NULL, streaminfo->payload_type, &rtp_payload_type_short_vals_ext, "Unknown (%u)");
970 printf("%s{\"ssrc\":%u", sepa, streaminfo->ssrc);
971 printf(",\"payload\":\"%s\"", payload);
973 printf(",\"saddr\":\"%s\"", src_addr);
974 printf(",\"sport\":%u", streaminfo->src_port);
976 printf(",\"daddr\":\"%s\"", dst_addr);
977 printf(",\"dport\":%u", streaminfo->dest_port);
979 printf(",\"pkts\":%u", streaminfo->packet_count);
981 printf(",\"max_delta\":%f", streaminfo->rtp_stats.max_delta);
982 printf(",\"max_jitter\":%f", streaminfo->rtp_stats.max_jitter);
983 printf(",\"mean_jitter\":%f", streaminfo->rtp_stats.mean_jitter);
985 expected = (streaminfo->rtp_stats.stop_seq_nr + streaminfo->rtp_stats.cycles * 65536) - streaminfo->rtp_stats.start_seq_nr + 1;
986 printf(",\"expectednr\":%u", expected);
987 printf(",\"totalnr\":%u", streaminfo->rtp_stats.total_nr);
989 printf(",\"problem\":%s", streaminfo->problem ? "true" : "false");
992 printf(",\"ipver\":%d", (streaminfo->src_addr.type == AT_IPv6) ? 6 : 4);
994 wmem_free(NULL, src_addr);
995 wmem_free(NULL, dst_addr);
996 wmem_free(NULL, payload);
1005 * sharkd_session_process_tap()
1007 * Process tap request
1010 * (m) tap0 - First tap request
1011 * (o) tap1...tap15 - Other tap requests
1013 * Output object with attributes:
1014 * (m) taps - array of object with attributes:
1015 * (m) tap - tap name
1016 * (m) type - tap output type
1018 * for type:stats see sharkd_session_process_tap_stats_cb()
1019 * for type:conv see sharkd_session_process_tap_conv_cb()
1020 * for type:host see sharkd_session_process_tap_conv_cb()
1021 * for type:rtp-streams see sharkd_session_process_tap_rtp_cb()
1023 * (m) err - error code
1026 sharkd_session_process_tap(char *buf, const jsmntok_t *tokens, int count)
1028 void *taps_data[16];
1032 rtpstream_tapinfo_t rtp_tapinfo =
1033 {NULL, NULL, NULL, NULL, 0, NULL, 0, TAP_ANALYSE, NULL, NULL, NULL, FALSE};
1035 for (i = 0; i < 16; i++)
1038 const char *tok_tap;
1040 tap_packet_cb tap_func = NULL;
1041 void *tap_data = NULL;
1042 const char *tap_filter = "";
1043 GString *tap_error = NULL;
1045 taps_data[i] = NULL;
1047 ws_snprintf(tapbuf, sizeof(tapbuf), "tap%d", i);
1048 tok_tap = json_find_attr(buf, tokens, count, tapbuf);
1052 if (!strncmp(tok_tap, "stat:", 5))
1054 stats_tree_cfg *cfg = stats_tree_get_cfg_by_abbr(tok_tap + 5);
1059 fprintf(stderr, "sharkd_session_process_tap() stat %s not found\n", tok_tap + 5);
1063 st = stats_tree_new(cfg, NULL, tap_filter);
1065 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);
1069 if (!tap_error && cfg->init)
1072 else if (!strncmp(tok_tap, "conv:", 5) || !strncmp(tok_tap, "endpt:", 6))
1074 struct register_ct *ct = NULL;
1075 const char *ct_tapname;
1076 struct sharkd_conv_tap_data *ct_data;
1078 if (!strncmp(tok_tap, "conv:", 5))
1080 ct = _get_conversation_table_by_name(tok_tap + 5);
1082 if (!ct || !(tap_func = get_conversation_packet_func(ct)))
1084 fprintf(stderr, "sharkd_session_process_tap() conv %s not found\n", tok_tap + 5);
1088 else if (!strncmp(tok_tap, "endpt:", 6))
1090 ct = _get_conversation_table_by_name(tok_tap + 6);
1092 if (!ct || !(tap_func = get_hostlist_packet_func(ct)))
1094 fprintf(stderr, "sharkd_session_process_tap() endpt %s not found\n", tok_tap + 5);
1100 fprintf(stderr, "sharkd_session_process_tap() conv/endpt(?): %s not found\n", tok_tap);
1104 ct_tapname = proto_get_protocol_filter_name(get_conversation_proto_id(ct));
1106 ct_data = (struct sharkd_conv_tap_data *) g_malloc0(sizeof(struct sharkd_conv_tap_data));
1107 ct_data->type = tok_tap;
1108 ct_data->hash.user_data = ct_data;
1110 /* XXX: make configurable */
1111 ct_data->resolve_name = TRUE;
1112 ct_data->resolve_port = TRUE;
1114 tap_error = register_tap_listener(ct_tapname, &ct_data->hash, tap_filter, 0, NULL, tap_func, sharkd_session_process_tap_conv_cb);
1116 tap_data = &ct_data->hash;
1118 else if (!strcmp(tok_tap, "rtp-streams"))
1120 tap_error = register_tap_listener("rtp", &rtp_tapinfo, tap_filter, 0, rtpstream_reset_cb, rtpstream_packet, sharkd_session_process_tap_rtp_cb);
1122 tap_data = &rtp_tapinfo;
1126 fprintf(stderr, "sharkd_session_process_tap() %s not recognized\n", tok_tap);
1132 /* XXX, tap data memleaks */
1133 fprintf(stderr, "sharkd_session_process_tap() name=%s error=%s", tok_tap, tap_error->str);
1134 g_string_free(tap_error, TRUE);
1138 taps_data[i] = tap_data;
1142 fprintf(stderr, "sharkd_session_process_tap() count=%d\n", taps_count);
1143 if (taps_count == 0)
1146 printf("{\"taps\":[");
1148 printf("null],\"err\":0}\n");
1150 for (i = 0; i < 16; i++)
1153 remove_tap_listener(taps_data[i]);
1155 /* XXX, taps data memleaks */
1160 sharkd_session_process_frame_cb_tree(proto_tree *tree, tvbuff_t **tvbs)
1163 const char *sepa = "";
1166 for (node = tree->first_child; node; node = node->next)
1168 field_info *finfo = PNODE_FINFO(node);
1173 /* XXX, for now always skip hidden */
1174 if (FI_GET_FLAG(finfo, FI_HIDDEN))
1177 printf("%s{", sepa);
1182 char label_str[ITEM_LABEL_LENGTH];
1184 label_str[0] = '\0';
1185 proto_item_fill_label(finfo, label_str);
1186 json_puts_string(label_str);
1190 json_puts_string(finfo->rep->representation);
1193 if (finfo->ds_tvb && tvbs && tvbs[0] != finfo->ds_tvb)
1197 for (idx = 1; tvbs[idx]; idx++)
1199 if (tvbs[idx] == finfo->ds_tvb)
1201 printf(",\"ds\":%d", idx);
1207 if (finfo->start >= 0 && finfo->length > 0)
1208 printf(",\"h\":[%u,%u]", finfo->start, finfo->length);
1210 if (finfo->appendix_start >= 0 && finfo->appendix_length > 0)
1211 printf(",\"i\":[%u,%u]", finfo->appendix_start, finfo->appendix_length);
1213 if (finfo->hfinfo && finfo->hfinfo->type == FT_PROTOCOL)
1214 printf(",\"t\":\"proto\"");
1216 if (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
1218 const char *severity = NULL;
1220 switch (FI_GET_FLAG(finfo, PI_SEVERITY_MASK))
1223 severity = "comment";
1242 g_assert(severity != NULL);
1244 printf(",\"s\":\"%s\"", severity);
1247 if (((proto_tree *) node)->first_child) {
1248 if (finfo->tree_type != -1)
1249 printf(",\"e\":%d", finfo->tree_type);
1251 sharkd_session_process_frame_cb_tree((proto_tree *) node, tvbs);
1261 sharkd_session_process_frame_cb(packet_info *pi, proto_tree *tree, struct epan_column_info *cinfo, const GSList *data_src, void *data)
1268 printf("\"err\":0");
1272 tvbuff_t **tvbs = NULL;
1274 printf(",\"tree\":");
1276 /* arrayize data src, to speedup searching for ds_tvb index */
1277 if (data_src && data_src->next /* only needed if there are more than one data source */)
1279 guint count = g_slist_length((GSList *) data_src);
1282 tvbs = (tvbuff_t **) g_malloc((count + 1) * sizeof(*tvbs));
1284 for (i = 0; i < count; i++)
1286 struct data_source *src = (struct data_source *) g_slist_nth_data((GSList *) data_src, i);
1288 tvbs[i] = get_data_source_tvb(src);
1294 sharkd_session_process_frame_cb_tree(tree, tvbs);
1303 printf(",\"col\":[");
1304 for (col = 0; col < cinfo->num_cols; ++col)
1306 const col_item_t *col_item = &cinfo->columns[col];
1308 printf("%s\"%s\"", (col) ? "," : "", col_item->col_data);
1315 struct data_source *src = (struct data_source *)data_src->data;
1316 const char *ds_sepa = NULL;
1321 tvb = get_data_source_tvb(src);
1322 length = tvb_captured_length(tvb);
1324 printf(",\"bytes\":");
1327 const guchar *cp = tvb_get_ptr(tvb, 0, length);
1329 /* XXX pi.fd->flags.encoding */
1330 json_print_base64(cp, length);
1334 json_print_base64("", 0);
1337 data_src = data_src->next;
1340 printf(",\"ds\":[");
1346 src = (struct data_source *)data_src->data;
1349 char *src_name = get_data_source_name(src);
1351 printf("%s{\"name\":", ds_sepa);
1352 json_puts_string(src_name);
1353 wmem_free(NULL, src_name);
1356 tvb = get_data_source_tvb(src);
1357 length = tvb_captured_length(tvb);
1359 printf(",\"bytes\":");
1362 const guchar *cp = tvb_get_ptr(tvb, 0, length);
1364 /* XXX pi.fd->flags.encoding */
1365 json_print_base64(cp, length);
1369 json_print_base64("", 0);
1375 data_src = data_src->next;
1378 /* close ds, only if was opened */
1379 if (ds_sepa != NULL)
1387 * sharkd_session_process_intervals()
1389 * Process intervals request - generate basic capture file statistics per requested interval.
1392 * (o) interval - interval time in ms, if not specified: 1000ms
1393 * (o) filter - filter for generating interval request
1395 * Output object with attributes:
1396 * (m) intervals - array of intervals, with indexes:
1397 * [0] - index of interval,
1398 * [1] - number of frames during interval,
1399 * [2] - number of bytes during interval.
1401 * (m) last - last interval number.
1402 * (m) frames - total number of frames
1403 * (m) bytes - total number of bytes
1405 * NOTE: If frames are not in order, there might be items with same interval index, or even negative one.
1408 sharkd_session_process_intervals(char *buf, const jsmntok_t *tokens, int count)
1410 const char *tok_interval = json_find_attr(buf, tokens, count, "interval");
1411 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
1413 const guint8 *filter_data = NULL;
1417 unsigned int frames;
1421 nstime_t *start_ts = NULL;
1423 guint32 interval_ms = 1000; /* default: one per second */
1425 const char *sepa = "";
1426 unsigned int framenum;
1431 if (!ws_strtou32(tok_interval, NULL, &interval_ms) || interval_ms == 0) {
1432 fprintf(stderr, "Invalid interval parameter: %s.\n", tok_interval);
1439 filter_data = sharkd_session_filter_data(tok_filter);
1444 st_total.frames = 0;
1452 printf("{\"intervals\":[");
1454 for (framenum = 1; framenum <= cfile.count; framenum++)
1456 frame_data *fdata = frame_data_sequence_find(cfile.frames, framenum);
1460 if (start_ts == NULL)
1461 start_ts = &fdata->abs_ts;
1463 if (filter_data && !(filter_data[framenum / 8] & (1 << (framenum % 8))))
1466 msec_rel = (fdata->abs_ts.secs - start_ts->secs) * 1000 + (fdata->abs_ts.nsecs - start_ts->nsecs) / 1000000;
1467 new_idx = msec_rel / interval_ms;
1473 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
1486 st.bytes += fdata->pkt_len;
1488 st_total.frames += 1;
1489 st_total.bytes += fdata->pkt_len;
1494 printf("%s[%" G_GINT64_FORMAT ",%u,%" G_GUINT64_FORMAT "]", sepa, idx, st.frames, st.bytes);
1498 printf("],\"last\":%" G_GINT64_FORMAT ",\"frames\":%u,\"bytes\":%" G_GUINT64_FORMAT "}\n", max_idx, st_total.frames, st_total.bytes);
1502 * sharkd_session_process_frame()
1504 * Process frame request
1507 * (m) frame - requested frame number
1508 * (o) proto - set if output frame tree
1509 * (o) columns - set if output frame columns
1510 * (o) bytes - set if output frame bytes
1512 * Output object with attributes:
1513 * (m) err - 0 if succeed
1514 * (o) tree - array of frame nodes with attributes:
1518 * e - subtree ett index
1519 * n - array of subtree nodes
1520 * h - two item array: (item start, item length)
1521 * i - two item array: (appendix start, appendix length)
1522 * p - [RESERVED] two item array: (protocol start, protocol length)
1523 * ds- data src index
1525 * (o) col - array of column data
1526 * (o) bytes - base64 of frame bytes
1527 * (o) ds - array of other data srcs
1530 sharkd_session_process_frame(char *buf, const jsmntok_t *tokens, int count)
1532 const char *tok_frame = json_find_attr(buf, tokens, count, "frame");
1533 int tok_proto = (json_find_attr(buf, tokens, count, "proto") != NULL);
1534 int tok_bytes = (json_find_attr(buf, tokens, count, "bytes") != NULL);
1535 int tok_columns = (json_find_attr(buf, tokens, count, "columns") != NULL);
1539 if (!tok_frame || !ws_strtou32(tok_frame, NULL, &framenum) || framenum == 0)
1542 sharkd_dissect_request(framenum, &sharkd_session_process_frame_cb, tok_bytes, tok_columns, tok_proto, NULL);
1546 * sharkd_session_process_check()
1548 * Process check request.
1551 * (o) filter - filter to be checked
1553 * Output object with attributes:
1554 * (m) err - always 0
1555 * (o) filter - 'ok', 'warn' or error message
1558 sharkd_session_process_check(char *buf, const jsmntok_t *tokens, int count)
1560 const char *tok_filter = json_find_attr(buf, tokens, count, "filter");
1562 printf("{\"err\":0");
1563 if (tok_filter != NULL)
1565 char *err_msg = NULL;
1568 if (dfilter_compile(tok_filter, &dfp, &err_msg))
1570 const char *s = "ok";
1572 if (dfilter_deprecated_tokens(dfp))
1575 printf(",\"filter\":\"%s\"", s);
1580 printf(",\"filter\":");
1581 json_puts_string(err_msg);
1590 struct sharkd_session_process_complete_pref_data
1598 sharkd_session_process_complete_pref_cb(module_t *module, gpointer d)
1600 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
1602 if (strncmp(data->pref, module->name, strlen(data->pref)) != 0)
1605 printf("%s{\"f\":\"%s\",\"d\":\"%s\"}", data->sepa, module->name, module->title);
1612 sharkd_session_process_complete_pref_option_cb(pref_t *pref, gpointer d)
1614 struct sharkd_session_process_complete_pref_data *data = (struct sharkd_session_process_complete_pref_data *) d;
1615 const char *pref_name = prefs_get_name(pref);
1616 const char *pref_title = prefs_get_title(pref);
1618 if (strncmp(data->pref, pref_name, strlen(data->pref)) != 0)
1621 printf("%s{\"f\":\"%s.%s\",\"d\":\"%s\"}", data->sepa, data->module, pref_name, pref_title);
1624 return 0; /* continue */
1628 * sharkd_session_process_complete()
1630 * Process complete request
1633 * (o) field - field to be completed
1634 * (o) pref - preference to be completed
1636 * Output object with attributes:
1637 * (m) err - always 0
1638 * (o) field - array of object with attributes:
1639 * (m) f - field text
1640 * (o) t - field type (FT_ number)
1641 * (o) n - field name
1642 * (o) pref - array of object with attributes:
1644 * (o) d - pref description
1647 sharkd_session_process_complete(char *buf, const jsmntok_t *tokens, int count)
1649 const char *tok_field = json_find_attr(buf, tokens, count, "field");
1650 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
1652 printf("{\"err\":0");
1653 if (tok_field != NULL && tok_field[0])
1655 const size_t filter_length = strlen(tok_field);
1656 const int filter_with_dot = !!strchr(tok_field, '.');
1661 const char *sepa = "";
1663 printf(",\"field\":[");
1665 for (proto_id = proto_get_first_protocol(&proto_cookie); proto_id != -1; proto_id = proto_get_next_protocol(&proto_cookie))
1667 protocol_t *protocol = find_protocol_by_id(proto_id);
1668 const char *protocol_filter;
1669 const char *protocol_name;
1670 header_field_info *hfinfo;
1672 if (!proto_is_protocol_enabled(protocol))
1675 protocol_name = proto_get_protocol_long_name(protocol);
1676 protocol_filter = proto_get_protocol_filter_name(proto_id);
1678 if (strlen(protocol_filter) >= filter_length && !g_ascii_strncasecmp(tok_field, protocol_filter, filter_length))
1680 printf("%s{", sepa);
1683 json_puts_string(protocol_filter);
1684 printf(",\"t\":%d", FT_PROTOCOL);
1686 json_puts_string(protocol_name);
1692 if (!filter_with_dot)
1695 for (hfinfo = proto_get_first_protocol_field(proto_id, &field_cookie); hfinfo != NULL; hfinfo = proto_get_next_protocol_field(proto_id, &field_cookie))
1697 if (hfinfo->same_name_prev_id != -1) /* ignore duplicate names */
1700 if (strlen(hfinfo->abbrev) >= filter_length && !g_ascii_strncasecmp(tok_field, hfinfo->abbrev, filter_length))
1702 printf("%s{", sepa);
1705 json_puts_string(hfinfo->abbrev);
1707 /* XXX, skip displaying name, if there are multiple (to not confuse user) */
1708 if (hfinfo->same_name_next == NULL)
1710 printf(",\"t\":%d", hfinfo->type);
1712 json_puts_string(hfinfo->name);
1724 if (tok_pref != NULL && tok_pref[0])
1726 struct sharkd_session_process_complete_pref_data data;
1729 data.module = tok_pref;
1730 data.pref = tok_pref;
1733 printf(",\"pref\":[");
1735 if ((dot_sepa = strchr(tok_pref, '.')))
1739 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
1740 data.pref = dot_sepa + 1;
1742 pref_mod = prefs_find_module(data.module);
1744 prefs_pref_foreach(pref_mod, sharkd_session_process_complete_pref_option_cb, &data);
1750 prefs_modules_foreach(sharkd_session_process_complete_pref_cb, &data);
1762 * sharkd_session_process_setconf()
1764 * Process setconf request
1767 * (m) name - preference name
1768 * (m) value - preference value
1770 * Output object with attributes:
1771 * (m) err - error code: 0 succeed
1774 sharkd_session_process_setconf(char *buf, const jsmntok_t *tokens, int count)
1776 const char *tok_name = json_find_attr(buf, tokens, count, "name");
1777 const char *tok_value = json_find_attr(buf, tokens, count, "value");
1780 prefs_set_pref_e ret;
1782 if (!tok_name || tok_name[0] == '\0' || !tok_value)
1785 ws_snprintf(pref, sizeof(pref), "%s:%s", tok_name, tok_value);
1787 ret = prefs_set_pref(pref);
1788 printf("{\"err\":%d}\n", ret);
1791 struct sharkd_session_process_dumpconf_data
1798 sharkd_session_process_dumpconf_cb(pref_t *pref, gpointer d)
1800 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
1801 const char *pref_name = prefs_get_name(pref);
1803 printf("%s\"%s.%s\":{", data->sepa, data->module->name, pref_name);
1805 switch (prefs_get_type(pref))
1808 case PREF_DECODE_AS_UINT:
1809 printf("\"u\":%u", prefs_get_uint_value_real(pref, pref_current));
1810 if (prefs_get_uint_base(pref) != 10)
1811 printf(",\"ub\":%d", prefs_get_uint_base(pref));
1815 printf("\"b\":%s", prefs_get_bool_value(pref, pref_current) ? "1" : "0");
1820 json_puts_string(prefs_get_string_value(pref, pref_current));
1825 const enum_val_t *enums;
1826 const char *enum_sepa = "";
1829 for (enums = prefs_get_enumvals(pref); enums->name; enums++)
1831 printf("%s{\"v\":%d", enum_sepa, enums->value);
1833 if (enums->value == prefs_get_enum_value(pref, pref_current))
1837 json_puts_string(enums->description);
1847 case PREF_DECODE_AS_RANGE:
1849 char *range_str = range_convert_range(NULL, prefs_get_range_value_real(pref, pref_current));
1850 printf("\"r\":\"%s\"", range_str);
1851 wmem_free(NULL, range_str);
1858 case PREF_STATIC_TEXT:
1866 json_puts_string(prefs_get_title(pref));
1872 return 0; /* continue */
1876 sharkd_session_process_dumpconf_mod_cb(module_t *module, gpointer d)
1878 struct sharkd_session_process_dumpconf_data *data = (struct sharkd_session_process_dumpconf_data *) d;
1880 data->module = module;
1881 prefs_pref_foreach(module, sharkd_session_process_dumpconf_cb, data);
1887 * sharkd_session_process_dumpconf()
1889 * Process dumpconf request
1892 * (o) pref - module, or preference, NULL for all
1894 * Output object with attributes:
1895 * (o) prefs - object with module preferences
1896 * (m) [KEY] - preference name
1897 * (o) u - preference value (only for PREF_UINT)
1898 * (o) ub - preference value suggested base for display (only for PREF_UINT) and if different than 10
1899 * (o) b - preference value (only for PREF_BOOL) (1 true, 0 false)
1900 * (o) s - preference value (only for PREF_STRING)
1901 * (o) e - preference possible values (only for PREF_ENUM)
1902 * (o) r - preference value (only for PREF_RANGE)
1903 * (o) t - preference value (only for PREF_UAT)
1906 sharkd_session_process_dumpconf(char *buf, const jsmntok_t *tokens, int count)
1908 const char *tok_pref = json_find_attr(buf, tokens, count, "pref");
1914 struct sharkd_session_process_dumpconf_data data;
1919 printf("{\"prefs\":{");
1920 prefs_modules_foreach(sharkd_session_process_dumpconf_mod_cb, &data);
1925 if ((dot_sepa = strchr(tok_pref, '.')))
1927 pref_t *pref = NULL;
1929 *dot_sepa = '\0'; /* XXX, C abuse: discarding-const */
1930 pref_mod = prefs_find_module(tok_pref);
1932 pref = prefs_find_preference(pref_mod, dot_sepa + 1);
1937 struct sharkd_session_process_dumpconf_data data;
1939 data.module = pref_mod;
1942 printf("{\"prefs\":{");
1943 sharkd_session_process_dumpconf_cb(pref, &data);
1950 pref_mod = prefs_find_module(tok_pref);
1953 struct sharkd_session_process_dumpconf_data data;
1955 data.module = pref_mod;
1958 printf("{\"prefs\":{");
1959 prefs_pref_foreach(pref_mod, sharkd_session_process_dumpconf_cb, &data);
1965 sharkd_session_process(char *buf, const jsmntok_t *tokens, int count)
1969 /* sanity check, and split strings */
1970 if (count < 1 || tokens[0].type != JSMN_OBJECT)
1972 fprintf(stderr, "sanity check(1): [0] not object\n");
1976 /* don't need [0] token */
1982 fprintf(stderr, "sanity check(2): %d not even\n", count);
1986 for (i = 0; i < count; i += 2)
1988 if (tokens[i].type != JSMN_STRING)
1990 fprintf(stderr, "sanity check(3): [%d] not string\n", i);
1994 buf[tokens[i + 0].end] = '\0';
1995 buf[tokens[i + 1].end] = '\0';
1997 json_unescape_str(&buf[tokens[i + 0].start]);
1998 json_unescape_str(&buf[tokens[i + 1].start]);
2002 const char *tok_req = json_find_attr(buf, tokens, count, "req");
2006 fprintf(stderr, "sanity check(4): no \"req\".\n");
2010 if (!strcmp(tok_req, "load"))
2011 sharkd_session_process_load(buf, tokens, count);
2012 else if (!strcmp(tok_req, "status"))
2013 sharkd_session_process_status();
2014 else if (!strcmp(tok_req, "analyse"))
2015 sharkd_session_process_analyse();
2016 else if (!strcmp(tok_req, "info"))
2017 sharkd_session_process_info();
2018 else if (!strcmp(tok_req, "check"))
2019 sharkd_session_process_check(buf, tokens, count);
2020 else if (!strcmp(tok_req, "complete"))
2021 sharkd_session_process_complete(buf, tokens, count);
2022 else if (!strcmp(tok_req, "frames"))
2023 sharkd_session_process_frames(buf, tokens, count);
2024 else if (!strcmp(tok_req, "tap"))
2025 sharkd_session_process_tap(buf, tokens, count);
2026 else if (!strcmp(tok_req, "intervals"))
2027 sharkd_session_process_intervals(buf, tokens, count);
2028 else if (!strcmp(tok_req, "frame"))
2029 sharkd_session_process_frame(buf, tokens, count);
2030 else if (!strcmp(tok_req, "setconf"))
2031 sharkd_session_process_setconf(buf, tokens, count);
2032 else if (!strcmp(tok_req, "dumpconf"))
2033 sharkd_session_process_dumpconf(buf, tokens, count);
2034 else if (!strcmp(tok_req, "bye"))
2037 fprintf(stderr, "::: req = %s\n", tok_req);
2039 /* reply for every command are 0+ lines of JSON reply (outputed above), finished by empty new line */
2043 * We do an explicit fflush after every line, because
2044 * we want output to be written to the socket as soon
2045 * as the line is complete.
2047 * The stream is fully-buffered by default, so it's
2048 * only flushed when the buffer fills or the FILE *
2049 * is closed. On UN*X, we could set it to be line
2050 * buffered, but the MSVC standard I/O routines don't
2051 * support line buffering - they only support *byte*
2052 * buffering, doing a write for every byte written,
2053 * which is too inefficient, and full buffering,
2054 * which is what you get if you request line buffering.
2061 sharkd_session_main(void)
2063 char buf[16 * 1024];
2064 jsmntok_t *tokens = NULL;
2065 int tokens_max = -1;
2067 fprintf(stderr, "Hello in child.\n");
2069 while (fgets(buf, sizeof(buf), stdin))
2071 /* every command is line seperated JSON */
2074 ret = wsjsmn_parse(buf, NULL, 0);
2077 fprintf(stderr, "invalid JSON -> closing\n");
2081 /* fprintf(stderr, "JSON: %d tokens\n", ret); */
2084 if (tokens == NULL || tokens_max < ret)
2087 tokens = (jsmntok_t *) g_realloc(tokens, sizeof(jsmntok_t) * tokens_max);
2090 memset(tokens, 0, ret * sizeof(jsmntok_t));
2092 ret = wsjsmn_parse(buf, tokens, ret);
2095 fprintf(stderr, "invalid JSON(2) -> closing\n");
2099 sharkd_session_process(buf, tokens, ret);
2108 * Editor modelines - http://www.wireshark.org/tools/modelines.html
2113 * indent-tabs-mode: t
2116 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
2117 * :indentSize=8:tabSize=8:noTabs=false: