Add a Manual entry to the Lua menu.
[obnox/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  * $Id$
7  *
8  * Wireshark - Network traffic analyzer
9  * By Gerald Combs <gerald@wireshark.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 /*
28  * 2009-09-19: jyoung
29  *
30  * New capinfos features
31  *
32  * Continue processing additional files after
33  * a wiretap open failure.  The new -C option
34  * reverts to capinfos' original behavior which
35  * is to cancels any further file processing at
36  * first file open failure.
37  *
38  * Change the behavior of how the default display
39  * of all infos is initiated.  This gets rid of a
40  * special post getopt() argument count test.
41  *
42  * Add new table output format (with related options)
43  * This feature allows outputting the various infos
44  * into a tab delimited text file, or to a comma
45  * separated variables file (*.csv) instead of the
46  * original "long" format.
47  */
48
49
50 #ifdef HAVE_CONFIG_H
51 #include "config.h"
52 #endif
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <stdarg.h>
58 #include <errno.h>
59
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63
64 #ifdef HAVE_SYS_TIME_H
65 #include <sys/time.h>
66 #endif
67
68 #include <glib.h>
69
70 #include <epan/packet.h>
71 #include <epan/filesystem.h>
72 #include <epan/plugins.h>
73 #include <epan/report_err.h>
74 #include "wtap.h"
75 #include <wsutil/privileges.h>
76
77 #ifdef HAVE_LIBGCRYPT
78 #include <gcrypt.h>
79 #include <wsutil/file_util.h>
80 #endif
81
82 #ifdef HAVE_GETOPT_H
83 #include <getopt.h>
84 #else
85 #include "wsutil/wsgetopt.h"
86 #endif
87
88 #ifdef _WIN32
89 #include <shellapi.h>
90 #endif /* _WIN32 */
91
92 #include "svnversion.h"
93
94 /*
95  * By default capinfos now continues processing
96  * the next filename if and when wiretap detects
97  * a problem opening a file.
98  * Use the '-C' option to revert back to original
99  * capinfos behavior which is to abort any
100  * additional file processing at first open file
101  * failure.
102  */
103
104 static gboolean continue_after_wtap_open_offline_failure = TRUE;
105
106 /*
107  * table report variables
108  */
109
110 static gboolean long_report = TRUE;         /* By default generate long report       */
111 static gchar table_report_header = TRUE;    /* Generate column header by default     */
112 static gchar field_separator = '\t';        /* Use TAB as field separator by default */
113 static gchar quote_char = '\0';             /* Do NOT quote fields by default        */
114
115 /*
116  * capinfos has the ability to report on a number of
117  * various characteristics ("infos") for each input file.
118  *
119  * By default reporting of all info fields is enabled.
120  *
121  * Optionally the reporting of any specific info field
122  * or combination of info fields can be enabled with
123  * individual options.
124  */
125
126 static gboolean report_all_infos = TRUE;    /* Report all infos           */
127
128 static gboolean cap_file_type = TRUE;       /* Report capture type        */
129 static gboolean cap_file_encap = TRUE;      /* Report encapsulation       */
130 static gboolean cap_snaplen = TRUE;         /* Packet size limit (snaplen)*/
131 static gboolean cap_packet_count = TRUE;    /* Report packet count        */
132 static gboolean cap_file_size = TRUE;       /* Report file size           */
133
134 static gboolean cap_data_size = TRUE;       /* Report packet byte size    */
135 static gboolean cap_duration = TRUE;        /* Report capture duration    */
136 static gboolean cap_start_time = TRUE;      /* Report capture start time  */
137 static gboolean cap_end_time = TRUE;        /* Report capture end time    */
138 static gboolean time_as_secs = FALSE;       /* Report time values as raw seconds */
139
140 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
141 static gboolean cap_data_rate_bit = TRUE;   /* Report data rate bites/sec */
142 static gboolean cap_packet_size = TRUE;     /* Report average packet size */
143 static gboolean cap_packet_rate = TRUE;     /* Report average packet rate */
144 static gboolean cap_in_order = TRUE;        /* Report if packets are in chronological order (True/False) */
145
146 #ifdef HAVE_LIBGCRYPT
147 static gboolean cap_file_hashes = TRUE;     /* Calculate file hashes */
148 #endif
149
150 #ifdef HAVE_LIBGCRYPT
151 #define HASH_SIZE_SHA1 20
152 #define HASH_SIZE_RMD160 20
153 #define HASH_SIZE_MD5 16
154
155 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
156 #define HASH_BUF_SIZE (1024 * 1024)
157
158
159 static gchar file_sha1[HASH_STR_SIZE];
160 static gchar file_rmd160[HASH_STR_SIZE];
161 static gchar file_md5[HASH_STR_SIZE];
162
163 #define FILE_HASH_OPT "H"
164 #else
165 #define FILE_HASH_OPT ""
166 #endif /* HAVE_LIBGCRYPT */
167
168 typedef struct _capture_info {
169   const char    *filename;
170   guint16       file_type;
171   int           file_encap;
172   gint64        filesize;
173
174   guint64       packet_bytes;
175   double        start_time;
176   double        stop_time;
177   guint32       packet_count;
178   gboolean      snap_set;                /* If set in capture file header      */
179   guint32       snaplen;                 /* value from the capture file header */
180   guint32       snaplen_min_inferred;    /* If caplen < len for 1 or more rcds */
181   guint32       snaplen_max_inferred;    /*  ...                               */
182   gboolean      drops_known;
183   guint32       drop_count;
184
185   double        duration;
186   double        packet_rate;
187   double        packet_size;
188   double        data_rate;              /* in bytes */
189   gboolean      in_order;
190 } capture_info;
191
192 static void
193 enable_all_infos(void)
194 {
195   report_all_infos = TRUE;
196
197   cap_file_type = TRUE;
198   cap_file_encap = TRUE;
199   cap_snaplen = TRUE;
200   cap_packet_count = TRUE;
201   cap_file_size = TRUE;
202
203   cap_data_size = TRUE;
204   cap_duration = TRUE;
205   cap_start_time = TRUE;
206   cap_end_time = TRUE;
207   cap_in_order = TRUE;
208
209   cap_data_rate_byte = TRUE;
210   cap_data_rate_bit = TRUE;
211   cap_packet_size = TRUE;
212   cap_packet_rate = TRUE;
213
214 #ifdef HAVE_LIBGCRYPT
215   cap_file_hashes = TRUE;
216 #endif /* HAVE_LIBGCRYPT */
217 }
218
219 static void
220 disable_all_infos(void)
221 {
222   report_all_infos   = FALSE;
223
224   cap_file_type      = FALSE;
225   cap_file_encap     = FALSE;
226   cap_snaplen        = FALSE;
227   cap_packet_count   = FALSE;
228   cap_file_size      = FALSE;
229
230   cap_data_size      = FALSE;
231   cap_duration       = FALSE;
232   cap_start_time     = FALSE;
233   cap_end_time       = FALSE;
234   cap_in_order       = FALSE;
235
236   cap_data_rate_byte = FALSE;
237   cap_data_rate_bit  = FALSE;
238   cap_packet_size    = FALSE;
239   cap_packet_rate    = FALSE;
240
241 #ifdef HAVE_LIBGCRYPT
242   cap_file_hashes = FALSE;
243 #endif /* HAVE_LIBGCRYPT */
244 }
245
246 /*
247  * ctime_no_lf()
248  *
249  * This function simply truncates the string returned
250  * from the ctime() function to remove the trailing
251  * '\n' character.
252  *
253  * The ctime() function returns a string formatted as:
254  *   "Www Mmm dd hh:mm:ss yyyy\n"
255  * The unwanted '\n' is the 24th character.
256  */
257
258 static gchar *
259 ctime_no_lf(const time_t* timer)
260 {
261   gchar *time_string;
262   time_string = ctime(timer);
263   time_string[24] = '\0';
264   return(time_string);
265 }
266
267 static gchar *
268 time_string(const time_t *timer, capture_info *cf_info, gboolean want_lf)
269 {
270   const gchar *lf = want_lf ? "\n" : "";
271   static gchar time_string_buf[15];
272
273   if (cf_info->packet_count > 0) {
274     if (time_as_secs) {
275       /* XXX - Would it be useful to show sub-second precision? */
276       g_snprintf(time_string_buf, 15, "%lu%s", (unsigned long) *timer, lf);
277       return time_string_buf;
278     } else if (want_lf) {
279       return ctime(timer);
280     } else {
281       return ctime_no_lf(timer);
282     }
283   }
284
285   g_snprintf(time_string_buf, 15, "n/a%s", lf);
286   return time_string_buf;
287 }
288
289 static double
290 secs_nsecs(const struct wtap_nstime * nstime)
291 {
292   return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
293 }
294
295 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
296   if (value > 0.0)
297     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
298   else
299     printf("%sn/a\n", text_p1);
300 }
301
302 static void
303 print_stats(const gchar *filename, capture_info *cf_info)
304 {
305   const gchar           *file_type_string, *file_encap_string;
306   time_t                start_time_t;
307   time_t                stop_time_t;
308
309   /* Build printable strings for various stats */
310   file_type_string = wtap_file_type_string(cf_info->file_type);
311   file_encap_string = wtap_encap_string(cf_info->file_encap);
312   start_time_t = (time_t)cf_info->start_time;
313   stop_time_t = (time_t)cf_info->stop_time;
314
315   if (filename)           printf     ("File name:           %s\n", filename);
316   if (cap_file_type)      printf     ("File type:           %s\n", file_type_string);
317   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
318   if (cap_snaplen && cf_info->snap_set)
319                           printf     ("Packet size limit:   file hdr: %u bytes\n", cf_info->snaplen);
320   else if(cap_snaplen && !cf_info->snap_set)
321                           printf     ("Packet size limit:   file hdr: (not set)\n");
322   if (cf_info->snaplen_max_inferred > 0) {
323     if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
324                           printf     ("Packet size limit:   inferred: %u bytes\n", cf_info->snaplen_min_inferred);
325     else
326                           printf     ("Packet size limit:   inferred: %u bytes - %u bytes (range)\n",
327                                       cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
328   }
329   if (cap_packet_count)   printf     ("Number of packets:   %u\n", cf_info->packet_count);
330   if (cap_file_size)      printf     ("File size:           %" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
331   if (cap_data_size)      printf     ("Data size:           %" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
332   if (cap_duration)       print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
333   if (cap_start_time)     printf     ("Start time:          %s", time_string(&start_time_t, cf_info, TRUE));
334   if (cap_end_time)       printf     ("End time:            %s", time_string(&stop_time_t, cf_info, TRUE));
335   if (cap_data_rate_byte) print_value("Data byte rate:      ", 2, " bytes/sec",   cf_info->data_rate);
336   if (cap_data_rate_bit)  print_value("Data bit rate:       ", 2, " bits/sec",    cf_info->data_rate*8);
337   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
338   if (cap_packet_rate)    print_value("Average packet rate: ", 2, " packets/sec", cf_info->packet_rate);
339 #ifdef HAVE_LIBGCRYPT
340   if (cap_file_hashes) {
341                           printf     ("SHA1:                %s\n", file_sha1);
342                           printf     ("RIPEMD160:           %s\n", file_rmd160);
343                           printf     ("MD5:                 %s\n", file_md5);
344   }
345   if (cap_in_order)       printf     ("Strict time order:   %s\n", (cf_info->in_order) ? "True" : "False");
346 #endif /* HAVE_LIBGCRYPT */
347 }
348
349 static void
350 putsep(void)
351 {
352   if (field_separator) putchar(field_separator);
353 }
354
355 static void
356 putquote(void)
357 {
358   if (quote_char) putchar(quote_char);
359 }
360
361 static void
362 print_stats_table_header_label(const gchar *label)
363 {
364   putsep();
365   putquote();
366   printf("%s", label);
367   putquote();
368 }
369
370 static void
371 print_stats_table_header(void)
372 {
373   putquote();
374   printf("File name");
375   putquote();
376
377   if (cap_file_type)      print_stats_table_header_label("File type");
378   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
379   if (cap_snaplen)        print_stats_table_header_label("Packet size limit");
380   if (cap_snaplen)        print_stats_table_header_label("Packet size limit min (inferred)");
381   if (cap_snaplen)        print_stats_table_header_label("Packet size limit max (inferred)");
382   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
383   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
384   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
385   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
386   if (cap_start_time)     print_stats_table_header_label("Start time");
387   if (cap_end_time)       print_stats_table_header_label("End time");
388   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
389   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
390   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
391   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
392 #ifdef HAVE_LIBGCRYPT
393   if (cap_file_hashes) {
394                           print_stats_table_header_label("SHA1");
395                           print_stats_table_header_label("RIPEMD160");
396                           print_stats_table_header_label("MD5");
397   }
398   if (cap_in_order)       print_stats_table_header_label("Strict time order");
399 #endif /* HAVE_LIBGCRYPT */
400
401   printf("\n");
402 }
403
404 static void
405 print_stats_table(const gchar *filename, capture_info *cf_info)
406 {
407   const gchar           *file_type_string, *file_encap_string;
408   time_t                start_time_t;
409   time_t                stop_time_t;
410
411   /* Build printable strings for various stats */
412   file_type_string = wtap_file_type_string(cf_info->file_type);
413   file_encap_string = wtap_encap_string(cf_info->file_encap);
414   start_time_t = (time_t)cf_info->start_time;
415   stop_time_t = (time_t)cf_info->stop_time;
416
417   if (filename) {
418     putquote();
419     printf("%s", filename);
420     putquote();
421   }
422
423   if (cap_file_type) {
424     putsep();
425     putquote();
426     printf("%s", file_type_string);
427     putquote();
428   }
429
430   if (cap_file_encap) {
431     putsep();
432     putquote();
433     printf("%s", file_encap_string);
434     putquote();
435   }
436
437   if (cap_snaplen) {
438     putsep();
439     putquote();
440     if(cf_info->snap_set)
441       printf("%u", cf_info->snaplen);
442     else
443       printf("(not set)");
444     putquote();
445     if (cf_info->snaplen_max_inferred > 0) {
446       putsep();
447       putquote();
448       printf("%u", cf_info->snaplen_min_inferred);
449       putquote();
450       putsep();
451       putquote();
452       printf("%u", cf_info->snaplen_max_inferred);
453       putquote();
454     }
455     else {
456       putsep();
457       putquote();
458       printf("n/a");
459       putquote();
460       putsep();
461       putquote();
462       printf("n/a");
463       putquote();
464     }
465   }
466
467   if (cap_packet_count) {
468     putsep();
469     putquote();
470     printf("%u", cf_info->packet_count);
471     putquote();
472   }
473
474   if (cap_file_size) {
475     putsep();
476     putquote();
477     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
478     putquote();
479   }
480
481   if (cap_data_size) {
482     putsep();
483     putquote();
484     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
485     putquote();
486   }
487
488   if (cap_duration) {
489     putsep();
490     putquote();
491     printf("%f", cf_info->duration);
492     putquote();
493   }
494
495   if (cap_start_time) {
496     putsep();
497     putquote();
498     printf("%s", time_string(&start_time_t, cf_info, FALSE));
499     putquote();
500   }
501
502   if (cap_end_time) {
503     putsep();
504     putquote();
505     printf("%s", time_string(&stop_time_t, cf_info, FALSE));
506     putquote();
507   }
508
509   if (cap_data_rate_byte) {
510     putsep();
511     putquote();
512     printf("%.2f", cf_info->data_rate);
513     putquote();
514   }
515
516   if (cap_data_rate_bit) {
517     putsep();
518     putquote();
519     printf("%.2f", cf_info->data_rate*8);
520     putquote();
521   }
522
523   if (cap_packet_size) {
524     putsep();
525     putquote();
526     printf("%.2f", cf_info->packet_size);
527     putquote();
528   }
529
530   if (cap_packet_rate) {
531     putsep();
532     putquote();
533     printf("%.2f", cf_info->packet_rate);
534     putquote();
535   }
536
537 #ifdef HAVE_LIBGCRYPT
538   if (cap_file_hashes) {
539     putsep();
540     putquote();
541     printf("%s", file_sha1);
542     putquote();
543
544     putsep();
545     putquote();
546     printf("%s", file_rmd160);
547     putquote();
548
549     putsep();
550     putquote();
551     printf("%s", file_md5);
552     putquote();
553   }
554 #endif /* HAVE_LIBGCRYPT */
555
556   if (cap_in_order) {
557     putsep();
558     putquote();
559     printf("%s", (cf_info->in_order) ? "True" : "False");
560     putquote();
561   }
562
563   printf("\n");
564 }
565
566 static int
567 process_cap_file(wtap *wth, const char *filename)
568 {
569   int                   err;
570   gchar                 *err_info;
571   gint64                size;
572   gint64                data_offset;
573
574   guint32               packet = 0;
575   gint64                bytes  = 0;
576   guint32               snaplen_min_inferred = 0xffffffff;
577   guint32               snaplen_max_inferred =          0;
578   const struct wtap_pkthdr *phdr;
579   capture_info          cf_info;
580   double                start_time = 0;
581   double                stop_time  = 0;
582   double                cur_time   = 0;
583   double                prev_time = 0;
584   gboolean              in_order = TRUE;
585
586   /* Tally up data that we need to parse through the file to find */
587   while (wtap_read(wth, &err, &err_info, &data_offset))  {
588     phdr = wtap_phdr(wth);
589     prev_time = cur_time;
590     cur_time = secs_nsecs(&phdr->ts);
591     if(packet==0) {
592       start_time = cur_time;
593       stop_time = cur_time;
594       prev_time = cur_time;
595     }
596     if (cur_time < prev_time) {
597       in_order = FALSE;
598     }
599     if (cur_time < start_time) {
600       start_time = cur_time;
601     }
602     if (cur_time > stop_time) {
603       stop_time = cur_time;
604     }
605
606     bytes+=phdr->len;
607     packet++;
608
609     /* If caplen < len for a rcd, then presumably           */
610     /* 'Limit packet capture length' was done for this rcd. */
611     /* Keep track as to the min/max actual snapshot lengths */
612     /*  seen for this file.                                 */
613     if (phdr->caplen < phdr->len) {
614       if (phdr->caplen < snaplen_min_inferred)
615         snaplen_min_inferred = phdr->caplen;
616       if (phdr->caplen > snaplen_max_inferred)
617         snaplen_max_inferred = phdr->caplen;
618     }
619
620   }
621
622   if (err != 0) {
623     fprintf(stderr,
624             "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
625             packet, filename, wtap_strerror(err));
626     switch (err) {
627
628     case WTAP_ERR_UNSUPPORTED:
629     case WTAP_ERR_UNSUPPORTED_ENCAP:
630     case WTAP_ERR_BAD_RECORD:
631       fprintf(stderr, "(%s)\n", err_info);
632       g_free(err_info);
633       break;
634     }
635     return 1;
636   }
637
638   /* File size */
639   size = wtap_file_size(wth, &err);
640   if (size == -1) {
641     fprintf(stderr,
642             "capinfos: Can't get size of \"%s\": %s.\n",
643             filename, strerror(err));
644     return 1;
645   }
646
647   cf_info.filesize = size;
648
649   /* File Type */
650   cf_info.file_type = wtap_file_type(wth);
651
652   /* File Encapsulation */
653   cf_info.file_encap = wtap_file_encap(wth);
654
655   /* Packet size limit (snaplen) */
656   cf_info.snaplen = wtap_snapshot_length(wth);
657   if(cf_info.snaplen > 0)
658     cf_info.snap_set = TRUE;
659   else
660     cf_info.snap_set = FALSE;
661
662   cf_info.snaplen_min_inferred = snaplen_min_inferred;
663   cf_info.snaplen_max_inferred = snaplen_max_inferred;
664
665   /* # of packets */
666   cf_info.packet_count = packet;
667
668   /* File Times */
669   cf_info.start_time = start_time;
670   cf_info.stop_time = stop_time;
671   cf_info.duration = stop_time-start_time;
672   cf_info.in_order = in_order;
673
674   /* Number of packet bytes */
675   cf_info.packet_bytes = bytes;
676
677   cf_info.data_rate   = 0.0;
678   cf_info.packet_rate = 0.0;
679   cf_info.packet_size = 0.0;
680
681   if (packet > 0) {
682     if (cf_info.duration > 0.0) {
683       cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
684       cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
685     }
686     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
687   }
688
689   if(long_report) {
690     print_stats(filename, &cf_info);
691   } else {
692     print_stats_table(filename, &cf_info);
693   }
694
695   return 0;
696 }
697
698 static void
699 usage(gboolean is_error)
700 {
701   FILE *output;
702
703   if (!is_error) {
704     output = stdout;
705     /* XXX - add capinfos header info here */
706   }
707   else {
708     output = stderr;
709   }
710
711   fprintf(output, "Capinfos %s"
712 #ifdef SVNVERSION
713           " (" SVNVERSION " from " SVNPATH ")"
714 #endif
715           "\n", VERSION);
716   fprintf(output, "Prints various information (infos) about capture files.\n");
717   fprintf(output, "See http://www.wireshark.org for more information.\n");
718   fprintf(output, "\n");
719   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
720   fprintf(output, "\n");
721   fprintf(output, "General infos:\n");
722   fprintf(output, "  -t display the capture file type\n");
723   fprintf(output, "  -E display the capture file encapsulation\n");
724 #ifdef HAVE_LIBGCRYPT
725   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
726 #endif
727   fprintf(output, "\n");
728   fprintf(output, "Size infos:\n");
729   fprintf(output, "  -c display the number of packets\n");
730   fprintf(output, "  -s display the size of the file (in bytes)\n");
731   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
732   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
733   fprintf(output, "\n");
734   fprintf(output, "Time infos:\n");
735   fprintf(output, "  -u display the capture duration (in seconds)\n");
736   fprintf(output, "  -a display the capture start time\n");
737   fprintf(output, "  -e display the capture end time\n");
738   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
739   fprintf(output, "  -S display start and end times as seconds\n");
740   fprintf(output, "\n");
741   fprintf(output, "Statistic infos:\n");
742   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
743   fprintf(output, "  -i display average data rate (in bits/sec)\n");
744   fprintf(output, "  -z display average packet size (in bytes)\n");
745   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
746   fprintf(output, "\n");
747   fprintf(output, "Output format:\n");
748   fprintf(output, "  -L generate long report (default)\n");
749   fprintf(output, "  -T generate table report\n");
750   fprintf(output, "\n");
751   fprintf(output, "Table report options:\n");
752   fprintf(output, "  -R generate header record (default)\n");
753   fprintf(output, "  -r do not generate header record\n");
754   fprintf(output, "\n");
755   fprintf(output, "  -B separate infos with TAB character (default)\n");
756   fprintf(output, "  -m separate infos with comma (,) character\n");
757   fprintf(output, "  -b separate infos with SPACE character\n");
758   fprintf(output, "\n");
759   fprintf(output, "  -N do not quote infos (default)\n");
760   fprintf(output, "  -q quote infos with single quotes (')\n");
761   fprintf(output, "  -Q quote infos with double quotes (\")\n");
762   fprintf(output, "\n");
763   fprintf(output, "Miscellaneous:\n");
764   fprintf(output, "  -h display this help and exit\n");
765   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
766   fprintf(output, "  -A generate all infos (default)\n");
767   fprintf(output, "\n");
768   fprintf(output, "Options are processed from left to right order with later options superceeding\n");
769   fprintf(output, "or adding to earlier options.\n");
770   fprintf(output, "\n");
771   fprintf(output, "If no options are given the default is to display all infos in long report\n");
772   fprintf(output, "output format.\n");
773 #ifndef HAVE_LIBGCRYPT
774   fprintf(output, "\nFile hashing support (-H) is not present.\n");
775 #endif
776 }
777
778 #ifdef HAVE_PLUGINS
779 /*
780  *  Don't report failures to load plugins because most (non-wiretap) plugins
781  *  *should* fail to load (because we're not linked against libwireshark and
782  *  dissector plugins need libwireshark).
783  */
784 static void
785 failure_message(const char *msg_format _U_, va_list ap _U_)
786 {
787   return;
788 }
789 #endif
790
791 #ifdef HAVE_LIBGCRYPT
792 static void
793 hash_to_str(const unsigned char *hash, size_t length, char *str) {
794   int i;
795
796   for (i = 0; i < (int) length; i++) {
797     sprintf(str+(i*2), "%02x", hash[i]);
798   }
799 }
800 #endif /* HAVE_LIBGCRYPT */
801
802 int
803 main(int argc, char *argv[])
804 {
805   wtap *wth;
806   int err;
807   gchar *err_info;
808   int opt;
809
810 #ifdef _WIN32
811   LPWSTR              *wc_argv;
812   int                  wc_argc, i;
813 #endif  /* _WIN32 */
814
815   int status = 0;
816 #ifdef HAVE_PLUGINS
817   char* init_progfile_dir_error;
818 #endif
819 #ifdef HAVE_LIBGCRYPT
820   FILE *fh;
821   char *hash_buf = NULL;
822   gcry_md_hd_t hd = NULL;
823   size_t hash_bytes;
824 #endif
825
826 #ifdef _WIN32
827   /* Convert our arg list to UTF-8. */
828   wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc);
829   if (wc_argv && wc_argc == argc) {
830     for (i = 0; i < argc; i++) {
831       argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
832     }
833   } /* XXX else bail because something is horribly, horribly wrong? */
834 #endif /* _WIN32 */
835
836   /*
837    * Get credential information for later use.
838    */
839   init_process_policies();
840
841 #ifdef HAVE_PLUGINS
842   /* Register wiretap plugins */
843
844   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
845     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
846     g_free(init_progfile_dir_error);
847   } else {
848     init_report_err(failure_message,NULL,NULL,NULL);
849     init_plugins();
850   }
851 #endif
852
853   /* Process the options */
854
855   while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxoCALTRrSNqQBmb")) !=-1) {
856
857     switch (opt) {
858
859     case 't':
860       if (report_all_infos) disable_all_infos();
861       cap_file_type = TRUE;
862       break;
863
864     case 'E':
865       if (report_all_infos) disable_all_infos();
866       cap_file_encap = TRUE;
867       break;
868
869     case 'l':
870       if (report_all_infos) disable_all_infos();
871       cap_snaplen = TRUE;
872       break;
873
874     case 'c':
875       if (report_all_infos) disable_all_infos();
876       cap_packet_count = TRUE;
877       break;
878
879     case 's':
880       if (report_all_infos) disable_all_infos();
881       cap_file_size = TRUE;
882       break;
883
884     case 'd':
885       if (report_all_infos) disable_all_infos();
886       cap_data_size = TRUE;
887       break;
888
889     case 'u':
890       if (report_all_infos) disable_all_infos();
891       cap_duration = TRUE;
892       break;
893
894     case 'a':
895       if (report_all_infos) disable_all_infos();
896       cap_start_time = TRUE;
897       break;
898
899     case 'e':
900       if (report_all_infos) disable_all_infos();
901       cap_end_time = TRUE;
902       break;
903
904     case 'S':
905       time_as_secs = TRUE;
906       break;
907
908     case 'y':
909       if (report_all_infos) disable_all_infos();
910       cap_data_rate_byte = TRUE;
911       break;
912
913     case 'i':
914       if (report_all_infos) disable_all_infos();
915       cap_data_rate_bit = TRUE;
916       break;
917
918     case 'z':
919       if (report_all_infos) disable_all_infos();
920       cap_packet_size = TRUE;
921       break;
922
923     case 'x':
924       if (report_all_infos) disable_all_infos();
925       cap_packet_rate = TRUE;
926       break;
927
928 #ifdef HAVE_LIBGCRYPT
929     case 'H':
930       if (report_all_infos) disable_all_infos();
931       cap_file_hashes = TRUE;
932       break;
933 #endif
934
935     case 'o':
936       if (report_all_infos) disable_all_infos();
937       cap_in_order = TRUE;
938       break;
939
940     case 'C':
941       continue_after_wtap_open_offline_failure = FALSE;
942       break;
943
944     case 'A':
945       enable_all_infos();
946       break;
947
948     case 'L':
949       long_report = TRUE;
950       break;
951
952     case 'T':
953       long_report = FALSE;
954       break;
955
956     case 'R':
957       table_report_header = TRUE;
958       break;
959
960     case 'r':
961       table_report_header = FALSE;
962       break;
963
964     case 'N':
965       quote_char = '\0';
966       break;
967
968     case 'q':
969       quote_char = '\'';
970       break;
971
972     case 'Q':
973       quote_char = '"';
974       break;
975
976     case 'B':
977       field_separator = '\t';
978       break;
979
980     case 'm':
981       field_separator = ',';
982       break;
983
984     case 'b':
985       field_separator = ' ';
986       break;
987
988     case 'h':
989       usage(FALSE);
990       exit(1);
991       break;
992
993     case '?':              /* Bad flag - print usage message */
994       usage(TRUE);
995       exit(1);
996       break;
997     }
998   }
999
1000   if ((argc - optind) < 1) {
1001     usage(TRUE);
1002     exit(1);
1003   }
1004
1005   if(!long_report && table_report_header) {
1006     print_stats_table_header();
1007   }
1008
1009 #ifdef HAVE_LIBGCRYPT
1010   if (cap_file_hashes) {
1011     gcry_check_version(NULL);
1012     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1013     if (hd) {
1014       gcry_md_enable(hd, GCRY_MD_RMD160);
1015       gcry_md_enable(hd, GCRY_MD_MD5);
1016     }
1017     hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1018   }
1019 #endif
1020
1021   for (opt = optind; opt < argc; opt++) {
1022
1023 #ifdef HAVE_LIBGCRYPT
1024     strcpy(file_sha1, "<unknown>");
1025     strcpy(file_rmd160, "<unknown>");
1026     strcpy(file_md5, "<unknown>");
1027
1028     if (cap_file_hashes) {
1029       fh = ws_fopen(argv[opt], "rb");
1030       if (fh && hd) {
1031         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1032           gcry_md_write(hd, hash_buf, hash_bytes);
1033         }
1034         gcry_md_final(hd);
1035         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1036         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1037         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1038       }
1039       if (fh) fclose(fh);
1040       if (hd) gcry_md_reset(hd);
1041     }
1042 #endif /* HAVE_LIBGCRYPT */
1043
1044     wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
1045
1046     if (!wth) {
1047       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1048         wtap_strerror(err));
1049       switch (err) {
1050
1051       case WTAP_ERR_UNSUPPORTED:
1052       case WTAP_ERR_UNSUPPORTED_ENCAP:
1053       case WTAP_ERR_BAD_RECORD:
1054         fprintf(stderr, "(%s)\n", err_info);
1055         g_free(err_info);
1056         break;
1057       }
1058       if(!continue_after_wtap_open_offline_failure)
1059         exit(1);
1060     }
1061
1062     if(wth) {
1063       if ((opt > optind) && (long_report))
1064         printf("\n");
1065       status = process_cap_file(wth, argv[opt]);
1066
1067       wtap_close(wth);
1068       if (status)
1069         exit(status);
1070     }
1071   }
1072   return 0;
1073 }
1074