epan/dissectors/packet-xml.c try to decrypt data, but the data doesn't look correct yet
[metze/wireshark/wip.git] / mergecap.c
index c6c97619ef07ac917fbf9f475123f55541ed3047..3a2f39a02fadfb3c5128e6a18096b7572398aa11 100644 (file)
@@ -4,19 +4,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
  * 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
  *
  * Mergecap written by Scott Renfro <scott@renfro.org> based on
  * editcap by Richard Sharpe and Guy Harris
 #include <wsutil/wsgetopt.h>
 #endif
 
 #include <wsutil/wsgetopt.h>
 #endif
 
-#include <wsutil/clopts_common.h>
-#include <wsutil/cmdarg_err.h>
-#include <wsutil/crash_info.h>
+#include <ui/clopts_common.h>
+#include <ui/cmdarg_err.h>
 #include <wsutil/filesystem.h>
 #include <wsutil/file_util.h>
 #include <wsutil/privileges.h>
 #include <wsutil/strnatcmp.h>
 #include <wsutil/filesystem.h>
 #include <wsutil/file_util.h>
 #include <wsutil/privileges.h>
 #include <wsutil/strnatcmp.h>
-#include <ws_version_info.h>
+
+#include <cli_main.h>
+#include <version_info.h>
 
 #ifdef HAVE_PLUGINS
 #include <wsutil/plugins.h>
 #endif
 
 
 #ifdef HAVE_PLUGINS
 #include <wsutil/plugins.h>
 #endif
 
-#include <wsutil/report_err.h>
+#include <wsutil/report_message.h>
 
 #include <wiretap/merge.h>
 
 
 #include <wiretap/merge.h>
 
-#ifdef _WIN32
-#include <wsutil/unicode-utils.h>
-#endif /* _WIN32 */
+#include "ui/failure_message.h"
 
 /*
  * Show the usage
 
 /*
  * Show the usage
@@ -77,7 +64,11 @@ print_usage(FILE *output)
   fprintf(output, "                    default is to merge based on frame timestamps.\n");
   fprintf(output, "  -s <snaplen>      truncate packets to <snaplen> bytes of data.\n");
   fprintf(output, "  -w <outfile>|-    set the output filename to <outfile> or '-' for stdout.\n");
   fprintf(output, "                    default is to merge based on frame timestamps.\n");
   fprintf(output, "  -s <snaplen>      truncate packets to <snaplen> bytes of data.\n");
   fprintf(output, "  -w <outfile>|-    set the output filename to <outfile> or '-' for stdout.\n");
+#ifdef PCAP_NG_DEFAULT
   fprintf(output, "  -F <capture type> set the output file type; default is pcapng.\n");
   fprintf(output, "  -F <capture type> set the output file type; default is pcapng.\n");
+#else
+  fprintf(output, "  -F <capture type> set the output file type; default is pcap.\n");
+#endif
   fprintf(output, "                    an empty \"-F\" option will list the file types.\n");
   fprintf(output, "  -I <IDB merge mode> set the merge mode for Interface Description Blocks; default is 'all'.\n");
   fprintf(output, "                    an empty \"-I\" option will list the merge modes.\n");
   fprintf(output, "                    an empty \"-F\" option will list the file types.\n");
   fprintf(output, "  -I <IDB merge mode> set the merge mode for Interface Description Blocks; default is 'all'.\n");
   fprintf(output, "                    an empty \"-I\" option will list the merge modes.\n");
@@ -127,18 +118,17 @@ string_elem_print(gpointer data, gpointer not_used _U_)
           ((struct string_elem *)data)->lstr);
 }
 
           ((struct string_elem *)data)->lstr);
 }
 
-#ifdef HAVE_PLUGINS
 /*
 /*
- *  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).
+ * General errors and warnings are reported with an console message
+ * in mergecap.
  */
 static void
  */
 static void
-failure_message(const char *msg_format _U_, va_list ap _U_)
+failure_warning_message(const char *msg_format, va_list ap)
 {
 {
-  return;
+  fprintf(stderr, "mergecap: ");
+  vfprintf(stderr, msg_format, ap);
+  fprintf(stderr, "\n");
 }
 }
-#endif
 
 static void
 list_capture_types(void) {
 
 static void
 list_capture_types(void) {
@@ -203,26 +193,26 @@ merge_callback(merge_event event, int num,
             fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
             fprintf(stderr, "          %s had type %s (%s)\n",
                     in_files[0].filename,
             fprintf(stderr, "          defaulting to WTAP_ENCAP_PER_PACKET\n");
             fprintf(stderr, "          %s had type %s (%s)\n",
                     in_files[0].filename,
-                    wtap_encap_string(first_frame_type),
-                    wtap_encap_short_string(first_frame_type));
+                    wtap_encap_description(first_frame_type),
+                    wtap_encap_name(first_frame_type));
             fprintf(stderr, "          %s had type %s (%s)\n",
                     in_files[i].filename,
             fprintf(stderr, "          %s had type %s (%s)\n",
                     in_files[i].filename,
-                    wtap_encap_string(this_frame_type),
-                    wtap_encap_short_string(this_frame_type));
+                    wtap_encap_description(this_frame_type),
+                    wtap_encap_name(this_frame_type));
             break;
           }
         }
       }
       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
             break;
           }
         }
       }
       fprintf(stderr, "mergecap: selected frame_type %s (%s)\n",
-              wtap_encap_string(num),
-              wtap_encap_short_string(num));
+              wtap_encap_description(num),
+              wtap_encap_name(num));
       break;
 
     case MERGE_EVENT_READY_TO_MERGE:
       fprintf(stderr, "mergecap: ready to merge records\n");
       break;
 
       break;
 
     case MERGE_EVENT_READY_TO_MERGE:
       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;
       /* for this event, num = count */
       fprintf(stderr, "Record: %d\n", num);
       break;
