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