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