Make the "per_choice_t" and "per_sequence_t" pointer arguments pointers
[obnox/wireshark/wip.git] / capinfo.c
1 /* capinfo.c
2  * Reports capture file information including # of packets, duration, others
3  *
4  * Copyright 2004 Ian Schorr
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
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 <glib.h>
34 #include <string.h>
35
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43
44 #include <string.h>
45 #include <epan/packet.h>
46 #include "wtap.h"
47
48 #ifdef NEED_GETOPT_H
49 #include "getopt.h"
50 #endif
51
52 #ifdef HAVE_SYS_STAT_H
53 #include <sys/stat.h>
54 #endif
55
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         guint64                 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_usecs(guint32 s, guint32 us)
91 {
92   return (us / 1000000.0) + (double)s;
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: %" PRIu64 " 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", ctime (&start_time_t));
113   if (cap_end_time) printf("End time: %s", ctime (&stop_time_t));
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
120 static int 
121 process_cap_file(wtap *wth)
122 {
123   int                   err;
124   gchar                 *err_info;
125   struct stat   cf_stat;
126   long                  data_offset;
127   
128   guint32               packet = 0;
129   gint64                bytes = 0;
130   const struct wtap_pkthdr *phdr;
131   capture_info  cf_info;
132   double                start_time = 0;
133   double                stop_time = 0;
134   double                cur_time = 0;
135   
136   /* Tally up data that we need to parse through the file to find */
137   while (wtap_read(wth, &err, &err_info, &data_offset))  {
138     phdr = wtap_phdr(wth);
139     cur_time = secs_usecs(phdr->ts.tv_sec, phdr->ts.tv_usec);
140     if(packet==0) {
141       start_time = cur_time;
142       stop_time = cur_time;
143     }
144     if (cur_time < start_time) {
145       start_time = cur_time;
146     }
147     if (cur_time > stop_time) {
148       stop_time = cur_time;
149     }
150     bytes+=phdr->len;
151     packet++;
152   }
153   
154   if (err != 0) {
155     fprintf(stderr, "Error after reading %i packets\n", packet);
156     exit(1);
157   }
158
159   /* File size */
160   if (fstat(wtap_fd(wth), &cf_stat) < 0) {
161     wtap_close(wth);
162     return 1;
163   }
164   
165   cf_info.filesize = cf_stat.st_size;
166   
167   /* File Type */
168   cf_info.file_type = wtap_file_type(wth);
169   
170   /* # of packets */
171   cf_info.packet_count = packet;
172   
173   /* File Times */
174   cf_info.start_time = start_time;
175   cf_info.stop_time = stop_time;
176   cf_info.duration = stop_time-start_time;
177
178   /* Number of packet bytes */
179   cf_info.packet_bytes = bytes;
180   
181   /* Data rate per second */
182   cf_info.data_rate = (double)bytes / (stop_time-start_time);
183   
184   /* Avg packet size */
185   cf_info.packet_size = (double)bytes/packet;
186   
187   print_stats(&cf_info);
188
189 return 0;
190 }
191
192 static void usage(gboolean is_error)
193 {
194   FILE *output;
195   
196   if (!is_error) {
197     output = stdout;
198     /* XXX - add capinfo header info here */
199   }
200   else {
201     output = stderr;
202   }
203
204
205   fprintf(output, "Usage: capinfo [-t] [-c] [-s] [-d] [-u] [-a] [-e] [-y]\n");
206   fprintf(output, "               [-i] [-z] [-h] <capfile>\n");
207   fprintf(output, "  where\t-t display the capture type of <capfile>\n");
208   fprintf(output, "       \t-c count the number of packets\n");
209   fprintf(output, "       \t-s display the size of the file \n");
210   fprintf(output, "       \t-d display the total length of all packets in the file\n");
211   fprintf(output, "       \t   (in bytes)\n");
212   fprintf(output, "       \t-u display the capture duration (in seconds) \n");
213   fprintf(output, "       \t-a display the capture start time\n");
214   fprintf(output, "       \t-e display the capture end time\n");
215   fprintf(output, "       \t-y display average data rate (in bytes)\n");
216   fprintf(output, "       \t-i display average data rate (in bits)\n");
217   fprintf(output, "       \t-z display average packet size (in bytes)\n");
218   fprintf(output, "       \t-h produces this help listing.\n");
219   fprintf(output, "\n      \t    If no data flags are given, default is to display all statistics\n");
220 }
221
222 int main(int argc, char *argv[])
223 {
224   wtap *wth;
225   int err;
226   gchar *err_info;
227   extern char *optarg;
228   extern int optind;
229   int opt;
230   int status = 0;
231
232   /* Process the options first */
233
234   while ((opt = getopt(argc, argv, "tcsduaeyizvh")) !=-1) {
235
236     switch (opt) {
237
238     case 't':
239       cap_file_type = TRUE;
240       break;
241
242     case 'c':
243       cap_packet_count = TRUE;
244       break;
245
246     case 's':
247       cap_file_size = TRUE;
248       break;
249
250     case 'd':
251       cap_data_size = TRUE;
252       break;
253
254     case 'u':
255       cap_duration = TRUE;
256       break;
257
258     case 'a':
259       cap_start_time = TRUE;
260       break;
261
262     case 'e':
263       cap_end_time = TRUE;
264       break;
265
266     case 'y':
267       cap_data_rate_byte = TRUE;
268       break;
269
270     case 'i':
271       cap_data_rate_bit = TRUE;
272       break;
273
274     case 'z':
275       cap_packet_size = TRUE;
276       break;
277
278     case 'h':
279       usage(FALSE);
280       exit(1);
281       break;
282
283     case '?':              /* Bad flag - print usage message */
284       usage(TRUE);
285       exit(1);
286       break;
287     }
288   }
289
290   if (optind < 2) {
291
292     /* If no arguments were given, by default display all statistics */
293     cap_file_type = TRUE;      
294     cap_packet_count = TRUE;   
295     cap_file_size = TRUE;      
296     cap_data_size = TRUE;      
297     cap_duration = TRUE;       
298     cap_start_time = TRUE;
299     cap_end_time = TRUE;
300
301     cap_data_rate_byte = TRUE;
302     cap_data_rate_bit = TRUE;
303     cap_packet_size = TRUE;
304   }
305   
306   if ((argc - optind) < 1) {
307     usage(TRUE);
308     exit(1);
309   }
310   
311   wth = wtap_open_offline(argv[optind], &err, &err_info, FALSE);
312
313   if (!wth) {
314     fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind],
315         wtap_strerror(err));
316     switch (err) {
317
318     case WTAP_ERR_UNSUPPORTED:
319     case WTAP_ERR_UNSUPPORTED_ENCAP:
320     case WTAP_ERR_BAD_RECORD:
321       fprintf(stderr, "(%s)\n", err_info);
322       g_free(err_info);
323       break;
324     }
325     exit(1);
326   }
327
328   status = process_cap_file(wth);
329   
330   wtap_close(wth);
331   return status;
332 }