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