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"
50 get_natural_int(const char *string, const char *name)
55 number = (int) strtol(string, &p, 10);
56 if (p == string || *p != '\0') {
57 fprintf(stderr, "mergecap: The specified %s \"%s\" isn't a decimal number\n",
62 fprintf(stderr, "mergecap: The specified %s is a negative number\n",
66 if (number > INT_MAX) {
67 fprintf(stderr, "mergecap: The specified %s is too large (greater than %d)\n",
75 get_positive_int(const char *string, const char *name)
79 number = get_natural_int(string, name);
82 fprintf(stderr, "mergecap: The specified %s is zero\n",
97 fprintf(stderr, "Mergecap %s"
99 " (" SVNVERSION " from " SVNPATH ")"
102 fprintf(stderr, "Merge two or more capture files into one.\n");
103 fprintf(stderr, "See http://www.wireshark.org for more information.\n");
104 fprintf(stderr, "\n");
105 fprintf(stderr, "Usage: mergecap [options] -w <outfile>|- <infile> ...\n");
106 fprintf(stderr, "\n");
107 fprintf(stderr, "Output:\n");
108 fprintf(stderr, " -a concatenate rather than merge files.\n");
109 fprintf(stderr, " default is to merge based on frame timestamps.\n");
110 fprintf(stderr, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
111 fprintf(stderr, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
112 fprintf(stderr, " -F <capture type> set the output file type; default is libpcap.\n");
113 fprintf(stderr, " an empty \"-F\" option will list the file types.\n");
114 fprintf(stderr, " -T <encap type> set the output file encapsulation type;\n");
115 fprintf(stderr, " default is the same as the first input file.\n");
116 fprintf(stderr, " an empty \"-T\" option will list the encapsulation types.\n");
117 fprintf(stderr, "\n");
118 fprintf(stderr, "Miscellaneous:\n");
119 fprintf(stderr, " -h display this help and exit.\n");
120 fprintf(stderr, " -v verbose output.\n");
123 static void list_capture_types(void) {
126 fprintf(stderr, "mergecap: The available capture file types for \"F\":\n");
127 for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
128 if (wtap_dump_can_open(i))
129 fprintf(stderr, " %s - %s\n",
130 wtap_file_type_short_string(i), wtap_file_type_string(i));
134 static void list_encap_types(void) {
138 fprintf(stderr, "mergecap: The available encapsulation types for \"T\":\n");
139 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
140 string = wtap_encap_short_string(i);
142 fprintf(stderr, " %s - %s\n",
143 string, wtap_encap_string(i));
148 main(int argc, char *argv[])
157 gboolean do_append = FALSE;
158 gboolean verbose = FALSE;
159 int in_file_count = 0;
161 int file_type = WTAP_FILE_PCAP; /* default to libpcap format */
164 merge_in_file_t *in_files = NULL;
167 struct wtap_pkthdr *phdr, snap_phdr;
169 int open_err, read_err, write_err, close_err;
172 char *out_filename = NULL;
173 gboolean got_read_error = FALSE, got_write_error = FALSE;
177 /* Convert our arg list to UTF-8. */
178 wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc);
179 if (wc_argv && wc_argc == argc) {
180 for (i = 0; i < argc; i++) {
181 argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL);
183 } /* XXX else bail because something is horribly, horribly wrong? */
186 /* Process the options first */
187 while ((opt = getopt(argc, argv, "hvas:T:F:w:")) != -1) {
191 out_filename = optarg;
195 do_append = !do_append;
199 frame_type = wtap_short_string_to_encap(optarg);
200 if (frame_type < 0) {
201 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
209 file_type = wtap_short_string_to_file_type(optarg);
211 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
213 list_capture_types();
223 snaplen = get_positive_int(optarg, "snapshot length");
231 case '?': /* Bad options if GNU getopt */
234 list_capture_types();
249 /* check for proper args; at a minimum, must have an output
250 * filename and one input file
252 in_file_count = argc - optind;
254 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
255 fprintf(stderr, " run with -h for help\n");
258 if (in_file_count < 1) {
259 fprintf(stderr, "mergecap: No input files were specified\n");
263 /* open the input files */
264 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
265 &open_err, &err_info, &err_fileno)) {
266 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
267 wtap_strerror(open_err));
270 case WTAP_ERR_UNSUPPORTED:
271 case WTAP_ERR_UNSUPPORTED_ENCAP:
272 case WTAP_ERR_BAD_RECORD:
273 fprintf(stderr, "(%s)\n", err_info);
281 for (i = 0; i < in_file_count; i++)
282 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
283 wtap_file_type_string(wtap_file_type(in_files[i].wth)));
288 * Snapshot length not specified - default to the maximum of the
289 * snapshot lengths of the input files.
291 snaplen = merge_max_snapshot_length(in_file_count, in_files);
294 /* set the outfile frame type */
295 if (frame_type == -2) {
297 * Default to the appropriate frame type for the input files.
299 frame_type = merge_select_frame_type(in_file_count, in_files);
301 if (frame_type == WTAP_ENCAP_PER_PACKET) {
303 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
305 int first_frame_type, this_frame_type;
307 first_frame_type = wtap_file_encap(in_files[0].wth);
308 for (i = 1; i < in_file_count; i++) {
309 this_frame_type = wtap_file_encap(in_files[i].wth);
310 if (first_frame_type != this_frame_type) {
311 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
312 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
313 fprintf(stderr, " %s had type %s (%s)\n",
314 in_files[0].filename,
315 wtap_encap_string(first_frame_type),
316 wtap_encap_short_string(first_frame_type));
317 fprintf(stderr, " %s had type %s (%s)\n",
318 in_files[i].filename,
319 wtap_encap_string(this_frame_type),
320 wtap_encap_short_string(this_frame_type));
325 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
326 wtap_encap_string(frame_type),
327 wtap_encap_short_string(frame_type));
331 /* open the outfile */
332 if (strncmp(out_filename, "-", 2) == 0) {
333 /* use stdout as the outfile */
334 out_fd = 1 /*stdout*/;
336 /* open the outfile */
337 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
339 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
340 out_filename, strerror(errno));
345 /* prepare the outfile */
346 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
348 merge_close_in_files(in_file_count, in_files);
350 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
351 wtap_strerror(open_err));
355 /* do the merge (or append) */
359 wth = merge_append_read_packet(in_file_count, in_files, &read_err,
362 wth = merge_read_packet(in_file_count, in_files, &read_err,
366 got_read_error = TRUE;
371 fprintf(stderr, "Record: %u\n", count++);
373 /* We simply write it, perhaps after truncating it; we could do other
374 * things, like modify it. */
375 phdr = wtap_phdr(wth);
376 if (snaplen != 0 && phdr->caplen > snaplen) {
378 snap_phdr.caplen = snaplen;
382 if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth),
383 wtap_buf_ptr(wth), &write_err)) {
384 got_write_error = TRUE;
389 merge_close_in_files(in_file_count, in_files);
390 if (!got_read_error && !got_write_error) {
391 if (!wtap_dump_close(pdh, &write_err))
392 got_write_error = TRUE;
394 wtap_dump_close(pdh, &close_err);
396 if (got_read_error) {
398 * Find the file on which we got the error, and report the error.
400 for (i = 0; i < in_file_count; i++) {
401 if (in_files[i].state == GOT_ERROR) {
402 fprintf(stderr, "mergecap: Error reading %s: %s\n",
403 in_files[i].filename, wtap_strerror(read_err));
406 case WTAP_ERR_UNSUPPORTED:
407 case WTAP_ERR_UNSUPPORTED_ENCAP:
408 case WTAP_ERR_BAD_RECORD:
409 fprintf(stderr, "(%s)\n", err_info);
417 if (got_write_error) {
418 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
419 wtap_strerror(write_err));
424 return (!got_read_error && !got_write_error) ? 0 : 2;