Qt: Fix leak on CaptureFileDialog preview of file with errors
[wireshark.git] / mergecap.c
blob2cbf01d7660feba13580057f451b9af7ef4da4e6
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
14 #include <config.h>
15 #define WS_LOG_DOMAIN LOG_DOMAIN_MAIN
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <glib.h>
21 #include <wsutil/ws_getopt.h>
23 #include <string.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>
36 #include <cli_main.h>
37 #include <wsutil/version_info.h>
39 #ifdef HAVE_PLUGINS
40 #include <wsutil/plugins.h>
41 #endif
43 #include <wsutil/report_message.h>
45 #include <wiretap/merge.h>
47 #include "ui/failure_message.h"
50 * Show the usage
52 static void
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, " -F <capture type> set the output file type; default is pcapng.\n");
64 fprintf(output, " an empty \"-F\" option will list the file types.\n");
65 fprintf(output, " -I <IDB merge mode> set the merge mode for Interface Description Blocks; default is 'all'.\n");
66 fprintf(output, " an empty \"-I\" option will list the merge modes.\n");
67 fprintf(output, "\n");
68 fprintf(output, "Miscellaneous:\n");
69 fprintf(output, " -h, --help display this help and exit.\n");
70 fprintf(output, " -V verbose output.\n");
71 fprintf(output, " -v, --version print version information and exit.\n");
75 * Report an error in command-line arguments.
77 static void
78 mergecap_cmdarg_err(const char *fmt, va_list ap)
80 fprintf(stderr, "mergecap: ");
81 vfprintf(stderr, fmt, ap);
82 fprintf(stderr, "\n");
86 * Report additional information for an error in command-line arguments.
88 static void
89 mergecap_cmdarg_err_cont(const char *fmt, va_list ap)
91 vfprintf(stderr, fmt, ap);
92 fprintf(stderr, "\n");
95 static void
96 list_capture_types(void) {
97 GArray *writable_type_subtypes;
99 fprintf(stderr, "mergecap: The available capture file types for the \"-F\" flag are:\n");
100 writable_type_subtypes = wtap_get_writable_file_types_subtypes(FT_SORT_BY_NAME);
101 for (guint i = 0; i < writable_type_subtypes->len; i++) {
102 int ft = g_array_index(writable_type_subtypes, int, i);
103 fprintf(stderr, " %s - %s\n", wtap_file_type_subtype_name(ft),
104 wtap_file_type_subtype_description(ft));
106 g_array_free(writable_type_subtypes, TRUE);
109 static void
110 list_idb_merge_modes(void) {
111 int i;
113 fprintf(stderr, "mergecap: The available IDB merge modes for the \"-I\" flag are:\n");
114 for (i = 0; i < IDB_MERGE_MODE_MAX; i++) {
115 fprintf(stderr, " %s\n", merge_idb_merge_mode_to_string(i));
119 static bool
120 merge_callback(merge_event event, int num,
121 const merge_in_file_t in_files[], const guint in_file_count,
122 void *data _U_)
124 guint i;
126 switch (event) {
128 case MERGE_EVENT_INPUT_FILES_OPENED:
129 for (i = 0; i < in_file_count; i++) {
130 fprintf(stderr, "mergecap: %s is type %s.\n", in_files[i].filename,
131 wtap_file_type_subtype_description(wtap_file_type_subtype(in_files[i].wth)));
133 break;
135 case MERGE_EVENT_FRAME_TYPE_SELECTED:
136 /* for this event, num = frame_type */
137 if (num == WTAP_ENCAP_PER_PACKET) {
139 * Find out why we had to choose WTAP_ENCAP_PER_PACKET.
141 int first_frame_type, this_frame_type;
143 first_frame_type = wtap_file_encap(in_files[0].wth);
144 for (i = 1; i < in_file_count; i++) {
145 this_frame_type = wtap_file_encap(in_files[i].wth);
146 if (first_frame_type != this_frame_type) {
147 fprintf(stderr, "mergecap: multiple frame encapsulation types detected\n");
148 fprintf(stderr, " defaulting to WTAP_ENCAP_PER_PACKET\n");
149 fprintf(stderr, " %s had type %s (%s)\n",
150 in_files[0].filename,
151 wtap_encap_description(first_frame_type),
152 wtap_encap_name(first_frame_type));
153 fprintf(stderr, " %s had type %s (%s)\n",
154 in_files[i].filename,
155 wtap_encap_description(this_frame_type),
156 wtap_encap_name(this_frame_type));
157 break;
161 fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
162 wtap_encap_description(num),
163 wtap_encap_name(num));
164 break;
166 case MERGE_EVENT_READY_TO_MERGE:
167 fprintf(stderr, "mergecap: ready to merge records\n");
168 break;
170 case MERGE_EVENT_RECORD_WAS_READ:
171 /* for this event, num = count */
172 fprintf(stderr, "Record: %d\n", num);
173 break;
175 case MERGE_EVENT_DONE:
176 fprintf(stderr, "mergecap: merging complete\n");
177 break;
180 /* false = do not stop merging */
181 return false;
185 main(int argc, char *argv[])
187 char *configuration_init_error;
188 static const struct report_message_routines mergecap_report_routines = {
189 failure_message,
190 failure_message,
191 open_failure_message,
192 read_failure_message,
193 write_failure_message,
194 cfile_open_failure_message,
195 cfile_dump_open_failure_message,
196 cfile_read_failure_message,
197 cfile_write_failure_message,
198 cfile_close_failure_message
200 int opt;
201 static const struct ws_option long_options[] = {
202 {"help", ws_no_argument, NULL, 'h'},
203 {"version", ws_no_argument, NULL, 'v'},
204 {0, 0, 0, 0 }
206 gboolean do_append = FALSE;
207 gboolean verbose = FALSE;
208 int in_file_count = 0;
209 guint32 snaplen = 0;
210 int file_type = WTAP_FILE_TYPE_SUBTYPE_UNKNOWN;
211 int err = 0;
212 gchar *err_info = NULL;
213 int err_fileno;
214 guint32 err_framenum;
215 char *out_filename = NULL;
216 merge_result status = MERGE_OK;
217 idb_merge_mode mode = IDB_MERGE_MODE_MAX;
218 merge_progress_callback_t cb;
220 cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
222 /* Initialize log handler early so we can have proper logging during startup. */
223 ws_log_init("mergecap", vcmdarg_err);
225 /* Early logging command-line initialization. */
226 ws_log_parse_args(&argc, argv, vcmdarg_err, 1);
228 ws_noisy("Finished log init and parsing command line log arguments");
230 #ifdef _WIN32
231 create_app_running_mutex();
232 #endif /* _WIN32 */
234 /* Initialize the version information. */
235 ws_init_version_info("Mergecap", NULL, NULL);
238 * Get credential information for later use.
240 init_process_policies();
243 * Attempt to get the pathname of the directory containing the
244 * executable file.
246 configuration_init_error = configuration_init(argv[0], NULL);
247 if (configuration_init_error != NULL) {
248 fprintf(stderr,
249 "mergecap: Can't get pathname of directory containing the mergecap program: %s.\n",
250 configuration_init_error);
251 g_free(configuration_init_error);
254 init_report_message("mergecap", &mergecap_report_routines);
256 wtap_init(TRUE);
258 /* Process the options first */
259 while ((opt = ws_getopt_long(argc, argv, "aF:hI:s:vVw:", long_options, NULL)) != -1) {
261 switch (opt) {
262 case 'a':
263 do_append = !do_append;
264 break;
266 case 'F':
267 file_type = wtap_name_to_file_type_subtype(ws_optarg);
268 if (file_type < 0) {
269 fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
270 ws_optarg);
271 list_capture_types();
272 status = MERGE_ERR_INVALID_OPTION;
273 goto clean_exit;
275 break;
277 case 'h':
278 show_help_header("Merge two or more capture files into one.");
279 print_usage(stdout);
280 goto clean_exit;
281 break;
283 case 'I':
284 mode = merge_string_to_idb_merge_mode(ws_optarg);
285 if (mode == IDB_MERGE_MODE_MAX) {
286 fprintf(stderr, "mergecap: \"%s\" isn't a valid IDB merge mode\n",
287 ws_optarg);
288 list_idb_merge_modes();
289 status = MERGE_ERR_INVALID_OPTION;
290 goto clean_exit;
292 break;
294 case 's':
295 snaplen = get_nonzero_guint32(ws_optarg, "snapshot length");
296 break;
298 case 'V':
299 verbose = TRUE;
300 break;
302 case 'v':
303 show_version();
304 goto clean_exit;
305 break;
307 case 'w':
308 out_filename = ws_optarg;
309 break;
311 case '?': /* Bad options if GNU getopt */
312 switch(ws_optopt) {
313 case'F':
314 list_capture_types();
315 break;
316 case'I':
317 list_idb_merge_modes();
318 break;
319 default:
320 print_usage(stderr);
322 status = MERGE_ERR_INVALID_OPTION;
323 goto clean_exit;
324 break;
328 /* Default to pcapng when writing. */
329 if (file_type == WTAP_FILE_TYPE_SUBTYPE_UNKNOWN)
330 file_type = wtap_pcapng_file_type_subtype();
332 cb.callback_func = merge_callback;
333 cb.data = NULL;
335 /* check for proper args; at a minimum, must have an output
336 * filename and one input file
338 in_file_count = argc - ws_optind;
339 if (!out_filename) {
340 fprintf(stderr, "mergecap: an output filename must be set with -w\n");
341 fprintf(stderr, " run with -h for help\n");
342 status = MERGE_ERR_INVALID_OPTION;
343 goto clean_exit;
345 if (in_file_count < 1) {
346 fprintf(stderr, "mergecap: No input files were specified\n");
347 return 1;
351 * Setting IDB merge mode must use a file format that supports
352 * (and thus requires) interface ID and information blocks.
354 if (mode != IDB_MERGE_MODE_MAX &&
355 wtap_file_type_subtype_supports_block(file_type, WTAP_BLOCK_IF_ID_AND_INFO) == BLOCK_NOT_SUPPORTED) {
356 fprintf(stderr, "The IDB merge mode can only be used with an output format that identifies interfaces\n");
357 status = MERGE_ERR_INVALID_OPTION;
358 goto clean_exit;
361 /* if they didn't set IDB merge mode, set it to our default */
362 if (mode == IDB_MERGE_MODE_MAX) {
363 mode = IDB_MERGE_MODE_ALL_SAME;
366 /* open the outfile */
367 if (strcmp(out_filename, "-") == 0) {
368 /* merge the files to the standard output */
369 status = merge_files_to_stdout(file_type,
370 (const char *const *) &argv[ws_optind],
371 in_file_count, do_append, mode, snaplen,
372 get_appname_and_version(),
373 verbose ? &cb : NULL,
374 &err, &err_info, &err_fileno, &err_framenum);
375 } else {
376 /* merge the files to the outfile */
377 status = merge_files(out_filename, file_type,
378 (const char *const *) &argv[ws_optind], in_file_count,
379 do_append, mode, snaplen, get_appname_and_version(),
380 verbose ? &cb : NULL,
381 &err, &err_info, &err_fileno, &err_framenum);
384 switch (status) {
385 case MERGE_OK:
386 break;
388 case MERGE_USER_ABORTED:
389 /* we don't catch SIGINT/SIGTERM (yet?), so we couldn't have aborted */
390 ws_assert_not_reached();
391 break;
393 case MERGE_ERR_CANT_OPEN_INFILE:
394 cfile_open_failure_message(argv[ws_optind + err_fileno], err, err_info);
395 break;
397 case MERGE_ERR_CANT_OPEN_OUTFILE:
398 cfile_dump_open_failure_message(out_filename, err, err_info, file_type);
399 break;
401 case MERGE_ERR_CANT_READ_INFILE:
402 cfile_read_failure_message(argv[ws_optind + err_fileno], err, err_info);
403 break;
405 case MERGE_ERR_BAD_PHDR_INTERFACE_ID:
406 cmdarg_err("Record %u of \"%s\" has an interface ID that does not match any IDB in its file.",
407 err_framenum, argv[ws_optind + err_fileno]);
408 break;
410 case MERGE_ERR_CANT_WRITE_OUTFILE:
411 cfile_write_failure_message(argv[ws_optind + err_fileno], out_filename,
412 err, err_info, err_framenum, file_type);
413 break;
415 case MERGE_ERR_CANT_CLOSE_OUTFILE:
416 cfile_close_failure_message(out_filename, err, err_info);
417 break;
419 case MERGE_ERR_INVALID_OPTION:
420 if (err_info) {
421 cmdarg_err("%s", err_info);
423 else {
424 cmdarg_err("Unspecified error with merge option");
426 break;
428 default:
429 cmdarg_err("Unknown merge_files error %d", status);
430 break;
433 clean_exit:
434 wtap_cleanup();
435 free_progdirs();
436 return (status == MERGE_OK) ? 0 : 2;