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).
74 #ifdef HAVE_SYS_TIME_H
81 #include <zlib.h> /* to get the libz version number */
84 #include <wsutil/privileges.h>
85 #include <wsutil/filesystem.h>
86 #include <wsutil/crash_info.h>
87 #include <wsutil/ws_version_info.h>
90 #include <wsutil/plugins.h>
94 #include <wsutil/report_err.h>
95 #include <wsutil/str_util.h>
96 #include <wsutil/file_util.h>
99 #include <wsutil/wsgcrypt.h>
102 #ifndef HAVE_GETOPT_LONG
103 #include "wsutil/wsgetopt.h"
107 #include <wsutil/unicode-utils.h>
111 * By default capinfos now continues processing
112 * the next filename if and when wiretap detects
113 * a problem opening a file.
114 * Use the '-C' option to revert back to original
115 * capinfos behavior which is to abort any
116 * additional file processing at first open file
120 static gboolean continue_after_wtap_open_offline_failure = TRUE;
123 * table report variables
126 static gboolean long_report = TRUE; /* By default generate long report */
127 static gchar table_report_header = TRUE; /* Generate column header by default */
128 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
129 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
130 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
133 * capinfos has the ability to report on a number of
134 * various characteristics ("infos") for each input file.
136 * By default reporting of all info fields is enabled.
138 * Optionally the reporting of any specific info field
139 * or combination of info fields can be enabled with
140 * individual options.
143 static gboolean report_all_infos = TRUE; /* Report all infos */
145 static gboolean cap_file_type = TRUE; /* Report capture type */
146 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
147 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
148 static gboolean cap_packet_count = TRUE; /* Report packet count */
149 static gboolean cap_file_size = TRUE; /* Report file size */
150 static gboolean cap_comment = TRUE; /* Display the capture comment */
152 static gboolean cap_data_size = TRUE; /* Report packet byte size */
153 static gboolean cap_duration = TRUE; /* Report capture duration */
154 static gboolean cap_start_time = TRUE; /* Report capture start time */
155 static gboolean cap_end_time = TRUE; /* Report capture end time */
156 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
158 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
159 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
160 static gboolean cap_packet_size = TRUE; /* Report average packet size */
161 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
162 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
164 #ifdef HAVE_LIBGCRYPT
165 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
169 static gboolean cap_help = FALSE;
170 static gboolean table_report = FALSE;
172 static GOptionEntry general_entries[] =
175 { "type", 't', 0, G_OPTION_ARG_NONE, &cap_file_type,
176 "display the capture file type", NULL },
177 { "Encapsulation", 'E', 0, G_OPTION_ARG_NONE, &cap_file_encap,
178 "display the capture file encapsulation", NULL },
179 #ifdef HAVE_LIBGCRYPT
180 { "Hash", 'H', 0, G_OPTION_ARG_NONE, &cap_file_hashes,
181 "display the SHA1, RMD160, and MD5 hashes of the file", NULL },
182 #endif /* HAVE_LIBGCRYPT */
183 { "capture-comment", 'k', 0, G_OPTION_ARG_NONE, &cap_comment,
184 "display the capture comment ", NULL },
185 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
187 static GOptionEntry size_entries[] =
190 { "packets", 'c', 0, G_OPTION_ARG_NONE, &cap_packet_count,
191 "display the number of packets", NULL },
192 { "size", 's', 0, G_OPTION_ARG_NONE, &cap_file_size,
193 "display the size of the file (in bytes)", NULL },
194 { "tot-len-of-pkts", 'd', 0, G_OPTION_ARG_NONE, &cap_data_size,
195 "display the total length of all packets (in bytes)", NULL },
196 { "snap", 'l', 0, G_OPTION_ARG_NONE, &cap_snaplen,
197 "display the packet size limit (snapshot length)", NULL },
198 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
200 static GOptionEntry time_entries[] =
203 { "duration", 'u', 0, G_OPTION_ARG_NONE, &cap_duration,
204 "display the capture duration (in seconds)", NULL },
205 { "start", 'a', 0, G_OPTION_ARG_NONE, &cap_start_time,
206 "display the capture start time", NULL },
207 { "end", 'e', 0, G_OPTION_ARG_NONE, &cap_end_time,
208 "display the capture end time", NULL },
209 { "cron", 'o', 0, G_OPTION_ARG_NONE, &cap_order,
210 "display the capture file chronological status (True/False)", NULL },
211 { "start-end-time-sec", 'S', 0, G_OPTION_ARG_NONE, &time_as_secs,
212 "display start and end times as seconds", NULL },
213 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
216 static GOptionEntry stats_entries[] =
219 { "bytes", 'y', 0, G_OPTION_ARG_NONE, &cap_data_rate_byte,
220 "display average data rate (in bytes/s)", NULL },
221 { "bits", 'i', 0, G_OPTION_ARG_NONE, &cap_data_rate_bit,
222 "display average data rate (in bits/s)", NULL },
223 { "packet-bytes", 'z', 0, G_OPTION_ARG_NONE, &cap_packet_size,
224 "display average packet size (in bytes)", NULL },
225 { "packets", 'x', 0, G_OPTION_ARG_NONE, &cap_packet_rate,
226 "display average packet rate (in packets/s)", NULL },
227 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
230 static GOptionEntry output_format_entries[] =
233 { "long", 'L', 0, G_OPTION_ARG_NONE, &long_report,
234 "generate long report (default)", NULL },
235 { "Table", 'T', 0, G_OPTION_ARG_NONE, &table_report,
236 "generate table report", NULL },
237 { "machine-readable", 'M', 0, G_OPTION_ARG_NONE, &machine_readable,
238 "display machine-readable (unabbreviated) values in long reports", NULL },
239 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
242 static GOptionEntry table_report_entries[] =
245 { "header-rec", 'R', 0, G_OPTION_ARG_NONE, &table_report_header,
246 "generate header record (default)", NULL },
247 { "no-table", 'r', 0, G_OPTION_ARG_NONE, &table_report_header,
248 "do not generate header record", NULL },
249 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
252 static GOptionEntry misc_entries[] =
254 { "helpcompat", 'h', 0, G_OPTION_ARG_NONE, &cap_help,
255 "display help", NULL },
256 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
260 GOptionGroup *general_grp, *size_grp, *time_grp, *stats_grp, *output_grp, *table_report_grp;
261 GError *parse_err = NULL;
263 #endif /* USE_GOPTION */
265 #ifdef HAVE_LIBGCRYPT
266 #define HASH_SIZE_SHA1 20
267 #define HASH_SIZE_RMD160 20
268 #define HASH_SIZE_MD5 16
270 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
271 #define HASH_BUF_SIZE (1024 * 1024)
274 static gchar file_sha1[HASH_STR_SIZE];
275 static gchar file_rmd160[HASH_STR_SIZE];
276 static gchar file_md5[HASH_STR_SIZE];
278 #define FILE_HASH_OPT "H"
280 #define FILE_HASH_OPT ""
281 #endif /* HAVE_LIBGCRYPT */
284 * If we have at least two packets with time stamps, and they're not in
285 * order - i.e., the later packet has a time stamp older than the earlier
286 * packet - the time stamps are known not to be in order.
288 * If every packet has a time stamp, and they're all in order, the time
289 * stamp is known to be in order.
291 * Otherwise, we have no idea.
299 typedef struct _capture_info {
300 const char *filename;
302 gboolean iscompressed;
307 guint64 packet_bytes;
308 gboolean times_known;
311 guint32 packet_count;
312 gboolean snap_set; /* If set in capture file header */
313 guint32 snaplen; /* value from the capture file header */
314 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
315 guint32 snaplen_max_inferred; /* ... */
316 gboolean drops_known;
322 double data_rate; /* in bytes */
326 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
331 enable_all_infos(void)
333 report_all_infos = TRUE;
335 cap_file_type = TRUE;
336 cap_file_encap = TRUE;
338 cap_packet_count = TRUE;
339 cap_file_size = TRUE;
342 cap_data_size = TRUE;
344 cap_start_time = TRUE;
348 cap_data_rate_byte = TRUE;
349 cap_data_rate_bit = TRUE;
350 cap_packet_size = TRUE;
351 cap_packet_rate = TRUE;
353 #ifdef HAVE_LIBGCRYPT
354 cap_file_hashes = TRUE;
355 #endif /* HAVE_LIBGCRYPT */
359 disable_all_infos(void)
361 report_all_infos = FALSE;
363 cap_file_type = FALSE;
364 cap_file_encap = FALSE;
366 cap_packet_count = FALSE;
367 cap_file_size = FALSE;
370 cap_data_size = FALSE;
371 cap_duration = FALSE;
372 cap_start_time = FALSE;
373 cap_end_time = FALSE;
376 cap_data_rate_byte = FALSE;
377 cap_data_rate_bit = FALSE;
378 cap_packet_size = FALSE;
379 cap_packet_rate = FALSE;
381 #ifdef HAVE_LIBGCRYPT
382 cap_file_hashes = FALSE;
383 #endif /* HAVE_LIBGCRYPT */
387 order_string(order_t order)
401 return "???"; /* "cannot happen" (the next step is "Profit!") */
406 time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
408 const gchar *lf = want_lf ? "\n" : "";
409 static gchar time_string_buf[20];
410 char *time_string_ctime;
412 if (cf_info->times_known && cf_info->packet_count > 0) {
414 /* XXX - Would it be useful to show sub-second precision? */
415 g_snprintf(time_string_buf, 20, "%lu%s", (unsigned long)timer, lf);
416 return time_string_buf;
418 time_string_ctime = ctime(&timer);
419 if (time_string_ctime == NULL) {
420 g_snprintf(time_string_buf, 20, "Not representable%s", lf);
421 return time_string_buf;
425 * The ctime() function returns a string formatted as:
426 * "Www Mmm dd hh:mm:ss yyyy\n"
427 * The unwanted '\n' is the 24th character.
429 time_string_ctime[24] = '\0';
431 return time_string_ctime;
435 g_snprintf(time_string_buf, 15, "n/a%s", lf);
436 return time_string_buf;
439 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
441 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
443 printf("%sn/a\n", text_p1);
447 print_stats(const gchar *filename, capture_info *cf_info)
449 const gchar *file_type_string, *file_encap_string;
454 /* Build printable strings for various stats */
455 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
456 file_encap_string = wtap_encap_string(cf_info->file_encap);
457 start_time_t = (time_t)cf_info->start_time;
458 stop_time_t = (time_t)cf_info->stop_time;
460 if (filename) printf ("File name: %s\n", filename);
461 if (cap_file_type) printf ("File type: %s%s\n",
463 cf_info->iscompressed ? " (gzip compressed)" : "");
464 if (cap_file_encap) printf ("File encapsulation: %s\n", file_encap_string);
465 if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
467 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
468 if (cf_info->encap_counts[i] > 0)
469 printf(" %s\n", wtap_encap_string(i));
472 if (cap_snaplen && cf_info->snap_set)
473 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
474 else if (cap_snaplen && !cf_info->snap_set)
475 printf ("Packet size limit: file hdr: (not set)\n");
476 if (cf_info->snaplen_max_inferred > 0) {
477 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
478 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
480 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
481 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
483 if (cap_packet_count) {
484 printf ("Number of packets: ");
485 if (machine_readable) {
486 printf ("%u\n", cf_info->packet_count);
488 size_string = format_size(cf_info->packet_count, format_size_unit_none);
489 printf ("%s\n", size_string);
494 printf ("File size: ");
495 if (machine_readable) {
496 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
498 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
499 printf ("%s\n", size_string);
504 printf ("Data size: ");
505 if (machine_readable) {
506 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
508 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
509 printf ("%s\n", size_string);
513 if (cf_info->times_known) {
514 if (cap_duration) /* XXX - shorten to hh:mm:ss */
515 print_value("Capture duration: ", 0, " seconds", cf_info->duration);
517 printf ("Start time: %s", time_string(start_time_t, cf_info, TRUE));
519 printf ("End time: %s", time_string(stop_time_t, cf_info, TRUE));
520 if (cap_data_rate_byte) {
521 printf ("Data byte rate: ");
522 if (machine_readable) {
523 print_value("", 2, " bytes/sec", cf_info->data_rate);
525 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
526 printf ("%s\n", size_string);
530 if (cap_data_rate_bit) {
531 printf ("Data bit rate: ");
532 if (machine_readable) {
533 print_value("", 2, " bits/sec", cf_info->data_rate*8);
535 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
536 printf ("%s\n", size_string);
541 if (cap_packet_size) printf ("Average packet size: %.2f bytes\n", cf_info->packet_size);
542 if (cf_info->times_known) {
543 if (cap_packet_rate) {
544 printf ("Average packet rate: ");
545 if (machine_readable) {
546 print_value("", 2, " packets/sec", cf_info->packet_rate);
548 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
549 printf ("%s\n", size_string);
554 #ifdef HAVE_LIBGCRYPT
555 if (cap_file_hashes) {
556 printf ("SHA1: %s\n", file_sha1);
557 printf ("RIPEMD160: %s\n", file_rmd160);
558 printf ("MD5: %s\n", file_md5);
560 #endif /* HAVE_LIBGCRYPT */
561 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
562 if (cap_comment && cf_info->comment)
563 printf ("Capture comment: %s\n", cf_info->comment);
569 if (field_separator) putchar(field_separator);
575 if (quote_char) putchar(quote_char);
579 print_stats_table_header_label(const gchar *label)
588 print_stats_table_header(void)
594 if (cap_file_type) print_stats_table_header_label("File type");
595 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
596 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
597 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
598 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
599 if (cap_packet_count) print_stats_table_header_label("Number of packets");
600 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
601 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
602 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
603 if (cap_start_time) print_stats_table_header_label("Start time");
604 if (cap_end_time) print_stats_table_header_label("End time");
605 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
606 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
607 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
608 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
609 #ifdef HAVE_LIBGCRYPT
610 if (cap_file_hashes) {
611 print_stats_table_header_label("SHA1");
612 print_stats_table_header_label("RIPEMD160");
613 print_stats_table_header_label("MD5");
615 #endif /* HAVE_LIBGCRYPT */
616 if (cap_order) print_stats_table_header_label("Strict time order");
617 if (cap_comment) print_stats_table_header_label("Capture comment");
623 print_stats_table(const gchar *filename, capture_info *cf_info)
625 const gchar *file_type_string, *file_encap_string;
629 /* Build printable strings for various stats */
630 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
631 file_encap_string = wtap_encap_string(cf_info->file_encap);
632 start_time_t = (time_t)cf_info->start_time;
633 stop_time_t = (time_t)cf_info->stop_time;
637 printf("%s", filename);
644 printf("%s", file_type_string);
648 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
649 * Output a line for each different encap with all fields repeated except
650 * the encapsulation field which has "Per Packet: ..." for each
651 * encapsulation type seen ?
653 if (cap_file_encap) {
656 printf("%s", file_encap_string);
663 if (cf_info->snap_set)
664 printf("%u", cf_info->snaplen);
668 if (cf_info->snaplen_max_inferred > 0) {
671 printf("%u", cf_info->snaplen_min_inferred);
675 printf("%u", cf_info->snaplen_max_inferred);
690 if (cap_packet_count) {
693 printf("%u", cf_info->packet_count);
700 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
707 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
714 if (cf_info->times_known)
715 printf("%f", cf_info->duration);
721 if (cap_start_time) {
724 printf("%s", time_string(start_time_t, cf_info, FALSE));
731 printf("%s", time_string(stop_time_t, cf_info, FALSE));
735 if (cap_data_rate_byte) {
738 if (cf_info->times_known)
739 printf("%.2f", cf_info->data_rate);
745 if (cap_data_rate_bit) {
748 if (cf_info->times_known)
749 printf("%.2f", cf_info->data_rate*8);
755 if (cap_packet_size) {
758 printf("%.2f", cf_info->packet_size);
762 if (cap_packet_rate) {
765 if (cf_info->times_known)
766 printf("%.2f", cf_info->packet_rate);
772 #ifdef HAVE_LIBGCRYPT
773 if (cap_file_hashes) {
776 printf("%s", file_sha1);
781 printf("%s", file_rmd160);
786 printf("%s", file_md5);
789 #endif /* HAVE_LIBGCRYPT */
794 printf("%s", order_string(cf_info->order));
801 printf("%s", cf_info->comment);
810 process_cap_file(wtap *wth, const char *filename)
820 guint32 snaplen_min_inferred = 0xffffffff;
821 guint32 snaplen_max_inferred = 0;
822 const struct wtap_pkthdr *phdr;
823 capture_info cf_info;
824 gboolean have_times = TRUE;
825 double start_time = 0;
826 double stop_time = 0;
828 double prev_time = 0;
829 gboolean know_order = FALSE;
830 order_t order = IN_ORDER;
831 wtapng_section_t *shb_inf;
835 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
837 /* Tally up data that we need to parse through the file to find */
838 while (wtap_read(wth, &err, &err_info, &data_offset)) {
839 phdr = wtap_phdr(wth);
840 if (phdr->presence_flags & WTAP_HAS_TS) {
841 prev_time = cur_time;
842 cur_time = nstime_to_sec(&phdr->ts);
844 start_time = cur_time;
845 stop_time = cur_time;
846 prev_time = cur_time;
848 if (cur_time < prev_time) {
849 order = NOT_IN_ORDER;
851 if (cur_time < start_time) {
852 start_time = cur_time;
854 if (cur_time > stop_time) {
855 stop_time = cur_time;
858 have_times = FALSE; /* at least one packet has no time stamp */
859 if (order != NOT_IN_ORDER)
860 order = ORDER_UNKNOWN;
863 if (phdr->rec_type == REC_TYPE_PACKET) {
867 /* If caplen < len for a rcd, then presumably */
868 /* 'Limit packet capture length' was done for this rcd. */
869 /* Keep track as to the min/max actual snapshot lengths */
870 /* seen for this file. */
871 if (phdr->caplen < phdr->len) {
872 if (phdr->caplen < snaplen_min_inferred)
873 snaplen_min_inferred = phdr->caplen;
874 if (phdr->caplen > snaplen_max_inferred)
875 snaplen_max_inferred = phdr->caplen;
878 /* Per-packet encapsulation */
879 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
880 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
881 cf_info.encap_counts[phdr->pkt_encap] += 1;
883 fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
884 phdr->pkt_encap, packet, filename);
893 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
894 packet, filename, wtap_strerror(err));
895 if (err == WTAP_ERR_SHORT_READ) {
896 /* Don't give up completely with this one. */
899 " (will continue anyway, checksums might be incorrect)\n");
901 if (err_info != NULL) {
902 fprintf(stderr, "(%s)\n", err_info);
906 g_free(cf_info.encap_counts);
912 size = wtap_file_size(wth, &err);
915 "capinfos: Can't get size of \"%s\": %s.\n",
916 filename, g_strerror(err));
917 g_free(cf_info.encap_counts);
921 cf_info.filesize = size;
924 cf_info.file_type = wtap_file_type_subtype(wth);
925 cf_info.iscompressed = wtap_iscompressed(wth);
927 /* File Encapsulation */
928 cf_info.file_encap = wtap_file_encap(wth);
930 /* Packet size limit (snaplen) */
931 cf_info.snaplen = wtap_snapshot_length(wth);
932 if (cf_info.snaplen > 0)
933 cf_info.snap_set = TRUE;
935 cf_info.snap_set = FALSE;
937 cf_info.snaplen_min_inferred = snaplen_min_inferred;
938 cf_info.snaplen_max_inferred = snaplen_max_inferred;
941 cf_info.packet_count = packet;
944 cf_info.times_known = have_times;
945 cf_info.start_time = start_time;
946 cf_info.stop_time = stop_time;
947 cf_info.duration = stop_time-start_time;
948 cf_info.know_order = know_order;
949 cf_info.order = order;
951 /* Number of packet bytes */
952 cf_info.packet_bytes = bytes;
954 cf_info.data_rate = 0.0;
955 cf_info.packet_rate = 0.0;
956 cf_info.packet_size = 0.0;
959 if (cf_info.duration > 0.0) {
960 cf_info.data_rate = (double)bytes / (stop_time-start_time); /* Data rate per second */
961 cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
963 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
966 cf_info.comment = NULL;
967 shb_inf = wtap_file_get_shb_info(wth);
969 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
970 cf_info.comment = g_strdup(shb_inf->opt_comment);
973 if (cf_info.comment) {
974 /* multi-line comments would conflict with the formatting that capinfos uses
975 we replace linefeeds with spaces */
985 print_stats(filename, &cf_info);
987 print_stats_table(filename, &cf_info);
990 g_free(cf_info.encap_counts);
991 g_free(cf_info.comment);
997 print_usage(FILE *output)
999 fprintf(output, "\n");
1000 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1001 fprintf(output, "\n");
1002 fprintf(output, "General infos:\n");
1003 fprintf(output, " -t display the capture file type\n");
1004 fprintf(output, " -E display the capture file encapsulation\n");
1005 #ifdef HAVE_LIBGCRYPT
1006 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1008 fprintf(output, " -k display the capture comment\n");
1009 fprintf(output, "\n");
1010 fprintf(output, "Size infos:\n");
1011 fprintf(output, " -c display the number of packets\n");
1012 fprintf(output, " -s display the size of the file (in bytes)\n");
1013 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1014 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1015 fprintf(output, "\n");
1016 fprintf(output, "Time infos:\n");
1017 fprintf(output, " -u display the capture duration (in seconds)\n");
1018 fprintf(output, " -a display the capture start time\n");
1019 fprintf(output, " -e display the capture end time\n");
1020 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1021 fprintf(output, " -S display start and end times as seconds\n");
1022 fprintf(output, "\n");
1023 fprintf(output, "Statistic infos:\n");
1024 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1025 fprintf(output, " -i display average data rate (in bits/sec)\n");
1026 fprintf(output, " -z display average packet size (in bytes)\n");
1027 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1028 fprintf(output, "\n");
1029 fprintf(output, "Output format:\n");
1030 fprintf(output, " -L generate long report (default)\n");
1031 fprintf(output, " -T generate table report\n");
1032 fprintf(output, " -M display machine-readable values in long reports\n");
1033 fprintf(output, "\n");
1034 fprintf(output, "Table report options:\n");
1035 fprintf(output, " -R generate header record (default)\n");
1036 fprintf(output, " -r do not generate header record\n");
1037 fprintf(output, "\n");
1038 fprintf(output, " -B separate infos with TAB character (default)\n");
1039 fprintf(output, " -m separate infos with comma (,) character\n");
1040 fprintf(output, " -b separate infos with SPACE character\n");
1041 fprintf(output, "\n");
1042 fprintf(output, " -N do not quote infos (default)\n");
1043 fprintf(output, " -q quote infos with single quotes (')\n");
1044 fprintf(output, " -Q quote infos with double quotes (\")\n");
1045 fprintf(output, "\n");
1046 fprintf(output, "Miscellaneous:\n");
1047 fprintf(output, " -h display this help and exit\n");
1048 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1049 fprintf(output, " -A generate all infos (default)\n");
1050 fprintf(output, "\n");
1051 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1052 fprintf(output, "or adding to earlier options.\n");
1053 fprintf(output, "\n");
1054 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1055 fprintf(output, "output format.\n");
1056 #ifndef HAVE_LIBGCRYPT
1057 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1063 * Don't report failures to load plugins because most (non-wiretap) plugins
1064 * *should* fail to load (because we're not linked against libwireshark and
1065 * dissector plugins need libwireshark).
1068 failure_message(const char *msg_format _U_, va_list ap _U_)
1074 #ifdef HAVE_LIBGCRYPT
1076 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1079 for (i = 0; i < (int) length; i++) {
1080 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1083 #endif /* HAVE_LIBGCRYPT */
1086 get_capinfos_compiled_info(GString *str)
1089 g_string_append(str, ", ");
1091 g_string_append(str, "with libz ");
1093 g_string_append(str, ZLIB_VERSION);
1094 #else /* ZLIB_VERSION */
1095 g_string_append(str, "(version unknown)");
1096 #endif /* ZLIB_VERSION */
1097 #else /* HAVE_LIBZ */
1098 g_string_append(str, "without libz");
1099 #endif /* HAVE_LIBZ */
1103 get_capinfos_runtime_info(GString *str)
1106 #if defined(HAVE_LIBZ) && !defined(_WIN32)
1107 g_string_append_printf(str, ", with libz %s", zlibVersion());
1112 main(int argc, char *argv[])
1114 GString *comp_info_str;
1115 GString *runtime_info_str;
1120 int overall_error_status;
1121 static const struct option long_options[] = {
1122 {(char *)"help", no_argument, NULL, 'h'},
1123 {(char *)"version", no_argument, NULL, 'v'},
1129 char *init_progfile_dir_error;
1131 #ifdef HAVE_LIBGCRYPT
1133 char *hash_buf = NULL;
1134 gcry_md_hd_t hd = NULL;
1138 /* Assemble the compile-time version information string */
1139 comp_info_str = g_string_new("Compiled ");
1140 get_compiled_version_info(comp_info_str, NULL, get_capinfos_compiled_info);
1142 /* Assemble the run-time version information string */
1143 runtime_info_str = g_string_new("Running ");
1144 get_runtime_version_info(runtime_info_str, get_capinfos_runtime_info);
1146 /* Add it to the information to be reported on a crash. */
1147 ws_add_crash_info("Capinfos (Wireshark) %s\n"
1152 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1155 arg_list_utf_16to8(argc, argv);
1156 create_app_running_mutex();
1160 * Get credential information for later use.
1162 init_process_policies();
1163 init_open_routines();
1166 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1167 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1168 g_free(init_progfile_dir_error);
1170 /* Register all the plugin types we have. */
1171 wtap_register_plugin_types(); /* Types known to libwiretap */
1173 init_report_err(failure_message, NULL, NULL, NULL);
1175 /* Scan for plugins. This does *not* call their registration routines;
1176 that's done later. */
1179 /* Register all libwiretap plugin modules. */
1180 register_all_wiretap_modules();
1184 /* Process the options */
1186 ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1187 general_grp = g_option_group_new("gen", "General infos:",
1188 "Show general options", NULL, NULL);
1189 size_grp = g_option_group_new("size", "Size infos:",
1190 "Show size options", NULL, NULL);
1191 time_grp = g_option_group_new("time", "Time infos:",
1192 "Show time options", NULL, NULL);
1193 stats_grp = g_option_group_new("stats", "Statistics infos:",
1194 "Show statistics options", NULL, NULL);
1195 output_grp = g_option_group_new("output", "Output format:",
1196 "Show output format options", NULL, NULL);
1197 table_report_grp = g_option_group_new("table", "Table report options:",
1198 "Show table report options", NULL, NULL);
1199 g_option_group_add_entries(general_grp, general_entries);
1200 g_option_group_add_entries(size_grp, size_entries);
1201 g_option_group_add_entries(time_grp, time_entries);
1202 g_option_group_add_entries(stats_grp, stats_entries);
1203 g_option_group_add_entries(output_grp, output_format_entries);
1204 g_option_group_add_entries(table_report_grp, table_report_entries);
1205 g_option_context_add_main_entries(ctx, misc_entries, NULL);
1206 g_option_context_add_group(ctx, general_grp);
1207 g_option_context_add_group(ctx, size_grp);
1208 g_option_context_add_group(ctx, time_grp);
1209 g_option_context_add_group(ctx, stats_grp);
1210 g_option_context_add_group(ctx, output_grp);
1211 g_option_context_add_group(ctx, table_report_grp);
1212 /* There's probably a better way to do this, but this works for now.
1213 GOptions displays the name in argv[0] as the name of the
1214 application. This is reasonable, but because we actually have a
1215 script wrapper that calls the executable. The name that gets
1216 displayed is not exactly the same as the command the user used
1219 argv[0] = (char *)"capinfos";
1221 /* if we have at least one cmdline option, we disable printing all infos */
1222 if (argc > 2 && report_all_infos)
1223 disable_all_infos();
1225 if ( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1227 g_printerr ("option parsing failed: %s\n", parse_err->message);
1228 g_printerr("%s", g_option_context_get_help (ctx, TRUE, NULL));
1232 g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1236 g_printerr("%s", g_option_context_get_help (ctx, FALSE, NULL));
1239 g_option_context_free(ctx);
1241 #endif /* USE_GOPTION */
1242 while ((opt = getopt_long(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxokCALTMRrSNqQBmb", long_options, NULL)) !=-1) {
1247 if (report_all_infos) disable_all_infos();
1248 cap_file_type = TRUE;
1252 if (report_all_infos) disable_all_infos();
1253 cap_file_encap = TRUE;
1257 if (report_all_infos) disable_all_infos();
1262 if (report_all_infos) disable_all_infos();
1263 cap_packet_count = TRUE;
1267 if (report_all_infos) disable_all_infos();
1268 cap_file_size = TRUE;
1272 if (report_all_infos) disable_all_infos();
1273 cap_data_size = TRUE;
1277 if (report_all_infos) disable_all_infos();
1278 cap_duration = TRUE;
1282 if (report_all_infos) disable_all_infos();
1283 cap_start_time = TRUE;
1287 if (report_all_infos) disable_all_infos();
1288 cap_end_time = TRUE;
1292 time_as_secs = TRUE;
1296 if (report_all_infos) disable_all_infos();
1297 cap_data_rate_byte = TRUE;
1301 if (report_all_infos) disable_all_infos();
1302 cap_data_rate_bit = TRUE;
1306 if (report_all_infos) disable_all_infos();
1307 cap_packet_size = TRUE;
1311 if (report_all_infos) disable_all_infos();
1312 cap_packet_rate = TRUE;
1315 #ifdef HAVE_LIBGCRYPT
1317 if (report_all_infos) disable_all_infos();
1318 cap_file_hashes = TRUE;
1323 if (report_all_infos) disable_all_infos();
1328 if (report_all_infos) disable_all_infos();
1333 continue_after_wtap_open_offline_failure = FALSE;
1345 long_report = FALSE;
1349 machine_readable = TRUE;
1353 table_report_header = TRUE;
1357 table_report_header = FALSE;
1373 field_separator = '\t';
1377 field_separator = ',';
1381 field_separator = ' ';
1385 printf("Capinfos (Wireshark) %s\n"
1386 "Print various information (infos) about capture files.\n"
1387 "See http://www.wireshark.org for more information.\n",
1388 get_ws_vcs_version_info());
1389 print_usage(stdout);
1394 show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1395 g_string_free(comp_info_str, TRUE);
1396 g_string_free(runtime_info_str, TRUE);
1400 case '?': /* Bad flag - print usage message */
1401 print_usage(stderr);
1407 /* Set the C-language locale to the native environment. */
1408 setlocale(LC_ALL, "");
1410 if ((argc - optind) < 1) {
1411 print_usage(stderr);
1415 if (!long_report && table_report_header) {
1416 print_stats_table_header();
1419 #ifdef HAVE_LIBGCRYPT
1420 if (cap_file_hashes) {
1421 gcry_check_version(NULL);
1422 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1424 gcry_md_enable(hd, GCRY_MD_RMD160);
1425 gcry_md_enable(hd, GCRY_MD_MD5);
1427 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1431 overall_error_status = 0;
1433 for (opt = optind; opt < argc; opt++) {
1435 #ifdef HAVE_LIBGCRYPT
1436 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1437 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1438 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1440 if (cap_file_hashes) {
1441 fh = ws_fopen(argv[opt], "rb");
1443 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1444 gcry_md_write(hd, hash_buf, hash_bytes);
1447 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1448 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1449 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1452 if (hd) gcry_md_reset(hd);
1454 #endif /* HAVE_LIBGCRYPT */
1456 wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1459 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1460 wtap_strerror(err));
1461 if (err_info != NULL) {
1462 fprintf(stderr, "(%s)\n", err_info);
1465 overall_error_status = 1; /* remember that an error has occurred */
1466 if (!continue_after_wtap_open_offline_failure)
1467 exit(1); /* error status */
1471 if ((opt > optind) && (long_report))
1473 status = process_cap_file(wth, argv[opt]);
1481 return overall_error_status;
1485 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1490 * indent-tabs-mode: nil
1493 * vi: set shiftwidth=2 tabstop=8 expandtab:
1494 * :indentSize=2:tabSize=8:noTabs=true: