2 * Routines for capture file summary info
4 * Wireshark - Network traffic analyzer
5 * By Gerald Combs <gerald@wireshark.org>
6 * Copyright 1998 Gerald Combs
8 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include <wiretap/pcap-encap.h>
14 #include <wiretap/wtap_opttypes.h>
15 #include <wiretap/pcapng.h>
17 #include <epan/packet.h>
18 #include <wsutil/file_util.h>
19 #include <wsutil/wsgcrypt.h>
21 #include "ui/summary.h"
23 // Strongest to weakest
24 #define HASH_SIZE_SHA256 32
25 #define HASH_SIZE_RMD160 20
26 #define HASH_SIZE_SHA1 20
28 #define HASH_BUF_SIZE (1024 * 1024)
31 tally_frame_data(frame_data *cur_frame, summary_tally *sum_tally)
35 sum_tally->bytes += cur_frame->pkt_len;
36 if (cur_frame->passed_dfilter){
37 sum_tally->filtered_count++;
38 sum_tally->filtered_bytes += cur_frame->pkt_len;
40 if (cur_frame->marked){
41 sum_tally->marked_count++;
42 sum_tally->marked_bytes += cur_frame->pkt_len;
44 if (cur_frame->ignored){
45 sum_tally->ignored_count++;
48 if (cur_frame->has_ts) {
49 /* This packet has a time stamp. */
50 cur_time = nstime_to_sec(&cur_frame->abs_ts);
52 sum_tally->packet_count_ts++;
53 if (cur_time < sum_tally->start_time) {
54 sum_tally->start_time = cur_time;
56 if (cur_time > sum_tally->stop_time){
57 sum_tally->stop_time = cur_time;
59 if (cur_frame->passed_dfilter){
60 sum_tally->filtered_count_ts++;
62 * If we've seen one filtered packet, this is the first
65 if (sum_tally->filtered_count == 1){
66 sum_tally->filtered_start= cur_time;
67 sum_tally->filtered_stop = cur_time;
69 if (cur_time < sum_tally->filtered_start) {
70 sum_tally->filtered_start = cur_time;
72 if (cur_time > sum_tally->filtered_stop) {
73 sum_tally->filtered_stop = cur_time;
77 if (cur_frame->marked){
78 sum_tally->marked_count_ts++;
80 * If we've seen one marked packet, this is the first
83 if (sum_tally->marked_count == 1){
84 sum_tally->marked_start= cur_time;
85 sum_tally->marked_stop = cur_time;
87 if (cur_time < sum_tally->marked_start) {
88 sum_tally->marked_start = cur_time;
90 if (cur_time > sum_tally->marked_stop) {
91 sum_tally->marked_stop = cur_time;
99 hash_to_str(const unsigned char *hash, size_t length, char *str) {
102 for (i = 0; i < (int) length; i++) {
103 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
108 summary_fill_in(capture_file *cf, summary_tally *st)
110 frame_data *first_frame, *cur_frame;
112 iface_summary_info iface;
114 wtapng_iface_descriptions_t* idb_info;
115 wtap_block_t wtapng_if_descr;
116 wtapng_if_descr_mandatory_t *wtapng_if_descr_mand;
117 wtap_block_t if_stats;
120 wtapng_if_descr_filter_t* if_filter;
127 st->packet_count_ts = 0;
131 st->filtered_count = 0;
132 st->filtered_count_ts = 0;
133 st->filtered_start = 0;
134 st->filtered_stop = 0;
135 st->filtered_bytes = 0;
136 st->marked_count = 0;
137 st->marked_count_ts = 0;
138 st->marked_start = 0;
140 st->marked_bytes = 0;
141 st->ignored_count = 0;
143 /* initialize the tally */
144 if (cf->count != 0) {
145 first_frame = frame_data_sequence_find(cf->provider.frames, 1);
146 st->start_time = nstime_to_sec(&first_frame->abs_ts);
147 st->stop_time = nstime_to_sec(&first_frame->abs_ts);
149 for (framenum = 1; framenum <= cf->count; framenum++) {
150 cur_frame = frame_data_sequence_find(cf->provider.frames, framenum);
151 tally_frame_data(cur_frame, st);
155 st->filename = cf->filename;
156 st->file_length = cf->f_datalen;
157 st->file_type = cf->cd_t;
158 st->compression_type = cf->compression_type;
159 st->is_tempfile = cf->is_tempfile;
160 st->file_encap_type = cf->lnk_t;
161 st->packet_encap_types = cf->linktypes;
163 st->elapsed_time = nstime_to_sec(&cf->elapsed_time);
164 st->packet_count = cf->count;
165 st->drops_known = cf->drops_known;
166 st->drops = cf->drops;
167 st->dfilter = cf->dfilter;
169 st->ifaces = g_array_new(FALSE, FALSE, sizeof(iface_summary_info));
170 idb_info = wtap_file_get_idb_info(cf->provider.wth);
171 for (i = 0; i < idb_info->interface_data->len; i++) {
172 wtapng_if_descr = g_array_index(idb_info->interface_data, wtap_block_t, i);
173 wtapng_if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(wtapng_if_descr);
174 if (wtap_block_get_custom_option_value(wtapng_if_descr, OPT_IDB_FILTER, (void**)&if_filter) == WTAP_OPTTYPE_SUCCESS) {
175 iface.cfilter = g_strdup(if_filter->if_filter_str);
177 iface.cfilter = NULL;
179 if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_NAME, &if_string) == WTAP_OPTTYPE_SUCCESS) {
180 iface.name = g_strdup(if_string);
184 if (wtap_block_get_string_option_value(wtapng_if_descr, OPT_IDB_DESCR, &if_string) == WTAP_OPTTYPE_SUCCESS) {
185 iface.descr = g_strdup(if_string);
189 iface.drops_known = FALSE;
191 iface.snap = wtapng_if_descr_mand->snap_len;
192 iface.encap_type = wtapng_if_descr_mand->wtap_encap;
193 iface.isb_comment = NULL;
194 if(wtapng_if_descr_mand->num_stat_entries == 1){
195 /* dumpcap only writes one ISB, only handle that for now */
196 if_stats = g_array_index(wtapng_if_descr_mand->interface_statistics, wtap_block_t, 0);
197 if (wtap_block_get_uint64_option_value(if_stats, OPT_ISB_IFDROP, &isb_ifdrop) == WTAP_OPTTYPE_SUCCESS) {
198 iface.drops_known = TRUE;
199 iface.drops = isb_ifdrop;
201 /* XXX: this doesn't get used, and might need to be g_strdup'ed when it does */
202 /* XXX - support multiple comments */
203 if (wtap_block_get_nth_string_option_value(if_stats, OPT_COMMENT, 0, &iface.isb_comment) != WTAP_OPTTYPE_SUCCESS) {
204 iface.isb_comment = NULL;
207 g_array_append_val(st->ifaces, iface);
211 g_strlcpy(st->file_sha256, "<unknown>", HASH_STR_SIZE);
212 g_strlcpy(st->file_rmd160, "<unknown>", HASH_STR_SIZE);
213 g_strlcpy(st->file_sha1, "<unknown>", HASH_STR_SIZE);
215 gcry_md_open(&hd, GCRY_MD_SHA256, 0);
217 gcry_md_enable(hd, GCRY_MD_RMD160);
218 gcry_md_enable(hd, GCRY_MD_SHA1);
220 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
222 fh = ws_fopen(cf->filename, "rb");
223 if (fh && hash_buf && hd) {
224 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
225 gcry_md_write(hd, hash_buf, hash_bytes);
228 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA256), HASH_SIZE_SHA256, st->file_sha256);
229 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, st->file_rmd160);
230 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, st->file_sha1);
239 summary_fill_in_capture(capture_file *cf,capture_options *capture_opts, summary_tally *st)
241 iface_summary_info iface;
245 if (st->ifaces->len == 0) {
247 * XXX - do this only if we have a live capture.
249 for (i = 0; i < capture_opts->all_ifaces->len; i++) {
250 device = &g_array_index(capture_opts->all_ifaces, interface_t, i);
251 if (!device->selected) {
254 iface.cfilter = g_strdup(device->cfilter);
255 iface.name = g_strdup(device->name);
256 iface.descr = g_strdup(device->display_name);
257 iface.drops_known = cf->drops_known;
258 iface.drops = cf->drops;
259 iface.snap = device->snaplen;
260 iface.encap_type = wtap_pcap_encap_to_wtap_encap(device->active_dlt);
261 g_array_append_val(st->ifaces, iface);
268 * Editor modelines - http://www.wireshark.org/tools/modelines.html
273 * indent-tabs-mode: nil
276 * ex: set shiftwidth=2 tabstop=8 expandtab:
277 * :indentSize=2:tabSize=8:noTabs=true: