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