@@ -236,12 +226,10 @@ merge_callback(merge_event event, int num,
   return FALSE;
 }
 
   return FALSE;
 }
 
-
 int
 main(int argc, char *argv[])
 {
 int
 main(int argc, char *argv[])
 {
-  GString            *comp_info_str;
-  GString            *runtime_info_str;
+  char               *init_progfile_dir_error;
   int                 opt;
   static const struct option long_options[] = {
       {"help", no_argument, NULL, 'h'},
   int                 opt;
   static const struct option long_options[] = {
       {"help", no_argument, NULL, 'h'},
@@ -251,72 +239,51 @@ main(int argc, char *argv[])
   gboolean            do_append          = FALSE;
   gboolean            verbose            = FALSE;
   int                 in_file_count      = 0;
   gboolean            do_append          = FALSE;
   gboolean            verbose            = FALSE;
   int                 in_file_count      = 0;
-  guint               snaplen            = 0;
+  guint32             snaplen            = 0;
 #ifdef PCAP_NG_DEFAULT
 #ifdef PCAP_NG_DEFAULT
-  int                 file_type          = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcap format */
+  int                 file_type          = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng format */
 #else
 #else
-  int                 file_type          = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcapng format */
+  int                 file_type          = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcap format */
 #endif
 #endif
-  int                 out_fd;
   int                 err                = 0;
   gchar              *err_info           = NULL;
   int                 err_fileno;
   int                 err                = 0;
   gchar              *err_info           = NULL;
   int                 err_fileno;
+  guint32             err_framenum;
   char               *out_filename       = NULL;
   char               *out_filename       = NULL;
-  merge_result        status;
+  merge_result        status             = MERGE_OK;
   idb_merge_mode      mode               = IDB_MERGE_MODE_MAX;
   idb_merge_mode      mode               = IDB_MERGE_MODE_MAX;
-  gboolean            use_stdout         = FALSE;
   merge_progress_callback_t cb;
 
   merge_progress_callback_t cb;
 
-#ifdef HAVE_PLUGINS
-  char  *init_progfile_dir_error;
-#endif
-
   cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
 
 #ifdef _WIN32
   cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
 
 #ifdef _WIN32
-  arg_list_utf_16to8(argc, argv);
   create_app_running_mutex();
 #endif /* _WIN32 */
 
   create_app_running_mutex();
 #endif /* _WIN32 */
 
-  /* Get the compile-time version information string */
-  comp_info_str = get_compiled_version_info(NULL, NULL);
-
-  /* Get the run-time version information string */
-  runtime_info_str = get_runtime_version_info(NULL);
-
-  /* Add it to the information to be reported on a crash. */
-  ws_add_crash_info("Mergecap (Wireshark) %s\n"
-       "\n"
-       "%s"
-       "\n"
-       "%s",
-    get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
+  /* Initialize the version information. */
+  ws_init_version_info("Mergecap (Wireshark)", NULL, NULL, NULL);
 
   /*
    * Get credential information for later use.
    */
   init_process_policies();
 
   /*
    * Get credential information for later use.
    */
   init_process_policies();
-  init_open_routines();
 
 
-#ifdef HAVE_PLUGINS
-  /* Register wiretap plugins */
-  if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
-    g_warning("mergecap: init_progfile_dir(): %s", init_progfile_dir_error);
+  /*
+   * Attempt to get the pathname of the directory containing the
+   * executable file.
+   */
+  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",
+            init_progfile_dir_error);
     g_free(init_progfile_dir_error);
     g_free(init_progfile_dir_error);
-  } else {
-    /* Register all the plugin types we have. */
-    wtap_register_plugin_types(); /* Types known to libwiretap */
-
-    init_report_err(failure_message,NULL,NULL,NULL);
+  }
 
 
-    /* Scan for plugins.  This does *not* call their registration routines;
-       that's done later. */
-    scan_plugins();
+  init_report_message(failure_warning_message, failure_warning_message,
+                      NULL, NULL, NULL);
 
 
-    /* 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) {
 
   /* Process the options first */
   while ((opt = getopt_long(argc, argv, "aF:hI:s:vVw:", long_options, NULL)) != -1) {
@@ -332,17 +299,15 @@ main(int argc, char *argv[])
         fprintf(stderr, "mergecap: \"%s\" isn't a valid capture file type\n",
                 optarg);
         list_capture_types();
         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;
 
     case 'h':
       }
       break;
 
     case 'h':
-      printf("Mergecap (Wireshark) %s\n"
-             "Merge two or more capture files into one.\n"
-             "See https://www.wireshark.org for more information.\n",
-             get_ws_vcs_version_info());
+      show_help_header("Merge two or more capture files into one.");
       print_usage(stdout);
       print_usage(stdout);
-      exit(0);
+      goto clean_exit;
       break;
 
     case 'I':
       break;
 
     case 'I':
@@ -351,12 +316,13 @@ main(int argc, char *argv[])
         fprintf(stderr, "mergecap: \"%s\" isn't a valid IDB merge mode\n",
                 optarg);
         list_idb_merge_modes();
         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;
 
     case 's':
       }
       break;
 
     case 's':
-      snaplen = get_positive_int(optarg, "snapshot length");
+      snaplen = get_nonzero_guint32(optarg, "snapshot length");
       break;
 
     case 'v':
       break;
 
     case 'v':
@@ -364,10 +330,8 @@ main(int argc, char *argv[])
       break;
 
     case 'V':
       break;
 
     case 'V':
-      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);
+      show_version();
+      goto clean_exit;
       break;
 
     case 'w':
       break;
 
     case 'w':
@@ -385,7 +349,8 @@ main(int argc, char *argv[])
       default:
         print_usage(stderr);
       }
       default:
         print_usage(stderr);
       }
