SCTP: plug the remaining memleaks in the tap
[metze/wireshark/wip.git] / mergecap.c
index fd2c2838c3153e2f8209ee8d072c450b02ed8ba4..bc55d63bc9dc2357e3575d25610a1c442d03c475 100644 (file)
@@ -4,19 +4,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  *
  * Mergecap written by Scott Renfro <scott@renfro.org> based on
  * editcap by Richard Sharpe and Guy Harris
 #include <wsutil/file_util.h>
 #include <wsutil/privileges.h>
 #include <wsutil/strnatcmp.h>
-#include <ws_version_info.h>
+#include <version_info.h>
 
 #ifdef HAVE_PLUGINS
 #include <wsutil/plugins.h>
 #endif
 
-#include <wsutil/report_err.h>
+#include <wsutil/report_message.h>
 
 #include <wiretap/merge.h>
 
@@ -63,6 +51,8 @@
 #include <wsutil/unicode-utils.h>
 #endif /* _WIN32 */
 
+#include "ui/failure_message.h"
+
 /*
  * Show the usage
  */
@@ -127,18 +117,17 @@ string_elem_print(gpointer data, gpointer not_used _U_)
           ((struct string_elem *)data)->lstr);
 }
 
-#ifdef HAVE_PLUGINS
 /*
- * General errors are reported with an console message in mergecap.
+ * General errors and warnings are reported with an console message
+ * in mergecap.
  */
 static void
-failure_message(const char *msg_format, va_list ap)
+failure_warning_message(const char *msg_format, va_list ap)
 {
   fprintf(stderr, "mergecap: ");
   vfprintf(stderr, msg_format, ap);
   fprintf(stderr, "\n");
 }
-#endif
 
 static void
 list_capture_types(void) {
@@ -222,7 +211,7 @@ merge_callback(merge_event event, int num,
       fprintf(stderr, "mergecap: ready to merge records\n");
       break;
 
-    case MERGE_EVENT_PACKET_WAS_READ:
+    case MERGE_EVENT_RECORD_WAS_READ:
       /* for this event, num = count */
       fprintf(stderr, "Record: %d\n", num);
       break;
@@ -261,10 +250,10 @@ main(int argc, char *argv[])
   int                 err                = 0;
   gchar              *err_info           = NULL;
   int                 err_fileno;
+  guint32             err_framenum;
   char               *out_filename       = NULL;
-  merge_result        status;
+  merge_result        status             = MERGE_OK;
   idb_merge_mode      mode               = IDB_MERGE_MODE_MAX;
-  gboolean            use_stdout         = FALSE;
   merge_progress_callback_t cb;
 
   cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
@@ -299,7 +288,7 @@ main(int argc, char *argv[])
    * Attempt to get the pathname of the directory containing the
    * executable file.
    */
-  init_progfile_dir_error = init_progfile_dir(argv[0], main);
+  init_progfile_dir_error = init_progfile_dir(argv[0]);
   if (init_progfile_dir_error != NULL) {
     fprintf(stderr,
             "mergecap: Can't get pathname of directory containing the mergecap program: %s.\n",
@@ -307,22 +296,10 @@ main(int argc, char *argv[])
     g_free(init_progfile_dir_error);
   }
 
-  wtap_init();
+  init_report_message(failure_warning_message, failure_warning_message,
+                      NULL, NULL, NULL);
 
-#ifdef HAVE_PLUGINS
-  init_report_err(failure_message,NULL,NULL,NULL);
-
-  /* Scan for plugins.  This does *not* call their registration routines;
-     that's done later.
-
-     Don't report failures to load plugins because most (non-wiretap)
-     plugins *should* fail to load (because we're not linked against
-     libwireshark and dissector plugins need libwireshark).*/
-  scan_plugins(DONT_REPORT_LOAD_FAILURE);
-
-  /* Register all libwiretap plugin modules. */
-  register_all_wiretap_modules();
-#endif
+  wtap_init(TRUE);
 
   /* Process the options first */
   while ((opt = getopt_long(argc, argv, "aF:hI:s:vVw:", long_options, NULL)) != -1) {
@@ -338,7 +315,8 @@ main(int argc, char *argv[])
         fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
                 optarg);
         list_capture_types();
-        exit(1);
+        status = MERGE_ERR_INVALID_OPTION;
+        goto clean_exit;
       }
       break;
 
@@ -348,7 +326,7 @@ main(int argc, char *argv[])
              "See https://www.wireshark.org for more information.\n",
              get_ws_vcs_version_info());
       print_usage(stdout);
-      exit(0);
+      goto clean_exit;
       break;
 
     case 'I':
@@ -357,7 +335,8 @@ main(int argc, char *argv[])
         fprintf(stderr, "mergecap: \"%s\" isn't a valid IDB merge mode\n",
                 optarg);
         list_idb_merge_modes();
-        exit(1);
+        status = MERGE_ERR_INVALID_OPTION;
+        goto clean_exit;
       }
       break;
 
