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