-      exit(1);
+      status = MERGE_ERR_INVALID_OPTION;
+      goto clean_exit;
       break;
     }
   }
       break;
     }
   }
@@ -400,7 +365,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");
   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");
   }
   if (in_file_count < 1) {
     fprintf(stderr, "mergecap: No input files were specified\n");
@@ -410,7 +376,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");
   /* 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 */
   }
 
   /* if they didn't set IDB merge mode, set it to our default */
@@ -420,25 +387,22 @@ main(int argc, char *argv[])
 
   /* open the outfile */
   if (strcmp(out_filename, "-") == 0) {
 
   /* open the outfile */
   if (strcmp(out_filename, "-") == 0) {
-    /* use stdout as the outfile */
-    use_stdout = TRUE;
-    out_fd = 1 /*stdout*/;
+    /* merge the files to the standard output */
+    status = merge_files_to_stdout(file_type,
+                                   (const char *const *) &argv[optind],
+                                   in_file_count, do_append, mode, snaplen,
+                                   get_appname_and_version(),
+                                   verbose ? &cb : NULL,
+                                   &err, &err_info, &err_fileno, &err_framenum);
   } else {
   } else {
-    /* open the outfile */
-    out_fd = ws_open(out_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
-    if (out_fd == -1) {
-      fprintf(stderr, "mergecap: Couldn't open output file %s: %s\n",
-              out_filename, g_strerror(errno));
-      exit(1);
-    }
+    /* 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, get_appname_and_version(),
+                         verbose ? &cb : NULL,
+                         &err, &err_info, &err_fileno, &err_framenum);
   }
 
   }
 
-  /* merge the files */
-  status = merge_files(out_fd, 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);
-
   switch (status) {
     case MERGE_OK:
       break;
   switch (status) {
     case MERGE_OK:
       break;
@@ -449,28 +413,42 @@ main(int argc, char *argv[])
       break;
 
     case MERGE_ERR_CANT_OPEN_INFILE:
       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:
       break;
 
     case MERGE_ERR_CANT_OPEN_OUTFILE:
-      fprintf(stderr, "mergecap: Can't open or create %s: %s\n", out_filename,
-                  wtap_strerror(err));
-      if (!use_stdout)
-        ws_close(out_fd);
+      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;
 
       break;
 
-    case MERGE_ERR_CANT_READ_INFILE:      /* fall through */
     case MERGE_ERR_BAD_PHDR_INTERFACE_ID:
     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:
     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:
     case MERGE_ERR_CANT_CLOSE_OUTFILE:
+        cfile_close_failure_message(out_filename, err);
+        break;
+
     default:
     default:
-      fprintf(stderr, "mergecap: %s\n", err_info ? err_info : "unknown error");
+      cmdarg_err("Unknown merge_files error %d", status);
       break;
   }
 
       break;
   }
 
-  g_free(err_info);
-
+clean_exit:
+  wtap_cleanup();
+  free_progdirs();
   return (status == MERGE_OK) ? 0 : 2;
 }
 
   return (status == MERGE_OK) ? 0 : 2;
 }
 
@@ -486,4 +464,3 @@ main(int argc, char *argv[])
  * vi: set shiftwidth=2 tabstop=8 expandtab:
  * :indentSize=2:tabSize=8:noTabs=true:
  */
  * vi: set shiftwidth=2 tabstop=8 expandtab:
  * :indentSize=2:tabSize=8:noTabs=true:
  */
-