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 <wsutil/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>
90 #include <wsutil/wsgcrypt.h>
93 #ifndef HAVE_GETOPT_LONG
94 #include "wsutil/wsgetopt.h"
98 #include <wsutil/unicode-utils.h>
102 * By default capinfos now continues processing
103 * the next filename if and when wiretap detects
104 * a problem opening a file.
105 * Use the '-C' option to revert back to original
106 * capinfos behavior which is to abort any
107 * additional file processing at first open file
111 static gboolean continue_after_wtap_open_offline_failure = TRUE;
114 * table report variables
117 static gboolean long_report = TRUE; /* By default generate long report */
118 static gchar table_report_header = TRUE; /* Generate column header by default */
119 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
120 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
121 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
124 * capinfos has the ability to report on a number of
125 * various characteristics ("infos") for each input file.
127 * By default reporting of all info fields is enabled.
129 * Optionally the reporting of any specific info field
130 * or combination of info fields can be enabled with
131 * individual options.
134 static gboolean report_all_infos = TRUE; /* Report all infos */
136 static gboolean cap_file_type = TRUE; /* Report capture type */
137 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
138 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
139 static gboolean cap_packet_count = TRUE; /* Report packet count */
140 static gboolean cap_file_size = TRUE; /* Report file size */
141 static gboolean cap_comment = TRUE; /* Display the capture comment */
142 static gboolean cap_file_more_info = TRUE; /* Report more file info */
143 static gboolean cap_file_idb = TRUE; /* Report Interface info */
145 static gboolean cap_data_size = TRUE; /* Report packet byte size */
146 static gboolean cap_duration = TRUE; /* Report capture duration */
147 static gboolean cap_start_time = TRUE; /* Report capture start time */
148 static gboolean cap_end_time = TRUE; /* Report capture end time */
149 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
151 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
152 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
153 static gboolean cap_packet_size = TRUE; /* Report average packet size */
154 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
155 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
157 #ifdef HAVE_LIBGCRYPT
158 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
160 #define HASH_SIZE_SHA1 20
161 #define HASH_SIZE_RMD160 20
162 #define HASH_SIZE_MD5 16
164 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
165 #define HASH_BUF_SIZE (1024 * 1024)
168 static gchar file_sha1[HASH_STR_SIZE];
169 static gchar file_rmd160[HASH_STR_SIZE];
170 static gchar file_md5[HASH_STR_SIZE];
172 #define FILE_HASH_OPT "H"
174 #define FILE_HASH_OPT ""
175 #endif /* HAVE_LIBGCRYPT */
178 * If we have at least two packets with time stamps, and they're not in
179 * order - i.e., the later packet has a time stamp older than the earlier
180 * packet - the time stamps are known not to be in order.
182 * If every packet has a time stamp, and they're all in order, the time
183 * stamp is known to be in order.
185 * Otherwise, we have no idea.
193 typedef struct _capture_info {
194 const char *filename;
196 gboolean iscompressed;
205 guint64 packet_bytes;
206 gboolean times_known;
208 int start_time_tsprec;
210 int stop_time_tsprec;
211 guint32 packet_count;
212 gboolean snap_set; /* If set in capture file header */
213 guint32 snaplen; /* value from the capture file header */
214 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
215 guint32 snaplen_max_inferred; /* ... */
216 gboolean drops_known;
223 double data_rate; /* in bytes */
227 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
229 guint num_interfaces; /* number of IDBs, and thus size of interface_ids array */
230 guint32 *interface_ids; /* array of per_packet interface_id counts; one entry per file IDB */
231 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
232 GArray *idb_info_strings; /* array of IDB info strings */
237 enable_all_infos(void)
239 report_all_infos = TRUE;
241 cap_file_type = TRUE;
242 cap_file_encap = TRUE;
244 cap_packet_count = TRUE;
245 cap_file_size = TRUE;
247 cap_file_more_info = TRUE;
250 cap_data_size = TRUE;
252 cap_start_time = TRUE;
256 cap_data_rate_byte = TRUE;
257 cap_data_rate_bit = TRUE;
258 cap_packet_size = TRUE;
259 cap_packet_rate = TRUE;
261 #ifdef HAVE_LIBGCRYPT
262 cap_file_hashes = TRUE;
263 #endif /* HAVE_LIBGCRYPT */
267 disable_all_infos(void)
269 report_all_infos = FALSE;
271 cap_file_type = FALSE;
272 cap_file_encap = FALSE;
274 cap_packet_count = FALSE;
275 cap_file_size = FALSE;
277 cap_file_more_info = FALSE;
278 cap_file_idb = FALSE;
280 cap_data_size = FALSE;
281 cap_duration = FALSE;
282 cap_start_time = FALSE;
283 cap_end_time = FALSE;
286 cap_data_rate_byte = FALSE;
287 cap_data_rate_bit = FALSE;
288 cap_packet_size = FALSE;
289 cap_packet_rate = FALSE;
291 #ifdef HAVE_LIBGCRYPT
292 cap_file_hashes = FALSE;
293 #endif /* HAVE_LIBGCRYPT */
297 order_string(order_t order)
311 return "???"; /* "cannot happen" (the next step is "Profit!") */
316 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
318 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
321 if (cf_info->times_known && cf_info->packet_count > 0) {
323 switch (tsprecision) {
325 case WTAP_TSPREC_SEC:
326 g_snprintf(time_string_buf, sizeof time_string_buf,
328 (unsigned long)timer->secs);
331 case WTAP_TSPREC_DSEC:
332 g_snprintf(time_string_buf, sizeof time_string_buf,
334 (unsigned long)timer->secs,
335 timer->nsecs / 100000000);
338 case WTAP_TSPREC_CSEC:
339 g_snprintf(time_string_buf, sizeof time_string_buf,
341 (unsigned long)timer->secs,
342 timer->nsecs / 10000000);
345 case WTAP_TSPREC_MSEC:
346 g_snprintf(time_string_buf, sizeof time_string_buf,
348 (unsigned long)timer->secs,
349 timer->nsecs / 1000000);
352 case WTAP_TSPREC_USEC:
353 g_snprintf(time_string_buf, sizeof time_string_buf,
355 (unsigned long)timer->secs,
356 timer->nsecs / 1000);
359 case WTAP_TSPREC_NSEC:
360 g_snprintf(time_string_buf, sizeof time_string_buf,
362 (unsigned long)timer->secs,
367 g_snprintf(time_string_buf, sizeof time_string_buf,
368 "Unknown precision %d",
372 return time_string_buf;
374 ti_tm = localtime(&timer->secs);
376 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
377 return time_string_buf;
379 switch (tsprecision) {
381 case WTAP_TSPREC_SEC:
382 g_snprintf(time_string_buf, sizeof time_string_buf,
383 "%04d-%02d-%02d %02d:%02d:%02d",
384 ti_tm->tm_year + 1900,
392 case WTAP_TSPREC_DSEC:
393 g_snprintf(time_string_buf, sizeof time_string_buf,
394 "%04d-%02d-%02d %02d:%02d:%02d.%01d",
395 ti_tm->tm_year + 1900,
401 timer->nsecs / 100000000);
404 case WTAP_TSPREC_CSEC:
405 g_snprintf(time_string_buf, sizeof time_string_buf,
406 "%04d-%02d-%02d %02d:%02d:%02d.%02d",
407 ti_tm->tm_year + 1900,
413 timer->nsecs / 10000000);
416 case WTAP_TSPREC_MSEC:
417 g_snprintf(time_string_buf, sizeof time_string_buf,
418 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
419 ti_tm->tm_year + 1900,
425 timer->nsecs / 1000000);
428 case WTAP_TSPREC_USEC:
429 g_snprintf(time_string_buf, sizeof time_string_buf,
430 "%04d-%02d-%02d %02d:%02d:%02d.%06d",
431 ti_tm->tm_year + 1900,
437 timer->nsecs / 1000);
440 case WTAP_TSPREC_NSEC:
441 g_snprintf(time_string_buf, sizeof time_string_buf,
442 "%04d-%02d-%02d %02d:%02d:%02d.%09d",
443 ti_tm->tm_year + 1900,
453 g_snprintf(time_string_buf, sizeof time_string_buf,
454 "Unknown precision %d",
458 return time_string_buf;
462 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
463 return time_string_buf;
467 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
469 const gchar *second = want_seconds ? " second" : "";
470 const gchar *plural = want_seconds ? "s" : "";
471 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
473 if (cf_info->times_known && cf_info->packet_count > 0) {
474 switch (tsprecision) {
476 case WTAP_TSPREC_SEC:
477 g_snprintf(time_string_buf, sizeof time_string_buf,
479 (unsigned long)timer->secs,
481 timer->secs == 1 ? "" : plural);
484 case WTAP_TSPREC_DSEC:
485 g_snprintf(time_string_buf, sizeof time_string_buf,
487 (unsigned long)timer->secs,
488 timer->nsecs / 100000000,
490 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
493 case WTAP_TSPREC_CSEC:
494 g_snprintf(time_string_buf, sizeof time_string_buf,
496 (unsigned long)timer->secs,
497 timer->nsecs / 10000000,
499 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
502 case WTAP_TSPREC_MSEC:
503 g_snprintf(time_string_buf, sizeof time_string_buf,
505 (unsigned long)timer->secs,
506 timer->nsecs / 1000000,
508 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
511 case WTAP_TSPREC_USEC:
512 g_snprintf(time_string_buf, sizeof time_string_buf,
514 (unsigned long)timer->secs,
517 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
520 case WTAP_TSPREC_NSEC:
521 g_snprintf(time_string_buf, sizeof time_string_buf,
523 (unsigned long)timer->secs,
526 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
530 g_snprintf(time_string_buf, sizeof time_string_buf,
531 "Unknown precision %d",
535 return time_string_buf;
538 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
539 return time_string_buf;
542 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
544 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
546 printf("%sn/a\n", text_p1);
550 print_stats(const gchar *filename, capture_info *cf_info)
552 const gchar *file_type_string, *file_encap_string;
555 /* Build printable strings for various stats */
556 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
557 file_encap_string = wtap_encap_string(cf_info->file_encap);
559 if (filename) printf ("File name: %s\n", filename);
560 if (cap_file_type) printf ("File type: %s%s\n",
562 cf_info->iscompressed ? " (gzip compressed)" : "");
564 if (cap_file_encap) {
565 printf ("File encapsulation: %s\n", file_encap_string);
566 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
568 printf ("Encapsulation in use by packets (# of pkts):\n");
569 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
570 if (cf_info->encap_counts[i] > 0)
572 wtap_encap_string(i), cf_info->encap_counts[i]);
576 if (cap_file_more_info) {
577 printf ("File timestamp precision: %s (%d)\n",
578 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
581 if (cap_snaplen && cf_info->snap_set)
582 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
583 else if (cap_snaplen && !cf_info->snap_set)
584 printf ("Packet size limit: file hdr: (not set)\n");
585 if (cf_info->snaplen_max_inferred > 0) {
586 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
587 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
589 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
590 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
592 if (cap_packet_count) {
593 printf ("Number of packets: ");
594 if (machine_readable) {
595 printf ("%u\n", cf_info->packet_count);
597 size_string = format_size(cf_info->packet_count, format_size_unit_none);
598 printf ("%s\n", size_string);
603 printf ("File size: ");
604 if (machine_readable) {
605 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
607 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
608 printf ("%s\n", size_string);
613 printf ("Data size: ");
614 if (machine_readable) {
615 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
617 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
618 printf ("%s\n", size_string);
622 if (cf_info->times_known) {
623 if (cap_duration) /* XXX - shorten to hh:mm:ss */
624 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
626 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
628 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
629 if (cap_data_rate_byte) {
630 printf("Data byte rate: ");
631 if (machine_readable) {
632 print_value("", 2, " bytes/sec", cf_info->data_rate);
634 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
635 printf ("%s\n", size_string);
639 if (cap_data_rate_bit) {
640 printf("Data bit rate: ");
641 if (machine_readable) {
642 print_value("", 2, " bits/sec", cf_info->data_rate*8);
644 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
645 printf ("%s\n", size_string);
650 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
651 if (cf_info->times_known) {
652 if (cap_packet_rate) {
653 printf("Average packet rate: ");
654 if (machine_readable) {
655 print_value("", 2, " packets/sec", cf_info->packet_rate);
657 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
658 printf ("%s\n", size_string);
663 #ifdef HAVE_LIBGCRYPT
664 if (cap_file_hashes) {
665 printf ("SHA1: %s\n", file_sha1);
666 printf ("RIPEMD160: %s\n", file_rmd160);
667 printf ("MD5: %s\n", file_md5);
669 #endif /* HAVE_LIBGCRYPT */
670 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
671 if (cap_comment && cf_info->comment)
672 printf ("Capture comment: %s\n", cf_info->comment);
673 if (cap_file_more_info) {
674 if (cf_info->hardware)
675 printf ("Capture hardware: %s\n", cf_info->hardware);
677 printf ("Capture oper-sys: %s\n", cf_info->os);
678 if (cf_info->usr_appl)
679 printf ("Capture application: %s\n", cf_info->usr_appl);
682 if (cap_file_idb && cf_info->num_interfaces != 0) {
684 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
685 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
686 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
687 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
688 printf ("Interface #%u info:\n", i);
690 printf (" Number of packets = %u\n", cf_info->interface_ids[i]);
698 if (field_separator) putchar(field_separator);
704 if (quote_char) putchar(quote_char);
708 print_stats_table_header_label(const gchar *label)
717 print_stats_table_header(void)
723 if (cap_file_type) print_stats_table_header_label("File type");
724 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
725 if (cap_file_more_info) print_stats_table_header_label("File time precision");
726 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
727 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
728 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
729 if (cap_packet_count) print_stats_table_header_label("Number of packets");
730 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
731 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
732 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
733 if (cap_start_time) print_stats_table_header_label("Start time");
734 if (cap_end_time) print_stats_table_header_label("End time");
735 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
736 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
737 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
738 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
739 #ifdef HAVE_LIBGCRYPT
740 if (cap_file_hashes) {
741 print_stats_table_header_label("SHA1");
742 print_stats_table_header_label("RIPEMD160");
743 print_stats_table_header_label("MD5");
745 #endif /* HAVE_LIBGCRYPT */
746 if (cap_order) print_stats_table_header_label("Strict time order");
747 if (cap_comment) print_stats_table_header_label("Capture comment");
748 if (cap_file_more_info) {
749 print_stats_table_header_label("Capture hardware");
750 print_stats_table_header_label("Capture oper-sys");
751 print_stats_table_header_label("Capture application");
758 print_stats_table(const gchar *filename, capture_info *cf_info)
760 const gchar *file_type_string, *file_encap_string;
762 /* Build printable strings for various stats */
763 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
764 file_encap_string = wtap_encap_string(cf_info->file_encap);
768 printf("%s", filename);
775 printf("%s", file_type_string);
779 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
780 * Output a line for each different encap with all fields repeated except
781 * the encapsulation field which has "Per Packet: ..." for each
782 * encapsulation type seen ?
784 if (cap_file_encap) {
787 printf("%s", file_encap_string);
791 if (cap_file_more_info) {
794 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
801 if (cf_info->snap_set)
802 printf("%u", cf_info->snaplen);
806 if (cf_info->snaplen_max_inferred > 0) {
809 printf("%u", cf_info->snaplen_min_inferred);
813 printf("%u", cf_info->snaplen_max_inferred);
828 if (cap_packet_count) {
831 printf("%u", cf_info->packet_count);
838 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
845 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
852 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
856 if (cap_start_time) {
859 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
866 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
870 if (cap_data_rate_byte) {
873 if (cf_info->times_known)
874 printf("%.2f", cf_info->data_rate);
880 if (cap_data_rate_bit) {
883 if (cf_info->times_known)
884 printf("%.2f", cf_info->data_rate*8);
890 if (cap_packet_size) {
893 printf("%.2f", cf_info->packet_size);
897 if (cap_packet_rate) {
900 if (cf_info->times_known)
901 printf("%.2f", cf_info->packet_rate);
907 #ifdef HAVE_LIBGCRYPT
908 if (cap_file_hashes) {
911 printf("%s", file_sha1);
916 printf("%s", file_rmd160);
921 printf("%s", file_md5);
924 #endif /* HAVE_LIBGCRYPT */
929 printf("%s", order_string(cf_info->order));
933 /* this is silly to put into a table format, but oh well */
937 printf("%s", cf_info->comment);
941 if (cap_file_more_info) {
944 printf("%s", cf_info->hardware);
949 printf("%s", cf_info->os);
954 printf("%s", cf_info->usr_appl);
962 cleanup_capture_info(capture_info *cf_info)
965 g_assert(cf_info != NULL);
967 g_free(cf_info->comment);
968 cf_info->comment = NULL;
970 g_free(cf_info->hardware);
971 cf_info->hardware = NULL;
976 g_free(cf_info->usr_appl);
977 cf_info->usr_appl = NULL;
979 g_free(cf_info->encap_counts);
980 cf_info->encap_counts = NULL;
982 g_free(cf_info->interface_ids);
983 cf_info->interface_ids = NULL;
985 if (cf_info->idb_info_strings) {
986 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
987 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
990 g_array_free(cf_info->idb_info_strings, TRUE);
992 cf_info->idb_info_strings = NULL;
995 /* multi-line comments would conflict with the formatting that capinfos uses
996 we replace linefeeds with spaces */
998 string_replace_newlines(gchar *str)
1004 while (*p != '\0') {
1016 process_cap_file(wtap *wth, const char *filename)
1026 guint32 snaplen_min_inferred = 0xffffffff;
1027 guint32 snaplen_max_inferred = 0;
1028 const struct wtap_pkthdr *phdr;
1029 capture_info cf_info;
1030 gboolean have_times = TRUE;
1031 nstime_t start_time;
1032 int start_time_tsprec;
1034 int stop_time_tsprec;
1037 gboolean know_order = FALSE;
1038 order_t order = IN_ORDER;
1039 wtap_optionblock_t shb_inf;
1041 wtapng_iface_descriptions_t *idb_info;
1044 g_assert(wth != NULL);
1045 g_assert(filename != NULL);
1047 nstime_set_zero(&start_time);
1048 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1049 nstime_set_zero(&stop_time);
1050 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1051 nstime_set_zero(&cur_time);
1052 nstime_set_zero(&prev_time);
1054 cf_info.comment = NULL;
1055 cf_info.hardware = NULL;
1057 cf_info.usr_appl = NULL;
1059 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1061 idb_info = wtap_file_get_idb_info(wth);
1063 g_assert(idb_info->interface_data != NULL);
1065 cf_info.num_interfaces = idb_info->interface_data->len;
1066 cf_info.interface_ids = g_new0(guint32, cf_info.num_interfaces);
1067 cf_info.pkt_interface_id_unknown = 0;
1069 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1071 /* get IDB info strings */
1072 for (i = 0; i < cf_info.num_interfaces; i++) {
1073 const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1074 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1075 g_array_append_val(cf_info.idb_info_strings, s);
1081 /* Tally up data that we need to parse through the file to find */
1082 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1083 phdr = wtap_phdr(wth);
1084 if (phdr->presence_flags & WTAP_HAS_TS) {
1085 prev_time = cur_time;
1086 cur_time = phdr->ts;
1088 start_time = phdr->ts;
1089 start_time_tsprec = phdr->pkt_tsprec;
1090 stop_time = phdr->ts;
1091 stop_time_tsprec = phdr->pkt_tsprec;
1092 prev_time = phdr->ts;
1094 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1095 order = NOT_IN_ORDER;
1097 if (nstime_cmp(&cur_time, &start_time) < 0) {
1098 start_time = cur_time;
1099 start_time_tsprec = phdr->pkt_tsprec;
1101 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1102 stop_time = cur_time;
1103 stop_time_tsprec = phdr->pkt_tsprec;
1106 have_times = FALSE; /* at least one packet has no time stamp */
1107 if (order != NOT_IN_ORDER)
1108 order = ORDER_UNKNOWN;
1111 if (phdr->rec_type == REC_TYPE_PACKET) {
1115 /* If caplen < len for a rcd, then presumably */
1116 /* 'Limit packet capture length' was done for this rcd. */
1117 /* Keep track as to the min/max actual snapshot lengths */
1118 /* seen for this file. */
1119 if (phdr->caplen < phdr->len) {
1120 if (phdr->caplen < snaplen_min_inferred)
1121 snaplen_min_inferred = phdr->caplen;
1122 if (phdr->caplen > snaplen_max_inferred)
1123 snaplen_max_inferred = phdr->caplen;
1126 /* Per-packet encapsulation */
1127 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1128 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1129 cf_info.encap_counts[phdr->pkt_encap] += 1;
1131 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1132 phdr->pkt_encap, packet, filename);
1136 /* Packet interface_id info */
1137 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1138 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1139 if (phdr->interface_id < cf_info.num_interfaces) {
1140 cf_info.interface_ids[phdr->interface_id] += 1;
1143 cf_info.pkt_interface_id_unknown += 1;
1147 /* it's for interface_id 0 */
1148 if (cf_info.num_interfaces != 0) {
1149 cf_info.interface_ids[0] += 1;
1152 cf_info.pkt_interface_id_unknown += 1;
1161 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1162 packet, filename, wtap_strerror(err));
1163 if (err == WTAP_ERR_SHORT_READ) {
1164 /* Don't give up completely with this one. */
1167 " (will continue anyway, checksums might be incorrect)\n");
1169 if (err_info != NULL) {
1170 fprintf(stderr, "(%s)\n", err_info);
1174 cleanup_capture_info(&cf_info);
1180 size = wtap_file_size(wth, &err);
1183 "capinfos: Can't get size of \"%s\": %s.\n",
1184 filename, g_strerror(err));
1185 cleanup_capture_info(&cf_info);
1189 cf_info.filesize = size;
1192 cf_info.file_type = wtap_file_type_subtype(wth);
1193 cf_info.iscompressed = wtap_iscompressed(wth);
1195 /* File Encapsulation */
1196 cf_info.file_encap = wtap_file_encap(wth);
1198 cf_info.file_tsprec = wtap_file_tsprec(wth);
1200 /* Packet size limit (snaplen) */
1201 cf_info.snaplen = wtap_snapshot_length(wth);
1202 if (cf_info.snaplen > 0)
1203 cf_info.snap_set = TRUE;
1205 cf_info.snap_set = FALSE;
1207 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1208 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1211 cf_info.packet_count = packet;
1214 cf_info.times_known = have_times;
1215 cf_info.start_time = start_time;
1216 cf_info.start_time_tsprec = start_time_tsprec;
1217 cf_info.stop_time = stop_time;
1218 cf_info.stop_time_tsprec = stop_time_tsprec;
1219 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1220 /* Duration precision is the higher of the start and stop time precisions. */
1221 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1222 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1224 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1225 cf_info.know_order = know_order;
1226 cf_info.order = order;
1228 /* Number of packet bytes */
1229 cf_info.packet_bytes = bytes;
1231 cf_info.data_rate = 0.0;
1232 cf_info.packet_rate = 0.0;
1233 cf_info.packet_size = 0.0;
1236 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1237 if (delta_time > 0.0) {
1238 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1239 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1241 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1244 shb_inf = wtap_file_get_shb(wth);
1246 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
1247 wtap_optionblock_get_option_string(shb_inf, OPT_COMMENT, &shb_str);
1248 cf_info.comment = g_strdup(shb_str);
1249 wtap_optionblock_get_option_string(shb_inf, OPT_SHB_HARDWARE, &shb_str);
1250 cf_info.hardware = g_strdup(shb_str);
1251 wtap_optionblock_get_option_string(shb_inf, OPT_SHB_OS, &shb_str);
1252 cf_info.os = g_strdup(shb_str);
1253 wtap_optionblock_get_option_string(shb_inf, OPT_SHB_USERAPPL, &shb_str);
1254 cf_info.usr_appl = g_strdup(shb_str);
1257 string_replace_newlines(cf_info.comment);
1258 string_replace_newlines(cf_info.hardware);
1259 string_replace_newlines(cf_info.os);
1260 string_replace_newlines(cf_info.usr_appl);
1263 print_stats(filename, &cf_info);
1265 print_stats_table(filename, &cf_info);
1268 cleanup_capture_info(&cf_info);
1274 print_usage(FILE *output)
1276 fprintf(output, "\n");
1277 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1278 fprintf(output, "\n");
1279 fprintf(output, "General infos:\n");
1280 fprintf(output, " -t display the capture file type\n");
1281 fprintf(output, " -E display the capture file encapsulation\n");
1282 fprintf(output, " -I display the capture file interface information\n");
1283 fprintf(output, " -F display additional capture file information\n");
1284 #ifdef HAVE_LIBGCRYPT
1285 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1287 fprintf(output, " -k display the capture comment\n");
1288 fprintf(output, "\n");
1289 fprintf(output, "Size infos:\n");
1290 fprintf(output, " -c display the number of packets\n");
1291 fprintf(output, " -s display the size of the file (in bytes)\n");
1292 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1293 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1294 fprintf(output, "\n");
1295 fprintf(output, "Time infos:\n");
1296 fprintf(output, " -u display the capture duration (in seconds)\n");
1297 fprintf(output, " -a display the capture start time\n");
1298 fprintf(output, " -e display the capture end time\n");
1299 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1300 fprintf(output, " -S display start and end times as seconds\n");
1301 fprintf(output, "\n");
1302 fprintf(output, "Statistic infos:\n");
1303 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1304 fprintf(output, " -i display average data rate (in bits/sec)\n");
1305 fprintf(output, " -z display average packet size (in bytes)\n");
1306 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1307 fprintf(output, "\n");
1308 fprintf(output, "Output format:\n");
1309 fprintf(output, " -L generate long report (default)\n");
1310 fprintf(output, " -T generate table report\n");
1311 fprintf(output, " -M display machine-readable values in long reports\n");
1312 fprintf(output, "\n");
1313 fprintf(output, "Table report options:\n");
1314 fprintf(output, " -R generate header record (default)\n");
1315 fprintf(output, " -r do not generate header record\n");
1316 fprintf(output, "\n");
1317 fprintf(output, " -B separate infos with TAB character (default)\n");
1318 fprintf(output, " -m separate infos with comma (,) character\n");
1319 fprintf(output, " -b separate infos with SPACE character\n");
1320 fprintf(output, "\n");
1321 fprintf(output, " -N do not quote infos (default)\n");
1322 fprintf(output, " -q quote infos with single quotes (')\n");
1323 fprintf(output, " -Q quote infos with double quotes (\")\n");
1324 fprintf(output, "\n");
1325 fprintf(output, "Miscellaneous:\n");
1326 fprintf(output, " -h display this help and exit\n");
1327 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1328 fprintf(output, " -A generate all infos (default)\n");
1329 fprintf(output, "\n");
1330 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1331 fprintf(output, "or adding to earlier options.\n");
1332 fprintf(output, "\n");
1333 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1334 fprintf(output, "output format.\n");
1335 #ifndef HAVE_LIBGCRYPT
1336 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1342 * Don't report failures to load plugins because most (non-wiretap) plugins
1343 * *should* fail to load (because we're not linked against libwireshark and
1344 * dissector plugins need libwireshark).
1347 failure_message(const char *msg_format _U_, va_list ap _U_)
1353 #ifdef HAVE_LIBGCRYPT
1355 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1358 for (i = 0; i < (int) length; i++) {
1359 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1362 #endif /* HAVE_LIBGCRYPT */
1365 main(int argc, char *argv[])
1367 GString *comp_info_str;
1368 GString *runtime_info_str;
1373 int overall_error_status;
1374 static const struct option long_options[] = {
1375 {"help", no_argument, NULL, 'h'},
1376 {"version", no_argument, NULL, 'v'},
1382 char *init_progfile_dir_error;
1384 #ifdef HAVE_LIBGCRYPT
1386 char *hash_buf = NULL;
1387 gcry_md_hd_t hd = NULL;
1391 /* Set the C-language locale to the native environment. */
1392 setlocale(LC_ALL, "");
1394 /* Get the compile-time version information string */
1395 comp_info_str = get_compiled_version_info(NULL, NULL);
1397 /* Get the run-time version information string */
1398 runtime_info_str = get_runtime_version_info(NULL);
1400 /* Add it to the information to be reported on a crash. */
1401 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1406 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1409 arg_list_utf_16to8(argc, argv);
1410 create_app_running_mutex();
1414 * Get credential information for later use.
1416 init_process_policies();
1417 init_open_routines();
1420 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1421 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1422 g_free(init_progfile_dir_error);
1424 /* Register all the plugin types we have. */
1425 wtap_register_plugin_types(); /* Types known to libwiretap */
1427 init_report_err(failure_message, NULL, NULL, NULL);
1429 /* Scan for plugins. This does *not* call their registration routines;
1430 that's done later. */
1433 /* Register all libwiretap plugin modules. */
1434 register_all_wiretap_modules();
1438 /* Process the options */
1439 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1440 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1445 if (report_all_infos) disable_all_infos();
1446 cap_file_type = TRUE;
1450 if (report_all_infos) disable_all_infos();
1451 cap_file_encap = TRUE;
1455 if (report_all_infos) disable_all_infos();
1460 if (report_all_infos) disable_all_infos();
1461 cap_packet_count = TRUE;
1465 if (report_all_infos) disable_all_infos();
1466 cap_file_size = TRUE;
1470 if (report_all_infos) disable_all_infos();
1471 cap_data_size = TRUE;
1475 if (report_all_infos) disable_all_infos();
1476 cap_duration = TRUE;
1480 if (report_all_infos) disable_all_infos();
1481 cap_start_time = TRUE;
1485 if (report_all_infos) disable_all_infos();
1486 cap_end_time = TRUE;
1490 time_as_secs = TRUE;
1494 if (report_all_infos) disable_all_infos();
1495 cap_data_rate_byte = TRUE;
1499 if (report_all_infos) disable_all_infos();
1500 cap_data_rate_bit = TRUE;
1504 if (report_all_infos) disable_all_infos();
1505 cap_packet_size = TRUE;
1509 if (report_all_infos) disable_all_infos();
1510 cap_packet_rate = TRUE;
1513 #ifdef HAVE_LIBGCRYPT
1515 if (report_all_infos) disable_all_infos();
1516 cap_file_hashes = TRUE;
1521 if (report_all_infos) disable_all_infos();
1526 if (report_all_infos) disable_all_infos();
1531 if (report_all_infos) disable_all_infos();
1532 cap_file_more_info = TRUE;
1536 if (report_all_infos) disable_all_infos();
1537 cap_file_idb = TRUE;
1541 continue_after_wtap_open_offline_failure = FALSE;
1553 long_report = FALSE;
1557 machine_readable = TRUE;
1561 table_report_header = TRUE;
1565 table_report_header = FALSE;
1581 field_separator = '\t';
1585 field_separator = ',';
1589 field_separator = ' ';
1593 printf("Capinfos (Wireshark) %s\n"
1594 "Print various information (infos) about capture files.\n"
1595 "See https://www.wireshark.org for more information.\n",
1596 get_ws_vcs_version_info());
1597 print_usage(stdout);
1602 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1603 g_string_free(comp_info_str, TRUE);
1604 g_string_free(runtime_info_str, TRUE);
1608 case '?': /* Bad flag - print usage message */
1609 print_usage(stderr);
1615 if ((argc - optind) < 1) {
1616 print_usage(stderr);
1620 if (!long_report && table_report_header) {
1621 print_stats_table_header();
1624 #ifdef HAVE_LIBGCRYPT
1625 if (cap_file_hashes) {
1626 gcry_check_version(NULL);
1627 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1629 gcry_md_enable(hd, GCRY_MD_RMD160);
1630 gcry_md_enable(hd, GCRY_MD_MD5);
1632 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1636 overall_error_status = 0;
1638 for (opt = optind; opt < argc; opt++) {
1640 #ifdef HAVE_LIBGCRYPT
1641 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1642 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1643 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1645 if (cap_file_hashes) {
1646 fh = ws_fopen(argv[opt], "rb");
1648 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1649 gcry_md_write(hd, hash_buf, hash_bytes);
1652 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1653 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1654 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1657 if (hd) gcry_md_reset(hd);
1659 #endif /* HAVE_LIBGCRYPT */
1661 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1664 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1665 wtap_strerror(err));
1666 if (err_info != NULL) {
1667 fprintf(stderr, "(%s)\n", err_info);
1670 overall_error_status = 1; /* remember that an error has occurred */
1671 if (!continue_after_wtap_open_offline_failure)
1672 exit(1); /* error status */
1676 if ((opt > optind) && (long_report))
1678 status = process_cap_file(wth, argv[opt]);
1686 return overall_error_status;
1690 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1695 * indent-tabs-mode: nil
1698 * vi: set shiftwidth=2 tabstop=8 expandtab:
1699 * :indentSize=2:tabSize=8:noTabs=true: