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