Witness: set col_info for interfaceInfo_state
[wireshark-wip.git] / capinfos.c
blobaf69da1478ef67eefa29b8bee718bafb43588767
1 /* capinfos.c
2 * Reports capture file information including # of packets, duration, others
4 * Copyright 2004 Ian Schorr
6 * $Id$
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.
28 * 2009-09-19: jyoung
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.
48 * 2011-04-05: wmeier
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).
59 #include "config.h"
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <stdarg.h>
65 #include <locale.h>
66 #include <errno.h>
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
72 #ifdef HAVE_SYS_TIME_H
73 #include <sys/time.h>
74 #endif
76 #include <glib.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.
86 #define WS_BUILD_DLL
87 #define RESET_SYMBOL_EXPORT
88 #include <epan/filesystem.h>
89 #include <epan/plugins.h>
90 #undef WS_BUILD_DLL
91 #define RESET_SYMBOL_EXPORT
93 #include "wtap.h"
94 #include <wsutil/report_err.h>
95 #include <wsutil/privileges.h>
96 #include <wsutil/str_util.h>
98 #ifdef HAVE_LIBGCRYPT
99 #include <wsutil/wsgcrypt.h>
100 #include <wsutil/file_util.h>
101 #endif
103 #ifndef HAVE_GETOPT
104 #include "wsutil/wsgetopt.h"
105 #endif
107 #ifdef _WIN32
108 #include <wsutil/unicode-utils.h>
109 #endif /* _WIN32 */
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
120 * failure.
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 */
169 #endif
171 #ifdef USE_GOPTION
172 static gboolean cap_help = FALSE;
173 static gboolean table_report = FALSE;
175 static GOptionEntry general_entries[] =
177 /* General */
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[] =
192 /* Size */
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[] =
205 /* Time */
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[] =
221 /* Statistics */
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[] =
235 /* Output format */
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[] =
247 /* Table report */
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 }
262 GOptionContext *ctx;
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"
282 #else
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.
296 typedef enum {
297 IN_ORDER,
298 NOT_IN_ORDER,
299 ORDER_UNKNOWN
300 } order_t;
302 typedef struct _capture_info {
303 const char *filename;
304 guint16 file_type;
305 gboolean iscompressed;
306 int file_encap;
307 gint64 filesize;
308 gchar *comment;
310 guint64 packet_bytes;
311 gboolean times_known;
312 double start_time;
313 double stop_time;
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;
320 guint32 drop_count;
322 double duration;
323 double packet_rate;
324 double packet_size;
325 double data_rate; /* in bytes */
326 gboolean know_order;
327 order_t order;
329 int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
330 } capture_info;
333 static void
334 enable_all_infos(void)
336 report_all_infos = TRUE;
338 cap_file_type = TRUE;
339 cap_file_encap = TRUE;
340 cap_snaplen = TRUE;
341 cap_packet_count = TRUE;
342 cap_file_size = TRUE;
343 cap_comment = TRUE;
345 cap_data_size = TRUE;
346 cap_duration = TRUE;
347 cap_start_time = TRUE;
348 cap_end_time = TRUE;
349 cap_order = 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 */
361 static void
362 disable_all_infos(void)
364 report_all_infos = FALSE;
366 cap_file_type = FALSE;
367 cap_file_encap = FALSE;
368 cap_snaplen = FALSE;
369 cap_packet_count = FALSE;
370 cap_file_size = FALSE;
371 cap_comment = FALSE;
373 cap_data_size = FALSE;
374 cap_duration = FALSE;
375 cap_start_time = FALSE;
376 cap_end_time = FALSE;
377 cap_order = 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 */
389 static const gchar *
390 order_string(order_t order)
392 switch (order) {
394 case IN_ORDER:
395 return "True";
397 case NOT_IN_ORDER:
398 return "False";
400 case ORDER_UNKNOWN:
401 return "Unknown";
403 default:
404 return "???"; /* "cannot happen" (the next step is "Profit!") */
408 static gchar *
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) {
416 if (time_as_secs) {
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;
420 } else {
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;
427 } else
428 #endif
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;
434 if (!want_lf) {
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;
450 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
451 if (value > 0.0)
452 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
453 else
454 printf("%sn/a\n", text_p1);
457 static void
458 print_stats(const gchar *filename, capture_info *cf_info)
460 const gchar *file_type_string, *file_encap_string;
461 time_t start_time_t;
462 time_t stop_time_t;
463 gchar *size_string;
465 /* Build printable strings for various stats */
466 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
467 file_encap_string = wtap_encap_string(cf_info->file_encap);
468 start_time_t = (time_t)cf_info->start_time;
469 stop_time_t = (time_t)cf_info->stop_time;
471 if (filename) printf ("File name: %s\n", filename);
472 if (cap_file_type) printf ("File type: %s%s\n",
473 file_type_string,
474 cf_info->iscompressed ? " (gzip compressed)" : "");
475 if (cap_file_encap) printf ("File encapsulation: %s\n", file_encap_string);
476 if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
477 int i;
478 for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
479 if (cf_info->encap_counts[i] > 0)
480 printf(" %s\n", wtap_encap_string(i));
483 if (cap_snaplen && cf_info->snap_set)
484 printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
485 else if(cap_snaplen && !cf_info->snap_set)
486 printf ("Packet size limit: file hdr: (not set)\n");
487 if (cf_info->snaplen_max_inferred > 0) {
488 if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
489 printf ("Packet size limit: inferred: %u bytes\n", cf_info->snaplen_min_inferred);
490 else
491 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
492 cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
494 if (cap_packet_count) {
495 printf ("Number of packets: ");
496 if (machine_readable) {
497 printf ("%u\n", cf_info->packet_count);
498 } else {
499 size_string = format_size(cf_info->packet_count, format_size_unit_none);
500 printf ("%s\n", size_string);
501 g_free(size_string);
504 if (cap_file_size) {
505 printf ("File size: ");
506 if (machine_readable) {
507 printf ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
508 } else {
509 size_string = format_size(cf_info->filesize, format_size_unit_bytes);
510 printf ("%s\n", size_string);
511 g_free(size_string);
514 if (cap_data_size) {
515 printf ("Data size: ");
516 if (machine_readable) {
517 printf ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
518 } else {
519 size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
520 printf ("%s\n", size_string);
521 g_free(size_string);
524 if (cf_info->times_known) {
525 if (cap_duration) /* XXX - shorten to hh:mm:ss */
526 print_value("Capture duration: ", 0, " seconds", cf_info->duration);
527 if (cap_start_time)
528 printf ("Start time: %s", time_string(start_time_t, cf_info, TRUE));
529 if (cap_end_time)
530 printf ("End time: %s", time_string(stop_time_t, cf_info, TRUE));
531 if (cap_data_rate_byte) {
532 printf ("Data byte rate: ");
533 if (machine_readable) {
534 print_value("", 2, " bytes/sec", cf_info->data_rate);
535 } else {
536 size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
537 printf ("%s\n", size_string);
538 g_free(size_string);
541 if (cap_data_rate_bit) {
542 printf ("Data bit rate: ");
543 if (machine_readable) {
544 print_value("", 2, " bits/sec", cf_info->data_rate*8);
545 } else {
546 size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
547 printf ("%s\n", size_string);
548 g_free(size_string);
552 if (cap_packet_size) printf ("Average packet size: %.2f bytes\n", cf_info->packet_size);
553 if (cf_info->times_known) {
554 if (cap_packet_rate) {
555 printf ("Average packet rate: ");
556 if (machine_readable) {
557 print_value("", 2, " packets/sec", cf_info->packet_rate);
558 } else {
559 size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_none);
560 printf ("%spackets/sec\n", size_string);
561 g_free(size_string);
565 #ifdef HAVE_LIBGCRYPT
566 if (cap_file_hashes) {
567 printf ("SHA1: %s\n", file_sha1);
568 printf ("RIPEMD160: %s\n", file_rmd160);
569 printf ("MD5: %s\n", file_md5);
571 #endif /* HAVE_LIBGCRYPT */
572 if (cap_order) printf ("Strict time order: %s\n", order_string(cf_info->order));
573 if (cap_comment && cf_info->comment)
574 printf ("Capture comment: %s\n", cf_info->comment);
577 static void
578 putsep(void)
580 if (field_separator) putchar(field_separator);
583 static void
584 putquote(void)
586 if (quote_char) putchar(quote_char);
589 static void
590 print_stats_table_header_label(const gchar *label)
592 putsep();
593 putquote();
594 printf("%s", label);
595 putquote();
598 static void
599 print_stats_table_header(void)
601 putquote();
602 printf("File name");
603 putquote();
605 if (cap_file_type) print_stats_table_header_label("File type");
606 if (cap_file_encap) print_stats_table_header_label("File encapsulation");
607 if (cap_snaplen) print_stats_table_header_label("Packet size limit");
608 if (cap_snaplen) print_stats_table_header_label("Packet size limit min (inferred)");
609 if (cap_snaplen) print_stats_table_header_label("Packet size limit max (inferred)");
610 if (cap_packet_count) print_stats_table_header_label("Number of packets");
611 if (cap_file_size) print_stats_table_header_label("File size (bytes)");
612 if (cap_data_size) print_stats_table_header_label("Data size (bytes)");
613 if (cap_duration) print_stats_table_header_label("Capture duration (seconds)");
614 if (cap_start_time) print_stats_table_header_label("Start time");
615 if (cap_end_time) print_stats_table_header_label("End time");
616 if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
617 if (cap_data_rate_bit) print_stats_table_header_label("Data bit rate (bits/sec)");
618 if (cap_packet_size) print_stats_table_header_label("Average packet size (bytes)");
619 if (cap_packet_rate) print_stats_table_header_label("Average packet rate (packets/sec)");
620 #ifdef HAVE_LIBGCRYPT
621 if (cap_file_hashes) {
622 print_stats_table_header_label("SHA1");
623 print_stats_table_header_label("RIPEMD160");
624 print_stats_table_header_label("MD5");
626 #endif /* HAVE_LIBGCRYPT */
627 if (cap_order) print_stats_table_header_label("Strict time order");
628 if (cap_comment) print_stats_table_header_label("Capture comment");
630 printf("\n");
633 static void
634 print_stats_table(const gchar *filename, capture_info *cf_info)
636 const gchar *file_type_string, *file_encap_string;
637 time_t start_time_t;
638 time_t stop_time_t;
640 /* Build printable strings for various stats */
641 file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
642 file_encap_string = wtap_encap_string(cf_info->file_encap);
643 start_time_t = (time_t)cf_info->start_time;
644 stop_time_t = (time_t)cf_info->stop_time;
646 if (filename) {
647 putquote();
648 printf("%s", filename);
649 putquote();
652 if (cap_file_type) {
653 putsep();
654 putquote();
655 printf("%s", file_type_string);
656 putquote();
659 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
660 * Output a line for each different encap with all fields repeated except
661 * the encapsulation field which has "Per Packet: ..." for each
662 * encapsulation type seen ?
664 if (cap_file_encap) {
665 putsep();
666 putquote();
667 printf("%s", file_encap_string);
668 putquote();
671 if (cap_snaplen) {
672 putsep();
673 putquote();
674 if(cf_info->snap_set)
675 printf("%u", cf_info->snaplen);
676 else
677 printf("(not set)");
678 putquote();
679 if (cf_info->snaplen_max_inferred > 0) {
680 putsep();
681 putquote();
682 printf("%u", cf_info->snaplen_min_inferred);
683 putquote();
684 putsep();
685 putquote();
686 printf("%u", cf_info->snaplen_max_inferred);
687 putquote();
689 else {
690 putsep();
691 putquote();
692 printf("n/a");
693 putquote();
694 putsep();
695 putquote();
696 printf("n/a");
697 putquote();
701 if (cap_packet_count) {
702 putsep();
703 putquote();
704 printf("%u", cf_info->packet_count);
705 putquote();
708 if (cap_file_size) {
709 putsep();
710 putquote();
711 printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
712 putquote();
715 if (cap_data_size) {
716 putsep();
717 putquote();
718 printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
719 putquote();
722 if (cap_duration) {
723 putsep();
724 putquote();
725 if (cf_info->times_known)
726 printf("%f", cf_info->duration);
727 else
728 printf("n/a");
729 putquote();
732 if (cap_start_time) {
733 putsep();
734 putquote();
735 printf("%s", time_string(start_time_t, cf_info, FALSE));
736 putquote();
739 if (cap_end_time) {
740 putsep();
741 putquote();
742 printf("%s", time_string(stop_time_t, cf_info, FALSE));
743 putquote();
746 if (cap_data_rate_byte) {
747 putsep();
748 putquote();
749 if (cf_info->times_known)
750 printf("%.2f", cf_info->data_rate);
751 else
752 printf("n/a");
753 putquote();
756 if (cap_data_rate_bit) {
757 putsep();
758 putquote();
759 if (cf_info->times_known)
760 printf("%.2f", cf_info->data_rate*8);
761 else
762 printf("n/a");
763 putquote();
766 if (cap_packet_size) {
767 putsep();
768 putquote();
769 printf("%.2f", cf_info->packet_size);
770 putquote();
773 if (cap_packet_rate) {
774 putsep();
775 putquote();
776 if (cf_info->times_known)
777 printf("%.2f", cf_info->packet_rate);
778 else
779 printf("n/a");
780 putquote();
783 #ifdef HAVE_LIBGCRYPT
784 if (cap_file_hashes) {
785 putsep();
786 putquote();
787 printf("%s", file_sha1);
788 putquote();
790 putsep();
791 putquote();
792 printf("%s", file_rmd160);
793 putquote();
795 putsep();
796 putquote();
797 printf("%s", file_md5);
798 putquote();
800 #endif /* HAVE_LIBGCRYPT */
802 if (cap_order) {
803 putsep();
804 putquote();
805 printf("%s", order_string(cf_info->order));
806 putquote();
809 if (cap_comment) {
810 putsep();
811 putquote();
812 printf("%s", cf_info->comment);
813 putquote();
817 printf("\n");
820 static int
821 process_cap_file(wtap *wth, const char *filename)
823 int status = 0;
824 int err;
825 gchar *err_info;
826 gint64 size;
827 gint64 data_offset;
829 guint32 packet = 0;
830 gint64 bytes = 0;
831 guint32 snaplen_min_inferred = 0xffffffff;
832 guint32 snaplen_max_inferred = 0;
833 const struct wtap_pkthdr *phdr;
834 capture_info cf_info;
835 gboolean have_times = TRUE;
836 double start_time = 0;
837 double stop_time = 0;
838 double cur_time = 0;
839 double prev_time = 0;
840 gboolean know_order = FALSE;
841 order_t order = IN_ORDER;
842 wtapng_section_t *shb_inf;
843 gchar *p;
846 cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
848 /* Tally up data that we need to parse through the file to find */
849 while (wtap_read(wth, &err, &err_info, &data_offset)) {
850 phdr = wtap_phdr(wth);
851 if (phdr->presence_flags & WTAP_HAS_TS) {
852 prev_time = cur_time;
853 cur_time = nstime_to_sec(&phdr->ts);
854 if(packet==0) {
855 start_time = cur_time;
856 stop_time = cur_time;
857 prev_time = cur_time;
859 if (cur_time < prev_time) {
860 order = NOT_IN_ORDER;
862 if (cur_time < start_time) {
863 start_time = cur_time;
865 if (cur_time > stop_time) {
866 stop_time = cur_time;
868 } else {
869 have_times = FALSE; /* at least one packet has no time stamp */
870 if (order != NOT_IN_ORDER)
871 order = ORDER_UNKNOWN;
874 bytes+=phdr->len;
875 packet++;
877 /* If caplen < len for a rcd, then presumably */
878 /* 'Limit packet capture length' was done for this rcd. */
879 /* Keep track as to the min/max actual snapshot lengths */
880 /* seen for this file. */
881 if (phdr->caplen < phdr->len) {
882 if (phdr->caplen < snaplen_min_inferred)
883 snaplen_min_inferred = phdr->caplen;
884 if (phdr->caplen > snaplen_max_inferred)
885 snaplen_max_inferred = phdr->caplen;
888 /* Per-packet encapsulation */
889 if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
890 if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
891 cf_info.encap_counts[phdr->pkt_encap] += 1;
892 } else {
893 fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet);
897 } /* while */
899 if (err != 0) {
900 fprintf(stderr,
901 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
902 packet, filename, wtap_strerror(err));
903 switch (err) {
905 case WTAP_ERR_SHORT_READ:
906 status = 1;
907 fprintf(stderr,
908 " (will continue anyway, checksums might be incorrect)\n");
909 break;
911 case WTAP_ERR_UNSUPPORTED:
912 case WTAP_ERR_UNSUPPORTED_ENCAP:
913 case WTAP_ERR_BAD_FILE:
914 case WTAP_ERR_DECOMPRESS:
915 fprintf(stderr, "(%s)\n", err_info);
916 g_free(err_info);
917 /* fallthrough */
919 default:
920 g_free(cf_info.encap_counts);
921 return 1;
925 /* File size */
926 size = wtap_file_size(wth, &err);
927 if (size == -1) {
928 fprintf(stderr,
929 "capinfos: Can't get size of \"%s\": %s.\n",
930 filename, g_strerror(err));
931 g_free(cf_info.encap_counts);
932 return 1;
935 cf_info.filesize = size;
937 /* File Type */
938 cf_info.file_type = wtap_file_type_subtype(wth);
939 cf_info.iscompressed = wtap_iscompressed(wth);
941 /* File Encapsulation */
942 cf_info.file_encap = wtap_file_encap(wth);
944 /* Packet size limit (snaplen) */
945 cf_info.snaplen = wtap_snapshot_length(wth);
946 if(cf_info.snaplen > 0)
947 cf_info.snap_set = TRUE;
948 else
949 cf_info.snap_set = FALSE;
951 cf_info.snaplen_min_inferred = snaplen_min_inferred;
952 cf_info.snaplen_max_inferred = snaplen_max_inferred;
954 /* # of packets */
955 cf_info.packet_count = packet;
957 /* File Times */
958 cf_info.times_known = have_times;
959 cf_info.start_time = start_time;
960 cf_info.stop_time = stop_time;
961 cf_info.duration = stop_time-start_time;
962 cf_info.know_order = know_order;
963 cf_info.order = order;
965 /* Number of packet bytes */
966 cf_info.packet_bytes = bytes;
968 cf_info.data_rate = 0.0;
969 cf_info.packet_rate = 0.0;
970 cf_info.packet_size = 0.0;
972 if (packet > 0) {
973 if (cf_info.duration > 0.0) {
974 cf_info.data_rate = (double)bytes / (stop_time-start_time); /* Data rate per second */
975 cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
977 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
980 cf_info.comment = NULL;
981 shb_inf = wtap_file_get_shb_info(wth);
982 if (shb_inf) {
983 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
984 cf_info.comment = g_strdup(shb_inf->opt_comment);
986 g_free(shb_inf);
987 if (cf_info.comment) {
988 /* multi-line comments would conflict with the formatting that capinfos uses
989 we replace linefeeds with spaces */
990 p = cf_info.comment;
991 while (*p != '\0') {
992 if (*p=='\n')
993 *p=' ';
994 p++;
998 if(long_report) {
999 print_stats(filename, &cf_info);
1000 } else {
1001 print_stats_table(filename, &cf_info);
1004 g_free(cf_info.encap_counts);
1005 g_free(cf_info.comment);
1007 return status;
1010 static void
1011 usage(gboolean is_error)
1013 FILE *output;
1015 if (!is_error) {
1016 output = stdout;
1017 /* XXX - add capinfos header info here */
1019 else {
1020 output = stderr;
1023 fprintf(output, "Capinfos %s"
1024 #ifdef SVNVERSION
1025 " (" SVNVERSION " from " SVNPATH ")"
1026 #endif
1027 "\n", VERSION);
1028 fprintf(output, "Prints various information (infos) about capture files.\n");
1029 fprintf(output, "See http://www.wireshark.org for more information.\n");
1030 fprintf(output, "\n");
1031 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1032 fprintf(output, "\n");
1033 fprintf(output, "General infos:\n");
1034 fprintf(output, " -t display the capture file type\n");
1035 fprintf(output, " -E display the capture file encapsulation\n");
1036 #ifdef HAVE_LIBGCRYPT
1037 fprintf(output, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1038 #endif
1039 fprintf(output, " -k display the capture comment\n");
1040 fprintf(output, "\n");
1041 fprintf(output, "Size infos:\n");
1042 fprintf(output, " -c display the number of packets\n");
1043 fprintf(output, " -s display the size of the file (in bytes)\n");
1044 fprintf(output, " -d display the total length of all packets (in bytes)\n");
1045 fprintf(output, " -l display the packet size limit (snapshot length)\n");
1046 fprintf(output, "\n");
1047 fprintf(output, "Time infos:\n");
1048 fprintf(output, " -u display the capture duration (in seconds)\n");
1049 fprintf(output, " -a display the capture start time\n");
1050 fprintf(output, " -e display the capture end time\n");
1051 fprintf(output, " -o display the capture file chronological status (True/False)\n");
1052 fprintf(output, " -S display start and end times as seconds\n");
1053 fprintf(output, "\n");
1054 fprintf(output, "Statistic infos:\n");
1055 fprintf(output, " -y display average data rate (in bytes/sec)\n");
1056 fprintf(output, " -i display average data rate (in bits/sec)\n");
1057 fprintf(output, " -z display average packet size (in bytes)\n");
1058 fprintf(output, " -x display average packet rate (in packets/sec)\n");
1059 fprintf(output, "\n");
1060 fprintf(output, "Output format:\n");
1061 fprintf(output, " -L generate long report (default)\n");
1062 fprintf(output, " -T generate table report\n");
1063 fprintf(output, " -M display machine-readable values in long reports\n");
1064 fprintf(output, "\n");
1065 fprintf(output, "Table report options:\n");
1066 fprintf(output, " -R generate header record (default)\n");
1067 fprintf(output, " -r do not generate header record\n");
1068 fprintf(output, "\n");
1069 fprintf(output, " -B separate infos with TAB character (default)\n");
1070 fprintf(output, " -m separate infos with comma (,) character\n");
1071 fprintf(output, " -b separate infos with SPACE character\n");
1072 fprintf(output, "\n");
1073 fprintf(output, " -N do not quote infos (default)\n");
1074 fprintf(output, " -q quote infos with single quotes (')\n");
1075 fprintf(output, " -Q quote infos with double quotes (\")\n");
1076 fprintf(output, "\n");
1077 fprintf(output, "Miscellaneous:\n");
1078 fprintf(output, " -h display this help and exit\n");
1079 fprintf(output, " -C cancel processing if file open fails (default is to continue)\n");
1080 fprintf(output, " -A generate all infos (default)\n");
1081 fprintf(output, "\n");
1082 fprintf(output, "Options are processed from left to right order with later options superceding\n");
1083 fprintf(output, "or adding to earlier options.\n");
1084 fprintf(output, "\n");
1085 fprintf(output, "If no options are given the default is to display all infos in long report\n");
1086 fprintf(output, "output format.\n");
1087 #ifndef HAVE_LIBGCRYPT
1088 fprintf(output, "\nFile hashing support (-H) is not present.\n");
1089 #endif
1092 #ifdef HAVE_PLUGINS
1094 * Don't report failures to load plugins because most (non-wiretap) plugins
1095 * *should* fail to load (because we're not linked against libwireshark and
1096 * dissector plugins need libwireshark).
1098 static void
1099 failure_message(const char *msg_format _U_, va_list ap _U_)
1101 return;
1103 #endif
1105 #ifdef HAVE_LIBGCRYPT
1106 static void
1107 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1108 int i;
1110 for (i = 0; i < (int) length; i++) {
1111 g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1114 #endif /* HAVE_LIBGCRYPT */
1117 main(int argc, char *argv[])
1119 wtap *wth;
1120 int err;
1121 gchar *err_info;
1122 int opt;
1123 int overall_error_status;
1125 int status = 0;
1126 #ifdef HAVE_PLUGINS
1127 char *init_progfile_dir_error;
1128 #endif
1129 #ifdef HAVE_LIBGCRYPT
1130 FILE *fh;
1131 char *hash_buf = NULL;
1132 gcry_md_hd_t hd = NULL;
1133 size_t hash_bytes;
1134 #endif
1136 #ifdef _WIN32
1137 arg_list_utf_16to8(argc, argv);
1138 create_app_running_mutex();
1139 #endif /* _WIN32 */
1142 * Get credential information for later use.
1144 init_process_policies();
1146 #ifdef HAVE_PLUGINS
1147 /* Register wiretap plugins */
1149 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1150 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1151 g_free(init_progfile_dir_error);
1152 } else {
1153 init_report_err(failure_message,NULL,NULL,NULL);
1154 init_plugins();
1156 #endif
1158 /* Process the options */
1159 #ifdef USE_GOPTION
1160 ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1161 general_grp = g_option_group_new("gen", "General infos:",
1162 "Show general options", NULL, NULL);
1163 size_grp = g_option_group_new("size", "Size infos:",
1164 "Show size options", NULL, NULL);
1165 time_grp = g_option_group_new("time", "Time infos:",
1166 "Show time options", NULL, NULL);
1167 stats_grp = g_option_group_new("stats", "Statistics infos:",
1168 "Show statistics options", NULL, NULL);
1169 output_grp = g_option_group_new("output", "Output format:",
1170 "Show output format options", NULL, NULL);
1171 table_report_grp = g_option_group_new("table", "Table report options:",
1172 "Show table report options", NULL, NULL);
1173 g_option_group_add_entries(general_grp, general_entries);
1174 g_option_group_add_entries(size_grp, size_entries);
1175 g_option_group_add_entries(time_grp, time_entries);
1176 g_option_group_add_entries(stats_grp, stats_entries);
1177 g_option_group_add_entries(output_grp, output_format_entries);
1178 g_option_group_add_entries(table_report_grp, table_report_entries);
1179 g_option_context_add_main_entries(ctx, misc_entries, NULL);
1180 g_option_context_add_group(ctx, general_grp);
1181 g_option_context_add_group(ctx, size_grp);
1182 g_option_context_add_group(ctx, time_grp);
1183 g_option_context_add_group(ctx, stats_grp);
1184 g_option_context_add_group(ctx, output_grp);
1185 g_option_context_add_group(ctx, table_report_grp);
1186 /* There's probably a better way to do this, but this works for now.
1187 GOptions displays the name in argv[0] as the name of the
1188 application. This is reasonable, but because we actually have a
1189 script wrapper that calls the executable. The name that gets
1190 displayed is not exactly the same as the command the user used
1191 ran.
1193 argv[0] = (char *)"capinfos";
1195 /* if we have at least one cmdline option, we disable printing all infos */
1196 if (argc>=2 && report_all_infos)
1197 disable_all_infos();
1199 if( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1200 if(parse_err) g_print ("option parsing failed: %s\n", parse_err->message);
1201 g_print("%s", g_option_context_get_help (ctx, TRUE, NULL));
1202 exit(1);
1204 if( cap_help || (argc < 2) ) {
1205 g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1206 exit(0);
1208 g_option_context_free(ctx);
1210 #endif /* USE_GOPTION */
1211 while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxokCALTMRrSNqQBmb")) !=-1) {
1213 switch (opt) {
1215 case 't':
1216 if (report_all_infos) disable_all_infos();
1217 cap_file_type = TRUE;
1218 break;
1220 case 'E':
1221 if (report_all_infos) disable_all_infos();
1222 cap_file_encap = TRUE;
1223 break;
1225 case 'l':
1226 if (report_all_infos) disable_all_infos();
1227 cap_snaplen = TRUE;
1228 break;
1230 case 'c':
1231 if (report_all_infos) disable_all_infos();
1232 cap_packet_count = TRUE;
1233 break;
1235 case 's':
1236 if (report_all_infos) disable_all_infos();
1237 cap_file_size = TRUE;
1238 break;
1240 case 'd':
1241 if (report_all_infos) disable_all_infos();
1242 cap_data_size = TRUE;
1243 break;
1245 case 'u':
1246 if (report_all_infos) disable_all_infos();
1247 cap_duration = TRUE;
1248 break;
1250 case 'a':
1251 if (report_all_infos) disable_all_infos();
1252 cap_start_time = TRUE;
1253 break;
1255 case 'e':
1256 if (report_all_infos) disable_all_infos();
1257 cap_end_time = TRUE;
1258 break;
1260 case 'S':
1261 time_as_secs = TRUE;
1262 break;
1264 case 'y':
1265 if (report_all_infos) disable_all_infos();
1266 cap_data_rate_byte = TRUE;
1267 break;
1269 case 'i':
1270 if (report_all_infos) disable_all_infos();
1271 cap_data_rate_bit = TRUE;
1272 break;
1274 case 'z':
1275 if (report_all_infos) disable_all_infos();
1276 cap_packet_size = TRUE;
1277 break;
1279 case 'x':
1280 if (report_all_infos) disable_all_infos();
1281 cap_packet_rate = TRUE;
1282 break;
1284 #ifdef HAVE_LIBGCRYPT
1285 case 'H':
1286 if (report_all_infos) disable_all_infos();
1287 cap_file_hashes = TRUE;
1288 break;
1289 #endif
1291 case 'o':
1292 if (report_all_infos) disable_all_infos();
1293 cap_order = TRUE;
1294 break;
1296 case 'k':
1297 if (report_all_infos) disable_all_infos();
1298 cap_comment = TRUE;
1299 break;
1301 case 'C':
1302 continue_after_wtap_open_offline_failure = FALSE;
1303 break;
1305 case 'A':
1306 enable_all_infos();
1307 break;
1309 case 'L':
1310 long_report = TRUE;
1311 break;
1313 case 'T':
1314 long_report = FALSE;
1315 break;
1317 case 'M':
1318 machine_readable = TRUE;
1319 break;
1321 case 'R':
1322 table_report_header = TRUE;
1323 break;
1325 case 'r':
1326 table_report_header = FALSE;
1327 break;
1329 case 'N':
1330 quote_char = '\0';
1331 break;
1333 case 'q':
1334 quote_char = '\'';
1335 break;
1337 case 'Q':
1338 quote_char = '"';
1339 break;
1341 case 'B':
1342 field_separator = '\t';
1343 break;
1345 case 'm':
1346 field_separator = ',';
1347 break;
1349 case 'b':
1350 field_separator = ' ';
1351 break;
1353 case 'h':
1354 usage(FALSE);
1355 exit(1);
1356 break;
1358 case '?': /* Bad flag - print usage message */
1359 usage(TRUE);
1360 exit(1);
1361 break;
1365 /* Set the C-language locale to the native environment. */
1366 setlocale(LC_ALL, "");
1368 if ((argc - optind) < 1) {
1369 usage(TRUE);
1370 exit(1);
1373 if(!long_report && table_report_header) {
1374 print_stats_table_header();
1377 #ifdef HAVE_LIBGCRYPT
1378 if (cap_file_hashes) {
1379 gcry_check_version(NULL);
1380 gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1381 if (hd) {
1382 gcry_md_enable(hd, GCRY_MD_RMD160);
1383 gcry_md_enable(hd, GCRY_MD_MD5);
1385 hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1387 #endif
1389 overall_error_status = 0;
1391 for (opt = optind; opt < argc; opt++) {
1393 #ifdef HAVE_LIBGCRYPT
1394 g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1395 g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1396 g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1398 if (cap_file_hashes) {
1399 fh = ws_fopen(argv[opt], "rb");
1400 if (fh && hd) {
1401 while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1402 gcry_md_write(hd, hash_buf, hash_bytes);
1404 gcry_md_final(hd);
1405 hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1406 hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1407 hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1409 if (fh) fclose(fh);
1410 if (hd) gcry_md_reset(hd);
1412 #endif /* HAVE_LIBGCRYPT */
1414 wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
1416 if (!wth) {
1417 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1418 wtap_strerror(err));
1419 switch (err) {
1421 case WTAP_ERR_UNSUPPORTED:
1422 case WTAP_ERR_UNSUPPORTED_ENCAP:
1423 case WTAP_ERR_BAD_FILE:
1424 fprintf(stderr, "(%s)\n", err_info);
1425 g_free(err_info);
1426 break;
1428 overall_error_status = 1; /* remember that an error has occurred */
1429 if(!continue_after_wtap_open_offline_failure)
1430 exit(1); /* error status */
1433 if(wth) {
1434 if ((opt > optind) && (long_report))
1435 printf("\n");
1436 status = process_cap_file(wth, argv[opt]);
1438 wtap_close(wth);
1439 if (status)
1440 exit(status);
1444 return overall_error_status;
1448 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1450 * Local variables:
1451 * c-basic-offset: 2
1452 * tab-width: 2
1453 * indent-tabs-mode: nil
1454 * End:
1456 * vi: set shiftwidth=2 tabstop=2 expandtab:
1457 * :indentSize=2:tabSize=2:noTabs=true: