Convert BOOTP/DHCP tap stats to new "generic stat API".
[metze/wireshark/wip.git] / tshark.c
index 0cdbb901fbeafba3c6118ddcd2fd6b2a5e1c8e4c..a7adc0ae5f8aa216f9b823b260016824ee538b12 100644 (file)
--- a/tshark.c
+++ b/tshark.c
@@ -44,7 +44,9 @@
 #include <fcntl.h>
 #endif
 
+#ifndef _WIN32
 #include <signal.h>
+#endif
 
 #ifdef HAVE_SYS_STAT_H
 # include <sys/stat.h>
@@ -58,7 +60,7 @@
 # include <sys/capability.h>
 #endif
 
-#ifndef HAVE_GETOPT
+#ifndef HAVE_GETOPT_LONG
 #include "wsutil/wsgetopt.h"
 #endif
 
 #include <wsutil/clopts_common.h>
 #include <wsutil/cmdarg_err.h>
 #include <wsutil/crash_info.h>
-#include <wsutil/privileges.h>
-#include <wsutil/file_util.h>
 #include <wsutil/filesystem.h>
+#include <wsutil/file_util.h>
+#include <wsutil/privileges.h>
 #include <wsutil/report_err.h>
-#include <wsutil/copyright_info.h>
-#include <wsutil/os_version_info.h>
+#include <wsutil/ws_diag_control.h>
 #include <wsutil/ws_version_info.h>
 
 #include "globals.h"
 #include "ui/util.h"
 #include "ui/ui_util.h"
 #include "ui/cli/tshark-tap.h"
-#include "version_info.h"
 #include "register.h"
 #include <epan/epan_dissect.h>
 #include <epan/tap.h>
 #include <epan/stat_tap_ui.h>
-#include <epan/timestamp.h>
 #include <epan/conversation_table.h>
+#include <epan/srt_table.h>
+#include <epan/rtd_table.h>
 #include <epan/ex-opt.h>
 
+#if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
+#include <epan/asn1.h>
+#include <epan/dissectors/packet-kerberos.h>
+#endif
+
 #include "capture_opts.h"
 
 #include "caputils/capture-pcap-util.h"
 #include "caputils/capture_ifinfo.h"
 #ifdef _WIN32
 #include "caputils/capture-wpcap.h"
+#include <wsutil/os_version_info.h>
 #include <wsutil/unicode-utils.h>
 #endif /* _WIN32 */
 #include <capchild/capture_session.h>
 #include <capchild/capture_sync.h>
-#include "capture_opts.h"
 #endif /* HAVE_LIBPCAP */
 #include "log.h"
 #include <epan/funnel.h>
@@ -225,6 +231,8 @@ static void failure_message_cont(const char *msg_format, va_list ap);
 
 capture_file cfile;
 
+static GHashTable *output_only_tables = NULL;
+
 struct string_elem {
   const char *sstr;   /* The short string */
   const char *lstr;   /* The long string */
@@ -304,7 +312,7 @@ print_usage(FILE *output)
 #ifdef HAVE_PCAP_CREATE
   fprintf(output, "  -I                       capture in monitor mode, if available\n");
 #endif
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
   fprintf(output, "  -B <buffer size>         size of kernel buffer (def: %dMB)\n", DEFAULT_CAPTURE_BUFFER_SIZE);
 #endif
   fprintf(output, "  -y <link type>           link layer type (def: first appropriate)\n");
@@ -417,6 +425,7 @@ glossary_option_help(void)
   fprintf(output, "Glossary table reports:\n");
   fprintf(output, "  -G column-formats        dump column format codes and exit\n");
   fprintf(output, "  -G decodes               dump \"layer type\"/\"decode as\" associations and exit\n");
+  fprintf(output, "  -G dissector-tables      dump dissector table names, types, and properties\n");
   fprintf(output, "  -G fields                dump fields glossary and exit\n");
   fprintf(output, "  -G ftypes                dump field type basic and descriptive names\n");
   fprintf(output, "  -G heuristic-decodes     dump heuristic dissector tables\n");
@@ -897,20 +906,6 @@ print_current_user(void) {
   }
 }
 
