1 /* Combine two dump files, either by appending or by merging by timestamp
3 * $Id: mergecap.c,v 1.11 2002/08/28 21:00:06 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
37 static int verbose = 0; /* Not so verbose */
40 * Structures to manage our files
42 typedef struct in_file_t {
50 typedef struct out_file_t {
58 static out_file_t out_file;
61 * Routine to write frame to output file
64 write_frame(guchar *user, const struct wtap_pkthdr *phdr, long offset _U_,
65 union wtap_pseudo_header *pseudo_header, const guchar *buf)
67 wtap_dumper *pdh = (wtap_dumper*)user;
69 struct wtap_pkthdr snap_phdr;
72 printf("Record: %u\n", out_file.count++);
74 /* We simply write it, perhaps after truncating it; we could do other
75 * things, like modify it. */
76 if (out_file.snaplen != 0 && phdr->caplen > out_file.snaplen) {
78 snap_phdr.caplen = out_file.snaplen;
82 if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
83 fprintf(stderr, "mergecap: Error writing to %s: %s\n",
84 out_file.filename, wtap_strerror(err));
91 * routine to concatenate files
94 append(int count, in_file_t in_files[], out_file_t *out_file)
99 for (i = 0; i < count; i++) {
100 if (!wtap_loop(in_files[i].wth, 0, write_frame,
101 (guchar*)out_file->pdh, &err)) {
102 fprintf(stderr, "mergecap: Error appending from %s to %s: %s\n",
103 in_files[i].filename, out_file->filename, wtap_strerror(err));
110 * returns TRUE if first argument is earlier than second
113 is_earlier(struct timeval *l, struct timeval *r) {
114 if (l->tv_sec > r->tv_sec) { /* left is later */
116 } else if (l->tv_sec < r->tv_sec) { /* left is earlier */
118 } else if (l->tv_usec > r->tv_usec) { /* tv_sec equal, l.usec later */
121 /* either one < two or one == two
122 * either way, return one
129 * returns index of earliest timestamp in set of input files
130 * or -1 if no valid files remain
133 earliest(int count, in_file_t in_files[]) {
136 struct timeval tv = {LONG_MAX, LONG_MAX};
138 for (i = 0; i < count; i++) {
139 struct wtap_pkthdr *phdr = wtap_phdr(in_files[i].wth);
141 if (in_files[i].ok && is_earlier(&(phdr->ts), &tv)) {
150 * actually merge the files
153 merge(int count, in_file_t in_files[], out_file_t *out_file)
157 /* prime the pump (read in first frame from each file) */
158 for (i = 0; i < count; i++) {
159 in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
160 &(in_files[i].data_offset));
163 /* now keep writing the earliest frame until we're out of frames */
164 while ( -1 != (i = earliest(count, in_files))) {
166 /* write out earliest frame, and fetch another from its
169 write_frame((guchar*)out_file->pdh,
170 wtap_phdr(in_files[i].wth),
171 in_files[i].data_offset,
172 wtap_pseudoheader(in_files[i].wth),
173 wtap_buf_ptr(in_files[i].wth));
174 in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
175 &(in_files[i].data_offset));
181 * Select an output frame type based on the input files
182 * From Guy: If all files have the same frame type, then use that.
183 * Otherwise select WTAP_ENCAP_PER_PACKET. If the selected
184 * output file type doesn't support per packet frame types,
185 * then the wtap_dump_open call will fail with a reasonable
189 select_frame_type(int count, in_file_t files[])
192 int selected_frame_type;
194 selected_frame_type = wtap_file_encap(files[0].wth);
196 for (i = 1; i < count; i++) {
197 int this_frame_type = wtap_file_encap(files[i].wth);
198 if (selected_frame_type != this_frame_type) {
199 selected_frame_type = WTAP_ENCAP_PER_PACKET;
201 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
202 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
203 fprintf(stderr, " %s had type %s (%s)\n",
205 wtap_encap_string(selected_frame_type),
206 wtap_encap_short_string(selected_frame_type));
207 fprintf(stderr, " %s had type %s (%s)\n",
209 wtap_encap_string(this_frame_type),
210 wtap_encap_short_string(this_frame_type));
217 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
218 wtap_encap_string(selected_frame_type),
219 wtap_encap_short_string(selected_frame_type));
222 return selected_frame_type;
227 * Close the output file
230 close_outfile(out_file_t *out_file)
233 if (!wtap_dump_close(out_file->pdh, &err)) {
234 fprintf(stderr, "mergecap: Error closing file %s: %s\n",
235 out_file->filename, wtap_strerror(err));
242 * Open the output file
244 * Return FALSE if file cannot be opened (so caller can clean up)
247 open_outfile(out_file_t *out_file, int snapshot_len)
251 fprintf(stderr, "mergecap: internal error (null out_file)\n");
255 out_file->pdh = wtap_dump_open(out_file->filename, out_file->file_type,
256 out_file->frame_type, snapshot_len, &err);
257 if (!out_file->pdh) {
258 fprintf(stderr, "mergecap: Can't open/create %s:\n", out_file->filename);
259 fprintf(stderr, " %s\n", wtap_strerror(err));
267 * Scan through input files and find maximum snapshot length
270 max_snapshot_length(int count, in_file_t in_files[])
273 int max_snapshot = 0;
276 for (i = 0; i < count; i++) {
277 snapshot_length = wtap_snapshot_length(in_files[i].wth);
278 if (snapshot_length == 0) {
279 /* Snapshot length of input file not known. */
280 snapshot_length = WTAP_MAX_PACKET_SIZE;
282 if (snapshot_length > max_snapshot)
283 max_snapshot = snapshot_length;
290 * Scan through and close each input file
293 close_in_files(int count, in_file_t in_files[])
296 for (i = 0; i < count; i++) {
297 wtap_close(in_files[i].wth);
303 * Scan through the arguments and open the input files
306 open_in_files(int argc, char *argv[], in_file_t *in_files[])
312 int files_size = argc * sizeof(in_file_t);
315 files = malloc(files_size);
317 fprintf(stderr, "mergecap: error allocating %d bytes of memory\n",
323 for (i = 0; i < argc; i++) {
324 files[count].filename = argv[i];
325 files[count].wth = wtap_open_offline(argv[i], &err, FALSE);
326 files[count].err = 0;
327 files[count].data_offset = 0;
328 files[count].ok = TRUE;
329 if (!files[count].wth) {
330 fprintf(stderr, "mergecap: skipping %s: %s\n", argv[i],
334 fprintf(stderr, "mergecap: %s is type %s.\n", argv[i],
335 wtap_file_type_string(wtap_file_type(files[count].wth)));
341 fprintf(stderr, "mergecap: opened %d of %d input files\n", count,
357 fprintf(stderr, "Usage: mergecap [-hva] [-s <snaplen>] [-T <encap type>]\n");
358 fprintf(stderr, " [-F <capture type>] -w <outfile> <infile> [...]\n\n");
359 fprintf(stderr, " where\t-h produces this help listing.\n");
360 fprintf(stderr, " \t-v verbose operation, default is silent\n");
361 fprintf(stderr, " \t-a files should be concatenated, not merged\n");
362 fprintf(stderr, " \t Default merges based on frame timestamps\n");
363 fprintf(stderr, " \t-s <snaplen>: truncate packets to <snaplen> bytes of data\n");
364 fprintf(stderr, " \t-w <outfile>: sets output filename to <outfile>\n");
365 fprintf(stderr, " \t-T <encap type> encapsulation type to use:\n");
366 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
367 string = wtap_encap_short_string(i);
369 fprintf(stderr, " \t %s - %s\n",
370 string, wtap_encap_string(i));
372 fprintf(stderr, " \t default is the same as the first input file\n");
373 fprintf(stderr, " \t-F <capture type> capture file type to write:\n");
374 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
375 if (wtap_dump_can_open(i))
376 fprintf(stderr, " \t %s - %s\n",
377 wtap_file_type_short_string(i), wtap_file_type_string(i));
379 fprintf(stderr, " \t default is libpcap\n");
385 main(int argc, char *argv[])
391 gboolean do_append = FALSE;
392 int in_file_count = 0;
393 in_file_t *in_files = NULL;
395 /* initialize out_file */
396 out_file.filename = NULL;
397 out_file.pdh = NULL; /* wiretap dumpfile */
398 out_file.file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
399 out_file.frame_type = -2; /* leave type alone */
400 out_file.snaplen = 0; /* no limit */
401 out_file.count = 1; /* frames output */
403 /* Process the options first */
404 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
408 out_file.filename = optarg;
412 do_append = !do_append;
416 out_file.frame_type = wtap_short_string_to_encap(optarg);
417 if (out_file.frame_type < 0) {
418 fprintf(stderr, "mergecap: \"%s\" is not a valid encapsulation type\n",
425 out_file.file_type = wtap_short_string_to_file_type(optarg);
426 if (out_file.file_type < 0) {
427 fprintf(stderr, "mergecap: \"%s\" is not a valid capture file type\n",
434 verbose = !verbose; /* Just invert */
438 out_file.snaplen = strtol(optarg, &p, 10);
439 if (p == optarg || *p != '\0') {
440 fprintf(stderr, "mergecap: \"%s\" is not a valid snapshot length\n",
447 fprintf(stderr, "mergecap version %s\n", VERSION);
452 case '?': /* Bad options if GNU getopt */
461 /* check for proper args; at a minimum, must have an output
462 * filename and one input file
464 in_file_count = argc - optind;
465 if (!out_file.filename) {
466 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
471 /* open the input files */
472 in_file_count = open_in_files(in_file_count, &argv[optind], &in_files);
473 if (in_file_count < 1) {
474 fprintf(stderr, "mergecap: No valid input files\n");
478 /* set the outfile frame type */
479 if (out_file.frame_type == -2)
480 out_file.frame_type = select_frame_type(in_file_count, in_files);
482 /* open the outfile */
483 if (!open_outfile(&out_file, max_snapshot_length(in_file_count, in_files))) {
484 close_in_files(in_file_count, in_files);
488 /* do the merge (or append) */
490 append(in_file_count, in_files, &out_file);
492 merge(in_file_count, in_files, &out_file);
494 close_in_files(in_file_count, in_files);
495 close_outfile(&out_file);