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