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