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).
73 #include <zlib.h> /* to get the libz version number */
76 #include <wsutil/crash_info.h>
77 #include <wsutil/filesystem.h>
78 #include <wsutil/privileges.h>
79 #include <wsutil/ws_diag_control.h>
80 #include <wsutil/ws_version_info.h>
83 #include <wsutil/plugins.h>
87 #include <wsutil/report_err.h>
88 #include <wsutil/str_util.h>
89 #include <wsutil/file_util.h>
92 #include <wsutil/wsgcrypt.h>
95 #ifndef HAVE_GETOPT_LONG
96 #include "wsutil/wsgetopt.h"
100 #include <wsutil/unicode-utils.h>
104 * By default capinfos now continues processing
105 * the next filename if and when wiretap detects
106 * a problem opening a file.
107 * Use the '-C' option to revert back to original
108 * capinfos behavior which is to abort any
109 * additional file processing at first open file
113 static gboolean continue_after_wtap_open_offline_failure = TRUE;
116 * table report variables
119 static gboolean long_report = TRUE; /* By default generate long report */
120 static gchar table_report_header = TRUE; /* Generate column header by default */
121 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
122 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
123 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
126 * capinfos has the ability to report on a number of
127 * various characteristics ("infos") for each input file.
129 * By default reporting of all info fields is enabled.
131 * Optionally the reporting of any specific info field
132 * or combination of info fields can be enabled with
133 * individual options.
136 static gboolean report_all_infos = TRUE; /* Report all infos */
138 static gboolean cap_file_type = TRUE; /* Report capture type */
139 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
140 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
141 static gboolean cap_packet_count = TRUE; /* Report packet count */
142 static gboolean cap_file_size = TRUE; /* Report file size */
143 static gboolean cap_comment = TRUE; /* Display the capture comment */
144 static gboolean cap_file_more_info = TRUE; /* Report more file info */
145 static gboolean cap_file_idb = TRUE; /* Report Interface info */
147 static gboolean cap_data_size = TRUE; /* Report packet byte size */
148 static gboolean cap_duration = TRUE; /* Report capture duration */
149 static gboolean cap_start_time = TRUE; /* Report capture start time */
150 static gboolean cap_end_time = TRUE; /* Report capture end time */
151 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
153 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
154 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
155 static gboolean cap_packet_size = TRUE; /* Report average packet size */
156 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
157 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
159 #ifdef HAVE_LIBGCRYPT
160 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
162 #define HASH_SIZE_SHA1 20
163 #define HASH_SIZE_RMD160 20
164 #define HASH_SIZE_MD5 16
166 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
167 #define HASH_BUF_SIZE (1024 * 1024)
170 static gchar file_sha1[HASH_STR_SIZE];
171 static gchar file_rmd160[HASH_STR_SIZE];
172 static gchar file_md5[HASH_STR_SIZE];
174 #define FILE_HASH_OPT "H"
176 #define FILE_HASH_OPT ""
177 #endif /* HAVE_LIBGCRYPT */
180 * If we have at least two packets with time stamps, and they're not in
181 * order - i.e., the later packet has a time stamp older than the earlier
182 * packet - the time stamps are known not to be in order.
184 * If every packet has a time stamp, and they're all in order, the time
185 * stamp is known to be in order.
187 * Otherwise, we have no idea.
195 typedef struct _capture_info {
196 const char *filename;
198 gboolean iscompressed;
207 guint64 packet_bytes;
208 gboolean times_known;
210 int start_time_tsprec;
212 int stop_time_tsprec;
213 guint32 packet_count;
214 gboolean snap_set; /* If set in capture file header */
215 guint32 snaplen; /* value from the capture file header */
216 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
217 guint32 snaplen_max_inferred; /* ... */
218 gboolean drops_known;
225 double data_rate; /* in bytes */
229 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
231 guint num_interfaces; /* number of IDBs, and thus size of interface_ids array */
232 guint32 *interface_ids; /* array of per_packet interface_id counts; one entry per file IDB */
233 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
234 GArray *idb_info_strings; /* array of IDB info strings */
239 enable_all_infos(void)
241 report_all_infos = TRUE;
243 cap_file_type = TRUE;
244 cap_file_encap = TRUE;
246 cap_packet_count = TRUE;
247 cap_file_size = TRUE;
249 cap_file_more_info = TRUE;
252 cap_data_size = TRUE;
254 cap_start_time = TRUE;
258 cap_data_rate_byte = TRUE;
259 cap_data_rate_bit = TRUE;
260 cap_packet_size = TRUE;
261 cap_packet_rate = TRUE;
263 #ifdef HAVE_LIBGCRYPT
264 cap_file_hashes = TRUE;
265 #endif /* HAVE_LIBGCRYPT */
269 disable_all_infos(void)
271 report_all_infos = FALSE;
273 cap_file_type = FALSE;
274 cap_file_encap = FALSE;
276 cap_packet_count = FALSE;
277 cap_file_size = FALSE;
279 cap_file_more_info = FALSE;
280 cap_file_idb = FALSE;
282 cap_data_size = FALSE;
283 cap_duration = FALSE;
284 cap_start_time = FALSE;
285 cap_end_time = FALSE;
288 cap_data_rate_byte = FALSE;
289 cap_data_rate_bit = FALSE;
290 cap_packet_size = FALSE;
291 cap_packet_rate = FALSE;
293 #ifdef HAVE_LIBGCRYPT
294 cap_file_hashes = FALSE;
295 #endif /* HAVE_LIBGCRYPT */
299 order_string(order_t order)
313 return "???"; /* "cannot happen" (the next step is "Profit!") */
318 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
320 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
323 if (cf_info->times_known && cf_info->packet_count > 0) {
325 switch (tsprecision) {
327 case WTAP_TSPREC_SEC:
328 g_snprintf(time_string_buf, sizeof time_string_buf,
330 (unsigned long)timer->secs);
333 case WTAP_TSPREC_DSEC:
334 g_snprintf(time_string_buf, sizeof time_string_buf,
336 (unsigned long)timer->secs,
337 timer->nsecs / 100000000);
340 case WTAP_TSPREC_CSEC:
341 g_snprintf(time_string_buf, sizeof time_string_buf,
343 (unsigned long)timer->secs,
344 timer->nsecs / 10000000);
347 case WTAP_TSPREC_MSEC:
348 g_snprintf(time_string_buf, sizeof time_string_buf,
350 (unsigned long)timer->secs,
351 timer->nsecs / 1000000);
354 case WTAP_TSPREC_USEC:
355 g_snprintf(time_string_buf, sizeof time_string_buf,
357 (unsigned long)timer->secs,
358 timer->nsecs / 1000);
361 case WTAP_TSPREC_NSEC:
362 g_snprintf(time_string_buf, sizeof time_string_buf,
364 (unsigned long)timer->secs,
369 g_snprintf(time_string_buf, sizeof time_string_buf,
370 "Unknown precision %d",
374 return time_string_buf;
376 ti_tm = localtime(&timer->secs);
378 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
379 return time_string_buf;
381 switch (tsprecision) {
383 case WTAP_TSPREC_SEC:
384 g_snprintf(time_string_buf, sizeof time_string_buf,
385 "%04d-%02d-%02d %02d:%02d:%02d",
386 ti_tm->tm_year + 1900,
394 case WTAP_TSPREC_DSEC:
395 g_snprintf(time_string_buf, sizeof time_string_buf,
396 "%04d-%02d-%02d %02d:%02d:%02d.%01d",
397 ti_tm->tm_year + 1900,
403 timer->nsecs / 100000000);
406 case WTAP_TSPREC_CSEC:
407 g_snprintf(time_string_buf, sizeof time_string_buf,
408 "%04d-%02d-%02d %02d:%02d:%02d.%02d",
409 ti_tm->tm_year + 1900,
415 timer->nsecs / 10000000);
418 case WTAP_TSPREC_MSEC:
419 g_snprintf(time_string_buf, sizeof time_string_buf,
420 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
421 ti_tm->tm_year + 1900,
427 timer->nsecs / 1000000);
430 case WTAP_TSPREC_USEC:
431 g_snprintf(time_string_buf, sizeof time_string_buf,
432 "%04d-%02d-%02d %02d:%02d:%02d.%06d",
433 ti_tm->tm_year + 1900,
439 timer->nsecs / 1000);
442 case WTAP_TSPREC_NSEC:
443 g_snprintf(time_string_buf, sizeof time_string_buf,
444 "%04d-%02d-%02d %02d:%02d:%02d.%09d",
445 ti_tm->tm_year + 1900,
455 g_snprintf(time_string_buf, sizeof time_string_buf,
456 "Unknown precision %d",
460 return time_string_buf;
464 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
465 return time_string_buf;
469 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
471 const gchar *second = want_seconds ? " second" : "";
472 const gchar *plural = want_seconds ? "s" : "";
473 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
475 if (cf_info->times_known && cf_info->packet_count > 0) {
476 switch (tsprecision) {
478 case WTAP_TSPREC_SEC:
479 g_snprintf(time_string_buf, sizeof time_string_buf,
481 (unsigned long)timer->secs,
483 timer->secs == 1 ? "" : plural);
486 case WTAP_TSPREC_DSEC:
487 g_snprintf(time_string_buf, sizeof time_string_buf,
489 (unsigned long)timer->secs,
490 timer->nsecs / 100000000,
492 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
495 case WTAP_TSPREC_CSEC:
496 g_snprintf(time_string_buf, sizeof time_string_buf,
498 (unsigned long)timer->secs,
499 timer->nsecs / 10000000,
501 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
504 case WTAP_TSPREC_MSEC:
505 g_snprintf(time_string_buf, sizeof time_string_buf,
507 (unsigned long)timer->secs,
508 timer->nsecs / 1000000,
510 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
513 case WTAP_TSPREC_USEC:
514 g_snprintf(time_string_buf, sizeof time_string_buf,
516 (unsigned long)timer->secs,
519 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
522 case WTAP_TSPREC_NSEC:
523 g_snprintf(time_string_buf, sizeof time_string_buf,
525 (unsigned long)timer->secs,
528 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
532 g_snprintf(time_string_buf, sizeof time_string_buf,
533 "Unknown precision %d",
537 return time_string_buf;
540 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
541 return time_string_buf;
544 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
546 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
548 printf("%sn/a\n", text_p1);
552 print_stats(const gchar *filename, capture_info *cf_info)
554 const gchar *file_type_string, *file_encap_string;
557 /* Build printable strings for various stats */
558 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
559 file_encap_string = wtap_encap_string(cf_info->file_encap);
561 if (filename) printf ("File name: %s\n", filename);
562 if (cap_file_type) printf ("File type: %s%s\n",
564 cf_info->iscompressed ? " (gzip compressed)" : "");
566 if (cap_file_encap) {
567 printf ("File encapsulation: %s\n", file_encap_string);
568 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
570 printf ("Encapsulation in use by packets (# of pkts):\n");
571 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
572 if (cf_info->encap_counts[i] > 0)
574 wtap_encap_string(i), cf_info->encap_counts[i]);
578 if (cap_file_more_info) {
579 printf ("File timestamp precision: %s (%d)\n",
580 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
583 if (cap_snaplen && cf_info->snap_set)
584 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
585 else if (cap_snaplen && !cf_info->snap_set)
586 printf ("Packet size limit: file hdr: (not set)\n");
587 if (cf_info->snaplen_max_inferred > 0) {
588 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
589 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
591 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
592 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
594 if (cap_packet_count) {
595 printf ("Number of packets: ");
596 if (machine_readable) {
597 printf ("%u\n", cf_info->packet_count);
599 size_string = format_size(cf_info->packet_count, format_size_unit_none);
600 printf ("%s\n", size_string);
605 printf ("File size: ");
606 if (machine_readable) {
607 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
609 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
610 printf ("%s\n", size_string);
615 printf ("Data size: ");
616 if (machine_readable) {
617 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
619 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
620 printf ("%s\n", size_string);
624 if (cf_info->times_known) {
625 if (cap_duration) /* XXX - shorten to hh:mm:ss */
626 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
628 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
630 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
631 if (cap_data_rate_byte) {
632 printf("Data byte rate: ");
633 if (machine_readable) {
634 print_value("", 2, " bytes/sec", cf_info->data_rate);
636 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
637 printf ("%s\n", size_string);
641 if (cap_data_rate_bit) {
642 printf("Data bit rate: ");
643 if (machine_readable) {
644 print_value("", 2, " bits/sec", cf_info->data_rate*8);
646 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
647 printf ("%s\n", size_string);
652 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
653 if (cf_info->times_known) {
654 if (cap_packet_rate) {
655 printf("Average packet rate: ");
656 if (machine_readable) {
657 print_value("", 2, " packets/sec", cf_info->packet_rate);
659 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
660 printf ("%s\n", size_string);
665 #ifdef HAVE_LIBGCRYPT
666 if (cap_file_hashes) {
667 printf ("SHA1: %s\n", file_sha1);
668 printf ("RIPEMD160: %s\n", file_rmd160);
669 printf ("MD5: %s\n", file_md5);
671 #endif /* HAVE_LIBGCRYPT */
672 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
673 if (cap_comment && cf_info->comment)
674 printf ("Capture comment: %s\n", cf_info->comment);
675 if (cap_file_more_info) {
676 if (cf_info->hardware)
677 printf ("Capture hardware: %s\n", cf_info->hardware);
679 printf ("Capture oper-sys: %s\n", cf_info->os);
680 if (cf_info->usr_appl)
681 printf ("Capture application: %s\n", cf_info->usr_appl);
684 if (cap_file_idb && cf_info->num_interfaces != 0) {
686 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
687 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
688 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
689 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
690 printf ("Interface #%u info:\n", i);
692 printf (" Number of packets = %u\n", cf_info->interface_ids[i]);
700 if (field_separator) putchar(field_separator);
706 if (quote_char) putchar(quote_char);
710 print_stats_table_header_label(const gchar *label)
719 print_stats_table_header(void)
725 if (cap_file_type) print_stats_table_header_label("File type");
726 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
727 if (cap_file_more_info) print_stats_table_header_label("File time precision");
728 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
729 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
730 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
731 if (cap_packet_count) print_stats_table_header_label("Number of packets");
732 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
733 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
734 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
735 if (cap_start_time) print_stats_table_header_label("Start time");
736 if (cap_end_time) print_stats_table_header_label("End time");
737 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
738 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
739 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
740 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
741 #ifdef HAVE_LIBGCRYPT
742 if (cap_file_hashes) {
743 print_stats_table_header_label("SHA1");
744 print_stats_table_header_label("RIPEMD160");
745 print_stats_table_header_label("MD5");
747 #endif /* HAVE_LIBGCRYPT */
748 if (cap_order) print_stats_table_header_label("Strict time order");
749 if (cap_comment) print_stats_table_header_label("Capture comment");
750 if (cap_file_more_info) {
751 print_stats_table_header_label("Capture hardware");
752 print_stats_table_header_label("Capture oper-sys");
753 print_stats_table_header_label("Capture application");
760 print_stats_table(const gchar *filename, capture_info *cf_info)
762 const gchar *file_type_string, *file_encap_string;
764 /* Build printable strings for various stats */
765 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
766 file_encap_string = wtap_encap_string(cf_info->file_encap);
770 printf("%s", filename);
777 printf("%s", file_type_string);
781 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
782 * Output a line for each different encap with all fields repeated except
783 * the encapsulation field which has "Per Packet: ..." for each
784 * encapsulation type seen ?
786 if (cap_file_encap) {
789 printf("%s", file_encap_string);
793 if (cap_file_more_info) {
796 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
803 if (cf_info->snap_set)
804 printf("%u", cf_info->snaplen);
808 if (cf_info->snaplen_max_inferred > 0) {
811 printf("%u", cf_info->snaplen_min_inferred);
815 printf("%u", cf_info->snaplen_max_inferred);
830 if (cap_packet_count) {
833 printf("%u", cf_info->packet_count);
840 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
847 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
854 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
858 if (cap_start_time) {
861 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
868 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
872 if (cap_data_rate_byte) {
875 if (cf_info->times_known)
876 printf("%.2f", cf_info->data_rate);
882 if (cap_data_rate_bit) {
885 if (cf_info->times_known)
886 printf("%.2f", cf_info->data_rate*8);
892 if (cap_packet_size) {
895 printf("%.2f", cf_info->packet_size);
899 if (cap_packet_rate) {
902 if (cf_info->times_known)
903 printf("%.2f", cf_info->packet_rate);
909 #ifdef HAVE_LIBGCRYPT
910 if (cap_file_hashes) {
913 printf("%s", file_sha1);
918 printf("%s", file_rmd160);
923 printf("%s", file_md5);
926 #endif /* HAVE_LIBGCRYPT */
931 printf("%s", order_string(cf_info->order));
935 /* this is silly to put into a table format, but oh well */
939 printf("%s", cf_info->comment);
943 if (cap_file_more_info) {
946 printf("%s", cf_info->hardware);
951 printf("%s", cf_info->os);
956 printf("%s", cf_info->usr_appl);
964 cleanup_capture_info(capture_info *cf_info)
967 g_assert(cf_info != NULL);
969 g_free(cf_info->comment);
970 cf_info->comment = NULL;
972 g_free(cf_info->hardware);
973 cf_info->hardware = NULL;
978 g_free(cf_info->usr_appl);
979 cf_info->usr_appl = NULL;
981 g_free(cf_info->encap_counts);
982 cf_info->encap_counts = NULL;
984 g_free(cf_info->interface_ids);
985 cf_info->interface_ids = NULL;
987 if (cf_info->idb_info_strings) {
988 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
989 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
992 g_array_free(cf_info->idb_info_strings, TRUE);
994 cf_info->idb_info_strings = NULL;
997 /* multi-line comments would conflict with the formatting that capinfos uses
998 we replace linefeeds with spaces */
1000 string_replace_newlines(gchar *str)
1006 while (*p != '\0') {
1018 process_cap_file(wtap *wth, const char *filename)
1028 guint32 snaplen_min_inferred = 0xffffffff;
1029 guint32 snaplen_max_inferred = 0;
1030 const struct wtap_pkthdr *phdr;
1031 capture_info cf_info;
1032 gboolean have_times = TRUE;
1033 nstime_t start_time;
1034 int start_time_tsprec;
1036 int stop_time_tsprec;
1039 gboolean know_order = FALSE;
1040 order_t order = IN_ORDER;
1041 const wtapng_section_t *shb_inf;
1043 wtapng_iface_descriptions_t *idb_info;
1045 g_assert(wth != NULL);
1046 g_assert(filename != NULL);
1048 nstime_set_zero(&start_time);
1049 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1050 nstime_set_zero(&stop_time);
1051 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1052 nstime_set_zero(&cur_time);
1053 nstime_set_zero(&prev_time);
1055 cf_info.comment = NULL;
1056 cf_info.hardware = NULL;
1058 cf_info.usr_appl = NULL;
1060 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1062 idb_info = wtap_file_get_idb_info(wth);
1064 g_assert(idb_info->interface_data != NULL);
1066 cf_info.num_interfaces = idb_info->interface_data->len;
1067 cf_info.interface_ids = g_new0(guint32, cf_info.num_interfaces);
1068 cf_info.pkt_interface_id_unknown = 0;
1070 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1072 /* get IDB info strings */
1073 for (i = 0; i < cf_info.num_interfaces; i++) {
1074 const wtapng_if_descr_t *if_descr = &g_array_index(idb_info->interface_data, wtapng_if_descr_t, i);
1075 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1076 g_array_append_val(cf_info.idb_info_strings, s);
1082 /* Tally up data that we need to parse through the file to find */
1083 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1084 phdr = wtap_phdr(wth);
1085 if (phdr->presence_flags & WTAP_HAS_TS) {
1086 prev_time = cur_time;
1087 cur_time = phdr->ts;
1089 start_time = phdr->ts;
1090 start_time_tsprec = phdr->pkt_tsprec;
1091 stop_time = phdr->ts;
1092 stop_time_tsprec = phdr->pkt_tsprec;
1093 prev_time = phdr->ts;
1095 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1096 order = NOT_IN_ORDER;
1098 if (nstime_cmp(&cur_time, &start_time) < 0) {
1099 start_time = cur_time;
1100 start_time_tsprec = phdr->pkt_tsprec;
1102 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1103 stop_time = cur_time;
1104 stop_time_tsprec = phdr->pkt_tsprec;
1107 have_times = FALSE; /* at least one packet has no time stamp */
1108 if (order != NOT_IN_ORDER)
1109 order = ORDER_UNKNOWN;
1112 if (phdr->rec_type == REC_TYPE_PACKET) {
1116 /* If caplen < len for a rcd, then presumably */
1117 /* 'Limit packet capture length' was done for this rcd. */
1118 /* Keep track as to the min/max actual snapshot lengths */
1119 /* seen for this file. */
1120 if (phdr->caplen < phdr->len) {
1121 if (phdr->caplen < snaplen_min_inferred)
1122 snaplen_min_inferred = phdr->caplen;
1123 if (phdr->caplen > snaplen_max_inferred)
1124 snaplen_max_inferred = phdr->caplen;
1127 /* Per-packet encapsulation */
1128 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1129 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1130 cf_info.encap_counts[phdr->pkt_encap] += 1;
1132 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1133 phdr->pkt_encap, packet, filename);
1137 /* Packet interface_id info */
1138 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1139 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1140 if (phdr->interface_id < cf_info.num_interfaces) {
1141 cf_info.interface_ids[phdr->interface_id] += 1;
1144 cf_info.pkt_interface_id_unknown += 1;
1148 /* it's for interface_id 0 */
1149 if (cf_info.num_interfaces != 0) {
1150 cf_info.interface_ids[0] += 1;
1153 cf_info.pkt_interface_id_unknown += 1;
1162 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1163 packet, filename, wtap_strerror(err));
1164 if (err == WTAP_ERR_SHORT_READ) {
1165 /* Don't give up completely with this one. */
1168 " (will continue anyway, checksums might be incorrect)\n");
1170 if (err_info != NULL) {
1171 fprintf(stderr, "(%s)\n", err_info);
1175 cleanup_capture_info(&cf_info);
1181 size = wtap_file_size(wth, &err);
1184 "capinfos: Can't get size of \"%s\": %s.\n",
1185 filename, g_strerror(err));
1186 cleanup_capture_info(&cf_info);
1190 cf_info.filesize = size;
1193 cf_info.file_type = wtap_file_type_subtype(wth);
1194 cf_info.iscompressed = wtap_iscompressed(wth);
1196 /* File Encapsulation */
1197 cf_info.file_encap = wtap_file_encap(wth);
1199 cf_info.file_tsprec = wtap_file_tsprec(wth);
1201 /* Packet size limit (snaplen) */
1202 cf_info.snaplen = wtap_snapshot_length(wth);
1203 if (cf_info.snaplen > 0)
1204 cf_info.snap_set = TRUE;
1206 cf_info.snap_set = FALSE;
1208 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1209 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1212 cf_info.packet_count = packet;
1215 cf_info.times_known = have_times;
1216 cf_info.start_time = start_time;
1217 cf_info.start_time_tsprec = start_time_tsprec;
1218 cf_info.stop_time = stop_time;
1219 cf_info.stop_time_tsprec = stop_time_tsprec;
1220 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1221 /* Duration precision is the higher of the start and stop time precisions. */
1222 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1223 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1225 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1226 cf_info.know_order = know_order;
1227 cf_info.order = order;
1229 /* Number of packet bytes */
1230 cf_info.packet_bytes = bytes;
1232 cf_info.data_rate = 0.0;
1233 cf_info.packet_rate = 0.0;
1234 cf_info.packet_size = 0.0;
1237 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1238 if (delta_time > 0.0) {
1239 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1240 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1242 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1245 shb_inf = wtap_file_get_shb(wth);
1247 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
1248 cf_info.comment = g_strdup(shb_inf->opt_comment);
1249 cf_info.hardware = g_strdup(shb_inf->shb_hardware);
1250 cf_info.os = g_strdup(shb_inf->shb_os);
1251 cf_info.usr_appl = g_strdup(shb_inf->shb_user_appl);
1254 string_replace_newlines(cf_info.comment);
1255 string_replace_newlines(cf_info.hardware);
1256 string_replace_newlines(cf_info.os);
1257 string_replace_newlines(cf_info.usr_appl);
1260 print_stats(filename, &cf_info);
1262 print_stats_table(filename, &cf_info);
1265 cleanup_capture_info(&cf_info);
1271 print_usage(FILE *output)
1273 fprintf(output, "\n");
1274 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1275 fprintf(output, "\n");
1276 fprintf(output, "General infos:\n");
1277 fprintf(output, " -t display the capture file type\n");
1278 fprintf(output, " -E display the capture file encapsulation\n");
1279 fprintf(output, " -I display the capture file interface information\n");
1280 fprintf(output, " -F display additional capture file information\n");
1281 #ifdef HAVE_LIBGCRYPT
1282 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1284 fprintf(output, " -k display the capture comment\n");
1285 fprintf(output, "\n");
1286 fprintf(output, "Size infos:\n");
1287 fprintf(output, " -c display the number of packets\n");
1288 fprintf(output, " -s display the size of the file (in bytes)\n");
1289 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1290 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1291 fprintf(output, "\n");
1292 fprintf(output, "Time infos:\n");
1293 fprintf(output, " -u display the capture duration (in seconds)\n");
1294 fprintf(output, " -a display the capture start time\n");
1295 fprintf(output, " -e display the capture end time\n");
1296 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1297 fprintf(output, " -S display start and end times as seconds\n");
1298 fprintf(output, "\n");
1299 fprintf(output, "Statistic infos:\n");
1300 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1301 fprintf(output, " -i display average data rate (in bits/sec)\n");
1302 fprintf(output, " -z display average packet size (in bytes)\n");
1303 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1304 fprintf(output, "\n");
1305 fprintf(output, "Output format:\n");
1306 fprintf(output, " -L generate long report (default)\n");
1307 fprintf(output, " -T generate table report\n");
1308 fprintf(output, " -M display machine-readable values in long reports\n");
1309 fprintf(output, "\n");
1310 fprintf(output, "Table report options:\n");
1311 fprintf(output, " -R generate header record (default)\n");
1312 fprintf(output, " -r do not generate header record\n");
1313 fprintf(output, "\n");
1314 fprintf(output, " -B separate infos with TAB character (default)\n");
1315 fprintf(output, " -m separate infos with comma (,) character\n");
1316 fprintf(output, " -b separate infos with SPACE character\n");
1317 fprintf(output, "\n");
1318 fprintf(output, " -N do not quote infos (default)\n");
1319 fprintf(output, " -q quote infos with single quotes (')\n");
1320 fprintf(output, " -Q quote infos with double quotes (\")\n");
1321 fprintf(output, "\n");
1322 fprintf(output, "Miscellaneous:\n");
1323 fprintf(output, " -h display this help and exit\n");
1324 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1325 fprintf(output, " -A generate all infos (default)\n");
1326 fprintf(output, "\n");
1327 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1328 fprintf(output, "or adding to earlier options.\n");
1329 fprintf(output, "\n");
1330 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1331 fprintf(output, "output format.\n");
1332 #ifndef HAVE_LIBGCRYPT
1333 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1339 * Don't report failures to load plugins because most (non-wiretap) plugins
1340 * *should* fail to load (because we're not linked against libwireshark and
1341 * dissector plugins need libwireshark).
1344 failure_message(const char *msg_format _U_, va_list ap _U_)
1350 #ifdef HAVE_LIBGCRYPT
1352 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1355 for (i = 0; i < (int) length; i++) {
1356 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1359 #endif /* HAVE_LIBGCRYPT */
1362 get_capinfos_compiled_info(GString *str)
1365 g_string_append(str, ", ");
1367 g_string_append(str, "with libz ");
1369 g_string_append(str, ZLIB_VERSION);
1370 #else /* ZLIB_VERSION */
1371 g_string_append(str, "(version unknown)");
1372 #endif /* ZLIB_VERSION */
1373 #else /* HAVE_LIBZ */
1374 g_string_append(str, "without libz");
1375 #endif /* HAVE_LIBZ */
1379 get_capinfos_runtime_info(GString *str)
1382 #if defined(HAVE_LIBZ) && !defined(_WIN32)
1383 g_string_append_printf(str, ", with libz %s", zlibVersion());
1388 main(int argc, char *argv[])
1390 GString *comp_info_str;
1391 GString *runtime_info_str;
1396 int overall_error_status;
1397 static const struct option long_options[] = {
1398 {"help", no_argument, NULL, 'h'},
1399 {"version", no_argument, NULL, 'v'},
1405 char *init_progfile_dir_error;
1407 #ifdef HAVE_LIBGCRYPT
1409 char *hash_buf = NULL;
1410 gcry_md_hd_t hd = NULL;
1414 /* Set the C-language locale to the native environment. */
1415 setlocale(LC_ALL, "");
1417 /* Get the compile-time version information string */
1418 comp_info_str = get_compiled_version_info(NULL, get_capinfos_compiled_info);
1420 /* Get the run-time version information string */
1421 runtime_info_str = get_runtime_version_info(get_capinfos_runtime_info);
1423 /* Add it to the information to be reported on a crash. */
1424 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1429 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1432 arg_list_utf_16to8(argc, argv);
1433 create_app_running_mutex();
1437 * Get credential information for later use.
1439 init_process_policies();
1440 init_open_routines();
1443 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1444 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1445 g_free(init_progfile_dir_error);
1447 /* Register all the plugin types we have. */
1448 wtap_register_plugin_types(); /* Types known to libwiretap */
1450 init_report_err(failure_message, NULL, NULL, NULL);
1452 /* Scan for plugins. This does *not* call their registration routines;
1453 that's done later. */
1456 /* Register all libwiretap plugin modules. */
1457 register_all_wiretap_modules();
1461 /* Process the options */
1462 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1463 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1468 if (report_all_infos) disable_all_infos();
1469 cap_file_type = TRUE;
1473 if (report_all_infos) disable_all_infos();
1474 cap_file_encap = TRUE;
1478 if (report_all_infos) disable_all_infos();
1483 if (report_all_infos) disable_all_infos();
1484 cap_packet_count = TRUE;
1488 if (report_all_infos) disable_all_infos();
1489 cap_file_size = TRUE;
1493 if (report_all_infos) disable_all_infos();
1494 cap_data_size = TRUE;
1498 if (report_all_infos) disable_all_infos();
1499 cap_duration = TRUE;
1503 if (report_all_infos) disable_all_infos();
1504 cap_start_time = TRUE;
1508 if (report_all_infos) disable_all_infos();
1509 cap_end_time = TRUE;
1513 time_as_secs = TRUE;
1517 if (report_all_infos) disable_all_infos();
1518 cap_data_rate_byte = TRUE;
1522 if (report_all_infos) disable_all_infos();
1523 cap_data_rate_bit = TRUE;
1527 if (report_all_infos) disable_all_infos();
1528 cap_packet_size = TRUE;
1532 if (report_all_infos) disable_all_infos();
1533 cap_packet_rate = TRUE;
1536 #ifdef HAVE_LIBGCRYPT
1538 if (report_all_infos) disable_all_infos();
1539 cap_file_hashes = TRUE;
1544 if (report_all_infos) disable_all_infos();
1549 if (report_all_infos) disable_all_infos();
1554 if (report_all_infos) disable_all_infos();
1555 cap_file_more_info = TRUE;
1559 if (report_all_infos) disable_all_infos();
1560 cap_file_idb = TRUE;
1564 continue_after_wtap_open_offline_failure = FALSE;
1576 long_report = FALSE;
1580 machine_readable = TRUE;
1584 table_report_header = TRUE;
1588 table_report_header = FALSE;
1604 field_separator = '\t';
1608 field_separator = ',';
1612 field_separator = ' ';
1616 printf("Capinfos (Wireshark) %s\n"
1617 "Print various information (infos) about capture files.\n"
1618 "See https://www.wireshark.org for more information.\n",
1619 get_ws_vcs_version_info());
1620 print_usage(stdout);
1625 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1626 g_string_free(comp_info_str, TRUE);
1627 g_string_free(runtime_info_str, TRUE);
1631 case '?': /* Bad flag - print usage message */
1632 print_usage(stderr);
1638 if ((argc - optind) < 1) {
1639 print_usage(stderr);
1643 if (!long_report && table_report_header) {
1644 print_stats_table_header();
1647 #ifdef HAVE_LIBGCRYPT
1648 if (cap_file_hashes) {
1649 gcry_check_version(NULL);
1650 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1652 gcry_md_enable(hd, GCRY_MD_RMD160);
1653 gcry_md_enable(hd, GCRY_MD_MD5);
1655 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1659 overall_error_status = 0;
1661 for (opt = optind; opt < argc; opt++) {
1663 #ifdef HAVE_LIBGCRYPT
1664 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1665 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1666 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1668 if (cap_file_hashes) {
1669 fh = ws_fopen(argv[opt], "rb");
1671 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1672 gcry_md_write(hd, hash_buf, hash_bytes);
1675 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1676 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1677 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1680 if (hd) gcry_md_reset(hd);
1682 #endif /* HAVE_LIBGCRYPT */
1684 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1687 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1688 wtap_strerror(err));
1689 if (err_info != NULL) {
1690 fprintf(stderr, "(%s)\n", err_info);
1693 overall_error_status = 1; /* remember that an error has occurred */
1694 if (!continue_after_wtap_open_offline_failure)
1695 exit(1); /* error status */
1699 if ((opt > optind) && (long_report))
1701 status = process_cap_file(wth, argv[opt]);
1709 return overall_error_status;
1713 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1718 * indent-tabs-mode: nil
1721 * vi: set shiftwidth=2 tabstop=8 expandtab:
1722 * :indentSize=2:tabSize=8:noTabs=true: