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