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
41 #ifdef HAVE_SYS_TIME_H
46 #include <zlib.h> /* to get the libz version number */
52 #ifndef HAVE_GETOPT_LONG
53 #include <wsutil/wsgetopt.h>
56 #include <wsutil/clopts_common.h>
57 #include <wsutil/cmdarg_err.h>
58 #include <wsutil/crash_info.h>
59 #include <wsutil/file_util.h>
60 #include <wsutil/strnatcmp.h>
61 #include <wsutil/ws_diag_control.h>
62 #include <wsutil/ws_version_info.h>
64 #include <wiretap/merge.h>
71 #include <wsutil/unicode-utils.h>
78 print_usage(FILE *output)
80 fprintf(output, "\n");
81 fprintf(output, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
82 fprintf(output, "\n");
83 fprintf(output, "Output:\n");
84 fprintf(output, " -a concatenate rather than merge files.\n");
85 fprintf(output, " default is to merge based on frame timestamps.\n");
86 fprintf(output, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
87 fprintf(output, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
88 fprintf(output, " -F <capture type> set the output file type; default is pcapng.\n");
89 fprintf(output, " an empty \"-F\" option will list the file types.\n");
90 fprintf(output, " -T <encap type> set the output file encapsulation type;\n");
91 fprintf(output, " default is the same as the first input file.\n");
92 fprintf(output, " an empty \"-T\" option will list the encapsulation types.\n");
93 fprintf(output, "\n");
94 fprintf(output, "Miscellaneous:\n");
95 fprintf(output, " -h display this help and exit.\n");
96 fprintf(output, " -v verbose output.\n");
100 * Report an error in command-line arguments.
103 mergecap_cmdarg_err(const char *fmt, va_list ap)
105 fprintf(stderr, "mergecap: ");
106 vfprintf(stderr, fmt, ap);
107 fprintf(stderr, "\n");
111 * Report additional information for an error in command-line arguments.
114 mergecap_cmdarg_err_cont(const char *fmt, va_list ap)
116 vfprintf(stderr, fmt, ap);
117 fprintf(stderr, "\n");
121 const char *sstr; /* The short string */
122 const char *lstr; /* The long string */
126 string_compare(gconstpointer a, gconstpointer b)
128 return strcmp(((const struct string_elem *)a)->sstr,
129 ((const struct string_elem *)b)->sstr);
133 string_nat_compare(gconstpointer a, gconstpointer b)
135 return ws_ascii_strnatcmp(((const struct string_elem *)a)->sstr,
136 ((const struct string_elem *)b)->sstr);
140 string_elem_print(gpointer data, gpointer not_used _U_)
142 fprintf(stderr, " %s - %s\n", ((struct string_elem *)data)->sstr,
143 ((struct string_elem *)data)->lstr);
147 list_capture_types(void) {
149 struct string_elem *captypes;
152 captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES_SUBTYPES);
154 fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
155 for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
156 if (wtap_dump_can_open(i)) {
157 captypes[i].sstr = wtap_file_type_subtype_short_string(i);
158 captypes[i].lstr = wtap_file_type_subtype_string(i);
159 list = g_slist_insert_sorted(list, &captypes[i], string_compare);
162 g_slist_foreach(list, string_elem_print, NULL);
168 list_encap_types(void) {
170 struct string_elem *encaps;
173 encaps = g_new(struct string_elem,WTAP_NUM_ENCAP_TYPES);
174 fprintf(stderr, "mergecap: The available encapsulation types for the \"-T\" flag are:\n");
175 for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
176 encaps[i].sstr = wtap_encap_short_string(i);
177 if (encaps[i].sstr != NULL) {
178 encaps[i].lstr = wtap_encap_string(i);
179 list = g_slist_insert_sorted(list, &encaps[i], string_nat_compare);
182 g_slist_foreach(list, string_elem_print, NULL);
188 get_mergecap_compiled_info(GString *str)
191 g_string_append(str, ", ");
193 g_string_append(str, "with libz ");
195 g_string_append(str, ZLIB_VERSION);
196 #else /* ZLIB_VERSION */
197 g_string_append(str, "(version unknown)");
198 #endif /* ZLIB_VERSION */
199 #else /* HAVE_LIBZ */
200 g_string_append(str, "without libz");
201 #endif /* HAVE_LIBZ */
205 get_mergecap_runtime_info(GString *str)
208 #if defined(HAVE_LIBZ) && !defined(_WIN32)
209 g_string_append_printf(str, ", with libz %s", zlibVersion());
214 main(int argc, char *argv[])
216 GString *comp_info_str;
217 GString *runtime_info_str;
220 static const struct option long_options[] = {
221 {(char *)"help", no_argument, NULL, 'h'},
222 {(char *)"version", no_argument, NULL, 'V'},
226 gboolean do_append = FALSE;
227 gboolean verbose = FALSE;
228 int in_file_count = 0;
230 #ifdef PCAP_NG_DEFAULT
231 int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */
233 int file_type = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */
237 merge_in_file_t *in_files = NULL, *in_file;
239 struct wtap_pkthdr *phdr, snap_phdr;
241 int open_err, read_err = 0, write_err, close_err;
242 gchar *err_info, *write_err_info = NULL;
244 char *out_filename = NULL;
245 gboolean got_read_error = FALSE, got_write_error = FALSE;
248 cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
251 arg_list_utf_16to8(argc, argv);
252 create_app_running_mutex();
255 /* Get the compile-time version information string */
256 comp_info_str = get_compiled_version_info(NULL, get_mergecap_compiled_info);
258 /* Get the run-time version information string */
259 runtime_info_str = get_runtime_version_info(get_mergecap_runtime_info);
261 /* Add it to the information to be reported on a crash. */
262 ws_add_crash_info("Mergecap (Wireshark) %s\n"
267 get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
269 /* Process the options first */
270 while ((opt = getopt_long(argc, argv, "aF:hs:T:vVw:", long_options, NULL)) != -1) {
274 do_append = !do_append;
278 file_type = wtap_short_string_to_file_type_subtype(optarg);
280 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
282 list_capture_types();
288 printf("Mergecap (Wireshark) %s\n"
289 "Merge two or more capture files into one.\n"
290 "See http://www.wireshark.org for more information.\n",
291 get_ws_vcs_version_info());
297 snaplen = get_positive_int(optarg, "snapshot length");
301 frame_type = wtap_short_string_to_encap(optarg);
302 if (frame_type < 0) {
303 fprintf(stderr, "mergecap: \"%s\" isn't a valid encapsulation type\n",
315 show_version("Mergecap (Wireshark)", comp_info_str, runtime_info_str);
316 g_string_free(comp_info_str, TRUE);
317 g_string_free(runtime_info_str, TRUE);
322 out_filename = optarg;
325 case '?': /* Bad options if GNU getopt */
328 list_capture_types();
341 /* check for proper args; at a minimum, must have an output
342 * filename and one input file
344 in_file_count = argc - optind;
346 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
347 fprintf(stderr, " run with -h for help\n");
350 if (in_file_count < 1) {
351 fprintf(stderr, "mergecap: No input files were specified\n");
355 /* open the input files */
356 if (!merge_open_in_files(in_file_count, &argv[optind], &in_files,
357 &open_err, &err_info, &err_fileno)) {
358 fprintf(stderr, "mergecap: Can't open %s: %s\n", argv[optind + err_fileno],
359 wtap_strerror(open_err));
360 if (err_info != NULL) {
361 fprintf(stderr, "(%s)\n", err_info);
368 for (i = 0; i < in_file_count; i++)
369 fprintf(stderr, "mergecap: %s is type %s.\n", argv[optind + i],
370 wtap_file_type_subtype_string(wtap_file_type_subtype(in_files[i].wth)));
375 * Snapshot length not specified - default to the maximum of the
376 * snapshot lengths of the input files.
378 snaplen = merge_max_snapshot_length(in_file_count, in_files);
381 /* set the outfile frame type */
382 if (frame_type == -2) {
384 * Default to the appropriate frame type for the input files.
386 frame_type = merge_select_frame_type(in_file_count, in_files);
388 if (frame_type == WTAP_ENCAP_PER_PACKET) {
390 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
392 int first_frame_type, this_frame_type;
394 first_frame_type = wtap_file_encap(in_files[0].wth);
395 for (i = 1; i < in_file_count; i++) {
396 this_frame_type = wtap_file_encap(in_files[i].wth);
397 if (first_frame_type != this_frame_type) {
398 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
399 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
400 fprintf(stderr, " %s had type %s (%s)\n",
401 in_files[0].filename,
402 wtap_encap_string(first_frame_type),
403 wtap_encap_short_string(first_frame_type));
404 fprintf(stderr, " %s had type %s (%s)\n",
405 in_files[i].filename,
406 wtap_encap_string(this_frame_type),
407 wtap_encap_short_string(this_frame_type));
412 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
413 wtap_encap_string(frame_type),
414 wtap_encap_short_string(frame_type));
418 /* open the outfile */
419 if (strncmp(out_filename, "-", 2) == 0) {
420 /* use stdout as the outfile */
421 out_fd = 1 /*stdout*/;
423 /* open the outfile */
424 out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
426 fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
427 out_filename, g_strerror(errno));
432 /* prepare the outfile */
433 if(file_type == WTAP_FILE_TYPE_SUBTYPE_PCAPNG ){
434 wtapng_section_t *shb_hdr;
435 GString *comment_gstr;
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 pdh = wtap_dump_fdopen_ng(out_fd, file_type, frame_type, snaplen,
451 FALSE /* compressed */, shb_hdr, NULL /* wtapng_iface_descriptions_t *idb_inf */, &open_err);
452 g_string_free(comment_gstr, TRUE);
454 pdh = wtap_dump_fdopen(out_fd, file_type, frame_type, snaplen, FALSE /* compressed */, &open_err);
457 merge_close_in_files(in_file_count, in_files);
459 fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
460 wtap_strerror(open_err));
464 /* do the merge (or append) */
468 in_file = merge_append_read_packet(in_file_count, in_files, &read_err,
471 in_file = merge_read_packet(in_file_count, in_files, &read_err,
473 if (in_file == NULL) {
479 /* I/O error reading from in_file */
480 got_read_error = TRUE;
485 fprintf(stderr, "Record: %d\n", count++);
487 /* We simply write it, perhaps after truncating it; we could do other
488 * things, like modify it. */
489 phdr = wtap_phdr(in_file->wth);
490 if (snaplen != 0 && phdr->caplen > snaplen) {
492 snap_phdr.caplen = snaplen;
496 if (!wtap_dump(pdh, phdr, wtap_buf_ptr(in_file->wth), &write_err, &write_err_info)) {
497 got_write_error = TRUE;
502 merge_close_in_files(in_file_count, in_files);
503 if (!got_write_error) {
504 if (!wtap_dump_close(pdh, &write_err))
505 got_write_error = TRUE;
508 * We already got a write error; no need to report another
509 * write error on close.
511 * Don't overwrite the earlier write error.
513 (void)wtap_dump_close(pdh, &close_err);
516 if (got_read_error) {
518 * Find the file on which we got the error, and report the error.
520 for (i = 0; i < in_file_count; i++) {
521 if (in_files[i].state == GOT_ERROR) {
522 fprintf(stderr, "mergecap: Error reading %s: %s\n",
523 in_files[i].filename, wtap_strerror(read_err));
524 if (err_info != NULL) {
525 fprintf(stderr, "(%s)\n", err_info);
532 if (got_write_error) {
535 case WTAP_ERR_UNWRITABLE_ENCAP:
537 * This is a problem with the particular frame we're writing and
538 * the file type and subtype we're wwriting; note that, and
539 * report the frame number and file type/subtype.
541 fprintf(stderr, "mergecap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n",
542 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
543 wtap_file_type_subtype_string(file_type));
546 case WTAP_ERR_PACKET_TOO_LARGE:
548 * This is a problem with the particular frame we're writing and
549 * the file type and subtype we're wwriting; note that, and
550 * report the frame number and file type/subtype.
552 fprintf(stderr, "mergecap: Frame %u of \"%s\" is too large for a \"%s\" file.\n",
553 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
554 wtap_file_type_subtype_string(file_type));
557 case WTAP_ERR_UNWRITABLE_REC_TYPE:
559 * This is a problem with the particular record we're writing and
560 * the file type and subtype we're wwriting; note that, and
561 * report the record number and file type/subtype.
563 fprintf(stderr, "mergecap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
564 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
565 wtap_file_type_subtype_string(file_type));
568 case WTAP_ERR_UNWRITABLE_REC_DATA:
570 * This is a problem with the particular record we're writing and
571 * the file type and subtype we're wwriting; note that, and
572 * report the record number and file type/subtype.
574 fprintf(stderr, "mergecap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
575 in_file ? in_file->packet_num : 0, in_file ? in_file->filename : "UNKNOWN",
576 wtap_file_type_subtype_string(file_type),
577 write_err_info != NULL ? write_err_info : "no information supplied");
578 g_free(write_err_info);
582 fprintf(stderr, "mergecap: Error writing to outfile: %s\n",
583 wtap_strerror(write_err));
590 return (!got_read_error && !got_write_error) ? 0 : 2;
594 * Editor modelines - http://www.wireshark.org/tools/modelines.html
599 * indent-tabs-mode: nil
602 * vi: set shiftwidth=2 tabstop=8 expandtab:
603 * :indentSize=2:tabSize=8:noTabs=true: