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