Removed some more "statement not reached" warnings.
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <errno.h>
36
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40
41 #ifdef HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #endif
44
45 #include <glib.h>
46
47 #include <epan/packet.h>
48 #include <epan/filesystem.h>
49 #include <epan/plugins.h>
50 #include <epan/report_err.h>
51 #include "wtap.h"
52
53 #ifdef NEED_GETOPT_H
54 #include "getopt.h"
55 #endif
56
57 static gboolean cap_file_type = FALSE;      /* Do not report capture type     */
58 static gboolean cap_file_encap = FALSE;     /* Do not report encapsulation    */
59 static gboolean cap_packet_count = FALSE;   /* Do not produce packet count    */
60 static gboolean cap_file_size = FALSE;      /* Do not report file size        */
61 static gboolean cap_data_size = FALSE;      /* Do not report packet byte size */
62 static gboolean cap_duration = FALSE;       /* Do not report capture duration */
63 static gboolean cap_start_time = FALSE;
64 static gboolean cap_end_time = FALSE;
65
66 static gboolean cap_data_rate_byte = FALSE;
67 static gboolean cap_data_rate_bit = FALSE;
68 static gboolean cap_packet_size = FALSE;
69
70
71 typedef struct _capture_info {
72         const char              *filename;
73         guint16                 file_type;
74         int                     file_encap;
75         gint64                  filesize;
76         guint64                 packet_bytes;
77         double                  start_time;
78         double                  stop_time;
79         guint32                 packet_count;
80         gboolean                snap_set;
81         guint32                 snaplen;
82         gboolean                drops_known;
83         guint32                 drop_count;
84
85         double                  duration;
86         double                  packet_rate;
87         double                  packet_size;
88         double                  data_rate;              /* in bytes */
89 } capture_info;
90
91 static double
92 secs_nsecs(const struct wtap_nstime * nstime)
93 {
94   return (nstime->nsecs / 1000000000.0) + (double)nstime->secs;
95 }
96
97 static void
98 print_stats(capture_info *cf_info)
99 {
100   const gchar           *file_type_string, *file_encap_string;
101   time_t                start_time_t;
102   time_t                stop_time_t;
103
104   /* Build printable strings for various stats */
105   file_type_string = wtap_file_type_string(cf_info->file_type);
106   file_encap_string = wtap_encap_string(cf_info->file_encap);
107   start_time_t = (time_t)cf_info->start_time;
108   stop_time_t = (time_t)cf_info->stop_time;
109
110   if (cap_file_type) printf("File type: %s\n", file_type_string);
111   if (cap_file_encap) printf("File encapsulation: %s\n", file_encap_string);
112   if (cap_packet_count) printf("Number of packets: %u \n", cf_info->packet_count);
113   if (cap_file_size) printf("File size: %" G_GINT64_MODIFIER "d bytes\n", cf_info->filesize);
114   if (cap_data_size) printf("Data size: %" G_GINT64_MODIFIER "u bytes\n", cf_info->packet_bytes);
115   if (cap_duration) printf("Capture duration: %f seconds\n", cf_info->duration);
116   if (cap_start_time) printf("Start time: %s", (cf_info->packet_count>0) ? ctime (&start_time_t) : "n/a\n");
117   if (cap_end_time) printf("End time: %s",     (cf_info->packet_count>0) ? ctime (&stop_time_t)  : "n/a\n");
118   if (cap_data_rate_byte) printf("Data rate: %.2f bytes/s\n", cf_info->data_rate);
119   if (cap_data_rate_bit) printf("Data rate: %.2f bits/s\n", cf_info->data_rate*8);
120   if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
121 }
122
123 static int
124 process_cap_file(wtap *wth, const char *filename)
125 {
126   int                   err;
127   gchar                 *err_info;
128   gint64                size;
129   gint64                data_offset;
130
131   guint32               packet = 0;
132   gint64                bytes = 0;
133   const struct wtap_pkthdr *phdr;
134   capture_info  cf_info;
135   double                start_time = 0;
136   double                stop_time = 0;
137   double                cur_time = 0;
138
139   /* Tally up data that we need to parse through the file to find */
140   while (wtap_read(wth, &err, &err_info, &data_offset))  {
141     phdr = wtap_phdr(wth);
142     cur_time = secs_nsecs(&phdr->ts);
143     if(packet==0) {
144       start_time = cur_time;
145       stop_time = cur_time;
146     }
147     if (cur_time < start_time) {
148       start_time = cur_time;
149     }
150     if (cur_time > stop_time) {
151       stop_time = cur_time;
152     }
153     bytes+=phdr->len;
154     packet++;
155   }
156
157   if (err != 0) {
158     fprintf(stderr,
159             "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
160             packet, filename, wtap_strerror(err));
161     switch (err) {
162
163     case WTAP_ERR_UNSUPPORTED:
164     case WTAP_ERR_UNSUPPORTED_ENCAP:
165     case WTAP_ERR_BAD_RECORD:
166       fprintf(stderr, "(%s)\n", err_info);
167       break;
168     }
169     return 1;
170   }
171
172   /* File size */
173   size = wtap_file_size(wth, &err);
174   if (size == -1) {
175     fprintf(stderr,
176             "capinfos: Can't get size of \"%s\": %s.\n",
177             filename, strerror(err));
178     return 1;
179   }
180
181   cf_info.filesize = size;
182
183   /* File Type */
184   cf_info.file_type = wtap_file_type(wth);
185
186   /* File Encapsulation */
187   cf_info.file_encap = wtap_file_encap(wth);
188
189   /* # of packets */
190   cf_info.packet_count = packet;
191
192   /* File Times */
193   cf_info.start_time = start_time;
194   cf_info.stop_time = stop_time;
195   cf_info.duration = stop_time-start_time;
196
197   /* Number of packet bytes */
198   cf_info.packet_bytes = bytes;
199
200   if (packet > 0) {
201     cf_info.data_rate   = (double)bytes / (stop_time-start_time);  /* Data rate per second */
202     cf_info.packet_size = (double)bytes / packet;                  /* Avg packet size      */
203   }
204   else {
205     cf_info.data_rate   = 0.0;
206     cf_info.packet_size = 0.0;
207   }
208
209   printf("File name: %s\n", filename);
210   print_stats(&cf_info);
211
212   return 0;
213 }
214
215 static void usage(gboolean is_error)
216 {
217   FILE *output;
218
219   if (!is_error) {
220     output = stdout;
221     /* XXX - add capinfos header info here */
222   }
223   else {
224     output = stderr;
225   }
226
227   fprintf(output, "Capinfos %s"
228 #ifdef SVNVERSION
229           " (" SVNVERSION ")"
230 #endif
231           "\n", VERSION);
232   fprintf(output, "Prints information about capture files.\n");
233   fprintf(output, "See http://www.wireshark.org for more information.\n");
234   fprintf(output, "\n");
235   fprintf(output, "Usage: capinfos [options] <infile> ...\n");
236   fprintf(output, "\n");
237   fprintf(output, "General:\n");
238   fprintf(output, "  -t display the capture file type\n");
239   fprintf(output, "  -E display the capture file encapsulation\n");
240   fprintf(output, "\n");
241   fprintf(output, "Size:\n");
242   fprintf(output, "  -c display the number of packets\n");
243   fprintf(output, "  -s display the size of the file (in bytes)\n");
244   fprintf(output, "  -d display the total length of all packets (in bytes)\n");
245   fprintf(output, "\n");
246   fprintf(output, "Time:\n");
247   fprintf(output, "  -u display the capture duration (in seconds) \n");
248   fprintf(output, "  -a display the capture start time\n");
249   fprintf(output, "  -e display the capture end time\n");
250   fprintf(output, "\n");
251   fprintf(output, "Statistic:\n");
252   fprintf(output, "  -y display average data rate (in bytes/s)\n");
253   fprintf(output, "  -i display average data rate (in bits/s)\n");
254   fprintf(output, "  -z display average packet size (in bytes)\n");
255   fprintf(output, "\n");
256   fprintf(output, "Miscellaneous:\n");
257   fprintf(output, "  -h display this help and exit\n");
258   fprintf(output, "\n");
259   fprintf(output, "If no options are given, default is to display all infos\n");
260 }
261
262 /*
263  * Errors are reported with a console message.
264  */
265 static void
266 failure_message(const char *msg_format, va_list ap)
267 {
268         fprintf(stderr, "capinfos: ");
269         vfprintf(stderr, msg_format, ap);
270         fprintf(stderr, "\n");
271 }
272
273
274 int main(int argc, char *argv[])
275 {
276   wtap *wth;
277   int err;
278   gchar *err_info;
279   extern char *optarg;
280   extern int optind;
281   int opt;
282   int status = 0;
283 #ifdef HAVE_PLUGINS
284   char* init_progfile_dir_error;
285
286   /* Register wiretap plugins */
287
288     if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
289                 g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
290                 g_free(init_progfile_dir_error);
291     } else {
292                 init_report_err(failure_message,NULL,NULL);
293                 init_plugins();
294     }
295 #endif
296
297   /* Process the options */
298
299   while ((opt = getopt(argc, argv, "tEcsduaeyizvh")) !=-1) {
300
301     switch (opt) {
302
303     case 't':
304       cap_file_type = TRUE;
305       break;
306
307     case 'E':
308       cap_file_encap = TRUE;
309       break;
310
311     case 'c':
312       cap_packet_count = TRUE;
313       break;
314
315     case 's':
316       cap_file_size = TRUE;
317       break;
318
319     case 'd':
320       cap_data_size = TRUE;
321       break;
322
323     case 'u':
324       cap_duration = TRUE;
325       break;
326
327     case 'a':
328       cap_start_time = TRUE;
329       break;
330
331     case 'e':
332       cap_end_time = TRUE;
333       break;
334
335     case 'y':
336       cap_data_rate_byte = TRUE;
337       break;
338
339     case 'i':
340       cap_data_rate_bit = TRUE;
341       break;
342
343     case 'z':
344       cap_packet_size = TRUE;
345       break;
346
347     case 'h':
348       usage(FALSE);
349       exit(1);
350       break;
351
352     case '?':              /* Bad flag - print usage message */
353       usage(TRUE);
354       exit(1);
355       break;
356     }
357   }
358
359   if (optind < 2) {
360
361     /* If no arguments were given, by default display all statistics */
362     cap_file_type = TRUE;
363     cap_file_encap = TRUE;
364     cap_packet_count = TRUE;
365     cap_file_size = TRUE;
366     cap_data_size = TRUE;
367     cap_duration = TRUE;
368     cap_start_time = TRUE;
369     cap_end_time = TRUE;
370
371     cap_data_rate_byte = TRUE;
372     cap_data_rate_bit = TRUE;
373     cap_packet_size = TRUE;
374   }
375
376   if ((argc - optind) < 1) {
377     usage(TRUE);
378     exit(1);
379   }
380
381   for (opt = optind; opt < argc; opt++) {
382
383     wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);
384
385     if (!wth) {
386       fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
387         wtap_strerror(err));
388       switch (err) {
389
390       case WTAP_ERR_UNSUPPORTED:
391       case WTAP_ERR_UNSUPPORTED_ENCAP:
392       case WTAP_ERR_BAD_RECORD:
393         fprintf(stderr, "(%s)\n", err_info);
394         g_free(err_info);
395         break;
396       }
397       exit(1);
398     }
399
400     if (opt > optind)
401       printf("\n");
402     status = process_cap_file(wth, argv[opt]);
403
404     wtap_close(wth);
405     if (status)
406       exit(status);
407   }
408   return 0;
409 }
410