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