1 /* Combine two dump files, either by appending or by merging by timestamp
3 * $Id: merge.c,v 1.1 2004/06/17 21:53:25 ulfl 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));
95 append_loop(wtap *wth, int count, wtap_handler callback, guchar* user, int *err,
101 /* Start by clearing error flag */
104 while ( (wtap_read(wth, err, err_info, &data_offset)) ) {
105 if(!write_frame(user, wtap_phdr(wth), data_offset,
106 wtap_pseudoheader(wth), wtap_buf_ptr(wth)))
108 if (count > 0 && ++loop >= count)
113 return TRUE; /* success */
115 return FALSE; /* failure */
122 * routine to concatenate files
125 append_files(int count, in_file_t in_files[], out_file_t *out_file)
131 for (i = 0; i < count; i++) {
132 if (!append_loop(in_files[i].wth, 0, write_frame,
133 (guchar*)out_file->pdh, &err, &err_info)) {
134 fprintf(stderr, "mergecap: Error appending %s to %s: %s\n",
135 in_files[i].filename, out_file->filename, wtap_strerror(err));
138 case WTAP_ERR_UNSUPPORTED:
139 case WTAP_ERR_UNSUPPORTED_ENCAP:
140 case WTAP_ERR_BAD_RECORD:
141 fprintf(stderr, "(%s)\n", err_info);
151 * returns TRUE if first argument is earlier than second
154 is_earlier(struct timeval *l, struct timeval *r) {
155 if (l->tv_sec > r->tv_sec) { /* left is later */
157 } else if (l->tv_sec < r->tv_sec) { /* left is earlier */
159 } else if (l->tv_usec > r->tv_usec) { /* tv_sec equal, l.usec later */
162 /* either one < two or one == two
163 * either way, return one
170 * returns index of earliest timestamp in set of input files
171 * or -1 if no valid files remain
174 earliest(int count, in_file_t in_files[]) {
177 struct timeval tv = {LONG_MAX, LONG_MAX};
179 for (i = 0; i < count; i++) {
180 struct wtap_pkthdr *phdr = wtap_phdr(in_files[i].wth);
182 if (in_files[i].ok && is_earlier(&(phdr->ts), &tv)) {
191 * actually merge the files
194 merge(int count, in_file_t in_files[], out_file_t *out_file)
198 /* prime the pump (read in first frame from each file) */
199 for (i = 0; i < count; i++) {
200 in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
201 &(in_files[i].err_info),
202 &(in_files[i].data_offset));
205 /* now keep writing the earliest frame until we're out of frames */
206 while ( -1 != (i = earliest(count, in_files))) {
208 /* write out earliest frame, and fetch another from its
211 if(!write_frame((guchar*)out_file->pdh,
212 wtap_phdr(in_files[i].wth),
213 in_files[i].data_offset,
214 wtap_pseudoheader(in_files[i].wth),
215 wtap_buf_ptr(in_files[i].wth)))
217 in_files[i].ok = wtap_read(in_files[i].wth, &(in_files[i].err),
218 &(in_files[i].err_info),
219 &(in_files[i].data_offset));
227 * Select an output frame type based on the input files
228 * From Guy: If all files have the same frame type, then use that.
229 * Otherwise select WTAP_ENCAP_PER_PACKET. If the selected
230 * output file type doesn't support per packet frame types,
231 * then the wtap_dump_open call will fail with a reasonable
235 select_frame_type(int count, in_file_t files[])
238 int selected_frame_type;
240 selected_frame_type = wtap_file_encap(files[0].wth);
242 for (i = 1; i < count; i++) {
243 int this_frame_type = wtap_file_encap(files[i].wth);
244 if (selected_frame_type != this_frame_type) {
245 selected_frame_type = WTAP_ENCAP_PER_PACKET;
247 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
248 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
249 fprintf(stderr, " %s had type %s (%s)\n",
251 wtap_encap_string(selected_frame_type),
252 wtap_encap_short_string(selected_frame_type));
253 fprintf(stderr, " %s had type %s (%s)\n",
255 wtap_encap_string(this_frame_type),
256 wtap_encap_short_string(this_frame_type));
263 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
264 wtap_encap_string(selected_frame_type),
265 wtap_encap_short_string(selected_frame_type));
268 return selected_frame_type;
273 * Close the output file
276 close_outfile(out_file_t *out_file)
279 if (!wtap_dump_close(out_file->pdh, &err)) {
280 fprintf(stderr, "mergecap: Error closing file %s: %s\n",
281 out_file->filename, wtap_strerror(err));
287 * Open the output file
289 * Return FALSE if file cannot be opened (so caller can clean up)
292 open_outfile(out_file_t *out_file, int snapshot_len)
297 fprintf(stderr, "mergecap: internal error (null out_file)\n");
301 /* Allow output to stdout by using - */
302 if (strncmp(out_file->filename, "-", 2) == 0)
303 out_file->filename = "";
306 out_file->pdh = wtap_dump_open(out_file->filename, out_file->file_type,
307 out_file->frame_type, snapshot_len, &err);
308 if (!out_file->pdh) {
309 fprintf(stderr, "mergecap: Can't open/create %s:\n", out_file->filename);
310 fprintf(stderr, " %s\n", wtap_strerror(err));
318 * Scan through input files and find maximum snapshot length
321 max_snapshot_length(int count, in_file_t in_files[])
324 int max_snapshot = 0;
327 for (i = 0; i < count; i++) {
328 snapshot_length = wtap_snapshot_length(in_files[i].wth);
329 if (snapshot_length == 0) {
330 /* Snapshot length of input file not known. */
331 snapshot_length = WTAP_MAX_PACKET_SIZE;
333 if (snapshot_length > max_snapshot)
334 max_snapshot = snapshot_length;
341 * Scan through and close each input file
344 close_in_files(int count, in_file_t in_files[])
347 for (i = 0; i < count; i++) {
348 wtap_close(in_files[i].wth);
354 * Scan through the arguments and open the input files
357 open_in_files(int in_file_count, char *argv[], in_file_t *in_files[])
364 int files_size = in_file_count * sizeof(in_file_t);
367 files = g_malloc(files_size);
370 for (i = 0; i < in_file_count; i++) {
371 files[count].filename = argv[i];
372 files[count].wth = wtap_open_offline(argv[i], &err, &err_info, FALSE);
373 files[count].err = 0;
374 files[count].data_offset = 0;
375 files[count].ok = TRUE;
376 if (!files[count].wth) {
377 fprintf(stderr, "mergecap: skipping %s: %s\n", argv[i],
381 case WTAP_ERR_UNSUPPORTED:
382 case WTAP_ERR_UNSUPPORTED_ENCAP:
383 case WTAP_ERR_BAD_RECORD:
384 fprintf(stderr, "(%s)\n", err_info);
390 fprintf(stderr, "mergecap: %s is type %s.\n", argv[i],
391 wtap_file_type_string(wtap_file_type(files[count].wth)));
397 fprintf(stderr, "mergecap: opened %d of %d input files\n", count,
405 merge_two_files(char *out_filename, char *in_file0, char *in_file1, gboolean do_append)
409 int in_file_count = 0;
410 in_file_t *in_files = NULL;
411 char *in_filenames[2];
413 /* initialize out_file */
414 out_file.filename = out_filename;
415 out_file.pdh = NULL; /* wiretap dumpfile */
416 out_file.file_type = WTAP_FILE_PCAP; /* default to "libpcap" */
417 out_file.frame_type = -2; /* leave type alone */
418 out_file.snaplen = 0; /* no limit */
419 out_file.count = 1; /* frames output */
421 /* check for proper args; at a minimum, must have an output
422 * filename and one input file
426 in_filenames[0] = in_file0;
427 in_filenames[1] = in_file1;
429 /* open the input files */
430 in_file_count = open_in_files(in_file_count, in_filenames, &in_files);
431 if (in_file_count < 1) {
432 fprintf(stderr, "mergecap: No valid input files\n");
436 /* set the outfile frame type */
437 if (out_file.frame_type == -2)
438 out_file.frame_type = select_frame_type(in_file_count, in_files);
440 /* open the outfile */
441 if (!open_outfile(&out_file, max_snapshot_length(in_file_count, in_files))) {
442 close_in_files(in_file_count, in_files);
446 /* do the merge (or append) */
448 append_files(in_file_count, in_files, &out_file);
450 merge(in_file_count, in_files, &out_file);
452 close_in_files(in_file_count, in_files);
453 close_outfile(&out_file);