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