From William Sims via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8755
[metze/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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include "config.h"
60
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <stdarg.h>
65 #include <locale.h>
66 #include <errno.h>
67
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifdef HAVE_SYS_TIME_H
73 #include <sys/time.h>
74 #endif
75
76 #include <glib.h>
77
78 #include <wsutil/privileges.h>
79
80 /*
81  * The symbols declared in the below are exported from libwireshark,
82  * but we don't want to link whole libwireshark to editcap.
83  * We link the object directly instead and this needs a little trick
84  * with the WS_BUILD_DLL #define.
85  */
86 #define WS_BUILD_DLL
87 #define RESET_SYMBOL_EXPORT
88 #include <epan/packet.h>
89 #include <epan/filesystem.h>
90 #include <epan/plugins.h>
91 #include <epan/report_err.h>
92 #undef WS_BUILD_DLL
93 #define RESET_SYMBOL_EXPORT
94
95 #include "wtap.h"
96 #include <wsutil/privileges.h>
97 #include <wsutil/str_util.h>
98
99 #ifdef HAVE_LIBGCRYPT
100 #include <wsutil/wsgcrypt.h>
101 #include <wsutil/file_util.h>
102 #endif
103
104 #ifndef HAVE_GETOPT
105 #include "wsutil/wsgetopt.h"
106 #endif
107
108 #ifdef _WIN32
109 #include <wsutil/unicode-utils.h>
110 #endif /* _WIN32 */
111
112 #include "svnversion.h"
113
114 /*
115  * By default capinfos now continues processing
116  * the next filename if and when wiretap detects
117  * a problem opening a file.
118  * Use the '-C' option to revert back to original
119  * capinfos behavior which is to abort any
120  * additional file processing at first open file
121  * failure.
122  */
123
124 static gboolean continue_after_wtap_open_offline_failure = TRUE;
125
126 /*
127  * table report variables
128  */
129
130 static gboolean long_report = TRUE;         /* By default generate long report       */
131 static gchar table_report_header = TRUE;    /* Generate column header by default     */
132 static gchar field_separator = '\t';        /* Use TAB as field separator by default */
133 static gchar quote_char = '\0';             /* Do NOT quote fields by default        */
134 static gboolean machine_readable = FALSE;   /* Display machine-readable numbers      */
135
136 /*
137  * capinfos has the ability to report on a number of
138  * various characteristics ("infos") for each input file.
139  *
140  * By default reporting of all info fields is enabled.
141  *
142  * Optionally the reporting of any specific info field
143  * or combination of info fields can be enabled with
144  * individual options.
145  */
146
147 static gboolean report_all_infos = TRUE;    /* Report all infos           */
148
149 static gboolean cap_file_type = TRUE;       /* Report capture type        */
150 static gboolean cap_file_encap = TRUE;      /* Report encapsulation       */
151 static gboolean cap_snaplen = TRUE;         /* Packet size limit (snaplen)*/
152 static gboolean cap_packet_count = TRUE;    /* Report packet count        */
153 static gboolean cap_file_size = TRUE;       /* Report file size           */
154 static gboolean cap_comment = TRUE;         /* Display the capture comment */
155
156 static gboolean cap_data_size = TRUE;       /* Report packet byte size    */
157 static gboolean cap_duration = TRUE;        /* Report capture duration    */
158 static gboolean cap_start_time = TRUE;      /* Report capture start time  */
159 static gboolean cap_end_time = TRUE;        /* Report capture end time    */
160 static gboolean time_as_secs = FALSE;       /* Report time values as raw seconds */
161
162 static gboolean cap_data_rate_byte = TRUE;  /* Report data rate bytes/sec */
163 static gboolean cap_data_rate_bit = TRUE;   /* Report data rate bites/sec */
164 static gboolean cap_packet_size = TRUE;     /* Report average packet size */
165 static gboolean cap_packet_rate = TRUE;     /* Report average packet rate */
166 static gboolean cap_order = TRUE;           /* Report if packets are in chronological order (True/False) */
167
168 #ifdef HAVE_LIBGCRYPT
169 static gboolean cap_file_hashes = TRUE;     /* Calculate file hashes */
170 #endif
171
172 #ifdef USE_GOPTION
173 static gboolean cap_help = FALSE;
174 static gboolean table_report = FALSE;
175
176 static GOptionEntry general_entries[] =
177 {
178   /* General */
179   { "type", 't', 0, G_OPTION_ARG_NONE, &cap_file_type,
180     "display the capture file type", NULL },
181   { "Encapsulation", 'E', 0, G_OPTION_ARG_NONE, &cap_file_encap,
182     "display the capture file encapsulation", NULL },
183 #ifdef HAVE_LIBGCRYPT
184   { "Hash", 'H', 0, G_OPTION_ARG_NONE, &cap_file_hashes,
185     "display the SHA1, RMD160, and MD5 hashes of the file", NULL },
186 #endif /* HAVE_LIBGCRYPT */
187   { "capture-comment", 'k', 0, G_OPTION_ARG_NONE, &cap_comment,
188     "display the capture comment ", NULL },
189   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
190 };
191 static GOptionEntry size_entries[] =
192 {
193   /* Size */
194   { "packets", 'c', 0, G_OPTION_ARG_NONE, &cap_packet_count,
195     "display the number of packets", NULL },
196   { "size", 's', 0, G_OPTION_ARG_NONE, &cap_file_size,
197     "display the size of the file (in bytes)", NULL },
198   { "tot-len-of-pkts", 'd', 0, G_OPTION_ARG_NONE, &cap_data_size,
199     "display the total length of all packets (in bytes)", NULL },
200   { "snap", 'l', 0, G_OPTION_ARG_NONE, &cap_snaplen,
201     "display the packet size limit (snapshot length)", NULL },
202   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
203 };
204 static GOptionEntry time_entries[] =
205 {
206   /* Time */
207   { "duration", 'u', 0, G_OPTION_ARG_NONE, &cap_duration,
208     "display the capture duration (in seconds)", NULL },
209   { "start", 'a', 0, G_OPTION_ARG_NONE, &cap_start_time,
210     "display the capture start time", NULL },
211   { "end", 'e', 0, G_OPTION_ARG_NONE, &cap_end_time,
212     "display the capture end time", NULL },
213   { "cron", 'o', 0, G_OPTION_ARG_NONE, &cap_order,
214     "display the capture file chronological status (True/False)", NULL },
215   { "start-end-time-sec", 'S', 0, G_OPTION_ARG_NONE, &time_as_secs,
216     "display start and end times as seconds", NULL },
217   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
218 };
219
220 static GOptionEntry stats_entries[] =
221 {
222   /* Statistics */
223   { "bytes", 'y', 0, G_OPTION_ARG_NONE, &cap_data_rate_byte,
224     "display average data rate (in bytes/s)", NULL },
225   { "bits", 'i', 0, G_OPTION_ARG_NONE, &cap_data_rate_bit,
226     "display average data rate (in bits/s)", NULL },
227   { "packet-bytes", 'z', 0, G_OPTION_ARG_NONE, &cap_packet_size,
228     "display average packet size (in bytes)", NULL },
229   { "packets", 'x', 0, G_OPTION_ARG_NONE, &cap_packet_rate,
230     "display average packet rate (in packets/s)", NULL },
231   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
232 };
233
234 static GOptionEntry output_format_entries[] =
235 {
236   /* Output format */
237   { "long", 'L', 0, G_OPTION_ARG_NONE, &long_report,
238     "generate long report (default)", NULL },
239   { "Table", 'T', 0, G_OPTION_ARG_NONE, &table_report,
240     "generate table report", NULL },
241   { "machine-readable", 'M', 0, G_OPTION_ARG_NONE, &machine_readable,
242     "display machine-readable (unabbreviated) values in long reports", NULL },
243   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
244 };
245
246 static GOptionEntry table_report_entries[] =
247 {
248   /* Table report */
249   { "header-rec", 'R', 0, G_OPTION_ARG_NONE, &table_report_header,
250     "generate header record (default)", NULL },
251   { "no-table", 'r', 0, G_OPTION_ARG_NONE, &table_report_header,
252     "do not generate header record", NULL },
253   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
254 };
255
256 static GOptionEntry misc_entries[] =
257 {
258   { "helpcompat", 'h', 0, G_OPTION_ARG_NONE, &cap_help,
259     "display help", NULL },
260   { NULL,'\0',0,G_OPTION_ARG_NONE,NULL,NULL,NULL }
261 };
262
263 GOptionContext *ctx;
264 GOptionGroup *general_grp, *size_grp, *time_grp, *stats_grp, *output_grp, *table_report_grp;
265 GError *parse_err = NULL;
266
267 #endif /* USE_GOPTION */
268
269 #ifdef HAVE_LIBGCRYPT
270 #define HASH_SIZE_SHA1 20
271 #define HASH_SIZE_RMD160 20
272 #define HASH_SIZE_MD5 16
273
274 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
275 #define HASH_BUF_SIZE (1024 * 1024)
276
277
278 static gchar file_sha1[HASH_STR_SIZE];
279 static gchar file_rmd160[HASH_STR_SIZE];
280 static gchar file_md5[HASH_STR_SIZE];
281
282 #define FILE_HASH_OPT "H"
283 #else
284 #define FILE_HASH_OPT ""
285 #endif /* HAVE_LIBGCRYPT */
286
287 /*
288  * If we have at least two packets with time stamps, and they're not in
289  * order - i.e., the later packet has a time stamp older than the earlier
290  * packet - the time stamps are known not to be in order.
291  *
292  * If every packet has a time stamp, and they're all in order, the time
293  * stamp is known to be in order.
294  *
295  * Otherwise, we have no idea.
296  */
297 typedef enum {
298   IN_ORDER,
299   NOT_IN_ORDER,
300   ORDER_UNKNOWN
301 } order_t;
302
303 typedef struct _capture_info {
304   const char    *filename;
305   guint16       file_type;
306   gboolean      iscompressed;
307   int           file_encap;
308   gint64        filesize;
309   gchar         *comment;
310
311   guint64       packet_bytes;
312   gboolean      times_known;
313   double        start_time;
314   double        stop_time;
315   guint32       packet_count;
316   gboolean      snap_set;                /* If set in capture file header      */
317   guint32       snaplen;                 /* value from the capture file header */
318   guint32       snaplen_min_inferred;    /* If caplen < len for 1 or more rcds */
319   guint32       snaplen_max_inferred;    /*  ...                               */
320   gboolean      drops_known;
321   guint32       drop_count;
322
323   double        duration;
324   double        packet_rate;
325   double        packet_size;
326   double        data_rate;              /* in bytes */
327   gboolean      know_order;
328   order_t       order;
329
330   int          *encap_counts;           /* array of per_packet encap counts; array has one entry per wtap_encap type */
331 } capture_info;
332
333
334 static void
335 enable_all_infos(void)
336 {
337   report_all_infos = TRUE;
338
339   cap_file_type = TRUE;
340   cap_file_encap = TRUE;
341   cap_snaplen = TRUE;
342   cap_packet_count = TRUE;
343   cap_file_size = TRUE;
344   cap_comment = TRUE;
345
346   cap_data_size = TRUE;
347   cap_duration = TRUE;
348   cap_start_time = TRUE;
349   cap_end_time = TRUE;
350   cap_order = TRUE;
351
352   cap_data_rate_byte = TRUE;
353   cap_data_rate_bit = TRUE;
354   cap_packet_size = TRUE;
355   cap_packet_rate = TRUE;
356
357 #ifdef HAVE_LIBGCRYPT
358   cap_file_hashes = TRUE;
359 #endif /* HAVE_LIBGCRYPT */
360 }
361
362 static void
363 disable_all_infos(void)
364 {
365   report_all_infos   = FALSE;
366
367   cap_file_type      = FALSE;
368   cap_file_encap     = FALSE;
369   cap_snaplen        = FALSE;
370   cap_packet_count   = FALSE;
371   cap_file_size      = FALSE;
372   cap_comment        = FALSE;
373
374   cap_data_size      = FALSE;
375   cap_duration       = FALSE;
376   cap_start_time     = FALSE;
377   cap_end_time       = FALSE;
378   cap_order          = FALSE;
379
380   cap_data_rate_byte = FALSE;
381   cap_data_rate_bit  = FALSE;
382   cap_packet_size    = FALSE;
383   cap_packet_rate    = FALSE;
384
385 #ifdef HAVE_LIBGCRYPT
386   cap_file_hashes = FALSE;
387 #endif /* HAVE_LIBGCRYPT */
388 }
389
390 static const gchar *
391 order_string(order_t order)
392 {
393   switch (order) {
394
395     case IN_ORDER:
396       return "True";
397
398     case NOT_IN_ORDER:
399       return "False";
400
401     case ORDER_UNKNOWN:
402       return "Unknown";
403
404     default:
405       return "???";  /* "cannot happen" (the next step is "Profit!") */
406   }
407 }
408
409 static gchar *
410 time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
411 {
412   const gchar *lf = want_lf ? "\n" : "";
413   static gchar time_string_buf[20];
414   char *time_string_ctime;
415
416   if (cf_info->times_known && cf_info->packet_count > 0) {
417     if (time_as_secs) {
418       /* XXX - Would it be useful to show sub-second precision? */
419       g_snprintf(time_string_buf, 20, "%lu%s", (unsigned long)timer, lf);
420       return time_string_buf;
421     } else {
422 #if (defined _WIN32) && (_MSC_VER < 1500)
423       /* calling localtime(), and thus ctime(), on MSVC 2005 with huge values causes it to crash */
424       /* XXX - find the exact value that still does work */
425       /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
426       if (timer > 2000000000) {
427         time_string_ctime = NULL;
428       } else
429 #endif
430         time_string_ctime = ctime(&timer);
431       if (time_string_ctime == NULL) {
432         g_snprintf(time_string_buf, 20, "Not representable%s", lf);
433         return time_string_buf;
434       }
435       if (!want_lf) {
436         /*
437          * The ctime() function returns a string formatted as:
438          *   "Www Mmm dd hh:mm:ss yyyy\n"
439          * The unwanted '\n' is the 24th character.
440          */
441         time_string_ctime[24] = '\0';
442       }
443       return time_string_ctime;
444     }
445   }
446
447   g_snprintf(time_string_buf, 15, "n/a%s", lf);
448   return time_string_buf;
449 }
450
451 static double
452 secs_nsecs(const struct wtap_nstime * nstime)
453 {
454   return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
455 }
456
457 static void print_value(const gchar *text_p1, gint width, const gchar *text_p2, double value) {
458   if (value > 0.0)
459     printf("%s%.*f%s\n", text_p1, width, value, text_p2);
460   else
461     printf("%sn/a\n", text_p1);
462 }
463
464 static void
465 print_stats(const gchar *filename, capture_info *cf_info)
466 {
467   const gchar           *file_type_string, *file_encap_string;
468   time_t                start_time_t;
469   time_t                stop_time_t;
470   gchar                 *size_string;
471
472   /* Build printable strings for various stats */
473   file_type_string = wtap_file_type_string(cf_info->file_type);
474   file_encap_string = wtap_encap_string(cf_info->file_encap);
475   start_time_t = (time_t)cf_info->start_time;
476   stop_time_t = (time_t)cf_info->stop_time;
477
478   if (filename)           printf     ("File name:           %s\n", filename);
479   if (cap_file_type)      printf     ("File type:           %s%s\n",
480       file_type_string,
481       cf_info->iscompressed ? " (gzip compressed)" : "");
482   if (cap_file_encap)     printf     ("File encapsulation:  %s\n", file_encap_string);
483   if (cap_file_encap && (cf_info->file_encap == WTAP_ENCAP_PER_PACKET)) {
484     int i;
485     for (i=0; i<WTAP_NUM_ENCAP_TYPES; i++) {
486       if (cf_info->encap_counts[i] > 0)
487         printf("                       %s\n", wtap_encap_string(i));
488     }
489   }
490   if (cap_snaplen && cf_info->snap_set)
491     printf     ("Packet size limit:   file hdr: %u bytes\n", cf_info->snaplen);
492   else if(cap_snaplen && !cf_info->snap_set)
493     printf     ("Packet size limit:   file hdr: (not set)\n");
494   if (cf_info->snaplen_max_inferred > 0) {
495     if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
496       printf     ("Packet size limit:   inferred: %u bytes\n", cf_info->snaplen_min_inferred);
497     else
498       printf     ("Packet size limit:   inferred: %u bytes - %u bytes (range)\n",
499           cf_info->snaplen_min_inferred, cf_info->snaplen_max_inferred);
500   }
501   if (cap_packet_count) {
502     printf     ("Number of packets:   ");
503     if (machine_readable) {
504       printf ("%u\n", cf_info->packet_count);
505     } else {
506       size_string = format_size(cf_info->packet_count, format_size_unit_none);
507       printf ("%s\n", size_string);
508       g_free(size_string);
509     }
510   }
511   if (cap_file_size) {
512     printf     ("File size:           ");
513     if (machine_readable) {
514       printf     ("%" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
515     } else {
516       size_string = format_size(cf_info->filesize, format_size_unit_bytes);
517       printf ("%s\n", size_string);
518       g_free(size_string);
519     }
520   }
521   if (cap_data_size) {
522     printf     ("Data size:           ");
523     if (machine_readable) {
524       printf     ("%" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
525     } else {
526       size_string = format_size(cf_info->packet_bytes, format_size_unit_bytes);
527       printf ("%s\n", size_string);
528       g_free(size_string);
529     }
530   }
531   if (cf_info->times_known) {
532     if (cap_duration) /* XXX - shorten to hh:mm:ss */
533                           print_value("Capture duration:    ", 0, " seconds",   cf_info->duration);
534     if (cap_start_time)
535                           printf     ("Start time:          %s", time_string(start_time_t, cf_info, TRUE));
536     if (cap_end_time)
537                           printf     ("End time:            %s", time_string(stop_time_t, cf_info, TRUE));
538     if (cap_data_rate_byte) {
539                           printf     ("Data byte rate:      ");
540       if (machine_readable) {
541         print_value("", 2, " bytes/sec",   cf_info->data_rate);
542       } else {
543         size_string = format_size((gint64)cf_info->data_rate, format_size_unit_bytes_s);
544         printf ("%s\n", size_string);
545         g_free(size_string);
546       }
547     }
548     if (cap_data_rate_bit) {
549                           printf     ("Data bit rate:       ");
550       if (machine_readable) {
551         print_value("", 2, " bits/sec",    cf_info->data_rate*8);
552       } else {
553         size_string = format_size((gint64)(cf_info->data_rate*8), format_size_unit_bits_s);
554         printf ("%s\n", size_string);
555         g_free(size_string);
556       }
557     }
558   }
559   if (cap_packet_size)    printf     ("Average packet size: %.2f bytes\n",        cf_info->packet_size);
560   if (cf_info->times_known) {
561     if (cap_packet_rate) {
562                           printf     ("Average packet rate: ");
563       if (machine_readable) {
564         print_value("", 2, " packets/sec", cf_info->packet_rate);
565       } else {
566         size_string = format_size((gint64)cf_info->packet_rate, format_size_unit_none);
567         printf ("%spackets/sec\n", size_string);
568         g_free(size_string);
569       }
570     }
571   }
572 #ifdef HAVE_LIBGCRYPT
573   if (cap_file_hashes) {
574     printf     ("SHA1:                %s\n", file_sha1);
575     printf     ("RIPEMD160:           %s\n", file_rmd160);
576     printf     ("MD5:                 %s\n", file_md5);
577   }
578 #endif /* HAVE_LIBGCRYPT */
579   if (cap_order)          printf     ("Strict time order:   %s\n", order_string(cf_info->order));
580   if (cap_comment && cf_info->comment)
581     printf     ("Capture comment:     %s\n", cf_info->comment);
582 }
583
584 static void
585 putsep(void)
586 {
587   if (field_separator) putchar(field_separator);
588 }
589
590 static void
591 putquote(void)
592 {
593   if (quote_char) putchar(quote_char);
594 }
595
596 static void
597 print_stats_table_header_label(const gchar *label)
598 {
599   putsep();
600   putquote();
601   printf("%s", label);
602   putquote();
603 }
604
605 static void
606 print_stats_table_header(void)
607 {
608   putquote();
609   printf("File name");
610   putquote();
611
612   if (cap_file_type)      print_stats_table_header_label("File type");
613   if (cap_file_encap)     print_stats_table_header_label("File encapsulation");
614   if (cap_snaplen)        print_stats_table_header_label("Packet size limit");
615   if (cap_snaplen)        print_stats_table_header_label("Packet size limit min (inferred)");
616   if (cap_snaplen)        print_stats_table_header_label("Packet size limit max (inferred)");
617   if (cap_packet_count)   print_stats_table_header_label("Number of packets");
618   if (cap_file_size)      print_stats_table_header_label("File size (bytes)");
619   if (cap_data_size)      print_stats_table_header_label("Data size (bytes)");
620   if (cap_duration)       print_stats_table_header_label("Capture duration (seconds)");
621   if (cap_start_time)     print_stats_table_header_label("Start time");
622   if (cap_end_time)       print_stats_table_header_label("End time");
623   if (cap_data_rate_byte) print_stats_table_header_label("Data byte rate (bytes/sec)");
624   if (cap_data_rate_bit)  print_stats_table_header_label("Data bit rate (bits/sec)");
625   if (cap_packet_size)    print_stats_table_header_label("Average packet size (bytes)");
626   if (cap_packet_rate)    print_stats_table_header_label("Average packet rate (packets/sec)");
627 #ifdef HAVE_LIBGCRYPT
628   if (cap_file_hashes) {
629     print_stats_table_header_label("SHA1");
630     print_stats_table_header_label("RIPEMD160");
631     print_stats_table_header_label("MD5");
632   }
633 #endif /* HAVE_LIBGCRYPT */
634   if (cap_order)          print_stats_table_header_label("Strict time order");
635   if (cap_comment)        print_stats_table_header_label("Capture comment");
636
637   printf("\n");
638 }
639
640 static void
641 print_stats_table(const gchar *filename, capture_info *cf_info)
642 {
643   const gchar           *file_type_string, *file_encap_string;
644   time_t                start_time_t;
645   time_t                stop_time_t;
646
647   /* Build printable strings for various stats */
648   file_type_string = wtap_file_type_string(cf_info->file_type);
649   file_encap_string = wtap_encap_string(cf_info->file_encap);
650   start_time_t = (time_t)cf_info->start_time;
651   stop_time_t = (time_t)cf_info->stop_time;
652
653   if (filename) {
654     putquote();
655     printf("%s", filename);
656     putquote();
657   }
658
659   if (cap_file_type) {
660     putsep();
661     putquote();
662     printf("%s", file_type_string);
663     putquote();
664   }
665
666   /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
667    *       Output a line for each different encap with all fields repeated except
668    *        the encapsulation field which has "Per Packet: ..." for each
669    *        encapsulation type seen ?
670    */
671   if (cap_file_encap) {
672     putsep();
673     putquote();
674     printf("%s", file_encap_string);
675     putquote();
676   }
677
678   if (cap_snaplen) {
679     putsep();
680     putquote();
681     if(cf_info->snap_set)
682       printf("%u", cf_info->snaplen);
683     else
684       printf("(not set)");
685     putquote();
686     if (cf_info->snaplen_max_inferred > 0) {
687       putsep();
688       putquote();
689       printf("%u", cf_info->snaplen_min_inferred);
690       putquote();
691       putsep();
692       putquote();
693       printf("%u", cf_info->snaplen_max_inferred);
694       putquote();
695     }
696     else {
697       putsep();
698       putquote();
699       printf("n/a");
700       putquote();
701       putsep();
702       putquote();
703       printf("n/a");
704       putquote();
705     }
706   }
707
708   if (cap_packet_count) {
709     putsep();
710     putquote();
711     printf("%u", cf_info->packet_count);
712     putquote();
713   }
714
715   if (cap_file_size) {
716     putsep();
717     putquote();
718     printf("%" G_GINT64_MODIFIER "d", cf_info->filesize);
719     putquote();
720   }
721
722   if (cap_data_size) {
723     putsep();
724     putquote();
725     printf("%" G_GINT64_MODIFIER "u", cf_info->packet_bytes);
726     putquote();
727   }
728
729   if (cap_duration) {
730     putsep();
731     putquote();
732     if (cf_info->times_known)
733       printf("%f", cf_info->duration);
734     else
735       printf("n/a");
736     putquote();
737   }
738
739   if (cap_start_time) {
740     putsep();
741     putquote();
742     printf("%s", time_string(start_time_t, cf_info, FALSE));
743     putquote();
744   }
745
746   if (cap_end_time) {
747     putsep();
748     putquote();
749     printf("%s", time_string(stop_time_t, cf_info, FALSE));
750     putquote();
751   }
752
753   if (cap_data_rate_byte) {
754     putsep();
755     putquote();
756     if (cf_info->times_known)
757       printf("%.2f", cf_info->data_rate);
758     else
759       printf("n/a");
760     putquote();
761   }
762
763   if (cap_data_rate_bit) {
764     putsep();
765     putquote();
766     if (cf_info->times_known)
767       printf("%.2f", cf_info->data_rate*8);
768     else
769       printf("n/a");
770     putquote();
771   }
772
773   if (cap_packet_size) {
774     putsep();
775     putquote();
776     printf("%.2f", cf_info->packet_size);
777     putquote();
778   }
779
780   if (cap_packet_rate) {
781     putsep();
782     putquote();
783     if (cf_info->times_known)
784       printf("%.2f", cf_info->packet_rate);
785     else
786       printf("n/a");
787     putquote();
788   }
789
790 #ifdef HAVE_LIBGCRYPT
791   if (cap_file_hashes) {
792     putsep();
793     putquote();
794     printf("%s", file_sha1);
795     putquote();
796
797     putsep();
798     putquote();
799     printf("%s", file_rmd160);
800     putquote();
801
802     putsep();
803     putquote();
804     printf("%s", file_md5);
805     putquote();
806   }
807 #endif /* HAVE_LIBGCRYPT */
808
809   if (cap_order) {
810     putsep();
811     putquote();
812     printf("%s", order_string(cf_info->order));
813     putquote();
814   }
815
816   if (cap_comment) {
817     putsep();
818     putquote();
819     printf("%s", cf_info->comment);
820     putquote();
821   }
822
823
824   printf("\n");
825 }
826
827 static int
828 process_cap_file(wtap *wth, const char *filename)
829 {
830   int                   err;
831   gchar                *err_info;
832   gint64                size;
833   gint64                data_offset;
834
835   guint32               packet = 0;
836   gint64                bytes  = 0;
837   guint32               snaplen_min_inferred = 0xffffffff;
838   guint32               snaplen_max_inferred =          0;
839   const struct wtap_pkthdr *phdr;
840   capture_info          cf_info;
841   gboolean              have_times = TRUE;
842   double                start_time = 0;
843   double                stop_time  = 0;
844   double                cur_time   = 0;
845   double                prev_time = 0;
846   gboolean              know_order = FALSE;
847   order_t               order = IN_ORDER;
848   wtapng_section_t     *shb_inf;
849   gchar                *p;
850
851
852   cf_info.encap_counts = g_new0(int,WTAP_NUM_ENCAP_TYPES);
853
854   /* Tally up data that we need to parse through the file to find */
855   while (wtap_read(wth, &err, &err_info, &data_offset))  {
856     phdr = wtap_phdr(wth);
857     if (phdr->presence_flags & WTAP_HAS_TS) {
858       prev_time = cur_time;
859       cur_time = secs_nsecs(&phdr->ts);
860       if(packet==0) {
861         start_time = cur_time;
862         stop_time = cur_time;
863         prev_time = cur_time;
864       }
865       if (cur_time < prev_time) {
866         order = NOT_IN_ORDER;
867       }
868       if (cur_time < start_time) {
869         start_time = cur_time;
870       }
871       if (cur_time > stop_time) {
872         stop_time = cur_time;
873       }
874     } else {
875       have_times = FALSE; /* at least one packet has no time stamp */
876       if (order != NOT_IN_ORDER)
877         order = ORDER_UNKNOWN;
878     }
879
880     bytes+=phdr->len;
881     packet++;
882
883     /* If caplen < len for a rcd, then presumably           */
884     /* 'Limit packet capture length' was done for this rcd. */
885     /* Keep track as to the min/max actual snapshot lengths */
886     /*  seen for this file.                                 */
887     if (phdr->caplen < phdr->len) {
888       if (phdr->caplen < snaplen_min_inferred)
889         snaplen_min_inferred = phdr->caplen;
890       if (phdr->caplen > snaplen_max_inferred)
891         snaplen_max_inferred = phdr->caplen;
892     }
893
894     /* Per-packet encapsulation */
895     if (wtap_file_encap(wth) == WTAP_ENCAP_PER_PACKET) {
896       if ((phdr->pkt_encap > 0) && (phdr->pkt_encap < WTAP_NUM_ENCAP_TYPES)) {
897         cf_info.encap_counts[phdr->pkt_encap] += 1;
898       } else {
899         fprintf(stderr, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr->pkt_encap, packet);
900       }
901     }
902
903   } /* while */
904
905   if (err != 0) {
906     fprintf(stderr,
907         "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
908         packet, filename, wtap_strerror(err));
909     switch (err) {
910
911       case WTAP_ERR_UNSUPPORTED:
912       case WTAP_ERR_UNSUPPORTED_ENCAP:
913       case WTAP_ERR_BAD_FILE:
914       case WTAP_ERR_DECOMPRESS:
915         fprintf(stderr, "(%s)\n", err_info);
916         g_free(err_info);
917         break;
918     }
919     g_free(cf_info.encap_counts);
920     return 1;
921   }
922
923   /* File size */
924   size = wtap_file_size(wth, &err);
925   if (size == -1) {
926     fprintf(stderr,
927         "capinfos: Can't get size of \"%s\": %s.\n",
928         filename, g_strerror(err));
929     g_free(cf_info.encap_counts);
930     return 1;
931   }
932
933   cf_info.filesize = size;
934
935   /* File Type */
936   cf_info.file_type = wtap_file_type(wth);
937   cf_info.iscompressed = wtap_iscompressed(wth);
938
939   /* File Encapsulation */
940   cf_info.file_encap = wtap_file_encap(wth);
941
942   /* Packet size limit (snaplen) */
943   cf_info.snaplen = wtap_snapshot_length(wth);
944   if(cf_info.snaplen > 0)
945     cf_info.snap_set = TRUE;
946   else
947     cf_info.snap_set = FALSE;
948
949   cf_info.snaplen_min_inferred = snaplen_min_inferred;
950   cf_info.snaplen_max_inferred = snaplen_max_inferred;
951
952   /* # of packets */
953   cf_info.packet_count = packet;
954
955   /* File Times */
956   cf_info.times_known = have_times;
957   cf_info.start_time = start_time;
958   cf_info.stop_time = stop_time;
959   cf_info.duration = stop_time-start_time;
960   cf_info.know_order = know_order;
961   cf_info.order = order;
962
963   /* Number of packet bytes */
964   cf_info.packet_bytes = bytes;
965
966   cf_info.data_rate   = 0.0;
967   cf_info.packet_rate = 0.0;
968   cf_info.packet_size = 0.0;
969
970   if (packet > 0) {
971     if (cf_info.duration > 0.0) {
972       cf_info.data_rate   = (double)bytes  / (stop_time-start_time); /* Data rate per second */
973       cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
974     }
975     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
976   }
977
978   cf_info.comment = NULL;
979   shb_inf = wtap_file_get_shb_info(wth);
980   if (shb_inf) {
981     /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
982     cf_info.comment = g_strdup(shb_inf->opt_comment);
983   }
984   g_free(shb_inf);
985   if (cf_info.comment) {
986     /* multi-line comments would conflict with the formatting that capinfos uses
987        we replace linefeeds with spaces */
988     p = cf_info.comment;
989     while (*p != '\0') {
990       if (*p=='\n')
991         *p=' ';
992       p++;
993     }
994   }
995
996   if(long_report) {
997     print_stats(filename, &cf_info);
998   } else {
999     print_stats_table(filename, &cf_info);
1000   }
1001
1002   g_free(cf_info.encap_counts);
1003   g_free(cf_info.comment);
1004
1005   return 0;
1006 }
1007
1008 static void
1009 usage(gboolean is_error)
1010 {
1011   FILE *output;
1012
1013   if (!is_error) {
1014     output = stdout;
1015     /* XXX - add capinfos header info here */
1016   }
1017   else {
1018     output = stderr;
1019   }
1020
1021   fprintf(output, "Capinfos %s"
1022 #ifdef SVNVERSION
1023       " (" SVNVERSION " from " SVNPATH ")"
1024 #endif
1025       "\n", VERSION);
1026   fprintf(output, "Prints various information (infos) about capture files.\n");
1027   fprintf(output, "See http://www.wireshark.org for more information.\n");
1028   fprintf(output, "\n");
1029   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
1030   fprintf(output, "\n");
1031   fprintf(output, "General infos:\n");
1032   fprintf(output, "  -t display the capture file type\n");
1033   fprintf(output, "  -E display the capture file encapsulation\n");
1034 #ifdef HAVE_LIBGCRYPT
1035   fprintf(output, "  -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1036 #endif
1037   fprintf(output, "  -k display the capture comment\n");
1038   fprintf(output, "\n");
1039   fprintf(output, "Size infos:\n");
1040   fprintf(output, "  -c display the number of packets\n");
1041   fprintf(output, "  -s display the size of the file (in bytes)\n");
1042   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
1043   fprintf(output, "  -l display the packet size limit (snapshot length)\n");
1044   fprintf(output, "\n");
1045   fprintf(output, "Time infos:\n");
1046   fprintf(output, "  -u display the capture duration (in seconds)\n");
1047   fprintf(output, "  -a display the capture start time\n");
1048   fprintf(output, "  -e display the capture end time\n");
1049   fprintf(output, "  -o display the capture file chronological status (True/False)\n");
1050   fprintf(output, "  -S display start and end times as seconds\n");
1051   fprintf(output, "\n");
1052   fprintf(output, "Statistic infos:\n");
1053   fprintf(output, "  -y display average data rate (in bytes/sec)\n");
1054   fprintf(output, "  -i display average data rate (in bits/sec)\n");
1055   fprintf(output, "  -z display average packet size (in bytes)\n");
1056   fprintf(output, "  -x display average packet rate (in packets/sec)\n");
1057   fprintf(output, "\n");
1058   fprintf(output, "Output format:\n");
1059   fprintf(output, "  -L generate long report (default)\n");
1060   fprintf(output, "  -T generate table report\n");
1061   fprintf(output, "  -M display machine-readable values in long reports\n");
1062   fprintf(output, "\n");
1063   fprintf(output, "Table report options:\n");
1064   fprintf(output, "  -R generate header record (default)\n");
1065   fprintf(output, "  -r do not generate header record\n");
1066   fprintf(output, "\n");
1067   fprintf(output, "  -B separate infos with TAB character (default)\n");
1068   fprintf(output, "  -m separate infos with comma (,) character\n");
1069   fprintf(output, "  -b separate infos with SPACE character\n");
1070   fprintf(output, "\n");
1071   fprintf(output, "  -N do not quote infos (default)\n");
1072   fprintf(output, "  -q quote infos with single quotes (')\n");
1073   fprintf(output, "  -Q quote infos with double quotes (\")\n");
1074   fprintf(output, "\n");
1075   fprintf(output, "Miscellaneous:\n");
1076   fprintf(output, "  -h display this help and exit\n");
1077   fprintf(output, "  -C cancel processing if file open fails (default is to continue)\n");
1078   fprintf(output, "  -A generate all infos (default)\n");
1079   fprintf(output, "\n");
1080   fprintf(output, "Options are processed from left to right order with later options superceeding\n");
1081   fprintf(output, "or adding to earlier options.\n");
1082   fprintf(output, "\n");
1083   fprintf(output, "If no options are given the default is to display all infos in long report\n");
1084   fprintf(output, "output format.\n");
1085 #ifndef HAVE_LIBGCRYPT
1086   fprintf(output, "\nFile hashing support (-H) is not present.\n");
1087 #endif
1088 }
1089
1090 #ifdef HAVE_PLUGINS
1091 /*
1092  *  Don't report failures to load plugins because most (non-wiretap) plugins
1093  *  *should* fail to load (because we're not linked against libwireshark and
1094  *  dissector plugins need libwireshark).
1095  */
1096 static void
1097 failure_message(const char *msg_format _U_, va_list ap _U_)
1098 {
1099   return;
1100 }
1101 #endif
1102
1103 #ifdef HAVE_LIBGCRYPT
1104 static void
1105 hash_to_str(const unsigned char *hash, size_t length, char *str) {
1106   int i;
1107
1108   for (i = 0; i < (int) length; i++) {
1109     g_snprintf(str+(i*2), 3, "%02x", hash[i]);
1110   }
1111 }
1112 #endif /* HAVE_LIBGCRYPT */
1113
1114 int
1115 main(int argc, char *argv[])
1116 {
1117   wtap  *wth;
1118   int    err;
1119   gchar *err_info;
1120   int    opt;
1121   int    overall_error_status;
1122
1123   int status = 0;
1124 #ifdef HAVE_PLUGINS
1125   char  *init_progfile_dir_error;
1126 #endif
1127 #ifdef HAVE_LIBGCRYPT
1128   FILE  *fh;
1129   char  *hash_buf = NULL;
1130   gcry_md_hd_t hd = NULL;
1131   size_t hash_bytes;
1132 #endif
1133
1134 #ifdef _WIN32
1135   arg_list_utf_16to8(argc, argv);
1136   create_app_running_mutex();
1137 #endif /* _WIN32 */
1138
1139   /*
1140    * Get credential information for later use.
1141    */
1142   init_process_policies();
1143
1144 #ifdef HAVE_PLUGINS
1145   /* Register wiretap plugins */
1146
1147   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
1148     g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
1149     g_free(init_progfile_dir_error);
1150   } else {
1151     init_report_err(failure_message,NULL,NULL,NULL);
1152     init_plugins();
1153   }
1154 #endif
1155
1156   /* Process the options */
1157 #ifdef USE_GOPTION
1158   ctx = g_option_context_new(" <infile> ... - print information about capture file(s)");
1159   general_grp = g_option_group_new("gen", "General infos:",
1160       "Show general options", NULL, NULL);
1161   size_grp = g_option_group_new("size", "Size infos:",
1162       "Show size options", NULL, NULL);
1163   time_grp = g_option_group_new("time", "Time infos:",
1164       "Show time options", NULL, NULL);
1165   stats_grp = g_option_group_new("stats", "Statistics infos:",
1166       "Show statistics options", NULL, NULL);
1167   output_grp = g_option_group_new("output", "Output format:",
1168       "Show output format options", NULL, NULL);
1169   table_report_grp = g_option_group_new("table", "Table report options:",
1170       "Show table report options", NULL, NULL);
1171   g_option_group_add_entries(general_grp, general_entries);
1172   g_option_group_add_entries(size_grp, size_entries);
1173   g_option_group_add_entries(time_grp, time_entries);
1174   g_option_group_add_entries(stats_grp, stats_entries);
1175   g_option_group_add_entries(output_grp, output_format_entries);
1176   g_option_group_add_entries(table_report_grp, table_report_entries);
1177   g_option_context_add_main_entries(ctx, misc_entries, NULL);
1178   g_option_context_add_group(ctx, general_grp);
1179   g_option_context_add_group(ctx, size_grp);
1180   g_option_context_add_group(ctx, time_grp);
1181   g_option_context_add_group(ctx, stats_grp);
1182   g_option_context_add_group(ctx, output_grp);
1183   g_option_context_add_group(ctx, table_report_grp);
1184   /* There's probably a better way to do this, but this works for now.
1185      GOptions displays the name in argv[0] as the name of the
1186      application.  This is reasonable, but because we actually have a
1187      script wrapper that calls the executable.  The name that gets
1188      displayed is not exactly the same as the command the user used
1189      ran.
1190    */
1191   argv[0] = (char *)"capinfos";
1192
1193   /* if we have at least one cmdline option, we disable printing all infos */
1194   if (argc>=2 && report_all_infos)
1195     disable_all_infos();
1196
1197   if( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
1198     if(parse_err) g_print ("option parsing failed: %s\n", parse_err->message);
1199     g_print("%s", g_option_context_get_help (ctx, TRUE, NULL));
1200     exit(1);
1201   }
1202   if( cap_help || (argc < 2) ) {
1203     g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
1204     exit(0);
1205   }
1206   g_option_context_free(ctx);
1207
1208 #endif /* USE_GOPTION */
1209   while ((opt = getopt(argc, argv, "tEcs" FILE_HASH_OPT "dluaeyizvhxokCALTMRrSNqQBmb")) !=-1) {
1210
1211     switch (opt) {
1212
1213       case 't':
1214         if (report_all_infos) disable_all_infos();
1215         cap_file_type = TRUE;
1216         break;
1217
1218       case 'E':
1219         if (report_all_infos) disable_all_infos();
1220         cap_file_encap = TRUE;
1221         break;
1222
1223       case 'l':
1224         if (report_all_infos) disable_all_infos();
1225         cap_snaplen = TRUE;
1226         break;
1227
1228       case 'c':
1229         if (report_all_infos) disable_all_infos();
1230         cap_packet_count = TRUE;
1231         break;
1232
1233       case 's':
1234         if (report_all_infos) disable_all_infos();
1235         cap_file_size = TRUE;
1236         break;
1237
1238       case 'd':
1239         if (report_all_infos) disable_all_infos();
1240         cap_data_size = TRUE;
1241         break;
1242
1243       case 'u':
1244         if (report_all_infos) disable_all_infos();
1245         cap_duration = TRUE;
1246         break;
1247
1248       case 'a':
1249         if (report_all_infos) disable_all_infos();
1250         cap_start_time = TRUE;
1251         break;
1252
1253       case 'e':
1254         if (report_all_infos) disable_all_infos();
1255         cap_end_time = TRUE;
1256         break;
1257
1258       case 'S':
1259         time_as_secs = TRUE;
1260         break;
1261
1262       case 'y':
1263         if (report_all_infos) disable_all_infos();
1264         cap_data_rate_byte = TRUE;
1265         break;
1266
1267       case 'i':
1268         if (report_all_infos) disable_all_infos();
1269         cap_data_rate_bit = TRUE;
1270         break;
1271
1272       case 'z':
1273         if (report_all_infos) disable_all_infos();
1274         cap_packet_size = TRUE;
1275         break;
1276
1277       case 'x':
1278         if (report_all_infos) disable_all_infos();
1279         cap_packet_rate = TRUE;
1280         break;
1281
1282 #ifdef HAVE_LIBGCRYPT
1283       case 'H':
1284         if (report_all_infos) disable_all_infos();
1285         cap_file_hashes = TRUE;
1286         break;
1287 #endif
1288
1289       case 'o':
1290         if (report_all_infos) disable_all_infos();
1291         cap_order = TRUE;
1292         break;
1293
1294       case 'k':
1295         if (report_all_infos) disable_all_infos();
1296         cap_comment = TRUE;
1297         break;
1298
1299       case 'C':
1300         continue_after_wtap_open_offline_failure = FALSE;
1301         break;
1302
1303       case 'A':
1304         enable_all_infos();
1305         break;
1306
1307       case 'L':
1308         long_report = TRUE;
1309         break;
1310
1311       case 'T':
1312         long_report = FALSE;
1313         break;
1314
1315       case 'M':
1316         machine_readable = TRUE;
1317         break;
1318
1319       case 'R':
1320         table_report_header = TRUE;
1321         break;
1322
1323       case 'r':
1324         table_report_header = FALSE;
1325         break;
1326
1327       case 'N':
1328         quote_char = '\0';
1329         break;
1330
1331       case 'q':
1332         quote_char = '\'';
1333         break;
1334
1335       case 'Q':
1336         quote_char = '"';
1337         break;
1338
1339       case 'B':
1340         field_separator = '\t';
1341         break;
1342
1343       case 'm':
1344         field_separator = ',';
1345         break;
1346
1347       case 'b':
1348         field_separator = ' ';
1349         break;
1350
1351       case 'h':
1352         usage(FALSE);
1353         exit(1);
1354         break;
1355
1356       case '?':              /* Bad flag - print usage message */
1357         usage(TRUE);
1358         exit(1);
1359         break;
1360     }
1361   }
1362
1363   /* Set the C-language locale to the native environment. */
1364   setlocale(LC_ALL, "");
1365
1366   if ((argc - optind) < 1) {
1367     usage(TRUE);
1368     exit(1);
1369   }
1370
1371   if(!long_report && table_report_header) {
1372     print_stats_table_header();
1373   }
1374
1375 #ifdef HAVE_LIBGCRYPT
1376   if (cap_file_hashes) {
1377     gcry_check_version(NULL);
1378     gcry_md_open(&hd, GCRY_MD_SHA1, 0);
1379     if (hd) {
1380       gcry_md_enable(hd, GCRY_MD_RMD160);
1381       gcry_md_enable(hd, GCRY_MD_MD5);
1382     }
1383     hash_buf = (char *)g_malloc(HASH_BUF_SIZE);
1384   }
1385 #endif
1386
1387   overall_error_status = 0;
1388
1389   for (opt = optind; opt < argc; opt++) {
1390
1391 #ifdef HAVE_LIBGCRYPT
1392     g_strlcpy(file_sha1, "<unknown>", HASH_STR_SIZE);
1393     g_strlcpy(file_rmd160, "<unknown>", HASH_STR_SIZE);
1394     g_strlcpy(file_md5, "<unknown>", HASH_STR_SIZE);
1395
1396     if (cap_file_hashes) {
1397       fh = ws_fopen(argv[opt], "rb");
1398       if (fh && hd) {
1399         while((hash_bytes = fread(hash_buf, 1, HASH_BUF_SIZE, fh)) > 0) {
1400           gcry_md_write(hd, hash_buf, hash_bytes);
1401         }
1402         gcry_md_final(hd);
1403         hash_to_str(gcry_md_read(hd, GCRY_MD_SHA1), HASH_SIZE_SHA1, file_sha1);
1404         hash_to_str(gcry_md_read(hd, GCRY_MD_RMD160), HASH_SIZE_RMD160, file_rmd160);
1405         hash_to_str(gcry_md_read(hd, GCRY_MD_MD5), HASH_SIZE_MD5, file_md5);
1406       }
1407       if (fh) fclose(fh);
1408       if (hd) gcry_md_reset(hd);
1409     }
1410 #endif /* HAVE_LIBGCRYPT */
1411
1412     wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
1413
1414     if (!wth) {
1415       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
1416           wtap_strerror(err));
1417       switch (err) {
1418
1419         case WTAP_ERR_UNSUPPORTED:
1420         case WTAP_ERR_UNSUPPORTED_ENCAP:
1421         case WTAP_ERR_BAD_FILE:
1422           fprintf(stderr, "(%s)\n", err_info);
1423           g_free(err_info);
1424           break;
1425       }
1426       overall_error_status = 1; /* remember that an error has occurred */
1427       if(!continue_after_wtap_open_offline_failure)
1428         exit(1); /* error status */
1429     }
1430
1431     if(wth) {
1432       if ((opt > optind) && (long_report))
1433         printf("\n");
1434       status = process_cap_file(wth, argv[opt]);
1435
1436       wtap_close(wth);
1437       if (status)
1438         exit(status);
1439     }
1440   }
1441
1442   return overall_error_status;
1443 }
1444
1445 /*
1446  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
1447  *
1448  * Local variables:
1449  * c-basic-offset: 2
1450  * tab-width: 2
1451  * indent-tabs-mode: nil
1452  * End:
1453  *
1454  * vi: set shiftwidth=2 tabstop=2 expandtab:
1455  * :indentSize=2:tabSize=2:noTabs=true:
1456  */