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