Don't assume all IDBs are available aftre we open the file.
[metze/wireshark/wip.git] / capinfos.c
1 /* capinfos.c
2  * Reports capture file information including # of packets, duration, others
3  *
4  * Copyright 2004 Ian Schorr
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 /*
26  * 2009-09-19: jyoung
27  *
28  * New capinfos features
29  *
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.
35  *
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.
39  *
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.
45  *
46  * 2011-04-05: wmeier
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).
52  *
53
54  */
55
56
57 #include <config.h>
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <stdarg.h>
63 #include <locale.h>
64 #include <errno.h>
65
66 #ifdef HAVE_GETOPT_H
67 #include <getopt.h>
68 #endif
69
70 #include <glib.h>
71
72 #include <wiretap/wtap.h>
73
74 #include <wsutil/crash_info.h>
75 #include <wsutil/filesystem.h>
76 #include <wsutil/privileges.h>
77 #include <ws_version_info.h>
78 #include <wiretap/wtap_opttypes.h>
79
80 #ifdef HAVE_PLUGINS
81 #include <wsutil/plugins.h>
82 #endif
83
84 #include <wsutil/report_err.h>
85 #include <wsutil/str_util.h>
86 #include <wsutil/file_util.h>
87
88 #include <wsutil/wsgcrypt.h>
89
90 #ifndef HAVE_GETOPT_LONG
91 #include "wsutil/wsgetopt.h"
92 #endif
93
94 #ifdef _WIN32
95 #include <wsutil/unicode-utils.h>
96 #endif /* _WIN32 */
97
98 /*
99  * By default capinfos now continues processing
100  * the next filename if and when wiretap detects
101  * a problem opening a file.
102  * Use the '-C' option to revert back to original
103  * capinfos behavior which is to abort any
104  * additional file processing at first open file
105  * failure.
106  */
107
108 static gboolean continue_after_wtap_open_offline_failure = TRUE;
109
110 /*
111  * table report variables
112  */
113
114 static gboolean long_report        = TRUE;  /* By default generate long report       */
115 static gchar table_report_header   = TRUE;  /* Generate column header by default     */
116 static gchar field_separator       = '\t';  /* Use TAB as field separator by default */
117 static gchar quote_char            = '\0';  /* Do NOT quote fields by default        */
118 static gboolean machine_readable   = FALSE; /* Display machine-readable numbers      */
119
120 /*
121  * capinfos has the ability to report on a number of
122  * various characteristics ("infos") for each input file.
123  *
124  * By default reporting of all info fields is enabled.
125  *
126  * Optionally the reporting of any specific info field
127  * or combination of info fields can be enabled with
128  * individual options.
129  */
130
131 static gboolean report_all_infos   = TRUE;  /* Report all infos           */
132
133 static gboolean cap_file_type      = TRUE;  /* Report capture type        */
134 static gboolean cap_file_encap     = TRUE;  /* Report encapsulation       */
135 static gboolean cap_snaplen        = TRUE;  /* Packet size limit (snaplen)*/
136 static gboolean cap_packet_count   = TRUE;  /* Report packet count        */
137 static gboolean cap_file_size      = TRUE;  /* Report file size           */
138 static gboolean cap_comment        = TRUE;  /* Display the capture comment */
139 static gboolean cap_file_more_info = TRUE;  /* Report more file info      */
140 static gboolean cap_file_idb       = TRUE;  /* Report Interface info      */
141
142 static gboolean cap_data_size      = TRUE;  /* Report packet byte size    */
143 static gboolean cap_duration       = TRUE;  /* Report capture duration    */
144 static gboolean cap_start_time     = TRUE;  /* Report capture start time  */
145 static gboolean cap_end_time       = TRUE;  /* Report capture end time    */
146 static gboolean time_as_secs       = FALSE; /* Report time values as raw seconds */
147
148 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
149 static gboolean cap_data_rate_bit  = TRUE;  /* Report data rate bites/sec */
150 static gboolean cap_packet_size    = TRUE;  /* Report average packet size */
151 static gboolean cap_packet_rate    = TRUE;  /* Report average packet rate */
152 static gboolean cap_order          = TRUE;  /* Report if packets are in chronological order (True/False) */
153
154 #ifdef HAVE_LIBGCRYPT
155 static gboolean cap_file_hashes    = TRUE;  /* Calculate file hashes */
156
157 #define HASH_SIZE_SHA1   20
158 #define HASH_SIZE_RMD160 20
159 #define HASH_SIZE_MD5    16
160
161 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
162 #define HASH_BUF_SIZE (1024 * 1024)
163
164
165 static gchar file_sha1[HASH_STR_SIZE];
166 static gchar file_rmd160[HASH_STR_SIZE];
167 static gchar file_md5[HASH_STR_SIZE];
168
169 #define FILE_HASH_OPT "H"
170 #else
171 #define FILE_HASH_OPT ""
172 #endif /* HAVE_LIBGCRYPT */
173
174 /*
175  * If we have at least two packets with time stamps, and they're not in
176  * order - i.e., the later packet has a time stamp older than the earlier
177  * packet - the time stamps are known not to be in order.
178  *
179  * If every packet has a time stamp, and they're all in order, the time
180  * stamp is known to be in order.
181  *
182  * Otherwise, we have no idea.
183  */
184 typedef enum {
185   IN_ORDER,
186   NOT_IN_ORDER,
187   ORDER_UNKNOWN
188 } order_t;
189
190 typedef struct _capture_info {
191   const char    *filename;
192   guint16        file_type;
193   gboolean       iscompressed;
194   int            file_encap;
195   int            file_tsprec;
196   gint64         filesize;
197   wtap_optionblock_t shb;
198   guint64        packet_bytes;
199   gboolean       times_known;
200   nstime_t       start_time;
201   int            start_time_tsprec;
202   nstime_t       stop_time;
203   int            stop_time_tsprec;
204   guint32        packet_count;
205   gboolean       snap_set;                /* If set in capture file header      */
206   guint32        snaplen;                 /* value from the capture file header */
207   guint32        snaplen_min_inferred;    /* If caplen < len for 1 or more rcds */
208   guint32        snaplen_max_inferred;    /*  ...                               */
209   gboolean       drops_known;
210   guint32        drop_count;
211
212   nstime_t       duration;
213   int            duration_tsprec;
214   double         packet_rate;
215   double         packet_size;
216   double         data_rate;              /* in bytes */
217   gboolean       know_order;
218   order_t        order;
219
220   int           *encap_counts;           /* array of per_packet encap counts; array has one entry per wtap_encap type */
221
222   guint          num_interfaces;         /* number of IDBs, and thus size of interface_packet_counts array */
223   GArray        *interface_packet_counts;  /* array of per_packet interface_id counts; one entry per file IDB */
224   guint32        pkt_interface_id_unknown; /* counts if packet interface_id didn't match a known one */
225   GArray        *idb_info_strings;       /* array of IDB info strings */
226 } capture_info;
227
228 static char *decimal_point;
229
230 static void
231 enable_all_infos(void)
232 {
233   report_all_infos   = TRUE;
234
235   cap_file_type      = TRUE;
236   cap_file_encap     = TRUE;
237   cap_snaplen        = TRUE;
238   cap_packet_count   = TRUE;
239   cap_file_size      = TRUE;
240   cap_comment        = TRUE;
241   cap_file_more_info = TRUE;
242   cap_file_idb       = TRUE;
243
244   cap_data_size      = TRUE;
245   cap_duration       = TRUE;
246   cap_start_time     = TRUE;
247   cap_end_time       = TRUE;
248   cap_order          = TRUE;
249
250   cap_data_rate_byte = TRUE;
251   cap_data_rate_bit  = TRUE;
252   cap_packet_size    = TRUE;
253   cap_packet_rate    = TRUE;
254
255 #ifdef HAVE_LIBGCRYPT
256   cap_file_hashes    = TRUE;
257 #endif /* HAVE_LIBGCRYPT */
258 }
259
260 static void
261 disable_all_infos(void)
262 {
263   report_all_infos   = FALSE;
264
265   cap_file_type      = FALSE;
266   cap_file_encap     = FALSE;
267   cap_snaplen        = FALSE;
268   cap_packet_count   = FALSE;
269   cap_file_size      = FALSE;
270   cap_comment        = FALSE;
271   cap_file_more_info = FALSE;
272   cap_file_idb       = FALSE;
273
274   cap_data_size      = FALSE;
275   cap_duration       = FALSE;
276   cap_start_time     = FALSE;
277   cap_end_time       = FALSE;
278   cap_order          = FALSE;
279
280   cap_data_rate_byte = FALSE;
281   cap_data_rate_bit  = FALSE;
282   cap_packet_size    = FALSE;
283   cap_packet_rate    = FALSE;
284
285 #ifdef HAVE_LIBGCRYPT
286   cap_file_hashes    = FALSE;
287 #endif /* HAVE_LIBGCRYPT */
288 }
289
290 static const gchar *
291 order_string(order_t order)
292 {
293   switch (order) {
294
295     case IN_ORDER:
296       return "True";
297
298     case NOT_IN_ORDER:
299       return "False";
300
301     case ORDER_UNKNOWN:
302       return "Unknown";
303
304     default:
305       return "???";  /* "cannot happen" (the next step is "Profit!") */
306   }
307 }
308
309 static gchar *
310 absolute_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info)
311 {
312   static gchar  time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+9+1+1];
313   struct tm *ti_tm;
314
315   if (cf_info->times_known && cf_info->packet_count > 0) {
316     if (time_as_secs) {
317       switch (tsprecision) {
318
319       case WTAP_TSPREC_SEC:
320         g_snprintf(time_string_buf, sizeof time_string_buf,
321                    "%lu",
322                    (unsigned long)timer->secs);
323         break;
324
325       case WTAP_TSPREC_DSEC:
326         g_snprintf(time_string_buf, sizeof time_string_buf,
327                    "%lu%s%01d",
328                    (unsigned long)timer->secs,
329                    decimal_point,
330                    timer->nsecs / 100000000);
331         break;
332
333       case WTAP_TSPREC_CSEC:
334         g_snprintf(time_string_buf, sizeof time_string_buf,
335                    "%lu%s%02d",
336                    (unsigned long)timer->secs,
337                    decimal_point,
338                    timer->nsecs / 10000000);
339         break;
340
341       case WTAP_TSPREC_MSEC:
342         g_snprintf(time_string_buf, sizeof time_string_buf,
343                    "%lu%s%03d",
344                    (unsigned long)timer->secs,
345                    decimal_point,
346                    timer->nsecs / 1000000);
347         break;
348
349       case WTAP_TSPREC_USEC:
350         g_snprintf(time_string_buf, sizeof time_string_buf,
351                    "%lu%s%06d",
352                    (unsigned long)timer->secs,
353                    decimal_point,
354                    timer->nsecs / 1000);
355         break;
356
357       case WTAP_TSPREC_NSEC:
358         g_snprintf(time_string_buf, sizeof time_string_buf,
359                    "%lu%s%09d",
360                    (unsigned long)timer->secs,
361                    decimal_point,
362                    timer->nsecs);
363         break;
364
365       default:
366         g_snprintf(time_string_buf, sizeof time_string_buf,
367                    "Unknown precision %d",
368                    tsprecision);
369         break;
370       }
371       return time_string_buf;
372     } else {
373       ti_tm = localtime(&timer->secs);
374       if (ti_tm == NULL) {
375         g_snprintf(time_string_buf, sizeof time_string_buf, "Not representable");
376         return time_string_buf;
377       }
378       switch (tsprecision) {
379
380       case WTAP_TSPREC_SEC:
381         g_snprintf(time_string_buf, sizeof time_string_buf,
382                    "%04d-%02d-%02d %02d:%02d:%02d",
383                    ti_tm->tm_year + 1900,
384                    ti_tm->tm_mon + 1,
385                    ti_tm->tm_mday,
386                    ti_tm->tm_hour,
387                    ti_tm->tm_min,
388                    ti_tm->tm_sec);
389         break;
390
391       case WTAP_TSPREC_DSEC:
392         g_snprintf(time_string_buf, sizeof time_string_buf,
393                    "%04d-%02d-%02d %02d:%02d:%02d%s%01d",
394                    ti_tm->tm_year + 1900,
395                    ti_tm->tm_mon + 1,
396                    ti_tm->tm_mday,
397                    ti_tm->tm_hour,
398                    ti_tm->tm_min,
399                    ti_tm->tm_sec,
400                    decimal_point,
401                    timer->nsecs / 100000000);
402         break;
403
404       case WTAP_TSPREC_CSEC:
405         g_snprintf(time_string_buf, sizeof time_string_buf,
406                    "%04d-%02d-%02d %02d:%02d:%02d%s%02d",
407                    ti_tm->tm_year + 1900,
408                    ti_tm->tm_mon + 1,
409                    ti_tm->tm_mday,
410                    ti_tm->tm_hour,
411                    ti_tm->tm_min,
412                    ti_tm->tm_sec,
413                    decimal_point,
414                    timer->nsecs / 10000000);
415         break;
416
417       case WTAP_TSPREC_MSEC:
418         g_snprintf(time_string_buf, sizeof time_string_buf,
419                    "%04d-%02d-%02d %02d:%02d:%02d%s%03d",
420                    ti_tm->tm_year + 1900,
421                    ti_tm->tm_mon + 1,
422                    ti_tm->tm_mday,
423                    ti_tm->tm_hour,
424                    ti_tm->tm_min,
425                    ti_tm->tm_sec,
426                    decimal_point,
427                    timer->nsecs / 1000000);
428         break;
429
430       case WTAP_TSPREC_USEC:
431         g_snprintf(time_string_buf, sizeof time_string_buf,
432                    "%04d-%02d-%02d %02d:%02d:%02d%s%06d",
433                    ti_tm->tm_year + 1900,
434                    ti_tm->tm_mon + 1,
435                    ti_tm->tm_mday,
436                    ti_tm->tm_hour,
437                    ti_tm->tm_min,
438                    ti_tm->tm_sec,
439                    decimal_point,
440                    timer->nsecs / 1000);
441         break;
442
443       case WTAP_TSPREC_NSEC:
444         g_snprintf(time_string_buf, sizeof time_string_buf,
445                    "%04d-%02d-%02d %02d:%02d:%02d%s%09d",
446                    ti_tm->tm_year + 1900,
447                    ti_tm->tm_mon + 1,
448                    ti_tm->tm_mday,
449                    ti_tm->tm_hour,
450                    ti_tm->tm_min,
451                    ti_tm->tm_sec,
452                    decimal_point,
453                    timer->nsecs);
454         break;
455
456       default:
457         g_snprintf(time_string_buf, sizeof time_string_buf,
458                    "Unknown precision %d",
459                    tsprecision);
460         break;
461       }
462       return time_string_buf;
463     }
464   }
465
466   g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
467   return time_string_buf;
468 }
469
470 static gchar *
471 relative_time_string(nstime_t *timer, int tsprecision, capture_info *cf_info, gboolean want_seconds)
472 {
473   const gchar  *second = want_seconds ? " second" : "";
474   const gchar  *plural = want_seconds ? "s" : "";
475   static gchar  time_string_buf[4+1+2+1+2+1+2+1+2+1+2+1+1];
476
477   if (cf_info->times_known && cf_info->packet_count > 0) {
478     switch (tsprecision) {
479
480     case WTAP_TSPREC_SEC:
481       g_snprintf(time_string_buf, sizeof time_string_buf,
482                  "%lu%s%s",
483                  (unsigned long)timer->secs,
484                  second,
485                  timer->secs == 1 ? "" : plural);
486       break;
487
488     case WTAP_TSPREC_DSEC:
489       g_snprintf(time_string_buf, sizeof time_string_buf,
490                  "%lu%s%01d%s%s",
491                  (unsigned long)timer->secs,
492                  decimal_point,
493                  timer->nsecs / 100000000,
494                  second,
495                  (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
496       break;
497
498     case WTAP_TSPREC_CSEC:
499       g_snprintf(time_string_buf, sizeof time_string_buf,
500                  "%lu%s%02d%s%s",
501                  (unsigned long)timer->secs,
502                  decimal_point,
503                  timer->nsecs / 10000000,
504                  second,
505                  (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
506       break;
507
508     case WTAP_TSPREC_MSEC:
509       g_snprintf(time_string_buf, sizeof time_string_buf,
510                  "%lu%s%03d%s%s",
511                  (unsigned long)timer->secs,
512                  decimal_point,
513                  timer->nsecs / 1000000,
514                  second,
515                  (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
516       break;
517
518     case WTAP_TSPREC_USEC:
519       g_snprintf(time_string_buf, sizeof time_string_buf,
520                  "%lu%s%06d%s%s",
521                  (unsigned long)timer->secs,
522                  decimal_point,
523                  timer->nsecs / 1000,
524                  second,
525                  (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
526       break;
527
528     case WTAP_TSPREC_NSEC:
529       g_snprintf(time_string_buf, sizeof time_string_buf,
530                  "%lu%s%09d%s%s",
531                  (unsigned long)timer->secs,
532                  decimal_point,
533                  timer->nsecs,
534                  second,
535                  (timer->secs == 1 && timer->nsecs == 0) ? "" : plural);
536       break;
537
538     default:
539       g_snprintf(time_string_buf, sizeof time_string_buf,
540                  "Unknown precision %d",
541                  tsprecision);
542       break;
543     }
544     return time_string_buf;
545   }
546
547   g_snprintf(time_string_buf, sizeof time_string_buf, "n/a");
548   return time_string_buf;
549 }
550
551 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
552   if (value > 0.0)
553     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
554   else
555     printf("%sn/a\n", text_p1);
556 }
557
558 /* multi-line comments would conflict with the formatting that capinfos uses
559    we replace linefeeds with spaces */
560 static void
561 string_replace_newlines(gchar *str)
562 {
563   gchar *p;
564
565   if (str) {
566     p = str;
567     while (*p != '\0') {
568       if (*p == '\n')
569         *p = ' ';
570       if (*p == '\r')
571         *p = ' ';
572       p++;
573     }
574   }
575 }
576
577 static void
578 show_option_string(const char *prefix, const char *option_str)
579 {
580   char *str;
581
582   if (option_str != NULL && option_str[0] != '\0') {
583     str = g_strdup(option_str);
584     string_replace_newlines(str);
585     printf("%s%s\n", prefix, str);
586     g_free(str);
587   }
588 }
589
590 static void
591 print_stats(const gchar *filename, capture_info *cf_info)
592 {
593   const gchar           *file_type_string, *file_encap_string;
594   gchar                 *size_string;
595
596   /* Build printable strings for various stats */
597   file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
598   file_encap_string = wtap_encap_string(cf_info->file_encap);
599
600   if (filename)           printf     ("File name:           %s\n", filename);
601   if (cap_file_type)      printf     ("File type:           %s%s\n",
602       file_type_string,
603       cf_info->iscompressed ? " (gzip compressed)" : "");
604
605   if (cap_file_encap) {
606     printf      ("File encapsulation:  %s\n", file_encap_string);
607     if (cf_info->file_encap == WTAP_ENCAP_PER_PACKET) {
608       int i;
609       printf    ("Encapsulation in use by packets (# of pkts):\n");
610       for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
611         if (cf_info->encap_counts[i] > 0)
612           printf("                     %s (%d)\n",
613                  wtap_encap_string(i), cf_info->encap_counts[i]);
614       }
615     }
616   }
617   if (cap_file_more_info) {
618     printf      ("File timestamp precision:  %s (%d)\n",
619       wtap_tsprec_string(cf_info->file_tsprec), cf_info->file_tsprec);
620   }
621
622   if (cap_snaplen && cf_info->snap_set)
623     printf     ("Packet size limit:   file hdr: %u bytes\n", cf_info->snaplen);
624   else if (cap_snaplen && !cf_info->snap_set)
625     printf     ("Packet size limit:   file hdr: (not set)\n");
626   if (cf_info->snaplen_max_inferred > 0) {
627     if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
628       printf     ("Packet size limit:   inferred: %u bytes\n", cf_info->snaplen_min_inferred);
629     else
630       printf     ("Packet size limit:   inferred: %u bytes - %u bytes (range)\n",
631           cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
632   }
633   if (cap_packet_count) {
634     printf     ("Number of packets:   ");
635     if (machine_readable) {
636       printf ("%u\n", cf_info->packet_count);
637     } else {
638       size_string = format_size(cf_info->packet_count, format_size_unit_none);
639       printf ("%s\n", size_string);
640       g_free(size_string);
641     }
642   }
643   if (cap_file_size) {
644     printf     ("File size:           ");
645     if (machine_readable) {
646       printf     ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
647     } else {
648       size_string = format_size(cf_info->filesize, format_size_unit_bytes);
649       printf ("%s\n", size_string);
650       g_free(size_string);
651     }
652   }
653   if (cap_data_size) {
654     printf     ("Data size:           ");
655     if (machine_readable) {
656       printf     ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
657     } else {
658       size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
659       printf ("%s\n", size_string);
660       g_free(size_string);
661     }
662   }
663   if (cf_info->times_known) {
664     if (cap_duration) /* XXX - shorten to hh:mm:ss */
665                           printf("Capture duration:    %s\n", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, TRUE));
666     if (cap_start_time)
667                           printf("First packet time:   %s\n", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
668     if (cap_end_time)
669                           printf("Last packet time:    %s\n", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
670     if (cap_data_rate_byte) {
671                           printf("Data byte rate:      ");
672       if (machine_readable) {
673         print_value("", 2, " bytes/sec",   cf_info->data_rate);
674       } else {
675         size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
676         printf ("%s\n", size_string);
677         g_free(size_string);
678       }
679     }
680     if (cap_data_rate_bit) {
681                           printf("Data bit rate:       ");
682       if (machine_readable) {
683         print_value("", 2, " bits/sec",    cf_info->data_rate*8);
684       } else {
685         size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
686         printf ("%s\n", size_string);
687         g_free(size_string);
688       }
689     }
690   }
691   if (cap_packet_size)    printf("Average packet size: %.2f bytes\n",        cf_info->packet_size);
692   if (cf_info->times_known) {
693     if (cap_packet_rate) {
694                           printf("Average packet rate: ");
695       if (machine_readable) {
696         print_value("", 2, " packets/sec", cf_info->packet_rate);
697       } else {
698         size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_packets_s);
699         printf ("%s\n", size_string);
700         g_free(size_string);
701       }
702     }
703   }
704 #ifdef HAVE_LIBGCRYPT
705   if (cap_file_hashes) {
706     printf     ("SHA1:                %s\n", file_sha1);
707     printf     ("RIPEMD160:           %s\n", file_rmd160);
708     printf     ("MD5:                 %s\n", file_md5);
709   }
710 #endif /* HAVE_LIBGCRYPT */
711   if (cap_order)          printf     ("Strict time order:   %s\n", order_string(cf_info->order));
712
713   if (cf_info->shb != NULL) {
714     if (cap_comment) {
715       GArray *opts;
716       unsigned int i;
717
718       wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts);
719       for (i = 0; i < opts->len; i++) {
720         show_option_string("Capture comment:     ", g_array_index(opts, char *, i));
721       }
722       g_array_free(opts, TRUE);
723     }
724     if (cap_file_more_info) {
725       char *str;
726
727       wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str);
728       show_option_string("Capture hardware:    ", str);
729       wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str);
730       show_option_string("Capture oper-sys:    ", str);
731       wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str);
732       show_option_string("Capture application: ", str);
733     }
734
735     if (cap_file_idb && cf_info->num_interfaces != 0) {
736       guint i;
737       g_assert(cf_info->num_interfaces == cf_info->idb_info_strings->len);
738       printf     ("Number of interfaces in file: %u\n", cf_info->num_interfaces);
739       for (i = 0; i < cf_info->idb_info_strings->len; i++) {
740         gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
741         printf   ("Interface #%u info:\n", i);
742         printf   ("%s", s);
743         printf   ("                     Number of packets = %u\n", g_array_index(cf_info->interface_packet_counts, guint32, i));
744       }
745     }
746   }
747 }
748
749 static void
750 putsep(void)
751 {
752   if (field_separator) putchar(field_separator);
753 }
754
755 static void
756 putquote(void)
757 {
758   if (quote_char) putchar(quote_char);
759 }
760
761 static void
762 print_stats_table_header_label(const gchar *label)
763 {
764   putsep();
765   putquote();
766   printf("%s", label);
767   putquote();
768 }
769
770 static void
771 print_stats_table_header(void)
772 {
773   putquote();
774   printf("File name");
775   putquote();
776
777   if (cap_file_type)      print_stats_table_header_label("File type");
778   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
779   if (cap_file_more_info) print_stats_table_header_label("File time precision");
780   if (cap_snaplen)        print_stats_table_header_label("Packet size limit");
781   if (cap_snaplen)        print_stats_table_header_label("Packet size limit min (inferred)");
782   if (cap_snaplen)        print_stats_table_header_label("Packet size limit max (inferred)");
783   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
784   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
785   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
786   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
787   if (cap_start_time)     print_stats_table_header_label("Start time");
788   if (cap_end_time)       print_stats_table_header_label("End time");
789   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
790   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
791   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
792   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
793 #ifdef HAVE_LIBGCRYPT
794   if (cap_file_hashes) {
795     print_stats_table_header_label("SHA1");
796     print_stats_table_header_label("RIPEMD160");
797     print_stats_table_header_label("MD5");
798   }
799 #endif /* HAVE_LIBGCRYPT */
800   if (cap_order)          print_stats_table_header_label("Strict time order");
801   if (cap_comment)        print_stats_table_header_label("Capture comment");
802   if (cap_file_more_info) {
803     print_stats_table_header_label("Capture hardware");
804     print_stats_table_header_label("Capture oper-sys");
805     print_stats_table_header_label("Capture application");
806   }
807
808   printf("\n");
809 }
810
811 static void
812 print_stats_table(const gchar *filename, capture_info *cf_info)
813 {
814   const gchar           *file_type_string, *file_encap_string;
815
816   /* Build printable strings for various stats */
817   file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
818   file_encap_string = wtap_encap_string(cf_info->file_encap);
819
820   if (filename) {
821     putquote();
822     printf("%s", filename);
823     putquote();
824   }
825
826   if (cap_file_type) {
827     putsep();
828     putquote();
829     printf("%s", file_type_string);
830     putquote();
831   }
832
833   /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
834    *       Output a line for each different encap with all fields repeated except
835    *        the encapsulation field which has "Per Packet: ..." for each
836    *        encapsulation type seen ?
837    */
838   if (cap_file_encap) {
839     putsep();
840     putquote();
841     printf("%s", file_encap_string);
842     putquote();
843   }
844
845   if (cap_file_more_info) {
846     putsep();
847     putquote();
848     printf("%s", wtap_tsprec_string(cf_info->file_tsprec));
849     putquote();
850   }
851
852   if (cap_snaplen) {
853     putsep();
854     putquote();
855     if (cf_info->snap_set)
856       printf("%u", cf_info->snaplen);
857     else
858       printf("(not set)");
859     putquote();
860     if (cf_info->snaplen_max_inferred > 0) {
861       putsep();
862       putquote();
863       printf("%u", cf_info->snaplen_min_inferred);
864       putquote();
865       putsep();
866       putquote();
867       printf("%u", cf_info->snaplen_max_inferred);
868       putquote();
869     }
870     else {
871       putsep();
872       putquote();
873       printf("n/a");
874       putquote();
875       putsep();
876       putquote();
877       printf("n/a");
878       putquote();
879     }
880   }
881
882   if (cap_packet_count) {
883     putsep();
884     putquote();
885     printf("%u", cf_info->packet_count);
886     putquote();
887   }
888
889   if (cap_file_size) {
890     putsep();
891     putquote();
892     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
893     putquote();
894   }
895
896   if (cap_data_size) {
897     putsep();
898     putquote();
899     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
900     putquote();
901   }
902
903   if (cap_duration) {
904     putsep();
905     putquote();
906     printf("%s", relative_time_string(&cf_info->duration, cf_info->duration_tsprec, cf_info, FALSE));
907     putquote();
908   }
909
910   if (cap_start_time) {
911     putsep();
912     putquote();
913     printf("%s", absolute_time_string(&cf_info->start_time, cf_info->start_time_tsprec, cf_info));
914     putquote();
915   }
916
917   if (cap_end_time) {
918     putsep();
919     putquote();
920     printf("%s", absolute_time_string(&cf_info->stop_time, cf_info->stop_time_tsprec, cf_info));
921     putquote();
922   }
923
924   if (cap_data_rate_byte) {
925     putsep();
926     putquote();
927     if (cf_info->times_known)
928       printf("%.2f", cf_info->data_rate);
929     else
930       printf("n/a");
931     putquote();
932   }
933
934   if (cap_data_rate_bit) {
935     putsep();
936     putquote();
937     if (cf_info->times_known)
938       printf("%.2f", cf_info->data_rate*8);
939     else
940       printf("n/a");
941     putquote();
942   }
943
944   if (cap_packet_size) {
945     putsep();
946     putquote();
947     printf("%.2f", cf_info->packet_size);
948     putquote();
949   }
950
951   if (cap_packet_rate) {
952     putsep();
953     putquote();
954     if (cf_info->times_known)
955       printf("%.2f", cf_info->packet_rate);
956     else
957       printf("n/a");
958     putquote();
959   }
960
961 #ifdef HAVE_LIBGCRYPT
962   if (cap_file_hashes) {
963     putsep();
964     putquote();
965     printf("%s", file_sha1);
966     putquote();
967
968     putsep();
969     putquote();
970     printf("%s", file_rmd160);
971     putquote();
972
973     putsep();
974     putquote();
975     printf("%s", file_md5);
976     putquote();
977   }
978 #endif /* HAVE_LIBGCRYPT */
979
980   if (cap_order) {
981     putsep();
982     putquote();
983     printf("%s", order_string(cf_info->order));
984     putquote();
985   }
986
987   if (cf_info->shb != NULL) {
988     /*
989      * this is silly to put into a table format, but oh well
990      * note that there may be *more than one* of each of these types
991      * of options
992      */
993     if (cap_comment) {
994       GArray *opts;
995       unsigned int i;
996
997       wtap_optionblock_get_string_options(cf_info->shb, OPT_COMMENT, &opts);
998       for (i = 0; i < opts->len; i++) {
999         const char *opt_comment = g_array_index(opts, char *, i);
1000
1001         if (opt_comment != NULL) {
1002           putsep();
1003           putquote();
1004           printf("%s", opt_comment);
1005           putquote();
1006         }
1007       }
1008       g_array_free(opts, TRUE);
1009     }
1010
1011     if (cap_file_more_info) {
1012       char *str;
1013
1014       wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_HARDWARE, &str);
1015       if (str != NULL) {
1016         putsep();
1017         putquote();
1018         printf("%s", str);
1019         putquote();
1020       }
1021       wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_OS, &str);
1022       if (str != NULL) {
1023         putsep();
1024         putquote();
1025         printf("%s", str);
1026         putquote();
1027       }
1028       wtap_optionblock_get_option_string(cf_info->shb, OPT_SHB_USERAPPL, &str);
1029       if (str != NULL) {
1030         putsep();
1031         putquote();
1032         printf("%s", str);
1033         putquote();
1034       }
1035     }
1036   }
1037
1038   printf("\n");
1039 }
1040
1041 static void
1042 cleanup_capture_info(capture_info *cf_info)
1043 {
1044   guint i;
1045   g_assert(cf_info != NULL);
1046
1047   g_free(cf_info->encap_counts);
1048   cf_info->encap_counts = NULL;
1049
1050   g_array_free(cf_info->interface_packet_counts, TRUE);
1051   cf_info->interface_packet_counts = NULL;
1052
1053   if (cf_info->idb_info_strings) {
1054     for (i = 0; i < cf_info->idb_info_strings->len; i++) {
1055       gchar *s = g_array_index(cf_info->idb_info_strings, gchar*, i);
1056       g_free(s);
1057     }
1058     g_array_free(cf_info->idb_info_strings, TRUE);
1059   }
1060   cf_info->idb_info_strings = NULL;
1061 }
1062
1063 static int
1064 process_cap_file(wtap *wth, const char *filename)
1065 {
1066   int                   status = 0;
1067   int                   err;
1068   gchar                *err_info;
1069   gint64                size;
1070   gint64                data_offset;
1071
1072   guint32               packet = 0;
1073   gint64                bytes  = 0;
1074   guint32               snaplen_min_inferred = 0xffffffff;
1075   guint32               snaplen_max_inferred =          0;
1076   const struct wtap_pkthdr *phdr;
1077   capture_info          cf_info;
1078   gboolean              have_times = TRUE;
1079   nstime_t              start_time;
1080   int                   start_time_tsprec;
1081   nstime_t              stop_time;
1082   int                   stop_time_tsprec;
1083   nstime_t              cur_time;
1084   nstime_t              prev_time;
1085   gboolean              know_order = FALSE;
1086   order_t               order = IN_ORDER;
1087   guint                 i;
1088   wtapng_iface_descriptions_t *idb_info;
1089
1090   g_assert(wth != NULL);
1091   g_assert(filename != NULL);
1092
1093   nstime_set_zero(&start_time);
1094   start_time_tsprec = WTAP_TSPREC_UNKNOWN;
1095   nstime_set_zero(&stop_time);
1096   stop_time_tsprec = WTAP_TSPREC_UNKNOWN;
1097   nstime_set_zero(&cur_time);
1098   nstime_set_zero(&prev_time);
1099
1100   cf_info.shb = wtap_file_get_shb(wth);
1101
1102   cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
1103
1104   idb_info = wtap_file_get_idb_info(wth);
1105
1106   g_assert(idb_info->interface_data != NULL);
1107
1108   cf_info.interface_packet_counts  = g_array_sized_new(FALSE, TRUE, sizeof(guint32), cf_info.num_interfaces);
1109   cf_info.pkt_interface_id_unknown = 0;
1110
1111   g_free(idb_info);
1112   idb_info = NULL;
1113
1114   /* Tally up data that we need to parse through the file to find */
1115   while (wtap_read(wth, &err, &err_info, &data_offset))  {
1116     phdr = wtap_phdr(wth);
1117     if (phdr->presence_flags & WTAP_HAS_TS) {
1118       prev_time = cur_time;
1119       cur_time = phdr->ts;
1120       if (packet == 0) {
1121         start_time = phdr->ts;
1122         start_time_tsprec = phdr->pkt_tsprec;
1123         stop_time  = phdr->ts;
1124         stop_time_tsprec = phdr->pkt_tsprec;
1125         prev_time  = phdr->ts;
1126       }
1127       if (nstime_cmp(&cur_time, &prev_time) < 0) {
1128         order = NOT_IN_ORDER;
1129       }
1130       if (nstime_cmp(&cur_time, &start_time) < 0) {
1131         start_time = cur_time;
1132         start_time_tsprec = phdr->pkt_tsprec;
1133       }
1134       if (nstime_cmp(&cur_time, &stop_time) > 0) {
1135         stop_time = cur_time;
1136         stop_time_tsprec = phdr->pkt_tsprec;
1137       }
1138     } else {
1139       have_times = FALSE; /* at least one packet has no time stamp */
1140       if (order != NOT_IN_ORDER)
1141         order = ORDER_UNKNOWN;
1142     }
1143
1144     if (phdr->rec_type == REC_TYPE_PACKET) {
1145       bytes+=phdr->len;
1146       packet++;
1147
1148       /* If caplen < len for a rcd, then presumably           */
1149       /* 'Limit packet capture length' was done for this rcd. */
1150       /* Keep track as to the min/max actual snapshot lengths */
1151       /*  seen for this file.                                 */
1152       if (phdr->caplen < phdr->len) {
1153         if (phdr->caplen < snaplen_min_inferred)
1154           snaplen_min_inferred = phdr->caplen;
1155         if (phdr->caplen > snaplen_max_inferred)
1156           snaplen_max_inferred = phdr->caplen;
1157       }
1158
1159       /* Per-packet encapsulation */
1160       if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
1161         if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
1162           cf_info.encap_counts[phdr->pkt_encap] += 1;
1163         } else {
1164           fprintf(stderr, "capinfos: Unknown per-packet encapsulation %d in frame %u of file \"%s\"\n",
1165                   phdr->pkt_encap, packet, filename);
1166         }
1167       }
1168
1169       /* Packet interface_id info */
1170       if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1171         /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1172         if (phdr->interface_id >= cf_info.num_interfaces) {
1173           /*
1174            * OK, re-fetch the number of interfaces, as there might have
1175            * been an interface that was in the middle of packets, and
1176            * grow the array to be big enough for the new number of
1177            * interfaces.
1178            */
1179           idb_info = wtap_file_get_idb_info(wth);
1180
1181           cf_info.num_interfaces = idb_info->interface_data->len;
1182           g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1183
1184           g_free(idb_info);
1185           idb_info = NULL;
1186         }
1187         if (phdr->interface_id < cf_info.num_interfaces) {
1188           g_array_index(cf_info.interface_packet_counts, guint32, phdr->interface_id) += 1;
1189         }
1190         else {
1191           cf_info.pkt_interface_id_unknown += 1;
1192         }
1193       }
1194       else {
1195         /* it's for interface_id 0 */
1196         if (cf_info.num_interfaces != 0) {
1197           g_array_index(cf_info.interface_packet_counts, guint32, 0) += 1;
1198         }
1199         else {
1200           cf_info.pkt_interface_id_unknown += 1;
1201         }
1202       }
1203     }
1204
1205   } /* while */
1206
1207   /*
1208    * Get IDB info strings.
1209    * We do this at the end, so we can get information for all IDBs in
1210    * the file, even those that come after packet records.
1211    */
1212   idb_info = wtap_file_get_idb_info(wth);
1213
1214   cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1215   cf_info.num_interfaces = idb_info->interface_data->len;
1216   for (i = 0; i < cf_info.num_interfaces; i++) {
1217     const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1218     gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1219     g_array_append_val(cf_info.idb_info_strings, s);
1220   }
1221
1222   g_free(idb_info);
1223   idb_info = NULL;
1224
1225   if (err != 0) {
1226     fprintf(stderr,
1227         "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1228         packet, filename, wtap_strerror(err));
1229     if (err == WTAP_ERR_SHORT_READ) {
1230         /* Don't give up completely with this one. */
1231         status = 1;
1232         fprintf(stderr,
1233           "  (will continue anyway, checksums might be incorrect)\n");
1234     } else {
1235         if (err_info != NULL) {
1236             fprintf(stderr, "(%s)\n", err_info);
1237             g_free(err_info);
1238         }
1239
1240         cleanup_capture_info(&cf_info);
1241         return 1;
1242     }
1243   }
1244
1245   /* File size */
1246   size = wtap_file_size(wth, &err);
1247   if (size == -1) {
1248     fprintf(stderr,
1249         "capinfos: Can't get size of \"%s\": %s.\n",
1250         filename, g_strerror(err));
1251     cleanup_capture_info(&cf_info);
1252     return 1;
1253   }
1254
1255   cf_info.filesize = size;
1256
1257   /* File Type */
1258   cf_info.file_type = wtap_file_type_subtype(wth);
1259   cf_info.iscompressed = wtap_iscompressed(wth);
1260
1261   /* File Encapsulation */
1262   cf_info.file_encap = wtap_file_encap(wth);
1263
1264   cf_info.file_tsprec = wtap_file_tsprec(wth);
1265
1266   /* Packet size limit (snaplen) */
1267   cf_info.snaplen = wtap_snapshot_length(wth);
1268   if (cf_info.snaplen > 0)
1269     cf_info.snap_set = TRUE;
1270   else
1271     cf_info.snap_set = FALSE;
1272
1273   cf_info.snaplen_min_inferred = snaplen_min_inferred;
1274   cf_info.snaplen_max_inferred = snaplen_max_inferred;
1275
1276   /* # of packets */
1277   cf_info.packet_count = packet;
1278
1279   /* File Times */
1280   cf_info.times_known = have_times;
1281   cf_info.start_time = start_time;
1282   cf_info.start_time_tsprec = start_time_tsprec;
1283   cf_info.stop_time = stop_time;
1284   cf_info.stop_time_tsprec = stop_time_tsprec;
1285   nstime_delta(&cf_info.duration, &stop_time, &start_time);
1286   /* Duration precision is the higher of the start and stop time precisions. */
1287   if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1288     cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1289   else
1290     cf_info.duration_tsprec = cf_info.start_time_tsprec;
1291   cf_info.know_order = know_order;
1292   cf_info.order = order;
1293
1294   /* Number of packet bytes */
1295   cf_info.packet_bytes = bytes;
1296
1297   cf_info.data_rate   = 0.0;
1298   cf_info.packet_rate = 0.0;
1299   cf_info.packet_size = 0.0;
1300
1301   if (packet > 0) {
1302     double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1303     if (delta_time > 0.0) {
1304       cf_info.data_rate   = (double)bytes  / delta_time; /* Data rate per second */
1305       cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1306     }
1307     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
1308   }
1309
1310   if (long_report) {
1311     print_stats(filename, &cf_info);
1312   } else {
1313     print_stats_table(filename, &cf_info);
1314   }
1315
1316   cleanup_capture_info(&cf_info);
1317
1318   return status;
1319 }
1320
1321 static void
1322 print_usage(FILE *output)
1323 {
1324   fprintf(output, "\n");
1325   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1326   fprintf(output, "\n");
1327   fprintf(output, "General infos:\n");
1328   fprintf(output, "  -t display the capture file type\n");
1329   fprintf(output, "  -E display the capture file encapsulation\n");
1330   fprintf(output, "  -I display the capture file interface information\n");
1331   fprintf(output, "  -F display additional capture file information\n");
1332 #ifdef HAVE_LIBGCRYPT
1333   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1334 #endif
1335   fprintf(output, "  -k display the capture comment\n");
1336   fprintf(output, "\n");
1337   fprintf(output, "Size infos:\n");
1338   fprintf(output, "  -c display the number of packets\n");
1339   fprintf(output, "  -s display the size of the file (in bytes)\n");
1340   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
1341   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
1342   fprintf(output, "\n");
1343   fprintf(output, "Time infos:\n");
1344   fprintf(output, "  -u display the capture duration (in seconds)\n");
1345   fprintf(output, "  -a display the capture start time\n");
1346   fprintf(output, "  -e display the capture end time\n");
1347   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
1348   fprintf(output, "  -S display start and end times as seconds\n");
1349   fprintf(output, "\n");
1350   fprintf(output, "Statistic infos:\n");
1351   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
1352   fprintf(output, "  -i display average data rate (in bits/sec)\n");
1353   fprintf(output, "  -z display average packet size (in bytes)\n");
1354   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
1355   fprintf(output, "\n");
1356   fprintf(output, "Output format:\n");
1357   fprintf(output, "  -L generate long report (default)\n");
1358   fprintf(output, "  -T generate table report\n");
1359   fprintf(output, "  -M display machine-readable values in long reports\n");
1360   fprintf(output, "\n");
1361   fprintf(output, "Table report options:\n");
1362   fprintf(output, "  -R generate header record (default)\n");
1363   fprintf(output, "  -r do not generate header record\n");
1364   fprintf(output, "\n");
1365   fprintf(output, "  -B separate infos with TAB character (default)\n");
1366   fprintf(output, "  -m separate infos with comma (,) character\n");
1367   fprintf(output, "  -b separate infos with SPACE character\n");
1368   fprintf(output, "\n");
1369   fprintf(output, "  -N do not quote infos (default)\n");
1370   fprintf(output, "  -q quote infos with single quotes (')\n");
1371   fprintf(output, "  -Q quote infos with double quotes (\")\n");
1372   fprintf(output, "\n");
1373   fprintf(output, "Miscellaneous:\n");
1374   fprintf(output, "  -h display this help and exit\n");
1375   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
1376   fprintf(output, "  -A generate all infos (default)\n");
1377   fprintf(output, "\n");
1378   fprintf(output, "Options are processed from left to right order with later options superceding\n");
1379   fprintf(output, "or adding to earlier options.\n");
1380   fprintf(output, "\n");
1381   fprintf(output, "If no options are given the default is to display all infos in long report\n");
1382   fprintf(output, "output format.\n");
1383 #ifndef HAVE_LIBGCRYPT
1384   fprintf(output, "\nFile hashing support (-H) is not present.\n");
1385 #endif
1386 }
1387
1388 #ifdef HAVE_PLUGINS
1389 /*
1390  *  Don't report failures to load plugins because most (non-wiretap) plugins
1391  *  *should* fail to load (because we're not linked against libwireshark and
1392  *  dissector plugins need libwireshark).
1393  */
1394 static void
1395 failure_message(const char *msg_format _U_, va_list ap _U_)
1396 {
1397   return;
1398 }
1399 #endif
1400
1401 #ifdef HAVE_LIBGCRYPT
1402 static void
1403 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1404   int i;
1405
1406   for (i = 0; i < (int) length; i++) {
1407     g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1408   }
1409 }
1410 #endif /* HAVE_LIBGCRYPT */
1411
1412 int
1413 main(int argc, char *argv[])
1414 {
1415   GString *comp_info_str;
1416   GString *runtime_info_str;
1417   wtap  *wth;
1418   int    err;
1419   gchar *err_info;
1420   int    opt;
1421   int    overall_error_status;
1422   static const struct option long_options[] = {
1423       {"help", no_argument, NULL, 'h'},
1424       {"version", no_argument, NULL, 'v'},
1425       {0, 0, 0, 0 }
1426   };
1427
1428   int status = 0;
1429 #ifdef HAVE_PLUGINS
1430   char  *init_progfile_dir_error;
1431 #endif
1432 #ifdef HAVE_LIBGCRYPT
1433   FILE  *fh;
1434   char  *hash_buf = NULL;
1435   gcry_md_hd_t hd = NULL;
1436   size_t hash_bytes;
1437 #endif
1438
1439   /* Set the C-language locale to the native environment. */
1440   setlocale(LC_ALL, "");
1441
1442   /* Get the decimal point. */
1443   decimal_point = g_strdup(localeconv()->decimal_point);
1444
1445   /* Get the compile-time version information string */
1446   comp_info_str = get_compiled_version_info(NULL, NULL);
1447
1448   /* Get the run-time version information string */
1449   runtime_info_str = get_runtime_version_info(NULL);
1450
1451   /* Add it to the information to be reported on a crash. */
1452   ws_add_crash_info("Capinfos (Wireshark) %s\n"
1453          "\n"
1454          "%s"
1455          "\n"
1456          "%s",
1457       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1458
1459 #ifdef _WIN32
1460   arg_list_utf_16to8(argc, argv);
1461   create_app_running_mutex();
1462 #endif /* _WIN32 */
1463
1464   /*
1465    * Get credential information for later use.
1466    */
1467   init_process_policies();
1468   init_open_routines();
1469
1470 #ifdef HAVE_PLUGINS
1471   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1472     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1473     g_free(init_progfile_dir_error);
1474   } else {
1475     /* Register all the plugin types we have. */
1476     wtap_register_plugin_types(); /* Types known to libwiretap */
1477
1478     init_report_err(failure_message, NULL, NULL, NULL);
1479
1480     /* Scan for plugins.  This does *not* call their registration routines;
1481        that's done later. */
1482     scan_plugins();
1483
1484     /* Register all libwiretap plugin modules. */
1485     register_all_wiretap_modules();
1486   }
1487 #endif
1488
1489   /* Process the options */
1490   /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1491   while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1492
1493     switch (opt) {
1494
1495       case 't':
1496         if (report_all_infos) disable_all_infos();
1497         cap_file_type = TRUE;
1498         break;
1499
1500       case 'E':
1501         if (report_all_infos) disable_all_infos();
1502         cap_file_encap = TRUE;
1503         break;
1504
1505       case 'l':
1506         if (report_all_infos) disable_all_infos();
1507         cap_snaplen = TRUE;
1508         break;
1509
1510       case 'c':
1511         if (report_all_infos) disable_all_infos();
1512         cap_packet_count = TRUE;
1513         break;
1514
1515       case 's':
1516         if (report_all_infos) disable_all_infos();
1517         cap_file_size = TRUE;
1518         break;
1519
1520       case 'd':
1521         if (report_all_infos) disable_all_infos();
1522         cap_data_size = TRUE;
1523         break;
1524
1525       case 'u':
1526         if (report_all_infos) disable_all_infos();
1527         cap_duration = TRUE;
1528         break;
1529
1530       case 'a':
1531         if (report_all_infos) disable_all_infos();
1532         cap_start_time = TRUE;
1533         break;
1534
1535       case 'e':
1536         if (report_all_infos) disable_all_infos();
1537         cap_end_time = TRUE;
1538         break;
1539
1540       case 'S':
1541         time_as_secs = TRUE;
1542         break;
1543
1544       case 'y':
1545         if (report_all_infos) disable_all_infos();
1546         cap_data_rate_byte = TRUE;
1547         break;
1548
1549       case 'i':
1550         if (report_all_infos) disable_all_infos();
1551         cap_data_rate_bit = TRUE;
1552         break;
1553
1554       case 'z':
1555         if (report_all_infos) disable_all_infos();
1556         cap_packet_size = TRUE;
1557         break;
1558
1559       case 'x':
1560         if (report_all_infos) disable_all_infos();
1561         cap_packet_rate = TRUE;
1562         break;
1563
1564 #ifdef HAVE_LIBGCRYPT
1565       case 'H':
1566         if (report_all_infos) disable_all_infos();
1567         cap_file_hashes = TRUE;
1568         break;
1569 #endif
1570
1571       case 'o':
1572         if (report_all_infos) disable_all_infos();
1573         cap_order = TRUE;
1574         break;
1575
1576       case 'k':
1577         if (report_all_infos) disable_all_infos();
1578         cap_comment = TRUE;
1579         break;
1580
1581       case 'F':
1582         if (report_all_infos) disable_all_infos();
1583         cap_file_more_info = TRUE;
1584         break;
1585
1586       case 'I':
1587         if (report_all_infos) disable_all_infos();
1588         cap_file_idb = TRUE;
1589         break;
1590
1591       case 'C':
1592         continue_after_wtap_open_offline_failure = FALSE;
1593         break;
1594
1595       case 'A':
1596         enable_all_infos();
1597         break;
1598
1599       case 'L':
1600         long_report = TRUE;
1601         break;
1602
1603       case 'T':
1604         long_report = FALSE;
1605         break;
1606
1607       case 'M':
1608         machine_readable = TRUE;
1609         break;
1610
1611       case 'R':
1612         table_report_header = TRUE;
1613         break;
1614
1615       case 'r':
1616         table_report_header = FALSE;
1617         break;
1618
1619       case 'N':
1620         quote_char = '\0';
1621         break;
1622
1623       case 'q':
1624         quote_char = '\'';
1625         break;
1626
1627       case 'Q':
1628         quote_char = '"';
1629         break;
1630
1631       case 'B':
1632         field_separator = '\t';
1633         break;
1634
1635       case 'm':
1636         field_separator = ',';
1637         break;
1638
1639       case 'b':
1640         field_separator = ' ';
1641         break;
1642
1643       case 'h':
1644         printf("Capinfos (Wireshark) %s\n"
1645                "Print various information (infos) about capture files.\n"
1646                "See https://www.wireshark.org for more information.\n",
1647                get_ws_vcs_version_info());
1648         print_usage(stdout);
1649         exit(0);
1650         break;
1651
1652       case 'v':
1653         show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1654         g_string_free(comp_info_str, TRUE);
1655         g_string_free(runtime_info_str, TRUE);
1656         exit(0);
1657         break;
1658
1659       case '?':              /* Bad flag - print usage message */
1660         print_usage(stderr);
1661         exit(1);
1662         break;
1663     }
1664   }
1665
1666   if ((argc - optind) < 1) {
1667     print_usage(stderr);
1668     exit(1);
1669   }
1670
1671   if (!long_report && table_report_header) {
1672     print_stats_table_header();
1673   }
1674
1675 #ifdef HAVE_LIBGCRYPT
1676   if (cap_file_hashes) {
1677     gcry_check_version(NULL);
1678     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1679     if (hd) {
1680       gcry_md_enable(hd, GCRY_MD_RMD160);
1681       gcry_md_enable(hd, GCRY_MD_MD5);
1682     }
1683     hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1684   }
1685 #endif
1686
1687   overall_error_status = 0;
1688
1689   for (opt = optind; opt < argc; opt++) {
1690
1691 #ifdef HAVE_LIBGCRYPT
1692     g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1693     g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1694     g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1695
1696     if (cap_file_hashes) {
1697       fh = ws_fopen(argv[opt], "rb");
1698       if (fh && hd) {
1699         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1700           gcry_md_write(hd, hash_buf, hash_bytes);
1701         }
1702         gcry_md_final(hd);
1703         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1704         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1705         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1706       }
1707       if (fh) fclose(fh);
1708       if (hd) gcry_md_reset(hd);
1709     }
1710 #endif /* HAVE_LIBGCRYPT */
1711
1712     wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1713
1714     if (!wth) {
1715       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1716           wtap_strerror(err));
1717       if (err_info != NULL) {
1718         fprintf(stderr, "(%s)\n", err_info);
1719         g_free(err_info);
1720       }
1721       overall_error_status = 1; /* remember that an error has occurred */
1722       if (!continue_after_wtap_open_offline_failure)
1723         exit(1); /* error status */
1724     }
1725
1726     if (wth) {
1727       if ((opt > optind) && (long_report))
1728         printf("\n");
1729       status = process_cap_file(wth, argv[opt]);
1730
1731       wtap_close(wth);
1732       if (status)
1733         exit(status);
1734     }
1735   }
1736
1737   return overall_error_status;
1738 }
1739
1740 /*
1741  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1742  *
1743  * Local variables:
1744  * c-basic-offset: 2
1745  * tab-width: 8
1746  * indent-tabs-mode: nil
1747  * End:
1748  *
1749  * vi: set shiftwidth=2 tabstop=8 expandtab:
1750  * :indentSize=2:tabSize=8:noTabs=true:
1751  */