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