The function pointer in a "per_choice_t" or a "per_sequence_t" is to a
[obnox/wireshark/wip.git] / mergecap.c
1 /* Combine two dump files, either by appending or by merging by timestamp
2  *
3  * $Id: mergecap.c,v 1.20 2004/03/04 08:20:46 jmayer Exp $
4  *
5  * Written by Scott Renfro <scott@renfro.org> based on
6  * editcap by Richard Sharpe and Guy Harris
7  *
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <glib.h>
17
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #ifdef HAVE_SYS_TIME_H
23 #include <sys/time.h>
24 #endif
25
26 #include <string.h>
27 #include "wtap.h"
28
29 #ifdef NEED_GETOPT_H
30 #include "getopt.h"
31 #endif
32
33 #include "cvsversion.h"
34
35 /*
36  * Global variables
37  */
38 static int verbose = 0;                      /* Not so verbose         */
39
40 /*
41  * Structures to manage our files
42  */
43 typedef struct in_file_t {
44   const char *filename;
45   wtap       *wth;
46   int         err;
47   gchar      *err_info;
48   long        data_offset;
49   gboolean    ok;
50 } in_file_t;
51
52 typedef struct out_file_t {
53   const char  *filename;
54   wtap_dumper *pdh;
55   int          file_type;
56   int          frame_type;
57   unsigned int snaplen;
58   int          count;
59 } out_file_t;
60 static out_file_t out_file;
61
62 /*
63  * Routine to write frame to output file
64  */
65 static void
66 write_frame(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
67             union wtap_pseudo_header *pseudo_header, const guchar *buf)
68 {
69   wtap_dumper *pdh = (wtap_dumper*)user;
70   int err;
71   struct wtap_pkthdr snap_phdr;
72
73   if (verbose)
74     printf("Record: %u\n", out_file.count++);
75
76   /* We simply write it, perhaps after truncating it; we could do other
77    * things, like modify it. */
78   if (out_file.snaplen != 0 && phdr->caplen > out_file.snaplen) {
79     snap_phdr = *phdr;
80     snap_phdr.caplen = out_file.snaplen;
81     phdr = &snap_phdr;
82   }
83
84   if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
85     fprintf(stderr, "mergecap: Error writing to %s: %s\n",
86             out_file.filename, wtap_strerror(err));
87     exit(1);
88   }
89 }
90
91
92 /*
93  * routine to concatenate files
94  */
95 static void
96 append(int count, in_file_t in_files[], out_file_t *out_file)
97 {
98   int i;
99   int err;
100   gchar *err_info;
101
102   for (i = 0; i < count; i++) {
103     if (!wtap_loop(in_files[i].wth, 0, write_frame,
104                    (guchar*)out_file->pdh, &err, &err_info)) {
105       fprintf(stderr, "mergecap: Error reading %s to append to %s: %s\n",
106               in_files[i].filename, out_file->filename, wtap_strerror(err));
107       switch (err) {
108
109       case WTAP_ERR_UNSUPPORTED:
110       case WTAP_ERR_UNSUPPORTED_ENCAP:
111       case WTAP_ERR_BAD_RECORD:
112         fprintf(stderr, "(%s)\n", err_info);
113         break;
114       }
115     }
116   }
117 }
118
119
120 /*
121  * returns TRUE if first argument is earlier than second
122  */
123 static gboolean
124 is_earlier(struct timeval *l, struct timeval *r) {
125   if (l->tv_sec > r->tv_sec) {  /* left is later */
126     return FALSE;
127   } else if (l->tv_sec < r->tv_sec) { /* left is earlier */
128     return TRUE;
129   } else if (l->tv_usec > r->tv_usec) { /* tv_sec equal, l.usec later */
130     return FALSE;
131   }
132   /* either one < two or one == two
133    * either way, return one
134    */
135   return TRUE;
136 }
137
138
139 /*
140  * returns index of earliest timestamp in set of input files
141  * or -1 if no valid files remain
142  */
143 static int
144 earliest(int count, in_file_t in_files[]) {
145   int i;
146   int ei = -1;
147   struct timeval tv = {LONG_MAX, LONG_MAX};
148
149   for (i = 0; i < count; i++) {
150     struct wtap_pkthdr *phdr = wtap_phdr(in_files[i].wth);
151
152     if (in_files[i].ok && is_earlier(&(phdr->ts), &tv)) {
153       tv = phdr->ts;
154       ei = i;
155     }
156   }
157   return ei;
158 }
159
160 /*
161  * actually merge the files
162  */
163 static void
164 merge(int count, in_file_t in_files[], out_file_t *out_file)
165 {
166   int i;
167
168   /* prime the pump (read in first frame from each file) */
169   for (i = 0; i < count; i++) {
170     in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
171                                &(in_files[i].err_info),
172                                &(in_files[i].data_offset));
173   }
174
175   /* now keep writing the earliest frame until we're out of frames */
176   while ( -1 != (i = earliest(count, in_files))) {
177
178     /* write out earliest frame, and fetch another from its
179      * input file
180      */
181     write_frame((guchar*)out_file->pdh,
182                 wtap_phdr(in_files[i].wth),
183                 in_files[i].data_offset,
184                 wtap_pseudoheader(in_files[i].wth),
185                 wtap_buf_ptr(in_files[i].wth));
186     in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
187                                &(in_files[i].err_info),
188                                &(in_files[i].data_offset));
189   }
190 }
191
192
193 /*
194  * Select an output frame type based on the input files
195  * From Guy: If all files have the same frame type, then use that.
196  *           Otherwise select WTAP_ENCAP_PER_PACKET.  If the selected
197  *           output file type doesn't support per packet frame types,
198  *           then the wtap_dump_open call will fail with a reasonable
199  *           error condition.
200  */
201 static int
202 select_frame_type(int count, in_file_t files[])
203 {
204   int i;
205   int selected_frame_type;
206
207   selected_frame_type = wtap_file_encap(files[0].wth);
208
209   for (i = 1; i < count; i++) {
210     int this_frame_type = wtap_file_encap(files[i].wth);
211     if (selected_frame_type != this_frame_type) {
212       selected_frame_type = WTAP_ENCAP_PER_PACKET;
213       if (verbose) {
214         fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
215         fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
216         fprintf(stderr, "          %s had type %s (%s)\n",
217                 files[0].filename,
218                 wtap_encap_string(selected_frame_type),
219                 wtap_encap_short_string(selected_frame_type));
220         fprintf(stderr, "          %s had type %s (%s)\n",
221                 files[i].filename,
222                 wtap_encap_string(this_frame_type),
223                 wtap_encap_short_string(this_frame_type));
224       }
225       break;
226     }
227   }
228
229   if (verbose) {
230       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
231               wtap_encap_string(selected_frame_type),
232               wtap_encap_short_string(selected_frame_type));
233   }
234
235   return selected_frame_type;
236 }
237
238
239 /*
240  * Close the output file
241  */
242 static void
243 close_outfile(out_file_t *out_file)
244 {
245   int err;
246   if (!wtap_dump_close(out_file->pdh, &err)) {
247     fprintf(stderr, "mergecap: Error closing file %s: %s\n",
248             out_file->filename, wtap_strerror(err));
249     exit(1);
250   }
251 }
252
253
254 /*
255  * Open the output file
256  *
257  * Return FALSE if file cannot be opened (so caller can clean up)
258  */
259 static gboolean
260 open_outfile(out_file_t *out_file, int snapshot_len)
261 {
262   int err;
263   if (!out_file) {
264     fprintf(stderr, "mergecap: internal error (null out_file)\n");
265     exit(1);
266   }
267
268   /* Allow output to stdout by using - */
269   if (strncmp(out_file->filename, "-", 2) == 0)
270     out_file->filename = "";
271
272
273   out_file->pdh = wtap_dump_open(out_file->filename, out_file->file_type,
274                                  out_file->frame_type, snapshot_len, &err);
275   if (!out_file->pdh) {
276     fprintf(stderr, "mergecap: Can't open/create %s:\n", out_file->filename);
277     fprintf(stderr, "          %s\n", wtap_strerror(err));
278     return FALSE;
279   }
280   return TRUE;
281 }
282
283
284 /*
285  * Scan through input files and find maximum snapshot length
286  */
287 static int
288 max_snapshot_length(int count, in_file_t in_files[])
289 {
290   int i;
291   int max_snapshot = 0;
292   int snapshot_length;
293
294   for (i = 0; i < count; i++) {
295     snapshot_length = wtap_snapshot_length(in_files[i].wth);
296     if (snapshot_length == 0) {
297       /* Snapshot length of input file not known. */
298       snapshot_length = WTAP_MAX_PACKET_SIZE;
299     }
300     if (snapshot_length > max_snapshot)
301       max_snapshot = snapshot_length;
302   }
303   return max_snapshot;
304 }
305
306
307 /*
308  * Scan through and close each input file
309  */
310 static void
311 close_in_files(int count, in_file_t in_files[])
312 {
313   int i;
314   for (i = 0; i < count; i++) {
315     wtap_close(in_files[i].wth);
316   }
317 }
318
319
320 /*
321  * Scan through the arguments and open the input files
322  */
323 static int
324 open_in_files(int argc, char *argv[], in_file_t *in_files[])
325 {
326   int i;
327   int count = 0;
328   int err;
329   gchar *err_info;
330   in_file_t *files;
331   int files_size = argc * sizeof(in_file_t);
332
333
334   files = malloc(files_size);
335   if (!files) {
336     fprintf(stderr, "mergecap: error allocating %d bytes of memory\n",
337             files_size);
338     exit(1);
339   }
340   *in_files = files;
341
342   for (i = 0; i < argc; i++) {
343     files[count].filename    = argv[i];
344     files[count].wth         = wtap_open_offline(argv[i], &err, &err_info, FALSE);
345     files[count].err         = 0;
346     files[count].data_offset = 0;
347     files[count].ok          = TRUE;
348     if (!files[count].wth) {
349       fprintf(stderr, "mergecap: skipping %s: %s\n", argv[i],
350               wtap_strerror(err));
351       switch (err) {
352
353       case WTAP_ERR_UNSUPPORTED:
354       case WTAP_ERR_UNSUPPORTED_ENCAP:
355       case WTAP_ERR_BAD_RECORD:
356         fprintf(stderr, "(%s)\n", err_info);
357         g_free(err_info);
358         break;
359       }
360     } else {
361       if (verbose) {
362         fprintf(stderr, "mergecap: %s is type %s.\n", argv[i],
363                 wtap_file_type_string(wtap_file_type(files[count].wth)));
364       }
365       count++;
366     }
367   }
368   if (verbose)
369     fprintf(stderr, "mergecap: opened %d of %d input files\n", count,
370     argc);
371
372   return count;
373 }
374
375
376 /*
377  * Show the usage
378  */
379 static void
380 usage(void)
381 {
382   int i;
383   const char *string;
384
385   printf("Usage: mergecap [-hva] [-s <snaplen>] [-T <encap type>]\n");
386   printf("          [-F <capture type>] -w <outfile> <infile> [...]\n\n");
387   printf("  where\t-h produces this help listing.\n");
388   printf("       \t-v verbose operation, default is silent\n");
389   printf("       \t-a files should be concatenated, not merged\n");
390   printf("       \t     Default merges based on frame timestamps\n");
391   printf("       \t-s <snaplen>: truncate packets to <snaplen> bytes of data\n");
392   printf("       \t-w <outfile>: sets output filename to <outfile>\n");
393   printf("       \t-T <encap type> encapsulation type to use:\n");
394   for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
395       string = wtap_encap_short_string(i);
396       if (string != NULL)
397         printf("       \t     %s - %s\n",
398           string, wtap_encap_string(i));
399   }
400   printf("       \t     default is the same as the first input file\n");
401   printf("       \t-F <capture type> capture file type to write:\n");
402   for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
403     if (wtap_dump_can_open(i))
404       printf("       \t     %s - %s\n",
405         wtap_file_type_short_string(i), wtap_file_type_string(i));
406   }
407   printf("       \t     default is libpcap\n");
408 }
409
410
411
412 int
413 main(int argc, char *argv[])
414 {
415   extern char *optarg;
416   extern int   optind;
417   int          opt;
418   char        *p;
419   gboolean     do_append     = FALSE;
420   int          in_file_count = 0;
421   in_file_t   *in_files      = NULL;
422
423   /* initialize out_file */
424   out_file.filename   = NULL;
425   out_file.pdh        = NULL;              /* wiretap dumpfile */
426   out_file.file_type  = WTAP_FILE_PCAP;    /* default to "libpcap" */
427   out_file.frame_type = -2;                /* leave type alone */
428   out_file.snaplen    = 0;                 /* no limit */
429   out_file.count      = 1;                 /* frames output */
430
431   /* Process the options first */
432   while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
433
434     switch (opt) {
435     case 'w':
436       out_file.filename = optarg;
437       break;
438
439     case 'a':
440       do_append = !do_append;
441       break;
442
443     case 'T':
444       out_file.frame_type = wtap_short_string_to_encap(optarg);
445       if (out_file.frame_type < 0) {
446         fprintf(stderr, "mergecap: \"%s\" is not a valid encapsulation type\n",
447             optarg);
448         exit(1);
449       }
450       break;
451
452     case 'F':
453       out_file.file_type = wtap_short_string_to_file_type(optarg);
454       if (out_file.file_type < 0) {
455         fprintf(stderr, "mergecap: \"%s\" is not a valid capture file type\n",
456             optarg);
457         exit(1);
458       }
459       break;
460
461     case 'v':
462       verbose = !verbose;  /* Just invert */
463       break;
464
465     case 's':
466       out_file.snaplen = strtol(optarg, &p, 10);
467       if (p == optarg || *p != '\0') {
468         fprintf(stderr, "mergecap: \"%s\" is not a valid snapshot length\n",
469             optarg);
470         exit(1);
471       }
472       break;
473
474     case 'h':
475       printf("mergecap version %s"
476 #ifdef CVSVERSION
477           " (" CVSVERSION ")"
478 #endif
479           "\n", VERSION);
480       usage();
481       exit(0);
482       break;
483
484     case '?':              /* Bad options if GNU getopt */
485       usage();
486       exit(1);
487       break;
488
489     }
490
491   }
492
493   /* check for proper args; at a minimum, must have an output
494    * filename and one input file
495    */
496   in_file_count = argc - optind;
497   if (!out_file.filename) {
498     fprintf(stderr, "mergecap: an output filename must be set with -w\n");
499     fprintf(stderr, "          run with -h for help\n");
500     exit(1);
501   }
502
503   /* open the input files */
504   in_file_count = open_in_files(in_file_count, &argv[optind], &in_files);
505   if (in_file_count < 1) {
506     fprintf(stderr, "mergecap: No valid input files\n");
507     exit(1);
508   }
509
510   /* set the outfile frame type */
511   if (out_file.frame_type == -2)
512     out_file.frame_type = select_frame_type(in_file_count, in_files);
513
514   /* open the outfile */
515   if (!open_outfile(&out_file, max_snapshot_length(in_file_count, in_files))) {
516     close_in_files(in_file_count, in_files);
517     exit(1);
518   }
519
520   /* do the merge (or append) */
521   if (do_append)
522     append(in_file_count, in_files, &out_file);
523   else
524     merge(in_file_count, in_files, &out_file);
525
526   close_in_files(in_file_count, in_files);
527   close_outfile(&out_file);
528
529   free(in_files);
530
531   return 0;
532 }