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;
203 guint64 packet_bytes;
204 gboolean times_known;
206 int start_time_tsprec;
208 int stop_time_tsprec;
209 guint32 packet_count;
210 gboolean snap_set; /* If set in capture file header */
211 guint32 snaplen; /* value from the capture file header */
212 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
213 guint32 snaplen_max_inferred; /* ... */
214 gboolean drops_known;
221 double data_rate; /* in bytes */
225 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
227 guint num_interfaces; /* number of IDBs, and thus size of interface_ids array */
228 guint32 *interface_ids; /* array of per_packet interface_id counts; one entry per file IDB */
229 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
230 GArray *idb_info_strings; /* array of IDB info strings */
235 enable_all_infos(void)
237 report_all_infos = TRUE;
239 cap_file_type = TRUE;
240 cap_file_encap = TRUE;
242 cap_packet_count = TRUE;
243 cap_file_size = TRUE;
245 cap_file_more_info = TRUE;
248 cap_data_size = TRUE;
250 cap_start_time = TRUE;
254 cap_data_rate_byte = TRUE;
255 cap_data_rate_bit = TRUE;
256 cap_packet_size = TRUE;
257 cap_packet_rate = TRUE;
259 #ifdef HAVE_LIBGCRYPT
260 cap_file_hashes = TRUE;
261 #endif /* HAVE_LIBGCRYPT */
265 disable_all_infos(void)
267 report_all_infos = FALSE;
269 cap_file_type = FALSE;
270 cap_file_encap = FALSE;
272 cap_packet_count = FALSE;
273 cap_file_size = FALSE;
275 cap_file_more_info = FALSE;
276 cap_file_idb = FALSE;
278 cap_data_size = FALSE;
279 cap_duration = FALSE;
280 cap_start_time = FALSE;
281 cap_end_time = FALSE;
284 cap_data_rate_byte = FALSE;
285 cap_data_rate_bit = FALSE;
286 cap_packet_size = FALSE;
287 cap_packet_rate = FALSE;
289 #ifdef HAVE_LIBGCRYPT
290 cap_file_hashes = FALSE;
291 #endif /* HAVE_LIBGCRYPT */
295 order_string(order_t order)
309 return "???"; /* "cannot happen" (the next step is "Profit!") */
314 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
316 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
319 if (cf_info->times_known && cf_info->packet_count > 0) {
321 switch (tsprecision) {
323 case WTAP_TSPREC_SEC:
324 g_snprintf(time_string_buf, sizeof time_string_buf,
326 (unsigned long)timer->secs);
329 case WTAP_TSPREC_DSEC:
330 g_snprintf(time_string_buf, sizeof time_string_buf,
332 (unsigned long)timer->secs,
333 timer->nsecs / 100000000);
336 case WTAP_TSPREC_CSEC:
337 g_snprintf(time_string_buf, sizeof time_string_buf,
339 (unsigned long)timer->secs,
340 timer->nsecs / 10000000);
343 case WTAP_TSPREC_MSEC:
344 g_snprintf(time_string_buf, sizeof time_string_buf,
346 (unsigned long)timer->secs,
347 timer->nsecs / 1000000);
350 case WTAP_TSPREC_USEC:
351 g_snprintf(time_string_buf, sizeof time_string_buf,
353 (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,
365 g_snprintf(time_string_buf, sizeof time_string_buf,
366 "Unknown precision %d",
370 return time_string_buf;
372 ti_tm = localtime(&timer->secs);
374 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
375 return time_string_buf;
377 switch (tsprecision) {
379 case WTAP_TSPREC_SEC:
380 g_snprintf(time_string_buf, sizeof time_string_buf,
381 "%04d-%02d-%02d %02d:%02d:%02d",
382 ti_tm->tm_year + 1900,
390 case WTAP_TSPREC_DSEC:
391 g_snprintf(time_string_buf, sizeof time_string_buf,
392 "%04d-%02d-%02d %02d:%02d:%02d.%01d",
393 ti_tm->tm_year + 1900,
399 timer->nsecs / 100000000);
402 case WTAP_TSPREC_CSEC:
403 g_snprintf(time_string_buf, sizeof time_string_buf,
404 "%04d-%02d-%02d %02d:%02d:%02d.%02d",
405 ti_tm->tm_year + 1900,
411 timer->nsecs / 10000000);
414 case WTAP_TSPREC_MSEC:
415 g_snprintf(time_string_buf, sizeof time_string_buf,
416 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
417 ti_tm->tm_year + 1900,
423 timer->nsecs / 1000000);
426 case WTAP_TSPREC_USEC:
427 g_snprintf(time_string_buf, sizeof time_string_buf,
428 "%04d-%02d-%02d %02d:%02d:%02d.%06d",
429 ti_tm->tm_year + 1900,
435 timer->nsecs / 1000);
438 case WTAP_TSPREC_NSEC:
439 g_snprintf(time_string_buf, sizeof time_string_buf,
440 "%04d-%02d-%02d %02d:%02d:%02d.%09d",
441 ti_tm->tm_year + 1900,
451 g_snprintf(time_string_buf, sizeof time_string_buf,
452 "Unknown precision %d",
456 return time_string_buf;
460 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
461 return time_string_buf;
465 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
467 const gchar *second = want_seconds ? " second" : "";
468 const gchar *plural = want_seconds ? "s" : "";
469 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
471 if (cf_info->times_known && cf_info->packet_count > 0) {
472 switch (tsprecision) {
474 case WTAP_TSPREC_SEC:
475 g_snprintf(time_string_buf, sizeof time_string_buf,
477 (unsigned long)timer->secs,
479 timer->secs == 1 ? "" : plural);
482 case WTAP_TSPREC_DSEC:
483 g_snprintf(time_string_buf, sizeof time_string_buf,
485 (unsigned long)timer->secs,
486 timer->nsecs / 100000000,
488 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
491 case WTAP_TSPREC_CSEC:
492 g_snprintf(time_string_buf, sizeof time_string_buf,
494 (unsigned long)timer->secs,
495 timer->nsecs / 10000000,
497 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
500 case WTAP_TSPREC_MSEC:
501 g_snprintf(time_string_buf, sizeof time_string_buf,
503 (unsigned long)timer->secs,
504 timer->nsecs / 1000000,
506 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
509 case WTAP_TSPREC_USEC:
510 g_snprintf(time_string_buf, sizeof time_string_buf,
512 (unsigned long)timer->secs,
515 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
518 case WTAP_TSPREC_NSEC:
519 g_snprintf(time_string_buf, sizeof time_string_buf,
521 (unsigned long)timer->secs,
524 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
528 g_snprintf(time_string_buf, sizeof time_string_buf,
529 "Unknown precision %d",
533 return time_string_buf;
536 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
537 return time_string_buf;
540 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
542 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
544 printf("%sn/a\n", text_p1);
548 print_stats(const gchar *filename, capture_info *cf_info)
550 const gchar *file_type_string, *file_encap_string;
553 /* Build printable strings for various stats */
554 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
555 file_encap_string = wtap_encap_string(cf_info->file_encap);
557 if (filename) printf ("File name: %s\n", filename);
558 if (cap_file_type) printf ("File type: %s%s\n",
560 cf_info->iscompressed ? " (gzip compressed)" : "");
562 if (cap_file_encap) {
563 printf ("File encapsulation: %s\n", file_encap_string);
564 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
566 printf ("Encapsulation in use by packets (# of pkts):\n");
567 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
568 if (cf_info->encap_counts[i] > 0)
570 wtap_encap_string(i), cf_info->encap_counts[i]);
574 if (cap_file_more_info) {
575 printf ("File timestamp precision: %s (%d)\n",
576 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
579 if (cap_snaplen && cf_info->snap_set)
580 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
581 else if (cap_snaplen && !cf_info->snap_set)
582 printf ("Packet size limit: file hdr: (not set)\n");
583 if (cf_info->snaplen_max_inferred > 0) {
584 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
585 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
587 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
588 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
590 if (cap_packet_count) {
591 printf ("Number of packets: ");
592 if (machine_readable) {
593 printf ("%u\n", cf_info->packet_count);
595 size_string = format_size(cf_info->packet_count, format_size_unit_none);
596 printf ("%s\n", size_string);
601 printf ("File size: ");
602 if (machine_readable) {
603 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
605 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
606 printf ("%s\n", size_string);
611 printf ("Data size: ");
612 if (machine_readable) {
613 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
615 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
616 printf ("%s\n", size_string);
620 if (cf_info->times_known) {
621 if (cap_duration) /* XXX - shorten to hh:mm:ss */
622 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
624 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
626 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
627 if (cap_data_rate_byte) {
628 printf("Data byte rate: ");
629 if (machine_readable) {
630 print_value("", 2, " bytes/sec", cf_info->data_rate);
632 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
633 printf ("%s\n", size_string);
637 if (cap_data_rate_bit) {
638 printf("Data bit rate: ");
639 if (machine_readable) {
640 print_value("", 2, " bits/sec", cf_info->data_rate*8);
642 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
643 printf ("%s\n", size_string);
648 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
649 if (cf_info->times_known) {
650 if (cap_packet_rate) {
651 printf("Average packet rate: ");
652 if (machine_readable) {
653 print_value("", 2, " packets/sec", cf_info->packet_rate);
655 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
656 printf ("%s\n", size_string);
661 #ifdef HAVE_LIBGCRYPT
662 if (cap_file_hashes) {
663 printf ("SHA1: %s\n", file_sha1);
664 printf ("RIPEMD160: %s\n", file_rmd160);
665 printf ("MD5: %s\n", file_md5);
667 #endif /* HAVE_LIBGCRYPT */
668 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
669 if (cap_comment && cf_info->comment)
670 printf ("Capture comment: %s\n", cf_info->comment);
671 if (cap_file_more_info) {
672 if (cf_info->hardware)
673 printf ("Capture hardware: %s\n", cf_info->hardware);
675 printf ("Capture oper-sys: %s\n", cf_info->os);
676 if (cf_info->usr_appl)
677 printf ("Capture application: %s\n", cf_info->usr_appl);
680 if (cap_file_idb && cf_info->num_interfaces != 0) {
682 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
683 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
684 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
685 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
686 printf ("Interface #%u info:\n", i);
688 printf (" Number of packets = %u\n", cf_info->interface_ids[i]);
696 if (field_separator) putchar(field_separator);
702 if (quote_char) putchar(quote_char);
706 print_stats_table_header_label(const gchar *label)
715 print_stats_table_header(void)
721 if (cap_file_type) print_stats_table_header_label("File type");
722 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
723 if (cap_file_more_info) print_stats_table_header_label("File time precision");
724 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
725 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
726 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
727 if (cap_packet_count) print_stats_table_header_label("Number of packets");
728 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
729 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
730 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
731 if (cap_start_time) print_stats_table_header_label("Start time");
732 if (cap_end_time) print_stats_table_header_label("End time");
733 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
734 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
735 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
736 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
737 #ifdef HAVE_LIBGCRYPT
738 if (cap_file_hashes) {
739 print_stats_table_header_label("SHA1");
740 print_stats_table_header_label("RIPEMD160");
741 print_stats_table_header_label("MD5");
743 #endif /* HAVE_LIBGCRYPT */
744 if (cap_order) print_stats_table_header_label("Strict time order");
745 if (cap_comment) print_stats_table_header_label("Capture comment");
746 if (cap_file_more_info) {
747 print_stats_table_header_label("Capture hardware");
748 print_stats_table_header_label("Capture oper-sys");
749 print_stats_table_header_label("Capture application");
756 print_stats_table(const gchar *filename, capture_info *cf_info)
758 const gchar *file_type_string, *file_encap_string;
760 /* Build printable strings for various stats */
761 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
762 file_encap_string = wtap_encap_string(cf_info->file_encap);
766 printf("%s", filename);
773 printf("%s", file_type_string);
777 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
778 * Output a line for each different encap with all fields repeated except
779 * the encapsulation field which has "Per Packet: ..." for each
780 * encapsulation type seen ?
782 if (cap_file_encap) {
785 printf("%s", file_encap_string);
789 if (cap_file_more_info) {
792 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
799 if (cf_info->snap_set)
800 printf("%u", cf_info->snaplen);
804 if (cf_info->snaplen_max_inferred > 0) {
807 printf("%u", cf_info->snaplen_min_inferred);
811 printf("%u", cf_info->snaplen_max_inferred);
826 if (cap_packet_count) {
829 printf("%u", cf_info->packet_count);
836 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
843 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
850 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
854 if (cap_start_time) {
857 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
864 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
868 if (cap_data_rate_byte) {
871 if (cf_info->times_known)
872 printf("%.2f", cf_info->data_rate);
878 if (cap_data_rate_bit) {
881 if (cf_info->times_known)
882 printf("%.2f", cf_info->data_rate*8);
888 if (cap_packet_size) {
891 printf("%.2f", cf_info->packet_size);
895 if (cap_packet_rate) {
898 if (cf_info->times_known)
899 printf("%.2f", cf_info->packet_rate);
905 #ifdef HAVE_LIBGCRYPT
906 if (cap_file_hashes) {
909 printf("%s", file_sha1);
914 printf("%s", file_rmd160);
919 printf("%s", file_md5);
922 #endif /* HAVE_LIBGCRYPT */
927 printf("%s", order_string(cf_info->order));
931 /* this is silly to put into a table format, but oh well */
935 printf("%s", cf_info->comment);
939 if (cap_file_more_info) {
942 printf("%s", cf_info->hardware);
947 printf("%s", cf_info->os);
952 printf("%s", cf_info->usr_appl);
960 cleanup_capture_info(capture_info *cf_info)
963 g_assert(cf_info != NULL);
965 g_free(cf_info->comment);
966 cf_info->comment = NULL;
968 g_free(cf_info->hardware);
969 cf_info->hardware = NULL;
974 g_free(cf_info->usr_appl);
975 cf_info->usr_appl = NULL;
977 g_free(cf_info->encap_counts);
978 cf_info->encap_counts = NULL;
980 g_free(cf_info->interface_ids);
981 cf_info->interface_ids = NULL;
983 if (cf_info->idb_info_strings) {
984 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
985 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
988 g_array_free(cf_info->idb_info_strings, TRUE);
990 cf_info->idb_info_strings = NULL;
993 /* multi-line comments would conflict with the formatting that capinfos uses
994 we replace linefeeds with spaces */
996 string_replace_newlines(gchar *str)
1002 while (*p != '\0') {
1014 process_cap_file(wtap *wth, const char *filename)
1024 guint32 snaplen_min_inferred = 0xffffffff;
1025 guint32 snaplen_max_inferred = 0;
1026 const struct wtap_pkthdr *phdr;
1027 capture_info cf_info;
1028 gboolean have_times = TRUE;
1029 nstime_t start_time;
1030 int start_time_tsprec;
1032 int stop_time_tsprec;
1035 gboolean know_order = FALSE;
1036 order_t order = IN_ORDER;
1037 wtap_optionblock_t shb_inf;
1039 wtapng_iface_descriptions_t *idb_info;
1042 g_assert(wth != NULL);
1043 g_assert(filename != NULL);
1045 nstime_set_zero(&start_time);
1046 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1047 nstime_set_zero(&stop_time);
1048 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1049 nstime_set_zero(&cur_time);
1050 nstime_set_zero(&prev_time);
1052 cf_info.comment = NULL;
1053 cf_info.hardware = NULL;
1055 cf_info.usr_appl = NULL;
1057 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1059 idb_info = wtap_file_get_idb_info(wth);
1061 g_assert(idb_info->interface_data != NULL);
1063 cf_info.num_interfaces = idb_info->interface_data->len;
1064 cf_info.interface_ids = g_new0(guint32, cf_info.num_interfaces);
1065 cf_info.pkt_interface_id_unknown = 0;
1067 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1069 /* get IDB info strings */
1070 for (i = 0; i < cf_info.num_interfaces; i++) {
1071 const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1072 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1073 g_array_append_val(cf_info.idb_info_strings, s);
1079 /* Tally up data that we need to parse through the file to find */
1080 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1081 phdr = wtap_phdr(wth);
1082 if (phdr->presence_flags & WTAP_HAS_TS) {
1083 prev_time = cur_time;
1084 cur_time = phdr->ts;
1086 start_time = phdr->ts;
1087 start_time_tsprec = phdr->pkt_tsprec;
1088 stop_time = phdr->ts;
1089 stop_time_tsprec = phdr->pkt_tsprec;
1090 prev_time = phdr->ts;
1092 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1093 order = NOT_IN_ORDER;
1095 if (nstime_cmp(&cur_time, &start_time) < 0) {
1096 start_time = cur_time;
1097 start_time_tsprec = phdr->pkt_tsprec;
1099 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1100 stop_time = cur_time;
1101 stop_time_tsprec = phdr->pkt_tsprec;
1104 have_times = FALSE; /* at least one packet has no time stamp */
1105 if (order != NOT_IN_ORDER)
1106 order = ORDER_UNKNOWN;
1109 if (phdr->rec_type == REC_TYPE_PACKET) {
1113 /* If caplen < len for a rcd, then presumably */
1114 /* 'Limit packet capture length' was done for this rcd. */
1115 /* Keep track as to the min/max actual snapshot lengths */
1116 /* seen for this file. */
1117 if (phdr->caplen < phdr->len) {
1118 if (phdr->caplen < snaplen_min_inferred)
1119 snaplen_min_inferred = phdr->caplen;
1120 if (phdr->caplen > snaplen_max_inferred)
1121 snaplen_max_inferred = phdr->caplen;
1124 /* Per-packet encapsulation */
1125 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1126 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1127 cf_info.encap_counts[phdr->pkt_encap] += 1;
1129 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1130 phdr->pkt_encap, packet, filename);
1134 /* Packet interface_id info */
1135 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1136 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1137 if (phdr->interface_id < cf_info.num_interfaces) {
1138 cf_info.interface_ids[phdr->interface_id] += 1;
1141 cf_info.pkt_interface_id_unknown += 1;
1145 /* it's for interface_id 0 */
1146 if (cf_info.num_interfaces != 0) {
1147 cf_info.interface_ids[0] += 1;
1150 cf_info.pkt_interface_id_unknown += 1;
1159 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1160 packet, filename, wtap_strerror(err));
1161 if (err == WTAP_ERR_SHORT_READ) {
1162 /* Don't give up completely with this one. */
1165 " (will continue anyway, checksums might be incorrect)\n");
1167 if (err_info != NULL) {
1168 fprintf(stderr, "(%s)\n", err_info);
1172 cleanup_capture_info(&cf_info);
1178 size = wtap_file_size(wth, &err);
1181 "capinfos: Can't get size of \"%s\": %s.\n",
1182 filename, g_strerror(err));
1183 cleanup_capture_info(&cf_info);
1187 cf_info.filesize = size;
1190 cf_info.file_type = wtap_file_type_subtype(wth);
1191 cf_info.iscompressed = wtap_iscompressed(wth);
1193 /* File Encapsulation */
1194 cf_info.file_encap = wtap_file_encap(wth);
1196 cf_info.file_tsprec = wtap_file_tsprec(wth);
1198 /* Packet size limit (snaplen) */
1199 cf_info.snaplen = wtap_snapshot_length(wth);
1200 if (cf_info.snaplen > 0)
1201 cf_info.snap_set = TRUE;
1203 cf_info.snap_set = FALSE;
1205 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1206 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1209 cf_info.packet_count = packet;
1212 cf_info.times_known = have_times;
1213 cf_info.start_time = start_time;
1214 cf_info.start_time_tsprec = start_time_tsprec;
1215 cf_info.stop_time = stop_time;
1216 cf_info.stop_time_tsprec = stop_time_tsprec;
1217 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1218 /* Duration precision is the higher of the start and stop time precisions. */
1219 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1220 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1222 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1223 cf_info.know_order = know_order;
1224 cf_info.order = order;
1226 /* Number of packet bytes */
1227 cf_info.packet_bytes = bytes;
1229 cf_info.data_rate = 0.0;
1230 cf_info.packet_rate = 0.0;
1231 cf_info.packet_size = 0.0;
1234 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1235 if (delta_time > 0.0) {
1236 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1237 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1239 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1242 shb_inf = wtap_file_get_shb(wth);
1244 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
1245 wtap_optionblock_get_option_string(shb_inf, OPT_COMMENT, &shb_str);
1246 cf_info.comment = g_strdup(shb_str);
1247 wtap_optionblock_get_option_string(shb_inf, OPT_SHB_HARDWARE, &shb_str);
1248 cf_info.hardware = g_strdup(shb_str);
1249 wtap_optionblock_get_option_string(shb_inf, OPT_SHB_OS, &shb_str);
1250 cf_info.os = g_strdup(shb_str);
1251 wtap_optionblock_get_option_string(shb_inf, OPT_SHB_USERAPPL, &shb_str);
1252 cf_info.usr_appl = g_strdup(shb_str);
1255 string_replace_newlines(cf_info.comment);
1256 string_replace_newlines(cf_info.hardware);
1257 string_replace_newlines(cf_info.os);
1258 string_replace_newlines(cf_info.usr_appl);
1261 print_stats(filename, &cf_info);
1263 print_stats_table(filename, &cf_info);
1266 cleanup_capture_info(&cf_info);
1272 print_usage(FILE *output)
1274 fprintf(output, "\n");
1275 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1276 fprintf(output, "\n");
1277 fprintf(output, "General infos:\n");
1278 fprintf(output, " -t display the capture file type\n");
1279 fprintf(output, " -E display the capture file encapsulation\n");
1280 fprintf(output, " -I display the capture file interface information\n");
1281 fprintf(output, " -F display additional capture file information\n");
1282 #ifdef HAVE_LIBGCRYPT
1283 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1285 fprintf(output, " -k display the capture comment\n");
1286 fprintf(output, "\n");
1287 fprintf(output, "Size infos:\n");
1288 fprintf(output, " -c display the number of packets\n");
1289 fprintf(output, " -s display the size of the file (in bytes)\n");
1290 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1291 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1292 fprintf(output, "\n");
1293 fprintf(output, "Time infos:\n");
1294 fprintf(output, " -u display the capture duration (in seconds)\n");
1295 fprintf(output, " -a display the capture start time\n");
1296 fprintf(output, " -e display the capture end time\n");
1297 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1298 fprintf(output, " -S display start and end times as seconds\n");
1299 fprintf(output, "\n");
1300 fprintf(output, "Statistic infos:\n");
1301 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1302 fprintf(output, " -i display average data rate (in bits/sec)\n");
1303 fprintf(output, " -z display average packet size (in bytes)\n");
1304 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1305 fprintf(output, "\n");
1306 fprintf(output, "Output format:\n");
1307 fprintf(output, " -L generate long report (default)\n");
1308 fprintf(output, " -T generate table report\n");
1309 fprintf(output, " -M display machine-readable values in long reports\n");
1310 fprintf(output, "\n");
1311 fprintf(output, "Table report options:\n");
1312 fprintf(output, " -R generate header record (default)\n");
1313 fprintf(output, " -r do not generate header record\n");
1314 fprintf(output, "\n");
1315 fprintf(output, " -B separate infos with TAB character (default)\n");
1316 fprintf(output, " -m separate infos with comma (,) character\n");
1317 fprintf(output, " -b separate infos with SPACE character\n");
1318 fprintf(output, "\n");
1319 fprintf(output, " -N do not quote infos (default)\n");
1320 fprintf(output, " -q quote infos with single quotes (')\n");
1321 fprintf(output, " -Q quote infos with double quotes (\")\n");
1322 fprintf(output, "\n");
1323 fprintf(output, "Miscellaneous:\n");
1324 fprintf(output, " -h display this help and exit\n");
1325 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1326 fprintf(output, " -A generate all infos (default)\n");
1327 fprintf(output, "\n");
1328 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1329 fprintf(output, "or adding to earlier options.\n");
1330 fprintf(output, "\n");
1331 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1332 fprintf(output, "output format.\n");
1333 #ifndef HAVE_LIBGCRYPT
1334 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1340 * Don't report failures to load plugins because most (non-wiretap) plugins
1341 * *should* fail to load (because we're not linked against libwireshark and
1342 * dissector plugins need libwireshark).
1345 failure_message(const char *msg_format _U_, va_list ap _U_)
1351 #ifdef HAVE_LIBGCRYPT
1353 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1356 for (i = 0; i < (int) length; i++) {
1357 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1360 #endif /* HAVE_LIBGCRYPT */
1363 main(int argc, char *argv[])
1365 GString *comp_info_str;
1366 GString *runtime_info_str;
1371 int overall_error_status;
1372 static const struct option long_options[] = {
1373 {"help", no_argument, NULL, 'h'},
1374 {"version", no_argument, NULL, 'v'},
1380 char *init_progfile_dir_error;
1382 #ifdef HAVE_LIBGCRYPT
1384 char *hash_buf = NULL;
1385 gcry_md_hd_t hd = NULL;
1389 /* Set the C-language locale to the native environment. */
1390 setlocale(LC_ALL, "");
1392 /* Get the compile-time version information string */
1393 comp_info_str = get_compiled_version_info(NULL, NULL);
1395 /* Get the run-time version information string */
1396 runtime_info_str = get_runtime_version_info(NULL);
1398 /* Add it to the information to be reported on a crash. */
1399 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1404 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1407 arg_list_utf_16to8(argc, argv);
1408 create_app_running_mutex();
1412 * Get credential information for later use.
1414 init_process_policies();
1415 init_open_routines();
1418 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1419 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1420 g_free(init_progfile_dir_error);
1422 /* Register all the plugin types we have. */
1423 wtap_register_plugin_types(); /* Types known to libwiretap */
1425 init_report_err(failure_message, NULL, NULL, NULL);
1427 /* Scan for plugins. This does *not* call their registration routines;
1428 that's done later. */
1431 /* Register all libwiretap plugin modules. */
1432 register_all_wiretap_modules();
1436 /* Process the options */
1437 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1438 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1443 if (report_all_infos) disable_all_infos();
1444 cap_file_type = TRUE;
1448 if (report_all_infos) disable_all_infos();
1449 cap_file_encap = TRUE;
1453 if (report_all_infos) disable_all_infos();
1458 if (report_all_infos) disable_all_infos();
1459 cap_packet_count = TRUE;
1463 if (report_all_infos) disable_all_infos();
1464 cap_file_size = TRUE;
1468 if (report_all_infos) disable_all_infos();
1469 cap_data_size = TRUE;
1473 if (report_all_infos) disable_all_infos();
1474 cap_duration = TRUE;
1478 if (report_all_infos) disable_all_infos();
1479 cap_start_time = TRUE;
1483 if (report_all_infos) disable_all_infos();
1484 cap_end_time = TRUE;
1488 time_as_secs = TRUE;
1492 if (report_all_infos) disable_all_infos();
1493 cap_data_rate_byte = TRUE;
1497 if (report_all_infos) disable_all_infos();
1498 cap_data_rate_bit = TRUE;
1502 if (report_all_infos) disable_all_infos();
1503 cap_packet_size = TRUE;
1507 if (report_all_infos) disable_all_infos();
1508 cap_packet_rate = TRUE;
1511 #ifdef HAVE_LIBGCRYPT
1513 if (report_all_infos) disable_all_infos();
1514 cap_file_hashes = TRUE;
1519 if (report_all_infos) disable_all_infos();
1524 if (report_all_infos) disable_all_infos();
1529 if (report_all_infos) disable_all_infos();
1530 cap_file_more_info = TRUE;
1534 if (report_all_infos) disable_all_infos();
1535 cap_file_idb = TRUE;
1539 continue_after_wtap_open_offline_failure = FALSE;
1551 long_report = FALSE;
1555 machine_readable = TRUE;
1559 table_report_header = TRUE;
1563 table_report_header = FALSE;
1579 field_separator = '\t';
1583 field_separator = ',';
1587 field_separator = ' ';
1591 printf("Capinfos (Wireshark) %s\n"
1592 "Print various information (infos) about capture files.\n"
1593 "See https://www.wireshark.org for more information.\n",
1594 get_ws_vcs_version_info());
1595 print_usage(stdout);
1600 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1601 g_string_free(comp_info_str, TRUE);
1602 g_string_free(runtime_info_str, TRUE);
1606 case '?': /* Bad flag - print usage message */
1607 print_usage(stderr);
1613 if ((argc - optind) < 1) {
1614 print_usage(stderr);
1618 if (!long_report && table_report_header) {
1619 print_stats_table_header();
1622 #ifdef HAVE_LIBGCRYPT
1623 if (cap_file_hashes) {
1624 gcry_check_version(NULL);
1625 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1627 gcry_md_enable(hd, GCRY_MD_RMD160);
1628 gcry_md_enable(hd, GCRY_MD_MD5);
1630 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1634 overall_error_status = 0;
1636 for (opt = optind; opt < argc; opt++) {
1638 #ifdef HAVE_LIBGCRYPT
1639 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1640 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1641 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1643 if (cap_file_hashes) {
1644 fh = ws_fopen(argv[opt], "rb");
1646 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1647 gcry_md_write(hd, hash_buf, hash_bytes);
1650 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1651 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1652 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1655 if (hd) gcry_md_reset(hd);
1657 #endif /* HAVE_LIBGCRYPT */
1659 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1662 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1663 wtap_strerror(err));
1664 if (err_info != NULL) {
1665 fprintf(stderr, "(%s)\n", err_info);
1668 overall_error_status = 1; /* remember that an error has occurred */
1669 if (!continue_after_wtap_open_offline_failure)
1670 exit(1); /* error status */
1674 if ((opt > optind) && (long_report))
1676 status = process_cap_file(wth, argv[opt]);
1684 return overall_error_status;
1688 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1693 * indent-tabs-mode: nil
1696 * vi: set shiftwidth=2 tabstop=8 expandtab:
1697 * :indentSize=2:tabSize=8:noTabs=true: