Initialize hd pointer to avoid a compiler warning.
[metze/wireshark/wip.git] / capinfos.c
1 /* capinfos.c
2  * Reports capture file information including # of packets, duration, others
3  *
4  * Copyright 2004 Ian Schorr
5  *
6  * $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 #ifdef _WIN32
79 #include <winposixtype.h>
80 #endif
81 #include <gcrypt.h>
82 #include <wsutil/file_util.h>
83 #endif
84
85 #ifdef HAVE_GETOPT_H
86 #include <getopt.h>
87 #else
88 #include "wsgetopt.h"
89 #endif
90
91 /*
92  * By default capinfos now continues processing
93  * the next filename if and when wiretap detects
94  * a problem opening a file.
95  * Use the '-C' option to revert back to original
96  * capinfos behavior which is to abort any
97  * additional file processing at first open file
98  * failure.
99  */
100
101 static gboolean continue_after_wtap_open_offline_failure = TRUE;
102
103 /*
104  * table report variables
105  */
106
107 static gboolean long_report = TRUE;         /* By default generate long report       */
108 static gchar table_report_header = TRUE;    /* Generate column header by default     */
109 static gchar field_separator = '\t';        /* Use TAB as field separator by default */
110 static gchar quote_char = '\0';             /* Do NOT quote fields by default        */
111
112 /*
113  * capinfos has the ability to report on a number of
114  * various characteristics ("infos") for each input file.
115  *
116  * By default reporting of all info fields is enabled.
117  *
118  * Optionally the reporting of any specific info field
119  * or combination of info fields can be enabled with
120  * individual options.
121  */
122
123 static gboolean report_all_infos = TRUE;    /* Report all infos           */
124
125 static gboolean cap_file_type = TRUE;       /* Report capture type        */
126 static gboolean cap_file_encap = TRUE;      /* Report encapsulation       */
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
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 double
254 secs_nsecs(const struct wtap_nstime * nstime)
255 {
256   return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
257 }
258
259 static void print_value(gchar *text_p1, gint width, gchar *text_p2, double value) {
260   if (value > 0.0)
261     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
262   else
263     printf("%sn/a\n", text_p1);
264 }
265
266 static void
267 print_stats(const gchar *filename, capture_info *cf_info)
268 {
269   const gchar           *file_type_string, *file_encap_string;
270   time_t                start_time_t;
271   time_t                stop_time_t;
272
273   /* Build printable strings for various stats */
274   file_type_string = wtap_file_type_string(cf_info->file_type);
275   file_encap_string = wtap_encap_string(cf_info->file_encap);
276   start_time_t = (time_t)cf_info->start_time;
277   stop_time_t = (time_t)cf_info->stop_time;
278
279   if (filename)           printf     ("File name:           %s\n", filename);
280   if (cap_file_type)      printf     ("File type:           %s\n", file_type_string);
281   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
282   if (cap_packet_count)   printf     ("Number of packets:   %u\n", cf_info->packet_count);
283   if (cap_file_size)      printf     ("File size:           %" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
284   if (cap_data_size)      printf     ("Data size:           %" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
285   if (cap_duration)       print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
286   if (cap_start_time)     printf     ("Start time:          %s", (cf_info->packet_count>0) ? ctime (&start_time_t) : "n/a\n");
287   if (cap_end_time)       printf     ("End time:            %s", (cf_info->packet_count>0) ? ctime (&stop_time_t)  : "n/a\n");
288   if (cap_data_rate_byte) print_value("Data byte rate:      ", 2, " bytes/sec",   cf_info->data_rate);
289   if (cap_data_rate_bit)  print_value("Data bit rate:       ", 2, " bits/sec",    cf_info->data_rate*8);
290   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
291   if (cap_packet_rate)    print_value("Average packet rate: ", 2, " packets/sec", cf_info->packet_rate);
292 #ifdef HAVE_LIBGCRYPT
293   if (cap_file_hashes) {
294                           printf     ("SHA1:                %s\n", file_sha1);
295                           printf     ("RIPEMD160:           %s\n", file_rmd160);
296                           printf     ("MD5:                 %s\n", file_md5);
297   }
298 #endif /* HAVE_LIBGCRYPT */
299 }
300
301 static void
302 putsep(void)
303 {
304   if (field_separator) putchar(field_separator);
305 }
306
307 static void
308 putquote(void)
309 {
310   if (quote_char) putchar(quote_char);
311 }
312
313 static void
314 print_stats_table_header_label(gchar *label)
315 {
316   putsep();
317   putquote();
318   printf("%s", label);
319   putquote();
320 }
321
322 static void
323 print_stats_table_header(void)
324 {
325   putquote();
326   printf("File name");
327   putquote();
328
329   if (cap_file_type)      print_stats_table_header_label("File type");
330   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
331   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
332   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
333   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
334   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
335   if (cap_start_time)     print_stats_table_header_label("Start time");
336   if (cap_end_time)       print_stats_table_header_label("End time");
337   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
338   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
339   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
340   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
341 #ifdef HAVE_LIBGCRYPT
342   if (cap_file_hashes) {
343                           print_stats_table_header_label("SHA1");
344                           print_stats_table_header_label("RIPEMD160");
345                           print_stats_table_header_label("MD5");
346   }
347 #endif /* HAVE_LIBGCRYPT */
348
349   printf("\n");
350 }
351
352 static void
353 print_stats_table(const gchar *filename, capture_info *cf_info)
354 {
355   const gchar           *file_type_string, *file_encap_string;
356   time_t                start_time_t;
357   time_t                stop_time_t;
358
359   /* Build printable strings for various stats */
360   file_type_string = wtap_file_type_string(cf_info->file_type);
361   file_encap_string = wtap_encap_string(cf_info->file_encap);
362   start_time_t = (time_t)cf_info->start_time;
363   stop_time_t = (time_t)cf_info->stop_time;
364
365   if (filename) {
366     putquote();
367     printf("%s", filename);
368     putquote();
369   }
370
371   if (cap_file_type) {
372     putsep();
373     putquote();
374     printf("%s", file_type_string);
375     putquote();
376   }
377
378   if (cap_file_encap) {
379     putsep();
380     putquote();
381     printf("%s", file_encap_string);
382     putquote();
383   }
384
385   if (cap_packet_count) {
386     putsep();
387     putquote();
388     printf("%u", cf_info->packet_count);
389     putquote();
390   }
391
392   if (cap_file_size) {
393     putsep();
394     putquote();
395     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
396     putquote();
397   }
398
399   if (cap_data_size) {
400     putsep();
401     putquote();
402     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
403     putquote();
404   }
405
406   if (cap_duration) {
407     putsep();
408     putquote();
409     printf("%f", cf_info->duration);
410     putquote();
411   }
412
413   if (cap_start_time) {
414     putsep();
415     putquote();
416     printf("%s", (cf_info->packet_count>0) ? ctime_no_lf (&start_time_t) : "n/a");
417     putquote();
418   }
419
420   if (cap_end_time) {
421     putsep();
422     putquote();
423     printf("%s", (cf_info->packet_count>0) ? ctime_no_lf (&stop_time_t) : "n/a");
424     putquote();
425   }
426
427   if (cap_data_rate_byte) {
428     putsep();
429     putquote();
430     printf("%.2f", cf_info->data_rate);
431     putquote();
432   }
433
434   if (cap_data_rate_bit) {
435     putsep();
436     putquote();
437     printf("%.2f", cf_info->data_rate*8);
438     putquote();
439   }
440
441   if (cap_packet_size) {
442     putsep();
443     putquote();
444     printf("%.2f", cf_info->packet_size);
445     putquote();
446   }
447
448   if (cap_packet_rate) {
449     putsep();
450     putquote();
451     printf("%.2f", cf_info->packet_rate);
452     putquote();
453   }
454
455 #ifdef HAVE_LIBGCRYPT
456   if (cap_file_hashes) {
457     putsep();
458     putquote();
459     printf("%s", file_sha1);
460     putquote();
461
462     putsep();
463     putquote();
464     printf("%s", file_rmd160);
465     putquote();
466
467     putsep();
468     putquote();
469     printf("%s", file_md5);
470     putquote();
471   }
472 #endif /* HAVE_LIBGCRYPT */
473
474   printf("\n");
475 }
476
477 static int
478 process_cap_file(wtap *wth, const char *filename)
479 {
480   int                   err;
481   gchar                 *err_info;
482   gint64                size;
483   gint64                data_offset;
484
485   guint32               packet = 0;
486   gint64                bytes = 0;
487   const struct wtap_pkthdr *phdr;
488   capture_info          cf_info;
489   double                start_time = 0;
490   double                stop_time = 0;
491   double                cur_time = 0;
492
493   /* Tally up data that we need to parse through the file to find */
494   while (wtap_read(wth, &err, &err_info, &data_offset))  {
495     phdr = wtap_phdr(wth);
496     cur_time = secs_nsecs(&phdr->ts);
497     if(packet==0) {
498       start_time = cur_time;
499       stop_time = cur_time;
500     }
501     if (cur_time < start_time) {
502       start_time = cur_time;
503     }
504     if (cur_time > stop_time) {
505       stop_time = cur_time;
506     }
507     bytes+=phdr->len;
508     packet++;
509   }
510
511   if (err != 0) {
512     fprintf(stderr,
513             "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
514             packet, filename, wtap_strerror(err));
515     switch (err) {
516
517     case WTAP_ERR_UNSUPPORTED:
518     case WTAP_ERR_UNSUPPORTED_ENCAP:
519     case WTAP_ERR_BAD_RECORD:
520       fprintf(stderr, "(%s)\n", err_info);
521       g_free(err_info);
522       break;
523     }
524     return 1;
525   }
526
527   /* File size */
528   size = wtap_file_size(wth, &err);
529   if (size == -1) {
530     fprintf(stderr,
531             "capinfos: Can't get size of \"%s\": %s.\n",
532             filename, strerror(err));
533     return 1;
534   }
535
536   cf_info.filesize = size;
537
538   /* File Type */
539   cf_info.file_type = wtap_file_type(wth);
540
541   /* File Encapsulation */
542   cf_info.file_encap = wtap_file_encap(wth);
543
544   /* # of packets */
545   cf_info.packet_count = packet;
546
547   /* File Times */
548   cf_info.start_time = start_time;
549   cf_info.stop_time = stop_time;
550   cf_info.duration = stop_time-start_time;
551
552   /* Number of packet bytes */
553   cf_info.packet_bytes = bytes;
554
555   cf_info.data_rate   = 0.0;
556   cf_info.packet_rate = 0.0;
557   cf_info.packet_size = 0.0;
558
559   if (packet > 0) {
560     if (cf_info.duration > 0.0) {
561       cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
562       cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
563     }
564     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
565   }
566
567   if(long_report) {
568     print_stats(filename, &cf_info);
569   } else {
570     print_stats_table(filename, &cf_info);
571   }
572
573   return 0;
574 }
575
576 static void
577 usage(gboolean is_error)
578 {
579   FILE *output;
580
581   if (!is_error) {
582     output = stdout;
583     /* XXX - add capinfos header info here */
584   }
585   else {
586     output = stderr;
587   }
588
589   fprintf(output, "Capinfos %s"
590 #ifdef SVNVERSION
591           " (" SVNVERSION " from " SVNPATH ")"
592 #endif
593           "\n", VERSION);
594   fprintf(output, "Prints various information (infos) about capture files.\n");
595   fprintf(output, "See http://www.wireshark.org for more information.\n");
596   fprintf(output, "\n");
597   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
598   fprintf(output, "\n");
599   fprintf(output, "General infos:\n");
600   fprintf(output, "  -t display the capture file type\n");
601   fprintf(output, "  -E display the capture file encapsulation\n");
602 #ifdef HAVE_LIBGCRYPT
603   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
604 #endif
605   fprintf(output, "\n");
606   fprintf(output, "Size infos:\n");
607   fprintf(output, "  -c display the number of packets\n");
608   fprintf(output, "  -s display the size of the file (in bytes)\n");
609   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
610   fprintf(output, "\n");
611   fprintf(output, "Time infos:\n");
612   fprintf(output, "  -u display the capture duration (in seconds)\n");
613   fprintf(output, "  -a display the capture start time\n");
614   fprintf(output, "  -e display the capture end time\n");
615   fprintf(output, "\n");
616   fprintf(output, "Statistic infos:\n");
617   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
618   fprintf(output, "  -i display average data rate (in bits/sec)\n");
619   fprintf(output, "  -z display average packet size (in bytes)\n");
620   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
621   fprintf(output, "\n");
622   fprintf(output, "Output format:\n");
623   fprintf(output, "  -L generate long report (default)\n");
624   fprintf(output, "  -T generate table report\n");
625   fprintf(output, "\n");
626   fprintf(output, "Table report options:\n");
627   fprintf(output, "  -R generate header record (default)\n");
628   fprintf(output, "  -r do not generate header record\n");
629   fprintf(output, "\n");
630   fprintf(output, "  -B separate infos with TAB character (default)\n");
631   fprintf(output, "  -m separate infos with comma (,) character\n");
632   fprintf(output, "  -b separate infos with SPACE character\n");
633   fprintf(output, "\n");
634   fprintf(output, "  -N do not quote infos (default)\n");
635   fprintf(output, "  -q quote infos with single quotes (')\n");
636   fprintf(output, "  -Q quote infos with double quotes (\")\n");
637   fprintf(output, "\n");
638   fprintf(output, "Miscellaneous:\n");
639   fprintf(output, "  -h display this help and exit\n");
640   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
641   fprintf(output, "  -A generate all infos (default)\n");
642   fprintf(output, "\n");
643   fprintf(output, "Options are processed from left to right order with later options superceeding\n");
644   fprintf(output, "or adding to earlier options.\n");
645   fprintf(output, "\n");
646   fprintf(output, "If no options are given the default is to display all infos in long report\n");
647   fprintf(output, "output format.\n");
648 #ifndef HAVE_LIBGCRYPT
649   fprintf(output, "\nFile hashing support (-H) is not present.\n");
650 #endif
651 }
652
653 #ifdef HAVE_PLUGINS
654 /*
655  *  Don't report failures to load plugins because most (non-wiretap) plugins
656  *  *should* fail to load (because we're not linked against libwireshark and
657  *  dissector plugins need libwireshark).
658  */
659 static void
660 failure_message(const char *msg_format _U_, va_list ap _U_)
661 {
662         return;
663 }
664 #endif
665
666 #ifdef HAVE_LIBGCRYPT
667 static void
668 hash_to_str(const unsigned char *hash, size_t length, char *str) {
669   int i;
670
671   for (i = 0; i < (int) length; i++) {
672     sprintf(str+(i*2), "%02x", hash[i]);
673   }
674 }
675 #endif /* HAVE_LIBGCRYPT */
676
677 int
678 main(int argc, char *argv[])
679 {
680   wtap *wth;
681   int err;
682   gchar *err_info;
683   int opt;
684   int status = 0;
685 #ifdef HAVE_PLUGINS
686   char* init_progfile_dir_error;
687 #endif
688 #ifdef HAVE_LIBGCRYPT
689   FILE *fh;
690   char *hash_buf = NULL;
691   gcry_md_hd_t hd = NULL;
692   size_t hash_bytes;
693 #endif
694
695   /*
696    * Get credential information for later use.
697    */
698   get_credential_info();
699
700 #ifdef HAVE_PLUGINS
701   /* Register wiretap plugins */
702
703     if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
704                 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
705                 g_free(init_progfile_dir_error);
706     } else {
707                 init_report_err(failure_message,NULL,NULL,NULL);
708                 init_plugins();
709     }
710 #endif
711
712   /* Process the options */
713
714   while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "duaeyizvhxCALTRrNqQBmb")) !=-1) {
715
716     switch (opt) {
717
718     case 't':
719       if (report_all_infos) disable_all_infos();
720       cap_file_type = TRUE;
721       break;
722
723     case 'E':
724       if (report_all_infos) disable_all_infos();
725       cap_file_encap = TRUE;
726       break;
727
728     case 'c':
729       if (report_all_infos) disable_all_infos();
730       cap_packet_count = TRUE;
731       break;
732
733     case 's':
734       if (report_all_infos) disable_all_infos();
735       cap_file_size = TRUE;
736       break;
737
738     case 'd':
739       if (report_all_infos) disable_all_infos();
740       cap_data_size = TRUE;
741       break;
742
743     case 'u':
744       if (report_all_infos) disable_all_infos();
745       cap_duration = TRUE;
746       break;
747
748     case 'a':
749       if (report_all_infos) disable_all_infos();
750       cap_start_time = TRUE;
751       break;
752
753     case 'e':
754       if (report_all_infos) disable_all_infos();
755       cap_end_time = TRUE;
756       break;
757
758     case 'y':
759       if (report_all_infos) disable_all_infos();
760       cap_data_rate_byte = TRUE;
761       break;
762
763     case 'i':
764       if (report_all_infos) disable_all_infos();
765       cap_data_rate_bit = TRUE;
766       break;
767
768     case 'z':
769       if (report_all_infos) disable_all_infos();
770       cap_packet_size = TRUE;
771       break;
772
773     case 'x':
774       if (report_all_infos) disable_all_infos();
775       cap_packet_rate = TRUE;
776       break;
777
778 #ifdef HAVE_LIBGCRYPT
779     case 'H':
780       if (report_all_infos) disable_all_infos();
781       cap_file_hashes = TRUE;
782       break;
783 #endif
784
785     case 'C':
786       continue_after_wtap_open_offline_failure = FALSE;
787       break;
788
789     case 'A':
790       enable_all_infos();
791       break;
792
793     case 'L':
794       long_report = TRUE;
795       break;
796
797     case 'T':
798       long_report = FALSE;
799       break;
800
801     case 'R':
802       table_report_header = TRUE;
803       break;
804
805     case 'r':
806       table_report_header = FALSE;
807       break;
808
809     case 'N':
810       quote_char = '\0';
811       break;
812
813     case 'q':
814       quote_char = '\'';
815       break;
816
817     case 'Q':
818       quote_char = '"';
819       break;
820
821     case 'B':
822       field_separator = '\t';
823       break;
824
825     case 'm':
826       field_separator = ',';
827       break;
828
829     case 'b':
830       field_separator = ' ';
831       break;
832
833     case 'h':
834       usage(FALSE);
835       exit(1);
836       break;
837
838     case '?':              /* Bad flag - print usage message */
839       usage(TRUE);
840       exit(1);
841       break;
842     }
843   }
844
845   if ((argc - optind) < 1) {
846     usage(TRUE);
847     exit(1);
848   }
849
850   if(!long_report && table_report_header) {
851     print_stats_table_header();
852   }
853
854 #ifdef HAVE_LIBGCRYPT
855   if (cap_file_hashes) {
856     gcry_check_version(NULL);
857     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
858     if (hd) {
859       gcry_md_enable(hd, GCRY_MD_RMD160);
860       gcry_md_enable(hd, GCRY_MD_MD5);
861     }
862     hash_buf = g_malloc(HASH_BUF_SIZE);
863   }
864 #endif
865
866   for (opt = optind; opt < argc; opt++) {
867
868 #ifdef HAVE_LIBGCRYPT
869     strcpy(file_sha1, "<unknown>");
870     strcpy(file_rmd160, "<unknown>");
871     strcpy(file_md5, "<unknown>");
872
873     if (cap_file_hashes) {
874       fh = ws_fopen(argv[opt], "rb");
875       if (fh && hd) {
876         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
877           gcry_md_write(hd, hash_buf, hash_bytes);
878         }
879         gcry_md_final(hd);
880         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
881         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
882         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
883       }
884       fclose(fh);
885       gcry_md_reset(hd);
886     }
887 #endif /* HAVE_LIBGCRYPT */
888
889     wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
890
891     if (!wth) {
892       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
893         wtap_strerror(err));
894       switch (err) {
895
896       case WTAP_ERR_UNSUPPORTED:
897       case WTAP_ERR_UNSUPPORTED_ENCAP:
898       case WTAP_ERR_BAD_RECORD:
899         fprintf(stderr, "(%s)\n", err_info);
900         g_free(err_info);
901         break;
902       }
903       if(!continue_after_wtap_open_offline_failure)
904         exit(1);
905     }
906
907     if(wth) {
908       if ((opt > optind) && (long_report))
909         printf("\n");
910       status = process_cap_file(wth, argv[opt]);
911
912       wtap_close(wth);
913       if (status)
914         exit(status);
915     }
916   }
917   return 0;
918 }
919