2 * Reports capture file information including # of packets, duration, others
4 * Copyright 2004 Ian Schorr
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * New capinfos features
30 * Continue processing additional files after
31 * a wiretap open failure. The new -C option
32 * reverts to capinfos' original behavior which
33 * is to cancel any further file processing at
34 * first file open failure.
36 * Change the behavior of how the default display
37 * of all infos is initiated. This gets rid of a
38 * special post getopt() argument count test.
40 * Add new table output format (with related options)
41 * This feature allows outputting the various infos
42 * into a tab delimited text file, or to a comma
43 * separated variables file (*.csv) instead of the
44 * original "long" format.
47 * behaviour changed: Upon exit capinfos will return
48 * an error status if an error occurred at any
49 * point during "continuous" file processing.
50 * (Previously a success status was always
51 * returned if the -C option was not used).
72 #include <wiretap/wtap.h>
74 #include <wsutil/crash_info.h>
75 #include <wsutil/filesystem.h>
76 #include <wsutil/privileges.h>
77 #include <ws_version_info.h>
78 #include <wiretap/wtap_opttypes.h>
81 #include <wsutil/plugins.h>
84 #include <wsutil/report_err.h>
85 #include <wsutil/str_util.h>
86 #include <wsutil/file_util.h>
88 #include <wsutil/wsgcrypt.h>
90 #ifndef HAVE_GETOPT_LONG
91 #include "wsutil/wsgetopt.h"
95 #include <wsutil/unicode-utils.h>
98 #define INVALID_OPTION 1
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 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
159 #define HASH_SIZE_SHA1 20
160 #define HASH_SIZE_RMD160 20
161 #define HASH_SIZE_MD5 16
163 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
164 #define HASH_BUF_SIZE (1024 * 1024)
167 static gchar file_sha1[HASH_STR_SIZE];
168 static gchar file_rmd160[HASH_STR_SIZE];
169 static gchar file_md5[HASH_STR_SIZE];
172 * If we have at least two packets with time stamps, and they're not in
173 * order - i.e., the later packet has a time stamp older than the earlier
174 * packet - the time stamps are known not to be in order.
176 * If every packet has a time stamp, and they're all in order, the time
177 * stamp is known to be in order.
179 * Otherwise, we have no idea.
187 typedef struct _capture_info {
188 const char *filename;
190 gboolean iscompressed;
195 guint64 packet_bytes;
196 gboolean times_known;
198 int start_time_tsprec;
200 int stop_time_tsprec;
201 guint32 packet_count;
202 gboolean snap_set; /* If set in capture file header */
203 guint32 snaplen; /* value from the capture file header */
204 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
205 guint32 snaplen_max_inferred; /* ... */
206 gboolean drops_known;
213 double data_rate; /* in bytes */
217 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
219 guint num_interfaces; /* number of IDBs, and thus size of interface_packet_counts array */
220 GArray *interface_packet_counts; /* array of per_packet interface_id counts; one entry per file IDB */
221 guint32 pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
222 GArray *idb_info_strings; /* array of IDB info strings */
225 static char *decimal_point;
228 enable_all_infos(void)
230 report_all_infos = TRUE;
232 cap_file_type = TRUE;
233 cap_file_encap = TRUE;
235 cap_packet_count = TRUE;
236 cap_file_size = TRUE;
238 cap_file_more_info = TRUE;
241 cap_data_size = TRUE;
243 cap_start_time = TRUE;
247 cap_data_rate_byte = TRUE;
248 cap_data_rate_bit = TRUE;
249 cap_packet_size = TRUE;
250 cap_packet_rate = TRUE;
252 cap_file_hashes = TRUE;
256 disable_all_infos(void)
258 report_all_infos = FALSE;
260 cap_file_type = FALSE;
261 cap_file_encap = FALSE;
263 cap_packet_count = FALSE;
264 cap_file_size = FALSE;
266 cap_file_more_info = FALSE;
267 cap_file_idb = FALSE;
269 cap_data_size = FALSE;
270 cap_duration = FALSE;
271 cap_start_time = FALSE;
272 cap_end_time = FALSE;
275 cap_data_rate_byte = FALSE;
276 cap_data_rate_bit = FALSE;
277 cap_packet_size = FALSE;
278 cap_packet_rate = FALSE;
280 cap_file_hashes = FALSE;
284 order_string(order_t order)
298 return "???"; /* "cannot happen" (the next step is "Profit!") */
303 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
305 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
308 if (cf_info->times_known && cf_info->packet_count > 0) {
310 switch (tsprecision) {
312 case WTAP_TSPREC_SEC:
313 g_snprintf(time_string_buf, sizeof time_string_buf,
315 (unsigned long)timer->secs);
318 case WTAP_TSPREC_DSEC:
319 g_snprintf(time_string_buf, sizeof time_string_buf,
321 (unsigned long)timer->secs,
323 timer->nsecs / 100000000);
326 case WTAP_TSPREC_CSEC:
327 g_snprintf(time_string_buf, sizeof time_string_buf,
329 (unsigned long)timer->secs,
331 timer->nsecs / 10000000);
334 case WTAP_TSPREC_MSEC:
335 g_snprintf(time_string_buf, sizeof time_string_buf,
337 (unsigned long)timer->secs,
339 timer->nsecs / 1000000);
342 case WTAP_TSPREC_USEC:
343 g_snprintf(time_string_buf, sizeof time_string_buf,
345 (unsigned long)timer->secs,
347 timer->nsecs / 1000);
350 case WTAP_TSPREC_NSEC:
351 g_snprintf(time_string_buf, sizeof time_string_buf,
353 (unsigned long)timer->secs,
359 g_snprintf(time_string_buf, sizeof time_string_buf,
360 "Unknown precision %d",
364 return time_string_buf;
366 ti_tm = localtime(&timer->secs);
368 g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
369 return time_string_buf;
371 switch (tsprecision) {
373 case WTAP_TSPREC_SEC:
374 g_snprintf(time_string_buf, sizeof time_string_buf,
375 "%04d-%02d-%02d %02d:%02d:%02d",
376 ti_tm->tm_year + 1900,
384 case WTAP_TSPREC_DSEC:
385 g_snprintf(time_string_buf, sizeof time_string_buf,
386 "%04d-%02d-%02d %02d:%02d:%02d%s%01d",
387 ti_tm->tm_year + 1900,
394 timer->nsecs / 100000000);
397 case WTAP_TSPREC_CSEC:
398 g_snprintf(time_string_buf, sizeof time_string_buf,
399 "%04d-%02d-%02d %02d:%02d:%02d%s%02d",
400 ti_tm->tm_year + 1900,
407 timer->nsecs / 10000000);
410 case WTAP_TSPREC_MSEC:
411 g_snprintf(time_string_buf, sizeof time_string_buf,
412 "%04d-%02d-%02d %02d:%02d:%02d%s%03d",
413 ti_tm->tm_year + 1900,
420 timer->nsecs / 1000000);
423 case WTAP_TSPREC_USEC:
424 g_snprintf(time_string_buf, sizeof time_string_buf,
425 "%04d-%02d-%02d %02d:%02d:%02d%s%06d",
426 ti_tm->tm_year + 1900,
433 timer->nsecs / 1000);
436 case WTAP_TSPREC_NSEC:
437 g_snprintf(time_string_buf, sizeof time_string_buf,
438 "%04d-%02d-%02d %02d:%02d:%02d%s%09d",
439 ti_tm->tm_year + 1900,
450 g_snprintf(time_string_buf, sizeof time_string_buf,
451 "Unknown precision %d",
455 return time_string_buf;
459 g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
460 return time_string_buf;
464 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
466 const gchar *second = want_seconds ? " second" : "";
467 const gchar *plural = want_seconds ? "s" : "";
468 static gchar time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
470 if (cf_info->times_known && cf_info->packet_count > 0) {
471 switch (tsprecision) {
473 case WTAP_TSPREC_SEC:
474 g_snprintf(time_string_buf, sizeof time_string_buf,
476 (unsigned long)timer->secs,
478 timer->secs == 1 ? "" : plural);
481 case WTAP_TSPREC_DSEC:
482 g_snprintf(time_string_buf, sizeof time_string_buf,
484 (unsigned long)timer->secs,
486 timer->nsecs / 100000000,
488 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
491 case WTAP_TSPREC_CSEC:
492 g_snprintf(time_string_buf, sizeof time_string_buf,
494 (unsigned long)timer->secs,
496 timer->nsecs / 10000000,
498 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
501 case WTAP_TSPREC_MSEC:
502 g_snprintf(time_string_buf, sizeof time_string_buf,
504 (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,
518 (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
521 case WTAP_TSPREC_NSEC:
522 g_snprintf(time_string_buf, sizeof time_string_buf,
524 (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);
551 /* multi-line comments would conflict with the formatting that capinfos uses
552 we replace linefeeds with spaces */
554 string_replace_newlines(gchar *str)
571 show_option_string(const char *prefix, const char *option_str)
575 if (option_str != NULL && option_str[0] != '\0') {
576 str = g_strdup(option_str);
577 string_replace_newlines(str);
578 printf("%s%s\n", prefix, str);
584 print_stats(const gchar *filename, capture_info *cf_info)
586 const gchar *file_type_string, *file_encap_string;
589 /* Build printable strings for various stats */
590 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
591 file_encap_string = wtap_encap_string(cf_info->file_encap);
593 if (filename) printf ("File name: %s\n", filename);
594 if (cap_file_type) printf ("File type: %s%s\n",
596 cf_info->iscompressed ? " (gzip compressed)" : "");
598 if (cap_file_encap) {
599 printf ("File encapsulation: %s\n", file_encap_string);
600 if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
602 printf ("Encapsulation in use by packets (# of pkts):\n");
603 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
604 if (cf_info->encap_counts[i] > 0)
606 wtap_encap_string(i), cf_info->encap_counts[i]);
610 if (cap_file_more_info) {
611 printf ("File timestamp precision: %s (%d)\n",
612 wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
615 if (cap_snaplen && cf_info->snap_set)
616 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
617 else if (cap_snaplen && !cf_info->snap_set)
618 printf ("Packet size limit: file hdr: (not set)\n");
619 if (cf_info->snaplen_max_inferred > 0) {
620 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
621 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
623 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
624 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
626 if (cap_packet_count) {
627 printf ("Number of packets: ");
628 if (machine_readable) {
629 printf ("%u\n", cf_info->packet_count);
631 size_string = format_size(cf_info->packet_count, format_size_unit_none);
632 printf ("%s\n", size_string);
637 printf ("File size: ");
638 if (machine_readable) {
639 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
641 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
642 printf ("%s\n", size_string);
647 printf ("Data size: ");
648 if (machine_readable) {
649 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
651 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
652 printf ("%s\n", size_string);
656 if (cf_info->times_known) {
657 if (cap_duration) /* XXX - shorten to hh:mm:ss */
658 printf("Capture duration: %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
660 printf("First packet time: %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
662 printf("Last packet time: %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
663 if (cap_data_rate_byte) {
664 printf("Data byte rate: ");
665 if (machine_readable) {
666 print_value("", 2, " bytes/sec", cf_info->data_rate);
668 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
669 printf ("%s\n", size_string);
673 if (cap_data_rate_bit) {
674 printf("Data bit rate: ");
675 if (machine_readable) {
676 print_value("", 2, " bits/sec", cf_info->data_rate*8);
678 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
679 printf ("%s\n", size_string);
684 if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
685 if (cf_info->times_known) {
686 if (cap_packet_rate) {
687 printf("Average packet rate: ");
688 if (machine_readable) {
689 print_value("", 2, " packets/sec", cf_info->packet_rate);
691 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
692 printf ("%s\n", size_string);
697 if (cap_file_hashes) {
698 printf ("SHA1: %s\n", file_sha1);
699 printf ("RIPEMD160: %s\n", file_rmd160);
700 printf ("MD5: %s\n", file_md5);
702 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
704 if (cf_info->shb != NULL) {
709 for (i = 0; wtap_block_get_nth_string_option_value(cf_info->shb, OPT_COMMENT, i, &str) == WTAP_OPTTYPE_SUCCESS; i++) {
710 show_option_string("Capture comment: ", str);
713 if (cap_file_more_info) {
716 if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS)
717 show_option_string("Capture hardware: ", str);
718 if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS)
719 show_option_string("Capture oper-sys: ", str);
720 if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS)
721 show_option_string("Capture application: ", str);
724 if (cap_file_idb && cf_info->num_interfaces != 0) {
726 g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
727 printf ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
728 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
729 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
730 guint32 packet_count = 0;
731 if (i < cf_info->interface_packet_counts->len)
732 packet_count = g_array_index(cf_info->interface_packet_counts, guint32, i);
733 printf ("Interface #%u info:\n", i);
735 printf (" Number of packets = %u\n", packet_count);
744 if (field_separator) putchar(field_separator);
750 if (quote_char) putchar(quote_char);
754 print_stats_table_header_label(const gchar *label)
763 print_stats_table_header(void)
769 if (cap_file_type) print_stats_table_header_label("File type");
770 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
771 if (cap_file_more_info) print_stats_table_header_label("File time precision");
772 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
773 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
774 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
775 if (cap_packet_count) print_stats_table_header_label("Number of packets");
776 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
777 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
778 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
779 if (cap_start_time) print_stats_table_header_label("Start time");
780 if (cap_end_time) print_stats_table_header_label("End time");
781 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
782 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
783 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
784 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
785 if (cap_file_hashes) {
786 print_stats_table_header_label("SHA1");
787 print_stats_table_header_label("RIPEMD160");
788 print_stats_table_header_label("MD5");
790 if (cap_order) print_stats_table_header_label("Strict time order");
791 if (cap_comment) print_stats_table_header_label("Capture comment");
792 if (cap_file_more_info) {
793 print_stats_table_header_label("Capture hardware");
794 print_stats_table_header_label("Capture oper-sys");
795 print_stats_table_header_label("Capture application");
802 print_stats_table(const gchar *filename, capture_info *cf_info)
804 const gchar *file_type_string, *file_encap_string;
806 /* Build printable strings for various stats */
807 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
808 file_encap_string = wtap_encap_string(cf_info->file_encap);
812 printf("%s", filename);
819 printf("%s", file_type_string);
823 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
824 * Output a line for each different encap with all fields repeated except
825 * the encapsulation field which has "Per Packet: ..." for each
826 * encapsulation type seen ?
828 if (cap_file_encap) {
831 printf("%s", file_encap_string);
835 if (cap_file_more_info) {
838 printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
845 if (cf_info->snap_set)
846 printf("%u", cf_info->snaplen);
850 if (cf_info->snaplen_max_inferred > 0) {
853 printf("%u", cf_info->snaplen_min_inferred);
857 printf("%u", cf_info->snaplen_max_inferred);
872 if (cap_packet_count) {
875 printf("%u", cf_info->packet_count);
882 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
889 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
896 printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
900 if (cap_start_time) {
903 printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
910 printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
914 if (cap_data_rate_byte) {
917 if (cf_info->times_known)
918 printf("%.2f", cf_info->data_rate);
924 if (cap_data_rate_bit) {
927 if (cf_info->times_known)
928 printf("%.2f", cf_info->data_rate*8);
934 if (cap_packet_size) {
937 printf("%.2f", cf_info->packet_size);
941 if (cap_packet_rate) {
944 if (cf_info->times_known)
945 printf("%.2f", cf_info->packet_rate);
951 if (cap_file_hashes) {
954 printf("%s", file_sha1);
959 printf("%s", file_rmd160);
964 printf("%s", file_md5);
971 printf("%s", order_string(cf_info->order));
975 if (cf_info->shb != NULL) {
977 * this is silly to put into a table format, but oh well
978 * note that there may be *more than one* of each of these types
979 * of options. To mitigate some of the potential silliness should
980 * the if(cap_comment) block be moved AFTER the if(cap_file_more_info)
981 * block? That would make any comments the last item(s) in each row.
982 * And/or should we add an cli option to inhibit the cap_comment to
983 * more easily manage the potential silliness? Potential silliness
984 * includes multiple comments and/or comments with embeded newlines.
989 gboolean have_cap = FALSE;
991 for (i = 0; wtap_block_get_nth_string_option_value(cf_info->shb, OPT_COMMENT, i, &opt_comment) == WTAP_OPTTYPE_SUCCESS; i++) {
995 printf("%s", opt_comment);
999 /* Maintain column alignment when we have no OPT_COMMENT */
1006 if (cap_file_more_info) {
1011 if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_HARDWARE, &str) == WTAP_OPTTYPE_SUCCESS) {
1018 if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_OS, &str) == WTAP_OPTTYPE_SUCCESS) {
1025 if (wtap_block_get_string_option_value(cf_info->shb, OPT_SHB_USERAPPL, &str) == WTAP_OPTTYPE_SUCCESS) {
1036 cleanup_capture_info(capture_info *cf_info)
1039 g_assert(cf_info != NULL);
1041 g_free(cf_info->encap_counts);
1042 cf_info->encap_counts = NULL;
1044 g_array_free(cf_info->interface_packet_counts, TRUE);
1045 cf_info->interface_packet_counts = NULL;
1047 if (cf_info->idb_info_strings) {
1048 for (i = 0; i < cf_info->idb_info_strings->len; i++) {
1049 gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
1052 g_array_free(cf_info->idb_info_strings, TRUE);
1054 cf_info->idb_info_strings = NULL;
1058 process_cap_file(wtap *wth, const char *filename)
1068 guint32 snaplen_min_inferred = 0xffffffff;
1069 guint32 snaplen_max_inferred = 0;
1070 const struct wtap_pkthdr *phdr;
1071 capture_info cf_info;
1072 gboolean have_times = TRUE;
1073 nstime_t start_time;
1074 int start_time_tsprec;
1076 int stop_time_tsprec;
1079 gboolean know_order = FALSE;
1080 order_t order = IN_ORDER;
1082 wtapng_iface_descriptions_t *idb_info;
1084 g_assert(wth != NULL);
1085 g_assert(filename != NULL);
1087 nstime_set_zero(&start_time);
1088 start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1089 nstime_set_zero(&stop_time);
1090 stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1091 nstime_set_zero(&cur_time);
1092 nstime_set_zero(&prev_time);
1094 cf_info.shb = wtap_file_get_shb(wth);
1096 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1098 idb_info = wtap_file_get_idb_info(wth);
1100 g_assert(idb_info->interface_data != NULL);
1102 cf_info.num_interfaces = idb_info->interface_data->len;
1103 cf_info.interface_packet_counts = g_array_sized_new(FALSE, TRUE, sizeof(guint32), cf_info.num_interfaces);
1104 g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1105 cf_info.pkt_interface_id_unknown = 0;
1110 /* Tally up data that we need to parse through the file to find */
1111 while (wtap_read(wth, &err, &err_info, &data_offset)) {
1112 phdr = wtap_phdr(wth);
1113 if (phdr->presence_flags & WTAP_HAS_TS) {
1114 prev_time = cur_time;
1115 cur_time = phdr->ts;
1117 start_time = phdr->ts;
1118 start_time_tsprec = phdr->pkt_tsprec;
1119 stop_time = phdr->ts;
1120 stop_time_tsprec = phdr->pkt_tsprec;
1121 prev_time = phdr->ts;
1123 if (nstime_cmp(&cur_time, &prev_time) < 0) {
1124 order = NOT_IN_ORDER;
1126 if (nstime_cmp(&cur_time, &start_time) < 0) {
1127 start_time = cur_time;
1128 start_time_tsprec = phdr->pkt_tsprec;
1130 if (nstime_cmp(&cur_time, &stop_time) > 0) {
1131 stop_time = cur_time;
1132 stop_time_tsprec = phdr->pkt_tsprec;
1135 have_times = FALSE; /* at least one packet has no time stamp */
1136 if (order != NOT_IN_ORDER)
1137 order = ORDER_UNKNOWN;
1140 if (phdr->rec_type == REC_TYPE_PACKET) {
1144 /* If caplen < len for a rcd, then presumably */
1145 /* 'Limit packet capture length' was done for this rcd. */
1146 /* Keep track as to the min/max actual snapshot lengths */
1147 /* seen for this file. */
1148 if (phdr->caplen < phdr->len) {
1149 if (phdr->caplen < snaplen_min_inferred)
1150 snaplen_min_inferred = phdr->caplen;
1151 if (phdr->caplen > snaplen_max_inferred)
1152 snaplen_max_inferred = phdr->caplen;
1155 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1156 cf_info.encap_counts[phdr->pkt_encap] += 1;
1158 fprintf(stderr, "capinfos: Unknown packet encapsulation %d in frame %u of file \"%s\"\n",
1159 phdr->pkt_encap, packet, filename);
1162 /* Packet interface_id info */
1163 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1164 /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1165 if (phdr->interface_id >= cf_info.num_interfaces) {
1167 * OK, re-fetch the number of interfaces, as there might have
1168 * been an interface that was in the middle of packets, and
1169 * grow the array to be big enough for the new number of
1172 idb_info = wtap_file_get_idb_info(wth);
1174 cf_info.num_interfaces = idb_info->interface_data->len;
1175 g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1180 if (phdr->interface_id < cf_info.num_interfaces) {
1181 g_array_index(cf_info.interface_packet_counts, guint32, phdr->interface_id) += 1;
1184 cf_info.pkt_interface_id_unknown += 1;
1188 /* it's for interface_id 0 */
1189 if (cf_info.num_interfaces != 0) {
1190 g_array_index(cf_info.interface_packet_counts, guint32, 0) += 1;
1193 cf_info.pkt_interface_id_unknown += 1;
1201 * Get IDB info strings.
1202 * We do this at the end, so we can get information for all IDBs in
1203 * the file, even those that come after packet records.
1205 idb_info = wtap_file_get_idb_info(wth);
1207 cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1208 cf_info.num_interfaces = idb_info->interface_data->len;
1209 for (i = 0; i < cf_info.num_interfaces; i++) {
1210 const wtap_block_t if_descr = g_array_index(idb_info->interface_data, wtap_block_t, i);
1211 gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1212 g_array_append_val(cf_info.idb_info_strings, s);
1220 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1221 packet, filename, wtap_strerror(err));
1222 if (err == WTAP_ERR_SHORT_READ) {
1223 /* Don't give up completely with this one. */
1226 " (will continue anyway, checksums might be incorrect)\n");
1228 if (err_info != NULL) {
1229 fprintf(stderr, "(%s)\n", err_info);
1233 cleanup_capture_info(&cf_info);
1239 size = wtap_file_size(wth, &err);
1242 "capinfos: Can't get size of \"%s\": %s.\n",
1243 filename, g_strerror(err));
1244 cleanup_capture_info(&cf_info);
1248 cf_info.filesize = size;
1251 cf_info.file_type = wtap_file_type_subtype(wth);
1252 cf_info.iscompressed = wtap_iscompressed(wth);
1254 /* File Encapsulation */
1255 cf_info.file_encap = wtap_file_encap(wth);
1257 cf_info.file_tsprec = wtap_file_tsprec(wth);
1259 /* Packet size limit (snaplen) */
1260 cf_info.snaplen = wtap_snapshot_length(wth);
1261 if (cf_info.snaplen > 0)
1262 cf_info.snap_set = TRUE;
1264 cf_info.snap_set = FALSE;
1266 cf_info.snaplen_min_inferred = snaplen_min_inferred;
1267 cf_info.snaplen_max_inferred = snaplen_max_inferred;
1270 cf_info.packet_count = packet;
1273 cf_info.times_known = have_times;
1274 cf_info.start_time = start_time;
1275 cf_info.start_time_tsprec = start_time_tsprec;
1276 cf_info.stop_time = stop_time;
1277 cf_info.stop_time_tsprec = stop_time_tsprec;
1278 nstime_delta(&cf_info.duration, &stop_time, &start_time);
1279 /* Duration precision is the higher of the start and stop time precisions. */
1280 if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1281 cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1283 cf_info.duration_tsprec = cf_info.start_time_tsprec;
1284 cf_info.know_order = know_order;
1285 cf_info.order = order;
1287 /* Number of packet bytes */
1288 cf_info.packet_bytes = bytes;
1290 cf_info.data_rate = 0.0;
1291 cf_info.packet_rate = 0.0;
1292 cf_info.packet_size = 0.0;
1295 double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1296 if (delta_time > 0.0) {
1297 cf_info.data_rate = (double)bytes / delta_time; /* Data rate per second */
1298 cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1300 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
1304 print_stats(filename, &cf_info);
1306 print_stats_table(filename, &cf_info);
1309 cleanup_capture_info(&cf_info);
1315 print_usage(FILE *output)
1317 fprintf(output, "\n");
1318 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1319 fprintf(output, "\n");
1320 fprintf(output, "General infos:\n");
1321 fprintf(output, " -t display the capture file type\n");
1322 fprintf(output, " -E display the capture file encapsulation\n");
1323 fprintf(output, " -I display the capture file interface information\n");
1324 fprintf(output, " -F display additional capture file information\n");
1325 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1326 fprintf(output, " -k display the capture comment\n");
1327 fprintf(output, "\n");
1328 fprintf(output, "Size infos:\n");
1329 fprintf(output, " -c display the number of packets\n");
1330 fprintf(output, " -s display the size of the file (in bytes)\n");
1331 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1332 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1333 fprintf(output, "\n");
1334 fprintf(output, "Time infos:\n");
1335 fprintf(output, " -u display the capture duration (in seconds)\n");
1336 fprintf(output, " -a display the capture start time\n");
1337 fprintf(output, " -e display the capture end time\n");
1338 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1339 fprintf(output, " -S display start and end times as seconds\n");
1340 fprintf(output, "\n");
1341 fprintf(output, "Statistic infos:\n");
1342 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1343 fprintf(output, " -i display average data rate (in bits/sec)\n");
1344 fprintf(output, " -z display average packet size (in bytes)\n");
1345 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1346 fprintf(output, "\n");
1347 fprintf(output, "Output format:\n");
1348 fprintf(output, " -L generate long report (default)\n");
1349 fprintf(output, " -T generate table report\n");
1350 fprintf(output, " -M display machine-readable values in long reports\n");
1351 fprintf(output, "\n");
1352 fprintf(output, "Table report options:\n");
1353 fprintf(output, " -R generate header record (default)\n");
1354 fprintf(output, " -r do not generate header record\n");
1355 fprintf(output, "\n");
1356 fprintf(output, " -B separate infos with TAB character (default)\n");
1357 fprintf(output, " -m separate infos with comma (,) character\n");
1358 fprintf(output, " -b separate infos with SPACE character\n");
1359 fprintf(output, "\n");
1360 fprintf(output, " -N do not quote infos (default)\n");
1361 fprintf(output, " -q quote infos with single quotes (')\n");
1362 fprintf(output, " -Q quote infos with double quotes (\")\n");
1363 fprintf(output, "\n");
1364 fprintf(output, "Miscellaneous:\n");
1365 fprintf(output, " -h display this help and exit\n");
1366 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1367 fprintf(output, " -A generate all infos (default)\n");
1368 fprintf(output, "\n");
1369 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1370 fprintf(output, "or adding to earlier options.\n");
1371 fprintf(output, "\n");
1372 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1373 fprintf(output, "output format.\n");
1378 * General errors are reported with an console message in capinfos.
1381 failure_message(const char *msg_format, va_list ap)
1383 fprintf(stderr, "capinfos: ");
1384 vfprintf(stderr, msg_format, ap);
1385 fprintf(stderr, "\n");
1390 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1393 for (i = 0; i < (int) length; i++) {
1394 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1399 main(int argc, char *argv[])
1401 GString *comp_info_str;
1402 GString *runtime_info_str;
1403 char *init_progfile_dir_error;
1408 int overall_error_status = EXIT_SUCCESS;
1409 static const struct option long_options[] = {
1410 {"help", no_argument, NULL, 'h'},
1411 {"version", no_argument, NULL, 'v'},
1417 char *hash_buf = NULL;
1418 gcry_md_hd_t hd = NULL;
1421 /* Set the C-language locale to the native environment. */
1422 setlocale(LC_ALL, "");
1424 /* Get the decimal point. */
1425 decimal_point = g_strdup(localeconv()->decimal_point);
1427 /* Get the compile-time version information string */
1428 comp_info_str = get_compiled_version_info(NULL, NULL);
1430 /* Get the run-time version information string */
1431 runtime_info_str = get_runtime_version_info(NULL);
1433 /* Add it to the information to be reported on a crash. */
1434 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1439 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1440 g_string_free(comp_info_str, TRUE);
1441 g_string_free(runtime_info_str, TRUE);
1444 arg_list_utf_16to8(argc, argv);
1445 create_app_running_mutex();
1449 * Get credential information for later use.
1451 init_process_policies();
1454 * Attempt to get the pathname of the directory containing the
1457 init_progfile_dir_error = init_progfile_dir(argv[0], main);
1458 if (init_progfile_dir_error != NULL) {
1460 "capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
1461 init_progfile_dir_error);
1462 g_free(init_progfile_dir_error);
1468 init_report_err(failure_message, NULL, NULL, NULL);
1470 /* Scan for plugins. This does *not* call their registration routines;
1473 Don't report failures to load plugins because most (non-wiretap)
1474 plugins *should* fail to load (because we're not linked against
1475 libwireshark and dissector plugins need libwireshark). */
1476 scan_plugins(DONT_REPORT_LOAD_FAILURE);
1478 /* Register all libwiretap plugin modules. */
1479 register_all_wiretap_modules();
1482 /* Process the options */
1483 while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEFHILMNQRST", long_options, NULL)) !=-1) {
1488 if (report_all_infos) disable_all_infos();
1489 cap_file_type = TRUE;
1493 if (report_all_infos) disable_all_infos();
1494 cap_file_encap = TRUE;
1498 if (report_all_infos) disable_all_infos();
1503 if (report_all_infos) disable_all_infos();
1504 cap_packet_count = TRUE;
1508 if (report_all_infos) disable_all_infos();
1509 cap_file_size = TRUE;
1513 if (report_all_infos) disable_all_infos();
1514 cap_data_size = TRUE;
1518 if (report_all_infos) disable_all_infos();
1519 cap_duration = TRUE;
1523 if (report_all_infos) disable_all_infos();
1524 cap_start_time = TRUE;
1528 if (report_all_infos) disable_all_infos();
1529 cap_end_time = TRUE;
1533 time_as_secs = TRUE;
1537 if (report_all_infos) disable_all_infos();
1538 cap_data_rate_byte = TRUE;
1542 if (report_all_infos) disable_all_infos();
1543 cap_data_rate_bit = TRUE;
1547 if (report_all_infos) disable_all_infos();
1548 cap_packet_size = TRUE;
1552 if (report_all_infos) disable_all_infos();
1553 cap_packet_rate = TRUE;
1557 if (report_all_infos) disable_all_infos();
1558 cap_file_hashes = TRUE;
1562 if (report_all_infos) disable_all_infos();
1567 if (report_all_infos) disable_all_infos();
1572 if (report_all_infos) disable_all_infos();
1573 cap_file_more_info = TRUE;
1577 if (report_all_infos) disable_all_infos();
1578 cap_file_idb = TRUE;
1582 continue_after_wtap_open_offline_failure = FALSE;
1594 long_report = FALSE;
1598 machine_readable = TRUE;
1602 table_report_header = TRUE;
1606 table_report_header = FALSE;
1622 field_separator = '\t';
1626 field_separator = ',';
1630 field_separator = ' ';
1634 printf("Capinfos (Wireshark) %s\n"
1635 "Print various information (infos) about capture files.\n"
1636 "See https://www.wireshark.org for more information.\n",
1637 get_ws_vcs_version_info());
1638 print_usage(stdout);
1643 comp_info_str = get_compiled_version_info(NULL, NULL);
1644 runtime_info_str = get_runtime_version_info(NULL);
1645 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1646 g_string_free(comp_info_str, TRUE);
1647 g_string_free(runtime_info_str, TRUE);
1651 case '?': /* Bad flag - print usage message */
1652 print_usage(stderr);
1653 overall_error_status = BAD_FLAG;
1659 if ((argc - optind) < 1) {
1660 print_usage(stderr);
1661 overall_error_status = INVALID_OPTION;
1665 if (!long_report && table_report_header) {
1666 print_stats_table_header();
1669 if (cap_file_hashes) {
1670 gcry_check_version(NULL);
1671 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1673 gcry_md_enable(hd, GCRY_MD_RMD160);
1674 gcry_md_enable(hd, GCRY_MD_MD5);
1676 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1679 overall_error_status = 0;
1681 for (opt = optind; opt < argc; opt++) {
1683 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1684 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1685 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1687 if (cap_file_hashes) {
1688 fh = ws_fopen(argv[opt], "rb");
1690 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1691 gcry_md_write(hd, hash_buf, hash_bytes);
1694 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1695 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1696 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1699 if (hd) gcry_md_reset(hd);
1702 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1705 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1706 wtap_strerror(err));
1707 if (err_info != NULL) {
1708 fprintf(stderr, "(%s)\n", err_info);
1711 overall_error_status = 2; /* remember that an error has occurred */
1712 if (!continue_after_wtap_open_offline_failure)
1717 if ((opt > optind) && (long_report))
1719 status = process_cap_file(wth, argv[opt]);
1723 overall_error_status = status;
1736 return overall_error_status;
1740 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1745 * indent-tabs-mode: nil
1748 * vi: set shiftwidth=2 tabstop=8 expandtab:
1749 * :indentSize=2:tabSize=8:noTabs=true: