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