aboutsummaryrefslogtreecommitdiffstats
path: root/capinfos.c
blob: fc398bf41e91b1522e96b83ba9d256b609761f20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* capinfos.c
 * Reports capture file information including # of packets, duration, others
 *
 * Copyright 2004 Ian Schorr
 *
 * $Id$
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#include <glib.h>

#include <epan/packet.h>
#include "wtap.h"

#ifdef NEED_GETOPT_H
#include "getopt.h"
#endif

static gboolean cap_file_type = FALSE;      /* Do not report capture type     */
static gboolean cap_packet_count = FALSE;   /* Do not produce packet count    */
static gboolean cap_file_size = FALSE;      /* Do not report file size        */
static gboolean cap_data_size = FALSE;      /* Do not report packet byte size */
static gboolean cap_duration = FALSE;       /* Do not report capture duration */
static gboolean cap_start_time = FALSE;
static gboolean cap_end_time = FALSE;

static gboolean cap_data_rate_byte = FALSE;
static gboolean cap_data_rate_bit = FALSE;
static gboolean cap_packet_size = FALSE;


typedef struct _capture_info {
	const char		*filename;
	guint16			file_type;
	guint64			filesize;
	guint64			packet_bytes;
	double			start_time;
	double			stop_time;
	guint32			packet_count;
	gboolean		snap_set;
	guint32			snaplen;
	gboolean		drops_known;
	guint32			drop_count;
	
	double			duration;
	double			packet_rate;
	double			packet_size;
	double			data_rate;		/* in bytes */
} capture_info;

static double
secs_usecs(guint32 s, guint32 us)
{
  return (us / 1000000.0) + (double)s;
}

static void
print_stats(capture_info *cf_info)
{
  const gchar		*file_type_string;
  time_t		start_time_t;
  time_t		stop_time_t;

  /* Build printable strings for various stats */
  file_type_string = wtap_file_type_string(cf_info->file_type);
  start_time_t = (time_t)cf_info->start_time;
  stop_time_t = (time_t)cf_info->stop_time;

  if (cap_file_type) printf("File type: %s\n", file_type_string);
  if (cap_packet_count) printf("Number of packets: %u \n", cf_info->packet_count);
  if (cap_file_size) printf("File size: %" PRIu64 " bytes\n", cf_info->filesize);
  if (cap_data_size) printf("Data size: %" PRIu64 " bytes\n", cf_info->packet_bytes);
  if (cap_duration) printf("Capture duration: %f seconds\n", cf_info->duration);
  if (cap_start_time) printf("Start time: %s", ctime (&start_time_t));
  if (cap_end_time) printf("End time: %s", ctime (&stop_time_t));
  if (cap_data_rate_byte) printf("Data rate: %.2f bytes/s\n", cf_info->data_rate);
  if (cap_data_rate_bit) printf("Data rate: %.2f bits/s\n", cf_info->data_rate*8);
  if (cap_packet_size) printf("Average packet size: %.2f bytes\n", cf_info->packet_size);
}

static int 
process_cap_file(wtap *wth, const char *filename)
{
  int			err;
  gchar			*err_info;
  struct stat   cf_stat;
  long  		data_offset;
  
  guint32		packet = 0;
  gint64		bytes = 0;
  const struct wtap_pkthdr *phdr;
  capture_info  cf_info;
  double		start_time = 0;
  double		stop_time = 0;
  double		cur_time = 0;
  
  /* Tally up data that we need to parse through the file to find */
  while (wtap_read(wth, &err, &err_info, &data_offset))  {
    phdr = wtap_phdr(wth);
    cur_time = secs_usecs(phdr->ts.tv_sec, phdr->ts.tv_usec);
    if(packet==0) {
      start_time = cur_time;
      stop_time = cur_time;
    }
    if (cur_time < start_time) {
      start_time = cur_time;
    }
    if (cur_time > stop_time) {
      stop_time = cur_time;
    }
    bytes+=phdr->len;
    packet++;
  }
  
  if (err != 0) {
    fprintf(stderr,
            "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n",
	    packet, filename, wtap_strerror(err));
    switch (err) {

    case WTAP_ERR_UNSUPPORTED:
    case WTAP_ERR_UNSUPPORTED_ENCAP:
    case WTAP_ERR_BAD_RECORD:
      fprintf(stderr, "(%s)\n", err_info);
      break;
    }
    return 1;
  }

  /* File size */
  if (fstat(wtap_fd(wth), &cf_stat) < 0) {
    fprintf(stderr,
            "capinfos: Can't fstat \"%s\": %s.\n",
	    filename, strerror(errno));
    return 1;
  }
  
  cf_info.filesize = cf_stat.st_size;
  
  /* File Type */
  cf_info.file_type = wtap_file_type(wth);
  
  /* # of packets */
  cf_info.packet_count = packet;
  
  /* File Times */
  cf_info.start_time = start_time;
  cf_info.stop_time = stop_time;
  cf_info.duration = stop_time-start_time;

  /* Number of packet bytes */
  cf_info.packet_bytes = bytes;
  
  /* Data rate per second */
  cf_info.data_rate = (double)bytes / (stop_time-start_time);
  
  /* Avg packet size */
  cf_info.packet_size = (double)bytes/packet;
  
  printf("File name: %s\n", filename);
  print_stats(&cf_info);

  return 0;
}

