2 * Reports capture file information including # of packets, duration, others
4 * Copyright 2004 Ian Schorr
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 * New capinfos features
32 * Continue processing additional files after
33 * a wiretap open failure. The new -C option
34 * reverts to capinfos' original behavior which
35 * is to cancel any further file processing at
36 * first file open failure.
38 * Change the behavior of how the default display
39 * of all infos is initiated. This gets rid of a
40 * special post getopt() argument count test.
42 * Add new table output format (with related options)
43 * This feature allows outputting the various infos
44 * into a tab delimited text file, or to a comma
45 * separated variables file (*.csv) instead of the
46 * original "long" format.
49 * behaviour changed: Upon exit capinfos will return
50 * an error status if an error occurred at any
51 * point during "continuous" file processing.
52 * (Previously a success status was always
53 * returned if the -C option was not used).
72 #ifdef HAVE_SYS_TIME_H
78 #include <wsutil/privileges.h>
81 * The symbols declared in the below are exported from libwireshark,
82 * but we don't want to link whole libwireshark to editcap.
83 * We link the object directly instead and this needs a little trick
84 * with the WS_BUILD_DLL #define.
87 #define RESET_SYMBOL_EXPORT
88 #include <epan/filesystem.h>
89 #include <epan/plugins.h>
91 #define RESET_SYMBOL_EXPORT
94 #include <wsutil/report_err.h>
95 #include <wsutil/privileges.h>
96 #include <wsutil/str_util.h>
99 #include <wsutil/wsgcrypt.h>
100 #include <wsutil/file_util.h>
104 #include "wsutil/wsgetopt.h"
108 #include <wsutil/unicode-utils.h>
111 #include "svnversion.h"
114 * By default capinfos now continues processing
115 * the next filename if and when wiretap detects
116 * a problem opening a file.
117 * Use the '-C' option to revert back to original
118 * capinfos behavior which is to abort any
119 * additional file processing at first open file
123 static gboolean continue_after_wtap_open_offline_failure = TRUE;
126 * table report variables
129 static gboolean long_report = TRUE; /* By default generate long report */
130 static gchar table_report_header = TRUE; /* Generate column header by default */
131 static gchar field_separator = '\t'; /* Use TAB as field separator by default */
132 static gchar quote_char = '\0'; /* Do NOT quote fields by default */
133 static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
136 * capinfos has the ability to report on a number of
137 * various characteristics ("infos") for each input file.
139 * By default reporting of all info fields is enabled.
141 * Optionally the reporting of any specific info field
142 * or combination of info fields can be enabled with
143 * individual options.
146 static gboolean report_all_infos = TRUE; /* Report all infos */
148 static gboolean cap_file_type = TRUE; /* Report capture type */
149 static gboolean cap_file_encap = TRUE; /* Report encapsulation */
150 static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
151 static gboolean cap_packet_count = TRUE; /* Report packet count */
152 static gboolean cap_file_size = TRUE; /* Report file size */
153 static gboolean cap_comment = TRUE; /* Display the capture comment */
155 static gboolean cap_data_size = TRUE; /* Report packet byte size */
156 static gboolean cap_duration = TRUE; /* Report capture duration */
157 static gboolean cap_start_time = TRUE; /* Report capture start time */
158 static gboolean cap_end_time = TRUE; /* Report capture end time */
159 static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
161 static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
162 static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
163 static gboolean cap_packet_size = TRUE; /* Report average packet size */
164 static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
165 static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
167 #ifdef HAVE_LIBGCRYPT
168 static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
172 static gboolean cap_help = FALSE;
173 static gboolean table_report = FALSE;
175 static GOptionEntry general_entries[] =
178 { "type", 't', 0, G_OPTION_ARG_NONE, &cap_file_type,
179 "display the capture file type", NULL },
180 { "Encapsulation", 'E', 0, G_OPTION_ARG_NONE, &cap_file_encap,
181 "display the capture file encapsulation", NULL },
182 #ifdef HAVE_LIBGCRYPT
183 { "Hash", 'H', 0, G_OPTION_ARG_NONE, &cap_file_hashes,
184 "display the SHA1, RMD160, and MD5 hashes of the file", NULL },
185 #endif /* HAVE_LIBGCRYPT */
186 { "capture-comment", 'k', 0, G_OPTION_ARG_NONE, &cap_comment,
187 "display the capture comment ", NULL },
188 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
190 static GOptionEntry size_entries[] =
193 { "packets", 'c', 0, G_OPTION_ARG_NONE, &cap_packet_count,
194 "display the number of packets", NULL },
195 { "size", 's', 0, G_OPTION_ARG_NONE, &cap_file_size,
196 "display the size of the file (in bytes)", NULL },
197 { "tot-len-of-pkts", 'd', 0, G_OPTION_ARG_NONE, &cap_data_size,
198 "display the total length of all packets (in bytes)", NULL },
199 { "snap", 'l', 0, G_OPTION_ARG_NONE, &cap_snaplen,
200 "display the packet size limit (snapshot length)", NULL },
201 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
203 static GOptionEntry time_entries[] =
206 { "duration", 'u', 0, G_OPTION_ARG_NONE, &cap_duration,
207 "display the capture duration (in seconds)", NULL },
208 { "start", 'a', 0, G_OPTION_ARG_NONE, &cap_start_time,
209 "display the capture start time", NULL },
210 { "end", 'e', 0, G_OPTION_ARG_NONE, &cap_end_time,
211 "display the capture end time", NULL },
212 { "cron", 'o', 0, G_OPTION_ARG_NONE, &cap_order,
213 "display the capture file chronological status (True/False)", NULL },
214 { "start-end-time-sec", 'S', 0, G_OPTION_ARG_NONE, &time_as_secs,
215 "display start and end times as seconds", NULL },
216 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
219 static GOptionEntry stats_entries[] =
222 { "bytes", 'y', 0, G_OPTION_ARG_NONE, &cap_data_rate_byte,
223 "display average data rate (in bytes/s)", NULL },
224 { "bits", 'i', 0, G_OPTION_ARG_NONE, &cap_data_rate_bit,
225 "display average data rate (in bits/s)", NULL },
226 { "packet-bytes", 'z', 0, G_OPTION_ARG_NONE, &cap_packet_size,
227 "display average packet size (in bytes)", NULL },
228 { "packets", 'x', 0, G_OPTION_ARG_NONE, &cap_packet_rate,
229 "display average packet rate (in packets/s)", NULL },
230 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
233 static GOptionEntry output_format_entries[] =
236 { "long", 'L', 0, G_OPTION_ARG_NONE, &long_report,
237 "generate long report (default)", NULL },
238 { "Table", 'T', 0, G_OPTION_ARG_NONE, &table_report,
239 "generate table report", NULL },
240 { "machine-readable", 'M', 0, G_OPTION_ARG_NONE, &machine_readable,
241 "display machine-readable (unabbreviated) values in long reports", NULL },
242 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
245 static GOptionEntry table_report_entries[] =
248 { "header-rec", 'R', 0, G_OPTION_ARG_NONE, &table_report_header,
249 "generate header record (default)", NULL },
250 { "no-table", 'r', 0, G_OPTION_ARG_NONE, &table_report_header,
251 "do not generate header record", NULL },
252 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
255 static GOptionEntry misc_entries[] =
257 { "helpcompat", 'h', 0, G_OPTION_ARG_NONE, &cap_help,
258 "display help", NULL },
259 { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
263 GOptionGroup *general_grp, *size_grp, *time_grp, *stats_grp, *output_grp, *table_report_grp;
264 GError *parse_err = NULL;
266 #endif /* USE_GOPTION */
268 #ifdef HAVE_LIBGCRYPT
269 #define HASH_SIZE_SHA1 20
270 #define HASH_SIZE_RMD160 20
271 #define HASH_SIZE_MD5 16
273 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
274 #define HASH_BUF_SIZE (1024 * 1024)
277 static gchar file_sha1[HASH_STR_SIZE];
278 static gchar file_rmd160[HASH_STR_SIZE];
279 static gchar file_md5[HASH_STR_SIZE];
281 #define FILE_HASH_OPT "H"
283 #define FILE_HASH_OPT ""
284 #endif /* HAVE_LIBGCRYPT */
287 * If we have at least two packets with time stamps, and they're not in
288 * order - i.e., the later packet has a time stamp older than the earlier
289 * packet - the time stamps are known not to be in order.
291 * If every packet has a time stamp, and they're all in order, the time
292 * stamp is known to be in order.
294 * Otherwise, we have no idea.
302 typedef struct _capture_info {
303 const char *filename;
305 gboolean iscompressed;
310 guint64 packet_bytes;
311 gboolean times_known;
314 guint32 packet_count;
315 gboolean snap_set; /* If set in capture file header */
316 guint32 snaplen; /* value from the capture file header */
317 guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
318 guint32 snaplen_max_inferred; /* ... */
319 gboolean drops_known;
325 double data_rate; /* in bytes */
329 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
334 enable_all_infos(void)
336 report_all_infos = TRUE;
338 cap_file_type = TRUE;
339 cap_file_encap = TRUE;
341 cap_packet_count = TRUE;
342 cap_file_size = TRUE;
345 cap_data_size = TRUE;
347 cap_start_time = TRUE;
351 cap_data_rate_byte = TRUE;
352 cap_data_rate_bit = TRUE;
353 cap_packet_size = TRUE;
354 cap_packet_rate = TRUE;
356 #ifdef HAVE_LIBGCRYPT
357 cap_file_hashes = TRUE;
358 #endif /* HAVE_LIBGCRYPT */
362 disable_all_infos(void)
364 report_all_infos = FALSE;
366 cap_file_type = FALSE;
367 cap_file_encap = FALSE;
369 cap_packet_count = FALSE;
370 cap_file_size = FALSE;
373 cap_data_size = FALSE;
374 cap_duration = FALSE;
375 cap_start_time = FALSE;
376 cap_end_time = FALSE;
379 cap_data_rate_byte = FALSE;
380 cap_data_rate_bit = FALSE;
381 cap_packet_size = FALSE;
382 cap_packet_rate = FALSE;
384 #ifdef HAVE_LIBGCRYPT
385 cap_file_hashes = FALSE;
386 #endif /* HAVE_LIBGCRYPT */
390 order_string(order_t order)
404 return "???"; /* "cannot happen" (the next step is "Profit!") */
409 time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
411 const gchar *lf = want_lf ? "\n" : "";
412 static gchar time_string_buf[20];
413 char *time_string_ctime;
415 if (cf_info->times_known && cf_info->packet_count > 0) {
417 /* XXX - Would it be useful to show sub-second precision? */
418 g_snprintf(time_string_buf, 20, "%lu%s", (unsigned long)timer, lf);
419 return time_string_buf;
421 #if (defined _WIN32) && (_MSC_VER < 1500)
422 /* calling localtime(), and thus ctime(), on MSVC 2005 with huge values causes it to crash */
423 /* XXX - find the exact value that still does work */
424 /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
425 if (timer > 2000000000) {
426 time_string_ctime = NULL;
429 time_string_ctime = ctime(&timer);
430 if (time_string_ctime == NULL) {
431 g_snprintf(time_string_buf, 20, "Not representable%s", lf);
432 return time_string_buf;
436 * The ctime() function returns a string formatted as:
437 * "Www Mmm dd hh:mm:ss yyyy\n"
438 * The unwanted '\n' is the 24th character.
440 time_string_ctime[24] = '\0';
442 return time_string_ctime;
446 g_snprintf(time_string_buf, 15, "n/a%s", lf);
447 return time_string_buf;
451 secs_nsecs(const struct wtap_nstime * nstime)
453 return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
456 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
458 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
460 printf("%sn/a\n", text_p1);
464 print_stats(const gchar *filename, capture_info *cf_info)
466 const gchar *file_type_string, *file_encap_string;
471 /* Build printable strings for various stats */
472 file_type_string = wtap_file_type_string(cf_info->file_type);
473 file_encap_string = wtap_encap_string(cf_info->file_encap);
474 start_time_t = (time_t)cf_info->start_time;
475 stop_time_t = (time_t)cf_info->stop_time;
477 if (filename) printf ("File name: %s\n", filename);
478 if (cap_file_type) printf ("File type: %s%s\n",
480 cf_info->iscompressed ? " (gzip compressed)" : "");
481 if (cap_file_encap) printf ("File encapsulation: %s\n", file_encap_string);
482 if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
484 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
485 if (cf_info->encap_counts[i] > 0)
486 printf(" %s\n", wtap_encap_string(i));
489 if (cap_snaplen && cf_info->snap_set)
490 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
491 else if(cap_snaplen && !cf_info->snap_set)
492 printf ("Packet size limit: file hdr: (not set)\n");
493 if (cf_info->snaplen_max_inferred > 0) {
494 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
495 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
497 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
498 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
500 if (cap_packet_count) {
501 printf ("Number of packets: ");
502 if (machine_readable) {
503 printf ("%u\n", cf_info->packet_count);
505 size_string = format_size(cf_info->packet_count, format_size_unit_none);
506 printf ("%s\n", size_string);
511 printf ("File size: ");
512 if (machine_readable) {
513 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
515 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
516 printf ("%s\n", size_string);
521 printf ("Data size: ");
522 if (machine_readable) {
523 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
525 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
526 printf ("%s\n", size_string);
530 if (cf_info->times_known) {
531 if (cap_duration) /* XXX - shorten to hh:mm:ss */
532 print_value("Capture duration: ", 0, " seconds", cf_info->duration);
534 printf ("Start time: %s", time_string(start_time_t, cf_info, TRUE));
536 printf ("End time: %s", time_string(stop_time_t, cf_info, TRUE));
537 if (cap_data_rate_byte) {
538 printf ("Data byte rate: ");
539 if (machine_readable) {
540 print_value("", 2, " bytes/sec", cf_info->data_rate);
542 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
543 printf ("%s\n", size_string);
547 if (cap_data_rate_bit) {
548 printf ("Data bit rate: ");
549 if (machine_readable) {
550 print_value("", 2, " bits/sec", cf_info->data_rate*8);
552 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
553 printf ("%s\n", size_string);
558 if (cap_packet_size) printf ("Average packet size: %.2f bytes\n", cf_info->packet_size);
559 if (cf_info->times_known) {
560 if (cap_packet_rate) {
561 printf ("Average packet rate: ");
562 if (machine_readable) {
563 print_value("", 2, " packets/sec", cf_info->packet_rate);
565 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_none);
566 printf ("%spackets/sec\n", size_string);
571 #ifdef HAVE_LIBGCRYPT
572 if (cap_file_hashes) {
573 printf ("SHA1: %s\n", file_sha1);
574 printf ("RIPEMD160: %s\n", file_rmd160);
575 printf ("MD5: %s\n", file_md5);
577 #endif /* HAVE_LIBGCRYPT */
578 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
579 if (cap_comment && cf_info->comment)
580 printf ("Capture comment: %s\n", cf_info->comment);
586 if (field_separator) putchar(field_separator);
592 if (quote_char) putchar(quote_char);
596 print_stats_table_header_label(const gchar *label)
605 print_stats_table_header(void)
611 if (cap_file_type) print_stats_table_header_label("File type");
612 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
613 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
614 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
615 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
616 if (cap_packet_count) print_stats_table_header_label("Number of packets");
617 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
618 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
619 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
620 if (cap_start_time) print_stats_table_header_label("Start time");
621 if (cap_end_time) print_stats_table_header_label("End time");
622 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
623 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
624 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
625 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
626 #ifdef HAVE_LIBGCRYPT
627 if (cap_file_hashes) {
628 print_stats_table_header_label("SHA1");
629 print_stats_table_header_label("RIPEMD160");
630 print_stats_table_header_label("MD5");
632 #endif /* HAVE_LIBGCRYPT */
633 if (cap_order) print_stats_table_header_label("Strict time order");
634 if (cap_comment) print_stats_table_header_label("Capture comment");
640 print_stats_table(const gchar *filename, capture_info *cf_info)
642 const gchar *file_type_string, *file_encap_string;
646 /* Build printable strings for various stats */
647 file_type_string = wtap_file_type_string(cf_info->file_type);
648 file_encap_string = wtap_encap_string(cf_info->file_encap);
649 start_time_t = (time_t)cf_info->start_time;
650 stop_time_t = (time_t)cf_info->stop_time;
654 printf("%s", filename);
661 printf("%s", file_type_string);
665 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
666 * Output a line for each different encap with all fields repeated except
667 * the encapsulation field which has "Per Packet: ..." for each
668 * encapsulation type seen ?
670 if (cap_file_encap) {
673 printf("%s", file_encap_string);
680 if(cf_info->snap_set)
681 printf("%u", cf_info->snaplen);
685 if (cf_info->snaplen_max_inferred > 0) {
688 printf("%u", cf_info->snaplen_min_inferred);
692 printf("%u", cf_info->snaplen_max_inferred);
707 if (cap_packet_count) {
710 printf("%u", cf_info->packet_count);
717 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
724 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
731 if (cf_info->times_known)
732 printf("%f", cf_info->duration);
738 if (cap_start_time) {
741 printf("%s", time_string(start_time_t, cf_info, FALSE));
748 printf("%s", time_string(stop_time_t, cf_info, FALSE));
752 if (cap_data_rate_byte) {
755 if (cf_info->times_known)
756 printf("%.2f", cf_info->data_rate);
762 if (cap_data_rate_bit) {
765 if (cf_info->times_known)
766 printf("%.2f", cf_info->data_rate*8);
772 if (cap_packet_size) {
775 printf("%.2f", cf_info->packet_size);
779 if (cap_packet_rate) {
782 if (cf_info->times_known)
783 printf("%.2f", cf_info->packet_rate);
789 #ifdef HAVE_LIBGCRYPT
790 if (cap_file_hashes) {
793 printf("%s", file_sha1);
798 printf("%s", file_rmd160);
803 printf("%s", file_md5);
806 #endif /* HAVE_LIBGCRYPT */
811 printf("%s", order_string(cf_info->order));
818 printf("%s", cf_info->comment);
827 process_cap_file(wtap *wth, const char *filename)
836 guint32 snaplen_min_inferred = 0xffffffff;
837 guint32 snaplen_max_inferred = 0;
838 const struct wtap_pkthdr *phdr;
839 capture_info cf_info;
840 gboolean have_times = TRUE;
841 double start_time = 0;
842 double stop_time = 0;
844 double prev_time = 0;
845 gboolean know_order = FALSE;
846 order_t order = IN_ORDER;
847 wtapng_section_t *shb_inf;
851 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
853 /* Tally up data that we need to parse through the file to find */
854 while (wtap_read(wth, &err, &err_info, &data_offset)) {
855 phdr = wtap_phdr(wth);
856 if (phdr->presence_flags & WTAP_HAS_TS) {
857 prev_time = cur_time;
858 cur_time = secs_nsecs(&phdr->ts);
860 start_time = cur_time;
861 stop_time = cur_time;
862 prev_time = cur_time;
864 if (cur_time < prev_time) {
865 order = NOT_IN_ORDER;
867 if (cur_time < start_time) {
868 start_time = cur_time;
870 if (cur_time > stop_time) {
871 stop_time = cur_time;
874 have_times = FALSE; /* at least one packet has no time stamp */
875 if (order != NOT_IN_ORDER)
876 order = ORDER_UNKNOWN;
882 /* If caplen < len for a rcd, then presumably */
883 /* 'Limit packet capture length' was done for this rcd. */
884 /* Keep track as to the min/max actual snapshot lengths */
885 /* seen for this file. */
886 if (phdr->caplen < phdr->len) {
887 if (phdr->caplen < snaplen_min_inferred)
888 snaplen_min_inferred = phdr->caplen;
889 if (phdr->caplen > snaplen_max_inferred)
890 snaplen_max_inferred = phdr->caplen;
893 /* Per-packet encapsulation */
894 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
895 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
896 cf_info.encap_counts[phdr->pkt_encap] += 1;
898 fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet);
906 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
907 packet, filename, wtap_strerror(err));
910 case WTAP_ERR_UNSUPPORTED:
911 case WTAP_ERR_UNSUPPORTED_ENCAP:
912 case WTAP_ERR_BAD_FILE:
913 case WTAP_ERR_DECOMPRESS:
914 fprintf(stderr, "(%s)\n", err_info);
918 g_free(cf_info.encap_counts);
923 size = wtap_file_size(wth, &err);
926 "capinfos: Can't get size of \"%s\": %s.\n",
927 filename, g_strerror(err));
928 g_free(cf_info.encap_counts);
932 cf_info.filesize = size;
935 cf_info.file_type = wtap_file_type(wth);
936 cf_info.iscompressed = wtap_iscompressed(wth);
938 /* File Encapsulation */
939 cf_info.file_encap = wtap_file_encap(wth);
941 /* Packet size limit (snaplen) */
942 cf_info.snaplen = wtap_snapshot_length(wth);
943 if(cf_info.snaplen > 0)
944 cf_info.snap_set = TRUE;
946 cf_info.snap_set = FALSE;
948 cf_info.snaplen_min_inferred = snaplen_min_inferred;
949 cf_info.snaplen_max_inferred = snaplen_max_inferred;
952 cf_info.packet_count = packet;
955 cf_info.times_known = have_times;
956 cf_info.start_time = start_time;
957 cf_info.stop_time = stop_time;
958 cf_info.duration = stop_time-start_time;
959 cf_info.know_order = know_order;
960 cf_info.order = order;
962 /* Number of packet bytes */
963 cf_info.packet_bytes = bytes;
965 cf_info.data_rate = 0.0;
966 cf_info.packet_rate = 0.0;
967 cf_info.packet_size = 0.0;
970 if (cf_info.duration > 0.0) {
971 cf_info.data_rate = (double)bytes / (stop_time-start_time); /* Data rate per second */
972 cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
974 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
977 cf_info.comment = NULL;
978 shb_inf = wtap_file_get_shb_info(wth);
980 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
981 cf_info.comment = g_strdup(shb_inf->opt_comment);
984 if (cf_info.comment) {
985 /* multi-line comments would conflict with the formatting that capinfos uses
986 we replace linefeeds with spaces */
996 print_stats(filename, &cf_info);
998 print_stats_table(filename, &cf_info);
1001 g_free(cf_info.encap_counts);
1002 g_free(cf_info.comment);
1008 usage(gboolean is_error)
1014 /* XXX - add capinfos header info here */
1020 fprintf(output, "Capinfos %s"
1022 " (" SVNVERSION " from " SVNPATH ")"
1025 fprintf(output, "Prints various information (infos) about capture files.\n");
1026 fprintf(output, "See http://www.wireshark.org for more information.\n");
1027 fprintf(output, "\n");
1028 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1029 fprintf(output, "\n");
1030 fprintf(output, "General infos:\n");
1031 fprintf(output, " -t display the capture file type\n");
1032 fprintf(output, " -E display the capture file encapsulation\n");
1033 #ifdef HAVE_LIBGCRYPT
1034 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1036 fprintf(output, " -k display the capture comment\n");
1037 fprintf(output, "\n");
1038 fprintf(output, "Size infos:\n");
1039 fprintf(output, " -c display the number of packets\n");
1040 fprintf(output, " -s display the size of the file (in bytes)\n");
1041 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1042 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1043 fprintf(output, "\n");
1044 fprintf(output, "Time infos:\n");
1045 fprintf(output, " -u display the capture duration (in seconds)\n");
1046 fprintf(output, " -a display the capture start time\n");
1047 fprintf(output, " -e display the capture end time\n");
1048 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1049 fprintf(output, " -S display start and end times as seconds\n");
1050 fprintf(output, "\n");
1051 fprintf(output, "Statistic infos:\n");
1052 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1053 fprintf(output, " -i display average data rate (in bits/sec)\n");
1054 fprintf(output, " -z display average packet size (in bytes)\n");
1055 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1056 fprintf(output, "\n");
1057 fprintf(output, "Output format:\n");
1058 fprintf(output, " -L generate long report (default)\n");
1059 fprintf(output, " -T generate table report\n");
1060 fprintf(output, " -M display machine-readable values in long reports\n");
1061 fprintf(output, "\n");
1062 fprintf(output, "Table report options:\n");
1063 fprintf(output, " -R generate header record (default)\n");
1064 fprintf(output, " -r do not generate header record\n");
1065 fprintf(output, "\n");
1066 fprintf(output, " -B separate infos with TAB character (default)\n");
1067 fprintf(output, " -m separate infos with comma (,) character\n");
1068 fprintf(output, " -b separate infos with SPACE character\n");
1069 fprintf(output, "\n");
1070 fprintf(output, " -N do not quote infos (default)\n");
1071 fprintf(output, " -q quote infos with single quotes (')\n");
1072 fprintf(output, " -Q quote infos with double quotes (\")\n");
1073 fprintf(output, "\n");
1074 fprintf(output, "Miscellaneous:\n");
1075 fprintf(output, " -h display this help and exit\n");
1076 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1077 fprintf(output, " -A generate all infos (default)\n");
1078 fprintf(output, "\n");
1079 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1080 fprintf(output, "or adding to earlier options.\n");
1081 fprintf(output, "\n");
1082 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1083 fprintf(output, "output format.\n");
1084 #ifndef HAVE_LIBGCRYPT
1085 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1091 * Don't report failures to load plugins because most (non-wiretap) plugins
1092 * *should* fail to load (because we're not linked against libwireshark and
1093 * dissector plugins need libwireshark).
1096 failure_message(const char *msg_format _U_, va_list ap _U_)
1102 #ifdef HAVE_LIBGCRYPT
1104 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1107 for (i = 0; i < (int) length; i++) {
1108 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1111 #endif /* HAVE_LIBGCRYPT */
1114 main(int argc, char *argv[])
1120 int overall_error_status;
1124 char *init_progfile_dir_error;
1126 #ifdef HAVE_LIBGCRYPT
1128 char *hash_buf = NULL;
1129 gcry_md_hd_t hd = NULL;
1134 arg_list_utf_16to8(argc, argv);
1135 create_app_running_mutex();
1139 * Get credential information for later use.
1141 init_process_policies();
1144 /* Register wiretap plugins */
1146 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1147 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1148 g_free(init_progfile_dir_error);
1150 init_report_err(failure_message,NULL,NULL,NULL);
1155 /* Process the options */
1157 ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1158 general_grp = g_option_group_new("gen", "General infos:",
1159 "Show general options", NULL, NULL);
1160 size_grp = g_option_group_new("size", "Size infos:",
1161 "Show size options", NULL, NULL);
1162 time_grp = g_option_group_new("time", "Time infos:",
1163 "Show time options", NULL, NULL);
1164 stats_grp = g_option_group_new("stats", "Statistics infos:",
1165 "Show statistics options", NULL, NULL);
1166 output_grp = g_option_group_new("output", "Output format:",
1167 "Show output format options", NULL, NULL);
1168 table_report_grp = g_option_group_new("table", "Table report options:",
1169 "Show table report options", NULL, NULL);
1170 g_option_group_add_entries(general_grp, general_entries);
1171 g_option_group_add_entries(size_grp, size_entries);
1172 g_option_group_add_entries(time_grp, time_entries);
1173 g_option_group_add_entries(stats_grp, stats_entries);
1174 g_option_group_add_entries(output_grp, output_format_entries);
1175 g_option_group_add_entries(table_report_grp, table_report_entries);
1176 g_option_context_add_main_entries(ctx, misc_entries, NULL);
1177 g_option_context_add_group(ctx, general_grp);
1178 g_option_context_add_group(ctx, size_grp);
1179 g_option_context_add_group(ctx, time_grp);
1180 g_option_context_add_group(ctx, stats_grp);
1181 g_option_context_add_group(ctx, output_grp);
1182 g_option_context_add_group(ctx, table_report_grp);
1183 /* There's probably a better way to do this, but this works for now.
1184 GOptions displays the name in argv[0] as the name of the
1185 application. This is reasonable, but because we actually have a
1186 script wrapper that calls the executable. The name that gets
1187 displayed is not exactly the same as the command the user used
1190 argv[0] = (char *)"capinfos";
1192 /* if we have at least one cmdline option, we disable printing all infos */
1193 if (argc>=2 && report_all_infos)
1194 disable_all_infos();
1196 if( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1197 if(parse_err) g_print ("option parsing failed: %s\n", parse_err->message);
1198 g_print("%s", g_option_context_get_help (ctx, TRUE, NULL));
1201 if( cap_help || (argc < 2) ) {
1202 g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1205 g_option_context_free(ctx);
1207 #endif /* USE_GOPTION */
1208 while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxokCALTMRrSNqQBmb")) !=-1) {
1213 if (report_all_infos) disable_all_infos();
1214 cap_file_type = TRUE;
1218 if (report_all_infos) disable_all_infos();
1219 cap_file_encap = TRUE;
1223 if (report_all_infos) disable_all_infos();
1228 if (report_all_infos) disable_all_infos();
1229 cap_packet_count = TRUE;
1233 if (report_all_infos) disable_all_infos();
1234 cap_file_size = TRUE;
1238 if (report_all_infos) disable_all_infos();
1239 cap_data_size = TRUE;
1243 if (report_all_infos) disable_all_infos();
1244 cap_duration = TRUE;
1248 if (report_all_infos) disable_all_infos();
1249 cap_start_time = TRUE;
1253 if (report_all_infos) disable_all_infos();
1254 cap_end_time = TRUE;
1258 time_as_secs = TRUE;
1262 if (report_all_infos) disable_all_infos();
1263 cap_data_rate_byte = TRUE;
1267 if (report_all_infos) disable_all_infos();
1268 cap_data_rate_bit = TRUE;
1272 if (report_all_infos) disable_all_infos();
1273 cap_packet_size = TRUE;
1277 if (report_all_infos) disable_all_infos();
1278 cap_packet_rate = TRUE;
1281 #ifdef HAVE_LIBGCRYPT
1283 if (report_all_infos) disable_all_infos();
1284 cap_file_hashes = TRUE;
1289 if (report_all_infos) disable_all_infos();
1294 if (report_all_infos) disable_all_infos();
1299 continue_after_wtap_open_offline_failure = FALSE;
1311 long_report = FALSE;
1315 machine_readable = TRUE;
1319 table_report_header = TRUE;
1323 table_report_header = FALSE;
1339 field_separator = '\t';
1343 field_separator = ',';
1347 field_separator = ' ';
1355 case '?': /* Bad flag - print usage message */
1362 /* Set the C-language locale to the native environment. */
1363 setlocale(LC_ALL, "");
1365 if ((argc - optind) < 1) {
1370 if(!long_report && table_report_header) {
1371 print_stats_table_header();
1374 #ifdef HAVE_LIBGCRYPT
1375 if (cap_file_hashes) {
1376 gcry_check_version(NULL);
1377 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1379 gcry_md_enable(hd, GCRY_MD_RMD160);
1380 gcry_md_enable(hd, GCRY_MD_MD5);
1382 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1386 overall_error_status = 0;
1388 for (opt = optind; opt < argc; opt++) {
1390 #ifdef HAVE_LIBGCRYPT
1391 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1392 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1393 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1395 if (cap_file_hashes) {
1396 fh = ws_fopen(argv[opt], "rb");
1398 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1399 gcry_md_write(hd, hash_buf, hash_bytes);
1402 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1403 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1404 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1407 if (hd) gcry_md_reset(hd);
1409 #endif /* HAVE_LIBGCRYPT */
1411 wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
1414 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1415 wtap_strerror(err));
1418 case WTAP_ERR_UNSUPPORTED:
1419 case WTAP_ERR_UNSUPPORTED_ENCAP:
1420 case WTAP_ERR_BAD_FILE:
1421 fprintf(stderr, "(%s)\n", err_info);
1425 overall_error_status = 1; /* remember that an error has occurred */
1426 if(!continue_after_wtap_open_offline_failure)
1427 exit(1); /* error status */
1431 if ((opt > optind) && (long_report))
1433 status = process_cap_file(wth, argv[opt]);
1441 return overall_error_status;
1445 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1450 * indent-tabs-mode: nil
1453 * vi: set shiftwidth=2 tabstop=2 expandtab:
1454 * :indentSize=2:tabSize=2:noTabs=true: