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_ids array */
223 guint32 *interface_ids; /* 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 */
230 enable_all_infos(void)
232 report_all_infos = TRUE;
234 cap_file_type = TRUE;
235 cap_file_encap = TRUE;
237 cap_packet_count = TRUE;
238 cap_file_size = TRUE;
240 cap_file_more_info = TRUE;
243 cap_data_size = TRUE;
245 cap_start_time = TRUE;
249 cap_data_rate_byte = TRUE;
250 cap_data_rate_bit = TRUE;
251 cap_packet_size = TRUE;
252 cap_packet_rate = TRUE;
254 #ifdef HAVE_LIBGCRYPT
255 cap_file_hashes = TRUE;
256 #endif /* HAVE_LIBGCRYPT */
260 disable_all_infos(void)
262 report_all_infos = FALSE;
264 cap_file_type = FALSE;
265 cap_file_encap = FALSE;
267 cap_packet_count = FALSE;
268 cap_file_size = FALSE;
270 cap_file_more_info = FALSE;
271 cap_file_idb = FALSE;
273 cap_data_size = FALSE;
274 cap_duration = FALSE;
275 cap_start_time = FALSE;
276 cap_end_time = FALSE;
279 cap_data_rate_byte = FALSE;
280 cap_data_rate_bit = FALSE;
281 cap_packet_size = FALSE;
282 cap_packet_rate = FALSE;
284 #ifdef HAVE_LIBGCRYPT
285 cap_file_hashes = FALSE;
286 #endif /* HAVE_LIBGCRYPT */
290 order_string(order_t order)
304 return "???"; /* "cannot happen" (the next step is "Profit!") */
309 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
311 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
314 if (cf_info->times_known && cf_info->packet_count > 0) {
316 switch (tsprecision) {
318 case WTAP_TSPREC_SEC:
319 g_snprintf(time_string_buf, sizeof time_string_buf,
321 (unsigned long)timer->secs);
324 case WTAP_TSPREC_DSEC:
325 g_snprintf(time_string_buf, sizeof time_string_buf,
327 (unsigned long)timer->secs,
328 timer->nsecs / 100000000);
331 case WTAP_TSPREC_CSEC:
332 g_snprintf(time_string_buf, sizeof time_string_buf,
334 (unsigned long)timer->secs,
335 timer->nsecs / 10000000);
338 case WTAP_TSPREC_MSEC:
339 g_snprintf(time_string_buf, sizeof time_string_buf,
341 (unsigned long)timer->secs,
342 timer->nsecs / 1000000);
345 case WTAP_TSPREC_USEC:
346 g_snprintf(time_string_buf, sizeof time_string_buf,
348 (unsigned long)timer->secs,
349 timer->nsecs / 1000);
352 case WTAP_TSPREC_NSEC:
353 g_snprintf(time_string_buf, sizeof time_string_buf,
355 (unsigned long)timer->secs,
360 g_snprintf(time_string_buf, sizeof time_string_buf,
361 "Unknown precision %d",
365 return time_string_buf;
367 ti_tm = localtime(&timer->secs);
369 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
370 return time_string_buf;
372 switch (tsprecision) {
374 case WTAP_TSPREC_SEC:
375 g_snprintf(time_string_buf, sizeof time_string_buf,
376 "%04d-%02d-%02d %02d:%02d:%02d",
377 ti_tm->tm_year + 1900,
385 case WTAP_TSPREC_DSEC:
386 g_snprintf(time_string_buf, sizeof time_string_buf,
387 "%04d-%02d-%02d %02d:%02d:%02d.%01d",
388 ti_tm->tm_year + 1900,
394 timer->nsecs / 100000000);
397 case WTAP_TSPREC_CSEC:
398 g_snprintf(time_string_buf, sizeof time_string_buf,
399 "%04d-%02d-%02d %02d:%02d:%02d.%02d",
400 ti_tm->tm_year + 1900,
406 timer->nsecs / 10000000);
409 case WTAP_TSPREC_MSEC:
410 g_snprintf(time_string_buf, sizeof time_string_buf,
411 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
412 ti_tm->tm_year + 1900,
418 timer->nsecs / 1000000);
421 case WTAP_TSPREC_USEC:
422 g_snprintf(time_string_buf, sizeof time_string_buf,
423 "%04d-%02d-%02d %02d:%02d:%02d.%06d",
424 ti_tm->tm_year + 1900,
430 timer->nsecs / 1000);
433 case WTAP_TSPREC_NSEC:
434 g_snprintf(time_string_buf, sizeof time_string_buf,
435 "%04d-%02d-%02d %02d:%02d:%02d.%09d",
436 ti_tm->tm_year + 1900,
446 g_snprintf(time_string_buf, sizeof time_string_buf,
447 "Unknown precision %d",
451 return time_string_buf;
455 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
456 return time_string_buf;
460 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
462 const gchar *second = want_seconds ? " second" : "";
463 const gchar *plural = want_seconds ? "s" : "";
464 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
466 if (cf_info->times_known && cf_info->packet_count > 0) {
467 switch (tsprecision) {
469 case WTAP_TSPREC_SEC:
470 g_snprintf(time_string_buf, sizeof time_string_buf,
472 (unsigned long)timer->secs,
474 timer->secs == 1 ? "" : plural);
477 case WTAP_TSPREC_DSEC:
478 g_snprintf(time_string_buf, sizeof time_string_buf,
480 (unsigned long)timer->secs,
481 timer->nsecs / 100000000,
483 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
486 case WTAP_TSPREC_CSEC:
487 g_snprintf(time_string_buf, sizeof time_string_buf,
489 (unsigned long)timer->secs,
490 timer->nsecs / 10000000,
492 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
495 case WTAP_TSPREC_MSEC:
496 g_snprintf(time_string_buf, sizeof time_string_buf,
498 (unsigned long)timer->secs,
499 timer->nsecs / 1000000,
501 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
504 case WTAP_TSPREC_USEC:
505 g_snprintf(time_string_buf, sizeof time_string_buf,
507 (unsigned long)timer->secs,
510 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
513 case WTAP_TSPREC_NSEC:
514 g_snprintf(time_string_buf, sizeof time_string_buf,
516 (unsigned long)timer->secs,
519 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
523 g_snprintf(time_string_buf, sizeof time_string_buf,
524 "Unknown precision %d",
528 return time_string_buf;
531 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
532 return time_string_buf;
535 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
537 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
539 printf("%sn/a\n", text_p1);
542 /* multi-line comments would conflict with the formatting that capinfos uses
543 we replace linefeeds with spaces */
545 string_replace_newlines(gchar *str)
562 show_option_string(const char *prefix, const char *option_str)
566 if (option_str != NULL && option_str[0] != '\0') {
567 str = g_strdup(option_str);
568 string_replace_newlines(str);
569 printf("%s%s\n", prefix, str);
575 print_stats(const gchar *filename, capture_info *cf_info)
577 const gchar *file_type_string, *file_encap_string;
580 /* Build printable strings for various stats */
581 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
582 file_encap_string = wtap_encap_string(cf_info->file_encap);
584 if (filename) printf ("File name: %s\n", filename);
585 if (cap_file_type) printf ("File type: %s%s\n",
587 cf_info->iscompressed ? " (gzip compressed)" : "");
589 if (cap_file_encap) {
590 printf ("File encapsulation: %s\n", file_encap_string);
591 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
593 printf ("Encapsulation in use by packets (# of pkts):\n");
594 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
595 if (cf_info->encap_counts[i] > 0)
597 wtap_encap_string(i), cf_info->encap_counts[i]);
601 if (cap_file_more_info) {
602 printf ("File timestamp precision: %s (%d)\n",
603 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
606 if (cap_snaplen && cf_info->snap_set)
607 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
608 else if (cap_snaplen && !cf_info->snap_set)
609 printf ("Packet size limit: file hdr: (not set)\n");
610 if (cf_info->snaplen_max_inferred > 0) {
611 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
612 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
614 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
615 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
617 if (cap_packet_count) {
618 printf ("Number of packets: ");
619 if (machine_readable) {
620 printf ("%u\n", cf_info->packet_count);
622 size_string = format_size(cf_info->packet_count, format_size_unit_none);
623 printf ("%s\n", size_string);
628 printf ("File size: ");
629 if (machine_readable) {
630 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
632 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
633 printf ("%s\n", size_string);
638 printf ("Data size: ");
639 if (machine_readable) {
640 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
642 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
643 printf ("%s\n", size_string);
647 if (cf_info->times_known) {
648 if (cap_duration) /* XXX - shorten to hh:mm:ss */
649 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
651 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
653 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
654 if (cap_data_rate_byte) {
655 printf("Data byte rate: ");
656 if (machine_readable) {
657 print_value("", 2, " bytes/sec", cf_info->data_rate);
659 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
660 printf ("%s\n", size_string);
664 if (cap_data_rate_bit) {
665 printf("Data bit rate: ");
666 if (machine_readable) {
667 print_value("", 2, " bits/sec", cf_info->data_rate*8);
669 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
670 printf ("%s\n", size_string);
675 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
676 if (cf_info->times_known) {
677 if (cap_packet_rate) {
678 printf("Average packet rate: ");
679 if (machine_readable) {
680 print_value("", 2, " packets/sec", cf_info->packet_rate);
682 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
683 printf ("%s\n", size_string);
688 #ifdef HAVE_LIBGCRYPT
689 if (cap_file_hashes) {
690 printf ("SHA1: %s\n", file_sha1);
691 printf ("RIPEMD160: %s\n", file_rmd160);
692 printf ("MD5: %s\n", file_md5);
694 #endif /* HAVE_LIBGCRYPT */
695 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
697 if (cf_info->shb != NULL) {
702 wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts);
703 for (i = 0; i < opts->len; i++) {
704 show_option_string("Capture comment: ", g_array_index(opts, char *, i));
706 g_array_free(opts, TRUE);
708 if (cap_file_more_info) {
711 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str);
712 show_option_string("Capture hardware: ", str);
713 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str);
714 show_option_string("Capture oper-sys: ", str);
715 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str);
716 show_option_string("Capture application: ", str);
719 if (cap_file_idb && cf_info->num_interfaces != 0) {
721 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
722 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
723 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
724 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
725 printf ("Interface #%u info:\n", i);
727 printf (" Number of packets = %u\n", cf_info->interface_ids[i]);
736 if (field_separator) putchar(field_separator);
742 if (quote_char) putchar(quote_char);
746 print_stats_table_header_label(const gchar *label)
755 print_stats_table_header(void)
761 if (cap_file_type) print_stats_table_header_label("File type");
762 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
763 if (cap_file_more_info) print_stats_table_header_label("File time precision");
764 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
765 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
766 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
767 if (cap_packet_count) print_stats_table_header_label("Number of packets");
768 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
769 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
770 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
771 if (cap_start_time) print_stats_table_header_label("Start time");
772 if (cap_end_time) print_stats_table_header_label("End time");
773 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
774 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
775 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
776 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
777 #ifdef HAVE_LIBGCRYPT
778 if (cap_file_hashes) {
779 print_stats_table_header_label("SHA1");
780 print_stats_table_header_label("RIPEMD160");
781 print_stats_table_header_label("MD5");
783 #endif /* HAVE_LIBGCRYPT */
784 if (cap_order) print_stats_table_header_label("Strict time order");
785 if (cap_comment) print_stats_table_header_label("Capture comment");
786 if (cap_file_more_info) {
787 print_stats_table_header_label("Capture hardware");
788 print_stats_table_header_label("Capture oper-sys");
789 print_stats_table_header_label("Capture application");
796 print_stats_table(const gchar *filename, capture_info *cf_info)
798 const gchar *file_type_string, *file_encap_string;
800 /* Build printable strings for various stats */
801 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
802 file_encap_string = wtap_encap_string(cf_info->file_encap);
806 printf("%s", filename);
813 printf("%s", file_type_string);
817 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
818 * Output a line for each different encap with all fields repeated except
819 * the encapsulation field which has "Per Packet: ..." for each
820 * encapsulation type seen ?
822 if (cap_file_encap) {
825 printf("%s", file_encap_string);
829 if (cap_file_more_info) {
832 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
839 if (cf_info->snap_set)
840 printf("%u", cf_info->snaplen);
844 if (cf_info->snaplen_max_inferred > 0) {
847 printf("%u", cf_info->snaplen_min_inferred);
851 printf("%u", cf_info->snaplen_max_inferred);
866 if (cap_packet_count) {
869 printf("%u", cf_info->packet_count);
876 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
883 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
890 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
894 if (cap_start_time) {
897 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
904 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
908 if (cap_data_rate_byte) {
911 if (cf_info->times_known)
912 printf("%.2f", cf_info->data_rate);
918 if (cap_data_rate_bit) {
921 if (cf_info->times_known)
922 printf("%.2f", cf_info->data_rate*8);
928 if (cap_packet_size) {
931 printf("%.2f", cf_info->packet_size);
935 if (cap_packet_rate) {
938 if (cf_info->times_known)
939 printf("%.2f", cf_info->packet_rate);
945 #ifdef HAVE_LIBGCRYPT
946 if (cap_file_hashes) {
949 printf("%s", file_sha1);
954 printf("%s", file_rmd160);
959 printf("%s", file_md5);
962 #endif /* HAVE_LIBGCRYPT */
967 printf("%s", order_string(cf_info->order));
971 if (cf_info->shb != NULL) {
973 * this is silly to put into a table format, but oh well
974 * note that there may be *more than one* of each of these types
981 wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts);
982 for (i = 0; i < opts->len; i++) {
983 const char *opt_comment = g_array_index(opts, char *, i);
985 if (opt_comment != NULL) {
988 printf("%s", opt_comment);
992 g_array_free(opts, TRUE);
995 if (cap_file_more_info) {
998 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str);
1005 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str);
1012 wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str);
1026 cleanup_capture_info(capture_info *cf_info)
1029 g_assert(cf_info != NULL);
1031 g_free(cf_info->encap_counts);
1032 cf_info->encap_counts = NULL;
1034 g_free(cf_info->interface_ids);
1035 cf_info->interface_ids = NULL;
1037 if (cf_info->idb_info_strings) {
1038 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
1039 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
1042 g_array_free(cf_info->idb_info_strings, TRUE);
1044 cf_info->idb_info_strings = NULL;
1048 process_cap_file(wtap *wth, const char *filename)
1058 guint32 snaplen_min_inferred = 0xffffffff;
1059 guint32 snaplen_max_inferred = 0;
1060 const struct wtap_pkthdr *phdr;
1061 capture_info cf_info;
1062 gboolean have_times = TRUE;
1063 nstime_t start_time;
1064 int start_time_tsprec;
1066 int stop_time_tsprec;
1069 gboolean know_order = FALSE;
1070 order_t order = IN_ORDER;
1072 wtapng_iface_descriptions_t *idb_info;
1074 g_assert(wth != NULL);
1075 g_assert(filename != NULL);
1077 nstime_set_zero(&start_time);
1078 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1079 nstime_set_zero(&stop_time);
1080 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1081 nstime_set_zero(&cur_time);
1082 nstime_set_zero(&prev_time);
1084 cf_info.shb = wtap_file_get_shb(wth);
1086 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1088 idb_info = wtap_file_get_idb_info(wth);
1090 g_assert(idb_info->interface_data != NULL);
1092 cf_info.num_interfaces = idb_info->interface_data->len;
1093 cf_info.interface_ids = g_new0(guint32, cf_info.num_interfaces);
1094 cf_info.pkt_interface_id_unknown = 0;
1096 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1098 /* get IDB info strings */
1099 for (i = 0; i < cf_info.num_interfaces; i++) {
1100 const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1101 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1102 g_array_append_val(cf_info.idb_info_strings, s);
1108 /* Tally up data that we need to parse through the file to find */
1109 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1110 phdr = wtap_phdr(wth);
1111 if (phdr->presence_flags & WTAP_HAS_TS) {
1112 prev_time = cur_time;
1113 cur_time = phdr->ts;
1115 start_time = phdr->ts;
1116 start_time_tsprec = phdr->pkt_tsprec;
1117 stop_time = phdr->ts;
1118 stop_time_tsprec = phdr->pkt_tsprec;
1119 prev_time = phdr->ts;
1121 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1122 order = NOT_IN_ORDER;
1124 if (nstime_cmp(&cur_time, &start_time) < 0) {
1125 start_time = cur_time;
1126 start_time_tsprec = phdr->pkt_tsprec;
1128 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1129 stop_time = cur_time;
1130 stop_time_tsprec = phdr->pkt_tsprec;
1133 have_times = FALSE; /* at least one packet has no time stamp */
1134 if (order != NOT_IN_ORDER)
1135 order = ORDER_UNKNOWN;
1138 if (phdr->rec_type == REC_TYPE_PACKET) {
1142 /* If caplen < len for a rcd, then presumably */
1143 /* 'Limit packet capture length' was done for this rcd. */
1144 /* Keep track as to the min/max actual snapshot lengths */
1145 /* seen for this file. */
1146 if (phdr->caplen < phdr->len) {
1147 if (phdr->caplen < snaplen_min_inferred)
1148 snaplen_min_inferred = phdr->caplen;
1149 if (phdr->caplen > snaplen_max_inferred)
1150 snaplen_max_inferred = phdr->caplen;
1153 /* Per-packet encapsulation */
1154 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1155 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1156 cf_info.encap_counts[phdr->pkt_encap] += 1;
1158 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1159 phdr->pkt_encap, packet, filename);
1163 /* Packet interface_id info */
1164 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1165 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1166 if (phdr->interface_id < cf_info.num_interfaces) {
1167 cf_info.interface_ids[phdr->interface_id] += 1;
1170 cf_info.pkt_interface_id_unknown += 1;
1174 /* it's for interface_id 0 */
1175 if (cf_info.num_interfaces != 0) {
1176 cf_info.interface_ids[0] += 1;
1179 cf_info.pkt_interface_id_unknown += 1;
1188 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1189 packet, filename, wtap_strerror(err));
1190 if (err == WTAP_ERR_SHORT_READ) {
1191 /* Don't give up completely with this one. */
1194 " (will continue anyway, checksums might be incorrect)\n");
1196 if (err_info != NULL) {
1197 fprintf(stderr, "(%s)\n", err_info);
1201 cleanup_capture_info(&cf_info);
1207 size = wtap_file_size(wth, &err);
1210 "capinfos: Can't get size of \"%s\": %s.\n",
1211 filename, g_strerror(err));
1212 cleanup_capture_info(&cf_info);
1216 cf_info.filesize = size;
1219 cf_info.file_type = wtap_file_type_subtype(wth);
1220 cf_info.iscompressed = wtap_iscompressed(wth);
1222 /* File Encapsulation */
1223 cf_info.file_encap = wtap_file_encap(wth);
1225 cf_info.file_tsprec = wtap_file_tsprec(wth);
1227 /* Packet size limit (snaplen) */
1228 cf_info.snaplen = wtap_snapshot_length(wth);
1229 if (cf_info.snaplen > 0)
1230 cf_info.snap_set = TRUE;
1232 cf_info.snap_set = FALSE;
1234 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1235 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1238 cf_info.packet_count = packet;
1241 cf_info.times_known = have_times;
1242 cf_info.start_time = start_time;
1243 cf_info.start_time_tsprec = start_time_tsprec;
1244 cf_info.stop_time = stop_time;
1245 cf_info.stop_time_tsprec = stop_time_tsprec;
1246 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1247 /* Duration precision is the higher of the start and stop time precisions. */
1248 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1249 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1251 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1252 cf_info.know_order = know_order;
1253 cf_info.order = order;
1255 /* Number of packet bytes */
1256 cf_info.packet_bytes = bytes;
1258 cf_info.data_rate = 0.0;
1259 cf_info.packet_rate = 0.0;
1260 cf_info.packet_size = 0.0;
1263 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1264 if (delta_time > 0.0) {
1265 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1266 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1268 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1272 print_stats(filename, &cf_info);
1274 print_stats_table(filename, &cf_info);
1277 cleanup_capture_info(&cf_info);
1283 print_usage(FILE *output)
1285 fprintf(output, "\n");
1286 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1287 fprintf(output, "\n");
1288 fprintf(output, "General infos:\n");
1289 fprintf(output, " -t display the capture file type\n");
1290 fprintf(output, " -E display the capture file encapsulation\n");
1291 fprintf(output, " -I display the capture file interface information\n");
1292 fprintf(output, " -F display additional capture file information\n");
1293 #ifdef HAVE_LIBGCRYPT
1294 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1296 fprintf(output, " -k display the capture comment\n");
1297 fprintf(output, "\n");
1298 fprintf(output, "Size infos:\n");
1299 fprintf(output, " -c display the number of packets\n");
1300 fprintf(output, " -s display the size of the file (in bytes)\n");
1301 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1302 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1303 fprintf(output, "\n");
1304 fprintf(output, "Time infos:\n");
1305 fprintf(output, " -u display the capture duration (in seconds)\n");
1306 fprintf(output, " -a display the capture start time\n");
1307 fprintf(output, " -e display the capture end time\n");
1308 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1309 fprintf(output, " -S display start and end times as seconds\n");
1310 fprintf(output, "\n");
1311 fprintf(output, "Statistic infos:\n");
1312 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1313 fprintf(output, " -i display average data rate (in bits/sec)\n");
1314 fprintf(output, " -z display average packet size (in bytes)\n");
1315 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1316 fprintf(output, "\n");
1317 fprintf(output, "Output format:\n");
1318 fprintf(output, " -L generate long report (default)\n");
1319 fprintf(output, " -T generate table report\n");
1320 fprintf(output, " -M display machine-readable values in long reports\n");
1321 fprintf(output, "\n");
1322 fprintf(output, "Table report options:\n");
1323 fprintf(output, " -R generate header record (default)\n");
1324 fprintf(output, " -r do not generate header record\n");
1325 fprintf(output, "\n");
1326 fprintf(output, " -B separate infos with TAB character (default)\n");
1327 fprintf(output, " -m separate infos with comma (,) character\n");
1328 fprintf(output, " -b separate infos with SPACE character\n");
1329 fprintf(output, "\n");
1330 fprintf(output, " -N do not quote infos (default)\n");
1331 fprintf(output, " -q quote infos with single quotes (')\n");
1332 fprintf(output, " -Q quote infos with double quotes (\")\n");
1333 fprintf(output, "\n");
1334 fprintf(output, "Miscellaneous:\n");
1335 fprintf(output, " -h display this help and exit\n");
1336 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1337 fprintf(output, " -A generate all infos (default)\n");
1338 fprintf(output, "\n");
1339 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1340 fprintf(output, "or adding to earlier options.\n");
1341 fprintf(output, "\n");
1342 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1343 fprintf(output, "output format.\n");
1344 #ifndef HAVE_LIBGCRYPT
1345 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1351 * Don't report failures to load plugins because most (non-wiretap) plugins
1352 * *should* fail to load (because we're not linked against libwireshark and
1353 * dissector plugins need libwireshark).
1356 failure_message(const char *msg_format _U_, va_list ap _U_)
1362 #ifdef HAVE_LIBGCRYPT
1364 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1367 for (i = 0; i < (int) length; i++) {
1368 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1371 #endif /* HAVE_LIBGCRYPT */
1374 main(int argc, char *argv[])
1376 GString *comp_info_str;
1377 GString *runtime_info_str;
1382 int overall_error_status;
1383 static const struct option long_options[] = {
1384 {"help", no_argument, NULL, 'h'},
1385 {"version", no_argument, NULL, 'v'},
1391 char *init_progfile_dir_error;
1393 #ifdef HAVE_LIBGCRYPT
1395 char *hash_buf = NULL;
1396 gcry_md_hd_t hd = NULL;
1400 /* Set the C-language locale to the native environment. */
1401 setlocale(LC_ALL, "");
1403 /* Get the compile-time version information string */
1404 comp_info_str = get_compiled_version_info(NULL, NULL);
1406 /* Get the run-time version information string */
1407 runtime_info_str = get_runtime_version_info(NULL);
1409 /* Add it to the information to be reported on a crash. */
1410 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1415 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1418 arg_list_utf_16to8(argc, argv);
1419 create_app_running_mutex();
1423 * Get credential information for later use.
1425 init_process_policies();
1426 init_open_routines();
1429 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1430 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1431 g_free(init_progfile_dir_error);
1433 /* Register all the plugin types we have. */
1434 wtap_register_plugin_types(); /* Types known to libwiretap */
1436 init_report_err(failure_message, NULL, NULL, NULL);
1438 /* Scan for plugins. This does *not* call their registration routines;
1439 that's done later. */
1442 /* Register all libwiretap plugin modules. */
1443 register_all_wiretap_modules();
1447 /* Process the options */
1448 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1449 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1454 if (report_all_infos) disable_all_infos();
1455 cap_file_type = TRUE;
1459 if (report_all_infos) disable_all_infos();
1460 cap_file_encap = TRUE;
1464 if (report_all_infos) disable_all_infos();
1469 if (report_all_infos) disable_all_infos();
1470 cap_packet_count = TRUE;
1474 if (report_all_infos) disable_all_infos();
1475 cap_file_size = TRUE;
1479 if (report_all_infos) disable_all_infos();
1480 cap_data_size = TRUE;
1484 if (report_all_infos) disable_all_infos();
1485 cap_duration = TRUE;
1489 if (report_all_infos) disable_all_infos();
1490 cap_start_time = TRUE;
1494 if (report_all_infos) disable_all_infos();
1495 cap_end_time = TRUE;
1499 time_as_secs = TRUE;
1503 if (report_all_infos) disable_all_infos();
1504 cap_data_rate_byte = TRUE;
1508 if (report_all_infos) disable_all_infos();
1509 cap_data_rate_bit = TRUE;
1513 if (report_all_infos) disable_all_infos();
1514 cap_packet_size = TRUE;
1518 if (report_all_infos) disable_all_infos();
1519 cap_packet_rate = TRUE;
1522 #ifdef HAVE_LIBGCRYPT
1524 if (report_all_infos) disable_all_infos();
1525 cap_file_hashes = TRUE;
1530 if (report_all_infos) disable_all_infos();
1535 if (report_all_infos) disable_all_infos();
1540 if (report_all_infos) disable_all_infos();
1541 cap_file_more_info = TRUE;
1545 if (report_all_infos) disable_all_infos();
1546 cap_file_idb = TRUE;
1550 continue_after_wtap_open_offline_failure = FALSE;
1562 long_report = FALSE;
1566 machine_readable = TRUE;
1570 table_report_header = TRUE;
1574 table_report_header = FALSE;
1590 field_separator = '\t';
1594 field_separator = ',';
1598 field_separator = ' ';
1602 printf("Capinfos (Wireshark) %s\n"
1603 "Print various information (infos) about capture files.\n"
1604 "See https://www.wireshark.org for more information.\n",
1605 get_ws_vcs_version_info());
1606 print_usage(stdout);
1611 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1612 g_string_free(comp_info_str, TRUE);
1613 g_string_free(runtime_info_str, TRUE);
1617 case '?': /* Bad flag - print usage message */
1618 print_usage(stderr);
1624 if ((argc - optind) < 1) {
1625 print_usage(stderr);
1629 if (!long_report && table_report_header) {
1630 print_stats_table_header();
1633 #ifdef HAVE_LIBGCRYPT
1634 if (cap_file_hashes) {
1635 gcry_check_version(NULL);
1636 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1638 gcry_md_enable(hd, GCRY_MD_RMD160);
1639 gcry_md_enable(hd, GCRY_MD_MD5);
1641 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1645 overall_error_status = 0;
1647 for (opt = optind; opt < argc; opt++) {
1649 #ifdef HAVE_LIBGCRYPT
1650 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1651 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1652 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1654 if (cap_file_hashes) {
1655 fh = ws_fopen(argv[opt], "rb");
1657 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1658 gcry_md_write(hd, hash_buf, hash_bytes);
1661 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1662 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1663 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1666 if (hd) gcry_md_reset(hd);
1668 #endif /* HAVE_LIBGCRYPT */
1670 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1673 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1674 wtap_strerror(err));
1675 if (err_info != NULL) {
1676 fprintf(stderr, "(%s)\n", err_info);
1679 overall_error_status = 1; /* remember that an error has occurred */
1680 if (!continue_after_wtap_open_offline_failure)
1681 exit(1); /* error status */
1685 if ((opt > optind) && (long_report))
1687 status = process_cap_file(wth, argv[opt]);
1695 return overall_error_status;
1699 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1704 * indent-tabs-mode: nil
1707 * vi: set shiftwidth=2 tabstop=8 expandtab:
1708 * :indentSize=2:tabSize=8:noTabs=true: