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