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