1 /* Combine dump files, either by appending or by merging by timestamp
3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Mergecap written by Scott Renfro <scott@renfro.org> based on
22 * editcap by Richard Sharpe and Guy Harris
42 #include <zlib.h> /* to get the libz version number */
48 #ifndef HAVE_GETOPT_LONG
49 #include <wsutil/wsgetopt.h>
52 #include <wsutil/clopts_common.h>
53 #include <wsutil/cmdarg_err.h>
54 #include <wsutil/crash_info.h>
55 #include <wsutil/file_util.h>
56 #include <wsutil/strnatcmp.h>
57 #include <wsutil/ws_diag_control.h>
58 #include <wsutil/ws_version_info.h>
60 #include <wiretap/merge.h>
61 #include <wiretap/pcap-encap.h>
68 #include <wsutil/unicode-utils.h>
75 print_usage(FILE *output)
77 fprintf(output, "\n");
78 fprintf(output, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
79 fprintf(output, "\n");
80 fprintf(output, "Output:\n");
81 fprintf(output, " -a concatenate rather than merge files.\n");
82 fprintf(output, " default is to merge based on frame timestamps.\n");
83 fprintf(output, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
84 fprintf(output, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
85 fprintf(output, " -F <capture type> set the output file type; default is pcapng.\n");
86 fprintf(output, " an empty \"-F\" option will list the file types.\n");
87 fprintf(output, " -T <encap type> set the output file encapsulation type;\n");
88 fprintf(output, " default is the same as the first input file.\n");
89 fprintf(output, " an empty \"-T\" option will list the encapsulation types.\n");
90 fprintf(output, "\n");
91 fprintf(output, "Miscellaneous:\n");
92 fprintf(output, " -h display this help and exit.\n");
93 fprintf(output, " -v verbose output.\n");
97 * Report an error in command-line arguments.
100 mergecap_cmdarg_err(const char *fmt, va_list ap)
102 fprintf(stderr, "mergecap: ");
103 vfprintf(stderr, fmt, ap);
104 fprintf(stderr, "\n");
108 * Report additional information for an error in command-line arguments.
111 mergecap_cmdarg_err_cont(const char *fmt, va_list ap)
113 vfprintf(stderr, fmt, ap);
114 fprintf(stderr, "\n");
118 const char *sstr; /* The short string */
119 const char *lstr; /* The long string */
123 string_compare(gconstpointer a, gconstpointer b)
125 return strcmp(((const struct string_elem *)a)->sstr,
126 ((const struct string_elem *)b)->sstr);
130 string_nat_compare(gconstpointer a, gconstpointer b)
132 return ws_ascii_strnatcmp(((const struct string_elem *)a)->sstr,
133 ((const struct string_elem *)b)->sstr);
137 string_elem_print(gpointer data, gpointer not_used _U_)
139 fprintf(stderr, " %s - %s\n", ((struct string_elem *)data)->sstr,
140 ((struct string_elem *)data)->lstr);
144 list_capture_types(void) {
146 struct string_elem *captypes;
149 captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
151 fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
152 for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
153 if (wtap_dump_can_open(i)) {
154 captypes[i].sstr = wtap_file_type_subtype_short_string(i);
155 captypes[i].lstr = wtap_file_type_subtype_string(i);
156 list = g_slist_insert_sorted(list, &captypes[i], string_compare);
159 g_slist_foreach(list, string_elem_print, NULL);
165 list_encap_types(void) {
167 struct string_elem *encaps;
170 encaps = g_new(struct string_elem,WTAP_NUM_ENCAP_TYPES);
171 fprintf(stderr, "mergecap: The available encapsulation types for the \"-T\" flag are:\n");
172 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
173 encaps[i].sstr = wtap_encap_short_string(i);
174 if (encaps[i].sstr != NULL) {
175 encaps[i].lstr = wtap_encap_string(i);
176 list = g_slist_insert_sorted(list, &encaps[i], string_nat_compare);
179 g_slist_foreach(list, string_elem_print, NULL);
185 get_mergecap_compiled_info(GString *str)
188 g_string_append(str, ", ");
190 g_string_append(str, "with libz ");
192 g_string_append(str, ZLIB_VERSION);
193 #else /* ZLIB_VERSION */
194 g_string_append(str, "(version unknown)");
195 #endif /* ZLIB_VERSION */
196 #else /* HAVE_LIBZ */
197 g_string_append(str, "without libz");
198 #endif /* HAVE_LIBZ */
202 get_mergecap_runtime_info(GString *str)
205 #if defined(HAVE_LIBZ) && !defined(_WIN32)
206 g_string_append_printf(str, ", with libz %s", zlibVersion());
211 main(int argc, char *argv[])
213 GString *comp_info_str;
214 GString *runtime_info_str;
217 static const struct option long_options[] = {
218 {(char *)"help", no_argument, NULL, 'h'},
219 {(char *)"version", no_argument, NULL, 'V'},
223 gboolean do_append = FALSE;
224 gboolean verbose = FALSE;
225 int in_file_count = 0;
227 #ifdef PCAP_NG_DEFAULT
228 int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */
230 int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */
234 merge_in_file_t *in_files = NULL, *in_file;
236 struct wtap_pkthdr *phdr, snap_phdr;
238 int open_err, read_err = 0, write_err, close_err;
239 gchar *err_info, *write_err_info = NULL;
241 char *out_filename = NULL;
242 gboolean got_read_error = FALSE, got_write_error = FALSE;
245 cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
248 arg_list_utf_16to8(argc, argv);
249 create_app_running_mutex();
252 /* Get the compile-time version information string */
253 comp_info_str = get_compiled_version_info(NULL, get_mergecap_compiled_info);
255 /* Get the run-time version information string */
256 runtime_info_str = get_runtime_version_info(get_mergecap_runtime_info);
258 /* Add it to the information to be reported on a crash. */
259 ws_add_crash_info("Mergecap (Wireshark) %s\n"
264 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
266 /* Process the options first */
267 while ((opt = getopt_long(argc, argv, "aF:hs:T:vVw:", long_options, NULL)) != -1) {
271 do_append = !do_append;
275 file_type = wtap_short_string_to_file_type_subtype(optarg);
277 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
279 list_capture_types();
285 printf("Mergecap (Wireshark) %s\n"
286 "Merge two or more capture files into one.\n"
287 "See http://www.wireshark.org for more information.\n",
288 get_ws_vcs_version_info());
294 snaplen = get_positive_int(optarg, "snapshot length");
298 frame_type = wtap_short_string_to_encap(optarg);
299 if (frame_type < 0) {
300 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
312 show_version("Mergecap (Wireshark)", comp_info_str, runtime_info_str);
313 g_string_free(comp_info_str, TRUE);
314 g_string_free(runtime_info_str, TRUE);
319 out_filename = optarg;
322 case '?': /* Bad options if GNU getopt */
325 list_capture_types();
338 /* check for proper args; at a minimum, must have an output
339 * filename and one input file
341 in_file_count = argc - optind;
343 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
344 fprintf(stderr, " run with -h for help\n");
347 if (in_file_count < 1) {
348 fprintf(stderr, "mergecap: No input files were specified\n");
352 /* open the input files */
353 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
354 &open_err, &err_info, &err_fileno)) {
355 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
356 wtap_strerror(open_err));
357 if (err_info != NULL) {
358 fprintf(stderr, "(%s)\n", err_info);
365 for (i = 0; i < in_file_count; i++)
366 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
367 wtap_file_type_subtype_string(wtap_file_type_subtype(in_files[i].wth)));
372 * Snapshot length not specified - default to the maximum of the
373 * snapshot lengths of the input files.
375 snaplen = merge_max_snapshot_length(in_file_count, in_files);
378 /* set the outfile frame type */
379 if (frame_type == -2) {
381 * Default to the appropriate frame type for the input files.
383 frame_type = merge_select_frame_type(in_file_count, in_files);
385 if (frame_type == WTAP_ENCAP_PER_PACKET) {
387 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
389 int first_frame_type, this_frame_type;
391 first_frame_type = wtap_file_encap(in_files[0].wth);
392 for (i = 1; i < in_file_count; i++) {
393 this_frame_type = wtap_file_encap(in_files[i].wth);
394 if (first_frame_type != this_frame_type) {
395 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
396 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
397 fprintf(stderr, " %s had type %s (%s)\n",
398 in_files[0].filename,
399 wtap_encap_string(first_frame_type),
400 wtap_encap_short_string(first_frame_type));
401 fprintf(stderr, " %s had type %s (%s)\n",
402 in_files[i].filename,
403 wtap_encap_string(this_frame_type),
404 wtap_encap_short_string(this_frame_type));
409 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
410 wtap_encap_string(frame_type),
411 wtap_encap_short_string(frame_type));
415 /* open the outfile */
416 if (strncmp(out_filename, "-", 2) == 0) {
417 /* use stdout as the outfile */
418 out_fd = 1 /*stdout*/;
420 /* open the outfile */
421 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
423 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
424 out_filename, g_strerror(errno));
429 /* prepare the outfile */
430 if(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG ){
431 wtapng_section_t *shb_hdr;
432 GString *comment_gstr;
433 wtapng_iface_descriptions_t *idb_inf = NULL, *idb_inf_merge_file;
434 wtapng_if_descr_t int_data, *file_int_data;
435 guint itf_count, itf_id = 0;
437 shb_hdr = g_new(wtapng_section_t,1);
438 comment_gstr = g_string_new("File created by merging: \n");
440 for (i = 0; i < in_file_count; i++) {
441 g_string_append_printf(comment_gstr, "File%d: %s \n",i+1,in_files[i].filename);
443 shb_hdr->section_length = -1;
445 shb_hdr->opt_comment = comment_gstr->str; /* NULL if not available */
446 shb_hdr->shb_hardware = NULL; /* NULL if not available, UTF-8 string containing the description of the hardware used to create this section. */
447 shb_hdr->shb_os = NULL; /* NULL if not available, UTF-8 string containing the name of the operating system used to create this section. */
448 shb_hdr->shb_user_appl = g_strdup("mergecap"); /* NULL if not available, UTF-8 string containing the name of the application used to create this section. */
450 if (frame_type == WTAP_ENCAP_PER_PACKET) {
451 /* create fake IDB info */
452 idb_inf = g_new(wtapng_iface_descriptions_t,1);
453 /* TODO make this the number of DIFFERENT encapsulation types
454 * check that snaplength is the same too?
456 idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
458 for (i = 0; i < in_file_count; i++) {
459 idb_inf_merge_file = wtap_file_get_idb_info(in_files[i].wth);
460 for (itf_count = 0; itf_count < idb_inf_merge_file->interface_data->len; itf_count++) {
461 /* read the interface data from the in file to our combined interface data */
462 file_int_data = &g_array_index (idb_inf_merge_file->interface_data, wtapng_if_descr_t, itf_count);
463 int_data.wtap_encap = file_int_data->wtap_encap;
464 int_data.time_units_per_second = file_int_data->time_units_per_second;
465 int_data.link_type = file_int_data->link_type;
466 int_data.snap_len = file_int_data->snap_len;
467 int_data.if_name = g_strdup(file_int_data->if_name);
468 int_data.opt_comment = NULL;
469 int_data.if_description = NULL;
470 int_data.if_speed = 0;
471 int_data.if_tsresol = file_int_data->if_tsresol;
472 int_data.if_filter_str = NULL;
473 int_data.bpf_filter_len = 0;
474 int_data.if_filter_bpf_bytes = NULL;
475 int_data.if_os = NULL;
476 int_data.if_fcslen = -1;
477 int_data.num_stat_entries = 0; /* Number of ISB:s */
478 int_data.interface_statistics = NULL;
480 g_array_append_val(idb_inf->interface_data, int_data);
482 g_free(idb_inf_merge_file);
484 /* Set fake interface Id in per file data */
485 in_files[i].interface_id = itf_id;
489 guint8 if_tsresol = 6;
490 guint64 time_units_per_second = 1000000;
491 for (i = 0; i < in_file_count; i++) {
492 idb_inf_merge_file = wtap_file_get_idb_info(in_files[i].wth);
493 for (itf_count = 0; itf_count < idb_inf_merge_file->interface_data->len; itf_count++) {
494 file_int_data = &g_array_index (idb_inf_merge_file->interface_data, wtapng_if_descr_t, itf_count);
495 if (file_int_data->time_units_per_second > time_units_per_second) {
496 time_units_per_second = file_int_data->time_units_per_second;
497 if_tsresol = file_int_data->if_tsresol;
500 g_free(idb_inf_merge_file);
502 if (time_units_per_second > 1000000) {
503 /* We are using a better than microsecond precision; let's create a fake IDB */
504 idb_inf = g_new(wtapng_iface_descriptions_t,1);
505 idb_inf->interface_data = g_array_new(FALSE, FALSE, sizeof(wtapng_if_descr_t));
506 int_data.wtap_encap = frame_type;
507 int_data.time_units_per_second = time_units_per_second;
508 int_data.link_type = wtap_wtap_encap_to_pcap_encap(frame_type);
509 int_data.snap_len = snaplen;
510 int_data.if_name = g_strdup("Unknown/not available in original file format(libpcap)");
511 int_data.opt_comment = NULL;
512 int_data.if_description = NULL;
513 int_data.if_speed = 0;
514 int_data.if_tsresol = if_tsresol;
515 int_data.if_filter_str = NULL;
516 int_data.bpf_filter_len = 0;
517 int_data.if_filter_bpf_bytes = NULL;
518 int_data.if_os = NULL;
519 int_data.if_fcslen = -1;
520 int_data.num_stat_entries = 0; /* Number of ISB:s */
521 int_data.interface_statistics = NULL;
522 g_array_append_val(idb_inf->interface_data, int_data);
526 pdh = wtap_dump_fdopen_ng(out_fd, file_type, frame_type, snaplen,
527 FALSE /* compressed */, shb_hdr, idb_inf, &open_err);
528 g_string_free(comment_gstr, TRUE);
530 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
533 merge_close_in_files(in_file_count, in_files);
535 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
536 wtap_strerror(open_err));
540 /* do the merge (or append) */
544 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
547 in_file = merge_read_packet(in_file_count, in_files, &read_err,
549 if (in_file == NULL) {
555 /* I/O error reading from in_file */
556 got_read_error = TRUE;
561 fprintf(stderr, "Record: %d\n", count++);
563 /* We simply write it, perhaps after truncating it; we could do other
564 * things, like modify it. */
565 phdr = wtap_phdr(in_file->wth);
566 if (snaplen != 0 && phdr->caplen > snaplen) {
568 snap_phdr.caplen = snaplen;
571 if ((file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG) && (frame_type == WTAP_ENCAP_PER_PACKET)) {
572 if (phdr->presence_flags & WTAP_HAS_INTERFACE_ID) {
573 phdr->interface_id += in_file->interface_id;
575 phdr->interface_id = in_file->interface_id;
576 phdr->presence_flags = phdr->presence_flags | WTAP_HAS_INTERFACE_ID;
580 if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err, &write_err_info)) {
581 got_write_error = TRUE;
586 merge_close_in_files(in_file_count, in_files);
587 if (!got_write_error) {
588 if (!wtap_dump_close(pdh, &write_err))
589 got_write_error = TRUE;
592 * We already got a write error; no need to report another
593 * write error on close.
595 * Don't overwrite the earlier write error.
597 (void)wtap_dump_close(pdh, &close_err);
600 if (got_read_error) {
602 * Find the file on which we got the error, and report the error.
604 for (i = 0; i < in_file_count; i++) {
605 if (in_files[i].state == GOT_ERROR) {
606 fprintf(stderr, "mergecap: Error reading %s: %s\n",
607 in_files[i].filename, wtap_strerror(read_err));
608 if (err_info != NULL) {
609 fprintf(stderr, "(%s)\n", err_info);
616 if (got_write_error) {
619 case WTAP_ERR_UNWRITABLE_ENCAP:
621 * This is a problem with the particular frame we're writing and
622 * the file type and subtype we're wwriting; note that, and
623 * report the frame number and file type/subtype.
625 fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n",
626 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
627 wtap_file_type_subtype_string(file_type));
630 case WTAP_ERR_PACKET_TOO_LARGE:
632 * This is a problem with the particular frame we're writing and
633 * the file type and subtype we're wwriting; note that, and
634 * report the frame number and file type/subtype.
636 fprintf(stderr, "mergecap: Frame %u of \"%s\" is too large for a \"%s\" file.\n",
637 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
638 wtap_file_type_subtype_string(file_type));
641 case WTAP_ERR_UNWRITABLE_REC_TYPE:
643 * This is a problem with the particular record we're writing and
644 * the file type and subtype we're wwriting; note that, and
645 * report the record number and file type/subtype.
647 fprintf(stderr, "mergecap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
648 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
649 wtap_file_type_subtype_string(file_type));
652 case WTAP_ERR_UNWRITABLE_REC_DATA:
654 * This is a problem with the particular record we're writing and
655 * the file type and subtype we're wwriting; note that, and
656 * report the record number and file type/subtype.
658 fprintf(stderr, "mergecap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
659 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
660 wtap_file_type_subtype_string(file_type),
661 write_err_info != NULL ? write_err_info : "no information supplied");
662 g_free(write_err_info);
666 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
667 wtap_strerror(write_err));
674 return (!got_read_error && !got_write_error) ? 0 : 2;
678 * Editor modelines - http://www.wireshark.org/tools/modelines.html
683 * indent-tabs-mode: nil
686 * vi: set shiftwidth=2 tabstop=8 expandtab:
687 * :indentSize=2:tabSize=8:noTabs=true: