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