Remove Makefile.common files
[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.num_interfaces = idb_info->interface_data->len;
1109   cf_info.interface_packet_counts  = g_array_sized_new(FALSE, TRUE, sizeof(guint32), cf_info.num_interfaces);
1110   g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1111   cf_info.pkt_interface_id_unknown = 0;
1112
1113   g_free(idb_info);
1114   idb_info = NULL;
1115
1116   /* Tally up data that we need to parse through the file to find */
1117   while (wtap_read(wth, &err, &err_info, &data_offset))  {
1118     phdr = wtap_phdr(wth);
1119     if (phdr->presence_flags & WTAP_HAS_TS) {
1120       prev_time = cur_time;
1121       cur_time = phdr->ts;
1122       if (packet == 0) {
1123         start_time = phdr->ts;
1124         start_time_tsprec = phdr->pkt_tsprec;
1125         stop_time  = phdr->ts;
1126         stop_time_tsprec = phdr->pkt_tsprec;
1127         prev_time  = phdr->ts;
1128       }
1129       if (nstime_cmp(&cur_time, &prev_time) < 0) {
1130         order = NOT_IN_ORDER;
1131       }
1132       if (nstime_cmp(&cur_time, &start_time) < 0) {
1133         start_time = cur_time;
1134         start_time_tsprec = phdr->pkt_tsprec;
1135       }
1136       if (nstime_cmp(&cur_time, &stop_time) > 0) {
1137         stop_time = cur_time;
1138         stop_time_tsprec = phdr->pkt_tsprec;
1139       }
1140     } else {
1141       have_times = FALSE; /* at least one packet has no time stamp */
1142       if (order != NOT_IN_ORDER)
1143         order = ORDER_UNKNOWN;
1144     }
1145
1146     if (phdr->rec_type == REC_TYPE_PACKET) {
1147       bytes+=phdr->len;
1148       packet++;
1149
1150       /* If caplen < len for a rcd, then presumably           */
1151       /* 'Limit packet capture length' was done for this rcd. */
1152       /* Keep track as to the min/max actual snapshot lengths */
1153       /*  seen for this file.                                 */
1154       if (phdr->caplen < phdr->len) {
1155         if (phdr->caplen < snaplen_min_inferred)
1156           snaplen_min_inferred = phdr->caplen;
1157         if (phdr->caplen > snaplen_max_inferred)
1158           snaplen_max_inferred = phdr->caplen;
1159       }
1160
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 packet encapsulation %d in frame %u of file \"%s\"\n",
1165                 phdr->pkt_encap, packet, filename);
1166       }
1167
1168       /* Packet interface_id info */
1169       if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
1170         /* cf_info.num_interfaces is size, not index, so it's one more than max index */
1171         if (phdr->interface_id >= cf_info.num_interfaces) {
1172           /*
1173            * OK, re-fetch the number of interfaces, as there might have
1174            * been an interface that was in the middle of packets, and
1175            * grow the array to be big enough for the new number of
1176            * interfaces.
1177            */
1178           idb_info = wtap_file_get_idb_info(wth);
1179
1180           cf_info.num_interfaces = idb_info->interface_data->len;
1181           g_array_set_size(cf_info.interface_packet_counts, cf_info.num_interfaces);
1182
1183           g_free(idb_info);
1184           idb_info = NULL;
1185         }
1186         if (phdr->interface_id < cf_info.num_interfaces) {
1187           g_array_index(cf_info.interface_packet_counts, guint32, phdr->interface_id) += 1;
1188         }
1189         else {
1190           cf_info.pkt_interface_id_unknown += 1;
1191         }
1192       }
1193       else {
1194         /* it's for interface_id 0 */
1195         if (cf_info.num_interfaces != 0) {
1196           g_array_index(cf_info.interface_packet_counts, guint32, 0) += 1;
1197         }
1198         else {
1199           cf_info.pkt_interface_id_unknown += 1;
1200         }
1201       }
1202     }
1203
1204   } /* while */
1205
1206   /*
1207    * Get IDB info strings.
1208    * We do this at the end, so we can get information for all IDBs in
1209    * the file, even those that come after packet records.
1210    */
1211   idb_info = wtap_file_get_idb_info(wth);
1212
1213   cf_info.idb_info_strings = g_array_sized_new(FALSE, FALSE, sizeof(gchar*), cf_info.num_interfaces);
1214   cf_info.num_interfaces = idb_info->interface_data->len;
1215   for (i = 0; i < cf_info.num_interfaces; i++) {
1216     const wtap_optionblock_t if_descr = g_array_index(idb_info->interface_data, wtap_optionblock_t, i);
1217     gchar *s = wtap_get_debug_if_descr(if_descr, 21, "\n");
1218     g_array_append_val(cf_info.idb_info_strings, s);
1219   }
1220
1221   g_free(idb_info);
1222   idb_info = NULL;
1223
1224   if (err != 0) {
1225     fprintf(stderr,
1226         "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
1227         packet, filename, wtap_strerror(err));
1228     if (err == WTAP_ERR_SHORT_READ) {
1229         /* Don't give up completely with this one. */
1230         status = 1;
1231         fprintf(stderr,
1232           "  (will continue anyway, checksums might be incorrect)\n");
1233     } else {
1234         if (err_info != NULL) {
1235             fprintf(stderr, "(%s)\n", err_info);
1236             g_free(err_info);
1237         }
1238
1239         cleanup_capture_info(&cf_info);
1240         return 1;
1241     }
1242   }
1243
1244   /* File size */
1245   size = wtap_file_size(wth, &err);
1246   if (size == -1) {
1247     fprintf(stderr,
1248         "capinfos: Can't get size of \"%s\": %s.\n",
1249         filename, g_strerror(err));
1250     cleanup_capture_info(&cf_info);
1251     return 1;
1252   }
1253
1254   cf_info.filesize = size;
1255
1256   /* File Type */
1257   cf_info.file_type = wtap_file_type_subtype(wth);
1258   cf_info.iscompressed = wtap_iscompressed(wth);
1259
1260   /* File Encapsulation */
1261   cf_info.file_encap = wtap_file_encap(wth);
1262
1263   cf_info.file_tsprec = wtap_file_tsprec(wth);
1264
1265   /* Packet size limit (snaplen) */
1266   cf_info.snaplen = wtap_snapshot_length(wth);
1267   if (cf_info.snaplen > 0)
1268     cf_info.snap_set = TRUE;
1269   else
1270     cf_info.snap_set = FALSE;
1271
1272   cf_info.snaplen_min_inferred = snaplen_min_inferred;
1273   cf_info.snaplen_max_inferred = snaplen_max_inferred;
1274
1275   /* # of packets */
1276   cf_info.packet_count = packet;
1277
1278   /* File Times */
1279   cf_info.times_known = have_times;
1280   cf_info.start_time = start_time;
1281   cf_info.start_time_tsprec = start_time_tsprec;
1282   cf_info.stop_time = stop_time;
1283   cf_info.stop_time_tsprec = stop_time_tsprec;
1284   nstime_delta(&cf_info.duration, &stop_time, &start_time);
1285   /* Duration precision is the higher of the start and stop time precisions. */
1286   if (cf_info.stop_time_tsprec > cf_info.start_time_tsprec)
1287     cf_info.duration_tsprec = cf_info.stop_time_tsprec;
1288   else
1289     cf_info.duration_tsprec = cf_info.start_time_tsprec;
1290   cf_info.know_order = know_order;
1291   cf_info.order = order;
1292
1293   /* Number of packet bytes */
1294   cf_info.packet_bytes = bytes;
1295
1296   cf_info.data_rate   = 0.0;
1297   cf_info.packet_rate = 0.0;
1298   cf_info.packet_size = 0.0;
1299
1300   if (packet > 0) {
1301     double delta_time = nstime_to_sec(&stop_time) - nstime_to_sec(&start_time);
1302     if (delta_time > 0.0) {
1303       cf_info.data_rate   = (double)bytes  / delta_time; /* Data rate per second */
1304       cf_info.packet_rate = (double)packet / delta_time; /* packet rate per second */
1305     }
1306     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
1307   }
1308
1309   if (long_report) {
1310     print_stats(filename, &cf_info);
1311   } else {
1312     print_stats_table(filename, &cf_info);
1313   }
1314
1315   cleanup_capture_info(&cf_info);
1316
1317   return status;
1318 }
1319
1320 static void
1321 print_usage(FILE *output)
1322 {
1323   fprintf(output, "\n");
1324   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1325   fprintf(output, "\n");
1326   fprintf(output, "General infos:\n");
1327   fprintf(output, "  -t display the capture file type\n");
1328   fprintf(output, "  -E display the capture file encapsulation\n");
1329   fprintf(output, "  -I display the capture file interface information\n");
1330   fprintf(output, "  -F display additional capture file information\n");
1331 #ifdef HAVE_LIBGCRYPT
1332   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1333 #endif
1334   fprintf(output, "  -k display the capture comment\n");
1335   fprintf(output, "\n");
1336   fprintf(output, "Size infos:\n");
1337   fprintf(output, "  -c display the number of packets\n");
1338   fprintf(output, "  -s display the size of the file (in bytes)\n");
1339   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
1340   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
1341   fprintf(output, "\n");
1342   fprintf(output, "Time infos:\n");
1343   fprintf(output, "  -u display the capture duration (in seconds)\n");
1344   fprintf(output, "  -a display the capture start time\n");
1345   fprintf(output, "  -e display the capture end time\n");
1346   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
1347   fprintf(output, "  -S display start and end times as seconds\n");
1348   fprintf(output, "\n");
1349   fprintf(output, "Statistic infos:\n");
1350   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
1351   fprintf(output, "  -i display average data rate (in bits/sec)\n");
1352   fprintf(output, "  -z display average packet size (in bytes)\n");
1353   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
1354   fprintf(output, "\n");
1355   fprintf(output, "Output format:\n");
1356   fprintf(output, "  -L generate long report (default)\n");
1357   fprintf(output, "  -T generate table report\n");
1358   fprintf(output, "  -M display machine-readable values in long reports\n");
1359   fprintf(output, "\n");
1360   fprintf(output, "Table report options:\n");
1361   fprintf(output, "  -R generate header record (default)\n");
1362   fprintf(output, "  -r do not generate header record\n");
1363   fprintf(output, "\n");
1364   fprintf(output, "  -B separate infos with TAB character (default)\n");
1365   fprintf(output, "  -m separate infos with comma (,) character\n");
1366   fprintf(output, "  -b separate infos with SPACE character\n");
1367   fprintf(output, "\n");
1368   fprintf(output, "  -N do not quote infos (default)\n");
1369   fprintf(output, "  -q quote infos with single quotes (')\n");
1370   fprintf(output, "  -Q quote infos with double quotes (\")\n");
1371   fprintf(output, "\n");
1372   fprintf(output, "Miscellaneous:\n");
1373   fprintf(output, "  -h display this help and exit\n");
1374   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
1375   fprintf(output, "  -A generate all infos (default)\n");
1376   fprintf(output, "\n");
1377   fprintf(output, "Options are processed from left to right order with later options superceding\n");
1378   fprintf(output, "or adding to earlier options.\n");
1379   fprintf(output, "\n");
1380   fprintf(output, "If no options are given the default is to display all infos in long report\n");
1381   fprintf(output, "output format.\n");
1382 #ifndef HAVE_LIBGCRYPT
1383   fprintf(output, "\nFile hashing support (-H) is not present.\n");
1384 #endif
1385 }
1386
1387 #ifdef HAVE_PLUGINS
1388 /*
1389  *  Don't report failures to load plugins because most (non-wiretap) plugins
1390  *  *should* fail to load (because we're not linked against libwireshark and
1391  *  dissector plugins need libwireshark).
1392  */
1393 static void
1394 failure_message(const char *msg_format _U_, va_list ap _U_)
1395 {
1396   return;
1397 }
1398 #endif
1399
1400 #ifdef HAVE_LIBGCRYPT
1401 static void
1402 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1403   int i;
1404
1405   for (i = 0; i < (int) length; i++) {
1406     g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1407   }
1408 }
1409 #endif /* HAVE_LIBGCRYPT */
1410
1411 int
1412 main(int argc, char *argv[])
1413 {
1414   GString *comp_info_str;
1415   GString *runtime_info_str;
1416   wtap  *wth;
1417   int    err;
1418   gchar *err_info;
1419   int    opt;
1420   int    overall_error_status;
1421   static const struct option long_options[] = {
1422       {"help", no_argument, NULL, 'h'},
1423       {"version", no_argument, NULL, 'v'},
1424       {0, 0, 0, 0 }
1425   };
1426
1427   int status = 0;
1428 #ifdef HAVE_PLUGINS
1429   char  *init_progfile_dir_error;
1430 #endif
1431 #ifdef HAVE_LIBGCRYPT
1432   FILE  *fh;
1433   char  *hash_buf = NULL;
1434   gcry_md_hd_t hd = NULL;
1435   size_t hash_bytes;
1436 #endif
1437
1438   /* Set the C-language locale to the native environment. */
1439   setlocale(LC_ALL, "");
1440
1441   /* Get the decimal point. */
1442   decimal_point = g_strdup(localeconv()->decimal_point);
1443
1444   /* Get the compile-time version information string */
1445   comp_info_str = get_compiled_version_info(NULL, NULL);
1446
1447   /* Get the run-time version information string */
1448   runtime_info_str = get_runtime_version_info(NULL);
1449
1450   /* Add it to the information to be reported on a crash. */
1451   ws_add_crash_info("Capinfos (Wireshark) %s\n"
1452          "\n"
1453          "%s"
1454          "\n"
1455          "%s",
1456       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
1457
1458 #ifdef _WIN32
1459   arg_list_utf_16to8(argc, argv);
1460   create_app_running_mutex();
1461 #endif /* _WIN32 */
1462
1463   /*
1464    * Get credential information for later use.
1465    */
1466   init_process_policies();
1467   init_open_routines();
1468
1469 #ifdef HAVE_PLUGINS
1470   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1471     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1472     g_free(init_progfile_dir_error);
1473   } else {
1474     /* Register all the plugin types we have. */
1475     wtap_register_plugin_types(); /* Types known to libwiretap */
1476
1477     init_report_err(failure_message, NULL, NULL, NULL);
1478
1479     /* Scan for plugins.  This does *not* call their registration routines;
1480        that's done later. */
1481     scan_plugins();
1482
1483     /* Register all libwiretap plugin modules. */
1484     register_all_wiretap_modules();
1485   }
1486 #endif
1487
1488   /* Process the options */
1489   /* FILE_HASH_OPT will be "H" if libgcrypt is compiled in, so don't use "H" */
1490   while ((opt = getopt_long(argc, argv, "abcdehiklmoqrstuvxyzABCEF" FILE_HASH_OPT "ILMNQRST", long_options, NULL)) !=-1) {
1491
1492     switch (opt) {
1493
1494       case 't':
1495         if (report_all_infos) disable_all_infos();
1496         cap_file_type = TRUE;
1497         break;
1498
1499       case 'E':
1500         if (report_all_infos) disable_all_infos();
1501         cap_file_encap = TRUE;
1502         break;
1503
1504       case 'l':
1505         if (report_all_infos) disable_all_infos();
1506         cap_snaplen = TRUE;
1507         break;
1508
1509       case 'c':
1510         if (report_all_infos) disable_all_infos();
1511         cap_packet_count = TRUE;
1512         break;
1513
1514       case 's':
1515         if (report_all_infos) disable_all_infos();
1516         cap_file_size = TRUE;
1517         break;
1518
1519       case 'd':
1520         if (report_all_infos) disable_all_infos();
1521         cap_data_size = TRUE;
1522         break;
1523
1524       case 'u':
1525         if (report_all_infos) disable_all_infos();
1526         cap_duration = TRUE;
1527         break;
1528
1529       case 'a':
1530         if (report_all_infos) disable_all_infos();
1531         cap_start_time = TRUE;
1532         break;
1533
1534       case 'e':
1535         if (report_all_infos) disable_all_infos();
1536         cap_end_time = TRUE;
1537         break;
1538
1539       case 'S':
1540         time_as_secs = TRUE;
1541         break;
1542
1543       case 'y':
1544         if (report_all_infos) disable_all_infos();
1545         cap_data_rate_byte = TRUE;
1546         break;
1547
1548       case 'i':
1549         if (report_all_infos) disable_all_infos();
1550         cap_data_rate_bit = TRUE;
1551         break;
1552
1553       case 'z':
1554         if (report_all_infos) disable_all_infos();
1555         cap_packet_size = TRUE;
1556         break;
1557
1558       case 'x':
1559         if (report_all_infos) disable_all_infos();
1560         cap_packet_rate = TRUE;
1561         break;
1562
1563 #ifdef HAVE_LIBGCRYPT
1564       case 'H':
1565         if (report_all_infos) disable_all_infos();
1566         cap_file_hashes = TRUE;
1567         break;
1568 #endif
1569
1570       case 'o':
1571         if (report_all_infos) disable_all_infos();
1572         cap_order = TRUE;
1573         break;
1574
1575       case 'k':
1576         if (report_all_infos) disable_all_infos();
1577         cap_comment = TRUE;
1578         break;
1579
1580       case 'F':
1581         if (report_all_infos) disable_all_infos();
1582         cap_file_more_info = TRUE;
1583         break;
1584
1585       case 'I':
1586         if (report_all_infos) disable_all_infos();
1587         cap_file_idb = TRUE;
1588         break;
1589
1590       case 'C':
1591         continue_after_wtap_open_offline_failure = FALSE;
1592         break;
1593
1594       case 'A':
1595         enable_all_infos();
1596         break;
1597
1598       case 'L':
1599         long_report = TRUE;
1600         break;
1601
1602       case 'T':
1603         long_report = FALSE;
1604         break;
1605
1606       case 'M':
1607         machine_readable = TRUE;
1608         break;
1609
1610       case 'R':
1611         table_report_header = TRUE;
1612         break;
1613
1614       case 'r':
1615         table_report_header = FALSE;
1616         break;
1617
1618       case 'N':
1619         quote_char = '\0';
1620         break;
1621
1622       case 'q':
1623         quote_char = '\'';
1624         break;
1625
1626       case 'Q':
1627         quote_char = '"';
1628         break;
1629
1630       case 'B':
1631         field_separator = '\t';
1632         break;
1633
1634       case 'm':
1635         field_separator = ',';
1636         break;
1637
1638       case 'b':
1639         field_separator = ' ';
1640         break;
1641
1642       case 'h':
1643         printf("Capinfos (Wireshark) %s\n"
1644                "Print various information (infos) about capture files.\n"
1645                "See https://www.wireshark.org for more information.\n",
1646                get_ws_vcs_version_info());
1647         print_usage(stdout);
1648         exit(0);
1649         break;
1650
1651       case 'v':
1652         show_version("Capinfos (Wireshark)", comp_info_str, runtime_info_str);
1653         g_string_free(comp_info_str, TRUE);
1654         g_string_free(runtime_info_str, TRUE);
1655         exit(0);
1656         break;
1657
1658       case '?':              /* Bad flag - print usage message */
1659         print_usage(stderr);
1660         exit(1);
1661         break;
1662     }
1663   }
1664
1665   if ((argc - optind) < 1) {
1666     print_usage(stderr);
1667     exit(1);
1668   }
1669
1670   if (!long_report && table_report_header) {
1671     print_stats_table_header();
1672   }
1673
1674 #ifdef HAVE_LIBGCRYPT
1675   if (cap_file_hashes) {
1676     gcry_check_version(NULL);
1677     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1678     if (hd) {
1679       gcry_md_enable(hd, GCRY_MD_RMD160);
1680       gcry_md_enable(hd, GCRY_MD_MD5);
1681     }
1682     hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1683   }
1684 #endif
1685
1686   overall_error_status = 0;
1687
1688   for (opt = optind; opt < argc; opt++) {
1689
1690 #ifdef HAVE_LIBGCRYPT
1691     g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1692     g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1693     g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1694
1695     if (cap_file_hashes) {
1696       fh = ws_fopen(argv[opt], "rb");
1697       if (fh && hd) {
1698         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1699           gcry_md_write(hd, hash_buf, hash_bytes);
1700         }
1701         gcry_md_final(hd);
1702         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1703         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1704         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1705       }
1706       if (fh) fclose(fh);
1707       if (hd) gcry_md_reset(hd);
1708     }
1709 #endif /* HAVE_LIBGCRYPT */
1710
1711     wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
1712
1713     if (!wth) {
1714       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1715           wtap_strerror(err));
1716       if (err_info != NULL) {
1717         fprintf(stderr, "(%s)\n", err_info);
1718         g_free(err_info);
1719       }
1720       overall_error_status = 1; /* remember that an error has occurred */
1721       if (!continue_after_wtap_open_offline_failure)
1722         exit(1); /* error status */
1723     }
1724
1725     if (wth) {
1726       if ((opt > optind) && (long_report))
1727         printf("\n");
1728       status = process_cap_file(wth, argv[opt]);
1729
1730       wtap_close(wth);
1731       if (status)
1732         exit(status);
1733     }
1734   }
1735
1736   return overall_error_status;
1737 }
1738
1739 /*
1740  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1741  *
1742  * Local variables:
1743  * c-basic-offset: 2
1744  * tab-width: 8
1745  * indent-tabs-mode: nil
1746  * End:
1747  *
1748  * vi: set shiftwidth=2 tabstop=8 expandtab:
1749  * :indentSize=2:tabSize=8:noTabs=true:
1750  */