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