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 * SPDX-License-Identifier: GPL-2.0-or-later
9 * Mergecap written by Scott Renfro <scott@renfro.org> based on
10 * editcap by Richard Sharpe and Guy Harris
15 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
21 #include <wsutil/ws_getopt.h>
25 #include <wiretap/wtap.h>
27 #include <wsutil/clopts_common.h>
28 #include <wsutil/cmdarg_err.h>
29 #include <wsutil/filesystem.h>
30 #include <wsutil/file_util.h>
31 #include <wsutil/privileges.h>
32 #include <wsutil/strnatcmp.h>
33 #include <wsutil/ws_assert.h>
34 #include <wsutil/wslog.h>
37 #include <wsutil/version_info.h>
40 #include <wsutil/plugins.h>
43 #include <wiretap/merge.h>
45 #include "ui/failure_message.h"
47 #define LONGOPT_COMPRESS LONGOPT_BASE_APPLICATION+1
53 print_usage(FILE *output
)
55 fprintf(output
, "\n");
56 fprintf(output
, "Usage: mergecap [options] -w <outfile>|- <infile> [<infile> ...]\n");
57 fprintf(output
, "\n");
58 fprintf(output
, "Output:\n");
59 fprintf(output
, " -a concatenate rather than merge files.\n");
60 fprintf(output
, " default is to merge based on frame timestamps.\n");
61 fprintf(output
, " -s <snaplen> truncate packets to <snaplen> bytes of data.\n");
62 fprintf(output
, " -w <outfile>|- set the output filename to <outfile> or '-' for stdout.\n");
63 fprintf(output
, " if the output filename has the .gz extension, it will be compressed to a gzip archive\n");
64 fprintf(output
, " -F <capture type> set the output file type; default is pcapng.\n");
65 fprintf(output
, " an empty \"-F\" option will list the file types.\n");
66 fprintf(output
, " -I <IDB merge mode> set the merge mode for Interface Description Blocks; default is 'all'.\n");
67 fprintf(output
, " an empty \"-I\" option will list the merge modes.\n");
68 fprintf(output
, " --compress <type> compress the output file using the type compression format.\n");
69 fprintf(output
, "\n");
70 fprintf(output
, "Miscellaneous:\n");
71 fprintf(output
, " -h, --help display this help and exit.\n");
72 fprintf(output
, " -V verbose output.\n");
73 fprintf(output
, " -v, --version print version information and exit.\n");
77 * Report an error in command-line arguments.
80 mergecap_cmdarg_err(const char *fmt
, va_list ap
)
82 fprintf(stderr
, "mergecap: ");
83 vfprintf(stderr
, fmt
, ap
);
84 fprintf(stderr
, "\n");
88 * Report additional information for an error in command-line arguments.
91 mergecap_cmdarg_err_cont(const char *fmt
, va_list ap
)
93 vfprintf(stderr
, fmt
, ap
);
94 fprintf(stderr
, "\n");
98 list_capture_types(void) {
99 GArray
*writable_type_subtypes
;
101 fprintf(stderr
, "mergecap: The available capture file types for the \"-F\" flag are:\n");
102 writable_type_subtypes
= wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME
);
103 for (unsigned i
= 0; i
< writable_type_subtypes
->len
; i
++) {
104 int ft
= g_array_index(writable_type_subtypes
, int, i
);
105 fprintf(stderr
, " %s - %s\n", wtap_file_type_subtype_name(ft
),
106 wtap_file_type_subtype_description(ft
));
108 g_array_free(writable_type_subtypes
, TRUE
);
112 list_idb_merge_modes(void) {
115 fprintf(stderr
, "mergecap: The available IDB merge modes for the \"-I\" flag are:\n");
116 for (i
= 0; i
< IDB_MERGE_MODE_MAX
; i
++) {
117 fprintf(stderr
, " %s\n", merge_idb_merge_mode_to_string(i
));
122 list_output_compression_types(void) {
123 GSList
*output_compression_types
;
125 fprintf(stderr
, "mergecap: The available output compress type(s) for the \"--compress\" flag are:\n");
126 output_compression_types
= wtap_get_all_output_compression_type_names_list();
127 for (GSList
*compression_type
= output_compression_types
;
128 compression_type
!= NULL
;
129 compression_type
= g_slist_next(compression_type
)) {
130 fprintf(stderr
, " %s\n", (const char *)compression_type
->data
);
133 g_slist_free(output_compression_types
);
137 merge_callback(merge_event event
, int num
,
138 const merge_in_file_t in_files
[], const unsigned in_file_count
,
145 case MERGE_EVENT_INPUT_FILES_OPENED
:
146 for (i
= 0; i
< in_file_count
; i
++) {
147 fprintf(stderr
, "mergecap: %s is type %s.\n", in_files
[i
].filename
,
148 wtap_file_type_subtype_description(wtap_file_type_subtype(in_files
[i
].wth
)));
152 case MERGE_EVENT_FRAME_TYPE_SELECTED
:
153 /* for this event, num = frame_type */
154 if (num
== WTAP_ENCAP_PER_PACKET
) {
156 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
158 int first_frame_type
, this_frame_type
;
160 first_frame_type
= wtap_file_encap(in_files
[0].wth
);
161 for (i
= 1; i
< in_file_count
; i
++) {
162 this_frame_type
= wtap_file_encap(in_files
[i
].wth
);
163 if (first_frame_type
!= this_frame_type
) {
164 fprintf(stderr
, "mergecap: multiple frame encapsulation types detected\n");
165 fprintf(stderr
, " defaulting to WTAP_ENCAP_PER_PACKET\n");
166 fprintf(stderr
, " %s had type %s (%s)\n",
167 in_files
[0].filename
,
168 wtap_encap_description(first_frame_type
),
169 wtap_encap_name(first_frame_type
));
170 fprintf(stderr
, " %s had type %s (%s)\n",
171 in_files
[i
].filename
,
172 wtap_encap_description(this_frame_type
),
173 wtap_encap_name(this_frame_type
));
178 fprintf(stderr
, "mergecap: selected frame_type %s (%s)\n",
179 wtap_encap_description(num
),
180 wtap_encap_name(num
));
183 case MERGE_EVENT_READY_TO_MERGE
:
184 fprintf(stderr
, "mergecap: ready to merge records\n");
187 case MERGE_EVENT_RECORD_WAS_READ
:
188 /* for this event, num = count */
189 fprintf(stderr
, "Record: %d\n", num
);
192 case MERGE_EVENT_DONE
:
193 fprintf(stderr
, "mergecap: merging complete\n");
197 /* false = do not stop merging */
202 main(int argc
, char *argv
[])
204 char *configuration_init_error
;
206 static const struct ws_option long_options
[] = {
207 {"help", ws_no_argument
, NULL
, 'h'},
208 {"version", ws_no_argument
, NULL
, 'v'},
209 {"compress", ws_required_argument
, NULL
, LONGOPT_COMPRESS
},
212 bool do_append
= false;
213 bool verbose
= false;
214 int in_file_count
= 0;
215 uint32_t snaplen
= 0;
216 int file_type
= WTAP_FILE_TYPE_SUBTYPE_UNKNOWN
;
217 char *out_filename
= NULL
;
219 idb_merge_mode mode
= IDB_MERGE_MODE_MAX
;
220 wtap_compression_type compression_type
= WTAP_UNKNOWN_COMPRESSION
;
221 merge_progress_callback_t cb
;
223 cmdarg_err_init(mergecap_cmdarg_err
, mergecap_cmdarg_err_cont
);
225 /* Initialize log handler early so we can have proper logging during startup. */
226 ws_log_init("mergecap", vcmdarg_err
);
228 /* Early logging command-line initialization. */
229 ws_log_parse_args(&argc
, argv
, vcmdarg_err
, 1);
231 ws_noisy("Finished log init and parsing command line log arguments");
234 create_app_running_mutex();
237 /* Initialize the version information. */
238 ws_init_version_info("Mergecap", NULL
, NULL
);
241 * Get credential information for later use.
243 init_process_policies();
246 * Attempt to get the pathname of the directory containing the
249 configuration_init_error
= configuration_init(argv
[0], NULL
);
250 if (configuration_init_error
!= NULL
) {
252 "Can't get pathname of directory containing the mergecap program: %s.",
253 configuration_init_error
);
254 g_free(configuration_init_error
);
257 init_report_failure_message("mergecap");
261 /* Process the options first */
262 while ((opt
= ws_getopt_long(argc
, argv
, "aF:hI:s:vVw:", long_options
, NULL
)) != -1) {
266 do_append
= !do_append
;
270 file_type
= wtap_name_to_file_type_subtype(ws_optarg
);
272 cmdarg_err("\"%s\" isn't a valid capture file type",
274 list_capture_types();
281 show_help_header("Merge two or more capture files into one.");
287 mode
= merge_string_to_idb_merge_mode(ws_optarg
);
288 if (mode
== IDB_MERGE_MODE_MAX
) {
289 cmdarg_err("\"%s\" isn't a valid IDB merge mode",
291 list_idb_merge_modes();
298 snaplen
= get_nonzero_uint32(ws_optarg
, "snapshot length");
311 out_filename
= ws_optarg
;
314 case LONGOPT_COMPRESS
:
315 compression_type
= wtap_name_to_compression_type(ws_optarg
);
316 if (compression_type
== WTAP_UNKNOWN_COMPRESSION
) {
317 cmdarg_err("\"%s\" isn't a valid output compression mode",
319 list_output_compression_types();
323 case '?': /* Bad options if GNU getopt */
326 list_capture_types();
329 list_idb_merge_modes();
331 case LONGOPT_COMPRESS
:
332 list_output_compression_types();
343 /* Default to pcapng when writing. */
344 if (file_type
== WTAP_FILE_TYPE_SUBTYPE_UNKNOWN
)
345 file_type
= wtap_pcapng_file_type_subtype();
347 cb
.callback_func
= merge_callback
;
350 /* check for proper args; at a minimum, must have an output
351 * filename and one input file
353 in_file_count
= argc
- ws_optind
;
355 cmdarg_err("an output filename must be set with -w");
356 cmdarg_err_cont("run with -h for help");
360 if (in_file_count
< 1) {
361 cmdarg_err("No input files were specified");
365 if (compression_type
== WTAP_UNKNOWN_COMPRESSION
) {
366 /* An explicitly specified compression type overrides filename
367 * magic. (Should we allow specifying "no" compression with, e.g.
368 * a ".gz" extension?) */
369 const char *sfx
= strrchr(out_filename
, '.');
371 compression_type
= wtap_extension_to_compression_type(sfx
+ 1);
375 if (compression_type
== WTAP_UNKNOWN_COMPRESSION
) {
376 compression_type
= WTAP_UNCOMPRESSED
;
379 if (!wtap_can_write_compression_type(compression_type
)) {
380 cmdarg_err("Output files can't be written as %s",
381 wtap_compression_type_description(compression_type
));
386 if (compression_type
!= WTAP_UNCOMPRESSED
&& !wtap_dump_can_compress(file_type
)) {
387 cmdarg_err("The file format %s can't be written to output compressed format",
388 wtap_file_type_subtype_name(file_type
));
394 * Setting IDB merge mode must use a file format that supports
395 * (and thus requires) interface ID and information blocks.
397 if (mode
!= IDB_MERGE_MODE_MAX
&&
398 wtap_file_type_subtype_supports_block(file_type
, WTAP_BLOCK_IF_ID_AND_INFO
) == BLOCK_NOT_SUPPORTED
) {
399 cmdarg_err("The IDB merge mode can only be used with an output format that identifies interfaces");
404 /* if they didn't set IDB merge mode, set it to our default */
405 if (mode
== IDB_MERGE_MODE_MAX
) {
406 mode
= IDB_MERGE_MODE_ALL_SAME
;
409 /* open the outfile */
410 if (strcmp(out_filename
, "-") == 0) {
411 /* merge the files to the standard output */
412 status
= merge_files_to_stdout(file_type
,
413 (const char *const *) &argv
[ws_optind
],
414 in_file_count
, do_append
, mode
, snaplen
,
415 get_appname_and_version(),
416 verbose
? &cb
: NULL
, compression_type
);
418 /* merge the files to the outfile */
419 status
= merge_files(out_filename
, file_type
,
420 (const char *const *) &argv
[ws_optind
], in_file_count
,
421 do_append
, mode
, snaplen
, get_appname_and_version(),
422 verbose
? &cb
: NULL
, compression_type
);
428 return status
? 0 : 2;