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