1 /* Combine two dump files, either by appending or by merging by timestamp
5 * Written by Scott Renfro <scott@renfro.org> based on
6 * editcap by Richard Sharpe and Guy Harris
23 #ifdef HAVE_SYS_TIME_H
34 #include "svnversion.h"
36 #include "file_util.h"
43 get_natural_int(const char *string, const char *name)
48 number = strtol(string, &p, 10);
49 if (p == string || *p != '\0') {
50 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
55 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
59 if (number > INT_MAX) {
60 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
68 get_positive_int(const char *string, const char *name)
72 number = get_natural_int(string, name);
75 fprintf(stderr, "mergecap: The specified %s is zero\n",
90 fprintf(stderr, "Mergecap %s"
95 fprintf(stderr, "Merge two or more capture files into one.\n");
96 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
97 fprintf(stderr, "\n");
98 fprintf(stderr, "Usage: mergecap [options] -w <outfile|-> <infile> ...\n");
99 fprintf(stderr, "\n");
100 fprintf(stderr, "Output:\n");
101 fprintf(stderr, " -a files should be concatenated, not merged\n");
102 fprintf(stderr, " Default merges based on frame timestamps\n");
103 fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data\n");
104 fprintf(stderr, " -w <outfile|-> set the output filename to <outfile> or '-' for stdout\n");
105 fprintf(stderr, " -F <capture type> set the output file type, default is libpcap\n");
106 fprintf(stderr, " an empty \"-F\" option will list the file types\n");
107 fprintf(stderr, " -T <encap type> set the output file encapsulation type,\n");
108 fprintf(stderr, " default is the same as the first input file\n");
109 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types\n");
110 fprintf(stderr, "\n");
111 fprintf(stderr, "Miscellaneous:\n");
112 fprintf(stderr, " -h display this help and exit\n");
113 fprintf(stderr, " -v verbose output\n");
116 static void list_capture_types(void) {
119 fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
120 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
121 if (wtap_dump_can_open(i))
122 fprintf(stderr, " %s - %s\n",
123 wtap_file_type_short_string(i), wtap_file_type_string(i));
127 static void list_encap_types(void) {
131 fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
132 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
133 string = wtap_encap_short_string(i);
135 fprintf(stderr, " %s - %s\n",
136 string, wtap_encap_string(i));
141 main(int argc, char *argv[])
146 gboolean do_append = FALSE;
147 gboolean verbose = FALSE;
148 int in_file_count = 0;
150 int file_type = WTAP_FILE_PCAP; /* default to libpcap format */
153 merge_in_file_t *in_files = NULL;
156 struct wtap_pkthdr *phdr, snap_phdr;
158 int open_err, read_err, write_err, close_err;
161 char *out_filename = NULL;
162 gboolean got_read_error = FALSE, got_write_error = FALSE;
165 /* Process the options first */
166 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
170 out_filename = optarg;
174 do_append = !do_append;
178 frame_type = wtap_short_string_to_encap(optarg);
179 if (frame_type < 0) {
180 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
188 file_type = wtap_short_string_to_file_type(optarg);
190 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
192 list_capture_types();
202 snaplen = get_positive_int(optarg, "snapshot length");
210 case '?': /* Bad options if GNU getopt */
213 list_capture_types();
228 /* check for proper args; at a minimum, must have an output
229 * filename and one input file
231 in_file_count = argc - optind;
233 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
234 fprintf(stderr, " run with -h for help\n");
237 if (in_file_count < 1) {
238 fprintf(stderr, "mergecap: No input files were specified\n");
242 /* open the input files */
243 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
244 &open_err, &err_info, &err_fileno)) {
245 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
246 wtap_strerror(open_err));
249 case WTAP_ERR_UNSUPPORTED:
250 case WTAP_ERR_UNSUPPORTED_ENCAP:
251 case WTAP_ERR_BAD_RECORD:
252 fprintf(stderr, "(%s)\n", err_info);
260 for (i = 0; i < in_file_count; i++)
261 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
262 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
267 * Snapshot length not specified - default to the maximum of the
268 * snapshot lengths of the input files.
270 snaplen = merge_max_snapshot_length(in_file_count, in_files);
273 /* set the outfile frame type */
274 if (frame_type == -2) {
276 * Default to the appropriate frame type for the input files.
278 frame_type = merge_select_frame_type(in_file_count, in_files);
280 if (frame_type == WTAP_ENCAP_PER_PACKET) {
282 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
284 int first_frame_type, this_frame_type;
286 first_frame_type = wtap_file_encap(in_files[0].wth);
287 for (i = 1; i < in_file_count; i++) {
288 this_frame_type = wtap_file_encap(in_files[i].wth);
289 if (first_frame_type != this_frame_type) {
290 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
291 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
292 fprintf(stderr, " %s had type %s (%s)\n",
293 in_files[0].filename,
294 wtap_encap_string(first_frame_type),
295 wtap_encap_short_string(first_frame_type));
296 fprintf(stderr, " %s had type %s (%s)\n",
297 in_files[i].filename,
298 wtap_encap_string(this_frame_type),
299 wtap_encap_short_string(this_frame_type));
304 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
305 wtap_encap_string(frame_type),
306 wtap_encap_short_string(frame_type));
310 /* open the outfile */
311 if (strncmp(out_filename, "-", 2) == 0) {
312 /* use stdout as the outfile */
313 out_fd = 1 /*stdout*/;
315 /* open the outfile */
316 out_fd = eth_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
318 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
319 out_filename, strerror(errno));
324 /* prepare the outfile */
325 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
327 merge_close_in_files(in_file_count, in_files);
329 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
330 wtap_strerror(open_err));
334 /* do the merge (or append) */
338 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
341 wth = merge_read_packet(in_file_count, in_files, &read_err,
345 got_read_error = TRUE;
350 fprintf(stderr, "Record: %u\n", count++);
352 /* We simply write it, perhaps after truncating it; we could do other
353 * things, like modify it. */
354 phdr = wtap_phdr(wth);
355 if (snaplen != 0 && phdr->caplen > snaplen) {
357 snap_phdr.caplen = snaplen;
361 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
362 wtap_buf_ptr(wth), &write_err)) {
363 got_write_error = TRUE;
368 merge_close_in_files(in_file_count, in_files);
369 if (!got_read_error && !got_write_error) {
370 if (!wtap_dump_close(pdh, &write_err))
371 got_write_error = TRUE;
373 wtap_dump_close(pdh, &close_err);
375 if (got_read_error) {
377 * Find the file on which we got the error, and report the error.
379 for (i = 0; i < in_file_count; i++) {
380 if (in_files[i].state == GOT_ERROR) {
381 fprintf(stderr, "mergecap: Error reading %s: %s\n",
382 in_files[i].filename, wtap_strerror(read_err));
385 case WTAP_ERR_UNSUPPORTED:
386 case WTAP_ERR_UNSUPPORTED_ENCAP:
387 case WTAP_ERR_BAD_RECORD:
388 fprintf(stderr, "(%s)\n", err_info);
396 if (got_write_error) {
397 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
398 wtap_strerror(write_err));
403 return (!got_read_error && !got_write_error) ? 0 : 2;