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
33 #include "wsutil/wsgetopt.h"
36 #include "svnversion.h"
38 #include "wsutil/file_util.h"
45 #include <wsutil/unicode-utils.h>
49 get_natural_int(const char *string, const char *name)
54 number = (int) strtol(string, &p, 10);
55 if (p == string || *p != '\0') {
56 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
61 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
65 if (number > INT_MAX) {
66 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
74 get_positive_int(const char *string, const char *name)
78 number = get_natural_int(string, name);
81 fprintf(stderr, "mergecap: The specified %s is zero\n",
96 fprintf(stderr, "Mergecap %s"
98 " (" SVNVERSION " from " SVNPATH ")"
101 fprintf(stderr, "Merge two or more capture files into one.\n");
102 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
103 fprintf(stderr, "\n");
104 fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> ...\n");
105 fprintf(stderr, "\n");
106 fprintf(stderr, "Output:\n");
107 fprintf(stderr, " -a concatenate rather than merge files.\n");
108 fprintf(stderr, " default is to merge based on frame timestamps.\n");
109 fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
110 fprintf(stderr, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
111 fprintf(stderr, " -F <capture type> set the output file type; default is libpcap.\n");
112 fprintf(stderr, " an empty \"-F\" option will list the file types.\n");
113 fprintf(stderr, " -T <encap type> set the output file encapsulation type;\n");
114 fprintf(stderr, " default is the same as the first input file.\n");
115 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types.\n");
116 fprintf(stderr, "\n");
117 fprintf(stderr, "Miscellaneous:\n");
118 fprintf(stderr, " -h display this help and exit.\n");
119 fprintf(stderr, " -v verbose output.\n");
122 static void list_capture_types(void) {
125 fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
126 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
127 if (wtap_dump_can_open(i))
128 fprintf(stderr, " %s - %s\n",
129 wtap_file_type_short_string(i), wtap_file_type_string(i));
133 static void list_encap_types(void) {
137 fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
138 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
139 string = wtap_encap_short_string(i);
141 fprintf(stderr, " %s - %s\n",
142 string, wtap_encap_string(i));
147 main(int argc, char *argv[])
151 gboolean do_append = FALSE;
152 gboolean verbose = FALSE;
153 int in_file_count = 0;
155 #ifdef PCAP_NG_DEFAULT
156 int file_type = WTAP_FILE_PCAPNG; /* default to pcap format */
158 int file_type = WTAP_FILE_PCAP; /* default to pcapng format */
162 merge_in_file_t *in_files = NULL;
165 struct wtap_pkthdr *phdr, snap_phdr;
167 int open_err, read_err, write_err, close_err;
170 char *out_filename = NULL;
171 gboolean got_read_error = FALSE, got_write_error = FALSE;
175 arg_list_utf_16to8(argc, argv);
178 /* Process the options first */
179 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
183 out_filename = optarg;
187 do_append = !do_append;
191 frame_type = wtap_short_string_to_encap(optarg);
192 if (frame_type < 0) {
193 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
201 file_type = wtap_short_string_to_file_type(optarg);
203 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
205 list_capture_types();
215 snaplen = get_positive_int(optarg, "snapshot length");
223 case '?': /* Bad options if GNU getopt */
226 list_capture_types();
241 /* check for proper args; at a minimum, must have an output
242 * filename and one input file
244 in_file_count = argc - optind;
246 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
247 fprintf(stderr, " run with -h for help\n");
250 if (in_file_count < 1) {
251 fprintf(stderr, "mergecap: No input files were specified\n");
255 /* open the input files */
256 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
257 &open_err, &err_info, &err_fileno)) {
258 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
259 wtap_strerror(open_err));
262 case WTAP_ERR_UNSUPPORTED:
263 case WTAP_ERR_UNSUPPORTED_ENCAP:
264 case WTAP_ERR_BAD_RECORD:
265 fprintf(stderr, "(%s)\n", err_info);
273 for (i = 0; i < in_file_count; i++)
274 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
275 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
280 * Snapshot length not specified - default to the maximum of the
281 * snapshot lengths of the input files.
283 snaplen = merge_max_snapshot_length(in_file_count, in_files);
286 /* set the outfile frame type */
287 if (frame_type == -2) {
289 * Default to the appropriate frame type for the input files.
291 frame_type = merge_select_frame_type(in_file_count, in_files);
293 if (frame_type == WTAP_ENCAP_PER_PACKET) {
295 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
297 int first_frame_type, this_frame_type;
299 first_frame_type = wtap_file_encap(in_files[0].wth);
300 for (i = 1; i < in_file_count; i++) {
301 this_frame_type = wtap_file_encap(in_files[i].wth);
302 if (first_frame_type != this_frame_type) {
303 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
304 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
305 fprintf(stderr, " %s had type %s (%s)\n",
306 in_files[0].filename,
307 wtap_encap_string(first_frame_type),
308 wtap_encap_short_string(first_frame_type));
309 fprintf(stderr, " %s had type %s (%s)\n",
310 in_files[i].filename,
311 wtap_encap_string(this_frame_type),
312 wtap_encap_short_string(this_frame_type));
317 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
318 wtap_encap_string(frame_type),
319 wtap_encap_short_string(frame_type));
323 /* open the outfile */
324 if (strncmp(out_filename, "-", 2) == 0) {
325 /* use stdout as the outfile */
326 out_fd = 1 /*stdout*/;
328 /* open the outfile */
329 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
331 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
332 out_filename, g_strerror(errno));
337 /* prepare the outfile */
338 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
340 merge_close_in_files(in_file_count, in_files);
342 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
343 wtap_strerror(open_err));
347 /* do the merge (or append) */
351 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
354 wth = merge_read_packet(in_file_count, in_files, &read_err,
358 got_read_error = TRUE;
363 fprintf(stderr, "Record: %u\n", count++);
365 /* We simply write it, perhaps after truncating it; we could do other
366 * things, like modify it. */
367 phdr = wtap_phdr(wth);
368 if (snaplen != 0 && phdr->caplen > snaplen) {
370 snap_phdr.caplen = snaplen;
374 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
375 wtap_buf_ptr(wth), &write_err)) {
376 got_write_error = TRUE;
381 merge_close_in_files(in_file_count, in_files);
382 if (!got_read_error && !got_write_error) {
383 if (!wtap_dump_close(pdh, &write_err))
384 got_write_error = TRUE;
386 wtap_dump_close(pdh, &close_err);
388 if (got_read_error) {
390 * Find the file on which we got the error, and report the error.
392 for (i = 0; i < in_file_count; i++) {
393 if (in_files[i].state == GOT_ERROR) {
394 fprintf(stderr, "mergecap: Error reading %s: %s\n",
395 in_files[i].filename, wtap_strerror(read_err));
398 case WTAP_ERR_UNSUPPORTED:
399 case WTAP_ERR_UNSUPPORTED_ENCAP:
400 case WTAP_ERR_BAD_RECORD:
401 fprintf(stderr, "(%s)\n", err_info);
409 if (got_write_error) {
410 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
411 wtap_strerror(write_err));
416 return (!got_read_error && !got_write_error) ? 0 : 2;