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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
41 #ifdef HAVE_SYS_TIME_H
47 #include <epan/packet.h>
48 #include <epan/filesystem.h>
49 #include <epan/plugins.h>
50 #include <epan/report_err.h>
52 #include <wsutil/privileges.h>
60 static gboolean cap_file_type = FALSE; /* Do not report capture type */
61 static gboolean cap_file_encap = FALSE; /* Do not report encapsulation */
62 static gboolean cap_packet_count = FALSE; /* Do not produce packet count */
63 static gboolean cap_file_size = FALSE; /* Do not report file size */
64 static gboolean cap_data_size = FALSE; /* Do not report packet byte size */
65 static gboolean cap_duration = FALSE; /* Do not report capture duration */
66 static gboolean cap_start_time = FALSE;
67 static gboolean cap_end_time = FALSE;
69 static gboolean cap_data_rate_byte = FALSE;
70 static gboolean cap_data_rate_bit = FALSE;
71 static gboolean cap_packet_size = FALSE;
72 static gboolean cap_packet_rate = FALSE;
75 typedef struct _capture_info {
92 double data_rate; /* in bytes */
96 secs_nsecs(const struct wtap_nstime * nstime)
98 return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
101 static void print_value(gchar *text_p1, gint width, gchar *text_p2, double value) {
103 printf("%s%.*f%s\n", text_p1, width, value, text_p2);
105 printf("%sn/a\n", text_p1);
109 print_stats(capture_info *cf_info)
111 const gchar *file_type_string, *file_encap_string;
115 /* Build printable strings for various stats */
116 file_type_string = wtap_file_type_string(cf_info->file_type);
117 file_encap_string = wtap_encap_string(cf_info->file_encap);
118 start_time_t = (time_t)cf_info->start_time;
119 stop_time_t = (time_t)cf_info->stop_time;
121 if (cap_file_type) printf ("File type: %s\n", file_type_string);
122 if (cap_file_encap) printf ("File encapsulation: %s\n", file_encap_string);
123 if (cap_packet_count) printf ("Number of packets: %u\n", cf_info->packet_count);
124 if (cap_file_size) printf ("File size: %" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
125 if (cap_data_size) printf ("Data size: %" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
126 if (cap_duration) print_value("Capture duration: ", 0, " seconds", cf_info->duration);
127 if (cap_start_time) printf ("Start time: %s", (cf_info->packet_count>0) ? ctime (&start_time_t) : "n/a\n");
128 if (cap_end_time) printf ("End time: %s", (cf_info->packet_count>0) ? ctime (&stop_time_t) : "n/a\n");
129 if (cap_data_rate_byte) print_value("Data byte rate: ", 2, " bytes/sec", cf_info->data_rate);
130 if (cap_data_rate_bit) print_value("Data bit rate: ", 2, " bits/sec", cf_info->data_rate*8);
131 if (cap_packet_size) printf ("Average packet size: %.2f bytes\n", cf_info->packet_size);
132 if (cap_packet_rate) print_value("Average packet rate: ", 2, " packets/sec", cf_info->packet_rate);
136 process_cap_file(wtap *wth, const char *filename)
145 const struct wtap_pkthdr *phdr;
146 capture_info cf_info;
147 double start_time = 0;
148 double stop_time = 0;
151 /* Tally up data that we need to parse through the file to find */
152 while (wtap_read(wth, &err, &err_info, &data_offset)) {
153 phdr = wtap_phdr(wth);
154 cur_time = secs_nsecs(&phdr->ts);
156 start_time = cur_time;
157 stop_time = cur_time;
159 if (cur_time < start_time) {
160 start_time = cur_time;
162 if (cur_time > stop_time) {
163 stop_time = cur_time;
171 "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
172 packet, filename, wtap_strerror(err));
175 case WTAP_ERR_UNSUPPORTED:
176 case WTAP_ERR_UNSUPPORTED_ENCAP:
177 case WTAP_ERR_BAD_RECORD:
178 fprintf(stderr, "(%s)\n", err_info);
186 size = wtap_file_size(wth, &err);
189 "capinfos: Can't get size of \"%s\": %s.\n",
190 filename, strerror(err));
194 cf_info.filesize = size;
197 cf_info.file_type = wtap_file_type(wth);
199 /* File Encapsulation */
200 cf_info.file_encap = wtap_file_encap(wth);
203 cf_info.packet_count = packet;
206 cf_info.start_time = start_time;
207 cf_info.stop_time = stop_time;
208 cf_info.duration = stop_time-start_time;
210 /* Number of packet bytes */
211 cf_info.packet_bytes = bytes;
213 cf_info.data_rate = 0.0;
214 cf_info.packet_rate = 0.0;
215 cf_info.packet_size = 0.0;
218 if (cf_info.duration > 0.0) {
219 cf_info.data_rate = (double)bytes / (stop_time-start_time); /* Data rate per second */
220 cf_info.packet_rate = (double)packet / (stop_time-start_time); /* packet rate per second */
222 cf_info.packet_size = (double)bytes / packet; /* Avg packet size */
225 printf("File name: %s\n", filename);
226 print_stats(&cf_info);
232 usage(gboolean is_error)
238 /* XXX - add capinfos header info here */
244 fprintf(output, "Capinfos %s"
246 " (" SVNVERSION " from " SVNPATH ")"
249 fprintf(output, "Prints information about capture files.\n");
250 fprintf(output, "See http://www.wireshark.org for more information.\n");
251 fprintf(output, "\n");
252 fprintf(output, "Usage: capinfos [options] <infile> ...\n");
253 fprintf(output, "\n");
254 fprintf(output, "General:\n");
255 fprintf(output, " -t display the capture file type\n");
256 fprintf(output, " -E display the capture file encapsulation\n");
257 fprintf(output, "\n");
258 fprintf(output, "Size:\n");
259 fprintf(output, " -c display the number of packets\n");
260 fprintf(output, " -s display the size of the file (in bytes)\n");
261 fprintf(output, " -d display the total length of all packets (in bytes)\n");
262 fprintf(output, "\n");
263 fprintf(output, "Time:\n");
264 fprintf(output, " -u display the capture duration (in seconds)\n");
265 fprintf(output, " -a display the capture start time\n");
266 fprintf(output, " -e display the capture end time\n");
267 fprintf(output, "\n");
268 fprintf(output, "Statistic:\n");
269 fprintf(output, " -y display average data rate (in bytes/sec)\n");
270 fprintf(output, " -i display average data rate (in bits/sec)\n");
271 fprintf(output, " -z display average packet size (in bytes)\n");
272 fprintf(output, " -x display average packet rate (in packets/sec)\n");
273 fprintf(output, "\n");
274 fprintf(output, "Miscellaneous:\n");
275 fprintf(output, " -h display this help and exit\n");
276 fprintf(output, "\n");
277 fprintf(output, "If no options are given the default is to display all infos\n");
282 * Don't report failures to load plugins because most (non-wiretap) plugins
283 * *should* fail to load (because we're not linked against libwireshark and
284 * dissector plugins need libwireshark).
287 failure_message(const char *msg_format _U_, va_list ap _U_)
295 main(int argc, char *argv[])
304 char* init_progfile_dir_error;
308 * Get credential information for later use.
310 get_credential_info();
313 /* Register wiretap plugins */
315 if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
316 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
317 g_free(init_progfile_dir_error);
319 init_report_err(failure_message,NULL,NULL,NULL);
324 /* Process the options */
326 while ((opt = getopt(argc, argv, "tEcsduaeyizvhx")) !=-1) {
331 cap_file_type = TRUE;
335 cap_file_encap = TRUE;
339 cap_packet_count = TRUE;
343 cap_file_size = TRUE;
347 cap_data_size = TRUE;
355 cap_start_time = TRUE;
363 cap_data_rate_byte = TRUE;
367 cap_data_rate_bit = TRUE;
371 cap_packet_size = TRUE;
375 cap_packet_rate = TRUE;
383 case '?': /* Bad flag - print usage message */
392 /* If no arguments were given, by default display all statistics */
393 cap_file_type = TRUE;
394 cap_file_encap = TRUE;
395 cap_packet_count = TRUE;
396 cap_file_size = TRUE;
397 cap_data_size = TRUE;
399 cap_start_time = TRUE;
402 cap_data_rate_byte = TRUE;
403 cap_data_rate_bit = TRUE;
404 cap_packet_size = TRUE;
405 cap_packet_rate = TRUE;
408 if ((argc - optind) < 1) {
413 for (opt = optind; opt < argc; opt++) {
415 wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
418 fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
422 case WTAP_ERR_UNSUPPORTED:
423 case WTAP_ERR_UNSUPPORTED_ENCAP:
424 case WTAP_ERR_BAD_RECORD:
425 fprintf(stderr, "(%s)\n", err_info);
434 status = process_cap_file(wth, argv[opt]);