2 * Reports capture file information including # of packets, duration, others
4 * Copyright 2004 Ian Schorr
8 * Wireshark - Network traffic analyzer
9 * By Gerald Combs <gerald@wireshark.org>
10 * Copyright 1998 Gerald Combs
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.
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.
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.
30 * New capinfos features
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.
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.
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.
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).
72 #ifdef HAVE_SYS_TIME_H
78 #include <wsutil/privileges.h>
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.
87 #define RESET_SYMBOL_EXPORT
88 #include <epan/filesystem.h>
89 #include <epan/plugins.h>
91 #define RESET_SYMBOL_EXPORT
94 #include <wsutil/report_err.h>
95 #include <wsutil/privileges.h>
96 #include <wsutil/str_util.h>
99 #include <wsutil/wsgcrypt.h>
100 #include <wsutil/file_util.h>
104 #include "wsutil/wsgetopt.h"
108 #include <wsutil/unicode-utils.h>
111 #include "svnversion.h"
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
123 static gboolean continue_after_wtap_open_offline_failure
= TRUE
;
126 * table report variables
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 */
136 * capinfos has the ability to report on a number of
137 * various characteristics ("infos") for each input file.
139 * By default reporting of all info fields is enabled.
141 * Optionally the reporting of any specific info field
142 * or combination of info fields can be enabled with
143 * individual options.
146 static gboolean report_all_infos
= TRUE
; /* Report all infos */
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 */
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 */
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) */
167 #ifdef HAVE_LIBGCRYPT
168 static gboolean cap_file_hashes
= TRUE
; /* Calculate file hashes */
172 static gboolean cap_help
= FALSE
;
173 static gboolean table_report
= FALSE
;
175 static GOptionEntry general_entries
[] =
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
}
190 static GOptionEntry size_entries
[] =
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
}
203 static GOptionEntry time_entries
[] =
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
}
219 static GOptionEntry stats_entries
[] =
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
}
233 static GOptionEntry output_format_entries
[] =
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
}
245 static GOptionEntry table_report_entries
[] =
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
}
255 static GOptionEntry misc_entries
[] =
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
}
263 GOptionGroup
*general_grp
, *size_grp
, *time_grp
, *stats_grp
, *output_grp
, *table_report_grp
;
264 GError
*parse_err
= NULL
;
266 #endif /* USE_GOPTION */
268 #ifdef HAVE_LIBGCRYPT
269 #define HASH_SIZE_SHA1 20
270 #define HASH_SIZE_RMD160 20
271 #define HASH_SIZE_MD5 16
273 #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
274 #define HASH_BUF_SIZE (1024 * 1024)
277 static gchar file_sha1
[HASH_STR_SIZE
];
278 static gchar file_rmd160
[HASH_STR_SIZE
];
279 static gchar file_md5
[HASH_STR_SIZE
];
281 #define FILE_HASH_OPT "H"
283 #define FILE_HASH_OPT ""
284 #endif /* HAVE_LIBGCRYPT */
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.
291 * If every packet has a time stamp, and they're all in order, the time
292 * stamp is known to be in order.
294 * Otherwise, we have no idea.
302 typedef struct _capture_info
{
303 const char *filename
;
305 gboolean iscompressed
;
310 guint64 packet_bytes
;
311 gboolean times_known
;
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
;
325 double data_rate
; /* in bytes */
329 int *encap_counts
; /* array of per_packet encap counts; array has one entry per wtap_encap type */
334 enable_all_infos(void)
336 report_all_infos
= TRUE
;
338 cap_file_type
= TRUE
;
339 cap_file_encap
= TRUE
;
341 cap_packet_count
= TRUE
;
342 cap_file_size
= TRUE
;
345 cap_data_size
= TRUE
;
347 cap_start_time
= TRUE
;
351 cap_data_rate_byte
= TRUE
;
352 cap_data_rate_bit
= TRUE
;
353 cap_packet_size
= TRUE
;
354 cap_packet_rate
= TRUE
;
356 #ifdef HAVE_LIBGCRYPT
357 cap_file_hashes
= TRUE
;
358 #endif /* HAVE_LIBGCRYPT */
362 disable_all_infos(void)
364 report_all_infos
= FALSE
;
366 cap_file_type
= FALSE
;
367 cap_file_encap
= FALSE
;
369 cap_packet_count
= FALSE
;
370 cap_file_size
= FALSE
;
373 cap_data_size
= FALSE
;
374 cap_duration
= FALSE
;
375 cap_start_time
= FALSE
;
376 cap_end_time
= FALSE
;
379 cap_data_rate_byte
= FALSE
;
380 cap_data_rate_bit
= FALSE
;
381 cap_packet_size
= FALSE
;
382 cap_packet_rate
= FALSE
;
384 #ifdef HAVE_LIBGCRYPT
385 cap_file_hashes
= FALSE
;
386 #endif /* HAVE_LIBGCRYPT */
390 order_string(order_t order
)
404 return "???"; /* "cannot happen" (the next step is "Profit!") */
409 time_string(time_t timer
, capture_info
*cf_info
, gboolean want_lf
)
411 const gchar
*lf
= want_lf
? "\n" : "";
412 static gchar time_string_buf
[20];
413 char *time_string_ctime
;
415 if (cf_info
->times_known
&& cf_info
->packet_count
> 0) {
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
;
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
;
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
;
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.
440 time_string_ctime
[24] = '\0';
442 return time_string_ctime
;
446 g_snprintf(time_string_buf
, 15, "n/a%s", lf
);
447 return time_string_buf
;
450 static void print_value(const gchar
*text_p1
, gint width
, const gchar
*text_p2
, double value
) {
452 printf("%s%.*f%s\n", text_p1
, width
, value
, text_p2
);
454 printf("%sn/a\n", text_p1
);
458 print_stats(const gchar
*filename
, capture_info
*cf_info
)
460 const gchar
*file_type_string
, *file_encap_string
;
465 /* Build printable strings for various stats */
466 file_type_string
= wtap_file_type_subtype_string(cf_info
->file_type
);
467 file_encap_string
= wtap_encap_string(cf_info
->file_encap
);
468 start_time_t
= (time_t)cf_info
->start_time
;
469 stop_time_t
= (time_t)cf_info
->stop_time
;
471 if (filename
) printf ("File name: %s\n", filename
);
472 if (cap_file_type
) printf ("File type: %s%s\n",
474 cf_info
->iscompressed
? " (gzip compressed)" : "");
475 if (cap_file_encap
) printf ("File encapsulation: %s\n", file_encap_string
);
476 if (cap_file_encap
&& (cf_info
->file_encap
== WTAP_ENCAP_PER_PACKET
)) {
478 for (i
=0; i
<WTAP_NUM_ENCAP_TYPES
; i
++) {
479 if (cf_info
->encap_counts
[i
] > 0)
480 printf(" %s\n", wtap_encap_string(i
));
483 if (cap_snaplen
&& cf_info
->snap_set
)
484 printf ("Packet size limit: file hdr: %u bytes\n", cf_info
->snaplen
);
485 else if(cap_snaplen
&& !cf_info
->snap_set
)
486 printf ("Packet size limit: file hdr: (not set)\n");
487 if (cf_info
->snaplen_max_inferred
> 0) {
488 if (cf_info
->snaplen_min_inferred
== cf_info
->snaplen_max_inferred
)
489 printf ("Packet size limit: inferred: %u bytes\n", cf_info
->snaplen_min_inferred
);
491 printf ("Packet size limit: inferred: %u bytes - %u bytes (range)\n",
492 cf_info
->snaplen_min_inferred
, cf_info
->snaplen_max_inferred
);
494 if (cap_packet_count
) {
495 printf ("Number of packets: ");
496 if (machine_readable
) {
497 printf ("%u\n", cf_info
->packet_count
);
499 size_string
= format_size(cf_info
->packet_count
, format_size_unit_none
);
500 printf ("%s\n", size_string
);
505 printf ("File size: ");
506 if (machine_readable
) {
507 printf ("%" G_GINT64_MODIFIER
"d bytes\n", cf_info
->filesize
);
509 size_string
= format_size(cf_info
->filesize
, format_size_unit_bytes
);
510 printf ("%s\n", size_string
);
515 printf ("Data size: ");
516 if (machine_readable
) {
517 printf ("%" G_GINT64_MODIFIER
"u bytes\n", cf_info
->packet_bytes
);
519 size_string
= format_size(cf_info
->packet_bytes
, format_size_unit_bytes
);
520 printf ("%s\n", size_string
);
524 if (cf_info
->times_known
) {
525 if (cap_duration
) /* XXX - shorten to hh:mm:ss */
526 print_value("Capture duration: ", 0, " seconds", cf_info
->duration
);
528 printf ("Start time: %s", time_string(start_time_t
, cf_info
, TRUE
));
530 printf ("End time: %s", time_string(stop_time_t
, cf_info
, TRUE
));
531 if (cap_data_rate_byte
) {
532 printf ("Data byte rate: ");
533 if (machine_readable
) {
534 print_value("", 2, " bytes/sec", cf_info
->data_rate
);
536 size_string
= format_size((gint64
)cf_info
->data_rate
, format_size_unit_bytes_s
);
537 printf ("%s\n", size_string
);
541 if (cap_data_rate_bit
) {
542 printf ("Data bit rate: ");
543 if (machine_readable
) {
544 print_value("", 2, " bits/sec", cf_info
->data_rate
*8);
546 size_string
= format_size((gint64
)(cf_info
->data_rate
*8), format_size_unit_bits_s
);
547 printf ("%s\n", size_string
);
552 if (cap_packet_size
) printf ("Average packet size: %.2f bytes\n", cf_info
->packet_size
);
553 if (cf_info
->times_known
) {
554 if (cap_packet_rate
) {
555 printf ("Average packet rate: ");
556 if (machine_readable
) {
557 print_value("", 2, " packets/sec", cf_info
->packet_rate
);
559 size_string
= format_size((gint64
)cf_info
->packet_rate
, format_size_unit_none
);
560 printf ("%spackets/sec\n", size_string
);
565 #ifdef HAVE_LIBGCRYPT
566 if (cap_file_hashes
) {
567 printf ("SHA1: %s\n", file_sha1
);
568 printf ("RIPEMD160: %s\n", file_rmd160
);
569 printf ("MD5: %s\n", file_md5
);
571 #endif /* HAVE_LIBGCRYPT */
572 if (cap_order
) printf ("Strict time order: %s\n", order_string(cf_info
->order
));
573 if (cap_comment
&& cf_info
->comment
)
574 printf ("Capture comment: %s\n", cf_info
->comment
);
580 if (field_separator
) putchar(field_separator
);
586 if (quote_char
) putchar(quote_char
);
590 print_stats_table_header_label(const gchar
*label
)
599 print_stats_table_header(void)
605 if (cap_file_type
) print_stats_table_header_label("File type");
606 if (cap_file_encap
) print_stats_table_header_label("File encapsulation");
607 if (cap_snaplen
) print_stats_table_header_label("Packet size limit");
608 if (cap_snaplen
) print_stats_table_header_label("Packet size limit min (inferred)");
609 if (cap_snaplen
) print_stats_table_header_label("Packet size limit max (inferred)");
610 if (cap_packet_count
) print_stats_table_header_label("Number of packets");
611 if (cap_file_size
) print_stats_table_header_label("File size (bytes)");
612 if (cap_data_size
) print_stats_table_header_label("Data size (bytes)");
613 if (cap_duration
) print_stats_table_header_label("Capture duration (seconds)");
614 if (cap_start_time
) print_stats_table_header_label("Start time");
615 if (cap_end_time
) print_stats_table_header_label("End time");
616 if (cap_data_rate_byte
) print_stats_table_header_label("Data byte rate (bytes/sec)");
617 if (cap_data_rate_bit
) print_stats_table_header_label("Data bit rate (bits/sec)");
618 if (cap_packet_size
) print_stats_table_header_label("Average packet size (bytes)");
619 if (cap_packet_rate
) print_stats_table_header_label("Average packet rate (packets/sec)");
620 #ifdef HAVE_LIBGCRYPT
621 if (cap_file_hashes
) {
622 print_stats_table_header_label("SHA1");
623 print_stats_table_header_label("RIPEMD160");
624 print_stats_table_header_label("MD5");
626 #endif /* HAVE_LIBGCRYPT */
627 if (cap_order
) print_stats_table_header_label("Strict time order");
628 if (cap_comment
) print_stats_table_header_label("Capture comment");
634 print_stats_table(const gchar
*filename
, capture_info
*cf_info
)
636 const gchar
*file_type_string
, *file_encap_string
;
640 /* Build printable strings for various stats */
641 file_type_string
= wtap_file_type_subtype_string(cf_info
->file_type
);
642 file_encap_string
= wtap_encap_string(cf_info
->file_encap
);
643 start_time_t
= (time_t)cf_info
->start_time
;
644 stop_time_t
= (time_t)cf_info
->stop_time
;
648 printf("%s", filename
);
655 printf("%s", file_type_string
);
659 /* ToDo: If WTAP_ENCAP_PER_PACKET, show the list of encapsulations encountered;
660 * Output a line for each different encap with all fields repeated except
661 * the encapsulation field which has "Per Packet: ..." for each
662 * encapsulation type seen ?
664 if (cap_file_encap
) {
667 printf("%s", file_encap_string
);
674 if(cf_info
->snap_set
)
675 printf("%u", cf_info
->snaplen
);
679 if (cf_info
->snaplen_max_inferred
> 0) {
682 printf("%u", cf_info
->snaplen_min_inferred
);
686 printf("%u", cf_info
->snaplen_max_inferred
);
701 if (cap_packet_count
) {
704 printf("%u", cf_info
->packet_count
);
711 printf("%" G_GINT64_MODIFIER
"d", cf_info
->filesize
);
718 printf("%" G_GINT64_MODIFIER
"u", cf_info
->packet_bytes
);
725 if (cf_info
->times_known
)
726 printf("%f", cf_info
->duration
);
732 if (cap_start_time
) {
735 printf("%s", time_string(start_time_t
, cf_info
, FALSE
));
742 printf("%s", time_string(stop_time_t
, cf_info
, FALSE
));
746 if (cap_data_rate_byte
) {
749 if (cf_info
->times_known
)
750 printf("%.2f", cf_info
->data_rate
);
756 if (cap_data_rate_bit
) {
759 if (cf_info
->times_known
)
760 printf("%.2f", cf_info
->data_rate
*8);
766 if (cap_packet_size
) {
769 printf("%.2f", cf_info
->packet_size
);
773 if (cap_packet_rate
) {
776 if (cf_info
->times_known
)
777 printf("%.2f", cf_info
->packet_rate
);
783 #ifdef HAVE_LIBGCRYPT
784 if (cap_file_hashes
) {
787 printf("%s", file_sha1
);
792 printf("%s", file_rmd160
);
797 printf("%s", file_md5
);
800 #endif /* HAVE_LIBGCRYPT */
805 printf("%s", order_string(cf_info
->order
));
812 printf("%s", cf_info
->comment
);
821 process_cap_file(wtap
*wth
, const char *filename
)
831 guint32 snaplen_min_inferred
= 0xffffffff;
832 guint32 snaplen_max_inferred
= 0;
833 const struct wtap_pkthdr
*phdr
;
834 capture_info cf_info
;
835 gboolean have_times
= TRUE
;
836 double start_time
= 0;
837 double stop_time
= 0;
839 double prev_time
= 0;
840 gboolean know_order
= FALSE
;
841 order_t order
= IN_ORDER
;
842 wtapng_section_t
*shb_inf
;
846 cf_info
.encap_counts
= g_new0(int,WTAP_NUM_ENCAP_TYPES
);
848 /* Tally up data that we need to parse through the file to find */
849 while (wtap_read(wth
, &err
, &err_info
, &data_offset
)) {
850 phdr
= wtap_phdr(wth
);
851 if (phdr
->presence_flags
& WTAP_HAS_TS
) {
852 prev_time
= cur_time
;
853 cur_time
= nstime_to_sec(&phdr
->ts
);
855 start_time
= cur_time
;
856 stop_time
= cur_time
;
857 prev_time
= cur_time
;
859 if (cur_time
< prev_time
) {
860 order
= NOT_IN_ORDER
;
862 if (cur_time
< start_time
) {
863 start_time
= cur_time
;
865 if (cur_time
> stop_time
) {
866 stop_time
= cur_time
;
869 have_times
= FALSE
; /* at least one packet has no time stamp */
870 if (order
!= NOT_IN_ORDER
)
871 order
= ORDER_UNKNOWN
;
877 /* If caplen < len for a rcd, then presumably */
878 /* 'Limit packet capture length' was done for this rcd. */
879 /* Keep track as to the min/max actual snapshot lengths */
880 /* seen for this file. */
881 if (phdr
->caplen
< phdr
->len
) {
882 if (phdr
->caplen
< snaplen_min_inferred
)
883 snaplen_min_inferred
= phdr
->caplen
;
884 if (phdr
->caplen
> snaplen_max_inferred
)
885 snaplen_max_inferred
= phdr
->caplen
;
888 /* Per-packet encapsulation */
889 if (wtap_file_encap(wth
) == WTAP_ENCAP_PER_PACKET
) {
890 if ((phdr
->pkt_encap
> 0) && (phdr
->pkt_encap
< WTAP_NUM_ENCAP_TYPES
)) {
891 cf_info
.encap_counts
[phdr
->pkt_encap
] += 1;
893 fprintf(stderr
, "capinfos: Unknown per-packet encapsulation: %d [frame number: %d]\n", phdr
->pkt_encap
, packet
);
901 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
902 packet
, filename
, wtap_strerror(err
));
905 case WTAP_ERR_SHORT_READ
:
908 " (will continue anyway, checksums might be incorrect)\n");
911 case WTAP_ERR_UNSUPPORTED
:
912 case WTAP_ERR_UNSUPPORTED_ENCAP
:
913 case WTAP_ERR_BAD_FILE
:
914 case WTAP_ERR_DECOMPRESS
:
915 fprintf(stderr
, "(%s)\n", err_info
);
920 g_free(cf_info
.encap_counts
);
926 size
= wtap_file_size(wth
, &err
);
929 "capinfos: Can't get size of \"%s\": %s.\n",
930 filename
, g_strerror(err
));
931 g_free(cf_info
.encap_counts
);
935 cf_info
.filesize
= size
;
938 cf_info
.file_type
= wtap_file_type_subtype(wth
);
939 cf_info
.iscompressed
= wtap_iscompressed(wth
);
941 /* File Encapsulation */
942 cf_info
.file_encap
= wtap_file_encap(wth
);
944 /* Packet size limit (snaplen) */
945 cf_info
.snaplen
= wtap_snapshot_length(wth
);
946 if(cf_info
.snaplen
> 0)
947 cf_info
.snap_set
= TRUE
;
949 cf_info
.snap_set
= FALSE
;
951 cf_info
.snaplen_min_inferred
= snaplen_min_inferred
;
952 cf_info
.snaplen_max_inferred
= snaplen_max_inferred
;
955 cf_info
.packet_count
= packet
;
958 cf_info
.times_known
= have_times
;
959 cf_info
.start_time
= start_time
;
960 cf_info
.stop_time
= stop_time
;
961 cf_info
.duration
= stop_time
-start_time
;
962 cf_info
.know_order
= know_order
;
963 cf_info
.order
= order
;
965 /* Number of packet bytes */
966 cf_info
.packet_bytes
= bytes
;
968 cf_info
.data_rate
= 0.0;
969 cf_info
.packet_rate
= 0.0;
970 cf_info
.packet_size
= 0.0;
973 if (cf_info
.duration
> 0.0) {
974 cf_info
.data_rate
= (double)bytes
/ (stop_time
-start_time
); /* Data rate per second */
975 cf_info
.packet_rate
= (double)packet
/ (stop_time
-start_time
); /* packet rate per second */
977 cf_info
.packet_size
= (double)bytes
/ packet
; /* Avg packet size */
980 cf_info
.comment
= NULL
;
981 shb_inf
= wtap_file_get_shb_info(wth
);
983 /* opt_comment is always 0-terminated by pcapng_read_section_header_block */
984 cf_info
.comment
= g_strdup(shb_inf
->opt_comment
);
987 if (cf_info
.comment
) {
988 /* multi-line comments would conflict with the formatting that capinfos uses
989 we replace linefeeds with spaces */
999 print_stats(filename
, &cf_info
);
1001 print_stats_table(filename
, &cf_info
);
1004 g_free(cf_info
.encap_counts
);
1005 g_free(cf_info
.comment
);
1011 usage(gboolean is_error
)
1017 /* XXX - add capinfos header info here */
1023 fprintf(output
, "Capinfos %s"
1025 " (" SVNVERSION
" from " SVNPATH
")"
1028 fprintf(output
, "Prints various information (infos) about capture files.\n");
1029 fprintf(output
, "See http://www.wireshark.org for more information.\n");
1030 fprintf(output
, "\n");
1031 fprintf(output
, "Usage: capinfos [options] <infile> ...\n");
1032 fprintf(output
, "\n");
1033 fprintf(output
, "General infos:\n");
1034 fprintf(output
, " -t display the capture file type\n");
1035 fprintf(output
, " -E display the capture file encapsulation\n");
1036 #ifdef HAVE_LIBGCRYPT
1037 fprintf(output
, " -H display the SHA1, RMD160, and MD5 hashes of the file\n");
1039 fprintf(output
, " -k display the capture comment\n");
1040 fprintf(output
, "\n");
1041 fprintf(output
, "Size infos:\n");
1042 fprintf(output
, " -c display the number of packets\n");
1043 fprintf(output
, " -s display the size of the file (in bytes)\n");
1044 fprintf(output
, " -d display the total length of all packets (in bytes)\n");
1045 fprintf(output
, " -l display the packet size limit (snapshot length)\n");
1046 fprintf(output
, "\n");
1047 fprintf(output
, "Time infos:\n");
1048 fprintf(output
, " -u display the capture duration (in seconds)\n");
1049 fprintf(output
, " -a display the capture start time\n");
1050 fprintf(output
, " -e display the capture end time\n");
1051 fprintf(output
, " -o display the capture file chronological status (True/False)\n");
1052 fprintf(output
, " -S display start and end times as seconds\n");
1053 fprintf(output
, "\n");
1054 fprintf(output
, "Statistic infos:\n");
1055 fprintf(output
, " -y display average data rate (in bytes/sec)\n");
1056 fprintf(output
, " -i display average data rate (in bits/sec)\n");
1057 fprintf(output
, " -z display average packet size (in bytes)\n");
1058 fprintf(output
, " -x display average packet rate (in packets/sec)\n");
1059 fprintf(output
, "\n");
1060 fprintf(output
, "Output format:\n");
1061 fprintf(output
, " -L generate long report (default)\n");
1062 fprintf(output
, " -T generate table report\n");
1063 fprintf(output
, " -M display machine-readable values in long reports\n");
1064 fprintf(output
, "\n");
1065 fprintf(output
, "Table report options:\n");
1066 fprintf(output
, " -R generate header record (default)\n");
1067 fprintf(output
, " -r do not generate header record\n");
1068 fprintf(output
, "\n");
1069 fprintf(output
, " -B separate infos with TAB character (default)\n");
1070 fprintf(output
, " -m separate infos with comma (,) character\n");
1071 fprintf(output
, " -b separate infos with SPACE character\n");
1072 fprintf(output
, "\n");
1073 fprintf(output
, " -N do not quote infos (default)\n");
1074 fprintf(output
, " -q quote infos with single quotes (')\n");
1075 fprintf(output
, " -Q quote infos with double quotes (\")\n");
1076 fprintf(output
, "\n");
1077 fprintf(output
, "Miscellaneous:\n");
1078 fprintf(output
, " -h display this help and exit\n");
1079 fprintf(output
, " -C cancel processing if file open fails (default is to continue)\n");
1080 fprintf(output
, " -A generate all infos (default)\n");
1081 fprintf(output
, "\n");
1082 fprintf(output
, "Options are processed from left to right order with later options superceding\n");
1083 fprintf(output
, "or adding to earlier options.\n");
1084 fprintf(output
, "\n");
1085 fprintf(output
, "If no options are given the default is to display all infos in long report\n");
1086 fprintf(output
, "output format.\n");
1087 #ifndef HAVE_LIBGCRYPT
1088 fprintf(output
, "\nFile hashing support (-H) is not present.\n");
1094 * Don't report failures to load plugins because most (non-wiretap) plugins
1095 * *should* fail to load (because we're not linked against libwireshark and
1096 * dissector plugins need libwireshark).
1099 failure_message(const char *msg_format _U_
, va_list ap _U_
)
1105 #ifdef HAVE_LIBGCRYPT
1107 hash_to_str(const unsigned char *hash
, size_t length
, char *str
) {
1110 for (i
= 0; i
< (int) length
; i
++) {
1111 g_snprintf(str
+(i
*2), 3, "%02x", hash
[i
]);
1114 #endif /* HAVE_LIBGCRYPT */
1117 main(int argc
, char *argv
[])
1123 int overall_error_status
;
1127 char *init_progfile_dir_error
;
1129 #ifdef HAVE_LIBGCRYPT
1131 char *hash_buf
= NULL
;
1132 gcry_md_hd_t hd
= NULL
;
1137 arg_list_utf_16to8(argc
, argv
);
1138 create_app_running_mutex();
1142 * Get credential information for later use.
1144 init_process_policies();
1147 /* Register wiretap plugins */
1149 if ((init_progfile_dir_error
= init_progfile_dir(argv
[0], main
))) {
1150 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error
);
1151 g_free(init_progfile_dir_error
);
1153 init_report_err(failure_message
,NULL
,NULL
,NULL
);
1158 /* Process the options */
1160 ctx
= g_option_context_new(" <infile> ... - print information about capture file(s)");
1161 general_grp
= g_option_group_new("gen", "General infos:",
1162 "Show general options", NULL
, NULL
);
1163 size_grp
= g_option_group_new("size", "Size infos:",
1164 "Show size options", NULL
, NULL
);
1165 time_grp
= g_option_group_new("time", "Time infos:",
1166 "Show time options", NULL
, NULL
);
1167 stats_grp
= g_option_group_new("stats", "Statistics infos:",
1168 "Show statistics options", NULL
, NULL
);
1169 output_grp
= g_option_group_new("output", "Output format:",
1170 "Show output format options", NULL
, NULL
);
1171 table_report_grp
= g_option_group_new("table", "Table report options:",
1172 "Show table report options", NULL
, NULL
);
1173 g_option_group_add_entries(general_grp
, general_entries
);
1174 g_option_group_add_entries(size_grp
, size_entries
);
1175 g_option_group_add_entries(time_grp
, time_entries
);
1176 g_option_group_add_entries(stats_grp
, stats_entries
);
1177 g_option_group_add_entries(output_grp
, output_format_entries
);
1178 g_option_group_add_entries(table_report_grp
, table_report_entries
);
1179 g_option_context_add_main_entries(ctx
, misc_entries
, NULL
);
1180 g_option_context_add_group(ctx
, general_grp
);
1181 g_option_context_add_group(ctx
, size_grp
);
1182 g_option_context_add_group(ctx
, time_grp
);
1183 g_option_context_add_group(ctx
, stats_grp
);
1184 g_option_context_add_group(ctx
, output_grp
);
1185 g_option_context_add_group(ctx
, table_report_grp
);
1186 /* There's probably a better way to do this, but this works for now.
1187 GOptions displays the name in argv[0] as the name of the
1188 application. This is reasonable, but because we actually have a
1189 script wrapper that calls the executable. The name that gets
1190 displayed is not exactly the same as the command the user used
1193 argv
[0] = (char *)"capinfos";
1195 /* if we have at least one cmdline option, we disable printing all infos */
1196 if (argc
>=2 && report_all_infos
)
1197 disable_all_infos();
1199 if( !g_option_context_parse(ctx
, &argc
, &argv
, &parse_err
) ) {
1200 if(parse_err
) g_print ("option parsing failed: %s\n", parse_err
->message
);
1201 g_print("%s", g_option_context_get_help (ctx
, TRUE
, NULL
));
1204 if( cap_help
|| (argc
< 2) ) {
1205 g_print("%s", g_option_context_get_help (ctx
, FALSE
, NULL
));
1208 g_option_context_free(ctx
);
1210 #endif /* USE_GOPTION */
1211 while ((opt
= getopt(argc
, argv
, "tEcs" FILE_HASH_OPT
"dluaeyizvhxokCALTMRrSNqQBmb")) !=-1) {
1216 if (report_all_infos
) disable_all_infos();
1217 cap_file_type
= TRUE
;
1221 if (report_all_infos
) disable_all_infos();
1222 cap_file_encap
= TRUE
;
1226 if (report_all_infos
) disable_all_infos();
1231 if (report_all_infos
) disable_all_infos();
1232 cap_packet_count
= TRUE
;
1236 if (report_all_infos
) disable_all_infos();
1237 cap_file_size
= TRUE
;
1241 if (report_all_infos
) disable_all_infos();
1242 cap_data_size
= TRUE
;
1246 if (report_all_infos
) disable_all_infos();
1247 cap_duration
= TRUE
;
1251 if (report_all_infos
) disable_all_infos();
1252 cap_start_time
= TRUE
;
1256 if (report_all_infos
) disable_all_infos();
1257 cap_end_time
= TRUE
;
1261 time_as_secs
= TRUE
;
1265 if (report_all_infos
) disable_all_infos();
1266 cap_data_rate_byte
= TRUE
;
1270 if (report_all_infos
) disable_all_infos();
1271 cap_data_rate_bit
= TRUE
;
1275 if (report_all_infos
) disable_all_infos();
1276 cap_packet_size
= TRUE
;
1280 if (report_all_infos
) disable_all_infos();
1281 cap_packet_rate
= TRUE
;
1284 #ifdef HAVE_LIBGCRYPT
1286 if (report_all_infos
) disable_all_infos();
1287 cap_file_hashes
= TRUE
;
1292 if (report_all_infos
) disable_all_infos();
1297 if (report_all_infos
) disable_all_infos();
1302 continue_after_wtap_open_offline_failure
= FALSE
;
1314 long_report
= FALSE
;
1318 machine_readable
= TRUE
;
1322 table_report_header
= TRUE
;
1326 table_report_header
= FALSE
;
1342 field_separator
= '\t';
1346 field_separator
= ',';
1350 field_separator
= ' ';
1358 case '?': /* Bad flag - print usage message */
1365 /* Set the C-language locale to the native environment. */
1366 setlocale(LC_ALL
, "");
1368 if ((argc
- optind
) < 1) {
1373 if(!long_report
&& table_report_header
) {
1374 print_stats_table_header();
1377 #ifdef HAVE_LIBGCRYPT
1378 if (cap_file_hashes
) {
1379 gcry_check_version(NULL
);
1380 gcry_md_open(&hd
, GCRY_MD_SHA1
, 0);
1382 gcry_md_enable(hd
, GCRY_MD_RMD160
);
1383 gcry_md_enable(hd
, GCRY_MD_MD5
);
1385 hash_buf
= (char *)g_malloc(HASH_BUF_SIZE
);
1389 overall_error_status
= 0;
1391 for (opt
= optind
; opt
< argc
; opt
++) {
1393 #ifdef HAVE_LIBGCRYPT
1394 g_strlcpy(file_sha1
, "<unknown>", HASH_STR_SIZE
);
1395 g_strlcpy(file_rmd160
, "<unknown>", HASH_STR_SIZE
);
1396 g_strlcpy(file_md5
, "<unknown>", HASH_STR_SIZE
);
1398 if (cap_file_hashes
) {
1399 fh
= ws_fopen(argv
[opt
], "rb");
1401 while((hash_bytes
= fread(hash_buf
, 1, HASH_BUF_SIZE
, fh
)) > 0) {
1402 gcry_md_write(hd
, hash_buf
, hash_bytes
);
1405 hash_to_str(gcry_md_read(hd
, GCRY_MD_SHA1
), HASH_SIZE_SHA1
, file_sha1
);
1406 hash_to_str(gcry_md_read(hd
, GCRY_MD_RMD160
), HASH_SIZE_RMD160
, file_rmd160
);
1407 hash_to_str(gcry_md_read(hd
, GCRY_MD_MD5
), HASH_SIZE_MD5
, file_md5
);
1410 if (hd
) gcry_md_reset(hd
);
1412 #endif /* HAVE_LIBGCRYPT */
1414 wth
= wtap_open_offline(argv
[opt
], &err
, &err_info
, FALSE
);
1417 fprintf(stderr
, "capinfos: Can't open %s: %s\n", argv
[opt
],
1418 wtap_strerror(err
));
1421 case WTAP_ERR_UNSUPPORTED
:
1422 case WTAP_ERR_UNSUPPORTED_ENCAP
:
1423 case WTAP_ERR_BAD_FILE
:
1424 fprintf(stderr
, "(%s)\n", err_info
);
1428 overall_error_status
= 1; /* remember that an error has occurred */
1429 if(!continue_after_wtap_open_offline_failure
)
1430 exit(1); /* error status */
1434 if ((opt
> optind
) && (long_report
))
1436 status
= process_cap_file(wth
, argv
[opt
]);
1444 return overall_error_status
;
1448 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1453 * indent-tabs-mode: nil
1456 * vi: set shiftwidth=2 tabstop=2 expandtab:
1457 * :indentSize=2:tabSize=2:noTabs=true: