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