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>
79 #include <wiretap/pcapng.h>
82 #include <wsutil/plugins.h>
85 #include <wsutil/report_err.h>
86 #include <wsutil/str_util.h>
87 #include <wsutil/file_util.h>
89 #include <wsutil/wsgcrypt.h>
91 #ifndef HAVE_GETOPT_LONG
92 #include "wsutil/wsgetopt.h"
96 #include <wsutil/unicode-utils.h>
100 * By default capinfos now continues processing
101 * the next filename if and when wiretap detects
102 * a problem opening a file.
103 * Use the '-C' option to revert back to original
104 * capinfos behavior which is to abort any
105 * additional file processing at first open file
109 static gboolean continue_after_wtap_open_offline_failure = TRUE;
112 * table report variables
115 static gboolean long_report = TRUE; /* By default generate long report */
116 static gchar table_report_header = TRUE; /* Generate column header by default */
117 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
118 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
119 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
122 * capinfos has the ability to report on a number of
123 * various characteristics ("infos") for each input file.
125 * By default reporting of all info fields is enabled.
127 * Optionally the reporting of any specific info field
128 * or combination of info fields can be enabled with
129 * individual options.
132 static gboolean report_all_infos = TRUE; /* Report all infos */
134 static gboolean cap_file_type = TRUE; /* Report capture type */
135 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
136 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
137 static gboolean cap_packet_count = TRUE; /* Report packet count */
138 static gboolean cap_file_size = TRUE; /* Report file size */
139 static gboolean cap_comment = TRUE; /* Display the capture comment */
140 static gboolean cap_file_more_info = TRUE; /* Report more file info */
141 static gboolean cap_file_idb = TRUE; /* Report Interface info */
143 static gboolean cap_data_size = TRUE; /* Report packet byte size */
144 static gboolean cap_duration = TRUE; /* Report capture duration */
145 static gboolean cap_start_time = TRUE; /* Report capture start time */
146 static gboolean cap_end_time = TRUE; /* Report capture end time */
147 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
149 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
150 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
151 static gboolean cap_packet_size = TRUE; /* Report average packet size */
152 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
153 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
155 #ifdef HAVE_LIBGCRYPT
156 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
158 #define HASH_SIZE_SHA1 20
159 #define HASH_SIZE_RMD160 20
160 #define HASH_SIZE_MD5 16
162 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
163 #define HASH_BUF_SIZE (1024 * 1024)
166 static gchar file_sha1[HASH_STR_SIZE];
167 static gchar file_rmd160[HASH_STR_SIZE];
168 static gchar file_md5[HASH_STR_SIZE];
170 #define FILE_HASH_OPT "H"
172 #define FILE_HASH_OPT ""
173 #endif /* HAVE_LIBGCRYPT */
176 * If we have at least two packets with time stamps, and they're not in
177 * order - i.e., the later packet has a time stamp older than the earlier
178 * packet - the time stamps are known not to be in order.
180 * If every packet has a time stamp, and they're all in order, the time
181 * stamp is known to be in order.
183 * Otherwise, we have no idea.
191 typedef struct _capture_info {
192 const char *filename;
194 gboolean iscompressed;
198 wtap_optionblock_t shb;
199 guint64 packet_bytes;
200 gboolean times_known;
202 int start_time_tsprec;
204 int stop_time_tsprec;
205 guint32 packet_count;
206 gboolean snap_set; /* If set in capture file header */
207 guint32 snaplen; /* value from the capture file header */
208 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
209 guint32 snaplen_max_inferred; /* ... */
210 gboolean drops_known;
217 double data_rate; /* in bytes */
221 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
223 guint num_interfaces; /* number of IDBs, and thus size of interface_ids array */
224 guint32 *interface_ids; /* array of per_packet interface_id counts; one entry per file IDB */
225 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
226 GArray *idb_info_strings; /* array of IDB info strings */
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,
329 timer->nsecs / 100000000);
332 case WTAP_TSPREC_CSEC:
333 g_snprintf(time_string_buf, sizeof time_string_buf,
335 (unsigned long)timer->secs,
336 timer->nsecs / 10000000);
339 case WTAP_TSPREC_MSEC:
340 g_snprintf(time_string_buf, sizeof time_string_buf,
342 (unsigned long)timer->secs,
343 timer->nsecs / 1000000);
346 case WTAP_TSPREC_USEC:
347 g_snprintf(time_string_buf, sizeof time_string_buf,
349 (unsigned long)timer->secs,
350 timer->nsecs / 1000);
353 case WTAP_TSPREC_NSEC:
354 g_snprintf(time_string_buf, sizeof time_string_buf,
356 (unsigned long)timer->secs,
361 g_snprintf(time_string_buf, sizeof time_string_buf,
362 "Unknown precision %d",
366 return time_string_buf;
368 ti_tm = localtime(&timer->secs);
370 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
371 return time_string_buf;
373 switch (tsprecision) {
375 case WTAP_TSPREC_SEC:
376 g_snprintf(time_string_buf, sizeof time_string_buf,
377 "%04d-%02d-%02d %02d:%02d:%02d",
378 ti_tm->tm_year + 1900,
386 case WTAP_TSPREC_DSEC:
387 g_snprintf(time_string_buf, sizeof time_string_buf,
388 "%04d-%02d-%02d %02d:%02d:%02d.%01d",
389 ti_tm->tm_year + 1900,
395 timer->nsecs / 100000000);
398 case WTAP_TSPREC_CSEC:
399 g_snprintf(time_string_buf, sizeof time_string_buf,
400 "%04d-%02d-%02d %02d:%02d:%02d.%02d",
401 ti_tm->tm_year + 1900,
407 timer->nsecs / 10000000);
410 case WTAP_TSPREC_MSEC:
411 g_snprintf(time_string_buf, sizeof time_string_buf,
412 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
413 ti_tm->tm_year + 1900,
419 timer->nsecs / 1000000);
422 case WTAP_TSPREC_USEC:
423 g_snprintf(time_string_buf, sizeof time_string_buf,
424 "%04d-%02d-%02d %02d:%02d:%02d.%06d",
425 ti_tm->tm_year + 1900,
431 timer->nsecs / 1000);
434 case WTAP_TSPREC_NSEC:
435 g_snprintf(time_string_buf, sizeof time_string_buf,
436 "%04d-%02d-%02d %02d:%02d:%02d.%09d",
437 ti_tm->tm_year + 1900,
447 g_snprintf(time_string_buf, sizeof time_string_buf,
448 "Unknown precision %d",
452 return time_string_buf;
456 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
457 return time_string_buf;
461 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
463 const gchar *second = want_seconds ? " second" : "";
464 const gchar *plural = want_seconds ? "s" : "";
465 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
467 if (cf_info->times_known && cf_info->packet_count > 0) {
468 switch (tsprecision) {
470 case WTAP_TSPREC_SEC:
471 g_snprintf(time_string_buf, sizeof time_string_buf,
473 (unsigned long)timer->secs,
475 timer->secs == 1 ? "" : plural);
478 case WTAP_TSPREC_DSEC:
479 g_snprintf(time_string_buf, sizeof time_string_buf,
481 (unsigned long)timer->secs,
482 timer->nsecs / 100000000,
484 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
487 case WTAP_TSPREC_CSEC:
488 g_snprintf(time_string_buf, sizeof time_string_buf,
490 (unsigned long)timer->secs,
491 timer->nsecs / 10000000,
493 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
496 case WTAP_TSPREC_MSEC:
497 g_snprintf(time_string_buf, sizeof time_string_buf,
499 (unsigned long)timer->secs,
500 timer->nsecs / 1000000,
502 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
505 case WTAP_TSPREC_USEC:
506 g_snprintf(time_string_buf, sizeof time_string_buf,
508 (unsigned long)timer->secs,
511 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
514 case WTAP_TSPREC_NSEC:
515 g_snprintf(time_string_buf, sizeof time_string_buf,
517 (unsigned long)timer->secs,
520 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
524 g_snprintf(time_string_buf, sizeof time_string_buf,
525 "Unknown precision %d",
529 return time_string_buf;
532 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
533 return time_string_buf;
536 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
538 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
540 printf("%sn/a\n", text_p1);
543 /* multi-line comments would conflict with the formatting that capinfos uses
544 we replace linefeeds with spaces */
546 string_replace_newlines(gchar *str)
563 show_comment(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
570 if (option != NULL && option->stringval != NULL) {
571 opt_str = g_strdup(option->stringval);
572 string_replace_newlines(opt_str);
573 printf("Capture comment: %s\n", opt_str);
578 /* Don't show other options */
584 show_capture_hardware(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
590 case OPT_SHB_HARDWARE:
591 if (option != NULL && option->stringval != NULL) {
592 opt_str = g_strdup(option->stringval);
593 string_replace_newlines(opt_str);
594 printf("Capture hardware: %s\n", opt_str);
599 /* Don't show other options */
605 show_capture_os(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
612 if (option != NULL && option->stringval != NULL) {
613 opt_str = g_strdup(option->stringval);
614 string_replace_newlines(opt_str);
615 printf("Capture oper-sys: %s\n", opt_str);
620 /* Don't show other options */
626 show_capture_userappl(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
632 case OPT_SHB_USERAPPL:
633 if (option != NULL && option->stringval != NULL) {
634 opt_str = g_strdup(option->stringval);
635 string_replace_newlines(opt_str);
636 printf("Capture application: %s\n", opt_str);
641 /* Don't show other options */
647 print_stats(const gchar *filename, capture_info *cf_info)
649 const gchar *file_type_string, *file_encap_string;
652 /* Build printable strings for various stats */
653 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
654 file_encap_string = wtap_encap_string(cf_info->file_encap);
656 if (filename) printf ("File name: %s\n", filename);
657 if (cap_file_type) printf ("File type: %s%s\n",
659 cf_info->iscompressed ? " (gzip compressed)" : "");
661 if (cap_file_encap) {
662 printf ("File encapsulation: %s\n", file_encap_string);
663 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
665 printf ("Encapsulation in use by packets (# of pkts):\n");
666 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
667 if (cf_info->encap_counts[i] > 0)
669 wtap_encap_string(i), cf_info->encap_counts[i]);
673 if (cap_file_more_info) {
674 printf ("File timestamp precision: %s (%d)\n",
675 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
678 if (cap_snaplen && cf_info->snap_set)
679 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
680 else if (cap_snaplen && !cf_info->snap_set)
681 printf ("Packet size limit: file hdr: (not set)\n");
682 if (cf_info->snaplen_max_inferred > 0) {
683 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
684 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
686 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
687 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
689 if (cap_packet_count) {
690 printf ("Number of packets: ");
691 if (machine_readable) {
692 printf ("%u\n", cf_info->packet_count);
694 size_string = format_size(cf_info->packet_count, format_size_unit_none);
695 printf ("%s\n", size_string);
700 printf ("File size: ");
701 if (machine_readable) {
702 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
704 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
705 printf ("%s\n", size_string);
710 printf ("Data size: ");
711 if (machine_readable) {
712 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
714 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
715 printf ("%s\n", size_string);
719 if (cf_info->times_known) {
720 if (cap_duration) /* XXX - shorten to hh:mm:ss */
721 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
723 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
725 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
726 if (cap_data_rate_byte) {
727 printf("Data byte rate: ");
728 if (machine_readable) {
729 print_value("", 2, " bytes/sec", cf_info->data_rate);
731 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
732 printf ("%s\n", size_string);
736 if (cap_data_rate_bit) {
737 printf("Data bit rate: ");
738 if (machine_readable) {
739 print_value("", 2, " bits/sec", cf_info->data_rate*8);
741 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
742 printf ("%s\n", size_string);
747 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
748 if (cf_info->times_known) {
749 if (cap_packet_rate) {
750 printf("Average packet rate: ");
751 if (machine_readable) {
752 print_value("", 2, " packets/sec", cf_info->packet_rate);
754 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
755 printf ("%s\n", size_string);
760 #ifdef HAVE_LIBGCRYPT
761 if (cap_file_hashes) {
762 printf ("SHA1: %s\n", file_sha1);
763 printf ("RIPEMD160: %s\n", file_rmd160);
764 printf ("MD5: %s\n", file_md5);
766 #endif /* HAVE_LIBGCRYPT */
767 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
770 wtap_optionblock_foreach_option(cf_info->shb, show_comment, NULL);
772 if (cap_file_more_info) {
773 wtap_optionblock_foreach_option(cf_info->shb, show_capture_hardware, NULL);
774 wtap_optionblock_foreach_option(cf_info->shb, show_capture_os, NULL);
775 wtap_optionblock_foreach_option(cf_info->shb, show_capture_userappl, NULL);
778 if (cap_file_idb && cf_info->num_interfaces != 0) {
780 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
781 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
782 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
783 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
784 printf ("Interface #%u info:\n", i);
786 printf (" Number of packets = %u\n", cf_info->interface_ids[i]);
794 if (field_separator) putchar(field_separator);
800 if (quote_char) putchar(quote_char);
804 print_stats_table_header_label(const gchar *label)
813 print_stats_table_header(void)
819 if (cap_file_type) print_stats_table_header_label("File type");
820 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
821 if (cap_file_more_info) print_stats_table_header_label("File time precision");
822 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
823 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
824 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
825 if (cap_packet_count) print_stats_table_header_label("Number of packets");
826 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
827 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
828 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
829 if (cap_start_time) print_stats_table_header_label("Start time");
830 if (cap_end_time) print_stats_table_header_label("End time");
831 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
832 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
833 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
834 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
835 #ifdef HAVE_LIBGCRYPT
836 if (cap_file_hashes) {
837 print_stats_table_header_label("SHA1");
838 print_stats_table_header_label("RIPEMD160");
839 print_stats_table_header_label("MD5");
841 #endif /* HAVE_LIBGCRYPT */
842 if (cap_order) print_stats_table_header_label("Strict time order");
843 if (cap_comment) print_stats_table_header_label("Capture comment");
844 if (cap_file_more_info) {
845 print_stats_table_header_label("Capture hardware");
846 print_stats_table_header_label("Capture oper-sys");
847 print_stats_table_header_label("Capture application");
854 put_comment(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
859 if (option != NULL && option->stringval != NULL) {
862 printf("%s", option->stringval);
867 /* Don't show other options */
873 put_capture_hardware(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
877 case OPT_SHB_HARDWARE:
878 if (option != NULL && option->stringval != NULL) {
881 printf("%s", option->stringval);
886 /* Don't show other options */
892 put_capture_os(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
897 if (option != NULL && option->stringval != NULL) {
900 printf("%s", option->stringval);
905 /* Don't show other options */
911 put_capture_userappl(wtap_optionblock_t block _U_, guint option_id, wtap_opttype_e option_type _U_, wtap_option_type* option, void* user_data _U_)
915 case OPT_SHB_USERAPPL:
916 if (option != NULL && option->stringval != NULL) {
919 printf("%s", option->stringval);
924 /* Don't show other options */
930 print_stats_table(const gchar *filename, capture_info *cf_info)
932 const gchar *file_type_string, *file_encap_string;
934 /* Build printable strings for various stats */
935 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
936 file_encap_string = wtap_encap_string(cf_info->file_encap);
940 printf("%s", filename);
947 printf("%s", file_type_string);
951 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
952 * Output a line for each different encap with all fields repeated except
953 * the encapsulation field which has "Per Packet: ..." for each
954 * encapsulation type seen ?
956 if (cap_file_encap) {
959 printf("%s", file_encap_string);
963 if (cap_file_more_info) {
966 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
973 if (cf_info->snap_set)
974 printf("%u", cf_info->snaplen);
978 if (cf_info->snaplen_max_inferred > 0) {
981 printf("%u", cf_info->snaplen_min_inferred);
985 printf("%u", cf_info->snaplen_max_inferred);
1000 if (cap_packet_count) {
1003 printf("%u", cf_info->packet_count);
1007 if (cap_file_size) {
1010 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
1014 if (cap_data_size) {
1017 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
1024 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
1028 if (cap_start_time) {
1031 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
1038 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
1042 if (cap_data_rate_byte) {
1045 if (cf_info->times_known)
1046 printf("%.2f", cf_info->data_rate);
1052 if (cap_data_rate_bit) {
1055 if (cf_info->times_known)
1056 printf("%.2f", cf_info->data_rate*8);
1062 if (cap_packet_size) {
1065 printf("%.2f", cf_info->packet_size);
1069 if (cap_packet_rate) {
1072 if (cf_info->times_known)
1073 printf("%.2f", cf_info->packet_rate);
1079 #ifdef HAVE_LIBGCRYPT
1080 if (cap_file_hashes) {
1083 printf("%s", file_sha1);
1088 printf("%s", file_rmd160);
1093 printf("%s", file_md5);
1096 #endif /* HAVE_LIBGCRYPT */
1101 printf("%s", order_string(cf_info->order));
1106 * this is silly to put into a table format, but oh well
1107 * note that there may be *more than one* of each of these types
1111 wtap_optionblock_foreach_option(cf_info->shb, put_comment, NULL);
1114 if (cap_file_more_info) {
1115 wtap_optionblock_foreach_option(cf_info->shb, put_capture_hardware, NULL);
1117 wtap_optionblock_foreach_option(cf_info->shb, put_capture_os, NULL);
1119 wtap_optionblock_foreach_option(cf_info->shb, put_capture_userappl, NULL);
1126 cleanup_capture_info(capture_info *cf_info)
1129 g_assert(cf_info != NULL);
1131 g_free(cf_info->encap_counts);
1132 cf_info->encap_counts = NULL;
1134 g_free(cf_info->interface_ids);
1135 cf_info->interface_ids = NULL;
1137 if (cf_info->idb_info_strings) {
1138 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
1139 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
1142 g_array_free(cf_info->idb_info_strings, TRUE);
1144 cf_info->idb_info_strings = NULL;
1148 process_cap_file(wtap *wth, const char *filename)
1158 guint32 snaplen_min_inferred = 0xffffffff;
1159 guint32 snaplen_max_inferred = 0;
1160 const struct wtap_pkthdr *phdr;
1161 capture_info cf_info;
1162 gboolean have_times = TRUE;
1163 nstime_t start_time;
1164 int start_time_tsprec;
1166 int stop_time_tsprec;
1169 gboolean know_order = FALSE;
1170 order_t order = IN_ORDER;
1172 wtapng_iface_descriptions_t *idb_info;
1174 g_assert(wth != NULL);
1175 g_assert(filename != NULL);
1177 nstime_set_zero(&start_time);
1178 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1179 nstime_set_zero(&stop_time);
1180 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1181 nstime_set_zero(&cur_time);
1182 nstime_set_zero(&prev_time);
1184 cf_info.shb = wtap_file_get_shb(wth);
1186 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1188 idb_info = wtap_file_get_idb_info(wth);
1190 g_assert(idb_info->interface_data != NULL);
1192 cf_info.num_interfaces = idb_info->interface_data->len;
1193 cf_info.interface_ids = g_new0(guint32, cf_info.num_interfaces);
1194 cf_info.pkt_interface_id_unknown = 0;
1196 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1198 /* get IDB info strings */
1199 for (i = 0; i < cf_info.num_interfaces; i++) {
1200 const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1201 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1202 g_array_append_val(cf_info.idb_info_strings, s);
1208 /* Tally up data that we need to parse through the file to find */
1209 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1210 phdr = wtap_phdr(wth);
1211 if (phdr->presence_flags & WTAP_HAS_TS) {
1212 prev_time = cur_time;
1213 cur_time = phdr->ts;
1215 start_time = phdr->ts;
1216 start_time_tsprec = phdr->pkt_tsprec;
1217 stop_time = phdr->ts;
1218 stop_time_tsprec = phdr->pkt_tsprec;
1219 prev_time = phdr->ts;
1221 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1222 order = NOT_IN_ORDER;
1224 if (nstime_cmp(&cur_time, &start_time) < 0) {
1225 start_time = cur_time;
1226 start_time_tsprec = phdr->pkt_tsprec;
1228 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1229 stop_time = cur_time;
1230 stop_time_tsprec = phdr->pkt_tsprec;
1233 have_times = FALSE; /* at least one packet has no time stamp */
1234 if (order != NOT_IN_ORDER)
1235 order = ORDER_UNKNOWN;
1238 if (phdr->rec_type == REC_TYPE_PACKET) {
1242 /* If caplen < len for a rcd, then presumably */
1243 /* 'Limit packet capture length' was done for this rcd. */
1244 /* Keep track as to the min/max actual snapshot lengths */
1245 /* seen for this file. */
1246 if (phdr->caplen < phdr->len) {
1247 if (phdr->caplen < snaplen_min_inferred)
1248 snaplen_min_inferred = phdr->caplen;
1249 if (phdr->caplen > snaplen_max_inferred)
1250 snaplen_max_inferred = phdr->caplen;
1253 /* Per-packet encapsulation */
1254 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1255 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1256 cf_info.encap_counts[phdr->pkt_encap] += 1;
1258 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1259 phdr->pkt_encap, packet, filename);
1263 /* Packet interface_id info */
1264 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1265 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1266 if (phdr->interface_id < cf_info.num_interfaces) {
1267 cf_info.interface_ids[phdr->interface_id] += 1;
1270 cf_info.pkt_interface_id_unknown += 1;
1274 /* it's for interface_id 0 */
1275 if (cf_info.num_interfaces != 0) {
1276 cf_info.interface_ids[0] += 1;
1279 cf_info.pkt_interface_id_unknown += 1;
1288 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1289 packet, filename, wtap_strerror(err));
1290 if (err == WTAP_ERR_SHORT_READ) {
1291 /* Don't give up completely with this one. */
1294 " (will continue anyway, checksums might be incorrect)\n");
1296 if (err_info != NULL) {
1297 fprintf(stderr, "(%s)\n", err_info);
1301 cleanup_capture_info(&cf_info);
1307 size = wtap_file_size(wth, &err);
1310 "capinfos: Can't get size of \"%s\": %s.\n",
1311 filename, g_strerror(err));
1312 cleanup_capture_info(&cf_info);
1316 cf_info.filesize = size;
1319 cf_info.file_type = wtap_file_type_subtype(wth);
1320 cf_info.iscompressed = wtap_iscompressed(wth);
1322 /* File Encapsulation */
1323 cf_info.file_encap = wtap_file_encap(wth);
1325 cf_info.file_tsprec = wtap_file_tsprec(wth);
1327 /* Packet size limit (snaplen) */
1328 cf_info.snaplen = wtap_snapshot_length(wth);
1329 if (cf_info.snaplen > 0)
1330 cf_info.snap_set = TRUE;
1332 cf_info.snap_set = FALSE;
1334 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1335 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1338 cf_info.packet_count = packet;
1341 cf_info.times_known = have_times;
1342 cf_info.start_time = start_time;
1343 cf_info.start_time_tsprec = start_time_tsprec;
1344 cf_info.stop_time = stop_time;
1345 cf_info.stop_time_tsprec = stop_time_tsprec;
1346 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1347 /* Duration precision is the higher of the start and stop time precisions. */
1348 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1349 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1351 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1352 cf_info.know_order = know_order;
1353 cf_info.order = order;
1355 /* Number of packet bytes */
1356 cf_info.packet_bytes = bytes;
1358 cf_info.data_rate = 0.0;
1359 cf_info.packet_rate = 0.0;
1360 cf_info.packet_size = 0.0;
1363 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1364 if (delta_time > 0.0) {
1365 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1366 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1368 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1372 print_stats(filename, &cf_info);
1374 print_stats_table(filename, &cf_info);
1377 cleanup_capture_info(&cf_info);
1383 print_usage(FILE *output)
1385 fprintf(output, "\n");
1386 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1387 fprintf(output, "\n");
1388 fprintf(output, "General infos:\n");
1389 fprintf(output, " -t display the capture file type\n");
1390 fprintf(output, " -E display the capture file encapsulation\n");
1391 fprintf(output, " -I display the capture file interface information\n");
1392 fprintf(output, " -F display additional capture file information\n");
1393 #ifdef HAVE_LIBGCRYPT
1394 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1396 fprintf(output, " -k display the capture comment\n");
1397 fprintf(output, "\n");
1398 fprintf(output, "Size infos:\n");
1399 fprintf(output, " -c display the number of packets\n");
1400 fprintf(output, " -s display the size of the file (in bytes)\n");
1401 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1402 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1403 fprintf(output, "\n");
1404 fprintf(output, "Time infos:\n");
1405 fprintf(output, " -u display the capture duration (in seconds)\n");
1406 fprintf(output, " -a display the capture start time\n");
1407 fprintf(output, " -e display the capture end time\n");
1408 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1409 fprintf(output, " -S display start and end times as seconds\n");
1410 fprintf(output, "\n");
1411 fprintf(output, "Statistic infos:\n");
1412 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1413 fprintf(output, " -i display average data rate (in bits/sec)\n");
1414 fprintf(output, " -z display average packet size (in bytes)\n");
1415 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1416 fprintf(output, "\n");
1417 fprintf(output, "Output format:\n");
1418 fprintf(output, " -L generate long report (default)\n");
1419 fprintf(output, " -T generate table report\n");
1420 fprintf(output, " -M display machine-readable values in long reports\n");
1421 fprintf(output, "\n");
1422 fprintf(output, "Table report options:\n");
1423 fprintf(output, " -R generate header record (default)\n");
1424 fprintf(output, " -r do not generate header record\n");
1425 fprintf(output, "\n");
1426 fprintf(output, " -B separate infos with TAB character (default)\n");
1427 fprintf(output, " -m separate infos with comma (,) character\n");
1428 fprintf(output, " -b separate infos with SPACE character\n");
1429 fprintf(output, "\n");
1430 fprintf(output, " -N do not quote infos (default)\n");
1431 fprintf(output, " -q quote infos with single quotes (')\n");
1432 fprintf(output, " -Q quote infos with double quotes (\")\n");
1433 fprintf(output, "\n");
1434 fprintf(output, "Miscellaneous:\n");
1435 fprintf(output, " -h display this help and exit\n");
1436 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1437 fprintf(output, " -A generate all infos (default)\n");
1438 fprintf(output, "\n");
1439 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1440 fprintf(output, "or adding to earlier options.\n");
1441 fprintf(output, "\n");
1442 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1443 fprintf(output, "output format.\n");
1444 #ifndef HAVE_LIBGCRYPT
1445 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1451 * Don't report failures to load plugins because most (non-wiretap) plugins
1452 * *should* fail to load (because we're not linked against libwireshark and
1453 * dissector plugins need libwireshark).
1456 failure_message(const char *msg_format _U_, va_list ap _U_)
1462 #ifdef HAVE_LIBGCRYPT
1464 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1467 for (i = 0; i < (int) length; i++) {
1468 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1471 #endif /* HAVE_LIBGCRYPT */
1474 main(int argc, char *argv[])
1476 GString *comp_info_str;
1477 GString *runtime_info_str;
1482 int overall_error_status;
1483 static const struct option long_options[] = {
1484 {"help", no_argument, NULL, 'h'},
1485 {"version", no_argument, NULL, 'v'},
1491 char *init_progfile_dir_error;
1493 #ifdef HAVE_LIBGCRYPT
1495 char *hash_buf = NULL;
1496 gcry_md_hd_t hd = NULL;
1500 /* Set the C-language locale to the native environment. */
1501 setlocale(LC_ALL, "");
1503 /* Get the compile-time version information string */
1504 comp_info_str = get_compiled_version_info(NULL, NULL);
1506 /* Get the run-time version information string */
1507 runtime_info_str = get_runtime_version_info(NULL);
1509 /* Add it to the information to be reported on a crash. */
1510 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1515 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1518 arg_list_utf_16to8(argc, argv);
1519 create_app_running_mutex();
1523 * Get credential information for later use.
1525 init_process_policies();
1526 init_open_routines();
1529 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1530 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1531 g_free(init_progfile_dir_error);
1533 /* Register all the plugin types we have. */
1534 wtap_register_plugin_types(); /* Types known to libwiretap */
1536 init_report_err(failure_message, NULL, NULL, NULL);
1538 /* Scan for plugins. This does *not* call their registration routines;
1539 that's done later. */
1542 /* Register all libwiretap plugin modules. */
1543 register_all_wiretap_modules();
1547 /* Process the options */
1548 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1549 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1554 if (report_all_infos) disable_all_infos();
1555 cap_file_type = TRUE;
1559 if (report_all_infos) disable_all_infos();
1560 cap_file_encap = TRUE;
1564 if (report_all_infos) disable_all_infos();
1569 if (report_all_infos) disable_all_infos();
1570 cap_packet_count = TRUE;
1574 if (report_all_infos) disable_all_infos();
1575 cap_file_size = TRUE;
1579 if (report_all_infos) disable_all_infos();
1580 cap_data_size = TRUE;
1584 if (report_all_infos) disable_all_infos();
1585 cap_duration = TRUE;
1589 if (report_all_infos) disable_all_infos();
1590 cap_start_time = TRUE;
1594 if (report_all_infos) disable_all_infos();
1595 cap_end_time = TRUE;
1599 time_as_secs = TRUE;
1603 if (report_all_infos) disable_all_infos();
1604 cap_data_rate_byte = TRUE;
1608 if (report_all_infos) disable_all_infos();
1609 cap_data_rate_bit = TRUE;
1613 if (report_all_infos) disable_all_infos();
1614 cap_packet_size = TRUE;
1618 if (report_all_infos) disable_all_infos();
1619 cap_packet_rate = TRUE;
1622 #ifdef HAVE_LIBGCRYPT
1624 if (report_all_infos) disable_all_infos();
1625 cap_file_hashes = TRUE;
1630 if (report_all_infos) disable_all_infos();
1635 if (report_all_infos) disable_all_infos();
1640 if (report_all_infos) disable_all_infos();
1641 cap_file_more_info = TRUE;
1645 if (report_all_infos) disable_all_infos();
1646 cap_file_idb = TRUE;
1650 continue_after_wtap_open_offline_failure = FALSE;
1662 long_report = FALSE;
1666 machine_readable = TRUE;
1670 table_report_header = TRUE;
1674 table_report_header = FALSE;
1690 field_separator = '\t';
1694 field_separator = ',';
1698 field_separator = ' ';
1702 printf("Capinfos (Wireshark) %s\n"
1703 "Print various information (infos) about capture files.\n"
1704 "See https://www.wireshark.org for more information.\n",
1705 get_ws_vcs_version_info());
1706 print_usage(stdout);
1711 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1712 g_string_free(comp_info_str, TRUE);
1713 g_string_free(runtime_info_str, TRUE);
1717 case '?': /* Bad flag - print usage message */
1718 print_usage(stderr);
1724 if ((argc - optind) < 1) {
1725 print_usage(stderr);
1729 if (!long_report && table_report_header) {
1730 print_stats_table_header();
1733 #ifdef HAVE_LIBGCRYPT
1734 if (cap_file_hashes) {
1735 gcry_check_version(NULL);
1736 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1738 gcry_md_enable(hd, GCRY_MD_RMD160);
1739 gcry_md_enable(hd, GCRY_MD_MD5);
1741 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1745 overall_error_status = 0;
1747 for (opt = optind; opt < argc; opt++) {
1749 #ifdef HAVE_LIBGCRYPT
1750 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1751 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1752 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1754 if (cap_file_hashes) {
1755 fh = ws_fopen(argv[opt], "rb");
1757 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1758 gcry_md_write(hd, hash_buf, hash_bytes);
1761 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1762 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1763 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1766 if (hd) gcry_md_reset(hd);
1768 #endif /* HAVE_LIBGCRYPT */
1770 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1773 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1774 wtap_strerror(err));
1775 if (err_info != NULL) {
1776 fprintf(stderr, "(%s)\n", err_info);
1779 overall_error_status = 1; /* remember that an error has occurred */
1780 if (!continue_after_wtap_open_offline_failure)
1781 exit(1); /* error status */
1785 if ((opt > optind) && (long_report))
1787 status = process_cap_file(wth, argv[opt]);
1795 return overall_error_status;
1799 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1804 * indent-tabs-mode: nil
1807 * vi: set shiftwidth=2 tabstop=8 expandtab:
1808 * :indentSize=2:tabSize=8:noTabs=true: