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).
77 #include <zlib.h> /* to get the libz version number */
80 #include <wsutil/crash_info.h>
81 #include <wsutil/filesystem.h>
82 #include <wsutil/privileges.h>
83 #include <wsutil/ws_diag_control.h>
84 #include <wsutil/ws_version_info.h>
87 #include <wsutil/plugins.h>
91 #include <wsutil/report_err.h>
92 #include <wsutil/str_util.h>
93 #include <wsutil/file_util.h>
96 #include <wsutil/wsgcrypt.h>
99 #ifndef HAVE_GETOPT_LONG
100 #include "wsutil/wsgetopt.h"
104 #include <wsutil/unicode-utils.h>
108 * By default capinfos now continues processing
109 * the next filename if and when wiretap detects
110 * a problem opening a file.
111 * Use the '-C' option to revert back to original
112 * capinfos behavior which is to abort any
113 * additional file processing at first open file
117 static gboolean continue_after_wtap_open_offline_failure = TRUE;
120 * table report variables
123 static gboolean long_report = TRUE; /* By default generate long report */
124 static gchar table_report_header = TRUE; /* Generate column header by default */
125 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
126 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
127 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
130 * capinfos has the ability to report on a number of
131 * various characteristics ("infos") for each input file.
133 * By default reporting of all info fields is enabled.
135 * Optionally the reporting of any specific info field
136 * or combination of info fields can be enabled with
137 * individual options.
140 static gboolean report_all_infos = TRUE; /* Report all infos */
142 static gboolean cap_file_type = TRUE; /* Report capture type */
143 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
144 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
145 static gboolean cap_packet_count = TRUE; /* Report packet count */
146 static gboolean cap_file_size = TRUE; /* Report file size */
147 static gboolean cap_comment = TRUE; /* Display the capture comment */
148 static gboolean cap_file_more_info = TRUE; /* Report more file info */
149 static gboolean cap_file_idb = TRUE; /* Report Interface info */
151 static gboolean cap_data_size = TRUE; /* Report packet byte size */
152 static gboolean cap_duration = TRUE; /* Report capture duration */
153 static gboolean cap_start_time = TRUE; /* Report capture start time */
154 static gboolean cap_end_time = TRUE; /* Report capture end time */
155 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
157 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
158 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
159 static gboolean cap_packet_size = TRUE; /* Report average packet size */
160 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
161 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
163 #ifdef HAVE_LIBGCRYPT
164 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
166 #define HASH_SIZE_SHA1 20
167 #define HASH_SIZE_RMD160 20
168 #define HASH_SIZE_MD5 16
170 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
171 #define HASH_BUF_SIZE (1024 * 1024)
174 static gchar file_sha1[HASH_STR_SIZE];
175 static gchar file_rmd160[HASH_STR_SIZE];
176 static gchar file_md5[HASH_STR_SIZE];
178 #define FILE_HASH_OPT "H"
180 #define FILE_HASH_OPT ""
181 #endif /* HAVE_LIBGCRYPT */
184 * If we have at least two packets with time stamps, and they're not in
185 * order - i.e., the later packet has a time stamp older than the earlier
186 * packet - the time stamps are known not to be in order.
188 * If every packet has a time stamp, and they're all in order, the time
189 * stamp is known to be in order.
191 * Otherwise, we have no idea.
199 typedef struct _capture_info {
200 const char *filename;
202 gboolean iscompressed;
211 guint64 packet_bytes;
212 gboolean times_known;
214 int start_time_tsprec;
216 int stop_time_tsprec;
217 guint32 packet_count;
218 gboolean snap_set; /* If set in capture file header */
219 guint32 snaplen; /* value from the capture file header */
220 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
221 guint32 snaplen_max_inferred; /* ... */
222 gboolean drops_known;
229 double data_rate; /* in bytes */
233 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
235 guint num_interfaces; /* number of IDBs, and thus size of interface_ids array */
236 guint32 *interface_ids; /* array of per_packet interface_id counts; one entry per file IDB */
237 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
238 GArray *idb_info_strings; /* array of IDB info strings */
243 enable_all_infos(void)
245 report_all_infos = TRUE;
247 cap_file_type = TRUE;
248 cap_file_encap = TRUE;
250 cap_packet_count = TRUE;
251 cap_file_size = TRUE;
253 cap_file_more_info = TRUE;
256 cap_data_size = TRUE;
258 cap_start_time = TRUE;
262 cap_data_rate_byte = TRUE;
263 cap_data_rate_bit = TRUE;
264 cap_packet_size = TRUE;
265 cap_packet_rate = TRUE;
267 #ifdef HAVE_LIBGCRYPT
268 cap_file_hashes = TRUE;
269 #endif /* HAVE_LIBGCRYPT */
273 disable_all_infos(void)
275 report_all_infos = FALSE;
277 cap_file_type = FALSE;
278 cap_file_encap = FALSE;
280 cap_packet_count = FALSE;
281 cap_file_size = FALSE;
283 cap_file_more_info = FALSE;
284 cap_file_idb = FALSE;
286 cap_data_size = FALSE;
287 cap_duration = FALSE;
288 cap_start_time = FALSE;
289 cap_end_time = FALSE;
292 cap_data_rate_byte = FALSE;
293 cap_data_rate_bit = FALSE;
294 cap_packet_size = FALSE;
295 cap_packet_rate = FALSE;
297 #ifdef HAVE_LIBGCRYPT
298 cap_file_hashes = FALSE;
299 #endif /* HAVE_LIBGCRYPT */
303 order_string(order_t order)
317 return "???"; /* "cannot happen" (the next step is "Profit!") */
322 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
324 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
327 if (cf_info->times_known && cf_info->packet_count > 0) {
329 switch (tsprecision) {
331 case WTAP_TSPREC_SEC:
332 g_snprintf(time_string_buf, sizeof time_string_buf,
334 (unsigned long)timer->secs);
337 case WTAP_TSPREC_DSEC:
338 g_snprintf(time_string_buf, sizeof time_string_buf,
340 (unsigned long)timer->secs,
341 timer->nsecs / 100000000);
344 case WTAP_TSPREC_CSEC:
345 g_snprintf(time_string_buf, sizeof time_string_buf,
347 (unsigned long)timer->secs,
348 timer->nsecs / 10000000);
351 case WTAP_TSPREC_MSEC:
352 g_snprintf(time_string_buf, sizeof time_string_buf,
354 (unsigned long)timer->secs,
355 timer->nsecs / 1000000);
358 case WTAP_TSPREC_USEC:
359 g_snprintf(time_string_buf, sizeof time_string_buf,
361 (unsigned long)timer->secs,
362 timer->nsecs / 1000);
365 case WTAP_TSPREC_NSEC:
366 g_snprintf(time_string_buf, sizeof time_string_buf,
368 (unsigned long)timer->secs,
373 g_snprintf(time_string_buf, sizeof time_string_buf,
374 "Unknown precision %d",
378 return time_string_buf;
380 ti_tm = localtime(&timer->secs);
382 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
383 return time_string_buf;
385 switch (tsprecision) {
387 case WTAP_TSPREC_SEC:
388 g_snprintf(time_string_buf, sizeof time_string_buf,
389 "%04d-%02d-%02d %02d:%02d:%02d",
390 ti_tm->tm_year + 1900,
398 case WTAP_TSPREC_DSEC:
399 g_snprintf(time_string_buf, sizeof time_string_buf,
400 "%04d-%02d-%02d %02d:%02d:%02d.%01d",
401 ti_tm->tm_year + 1900,
407 timer->nsecs / 100000000);
410 case WTAP_TSPREC_CSEC:
411 g_snprintf(time_string_buf, sizeof time_string_buf,
412 "%04d-%02d-%02d %02d:%02d:%02d.%02d",
413 ti_tm->tm_year + 1900,
419 timer->nsecs / 10000000);
422 case WTAP_TSPREC_MSEC:
423 g_snprintf(time_string_buf, sizeof time_string_buf,
424 "%04d-%02d-%02d %02d:%02d:%02d.%03d",
425 ti_tm->tm_year + 1900,
431 timer->nsecs / 1000000);
434 case WTAP_TSPREC_USEC:
435 g_snprintf(time_string_buf, sizeof time_string_buf,
436 "%04d-%02d-%02d %02d:%02d:%02d.%06d",
437 ti_tm->tm_year + 1900,
443 timer->nsecs / 1000);
446 case WTAP_TSPREC_NSEC:
447 g_snprintf(time_string_buf, sizeof time_string_buf,
448 "%04d-%02d-%02d %02d:%02d:%02d.%09d",
449 ti_tm->tm_year + 1900,
459 g_snprintf(time_string_buf, sizeof time_string_buf,
460 "Unknown precision %d",
464 return time_string_buf;
468 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
469 return time_string_buf;
473 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
475 const gchar *second = want_seconds ? " second" : "";
476 const gchar *plural = want_seconds ? "s" : "";
477 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
479 if (cf_info->times_known && cf_info->packet_count > 0) {
480 switch (tsprecision) {
482 case WTAP_TSPREC_SEC:
483 g_snprintf(time_string_buf, sizeof time_string_buf,
485 (unsigned long)timer->secs,
487 timer->secs == 1 ? "" : plural);
490 case WTAP_TSPREC_DSEC:
491 g_snprintf(time_string_buf, sizeof time_string_buf,
493 (unsigned long)timer->secs,
494 timer->nsecs / 100000000,
496 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
499 case WTAP_TSPREC_CSEC:
500 g_snprintf(time_string_buf, sizeof time_string_buf,
502 (unsigned long)timer->secs,
503 timer->nsecs / 10000000,
505 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
508 case WTAP_TSPREC_MSEC:
509 g_snprintf(time_string_buf, sizeof time_string_buf,
511 (unsigned long)timer->secs,
512 timer->nsecs / 1000000,
514 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
517 case WTAP_TSPREC_USEC:
518 g_snprintf(time_string_buf, sizeof time_string_buf,
520 (unsigned long)timer->secs,
523 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
526 case WTAP_TSPREC_NSEC:
527 g_snprintf(time_string_buf, sizeof time_string_buf,
529 (unsigned long)timer->secs,
532 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
536 g_snprintf(time_string_buf, sizeof time_string_buf,
537 "Unknown precision %d",
541 return time_string_buf;
544 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
545 return time_string_buf;
548 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
550 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
552 printf("%sn/a\n", text_p1);
556 print_stats(const gchar *filename, capture_info *cf_info)
558 const gchar *file_type_string, *file_encap_string;
561 /* Build printable strings for various stats */
562 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
563 file_encap_string = wtap_encap_string(cf_info->file_encap);
565 if (filename) printf ("File name: %s\n", filename);
566 if (cap_file_type) printf ("File type: %s%s\n",
568 cf_info->iscompressed ? " (gzip compressed)" : "");
570 if (cap_file_encap) {
571 printf ("File encapsulation: %s\n", file_encap_string);
572 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
574 printf ("Encapsulation in use by packets (# of pkts):\n");
575 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
576 if (cf_info->encap_counts[i] > 0)
578 wtap_encap_string(i), cf_info->encap_counts[i]);
582 if (cap_file_more_info) {
583 printf ("File timestamp precision: %s (%d)\n",
584 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
587 if (cap_snaplen && cf_info->snap_set)
588 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
589 else if (cap_snaplen && !cf_info->snap_set)
590 printf ("Packet size limit: file hdr: (not set)\n");
591 if (cf_info->snaplen_max_inferred > 0) {
592 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
593 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
595 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
596 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
598 if (cap_packet_count) {
599 printf ("Number of packets: ");
600 if (machine_readable) {
601 printf ("%u\n", cf_info->packet_count);
603 size_string = format_size(cf_info->packet_count, format_size_unit_none);
604 printf ("%s\n", size_string);
609 printf ("File size: ");
610 if (machine_readable) {
611 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
613 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
614 printf ("%s\n", size_string);
619 printf ("Data size: ");
620 if (machine_readable) {
621 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
623 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
624 printf ("%s\n", size_string);
628 if (cf_info->times_known) {
629 if (cap_duration) /* XXX - shorten to hh:mm:ss */
630 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
632 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
634 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
635 if (cap_data_rate_byte) {
636 printf("Data byte rate: ");
637 if (machine_readable) {
638 print_value("", 2, " bytes/sec", cf_info->data_rate);
640 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
641 printf ("%s\n", size_string);
645 if (cap_data_rate_bit) {
646 printf("Data bit rate: ");
647 if (machine_readable) {
648 print_value("", 2, " bits/sec", cf_info->data_rate*8);
650 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
651 printf ("%s\n", size_string);
656 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
657 if (cf_info->times_known) {
658 if (cap_packet_rate) {
659 printf("Average packet rate: ");
660 if (machine_readable) {
661 print_value("", 2, " packets/sec", cf_info->packet_rate);
663 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
664 printf ("%s\n", size_string);
669 #ifdef HAVE_LIBGCRYPT
670 if (cap_file_hashes) {
671 printf ("SHA1: %s\n", file_sha1);
672 printf ("RIPEMD160: %s\n", file_rmd160);
673 printf ("MD5: %s\n", file_md5);
675 #endif /* HAVE_LIBGCRYPT */
676 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
677 if (cap_comment && cf_info->comment)
678 printf ("Capture comment: %s\n", cf_info->comment);
679 if (cap_file_more_info) {
680 if (cf_info->hardware)
681 printf ("Capture hardware: %s\n", cf_info->hardware);
683 printf ("Capture oper-sys: %s\n", cf_info->os);
684 if (cf_info->usr_appl)
685 printf ("Capture application: %s\n", cf_info->usr_appl);
688 if (cap_file_idb && cf_info->num_interfaces != 0) {
690 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
691 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
692 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
693 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
694 printf ("Interface #%u info:\n", i);
696 printf (" Number of packets = %u\n", cf_info->interface_ids[i]);
704 if (field_separator) putchar(field_separator);
710 if (quote_char) putchar(quote_char);
714 print_stats_table_header_label(const gchar *label)
723 print_stats_table_header(void)
729 if (cap_file_type) print_stats_table_header_label("File type");
730 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
731 if (cap_file_more_info) print_stats_table_header_label("File time precision");
732 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
733 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
734 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
735 if (cap_packet_count) print_stats_table_header_label("Number of packets");
736 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
737 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
738 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
739 if (cap_start_time) print_stats_table_header_label("Start time");
740 if (cap_end_time) print_stats_table_header_label("End time");
741 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
742 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
743 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
744 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
745 #ifdef HAVE_LIBGCRYPT
746 if (cap_file_hashes) {
747 print_stats_table_header_label("SHA1");
748 print_stats_table_header_label("RIPEMD160");
749 print_stats_table_header_label("MD5");
751 #endif /* HAVE_LIBGCRYPT */
752 if (cap_order) print_stats_table_header_label("Strict time order");
753 if (cap_comment) print_stats_table_header_label("Capture comment");
754 if (cap_file_more_info) {
755 print_stats_table_header_label("Capture hardware");
756 print_stats_table_header_label("Capture oper-sys");
757 print_stats_table_header_label("Capture application");
764 print_stats_table(const gchar *filename, capture_info *cf_info)
766 const gchar *file_type_string, *file_encap_string;
768 /* Build printable strings for various stats */
769 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
770 file_encap_string = wtap_encap_string(cf_info->file_encap);
774 printf("%s", filename);
781 printf("%s", file_type_string);
785 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
786 * Output a line for each different encap with all fields repeated except
787 * the encapsulation field which has "Per Packet: ..." for each
788 * encapsulation type seen ?
790 if (cap_file_encap) {
793 printf("%s", file_encap_string);
797 if (cap_file_more_info) {
800 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
807 if (cf_info->snap_set)
808 printf("%u", cf_info->snaplen);
812 if (cf_info->snaplen_max_inferred > 0) {
815 printf("%u", cf_info->snaplen_min_inferred);
819 printf("%u", cf_info->snaplen_max_inferred);
834 if (cap_packet_count) {
837 printf("%u", cf_info->packet_count);
844 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
851 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
858 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
862 if (cap_start_time) {
865 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
872 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
876 if (cap_data_rate_byte) {
879 if (cf_info->times_known)
880 printf("%.2f", cf_info->data_rate);
886 if (cap_data_rate_bit) {
889 if (cf_info->times_known)
890 printf("%.2f", cf_info->data_rate*8);
896 if (cap_packet_size) {
899 printf("%.2f", cf_info->packet_size);
903 if (cap_packet_rate) {
906 if (cf_info->times_known)
907 printf("%.2f", cf_info->packet_rate);
913 #ifdef HAVE_LIBGCRYPT
914 if (cap_file_hashes) {
917 printf("%s", file_sha1);
922 printf("%s", file_rmd160);
927 printf("%s", file_md5);
930 #endif /* HAVE_LIBGCRYPT */
935 printf("%s", order_string(cf_info->order));
939 /* this is silly to put into a table format, but oh well */
943 printf("%s", cf_info->comment);
947 if (cap_file_more_info) {
950 printf("%s", cf_info->hardware);
955 printf("%s", cf_info->os);
960 printf("%s", cf_info->usr_appl);
968 cleanup_capture_info(capture_info *cf_info)
971 g_assert(cf_info != NULL);
973 g_free(cf_info->comment);
974 cf_info->comment = NULL;
976 g_free(cf_info->hardware);
977 cf_info->hardware = NULL;
982 g_free(cf_info->usr_appl);
983 cf_info->usr_appl = NULL;
985 g_free(cf_info->encap_counts);
986 cf_info->encap_counts = NULL;
988 g_free(cf_info->interface_ids);
989 cf_info->interface_ids = NULL;
991 if (cf_info->idb_info_strings) {
992 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
993 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
996 g_array_free(cf_info->idb_info_strings, TRUE);
998 cf_info->idb_info_strings = NULL;
1001 /* multi-line comments would conflict with the formatting that capinfos uses
1002 we replace linefeeds with spaces */
1004 string_replace_newlines(gchar *str)
1010 while (*p != '\0') {
1022 process_cap_file(wtap *wth, const char *filename)
1032 guint32 snaplen_min_inferred = 0xffffffff;
1033 guint32 snaplen_max_inferred = 0;
1034 const struct wtap_pkthdr *phdr;
1035 capture_info cf_info;
1036 gboolean have_times = TRUE;
1037 nstime_t start_time;
1038 int start_time_tsprec;
1040 int stop_time_tsprec;
1043 gboolean know_order = FALSE;
1044 order_t order = IN_ORDER;
1045 const wtapng_section_t *shb_inf;
1047 wtapng_iface_descriptions_t *idb_info;
1049 g_assert(wth != NULL);
1050 g_assert(filename != NULL);
1052 nstime_set_zero(&start_time);
1053 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1054 nstime_set_zero(&stop_time);
1055 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1056 nstime_set_zero(&cur_time);
1057 nstime_set_zero(&prev_time);
1059 cf_info.comment = NULL;
1060 cf_info.hardware = NULL;
1062 cf_info.usr_appl = NULL;
1064 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1066 idb_info = wtap_file_get_idb_info(wth);
1068 g_assert(idb_info->interface_data != NULL);
1070 cf_info.num_interfaces = idb_info->interface_data->len;
1071 cf_info.interface_ids = g_new0(guint32, cf_info.num_interfaces);
1072 cf_info.pkt_interface_id_unknown = 0;
1074 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1076 /* get IDB info strings */
1077 for (i = 0; i < cf_info.num_interfaces; i++) {
1078 const wtapng_if_descr_t *if_descr = &g_array_index(idb_info->interface_data, wtapng_if_descr_t, i);
1079 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1080 g_array_append_val(cf_info.idb_info_strings, s);
1086 /* Tally up data that we need to parse through the file to find */
1087 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1088 phdr = wtap_phdr(wth);
1089 if (phdr->presence_flags & WTAP_HAS_TS) {
1090 prev_time = cur_time;
1091 cur_time = phdr->ts;
1093 start_time = phdr->ts;
1094 start_time_tsprec = phdr->pkt_tsprec;
1095 stop_time = phdr->ts;
1096 stop_time_tsprec = phdr->pkt_tsprec;
1097 prev_time = phdr->ts;
1099 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1100 order = NOT_IN_ORDER;
1102 if (nstime_cmp(&cur_time, &start_time) < 0) {
1103 start_time = cur_time;
1104 start_time_tsprec = phdr->pkt_tsprec;
1106 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1107 stop_time = cur_time;
1108 stop_time_tsprec = phdr->pkt_tsprec;
1111 have_times = FALSE; /* at least one packet has no time stamp */
1112 if (order != NOT_IN_ORDER)
1113 order = ORDER_UNKNOWN;
1116 if (phdr->rec_type == REC_TYPE_PACKET) {
1120 /* If caplen < len for a rcd, then presumably */
1121 /* 'Limit packet capture length' was done for this rcd. */
1122 /* Keep track as to the min/max actual snapshot lengths */
1123 /* seen for this file. */
1124 if (phdr->caplen < phdr->len) {
1125 if (phdr->caplen < snaplen_min_inferred)
1126 snaplen_min_inferred = phdr->caplen;
1127 if (phdr->caplen > snaplen_max_inferred)
1128 snaplen_max_inferred = phdr->caplen;
1131 /* Per-packet encapsulation */
1132 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1133 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1134 cf_info.encap_counts[phdr->pkt_encap] += 1;
1136 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1137 phdr->pkt_encap, packet, filename);
1141 /* Packet interface_id info */
1142 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1143 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1144 if (phdr->interface_id < cf_info.num_interfaces) {
1145 cf_info.interface_ids[phdr->interface_id] += 1;
1148 cf_info.pkt_interface_id_unknown += 1;
1152 /* it's for interface_id 0 */
1153 if (cf_info.num_interfaces != 0) {
1154 cf_info.interface_ids[0] += 1;
1157 cf_info.pkt_interface_id_unknown += 1;
1166 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1167 packet, filename, wtap_strerror(err));
1168 if (err == WTAP_ERR_SHORT_READ) {
1169 /* Don't give up completely with this one. */
1172 " (will continue anyway, checksums might be incorrect)\n");
1174 if (err_info != NULL) {
1175 fprintf(stderr, "(%s)\n", err_info);
1179 cleanup_capture_info(&cf_info);
1185 size = wtap_file_size(wth, &err);
1188 "capinfos: Can't get size of \"%s\": %s.\n",
1189 filename, g_strerror(err));
1190 cleanup_capture_info(&cf_info);
1194 cf_info.filesize = size;
1197 cf_info.file_type = wtap_file_type_subtype(wth);
1198 cf_info.iscompressed = wtap_iscompressed(wth);
1200 /* File Encapsulation */
1201 cf_info.file_encap = wtap_file_encap(wth);
1203 cf_info.file_tsprec = wtap_file_tsprec(wth);
1205 /* Packet size limit (snaplen) */
1206 cf_info.snaplen = wtap_snapshot_length(wth);
1207 if (cf_info.snaplen > 0)
1208 cf_info.snap_set = TRUE;
1210 cf_info.snap_set = FALSE;
1212 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1213 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1216 cf_info.packet_count = packet;
1219 cf_info.times_known = have_times;
1220 cf_info.start_time = start_time;
1221 cf_info.start_time_tsprec = start_time_tsprec;
1222 cf_info.stop_time = stop_time;
1223 cf_info.stop_time_tsprec = stop_time_tsprec;
1224 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1225 /* Duration precision is the higher of the start and stop time precisions. */
1226 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1227 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1229 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1230 cf_info.know_order = know_order;
1231 cf_info.order = order;
1233 /* Number of packet bytes */
1234 cf_info.packet_bytes = bytes;
1236 cf_info.data_rate = 0.0;
1237 cf_info.packet_rate = 0.0;
1238 cf_info.packet_size = 0.0;
1241 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1242 if (delta_time > 0.0) {
1243 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1244 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1246 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1249 shb_inf = wtap_file_get_shb(wth);
1251 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
1252 cf_info.comment = g_strdup(shb_inf->opt_comment);
1253 cf_info.hardware = g_strdup(shb_inf->shb_hardware);
1254 cf_info.os = g_strdup(shb_inf->shb_os);
1255 cf_info.usr_appl = g_strdup(shb_inf->shb_user_appl);
1258 string_replace_newlines(cf_info.comment);
1259 string_replace_newlines(cf_info.hardware);
1260 string_replace_newlines(cf_info.os);
1261 string_replace_newlines(cf_info.usr_appl);
1264 print_stats(filename, &cf_info);
1266 print_stats_table(filename, &cf_info);
1269 cleanup_capture_info(&cf_info);
1275 print_usage(FILE *output)
1277 fprintf(output, "\n");
1278 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1279 fprintf(output, "\n");
1280 fprintf(output, "General infos:\n");
1281 fprintf(output, " -t display the capture file type\n");
1282 fprintf(output, " -E display the capture file encapsulation\n");
1283 fprintf(output, " -I display the capture file interface information\n");
1284 fprintf(output, " -F display additional capture file information\n");
1285 #ifdef HAVE_LIBGCRYPT
1286 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1288 fprintf(output, " -k display the capture comment\n");
1289 fprintf(output, "\n");
1290 fprintf(output, "Size infos:\n");
1291 fprintf(output, " -c display the number of packets\n");
1292 fprintf(output, " -s display the size of the file (in bytes)\n");
1293 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1294 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1295 fprintf(output, "\n");
1296 fprintf(output, "Time infos:\n");
1297 fprintf(output, " -u display the capture duration (in seconds)\n");
1298 fprintf(output, " -a display the capture start time\n");
1299 fprintf(output, " -e display the capture end time\n");
1300 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1301 fprintf(output, " -S display start and end times as seconds\n");
1302 fprintf(output, "\n");
1303 fprintf(output, "Statistic infos:\n");
1304 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1305 fprintf(output, " -i display average data rate (in bits/sec)\n");
1306 fprintf(output, " -z display average packet size (in bytes)\n");
1307 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1308 fprintf(output, "\n");
1309 fprintf(output, "Output format:\n");
1310 fprintf(output, " -L generate long report (default)\n");
1311 fprintf(output, " -T generate table report\n");
1312 fprintf(output, " -M display machine-readable values in long reports\n");
1313 fprintf(output, "\n");
1314 fprintf(output, "Table report options:\n");
1315 fprintf(output, " -R generate header record (default)\n");
1316 fprintf(output, " -r do not generate header record\n");
1317 fprintf(output, "\n");
1318 fprintf(output, " -B separate infos with TAB character (default)\n");
1319 fprintf(output, " -m separate infos with comma (,) character\n");
1320 fprintf(output, " -b separate infos with SPACE character\n");
1321 fprintf(output, "\n");
1322 fprintf(output, " -N do not quote infos (default)\n");
1323 fprintf(output, " -q quote infos with single quotes (')\n");
1324 fprintf(output, " -Q quote infos with double quotes (\")\n");
1325 fprintf(output, "\n");
1326 fprintf(output, "Miscellaneous:\n");
1327 fprintf(output, " -h display this help and exit\n");
1328 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1329 fprintf(output, " -A generate all infos (default)\n");
1330 fprintf(output, "\n");
1331 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1332 fprintf(output, "or adding to earlier options.\n");
1333 fprintf(output, "\n");
1334 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1335 fprintf(output, "output format.\n");
1336 #ifndef HAVE_LIBGCRYPT
1337 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1343 * Don't report failures to load plugins because most (non-wiretap) plugins
1344 * *should* fail to load (because we're not linked against libwireshark and
1345 * dissector plugins need libwireshark).
1348 failure_message(const char *msg_format _U_, va_list ap _U_)
1354 #ifdef HAVE_LIBGCRYPT
1356 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1359 for (i = 0; i < (int) length; i++) {
1360 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1363 #endif /* HAVE_LIBGCRYPT */
1366 get_capinfos_compiled_info(GString *str)
1369 g_string_append(str, ", ");
1371 g_string_append(str, "with libz ");
1373 g_string_append(str, ZLIB_VERSION);
1374 #else /* ZLIB_VERSION */
1375 g_string_append(str, "(version unknown)");
1376 #endif /* ZLIB_VERSION */
1377 #else /* HAVE_LIBZ */
1378 g_string_append(str, "without libz");
1379 #endif /* HAVE_LIBZ */
1383 get_capinfos_runtime_info(GString *str)
1386 #if defined(HAVE_LIBZ) && !defined(_WIN32)
1387 g_string_append_printf(str, ", with libz %s", zlibVersion());
1392 main(int argc, char *argv[])
1394 GString *comp_info_str;
1395 GString *runtime_info_str;
1400 int overall_error_status;
1402 static const struct option long_options[] = {
1403 {(char *)"help", no_argument, NULL, 'h'},
1404 {(char *)"version", no_argument, NULL, 'v'},
1411 char *init_progfile_dir_error;
1413 #ifdef HAVE_LIBGCRYPT
1415 char *hash_buf = NULL;
1416 gcry_md_hd_t hd = NULL;
1420 /* Set the C-language locale to the native environment. */
1421 setlocale(LC_ALL, "");
1423 /* Get the compile-time version information string */
1424 comp_info_str = get_compiled_version_info(NULL, get_capinfos_compiled_info);
1426 /* Get the run-time version information string */
1427 runtime_info_str = get_runtime_version_info(get_capinfos_runtime_info);
1429 /* Add it to the information to be reported on a crash. */
1430 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1435 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1438 arg_list_utf_16to8(argc, argv);
1439 create_app_running_mutex();
1443 * Get credential information for later use.
1445 init_process_policies();
1446 init_open_routines();
1449 if ((init_progfile_dir_error = init_progfile_dir(argv[0], (void *)main))) {
1450 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1451 g_free(init_progfile_dir_error);
1453 /* Register all the plugin types we have. */
1454 wtap_register_plugin_types(); /* Types known to libwiretap */
1456 init_report_err(failure_message, NULL, NULL, NULL);
1458 /* Scan for plugins. This does *not* call their registration routines;
1459 that's done later. */
1462 /* Register all libwiretap plugin modules. */
1463 register_all_wiretap_modules();
1467 /* Process the options */
1468 /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1469 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1474 if (report_all_infos) disable_all_infos();
1475 cap_file_type = TRUE;
1479 if (report_all_infos) disable_all_infos();
1480 cap_file_encap = TRUE;
1484 if (report_all_infos) disable_all_infos();
1489 if (report_all_infos) disable_all_infos();
1490 cap_packet_count = TRUE;
1494 if (report_all_infos) disable_all_infos();
1495 cap_file_size = TRUE;
1499 if (report_all_infos) disable_all_infos();
1500 cap_data_size = TRUE;
1504 if (report_all_infos) disable_all_infos();
1505 cap_duration = TRUE;
1509 if (report_all_infos) disable_all_infos();
1510 cap_start_time = TRUE;
1514 if (report_all_infos) disable_all_infos();
1515 cap_end_time = TRUE;
1519 time_as_secs = TRUE;
1523 if (report_all_infos) disable_all_infos();
1524 cap_data_rate_byte = TRUE;
1528 if (report_all_infos) disable_all_infos();
1529 cap_data_rate_bit = TRUE;
1533 if (report_all_infos) disable_all_infos();
1534 cap_packet_size = TRUE;
1538 if (report_all_infos) disable_all_infos();
1539 cap_packet_rate = TRUE;
1542 #ifdef HAVE_LIBGCRYPT
1544 if (report_all_infos) disable_all_infos();
1545 cap_file_hashes = TRUE;
1550 if (report_all_infos) disable_all_infos();
1555 if (report_all_infos) disable_all_infos();
1560 if (report_all_infos) disable_all_infos();
1561 cap_file_more_info = TRUE;
1565 if (report_all_infos) disable_all_infos();
1566 cap_file_idb = TRUE;
1570 continue_after_wtap_open_offline_failure = FALSE;
1582 long_report = FALSE;
1586 machine_readable = TRUE;
1590 table_report_header = TRUE;
1594 table_report_header = FALSE;
1610 field_separator = '\t';
1614 field_separator = ',';
1618 field_separator = ' ';
1622 printf("Capinfos (Wireshark) %s\n"
1623 "Print various information (infos) about capture files.\n"
1624 "See http://www.wireshark.org for more information.\n",
1625 get_ws_vcs_version_info());
1626 print_usage(stdout);
1631 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1632 g_string_free(comp_info_str, TRUE);
1633 g_string_free(runtime_info_str, TRUE);
1637 case '?': /* Bad flag - print usage message */
1638 print_usage(stderr);
1644 if ((argc - optind) < 1) {
1645 print_usage(stderr);
1649 if (!long_report && table_report_header) {
1650 print_stats_table_header();
1653 #ifdef HAVE_LIBGCRYPT
1654 if (cap_file_hashes) {
1655 gcry_check_version(NULL);
1656 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1658 gcry_md_enable(hd, GCRY_MD_RMD160);
1659 gcry_md_enable(hd, GCRY_MD_MD5);
1661 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1665 overall_error_status = 0;
1667 for (opt = optind; opt < argc; opt++) {
1669 #ifdef HAVE_LIBGCRYPT
1670 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1671 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1672 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1674 if (cap_file_hashes) {
1675 fh = ws_fopen(argv[opt], "rb");
1677 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1678 gcry_md_write(hd, hash_buf, hash_bytes);
1681 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1682 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1683 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1686 if (hd) gcry_md_reset(hd);
1688 #endif /* HAVE_LIBGCRYPT */
1690 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1693 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1694 wtap_strerror(err));
1695 if (err_info != NULL) {
1696 fprintf(stderr, "(%s)\n", err_info);
1699 overall_error_status = 1; /* remember that an error has occurred */
1700 if (!continue_after_wtap_open_offline_failure)
1701 exit(1); /* error status */
1705 if ((opt > optind) && (long_report))
1707 status = process_cap_file(wth, argv[opt]);
1715 return overall_error_status;
1719 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1724 * indent-tabs-mode: nil
1727 * vi: set shiftwidth=2 tabstop=8 expandtab:
1728 * :indentSize=2:tabSize=8:noTabs=true: