1 /* Combine two dump files, either by appending or by merging by timestamp
3 * $Id: mergecap.c,v 1.4 2001/07/20 07:22:40 guy 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
41 static int verbose = 0; /* Not so verbose */
44 * Structures to manage our files
46 typedef struct in_file_t {
54 typedef struct out_file_t {
62 static out_file_t out_file;
65 * Routine to write frame to output file
68 write_frame(u_char *user, const struct wtap_pkthdr *phdr, int offset,
69 union wtap_pseudo_header *pseudo_header, const u_char *buf)
71 wtap_dumper *pdh = (wtap_dumper*)user;
73 struct wtap_pkthdr snap_phdr;
76 printf("Record: %u\n", out_file.count++);
78 /* We simply write it, perhaps after truncating it; we could do other
79 * things, like modify it. */
80 if (out_file.snaplen != 0 && phdr->caplen > out_file.snaplen) {
82 snap_phdr.caplen = out_file.snaplen;
86 if (!wtap_dump(pdh, phdr, pseudo_header, buf, &err)) {
87 fprintf(stderr, "mergecap: Error writing to %s: %s\n",
88 out_file.filename, wtap_strerror(err));
95 * routine to concatenate files
98 append(int count, in_file_t in_files[], out_file_t *out_file)
103 for (i = 0; i < count; i++) {
104 if (!wtap_loop(in_files[i].wth, 0, write_frame,
105 (u_char*)out_file->pdh, &err)) {
106 fprintf(stderr, "mergecap: Error appending from %s to %s: %s\n",
107 in_files[i].filename, out_file->filename, wtap_strerror(err));
114 * returns TRUE if first argument is earlier than second
117 is_earlier(struct timeval *l, struct timeval *r) {
118 if (l->tv_sec > r->tv_sec) { /* left is later */
120 } else if (l->tv_sec < r->tv_sec) { /* left is earlier */
122 } else if (l->tv_usec > r->tv_usec) { /* tv_sec equal, l.usec later */
125 /* either one < two or one == two
126 * either way, return one
133 * returns index of earliest timestamp in set of input files
134 * or -1 if no valid files remain
137 earliest(int count, in_file_t in_files[]) {
140 struct timeval tv = {LONG_MAX, LONG_MAX};
142 for (i = 0; i < count; i++) {
143 struct wtap_pkthdr *phdr = wtap_phdr(in_files[i].wth);
145 if (in_files[i].ok && is_earlier(&(phdr->ts), &tv)) {
154 * actually merge the files
157 merge(int count, in_file_t in_files[], out_file_t *out_file)
161 /* prime the pump (read in first frame from each file) */
162 for (i = 0; i < count; i++) {
163 in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
164 &(in_files[i].data_offset));
167 /* now keep writing the earliest frame until we're out of frames */
168 while ( -1 != (i = earliest(count, in_files))) {
170 /* write out earliest frame, and fetch another from its
173 write_frame((u_char*)out_file->pdh,
174 wtap_phdr(in_files[i].wth),
175 in_files[i].data_offset,
176 wtap_pseudoheader(in_files[i].wth),
177 wtap_buf_ptr(in_files[i].wth));
178 in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
179 &(in_files[i].data_offset));
185 * Select an output frame type based on the input files
186 * From Guy: If all files have the same frame type, then use that.
187 * Otherwise select WTAP_ENCAP_PER_PACKET. If the selected
188 * output file type doesn't support per packet frame types,
189 * then the wtap_dump_open call will fail with a reasonable
193 select_frame_type(int count, in_file_t files[])
196 int selected_frame_type;
198 selected_frame_type = wtap_file_encap(files[0].wth);
200 for (i = 1; i < count; i++) {
201 int this_frame_type = wtap_file_encap(files[i].wth);
202 if (selected_frame_type != this_frame_type) {
203 selected_frame_type = WTAP_ENCAP_PER_PACKET;
205 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
206 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
207 fprintf(stderr, " %s had type %s (%s)\n",
209 wtap_encap_string(selected_frame_type),
210 wtap_encap_short_string(selected_frame_type));
211 fprintf(stderr, " %s had type %s (%s)\n",
213 wtap_encap_string(this_frame_type),
214 wtap_encap_short_string(this_frame_type));
221 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
222 wtap_encap_string(selected_frame_type),
223 wtap_encap_short_string(selected_frame_type));
226 return selected_frame_type;
231 * Close the output file
234 close_outfile(out_file_t *out_file)
237 if (!wtap_dump_close(out_file->pdh, &err)) {
238 fprintf(stderr, "mergecap: Error closing file %s: %s\n",
239 out_file->filename, wtap_strerror(err));
246 * Open the output file
248 * Return FALSE if file cannot be opened (so caller can clean up)
251 open_outfile(out_file_t *out_file, int snapshot_len)
255 fprintf(stderr, "mergecap: internal error (null out_file)\n");
259 out_file->pdh = wtap_dump_open(out_file->filename, out_file->file_type,
260 out_file->frame_type, snapshot_len, &err);
261 if (!out_file->pdh) {
262 fprintf(stderr, "mergecap: Can't open/create %s:\n", out_file->filename);
263 fprintf(stderr, " %s\n", wtap_strerror(err));
271 * Scan through input files and find maximum snapshot length
274 max_snapshot_length(int count, in_file_t in_files[])
277 int max_snapshot = 0;
279 for (i = 0; i < count; i++) {
280 if (wtap_snapshot_length(in_files[i].wth) > max_snapshot)
281 max_snapshot = wtap_snapshot_length(in_files[i].wth);
288 * Scan through and close each input file
291 close_in_files(int count, in_file_t in_files[])
294 for (i = 0; i < count; i++) {
295 wtap_close(in_files[i].wth);
301 * Scan through the arguments and open the input files
304 open_in_files(int argc, char *argv[], in_file_t *in_files[])
310 int files_size = argc * sizeof(in_file_t);
313 files = malloc(files_size);
315 fprintf(stderr, "mergecap: error allocating %d bytes of memory\n",
321 for (i = 0; i < argc; i++) {
322 files[count].filename = argv[i];
323 files[count].wth = wtap_open_offline(argv[i], &err, FALSE);
324 files[count].err = 0;
325 files[count].data_offset = 0;
326 files[count].ok = TRUE;
327 if (!files[count].wth) {
328 fprintf(stderr, "mergecap: skipping %s: %s\n", argv[i],
332 fprintf(stderr, "mergecap: %s is type %s.\n", argv[i],
333 wtap_file_type_string(wtap_file_type(files[count].wth)));
339 fprintf(stderr, "mergecap: opened %d of %d input files\n", count,
355 fprintf(stderr, "Usage: mergecap [-hva] [-s <snaplen>] [-T <encap type>]\n");
356 fprintf(stderr, " [-F <capture type>] -w <outfile> <infile> [...]\n\n");
357 fprintf(stderr, " where\t-h produces this help listing.\n");
358 fprintf(stderr, " \t-v verbose operation, default is silent\n");
359 fprintf(stderr, " \t-a files should be concatenated, not merged\n");
360 fprintf(stderr, " \t Default merges based on frame timestamps\n");
361 fprintf(stderr, " \t-s <snaplen>: truncate packets to <snaplen> bytes of data\n");
362 fprintf(stderr, " \t-w <outfile>: sets output filename to <outfile>\n");
363 fprintf(stderr, " \t-T <encap type> encapsulation type to use:\n");
364 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
365 string = wtap_encap_short_string(i);
367 fprintf(stderr, " \t %s - %s\n",
368 string, wtap_encap_string(i));
370 fprintf(stderr, " \t default is the same as the first input file\n");
371 fprintf(stderr, " \t-F <capture type> capture file type to write:\n");
372 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
373 if (wtap_dump_can_open(i))
374 fprintf(stderr, " \t %s - %s\n",
375 wtap_file_type_short_string(i), wtap_file_type_string(i));
377 fprintf(stderr, " \t default is libpcap\n");
383 main(int argc, char *argv[])
389 gboolean do_append = FALSE;
390 int in_file_count = 0;
391 in_file_t *in_files = NULL;
393 /* initialize out_file */
394 out_file.filename = NULL;
395 out_file.pdh = NULL; /* wiretap dumpfile */
396 out_file.file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
397 out_file.frame_type = -2; /* leave type alone */
398 out_file.snaplen = 0; /* no limit */
399 out_file.count = 1; /* frames output */
401 /* Process the options first */
402 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != EOF) {
406 out_file.filename = optarg;
410 do_append = !do_append;
414 out_file.frame_type = wtap_short_string_to_encap(optarg);
415 if (out_file.frame_type < 0) {
416 fprintf(stderr, "mergecap: \"%s\" is not a valid encapsulation type\n",
423 out_file.file_type = wtap_short_string_to_file_type(optarg);
424 if (out_file.file_type < 0) {
425 fprintf(stderr, "mergecap: \"%s\" is not a valid capture file type\n",
432 verbose = !verbose; /* Just invert */
436 out_file.snaplen = strtol(optarg, &p, 10);
437 if (p == optarg || *p != '\0') {
438 fprintf(stderr, "mergecap: \"%s\" is not a valid snapshot length\n",
445 fprintf(stderr, "mergecap version %s\n", VERSION);
450 case '?': /* Bad options if GNU getopt */
459 /* check for proper args; at a minimum, must have an output
460 * filename and one input file
462 in_file_count = argc - optind;
463 if (!out_file.filename) {
464 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
469 /* open the input files */
470 in_file_count = open_in_files(in_file_count, &argv[optind], &in_files);
471 if (in_file_count < 1) {
472 fprintf(stderr, "mergecap: No valid input files\n");
476 /* set the outfile frame type */
477 if (out_file.frame_type == -2)
478 out_file.frame_type = select_frame_type(in_file_count, in_files);
480 /* open the outfile */
481 if (!open_outfile(&out_file, max_snapshot_length(in_file_count, in_files))) {
482 close_in_files(in_file_count, in_files);
486 /* do the merge (or append) */
488 append(in_file_count, in_files, &out_file);
490 merge(in_file_count, in_files, &out_file);
492 close_in_files(in_file_count, in_files);
493 close_outfile(&out_file);