-static void
-show_version(GString *comp_info_str, GString *runtime_info_str)
-{
-  printf("TShark (Wireshark) %s\n"
-         "\n"
-         "%s"
-         "\n"
-         "%s"
-         "\n"
-         "%s",
-         get_ws_vcs_version_info(), get_copyright_info(),
-         comp_info_str->str, runtime_info_str->str);
-}
-
 static void
 get_tshark_compiled_version_info(GString *str)
 {
@@ -956,12 +951,14 @@ main(int argc, char *argv[])
   GString             *runtime_info_str;
   char                *init_progfile_dir_error;
   int                  opt;
+DIAG_OFF(cast-qual)
   static const struct option long_options[] = {
     {(char *)"help", no_argument, NULL, 'h'},
     {(char *)"version", no_argument, NULL, 'v'},
     LONGOPT_CAPTURE_COMMON
     {0, 0, 0, 0 }
   };
+DIAG_ON(cast-qual)
   gboolean             arg_error = FALSE;
 
 #ifdef _WIN32
@@ -1001,19 +998,38 @@ main(int argc, char *argv[])
 #endif
   dfilter_t           *rfcode = NULL;
   dfilter_t           *dfcode = NULL;
+  gchar               *err_msg;
   e_prefs             *prefs_p;
   char                 badopt;
   int                  log_flags;
-  int                  optind_initial;
   gchar               *output_only = NULL;
 
-/* the leading - ensures that getopt() does not permute the argv[] entries
-   we have to make sure that the first getopt() preserves the content of argv[]
-   for the subsequent getopt_long() call */
-#define OPTSTRING "-2" OPTSTRING_CAPTURE_COMMON "C:d:e:E:F:gG:hH:" "K:lnN:o:O:PqQr:R:S:t:T:u:vVw:W:xX:Y:z:"
+/*
+ * The leading + ensures that getopt_long() does not permute the argv[]
+ * entries.
+ *
+ * We have to make sure that the first getopt_long() preserves the content
+ * of argv[] for the subsequent getopt_long() call.
+ *
+ * We use getopt_long() in both cases to ensure that we're using a routine
+ * whose permutation behavior we can control in the same fashion on all
+ * platforms, and so that, if we ever need to process a long argument before
+ * doing further initialization, we can do so.
+ *
+ * Glibc and Solaris libc document that a leading + disables permutation
+ * of options, regardless of whether POSIXLY_CORRECT is set or not; *BSD
+ * and OS X don't document it, but do so anyway.
+ *
+ * We do *not* use a leading - because the behavior of a leading - is
+ * platform-dependent.
+ */
+#define OPTSTRING "+2" OPTSTRING_CAPTURE_COMMON "C:d:e:E:F:gG:hH:" "K:lnN:o:O:PqQr:R:S:t:T:u:vVw:W:xX:Y:z:"
 
   static const char    optstring[] = OPTSTRING;
 
+  /* Set the C-language locale to the native environment. */
+  setlocale(LC_ALL, "");
+
   cmdarg_err_init(failure_message, failure_message_cont);
 
 #ifdef _WIN32
@@ -1036,7 +1052,7 @@ main(int argc, char *argv[])
   /*
    * Attempt to get the pathname of the executable file.
    */
-  init_progfile_dir_error = init_progfile_dir(argv[0], main);
+  init_progfile_dir_error = init_progfile_dir(argv[0], (void *)main);
   if (init_progfile_dir_error != NULL) {
     fprintf(stderr, "tshark: Can't get pathname of tshark program: %s.\n",
             init_progfile_dir_error);
@@ -1055,14 +1071,12 @@ main(int argc, char *argv[])
   }
 #endif
 
-  /* Assemble the compile-time version information string */
-  comp_info_str = g_string_new("Compiled ");
-  get_compiled_version_info(comp_info_str, get_tshark_compiled_version_info,
-                            epan_get_compiled_version_info);
+  /* Get the compile-time version information string */
+  comp_info_str = get_compiled_version_info(get_tshark_compiled_version_info,
+                                            epan_get_compiled_version_info);
 
-  /* Assemble the run-time version information string */
-  runtime_info_str = g_string_new("Running ");
-  get_runtime_version_info(runtime_info_str, get_tshark_runtime_version_info);
+  /* Get the run-time version information string */
+  runtime_info_str = get_runtime_version_info(get_tshark_runtime_version_info);
 
   /* Add it to the information to be reported on a crash. */
   ws_add_crash_info("TShark (Wireshark) %s\n"
@@ -1071,15 +1085,24 @@ main(int argc, char *argv[])
          "\n"
          "%s",
       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
+  g_string_free(comp_info_str, TRUE);
+  g_string_free(runtime_info_str, TRUE);
 
   /*
    * In order to have the -X opts assigned before the wslua machine starts
-   * we need to call getopts before epan_init() gets called.
+   * we need to call getopt_long before epan_init() gets called.
+   *
+   * In order to handle, for example, -o options, we also need to call it
+   * *after* epan_init() gets called, so that the dissectors have had a
+   * chance to register their preferences.
+   *
+   * XXX - can we do this all with one getopt_long() call, saving the
+   * arguments we can't handle until after initializing libwireshark,
+   * and then process them after initializing libwireshark?
    */
   opterr = 0;
-  optind_initial = optind;
 
-  while ((opt = getopt(argc, argv, optstring)) != -1) {
+  while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
     switch (opt) {
     case 'C':        /* Configuration Profile */
       if (profile_exists (optarg, FALSE)) {
@@ -1124,11 +1147,6 @@ main(int argc, char *argv[])
   if (print_summary == -1)
     print_summary = (print_details || print_hex) ? FALSE : TRUE;
 
-  optind = optind_initial;
-  opterr = 1;
-
-
-
 /** Send All g_log messages to our own handler **/
 
   log_flags =
@@ -1161,7 +1179,7 @@ main(int argc, char *argv[])
 
 #ifdef HAVE_LIBPCAP
   capture_opts_init(&global_capture_opts);
-  capture_session_init(&global_capture_session, (void *)&cfile);
+  capture_session_init(&global_capture_session, &cfile);
 #endif
 
   timestamp_set_type(TS_RELATIVE);
@@ -1200,7 +1218,10 @@ main(int argc, char *argv[])
 #endif
   register_all_tap_listeners();
   conversation_table_set_gui_info(init_iousers);
-  hostlist_table_set_gui_info(NULL);  /* XXX - TODO: Provide "GUI" function for TShark */
+  hostlist_table_set_gui_info(init_hostlists);
+  srt_table_iterate_tables(register_srt_tables, NULL);
+  rtd_table_iterate_tables(register_rtd_tables, NULL);
+  new_stat_tap_iterate_tables(register_simple_stat_tables, NULL);
 
   /* If invoked with the "-G" flag, we dump out information based on
      the argument to the "-G" flag; if no argument is specified,
@@ -1227,6 +1248,8 @@ main(int argc, char *argv[])
         dissector_dump_decodes();
       else if (strcmp(argv[2], "defaultprefs") == 0)
         write_prefs(NULL);
+      else if (strcmp(argv[2], "dissector-tables") == 0)
+        dissector_dump_dissector_tables();
       else if (strcmp(argv[2], "fields") == 0)
         proto_registrar_dump_fields();
       else if (strcmp(argv[2], "ftypes") == 0)
@@ -1257,9 +1280,6 @@ main(int argc, char *argv[])
     return 0;
   }
 
-  /* Set the C-language locale to the native environment. */
-  setlocale(LC_ALL, "");
-
   prefs_p = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
                      &pf_open_errno, &pf_read_errno, &pf_path);
   if (gpf_path != NULL) {
@@ -1320,6 +1340,28 @@ main(int argc, char *argv[])
 
   output_fields = output_fields_new();
 
+  /*
+   * To reset the options parser, set optreset to 1 on platforms that
+   * have optreset (documented in *BSD and OS X, apparently present but
+   * not documented in Solaris - the Illumos repository seems to
+   * suggest that the first Solaris getopt_long(), at least as of 2004,
+   * was based on the NetBSD one, it had optreset) and set optind to 1,
+   * and set optind to 0 otherwise (documented as working in the GNU
+   * getopt_long().  Setting optind to 0 didn't originally work in the
+   * NetBSD one, but that was added later - we don't want to depend on
+   * it if we have optreset).
+   *
+   * Also reset opterr to 1, so that error messages are printed by
+   * getopt_long().
+   */
+#ifdef HAVE_OPTRESET
+  optreset = 1;
+  optind = 1;
+#else
+  optind = 0;
+#endif
+  opterr = 1;
+
   /* Now get our args */
   while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
     switch (opt) {
@@ -1343,9 +1385,9 @@ main(int argc, char *argv[])
     case 'w':        /* Write to capture file x */
     case 'y':        /* Set the pcap data link type */
     case  LONGOPT_NUM_CAP_COMMENT: /* add a capture comment */
-#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#ifdef CAN_SET_CAPTURE_BUFFER_SIZE
     case 'B':        /* Buffer size */
-#endif /* _WIN32 or HAVE_PCAP_CREATE */
+#endif
 #ifdef HAVE_LIBPCAP
       status = capture_opts_add_opt(&global_capture_opts, opt, optarg, &start_capture);
       if (status != 0) {
@@ -1365,7 +1407,7 @@ main(int argc, char *argv[])
 #endif
       break;
     case 'C':
-      /* Configuration profile settings were already processed just ignore them this time*/
+      /* already processed; just ignore it now */
       break;
     case 'd':        /* Decode as rule */
       if (!add_decode_as(optarg))
@@ -1438,7 +1480,7 @@ main(int argc, char *argv[])
     case 'h':        /* Print help and exit */
       printf("TShark (Wireshark) %s\n"
              "Dump and analyze network traffic.\n"
-             "See http://www.wireshark.org for more information.\n",
+             "See https://www.wireshark.org for more information.\n",
              get_ws_vcs_version_info());
       print_usage(stdout);
       return 0;
@@ -1613,8 +1655,10 @@ main(int argc, char *argv[])
       }
       break;
     case 'v':         /* Show version and exit */
-    {
-      show_version(comp_info_str, runtime_info_str);
+      comp_info_str = get_compiled_version_info(get_tshark_compiled_version_info,
+                                                epan_get_compiled_version_info);
+      runtime_info_str = get_runtime_version_info(get_tshark_runtime_version_info);
+      show_version("TShark (Wireshark)", comp_info_str, runtime_info_str);
       g_string_free(comp_info_str, TRUE);
       g_string_free(runtime_info_str, TRUE);
       /* We don't really have to cleanup here, but it's a convenient way to test
@@ -1624,7 +1668,6 @@ main(int argc, char *argv[])
        * much more useful. */
       epan_cleanup();
       return 0;
-    }
     case 'O':        /* Only output these protocols */
       /* already processed; just ignore it now */
       break;
@@ -1635,6 +1678,7 @@ main(int argc, char *argv[])
       /* already processed; just ignore it now */
       break;
     case 'X':
+      /* already processed; just ignore it now */
       break;
     case 'Y':
       dfilter = optarg;
@@ -1994,8 +2038,9 @@ main(int argc, char *argv[])
 #endif
 
   if (rfilter != NULL) {
-    if (!dfilter_compile(rfilter, &rfcode)) {
-      cmdarg_err("%s", dfilter_error_msg);
+    if (!dfilter_compile(rfilter, &rfcode, &err_msg)) {
+      cmdarg_err("%s", err_msg);
+      g_free(err_msg);
       epan_cleanup();
 #ifdef HAVE_PCAP_OPEN_DEAD
       {
@@ -2018,8 +2063,9 @@ main(int argc, char *argv[])
   cfile.rfcode = rfcode;
 
   if (dfilter != NULL) {
-    if (!dfilter_compile(dfilter, &dfcode)) {
-      cmdarg_err("%s", dfilter_error_msg);
+    if (!dfilter_compile(dfilter, &dfcode, &err_msg)) {
+      cmdarg_err("%s", err_msg);
+      g_free(err_msg);
       epan_cleanup();
 #ifdef HAVE_PCAP_OPEN_DEAD
       {
@@ -2098,7 +2144,7 @@ main(int argc, char *argv[])
               "Sorry, but TShark has to terminate now.\n"
               "\n"
               "More information and workarounds can be found at\n"
-              "http://wiki.wireshark.org/KnownBugs/OutOfMemory\n");
+              "https://wiki.wireshark.org/KnownBugs/OutOfMemory\n");
       err = ENOMEM;
     }
     ENDTRY;
@@ -2177,7 +2223,7 @@ main(int argc, char *argv[])
       print_packet_counts = TRUE;
 
     if (print_packet_info) {
-      if (!write_preamble(NULL)) {
+      if (!write_preamble(&cfile)) {
         show_print_file_io_error(errno);
         return 2;
       }
@@ -2240,7 +2286,7 @@ main(int argc, char *argv[])
 typedef struct pipe_input_tag {
   gint             source;
   gpointer         user_data;
-  int             *child_process;
+  ws_process_id   *child_process;
   pipe_input_cb_t  input_cb;
   guint            pipe_input_id;
 #ifdef _WIN32
@@ -2313,7 +2359,7 @@ pipe_timer_cb(gpointer data)
 
 
 void
-pipe_input_set_handler(gint source, gpointer user_data, int *child_process, pipe_input_cb_t input_cb)
+pipe_input_set_handler(gint source, gpointer user_data, ws_process_id *child_process, pipe_input_cb_t input_cb)
 {
 
   pipe_input.source         = source;
@@ -2380,7 +2426,7 @@ capture(void)
 {
   gboolean          ret;
   guint             i;
-  GString          *str = g_string_new("");
+  GString          *str;
 #ifdef USE_TSHARK_SELECT
   fd_set            readfds;
 #endif
@@ -2439,30 +2485,7 @@ capture(void)
     global_capture_opts.ifaces = g_array_remove_index(global_capture_opts.ifaces, i);
     g_array_insert_val(global_capture_opts.ifaces, i, interface_opts);
   }
-#ifdef _WIN32
-  if (global_capture_opts.ifaces->len < 2)
-#else
-  if (global_capture_opts.ifaces->len < 4)
-#endif
-  {
-    for (i = 0; i < global_capture_opts.ifaces->len; i++) {
-      interface_options interface_opts;
-
-      interface_opts = g_array_index(global_capture_opts.ifaces, interface_options, i);
-      if (i > 0) {
-          if (global_capture_opts.ifaces->len > 2) {
-              g_string_append_printf(str, ",");
-          }
-          g_string_append_printf(str, " ");
-          if (i == global_capture_opts.ifaces->len - 1) {
-              g_string_append_printf(str, "and ");
-          }
-      }
-      g_string_append_printf(str, "'%s'", interface_opts.descr);
-    }
-  } else {
-    g_string_append_printf(str, "%u interfaces", global_capture_opts.ifaces->len);
-  }
+  str = get_iface_list_string(&global_capture_opts, IFLIST_QUOTE_IF_DESCRIPTION);
   if (really_quiet == FALSE)
     fprintf(stderr, "Capturing on %s\n", str->str);
   fflush(stderr);
@@ -2528,7 +2551,7 @@ capture(void)
             "Sorry, but TShark has to terminate now.\n"
             "\n"
             "More information and workarounds can be found at\n"
-            "http://wiki.wireshark.org/KnownBugs/OutOfMemory\n");
+            "https://wiki.wireshark.org/KnownBugs/OutOfMemory\n");
     exit(1);
   }
   ENDTRY;
@@ -2555,7 +2578,7 @@ capture_input_cfilter_error_message(capture_session *cap_session, guint i, char
   g_assert(i < capture_opts->ifaces->len);
   interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
 
-  if (dfilter_compile(interface_opts.cfilter, &rfcode) && rfcode != NULL) {
+  if (dfilter_compile(interface_opts.cfilter, &rfcode, NULL) && rfcode != NULL) {
     cmdarg_err(
       "Invalid capture filter \"%s\" for interface '%s'.\n"
       "\n"
@@ -3062,12 +3085,12 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
   guint        tap_flags;
   wtapng_section_t            *shb_hdr;
   wtapng_iface_descriptions_t *idb_inf;
-  char         appname[100];
+  char         *appname = NULL;
   struct wtap_pkthdr phdr;
   Buffer       buf;
   epan_dissect_t *edt = NULL;
 
-  memset(&phdr, 0, sizeof(struct wtap_pkthdr));
+  wtap_phdr_init(&phdr);
 
   shb_hdr = wtap_file_get_shb_info(cf->wth);
   idb_inf = wtap_file_get_idb_info(cf->wth);
@@ -3092,7 +3115,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
     }
     /* If we don't have an application name add Tshark */
     if (shb_hdr->shb_user_appl == NULL) {
-        g_snprintf(appname, sizeof(appname), "TShark (Wireshark) %s", get_ws_vcs_version_info());
+        appname = g_strdup_printf("TShark (Wireshark) %s", get_ws_vcs_version_info());
         shb_hdr->shb_user_appl = appname;
     }
 
@@ -3111,11 +3134,11 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
       /* We couldn't set up to write to the capture file. */
       switch (err) {
 
-      case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
+      case WTAP_ERR_UNWRITABLE_FILE_TYPE:
         cmdarg_err("Capture files can't be written in that format.");
         break;
 
-      case WTAP_ERR_UNSUPPORTED_ENCAP:
+      case WTAP_ERR_UNWRITABLE_ENCAP:
       case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
         cmdarg_err("The capture file being read can't be written as a "
           "\"%s\" file.", wtap_file_type_subtype_short_string(out_file_type));
@@ -3233,11 +3256,11 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
              filter, so, if we're writing to a capture file, write
              this packet out. */
           if (pdh != NULL) {
-            if (!wtap_dump(pdh, &phdr, ws_buffer_start_ptr(&buf), &err)) {
+            if (!wtap_dump(pdh, &phdr, ws_buffer_start_ptr(&buf), &err, &err_info)) {
               /* Error writing to a capture file */
               switch (err) {
 
-              case WTAP_ERR_UNSUPPORTED_ENCAP:
+              case WTAP_ERR_UNWRITABLE_ENCAP:
                 /*
                  * This is a problem with the particular frame we're writing
                  * and the file type and subtype we're writing; note that,
@@ -3267,12 +3290,45 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
                         wtap_file_type_subtype_short_string(out_file_type));
                 break;
 
+              case WTAP_ERR_UNWRITABLE_REC_TYPE:
+                /*
+                 * This is a problem with the particular record we're writing
+                 * and the file type and subtype we're writing; note that,
+                 * and report the record number and file type/subtype.
+                 *
+                 * XXX - framenum is not necessarily the record number in
+                 * the input file if there was a read filter.
+                 */
+                fprintf(stderr,
+                        "Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
+                        framenum, cf->filename,
+                        wtap_file_type_subtype_short_string(out_file_type));
+                break;
+
+              case WTAP_ERR_UNWRITABLE_REC_DATA:
+                /*
+                 * This is a problem with the particular record we're writing
+                 * and the file type and subtype we're writing; note that,
+                 * and report the record number and file type/subtype.
+                 *
+                 * XXX - framenum is not necessarily the record number in
+                 * the input file if there was a read filter.
+                 */
+                fprintf(stderr,
+                        "Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
+                        framenum, cf->filename,
+                        wtap_file_type_subtype_short_string(out_file_type),
+                        err_info != NULL ? err_info : "no information supplied");
+                g_free(err_info);
+                break;
+
               default:
                 show_capture_file_io_error(save_file, err, FALSE);
                 break;
               }
               wtap_dump_close(pdh, &err);
               g_free(shb_hdr);
+              g_free(appname);
               exit(2);
             }
           }
@@ -3316,11 +3372,11 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
            filter, so, if we're writing to a capture file, write
            this packet out. */
         if (pdh != NULL) {
-          if (!wtap_dump(pdh, wtap_phdr(cf->wth), wtap_buf_ptr(cf->wth), &err)) {
+          if (!wtap_dump(pdh, wtap_phdr(cf->wth), wtap_buf_ptr(cf->wth), &err, &err_info)) {
             /* Error writing to a capture file */
             switch (err) {
 
-            case WTAP_ERR_UNSUPPORTED_ENCAP:
+            case WTAP_ERR_UNWRITABLE_ENCAP:
               /*
                * This is a problem with the particular frame we're writing
                * and the file type and subtype we're writing; note that,
@@ -3344,12 +3400,39 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
                       wtap_file_type_subtype_short_string(out_file_type));
               break;
 
+            case WTAP_ERR_UNWRITABLE_REC_TYPE:
+              /*
+               * This is a problem with the particular record we're writing
+               * and the file type and subtype we're writing; note that,
+               * and report the record number and file type/subtype.
+               */
+              fprintf(stderr,
+                      "Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n",
+                      framenum, cf->filename,
+                      wtap_file_type_subtype_short_string(out_file_type));
+              break;
+
+            case WTAP_ERR_UNWRITABLE_REC_DATA:
+              /*
+               * This is a problem with the particular record we're writing
+               * and the file type and subtype we're writing; note that,
+               * and report the record number and file type/subtype.
+               */
+              fprintf(stderr,
+                      "Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n",
+                      framenum, cf->filename,
+                      wtap_file_type_subtype_short_string(out_file_type),
+                      err_info != NULL ? err_info : "no information supplied");
+              g_free(err_info);
+              break;
+
             default:
               show_capture_file_io_error(save_file, err, FALSE);
               break;
             }
             wtap_dump_close(pdh, &err);
             g_free(shb_hdr);
+            g_free(appname);
             exit(2);
           }
         }
@@ -3371,6 +3454,8 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
     }
   }
 
+  wtap_phdr_cleanup(&phdr);
+
   if (err != 0) {
     /*
      * Print a message noting that the read failed somewhere along the line.
@@ -3400,13 +3485,8 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
 
     case WTAP_ERR_UNSUPPORTED:
       cmdarg_err("The file \"%s\" contains record data that TShark doesn't support.\n(%s)",
-                 cf->filename, err_info);
-      g_free(err_info);
-      break;
-
-    case WTAP_ERR_UNSUPPORTED_ENCAP:
-      cmdarg_err("The file \"%s\" has a packet with a network type that TShark doesn't support.\n(%s)",
-                 cf->filename, err_info);
+                 cf->filename,
+                 err_info != NULL ? err_info : "no information supplied");
       g_free(err_info);
       break;
 
@@ -3417,13 +3497,15 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
 
     case WTAP_ERR_BAD_FILE:
       cmdarg_err("The file \"%s\" appears to be damaged or corrupt.\n(%s)",
-                 cf->filename, err_info);
+                 cf->filename,
+                 err_info != NULL ? err_info : "no information supplied");
       g_free(err_info);
       break;
 
     case WTAP_ERR_DECOMPRESS:
       cmdarg_err("The compressed file \"%s\" appears to be damaged or corrupt.\n"
-                 "(%s)", cf->filename, err_info);
+                 "(%s)", cf->filename,
+                 err_info != NULL ? err_info : "no information supplied");
       g_free(err_info);
       break;
 
@@ -3464,6 +3546,7 @@ out:
 
   g_free(save_file_string);
   g_free(shb_hdr);
+  g_free(appname);
 
   return err;
 }
@@ -3587,13 +3670,13 @@ write_preamble(capture_file *cf)
   switch (output_action) {
 
   case WRITE_TEXT:
-    return print_preamble(print_stream, cf ? cf->filename : NULL, get_ws_vcs_version_info());
+    return print_preamble(print_stream, cf->filename, get_ws_vcs_version_info());
 
   case WRITE_XML:
     if (print_details)
-      write_pdml_preamble(stdout, cf ? cf->filename : NULL);
+      write_pdml_preamble(stdout, cf->filename);
     else
-      write_psml_preamble(stdout);
+      write_psml_preamble(&cf->cinfo, stdout);
     return !ferror(stdout);
 
   case WRITE_FIELDS:
@@ -3666,21 +3749,23 @@ print_columns(capture_file *cf)
   size_t  buf_offset;
   size_t  column_len;
   size_t  col_len;
+  col_item_t* col_item;
 
   line_bufp = get_line_buf(256);
   buf_offset = 0;
   *line_bufp = '\0';
   for (i = 0; i < cf->cinfo.num_cols; i++) {
+    col_item = &cf->cinfo.columns[i];
     /* Skip columns not marked as visible. */
     if (!get_column_visible(i))
       continue;
-    switch (cf->cinfo.col_fmt[i]) {
+    switch (col_item->col_fmt) {
     case COL_NUMBER:
-      column_len = col_len = strlen(cf->cinfo.col_data[i]);
+      column_len = col_len = strlen(col_item->col_data);
       if (column_len < 3)
         column_len = 3;
       line_bufp = get_line_buf(buf_offset + column_len);
-      put_spaces_string(line_bufp + buf_offset, cf->cinfo.col_data[i], col_len, column_len);
+      put_spaces_string(line_bufp + buf_offset, col_item->col_data, col_len, column_len);
       break;
 
     case COL_CLS_TIME:
@@ -3691,11 +3776,11 @@ print_columns(capture_file *cf)
     case COL_UTC_TIME:
     case COL_UTC_YMD_TIME:  /* XXX - wider */
     case COL_UTC_YDOY_TIME: /* XXX - wider */
-      column_len = col_len = strlen(cf->cinfo.col_data[i]);
+      column_len = col_len = strlen(col_item->col_data);
       if (column_len < 10)
         column_len = 10;
       line_bufp = get_line_buf(buf_offset + column_len);
-      put_spaces_string(line_bufp + buf_offset, cf->cinfo.col_data[i], col_len, column_len);
+      put_spaces_string(line_bufp + buf_offset, col_item->col_data, col_len, column_len);
       break;
 
     case COL_DEF_SRC:
@@ -3707,11 +3792,11 @@ print_columns(capture_file *cf)
     case COL_DEF_NET_SRC:
     case COL_RES_NET_SRC:
     case COL_UNRES_NET_SRC:
-      column_len = col_len = strlen(cf->cinfo.col_data[i]);
+      column_len = col_len = strlen(col_item->col_data);
       if (column_len < 12)
         column_len = 12;
       line_bufp = get_line_buf(buf_offset + column_len);
-      put_spaces_string(line_bufp + buf_offset, cf->cinfo.col_data[i], col_len, column_len);
+      put_spaces_string(line_bufp + buf_offset, col_item->col_data, col_len, column_len);
       break;
 
     case COL_DEF_DST:
@@ -3723,17 +3808,17 @@ print_columns(capture_file *cf)
     case COL_DEF_NET_DST:
     case COL_RES_NET_DST:
     case COL_UNRES_NET_DST:
-      column_len = col_len = strlen(cf->cinfo.col_data[i]);
+      column_len = col_len = strlen(col_item->col_data);
       if (column_len < 12)
         column_len = 12;
       line_bufp = get_line_buf(buf_offset + column_len);
-      put_string_spaces(line_bufp + buf_offset, cf->cinfo.col_data[i], col_len, column_len);
+      put_string_spaces(line_bufp + buf_offset, col_item->col_data, col_len, column_len);
       break;
 
     default:
-      column_len = strlen(cf->cinfo.col_data[i]);
+      column_len = strlen(col_item->col_data);
       line_bufp = get_line_buf(buf_offset + column_len);
-      put_string(line_bufp + buf_offset, cf->cinfo.col_data[i], column_len);
+      put_string(line_bufp + buf_offset, col_item->col_data, column_len);
       break;
     }
     buf_offset += column_len;
@@ -3753,12 +3838,12 @@ print_columns(capture_file *cf)
        * even if we're only adding " ".
        */
       line_bufp = get_line_buf(buf_offset + 4);
-      switch (cf->cinfo.col_fmt[i]) {
+      switch (col_item->col_fmt) {
 
       case COL_DEF_SRC:
       case COL_RES_SRC:
       case COL_UNRES_SRC:
-        switch (cf->cinfo.col_fmt[i + 1]) {
+        switch (cf->cinfo.columns[i+1].col_fmt) {
 
         case COL_DEF_DST:
         case COL_RES_DST:
@@ -3777,7 +3862,7 @@ print_columns(capture_file *cf)
       case COL_DEF_DL_SRC:
       case COL_RES_DL_SRC:
       case COL_UNRES_DL_SRC:
-        switch (cf->cinfo.col_fmt[i + 1]) {
+        switch (cf->cinfo.columns[i+1].col_fmt) {
 
         case COL_DEF_DL_DST:
         case COL_RES_DL_DST:
@@ -3796,7 +3881,7 @@ print_columns(capture_file *cf)
       case COL_DEF_NET_SRC:
       case COL_RES_NET_SRC:
       case COL_UNRES_NET_SRC:
-        switch (cf->cinfo.col_fmt[i + 1]) {
+        switch (cf->cinfo.columns[i+1].col_fmt) {
 
         case COL_DEF_NET_DST:
         case COL_RES_NET_DST:
@@ -3815,7 +3900,7 @@ print_columns(capture_file *cf)
       case COL_DEF_DST:
       case COL_RES_DST:
       case COL_UNRES_DST:
-        switch (cf->cinfo.col_fmt[i + 1]) {
+        switch (cf->cinfo.columns[i+1].col_fmt) {
 
         case COL_DEF_SRC:
         case COL_RES_SRC:
@@ -3834,7 +3919,7 @@ print_columns(capture_file *cf)
       case COL_DEF_DL_DST:
       case COL_RES_DL_DST:
       case COL_UNRES_DL_DST:
-        switch (cf->cinfo.col_fmt[i + 1]) {
+        switch (cf->cinfo.columns[i+1].col_fmt) {
 
         case COL_DEF_DL_SRC:
         case COL_RES_DL_SRC:
@@ -3853,7 +3938,7 @@ print_columns(capture_file *cf)
       case COL_DEF_NET_DST:
       case COL_RES_NET_DST:
       case COL_UNRES_NET_DST:
-        switch (cf->cinfo.col_fmt[i + 1]) {
+        switch (cf->cinfo.columns[i+1].col_fmt) {
 
         case COL_DEF_NET_SRC:
         case COL_RES_NET_SRC:
@@ -3898,7 +3983,7 @@ print_packet(capture_file *cf, epan_dissect_t *edt)
         break;
 
       case WRITE_XML:
-        proto_tree_write_psml(edt, stdout);
+        write_psml_columns(edt, stdout);
         return !ferror(stdout);
       case WRITE_FIELDS: /*No non-verbose "fields" format */
         g_assert_not_reached();
@@ -3923,7 +4008,7 @@ print_packet(capture_file *cf, epan_dissect_t *edt)
       print_args.print_hex = print_hex;
       print_args.print_dissections = print_details ? print_dissections_expanded : print_dissections_none;
 
-      if (!proto_tree_print(&print_args, edt, print_stream))
+      if (!proto_tree_print(&print_args, edt, output_only_tables, print_stream))
         return FALSE;
       if (!print_hex) {
         if (!print_line(print_stream, 0, separator))
@@ -3932,11 +4017,11 @@ print_packet(capture_file *cf, epan_dissect_t *edt)
       break;
 
     case WRITE_XML:
-      proto_tree_write_pdml(edt, stdout);
+      write_pdml_proto_tree(edt, stdout);
       printf("\n");
       return !ferror(stdout);
     case WRITE_FIELDS:
-      proto_tree_write_fields(output_fields, edt, &cf->cinfo, stdout);
+      write_fields_proto_tree(output_fields, edt, &cf->cinfo, stdout);
       printf("\n");
       return !ferror(stdout);
     }
@@ -4139,8 +4224,9 @@ cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
     case WTAP_ERR_UNSUPPORTED:
       /* Seen only when opening a capture file for reading. */
       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
-               "The file \"%%s\" isn't a capture file in a format TShark understands.\n"
-               "(%s)", err_info);
+                 "The file \"%%s\" contains record data that TShark doesn't support.\n"
+                 "(%s)",
+                 err_info != NULL ? err_info : "no information supplied");
       g_free(err_info);
       errmsg = errmsg_errno;
       break;
@@ -4153,22 +4239,16 @@ cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
       errmsg = errmsg_errno;
       break;
 
-    case WTAP_ERR_UNSUPPORTED_FILE_TYPE:
+    case WTAP_ERR_UNWRITABLE_FILE_TYPE:
       /* Seen only when opening a capture file for writing. */
       errmsg = "TShark doesn't support writing capture files in that format.";
       break;
 
-    case WTAP_ERR_UNSUPPORTED_ENCAP:
-      if (for_writing) {
-        g_snprintf(errmsg_errno, sizeof(errmsg_errno),
-                   "TShark can't save this capture as a \"%s\" file.",
-                   wtap_file_type_subtype_short_string(file_type));
-      } else {
-        g_snprintf(errmsg_errno, sizeof(errmsg_errno),
-                 "The file \"%%s\" is a capture for a network type that TShark doesn't support.\n"
-                 "(%s)", err_info);
-        g_free(err_info);
-      }
+    case WTAP_ERR_UNWRITABLE_ENCAP:
+      /* Seen only when opening a capture file for writing. */
+      g_snprintf(errmsg_errno, sizeof(errmsg_errno),
+                 "TShark can't save this capture as a \"%s\" file.",
+                 wtap_file_type_subtype_short_string(file_type));
       errmsg = errmsg_errno;
       break;
 
@@ -4185,8 +4265,9 @@ cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
     case WTAP_ERR_BAD_FILE:
       /* Seen only when opening a capture file for reading. */
       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
-               "The file \"%%s\" appears to be damaged or corrupt.\n"
-               "(%s)", err_info);
+                 "The file \"%%s\" appears to be damaged or corrupt.\n"
+                 "(%s)",
+                 err_info != NULL ? err_info : "no information supplied");
       g_free(err_info);
       errmsg = errmsg_errno;
       break;
@@ -4215,7 +4296,8 @@ cf_open_error_message(int err, gchar *err_info, gboolean for_writing,
       /* Seen only when opening a capture file for reading. */
       g_snprintf(errmsg_errno, sizeof(errmsg_errno),
                  "The compressed file \"%%s\" appears to be damaged or corrupt.\n"
-                 "(%s)", err_info);
+                 "(%s)",
+                 err_info != NULL ? err_info : "no information supplied");
       g_free(err_info);
       errmsg = errmsg_errno;
       break;
@@ -4286,7 +4368,7 @@ failure_message_cont(const char *msg_format, va_list ap)
 }
 
 /*
- * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
  *
  * Local variables:
  * c-basic-offset: 2