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
31 #include "wsutil/wsgetopt.h"
34 #include "svnversion.h"
36 #include "wsutil/file_util.h"
43 #include <wsutil/unicode-utils.h>
47 get_natural_int(const char *string, const char *name)
52 number = (int) strtol(string, &p, 10);
53 if (p == string || *p != '\0') {
54 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
59 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
63 if (number > INT_MAX) {
64 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
72 get_positive_int(const char *string, const char *name)
76 number = get_natural_int(string, name);
79 fprintf(stderr, "mergecap: The specified %s is zero\n",
94 fprintf(stderr, "Mergecap %s"
96 " (" SVNVERSION " from " SVNPATH ")"
99 fprintf(stderr, "Merge two or more capture files into one.\n");
100 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
101 fprintf(stderr, "\n");
102 fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> ...\n");
103 fprintf(stderr, "\n");
104 fprintf(stderr, "Output:\n");
105 fprintf(stderr, " -a concatenate rather than merge files.\n");
106 fprintf(stderr, " default is to merge based on frame timestamps.\n");
107 fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
108 fprintf(stderr, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
109 fprintf(stderr, " -F <capture type> set the output file type; default is libpcap.\n");
110 fprintf(stderr, " an empty \"-F\" option will list the file types.\n");
111 fprintf(stderr, " -T <encap type> set the output file encapsulation type;\n");
112 fprintf(stderr, " default is the same as the first input file.\n");
113 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types.\n");
114 fprintf(stderr, "\n");
115 fprintf(stderr, "Miscellaneous:\n");
116 fprintf(stderr, " -h display this help and exit.\n");
117 fprintf(stderr, " -v verbose output.\n");
120 static void list_capture_types(void) {
123 fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
124 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
125 if (wtap_dump_can_open(i))
126 fprintf(stderr, " %s - %s\n",
127 wtap_file_type_short_string(i), wtap_file_type_string(i));
131 static void list_encap_types(void) {
135 fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
136 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
137 string = wtap_encap_short_string(i);
139 fprintf(stderr, " %s - %s\n",
140 string, wtap_encap_string(i));
145 main(int argc, char *argv[])
149 gboolean do_append = FALSE;
150 gboolean verbose = FALSE;
151 int in_file_count = 0;
153 #ifdef PCAP_NG_DEFAULT
154 int file_type = WTAP_FILE_PCAPNG; /* default to pcap format */
156 int file_type = WTAP_FILE_PCAP; /* default to pcapng format */
160 merge_in_file_t *in_files = NULL, *in_file;
162 struct wtap_pkthdr *phdr, snap_phdr;
164 int open_err, read_err, write_err, close_err;
167 char *out_filename = NULL;
168 gboolean got_read_error = FALSE, got_write_error = FALSE;
172 arg_list_utf_16to8(argc, argv);
175 /* Process the options first */
176 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
180 out_filename = optarg;
184 do_append = !do_append;
188 frame_type = wtap_short_string_to_encap(optarg);
189 if (frame_type < 0) {
190 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
198 file_type = wtap_short_string_to_file_type(optarg);
200 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
202 list_capture_types();
212 snaplen = get_positive_int(optarg, "snapshot length");
220 case '?': /* Bad options if GNU getopt */
223 list_capture_types();
238 /* check for proper args; at a minimum, must have an output
239 * filename and one input file
241 in_file_count = argc - optind;
243 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
244 fprintf(stderr, " run with -h for help\n");
247 if (in_file_count < 1) {
248 fprintf(stderr, "mergecap: No input files were specified\n");
252 /* open the input files */
253 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
254 &open_err, &err_info, &err_fileno)) {
255 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
256 wtap_strerror(open_err));
259 case WTAP_ERR_UNSUPPORTED:
260 case WTAP_ERR_UNSUPPORTED_ENCAP:
261 case WTAP_ERR_BAD_RECORD:
262 fprintf(stderr, "(%s)\n", err_info);
270 for (i = 0; i < in_file_count; i++)
271 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
272 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
277 * Snapshot length not specified - default to the maximum of the
278 * snapshot lengths of the input files.
280 snaplen = merge_max_snapshot_length(in_file_count, in_files);
283 /* set the outfile frame type */
284 if (frame_type == -2) {
286 * Default to the appropriate frame type for the input files.
288 frame_type = merge_select_frame_type(in_file_count, in_files);
290 if (frame_type == WTAP_ENCAP_PER_PACKET) {
292 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
294 int first_frame_type, this_frame_type;
296 first_frame_type = wtap_file_encap(in_files[0].wth);
297 for (i = 1; i < in_file_count; i++) {
298 this_frame_type = wtap_file_encap(in_files[i].wth);
299 if (first_frame_type != this_frame_type) {
300 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
301 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
302 fprintf(stderr, " %s had type %s (%s)\n",
303 in_files[0].filename,
304 wtap_encap_string(first_frame_type),
305 wtap_encap_short_string(first_frame_type));
306 fprintf(stderr, " %s had type %s (%s)\n",
307 in_files[i].filename,
308 wtap_encap_string(this_frame_type),
309 wtap_encap_short_string(this_frame_type));
314 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
315 wtap_encap_string(frame_type),
316 wtap_encap_short_string(frame_type));
320 /* open the outfile */
321 if (strncmp(out_filename, "-", 2) == 0) {
322 /* use stdout as the outfile */
323 out_fd = 1 /*stdout*/;
325 /* open the outfile */
326 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
328 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
329 out_filename, g_strerror(errno));
334 /* prepare the outfile */
335 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
337 merge_close_in_files(in_file_count, in_files);
339 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
340 wtap_strerror(open_err));
344 /* do the merge (or append) */
348 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
351 in_file = merge_read_packet(in_file_count, in_files, &read_err,
353 if (in_file == NULL) {
359 /* I/O error reading from in_file */
360 got_read_error = TRUE;
365 fprintf(stderr, "Record: %u\n", count++);
367 /* We simply write it, perhaps after truncating it; we could do other
368 * things, like modify it. */
369 phdr = wtap_phdr(in_file->wth);
370 if (snaplen != 0 && phdr->caplen > snaplen) {
372 snap_phdr.caplen = snaplen;
376 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(in_file->wth),
377 wtap_buf_ptr(in_file->wth), &write_err)) {
378 got_write_error = TRUE;
383 merge_close_in_files(in_file_count, in_files);
384 if (!got_read_error && !got_write_error) {
385 if (!wtap_dump_close(pdh, &write_err))
386 got_write_error = TRUE;
388 wtap_dump_close(pdh, &close_err);
390 if (got_read_error) {
392 * Find the file on which we got the error, and report the error.
394 for (i = 0; i < in_file_count; i++) {
395 if (in_files[i].state == GOT_ERROR) {
396 fprintf(stderr, "mergecap: Error reading %s: %s\n",
397 in_files[i].filename, wtap_strerror(read_err));
400 case WTAP_ERR_UNSUPPORTED:
401 case WTAP_ERR_UNSUPPORTED_ENCAP:
402 case WTAP_ERR_BAD_RECORD:
403 fprintf(stderr, "(%s)\n", err_info);
411 if (got_write_error) {
414 case WTAP_ERR_UNSUPPORTED_ENCAP:
416 * This is a problem with the particular frame we're writing;
417 * note that, and give the frame number.
419 fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
420 in_file->packet_num, in_file->filename);
424 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
425 wtap_strerror(write_err));
432 return (!got_read_error && !got_write_error) ? 0 : 2;