1 /* Combine two dump files, either by appending or by merging by timestamp
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Mergecap written by Scott Renfro <scott@renfro.org> based on
24 * editcap by Richard Sharpe and Guy Harris
41 #ifdef HAVE_SYS_TIME_H
49 #include "wsutil/wsgetopt.h"
52 #include "svnversion.h"
54 #include "wsutil/file_util.h"
61 #include <wsutil/unicode-utils.h>
65 get_natural_int(const char *string, const char *name)
70 number = (int) strtol(string, &p, 10);
71 if (p == string || *p != '\0') {
72 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
77 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
81 if (number > INT_MAX) {
82 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
90 get_positive_int(const char *string, const char *name)
94 number = get_natural_int(string, name);
97 fprintf(stderr, "mergecap: The specified %s is zero\n",
112 fprintf(stderr, "Mergecap %s"
114 " (" SVNVERSION " from " SVNPATH ")"
117 fprintf(stderr, "Merge two or more capture files into one.\n");
118 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
119 fprintf(stderr, "\n");
120 fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
121 fprintf(stderr, "\n");
122 fprintf(stderr, "Output:\n");
123 fprintf(stderr, " -a concatenate rather than merge files.\n");
124 fprintf(stderr, " default is to merge based on frame timestamps.\n");
125 fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
126 fprintf(stderr, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
127 fprintf(stderr, " -F <capture type> set the output file type; default is libpcap.\n");
128 fprintf(stderr, " an empty \"-F\" option will list the file types.\n");
129 fprintf(stderr, " -T <encap type> set the output file encapsulation type;\n");
130 fprintf(stderr, " default is the same as the first input file.\n");
131 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types.\n");
132 fprintf(stderr, "\n");
133 fprintf(stderr, "Miscellaneous:\n");
134 fprintf(stderr, " -h display this help and exit.\n");
135 fprintf(stderr, " -v verbose output.\n");
138 static void list_capture_types(void) {
141 fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
142 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
143 if (wtap_dump_can_open(i))
144 fprintf(stderr, " %s - %s\n",
145 wtap_file_type_short_string(i), wtap_file_type_string(i));
149 static void list_encap_types(void) {
153 fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
154 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
155 string = wtap_encap_short_string(i);
157 fprintf(stderr, " %s - %s\n",
158 string, wtap_encap_string(i));
163 main(int argc, char *argv[])
167 gboolean do_append = FALSE;
168 gboolean verbose = FALSE;
169 int in_file_count = 0;
171 #ifdef PCAP_NG_DEFAULT
172 int file_type = WTAP_FILE_PCAPNG; /* default to pcap format */
174 int file_type = WTAP_FILE_PCAP; /* default to pcapng format */
178 merge_in_file_t *in_files = NULL, *in_file;
180 struct wtap_pkthdr *phdr, snap_phdr;
182 int open_err, read_err=0, write_err, close_err;
185 char *out_filename = NULL;
186 gboolean got_read_error = FALSE, got_write_error = FALSE;
190 arg_list_utf_16to8(argc, argv);
193 /* Process the options first */
194 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
198 out_filename = optarg;
202 do_append = !do_append;
206 frame_type = wtap_short_string_to_encap(optarg);
207 if (frame_type < 0) {
208 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
216 file_type = wtap_short_string_to_file_type(optarg);
218 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
220 list_capture_types();
230 snaplen = get_positive_int(optarg, "snapshot length");
238 case '?': /* Bad options if GNU getopt */
241 list_capture_types();
256 /* check for proper args; at a minimum, must have an output
257 * filename and one input file
259 in_file_count = argc - optind;
261 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
262 fprintf(stderr, " run with -h for help\n");
265 if (in_file_count < 1) {
266 fprintf(stderr, "mergecap: No input files were specified\n");
270 /* open the input files */
271 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
272 &open_err, &err_info, &err_fileno)) {
273 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
274 wtap_strerror(open_err));
277 case WTAP_ERR_UNSUPPORTED:
278 case WTAP_ERR_UNSUPPORTED_ENCAP:
279 case WTAP_ERR_BAD_FILE:
280 fprintf(stderr, "(%s)\n", err_info);
288 for (i = 0; i < in_file_count; i++)
289 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
290 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
295 * Snapshot length not specified - default to the maximum of the
296 * snapshot lengths of the input files.
298 snaplen = merge_max_snapshot_length(in_file_count, in_files);
301 /* set the outfile frame type */
302 if (frame_type == -2) {
304 * Default to the appropriate frame type for the input files.
306 frame_type = merge_select_frame_type(in_file_count, in_files);
308 if (frame_type == WTAP_ENCAP_PER_PACKET) {
310 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
312 int first_frame_type, this_frame_type;
314 first_frame_type = wtap_file_encap(in_files[0].wth);
315 for (i = 1; i < in_file_count; i++) {
316 this_frame_type = wtap_file_encap(in_files[i].wth);
317 if (first_frame_type != this_frame_type) {
318 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
319 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
320 fprintf(stderr, " %s had type %s (%s)\n",
321 in_files[0].filename,
322 wtap_encap_string(first_frame_type),
323 wtap_encap_short_string(first_frame_type));
324 fprintf(stderr, " %s had type %s (%s)\n",
325 in_files[i].filename,
326 wtap_encap_string(this_frame_type),
327 wtap_encap_short_string(this_frame_type));
332 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
333 wtap_encap_string(frame_type),
334 wtap_encap_short_string(frame_type));
338 /* open the outfile */
339 if (strncmp(out_filename, "-", 2) == 0) {
340 /* use stdout as the outfile */
341 out_fd = 1 /*stdout*/;
343 /* open the outfile */
344 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
346 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
347 out_filename, g_strerror(errno));
352 /* prepare the outfile */
353 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
355 merge_close_in_files(in_file_count, in_files);
357 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
358 wtap_strerror(open_err));
362 /* do the merge (or append) */
366 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
369 in_file = merge_read_packet(in_file_count, in_files, &read_err,
371 if (in_file == NULL) {
377 /* I/O error reading from in_file */
378 got_read_error = TRUE;
383 fprintf(stderr, "Record: %u\n", count++);
385 /* We simply write it, perhaps after truncating it; we could do other
386 * things, like modify it. */
387 phdr = wtap_phdr(in_file->wth);
388 if (snaplen != 0 && phdr->caplen > snaplen) {
390 snap_phdr.caplen = snaplen;
394 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(in_file->wth),
395 wtap_buf_ptr(in_file->wth), &write_err)) {
396 got_write_error = TRUE;
401 merge_close_in_files(in_file_count, in_files);
402 if (!got_read_error && !got_write_error) {
403 if (!wtap_dump_close(pdh, &write_err))
404 got_write_error = TRUE;
406 wtap_dump_close(pdh, &close_err);
408 if (got_read_error) {
410 * Find the file on which we got the error, and report the error.
412 for (i = 0; i < in_file_count; i++) {
413 if (in_files[i].state == GOT_ERROR) {
414 fprintf(stderr, "mergecap: Error reading %s: %s\n",
415 in_files[i].filename, wtap_strerror(read_err));
418 case WTAP_ERR_UNSUPPORTED:
419 case WTAP_ERR_UNSUPPORTED_ENCAP:
420 case WTAP_ERR_BAD_FILE:
421 fprintf(stderr, "(%s)\n", err_info);
429 if (got_write_error) {
432 case WTAP_ERR_UNSUPPORTED_ENCAP:
434 * This is a problem with the particular frame we're writing;
435 * note that, and give the frame number.
437 fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
438 in_file->packet_num, in_file->filename);
442 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
443 wtap_strerror(write_err));
450 return (!got_read_error && !got_write_error) ? 0 : 2;