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