2 * Reports capture file information including # of packets, duration, others
4 * Copyright 2004 Ian Schorr
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * New capinfos features
30 * Continue processing additional files after
31 * a wiretap open failure. The new -C option
32 * reverts to capinfos' original behavior which
33 * is to cancel any further file processing at
34 * first file open failure.
36 * Change the behavior of how the default display
37 * of all infos is initiated. This gets rid of a
38 * special post getopt() argument count test.
40 * Add new table output format (with related options)
41 * This feature allows outputting the various infos
42 * into a tab delimited text file, or to a comma
43 * separated variables file (*.csv) instead of the
44 * original "long" format.
47 * behaviour changed: Upon exit capinfos will return
48 * an error status if an error occurred at any
49 * point during "continuous" file processing.
50 * (Previously a success status was always
51 * returned if the -C option was not used).
72 #include <wiretap/wtap.h>
74 #include <wsutil/crash_info.h>
75 #include <wsutil/filesystem.h>
76 #include <wsutil/privileges.h>
77 #include <ws_version_info.h>
78 #include <wiretap/wtap_opttypes.h>
81 #include <wsutil/plugins.h>
84 #include <wsutil/report_err.h>
85 #include <wsutil/str_util.h>
86 #include <wsutil/file_util.h>
88 #include <wsutil/wsgcrypt.h>
90 #ifndef HAVE_GETOPT_LONG
91 #include "wsutil/wsgetopt.h"
95 #include <wsutil/unicode-utils.h>
99 * By default capinfos now continues processing
100 * the next filename if and when wiretap detects
101 * a problem opening a file.
102 * Use the '-C' option to revert back to original
103 * capinfos behavior which is to abort any
104 * additional file processing at first open file
108 static gboolean continue_after_wtap_open_offline_failure = TRUE;
111 * table report variables
114 static gboolean long_report = TRUE; /* By default generate long report */
115 static gchar table_report_header = TRUE; /* Generate column header by default */
116 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
117 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
118 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
121 * capinfos has the ability to report on a number of
122 * various characteristics ("infos") for each input file.
124 * By default reporting of all info fields is enabled.
126 * Optionally the reporting of any specific info field
127 * or combination of info fields can be enabled with
128 * individual options.
131 static gboolean report_all_infos = TRUE; /* Report all infos */
133 static gboolean cap_file_type = TRUE; /* Report capture type */
134 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
135 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
136 static gboolean cap_packet_count = TRUE; /* Report packet count */
137 static gboolean cap_file_size = TRUE; /* Report file size */
138 static gboolean cap_comment = TRUE; /* Display the capture comment */
139 static gboolean cap_file_more_info = TRUE; /* Report more file info */
140 static gboolean cap_file_idb = TRUE; /* Report Interface info */
142 static gboolean cap_data_size = TRUE; /* Report packet byte size */
143 static gboolean cap_duration = TRUE; /* Report capture duration */
144 static gboolean cap_start_time = TRUE; /* Report capture start time */
145 static gboolean cap_end_time = TRUE; /* Report capture end time */
146 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
148 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
149 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
150 static gboolean cap_packet_size = TRUE; /* Report average packet size */
151 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
152 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
154 #ifdef HAVE_LIBGCRYPT
155 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
157 #define HASH_SIZE_SHA1 20
158 #define HASH_SIZE_RMD160 20
159 #define HASH_SIZE_MD5 16
161 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
162 #define HASH_BUF_SIZE (1024 * 1024)
165 static gchar file_sha1[HASH_STR_SIZE];
166 static gchar file_rmd160[HASH_STR_SIZE];
167 static gchar file_md5[HASH_STR_SIZE];
169 #define FILE_HASH_OPT "H"
171 #define FILE_HASH_OPT ""
172 #endif /* HAVE_LIBGCRYPT */
175 * If we have at least two packets with time stamps, and they're not in
176 * order - i.e., the later packet has a time stamp older than the earlier
177 * packet - the time stamps are known not to be in order.
179 * If every packet has a time stamp, and they're all in order, the time
180 * stamp is known to be in order.
182 * Otherwise, we have no idea.
190 typedef struct _capture_info {
191 const char *filename;
193 gboolean iscompressed;
197 wtap_optionblock_t shb;
198 guint64 packet_bytes;
199 gboolean times_known;
201 int start_time_tsprec;
203 int stop_time_tsprec;
204 guint32 packet_count;
205 gboolean snap_set; /* If set in capture file header */
206 guint32 snaplen; /* value from the capture file header */
207 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
208 guint32 snaplen_max_inferred; /* ... */
209 gboolean drops_known;
216 double data_rate; /* in bytes */
220 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
222 guint num_interfaces; /* number of IDBs, and thus size of interface_packet_counts array */
223 GArray *interface_packet_counts; /* array of per_packet interface_id counts; one entry per file IDB */
224 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
225 GArray *idb_info_strings; /* array of IDB info strings */
228 static char *decimal_point;
231 enable_all_infos(void)
233 report_all_infos = TRUE;
235 cap_file_type = TRUE;
236 cap_file_encap = TRUE;
238 cap_packet_count = TRUE;
239 cap_file_size = TRUE;
241 cap_file_more_info = TRUE;
244 cap_data_size = TRUE;
246 cap_start_time = TRUE;
250 cap_data_rate_byte = TRUE;
251 cap_data_rate_bit = TRUE;
252 cap_packet_size = TRUE;
253 cap_packet_rate = TRUE;
255 #ifdef HAVE_LIBGCRYPT
256 cap_file_hashes = TRUE;
257 #endif /* HAVE_LIBGCRYPT */
261 disable_all_infos(void)
263 report_all_infos = FALSE;
265 cap_file_type = FALSE;
266 cap_file_encap = FALSE;
268 cap_packet_count = FALSE;
269 cap_file_size = FALSE;
271 cap_file_more_info = FALSE;
272 cap_file_idb = FALSE;
274 cap_data_size = FALSE;
275 cap_duration = FALSE;
276 cap_start_time = FALSE;
277 cap_end_time = FALSE;
280 cap_data_rate_byte = FALSE;
281 cap_data_rate_bit = FALSE;
282 cap_packet_size = FALSE;
283 cap_packet_rate = FALSE;
285 #ifdef HAVE_LIBGCRYPT
286 cap_file_hashes = FALSE;
287 #endif /* HAVE_LIBGCRYPT */
291 order_string(order_t order)
305 return "???"; /* "cannot happen" (the next step is "Profit!") */
310 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
312 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
315 if (cf_info->times_known && cf_info->packet_count > 0) {
317 switch (tsprecision) {
319 case WTAP_TSPREC_SEC:
320 g_snprintf(time_string_buf, sizeof time_string_buf,
322 (unsigned long)timer->secs);
325 case WTAP_TSPREC_DSEC:
326 g_snprintf(time_string_buf, sizeof time_string_buf,
328 (unsigned long)timer->secs,
330 timer->nsecs / 100000000);
333 case WTAP_TSPREC_CSEC:
334 g_snprintf(time_string_buf, sizeof time_string_buf,
336 (unsigned long)timer->secs,
338 timer->nsecs / 10000000);
341 case WTAP_TSPREC_MSEC:
342 g_snprintf(time_string_buf, sizeof time_string_buf,
344 (unsigned long)timer->secs,
346 timer->nsecs / 1000000);
349 case WTAP_TSPREC_USEC:
350 g_snprintf(time_string_buf, sizeof time_string_buf,
352 (unsigned long)timer->secs,
354 timer->nsecs / 1000);
357 case WTAP_TSPREC_NSEC:
358 g_snprintf(time_string_buf, sizeof time_string_buf,
360 (unsigned long)timer->secs,
366 g_snprintf(time_string_buf, sizeof time_string_buf,
367 "Unknown precision %d",
371 return time_string_buf;
373 ti_tm = localtime(&timer->secs);
375 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
376 return time_string_buf;
378 switch (tsprecision) {
380 case WTAP_TSPREC_SEC:
381 g_snprintf(time_string_buf, sizeof time_string_buf,
382 "%04d-%02d-%02d %02d:%02d:%02d",
383 ti_tm->tm_year + 1900,
391 case WTAP_TSPREC_DSEC:
392 g_snprintf(time_string_buf, sizeof time_string_buf,
393 "%04d-%02d-%02d %02d:%02d:%02d%s%01d",
394 ti_tm->tm_year + 1900,
401 timer->nsecs / 100000000);
404 case WTAP_TSPREC_CSEC:
405 g_snprintf(time_string_buf, sizeof time_string_buf,
406 "%04d-%02d-%02d %02d:%02d:%02d%s%02d",
407 ti_tm->tm_year + 1900,
414 timer->nsecs / 10000000);
417 case WTAP_TSPREC_MSEC:
418 g_snprintf(time_string_buf, sizeof time_string_buf,
419 "%04d-%02d-%02d %02d:%02d:%02d%s%03d",
420 ti_tm->tm_year + 1900,
427 timer->nsecs / 1000000);
430 case WTAP_TSPREC_USEC:
431 g_snprintf(time_string_buf, sizeof time_string_buf,
432 "%04d-%02d-%02d %02d:%02d:%02d%s%06d",
433 ti_tm->tm_year + 1900,
440 timer->nsecs / 1000);
443 case WTAP_TSPREC_NSEC:
444 g_snprintf(time_string_buf, sizeof time_string_buf,
445 "%04d-%02d-%02d %02d:%02d:%02d%s%09d",
446 ti_tm->tm_year + 1900,
457 g_snprintf(time_string_buf, sizeof time_string_buf,
458 "Unknown precision %d",
462 return time_string_buf;
466 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
467 return time_string_buf;
471 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
473 const gchar *second = want_seconds ? " second" : "";
474 const gchar *plural = want_seconds ? "s" : "";
475 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
477 if (cf_info->times_known && cf_info->packet_count > 0) {
478 switch (tsprecision) {
480 case WTAP_TSPREC_SEC:
481 g_snprintf(time_string_buf, sizeof time_string_buf,
483 (unsigned long)timer->secs,
485 timer->secs == 1 ? "" : plural);
488 case WTAP_TSPREC_DSEC:
489 g_snprintf(time_string_buf, sizeof time_string_buf,
491 (unsigned long)timer->secs,
493 timer->nsecs / 100000000,
495 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
498 case WTAP_TSPREC_CSEC:
499 g_snprintf(time_string_buf, sizeof time_string_buf,
501 (unsigned long)timer->secs,
503 timer->nsecs / 10000000,
505 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
508 case WTAP_TSPREC_MSEC:
509 g_snprintf(time_string_buf, sizeof time_string_buf,
511 (unsigned long)timer->secs,
513 timer->nsecs / 1000000,
515 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
518 case WTAP_TSPREC_USEC:
519 g_snprintf(time_string_buf, sizeof time_string_buf,
521 (unsigned long)timer->secs,
525 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
528 case WTAP_TSPREC_NSEC:
529 g_snprintf(time_string_buf, sizeof time_string_buf,
531 (unsigned long)timer->secs,
535 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
539 g_snprintf(time_string_buf, sizeof time_string_buf,
540 "Unknown precision %d",
544 return time_string_buf;
547 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
548 return time_string_buf;
551 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
553 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
555 printf("%sn/a\n", text_p1);
558 /* multi-line comments would conflict with the formatting that capinfos uses
559 we replace linefeeds with spaces */
561 string_replace_newlines(gchar *str)
578 show_option_string(const char *prefix, const char *option_str)
582 if (option_str != NULL && option_str[0] != '\0') {
583 str = g_strdup(option_str);
584 string_replace_newlines(str);
585 printf("%s%s\n", prefix, str);
591 print_stats(const gchar *filename, capture_info *cf_info)
593 const gchar *file_type_string, *file_encap_string;
596 /* Build printable strings for various stats */
597 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
598 file_encap_string = wtap_encap_string(cf_info->file_encap);
600 if (filename) printf ("File name: %s\n", filename);
601 if (cap_file_type) printf ("File type: %s%s\n",
603 cf_info->iscompressed ? " (gzip compressed)" : "");
605 if (cap_file_encap) {
606 printf ("File encapsulation: %s\n", file_encap_string);
607 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
609 printf ("Encapsulation in use by packets (# of pkts):\n");
610 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
611 if (cf_info->encap_counts[i] > 0)
613 wtap_encap_string(i), cf_info->encap_counts[i]);
617 if (cap_file_more_info) {
618 printf ("File timestamp precision: %s (%d)\n",
619 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
622 if (cap_snaplen && cf_info->snap_set)
623 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
624 else if (cap_snaplen && !cf_info->snap_set)
625 printf ("Packet size limit: file hdr: (not set)\n");
626 if (cf_info->snaplen_max_inferred > 0) {
627 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
628 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
630 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
631 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
633 if (cap_packet_count) {
634 printf ("Number of packets: ");
635 if (machine_readable) {
636 printf ("%u\n", cf_info->packet_count);
638 size_string = format_size(cf_info->packet_count, format_size_unit_none);
639 printf ("%s\n", size_string);
644 printf ("File size: ");
645 if (machine_readable) {
646 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
648 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
649 printf ("%s\n", size_string);
654 printf ("Data size: ");
655 if (machine_readable) {
656 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
658 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
659 printf ("%s\n", size_string);
663 if (cf_info->times_known) {
664 if (cap_duration) /* XXX - shorten to hh:mm:ss */
665 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
667 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
669 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
670 if (cap_data_rate_byte) {
671 printf("Data byte rate: ");
672 if (machine_readable) {
673 print_value("", 2, " bytes/sec", cf_info->data_rate);
675 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
676 printf ("%s\n", size_string);
680 if (cap_data_rate_bit) {
681 printf("Data bit rate: ");
682 if (machine_readable) {
683 print_value("", 2, " bits/sec", cf_info->data_rate*8);
685 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
686 printf ("%s\n", size_string);
691 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
692 if (cf_info->times_known) {
693 if (cap_packet_rate) {
694 printf("Average packet rate: ");
695 if (machine_readable) {
696 print_value("", 2, " packets/sec", cf_info->packet_rate);
698 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
699 printf ("%s\n", size_string);
704 #ifdef HAVE_LIBGCRYPT
705 if (cap_file_hashes) {
706 printf ("SHA1: %s\n", file_sha1);
707 printf ("RIPEMD160: %s\n", file_rmd160);
708 printf ("MD5: %s\n", file_md5);
710 #endif /* HAVE_LIBGCRYPT */
711 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
713 if (cf_info->shb != NULL) {
718 wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts);
719 for (i = 0; i < opts->len; i++) {
720 show_option_string("Capture comment: ", g_array_index(opts, char *, i));
722 g_array_free(opts, TRUE);
724 if (cap_file_more_info) {
727 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str);
728 show_option_string("Capture hardware: ", str);
729 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str);
730 show_option_string("Capture oper-sys: ", str);
731 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str);
732 show_option_string("Capture application: ", str);
735 if (cap_file_idb && cf_info->num_interfaces != 0) {
737 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
738 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
739 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
740 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
741 printf ("Interface #%u info:\n", i);
743 printf (" Number of packets = %u\n", g_array_index(cf_info->interface_packet_counts, guint32, i));
752 if (field_separator) putchar(field_separator);
758 if (quote_char) putchar(quote_char);
762 print_stats_table_header_label(const gchar *label)
771 print_stats_table_header(void)
777 if (cap_file_type) print_stats_table_header_label("File type");
778 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
779 if (cap_file_more_info) print_stats_table_header_label("File time precision");
780 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
781 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
782 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
783 if (cap_packet_count) print_stats_table_header_label("Number of packets");
784 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
785 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
786 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
787 if (cap_start_time) print_stats_table_header_label("Start time");
788 if (cap_end_time) print_stats_table_header_label("End time");
789 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
790 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
791 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
792 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
793 #ifdef HAVE_LIBGCRYPT
794 if (cap_file_hashes) {
795 print_stats_table_header_label("SHA1");
796 print_stats_table_header_label("RIPEMD160");
797 print_stats_table_header_label("MD5");
799 #endif /* HAVE_LIBGCRYPT */
800 if (cap_order) print_stats_table_header_label("Strict time order");
801 if (cap_comment) print_stats_table_header_label("Capture comment");
802 if (cap_file_more_info) {
803 print_stats_table_header_label("Capture hardware");
804 print_stats_table_header_label("Capture oper-sys");
805 print_stats_table_header_label("Capture application");
812 print_stats_table(const gchar *filename, capture_info *cf_info)
814 const gchar *file_type_string, *file_encap_string;
816 /* Build printable strings for various stats */
817 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
818 file_encap_string = wtap_encap_string(cf_info->file_encap);
822 printf("%s", filename);
829 printf("%s", file_type_string);
833 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
834 * Output a line for each different encap with all fields repeated except
835 * the encapsulation field which has "Per Packet: ..." for each
836 * encapsulation type seen ?
838 if (cap_file_encap) {
841 printf("%s", file_encap_string);
845 if (cap_file_more_info) {
848 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
855 if (cf_info->snap_set)
856 printf("%u", cf_info->snaplen);
860 if (cf_info->snaplen_max_inferred > 0) {
863 printf("%u", cf_info->snaplen_min_inferred);
867 printf("%u", cf_info->snaplen_max_inferred);
882 if (cap_packet_count) {
885 printf("%u", cf_info->packet_count);
892 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
899 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
906 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
910 if (cap_start_time) {
913 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
920 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
924 if (cap_data_rate_byte) {
927 if (cf_info->times_known)
928 printf("%.2f", cf_info->data_rate);
934 if (cap_data_rate_bit) {
937 if (cf_info->times_known)
938 printf("%.2f", cf_info->data_rate*8);
944 if (cap_packet_size) {
947 printf("%.2f", cf_info->packet_size);
951 if (cap_packet_rate) {
954 if (cf_info->times_known)
955 printf("%.2f", cf_info->packet_rate);
961 #ifdef HAVE_LIBGCRYPT
962 if (cap_file_hashes) {
965 printf("%s", file_sha1);
970 printf("%s", file_rmd160);
975 printf("%s", file_md5);
978 #endif /* HAVE_LIBGCRYPT */
983 printf("%s", order_string(cf_info->order));
987 if (cf_info->shb != NULL) {
989 * this is silly to put into a table format, but oh well
990 * note that there may be *more than one* of each of these types
997 wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts);
998 for (i = 0; i < opts->len; i++) {
999 const char *opt_comment = g_array_index(opts, char *, i);
1001 if (opt_comment != NULL) {
1004 printf("%s", opt_comment);
1008 g_array_free(opts, TRUE);
1011 if (cap_file_more_info) {
1014 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str);
1021 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str);
1028 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str);
1042 cleanup_capture_info(capture_info *cf_info)
1045 g_assert(cf_info != NULL);
1047 g_free(cf_info->encap_counts);
1048 cf_info->encap_counts = NULL;
1050 g_array_free(cf_info->interface_packet_counts, TRUE);
1051 cf_info->interface_packet_counts = NULL;
1053 if (cf_info->idb_info_strings) {
1054 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
1055 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
1058 g_array_free(cf_info->idb_info_strings, TRUE);
1060 cf_info->idb_info_strings = NULL;
1064 process_cap_file(wtap *wth, const char *filename)
1074 guint32 snaplen_min_inferred = 0xffffffff;
1075 guint32 snaplen_max_inferred = 0;
1076 const struct wtap_pkthdr *phdr;
1077 capture_info cf_info;
1078 gboolean have_times = TRUE;
1079 nstime_t start_time;
1080 int start_time_tsprec;
1082 int stop_time_tsprec;
1085 gboolean know_order = FALSE;
1086 order_t order = IN_ORDER;
1088 wtapng_iface_descriptions_t *idb_info;
1090 g_assert(wth != NULL);
1091 g_assert(filename != NULL);
1093 nstime_set_zero(&start_time);
1094 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1095 nstime_set_zero(&stop_time);
1096 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1097 nstime_set_zero(&cur_time);
1098 nstime_set_zero(&prev_time);
1100 cf_info.shb = wtap_file_get_shb(wth);
1102 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1104 idb_info = wtap_file_get_idb_info(wth);
1106 g_assert(idb_info->interface_data != NULL);
1108 cf_info.num_interfaces = idb_info->interface_data->len;
1109 cf_info.interface_packet_counts = g_array_sized_new(FALSE, TRUE, sizeof(guint32), cf_info.num_interfaces);
1110 g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1111 cf_info.pkt_interface_id_unknown = 0;
1116 /* Tally up data that we need to parse through the file to find */
1117 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1118 phdr = wtap_phdr(wth);
1119 if (phdr->presence_flags & WTAP_HAS_TS) {
1120 prev_time = cur_time;
1121 cur_time = phdr->ts;
1123 start_time = phdr->ts;
1124 start_time_tsprec = phdr->pkt_tsprec;
1125 stop_time = phdr->ts;
1126 stop_time_tsprec = phdr->pkt_tsprec;
1127 prev_time = phdr->ts;
1129 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1130 order = NOT_IN_ORDER;
1132 if (nstime_cmp(&cur_time, &start_time) < 0) {
1133 start_time = cur_time;
1134 start_time_tsprec = phdr->pkt_tsprec;
1136 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1137 stop_time = cur_time;
1138 stop_time_tsprec = phdr->pkt_tsprec;
1141 have_times = FALSE; /* at least one packet has no time stamp */
1142 if (order != NOT_IN_ORDER)
1143 order = ORDER_UNKNOWN;
1146 if (phdr->rec_type == REC_TYPE_PACKET) {
1150 /* If caplen < len for a rcd, then presumably */
1151 /* 'Limit packet capture length' was done for this rcd. */
1152 /* Keep track as to the min/max actual snapshot lengths */
1153 /* seen for this file. */
1154 if (phdr->caplen < phdr->len) {
1155 if (phdr->caplen < snaplen_min_inferred)
1156 snaplen_min_inferred = phdr->caplen;
1157 if (phdr->caplen > snaplen_max_inferred)
1158 snaplen_max_inferred = phdr->caplen;
1161 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1162 cf_info.encap_counts[phdr->pkt_encap] += 1;
1164 fprintf(stderr, "capinfos: Unknown packet encapsulation %d in frame %u of file \"%s\"\n",
1165 phdr->pkt_encap, packet, filename);
1168 /* Packet interface_id info */
1169 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1170 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1171 if (phdr->interface_id >= cf_info.num_interfaces) {
1173 * OK, re-fetch the number of interfaces, as there might have
1174 * been an interface that was in the middle of packets, and
1175 * grow the array to be big enough for the new number of
1178 idb_info = wtap_file_get_idb_info(wth);
1180 cf_info.num_interfaces = idb_info->interface_data->len;
1181 g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1186 if (phdr->interface_id < cf_info.num_interfaces) {
1187 g_array_index(cf_info.interface_packet_counts, guint32, phdr->interface_id) += 1;
1190 cf_info.pkt_interface_id_unknown += 1;
1194 /* it's for interface_id 0 */
1195 if (cf_info.num_interfaces != 0) {
1196 g_array_index(cf_info.interface_packet_counts, guint32, 0) += 1;
1199 cf_info.pkt_interface_id_unknown += 1;
1207 * Get IDB info strings.
1208 * We do this at the end, so we can get information for all IDBs in
1209 * the file, even those that come after packet records.
1211 idb_info = wtap_file_get_idb_info(wth);
1213 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1214 cf_info.num_interfaces = idb_info->interface_data->len;
1215 for (i = 0; i < cf_info.num_interfaces; i++) {
1216 const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1217 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1218 g_array_append_val(cf_info.idb_info_strings, s);
1226 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1227 packet, filename, wtap_strerror(err));
1228 if (err == WTAP_ERR_SHORT_READ) {
1229 /* Don't give up completely with this one. */
1232 " (will continue anyway, checksums might be incorrect)\n");
1234 if (err_info != NULL) {
1235 fprintf(stderr, "(%s)\n", err_info);
1239 cleanup_capture_info(&cf_info);
1245 size = wtap_file_size(wth, &err);
1248 "capinfos: Can't get size of \"%s\": %s.\n",
1249 filename, g_strerror(err));
1250 cleanup_capture_info(&cf_info);
1254 cf_info.filesize = size;
1257 cf_info.file_type = wtap_file_type_subtype(wth);
1258 cf_info.iscompressed = wtap_iscompressed(wth);
1260 /* File Encapsulation */
1261 cf_info.file_encap = wtap_file_encap(wth);
1263 cf_info.file_tsprec = wtap_file_tsprec(wth);
1265 /* Packet size limit (snaplen) */
1266 cf_info.snaplen = wtap_snapshot_length(wth);
1267 if (cf_info.snaplen > 0)
1268 cf_info.snap_set = TRUE;
1270 cf_info.snap_set = FALSE;
1272 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1273 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1276 cf_info.packet_count = packet;
1279 cf_info.times_known = have_times;
1280 cf_info.start_time = start_time;
1281 cf_info.start_time_tsprec = start_time_tsprec;
1282 cf_info.stop_time = stop_time;
1283 cf_info.stop_time_tsprec = stop_time_tsprec;
1284 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1285 /* Duration precision is the higher of the start and stop time precisions. */
1286 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1287 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1289 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1290 cf_info.know_order = know_order;
1291 cf_info.order = order;
1293 /* Number of packet bytes */
1294 cf_info.packet_bytes = bytes;
1296 cf_info.data_rate = 0.0;
1297 cf_info.packet_rate = 0.0;
1298 cf_info.packet_size = 0.0;
1301 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1302 if (delta_time > 0.0) {
1303 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1304 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1306 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1310 print_stats(filename, &cf_info);
1312 print_stats_table(filename, &cf_info);
1315 cleanup_capture_info(&cf_info);
1321 print_usage(FILE *output)
1323 fprintf(output, "\n");
1324 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1325 fprintf(output, "\n");
1326 fprintf(output, "General infos:\n");
1327 fprintf(output, " -t display the capture file type\n");
1328 fprintf(output, " -E display the capture file encapsulation\n");
1329 fprintf(output, " -I display the capture file interface information\n");
1330 fprintf(output, " -F display additional capture file information\n");
1331 #ifdef HAVE_LIBGCRYPT
1332 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1334 fprintf(output, " -k display the capture comment\n");
1335 fprintf(output, "\n");
1336 fprintf(output, "Size infos:\n");
1337 fprintf(output, " -c display the number of packets\n");
1338 fprintf(output, " -s display the size of the file (in bytes)\n");
1339 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1340 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1341 fprintf(output, "\n");
1342 fprintf(output, "Time infos:\n");
1343 fprintf(output, " -u display the capture duration (in seconds)\n");
1344 fprintf(output, " -a display the capture start time\n");
1345 fprintf(output, " -e display the capture end time\n");
1346 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1347 fprintf(output, " -S display start and end times as seconds\n");
1348 fprintf(output, "\n");
1349 fprintf(output, "Statistic infos:\n");
1350 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1351 fprintf(output, " -i display average data rate (in bits/sec)\n");
1352 fprintf(output, " -z display average packet size (in bytes)\n");
1353 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1354 fprintf(output, "\n");
1355 fprintf(output, "Output format:\n");
1356 fprintf(output, " -L generate long report (default)\n");
1357 fprintf(output, " -T generate table report\n");
1358 fprintf(output, " -M display machine-readable values in long reports\n");
1359 fprintf(output, "\n");
1360 fprintf(output, "Table report options:\n");
1361 fprintf(output, " -R generate header record (default)\n");
1362 fprintf(output, " -r do not generate header record\n");
1363 fprintf(output, "\n");
1364 fprintf(output, " -B separate infos with TAB character (default)\n");
1365 fprintf(output, " -m separate infos with comma (,) character\n");
1366 fprintf(output, " -b separate infos with SPACE character\n");
1367 fprintf(output, "\n");
1368 fprintf(output, " -N do not quote infos (default)\n");
1369 fprintf(output, " -q quote infos with single quotes (')\n");
1370 fprintf(output, " -Q quote infos with double quotes (\")\n");
1371 fprintf(output, "\n");
1372 fprintf(output, "Miscellaneous:\n");
1373 fprintf(output, " -h display this help and exit\n");
1374 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1375 fprintf(output, " -A generate all infos (default)\n");
1376 fprintf(output, "\n");
1377 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1378 fprintf(output, "or adding to earlier options.\n");
1379 fprintf(output, "\n");
1380 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1381 fprintf(output, "output format.\n");
1382 #ifndef HAVE_LIBGCRYPT
1383 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1389 * Don't report failures to load plugins because most (non-wiretap) plugins
1390 * *should* fail to load (because we're not linked against libwireshark and
1391 * dissector plugins need libwireshark).
1394 failure_message(const char *msg_format _U_, va_list ap _U_)
1400 #ifdef HAVE_LIBGCRYPT
1402 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1405 for (i = 0; i < (int) length; i++) {
1406 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1409 #endif /* HAVE_LIBGCRYPT */
1412 main(int argc, char *argv[])
1414 GString *comp_info_str;
1415 GString *runtime_info_str;
1420 int overall_error_status;
1421 static const struct option long_options[] = {
1422 {"help", no_argument, NULL, 'h'},
1423 {"version", no_argument, NULL, 'v'},
1429 char *init_progfile_dir_error;
1431 #ifdef HAVE_LIBGCRYPT
1433 char *hash_buf = NULL;
1434 gcry_md_hd_t hd = NULL;
1438 /* Set the C-language locale to the native environment. */
1439 setlocale(LC_ALL, "");
1441 /* Get the decimal point. */
1442 decimal_point = g_strdup(localeconv()->decimal_point);
1444 /* Get the compile-time version information string */
1445 comp_info_str = get_compiled_version_info(NULL, NULL);
1447 /* Get the run-time version information string */
1448 runtime_info_str = get_runtime_version_info(NULL);
1450 /* Add it to the information to be reported on a crash. */
1451 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1456 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1459 arg_list_utf_16to8(argc, argv);
1460 create_app_running_mutex();
1464 * Get credential information for later use.
1466 init_process_policies();
1467 init_open_routines();
1470 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1471 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1472 g_free(init_progfile_dir_error);
1474 /* Register all the plugin types we have. */
1475 wtap_register_plugin_types(); /* Types known to libwiretap */
1477 init_report_err(failure_message, NULL, NULL, NULL);
1479 /* Scan for plugins. This does *not* call their registration routines;
1480 that's done later. */
1483 /* Register all libwiretap plugin modules. */
1484 register_all_wiretap_modules();
1488 /* Process the options */
1489 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1490 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1495 if (report_all_infos) disable_all_infos();
1496 cap_file_type = TRUE;
1500 if (report_all_infos) disable_all_infos();
1501 cap_file_encap = TRUE;
1505 if (report_all_infos) disable_all_infos();
1510 if (report_all_infos) disable_all_infos();
1511 cap_packet_count = TRUE;
1515 if (report_all_infos) disable_all_infos();
1516 cap_file_size = TRUE;
1520 if (report_all_infos) disable_all_infos();
1521 cap_data_size = TRUE;
1525 if (report_all_infos) disable_all_infos();
1526 cap_duration = TRUE;
1530 if (report_all_infos) disable_all_infos();
1531 cap_start_time = TRUE;
1535 if (report_all_infos) disable_all_infos();
1536 cap_end_time = TRUE;
1540 time_as_secs = TRUE;
1544 if (report_all_infos) disable_all_infos();
1545 cap_data_rate_byte = TRUE;
1549 if (report_all_infos) disable_all_infos();
1550 cap_data_rate_bit = TRUE;
1554 if (report_all_infos) disable_all_infos();
1555 cap_packet_size = TRUE;
1559 if (report_all_infos) disable_all_infos();
1560 cap_packet_rate = TRUE;
1563 #ifdef HAVE_LIBGCRYPT
1565 if (report_all_infos) disable_all_infos();
1566 cap_file_hashes = TRUE;
1571 if (report_all_infos) disable_all_infos();
1576 if (report_all_infos) disable_all_infos();
1581 if (report_all_infos) disable_all_infos();
1582 cap_file_more_info = TRUE;
1586 if (report_all_infos) disable_all_infos();
1587 cap_file_idb = TRUE;
1591 continue_after_wtap_open_offline_failure = FALSE;
1603 long_report = FALSE;
1607 machine_readable = TRUE;
1611 table_report_header = TRUE;
1615 table_report_header = FALSE;
1631 field_separator = '\t';
1635 field_separator = ',';
1639 field_separator = ' ';
1643 printf("Capinfos (Wireshark) %s\n"
1644 "Print various information (infos) about capture files.\n"
1645 "See https://www.wireshark.org for more information.\n",
1646 get_ws_vcs_version_info());
1647 print_usage(stdout);
1652 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1653 g_string_free(comp_info_str, TRUE);
1654 g_string_free(runtime_info_str, TRUE);
1658 case '?': /* Bad flag - print usage message */
1659 print_usage(stderr);
1665 if ((argc - optind) < 1) {
1666 print_usage(stderr);
1670 if (!long_report && table_report_header) {
1671 print_stats_table_header();
1674 #ifdef HAVE_LIBGCRYPT
1675 if (cap_file_hashes) {
1676 gcry_check_version(NULL);
1677 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1679 gcry_md_enable(hd, GCRY_MD_RMD160);
1680 gcry_md_enable(hd, GCRY_MD_MD5);
1682 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1686 overall_error_status = 0;
1688 for (opt = optind; opt < argc; opt++) {
1690 #ifdef HAVE_LIBGCRYPT
1691 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1692 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1693 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1695 if (cap_file_hashes) {
1696 fh = ws_fopen(argv[opt], "rb");
1698 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1699 gcry_md_write(hd, hash_buf, hash_bytes);
1702 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1703 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1704 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1707 if (hd) gcry_md_reset(hd);
1709 #endif /* HAVE_LIBGCRYPT */
1711 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1714 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1715 wtap_strerror(err));
1716 if (err_info != NULL) {
1717 fprintf(stderr, "(%s)\n", err_info);
1720 overall_error_status = 1; /* remember that an error has occurred */
1721 if (!continue_after_wtap_open_offline_failure)
1722 exit(1); /* error status */
1726 if ((opt > optind) && (long_report))
1728 status = process_cap_file(wth, argv[opt]);
1736 return overall_error_status;
1740 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1745 * indent-tabs-mode: nil
1748 * vi: set shiftwidth=2 tabstop=8 expandtab:
1749 * :indentSize=2:tabSize=8:noTabs=true: