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;
163 struct wtap_pkthdr *phdr, snap_phdr;
165 int open_err, read_err, write_err, close_err;
168 char *out_filename = NULL;
169 gboolean got_read_error = FALSE, got_write_error = FALSE;
173 arg_list_utf_16to8(argc, argv);
176 /* Process the options first */
177 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
181 out_filename = optarg;
185 do_append = !do_append;
189 frame_type = wtap_short_string_to_encap(optarg);
190 if (frame_type < 0) {
191 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
199 file_type = wtap_short_string_to_file_type(optarg);
201 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
203 list_capture_types();
213 snaplen = get_positive_int(optarg, "snapshot length");
221 case '?': /* Bad options if GNU getopt */
224 list_capture_types();
239 /* check for proper args; at a minimum, must have an output
240 * filename and one input file
242 in_file_count = argc - optind;
244 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
245 fprintf(stderr, " run with -h for help\n");
248 if (in_file_count < 1) {
249 fprintf(stderr, "mergecap: No input files were specified\n");
253 /* open the input files */
254 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
255 &open_err, &err_info, &err_fileno)) {
256 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
257 wtap_strerror(open_err));
260 case WTAP_ERR_UNSUPPORTED:
261 case WTAP_ERR_UNSUPPORTED_ENCAP:
262 case WTAP_ERR_BAD_RECORD:
263 fprintf(stderr, "(%s)\n", err_info);
271 for (i = 0; i < in_file_count; i++)
272 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
273 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
278 * Snapshot length not specified - default to the maximum of the
279 * snapshot lengths of the input files.
281 snaplen = merge_max_snapshot_length(in_file_count, in_files);
284 /* set the outfile frame type */
285 if (frame_type == -2) {
287 * Default to the appropriate frame type for the input files.
289 frame_type = merge_select_frame_type(in_file_count, in_files);
291 if (frame_type == WTAP_ENCAP_PER_PACKET) {
293 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
295 int first_frame_type, this_frame_type;
297 first_frame_type = wtap_file_encap(in_files[0].wth);
298 for (i = 1; i < in_file_count; i++) {
299 this_frame_type = wtap_file_encap(in_files[i].wth);
300 if (first_frame_type != this_frame_type) {
301 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
302 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
303 fprintf(stderr, " %s had type %s (%s)\n",
304 in_files[0].filename,
305 wtap_encap_string(first_frame_type),
306 wtap_encap_short_string(first_frame_type));
307 fprintf(stderr, " %s had type %s (%s)\n",
308 in_files[i].filename,
309 wtap_encap_string(this_frame_type),
310 wtap_encap_short_string(this_frame_type));
315 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
316 wtap_encap_string(frame_type),
317 wtap_encap_short_string(frame_type));
321 /* open the outfile */
322 if (strncmp(out_filename, "-", 2) == 0) {
323 /* use stdout as the outfile */
324 out_fd = 1 /*stdout*/;
326 /* open the outfile */
327 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
329 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
330 out_filename, g_strerror(errno));
335 /* prepare the outfile */
336 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
338 merge_close_in_files(in_file_count, in_files);
340 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
341 wtap_strerror(open_err));
345 /* do the merge (or append) */
349 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
352 wth = merge_read_packet(in_file_count, in_files, &read_err,
356 got_read_error = TRUE;
361 fprintf(stderr, "Record: %u\n", count++);
363 /* We simply write it, perhaps after truncating it; we could do other
364 * things, like modify it. */
365 phdr = wtap_phdr(wth);
366 if (snaplen != 0 && phdr->caplen > snaplen) {
368 snap_phdr.caplen = snaplen;
372 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
373 wtap_buf_ptr(wth), &write_err)) {
374 got_write_error = TRUE;
379 merge_close_in_files(in_file_count, in_files);
380 if (!got_read_error && !got_write_error) {
381 if (!wtap_dump_close(pdh, &write_err))
382 got_write_error = TRUE;
384 wtap_dump_close(pdh, &close_err);
386 if (got_read_error) {
388 * Find the file on which we got the error, and report the error.
390 for (i = 0; i < in_file_count; i++) {
391 if (in_files[i].state == GOT_ERROR) {
392 fprintf(stderr, "mergecap: Error reading %s: %s\n",
393 in_files[i].filename, wtap_strerror(read_err));
396 case WTAP_ERR_UNSUPPORTED:
397 case WTAP_ERR_UNSUPPORTED_ENCAP:
398 case WTAP_ERR_BAD_RECORD:
399 fprintf(stderr, "(%s)\n", err_info);
407 if (got_write_error) {
408 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
409 wtap_strerror(write_err));
414 return (!got_read_error && !got_write_error) ? 0 : 2;