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