@@ -375,7 +354,7 @@ main(int argc, char *argv[])
       show_version("Mergecap (Wireshark)", comp_info_str, runtime_info_str);
       g_string_free(comp_info_str, TRUE);
       g_string_free(runtime_info_str, TRUE);
-      exit(0);
+      goto clean_exit;
       break;
 
     case 'w':
@@ -393,7 +372,8 @@ main(int argc, char *argv[])
       default:
         print_usage(stderr);
       }
-      exit(1);
+      status = MERGE_ERR_INVALID_OPTION;
+      goto clean_exit;
       break;
     }
   }
@@ -408,7 +388,8 @@ main(int argc, char *argv[])
   if (!out_filename) {
     fprintf(stderr, "mergecap: an output filename must be set with -w\n");
     fprintf(stderr, "          run with -h for help\n");
-    return 1;
+    status = MERGE_ERR_INVALID_OPTION;
+    goto clean_exit;
   }
   if (in_file_count < 1) {
     fprintf(stderr, "mergecap: No input files were specified\n");
@@ -418,7 +399,8 @@ main(int argc, char *argv[])
   /* setting IDB merge mode must use PCAPNG output */
   if (mode != IDB_MERGE_MODE_MAX && file_type != WTAP_FILE_TYPE_SUBTYPE_PCAPNG) {
     fprintf(stderr, "The IDB merge mode can only be used with PCAPNG output format\n");
-    return 1;
+    status = MERGE_ERR_INVALID_OPTION;
+    goto clean_exit;
   }
 
   /* if they didn't set IDB merge mode, set it to our default */
@@ -429,18 +411,17 @@ main(int argc, char *argv[])
   /* open the outfile */
   if (strcmp(out_filename, "-") == 0) {
     /* merge the files to the standard output */
-    use_stdout = TRUE;
     status = merge_files_to_stdout(file_type,
                                    (const char *const *) &argv[optind],
                                    in_file_count, do_append, mode, snaplen,
                                    "mergecap", verbose ? &cb : NULL,
-                                   &err, &err_info, &err_fileno);
+                                   &err, &err_info, &err_fileno, &err_framenum);
   } else {
     /* merge the files to the outfile */
     status = merge_files(out_filename, file_type,
                          (const char *const *) &argv[optind], in_file_count,
                          do_append, mode, snaplen, "mergecap", verbose ? &cb : NULL,
-                         &err, &err_info, &err_fileno);
+                         &err, &err_info, &err_fileno, &err_framenum);
   }
 
   switch (status) {
@@ -453,31 +434,42 @@ main(int argc, char *argv[])
       break;
 
     case MERGE_ERR_CANT_OPEN_INFILE:
-      fprintf(stderr, "mergecap: Can't open %s: %s (%s)\n", argv[optind + err_fileno],
-              wtap_strerror(err), err_info ? err_info : "no more information");
+      cfile_open_failure_message("mergecap", argv[optind + err_fileno],
+                                 err, err_info);
       break;
 
     case MERGE_ERR_CANT_OPEN_OUTFILE:
-      if (use_stdout) {
-        fprintf(stderr, "mergecap: Can't set up the standard output: %s\n",
-                    wtap_strerror(err));
-      } else {
-        fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
-                    wtap_strerror(err));
-      }
+      cfile_dump_open_failure_message("mergecap", out_filename, err, file_type);
+      break;
+
+    case MERGE_ERR_CANT_READ_INFILE:
+      cfile_read_failure_message("mergecap", argv[optind + err_fileno],
+                                 err, err_info);
       break;
 
-    case MERGE_ERR_CANT_READ_INFILE:      /* fall through */
     case MERGE_ERR_BAD_PHDR_INTERFACE_ID:
+      cmdarg_err("Record %u of \"%s\" has an interface ID that does not match any IDB in its file.",
+                 err_framenum, argv[optind + err_fileno]);
+      break;
+
     case MERGE_ERR_CANT_WRITE_OUTFILE:
+       cfile_write_failure_message("mergecap", argv[optind + err_fileno],
+                                   out_filename, err, err_info, err_framenum,
+                                   file_type);
+       break;
+
     case MERGE_ERR_CANT_CLOSE_OUTFILE:
+        cfile_close_failure_message(out_filename, err);
+        break;
+
     default:
-      fprintf(stderr, "mergecap: %s\n", err_info ? err_info : "unknown error");
+      cmdarg_err("Unknown merge_files error %d", status);
       break;
   }
 
-  g_free(err_info);
-
+clean_exit:
+  wtap_cleanup();
+  free_progdirs();
   return (status == MERGE_OK) ? 0 : 2;
 }