1 /* Combine two dump files, either by appending or by merging by timestamp
3 * $Id: mergecap.c,v 1.20 2004/03/04 08:20:46 jmayer Exp $
5 * Written by Scott Renfro <scott@renfro.org> based on
6 * editcap by Richard Sharpe and Guy Harris
22 #ifdef HAVE_SYS_TIME_H
33 #include "cvsversion.h"
38 static int verbose = 0; /* Not so verbose */
41 * Structures to manage our files
43 typedef struct in_file_t {
52 typedef struct out_file_t {
60 static out_file_t out_file;
63 * Routine to write frame to output file
66 write_frame(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
67 union wtap_pseudo_header *pseudo_header, const guchar *buf)
69 wtap_dumper *pdh = (wtap_dumper*)user;
71 struct wtap_pkthdr snap_phdr;
74 printf("Record: %u\n", out_file.count++);
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) {
80 snap_phdr.caplen = out_file.snaplen;
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));
93 * routine to concatenate files
96 append(int count, in_file_t in_files[], out_file_t *out_file)
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));
109 case WTAP_ERR_UNSUPPORTED:
110 case WTAP_ERR_UNSUPPORTED_ENCAP:
111 case WTAP_ERR_BAD_RECORD:
112 fprintf(stderr, "(%s)\n", err_info);
121 * returns TRUE if first argument is earlier than second
124 is_earlier(struct timeval *l, struct timeval *r) {
125 if (l->tv_sec > r->tv_sec) { /* left is later */
127 } else if (l->tv_sec < r->tv_sec) { /* left is earlier */
129 } else if (l->tv_usec > r->tv_usec) { /* tv_sec equal, l.usec later */
132 /* either one < two or one == two
133 * either way, return one
140 * returns index of earliest timestamp in set of input files
141 * or -1 if no valid files remain
144 earliest(int count, in_file_t in_files[]) {
147 struct timeval tv = {LONG_MAX, LONG_MAX};
149 for (i = 0; i < count; i++) {
150 struct wtap_pkthdr *phdr = wtap_phdr(in_files[i].wth);
152 if (in_files[i].ok && is_earlier(&(phdr->ts), &tv)) {
161 * actually merge the files
164 merge(int count, in_file_t in_files[], out_file_t *out_file)
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));
175 /* now keep writing the earliest frame until we're out of frames */
176 while ( -1 != (i = earliest(count, in_files))) {
178 /* write out earliest frame, and fetch another from its
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));
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
202 select_frame_type(int count, in_file_t files[])
205 int selected_frame_type;
207 selected_frame_type = wtap_file_encap(files[0].wth);
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;
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",
218 wtap_encap_string(selected_frame_type),
219 wtap_encap_short_string(selected_frame_type));
220 fprintf(stderr, " %s had type %s (%s)\n",
222 wtap_encap_string(this_frame_type),
223 wtap_encap_short_string(this_frame_type));
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));
235 return selected_frame_type;
240 * Close the output file
243 close_outfile(out_file_t *out_file)
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));
255 * Open the output file
257 * Return FALSE if file cannot be opened (so caller can clean up)
260 open_outfile(out_file_t *out_file, int snapshot_len)
264 fprintf(stderr, "mergecap: internal error (null out_file)\n");
268 /* Allow output to stdout by using - */
269 if (strncmp(out_file->filename, "-", 2) == 0)
270 out_file->filename = "";
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));
285 * Scan through input files and find maximum snapshot length
288 max_snapshot_length(int count, in_file_t in_files[])
291 int max_snapshot = 0;
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;
300 if (snapshot_length > max_snapshot)
301 max_snapshot = snapshot_length;
308 * Scan through and close each input file
311 close_in_files(int count, in_file_t in_files[])
314 for (i = 0; i < count; i++) {
315 wtap_close(in_files[i].wth);
321 * Scan through the arguments and open the input files
324 open_in_files(int argc, char *argv[], in_file_t *in_files[])
331 int files_size = argc * sizeof(in_file_t);
334 files = malloc(files_size);
336 fprintf(stderr, "mergecap: error allocating %d bytes of memory\n",
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],
353 case WTAP_ERR_UNSUPPORTED:
354 case WTAP_ERR_UNSUPPORTED_ENCAP:
355 case WTAP_ERR_BAD_RECORD:
356 fprintf(stderr, "(%s)\n", err_info);
362 fprintf(stderr, "mergecap: %s is type %s.\n", argv[i],
363 wtap_file_type_string(wtap_file_type(files[count].wth)));
369 fprintf(stderr, "mergecap: opened %d of %d input files\n", count,
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);
397 printf(" \t %s - %s\n",
398 string, wtap_encap_string(i));
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));
407 printf(" \t default is libpcap\n");
413 main(int argc, char *argv[])
419 gboolean do_append = FALSE;
420 int in_file_count = 0;
421 in_file_t *in_files = NULL;
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 */
431 /* Process the options first */
432 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
436 out_file.filename = optarg;
440 do_append = !do_append;
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",
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",
462 verbose = !verbose; /* Just invert */
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",
475 printf("mergecap version %s"
484 case '?': /* Bad options if GNU getopt */
493 /* check for proper args; at a minimum, must have an output
494 * filename and one input file
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");
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");
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);
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);
520 /* do the merge (or append) */
522 append(in_file_count, in_files, &out_file);
524 merge(in_file_count, in_files, &out_file);
526 close_in_files(in_file_count, in_files);
527 close_outfile(&out_file);