static void usage(gboolean is_error)
{
  FILE *output;
  
  if (!is_error) {
    output = stdout;
    /* XXX - add capinfos header info here */
  }
  else {
    output = stderr;
  }


  fprintf(output, "Usage: capinfos [-t] [-c] [-s] [-d] [-u] [-a] [-e] [-y]\n");
  fprintf(output, "                [-i] [-z] [-h] <capfile>\n");
  fprintf(output, "  where\t-t display the capture type of <capfile>\n");
  fprintf(output, "       \t-c count the number of packets\n");
  fprintf(output, "       \t-s display the size of the file \n");
  fprintf(output, "       \t-d display the total length of all packets in the file\n");
  fprintf(output, "       \t   (in bytes)\n");
  fprintf(output, "       \t-u display the capture duration (in seconds) \n");
  fprintf(output, "       \t-a display the capture start time\n");
  fprintf(output, "       \t-e display the capture end time\n");
  fprintf(output, "       \t-y display average data rate (in bytes)\n");
  fprintf(output, "       \t-i display average data rate (in bits)\n");
  fprintf(output, "       \t-z display average packet size (in bytes)\n");
  fprintf(output, "       \t-h produces this help listing.\n");
  fprintf(output, "\n      \t    If no data flags are given, default is to display all statistics\n");
}

int main(int argc, char *argv[])
{
  wtap *wth;
  int err;
  gchar *err_info;
  extern char *optarg;
  extern int optind;
  int opt;
  int status = 0;

  /* Process the options first */

  while ((opt = getopt(argc, argv, "tcsduaeyizvh")) !=-1) {

    switch (opt) {

    case 't':
      cap_file_type = TRUE;
      break;

    case 'c':
      cap_packet_count = TRUE;
      break;

    case 's':
      cap_file_size = TRUE;
      break;

    case 'd':
      cap_data_size = TRUE;
      break;

    case 'u':
      cap_duration = TRUE;
      break;

    case 'a':
      cap_start_time = TRUE;
      break;

    case 'e':
      cap_end_time = TRUE;
      break;

    case 'y':
      cap_data_rate_byte = TRUE;
      break;

    case 'i':
      cap_data_rate_bit = TRUE;
      break;

    case 'z':
      cap_packet_size = TRUE;
      break;

    case 'h':
      usage(FALSE);
      exit(1);
      break;

    case '?':              /* Bad flag - print usage message */
      usage(TRUE);
      exit(1);
      break;
    }
  }

  if (optind < 2) {

    /* If no arguments were given, by default display all statistics */
    cap_file_type = TRUE;      
    cap_packet_count = TRUE;   
    cap_file_size = TRUE;      
    cap_data_size = TRUE;      
    cap_duration = TRUE;       
    cap_start_time = TRUE;
    cap_end_time = TRUE;

    cap_data_rate_byte = TRUE;
    cap_data_rate_bit = TRUE;
    cap_packet_size = TRUE;
  }
  
  if ((argc - optind) < 1) {
    usage(TRUE);
    exit(1);
  }

  for (opt = optind; opt < argc; opt++) {
  
    wth = wtap_open_offline(argv[opt], &err, &err_info, FALSE);

    if (!wth) {
      fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt],
	wtap_strerror(err));
      switch (err) {

      case WTAP_ERR_UNSUPPORTED:
      case WTAP_ERR_UNSUPPORTED_ENCAP:
      case WTAP_ERR_BAD_RECORD:
        fprintf(stderr, "(%s)\n", err_info);
        g_free(err_info);
        break;
      }
      exit(1);
    }

    if (opt > optind)
      printf("\n");
    status = process_cap_file(wth, argv[opt]);
  
    wtap_close(wth);
    if (status)
      exit(status);
  }
  return 0;
}