Update some links.
[obnox/wireshark/wip.git] / tshark.c
index 6d33efcbfe37aa0888ca694dc7a5e9a762c65e0c..5c63d2c7ef99c418ef78f87b4b2d11f6a336ab00 100644 (file)
--- a/tshark.c
+++ b/tshark.c
 #endif
 
 #ifdef NEED_STRERROR_H
-#include "strerror.h"
+#include "wsutil/strerror.h"
 #endif
 
-#ifdef NEED_GETOPT_H
-#include "getopt.h"
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#else
+#include "wsutil/wsgetopt.h"
 #endif
 
 #include <glib.h>
@@ -75,6 +77,7 @@
 #include <epan/addr_resolv.h>
 #include "util.h"
 #include "clopts_common.h"
+#include "console_io.h"
 #include "cmdarg_err.h"
 #include "version_info.h"
 #include <epan/plugins.h>
@@ -87,7 +90,7 @@
 
 #ifdef HAVE_LIBPCAP
 #include "capture_ui_utils.h"
-#include <pcap.h>
+#include "capture_ifinfo.h"
 #include "capture-pcap-util.h"
 #ifdef _WIN32
 #include "capture-wpcap.h"
@@ -112,6 +115,8 @@ static nstime_t prev_cap_ts;
 
 static gboolean print_packet_info;     /* TRUE if we're to print packet information */
 
+static gboolean perform_two_pass_analysis;
+
 /*
  * The way the packet decode is to be written.
  */
@@ -133,12 +138,6 @@ static print_stream_t *print_stream;
 
 static output_fields_t* output_fields  = NULL;
 
-/*
- * Standard secondary message for unexpected errors.
- */
-static const char please_report[] =
-    "Please report this to the Wireshark developers";
-
 #ifdef HAVE_LIBPCAP
 /*
  * TRUE if we're to print packet counts to keep track of captured packets.
@@ -153,7 +152,7 @@ static gboolean infodelay;  /* if TRUE, don't print capture info in SIGINFO handl
 static gboolean infoprint;     /* if TRUE, print capture info after clearing infodelay */
 #endif /* SIGINFO */
 
-static int capture(void);
+static gboolean capture(void);
 static void report_counts(void);
 #ifdef _WIN32
 static BOOL WINAPI capture_cleanup(DWORD);
@@ -183,13 +182,6 @@ static void failure_message(const char *msg_format, va_list ap);
 static void read_failure_message(const char *filename, int err);
 static void write_failure_message(const char *filename, int err);
 
-extern void frame_data_init(frame_data *fdata, capture_file *cf,
-                  const struct wtap_pkthdr *phdr, gint64 offset,
-                  guint32 *cum_bytes,
-                  nstime_t *first_ts,
-                  nstime_t *prev_dis_ts,
-                  nstime_t *prev_cap_ts);
-
 capture_file cfile;
 
 void
@@ -236,7 +228,10 @@ print_usage(gboolean print_ver)
   fprintf(output, "  -f <capture filter>      packet filter in libpcap filter syntax\n");
   fprintf(output, "  -s <snaplen>             packet snapshot length (def: 65535)\n");
   fprintf(output, "  -p                       don't capture in promiscuous mode\n");
-#ifdef _WIN32
+#ifdef HAVE_PCAP_CREATE
+  fprintf(output, "  -I                       capture in monitor mode, if available\n");
+#endif
+#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
   fprintf(output, "  -B <buffer size>         size of kernel buffer (def: 1MB)\n");
 #endif
   fprintf(output, "  -y <link type>           link layer type (def: first appropriate)\n");
@@ -285,8 +280,11 @@ print_usage(gboolean print_ver)
   fprintf(output, "  -E<fieldsoption>=<value> set options for output when -Tfields selected:\n");
   fprintf(output, "     header=y|n            switch headers on and off\n");
   fprintf(output, "     separator=/t|/s|<char> select tab, space, printable character as separator\n");
+  fprintf(output, "     occurrence=f|l|a      print first, last or all occurrences of each field\n");
+  fprintf(output, "     aggregator=,|/s|<char> select comma, space, printable character as aggregator\n");
   fprintf(output, "     quote=d|s|n           select double, single, no quotes for values\n");
   fprintf(output, "  -t ad|a|r|d|dd|e         output format of time stamps (def: r: rel. to first)\n");
+  fprintf(output, "  -u s|hms                 output format of seconds (def: s: seconds)\n");
   fprintf(output, "  -l                       flush standard output after each packet\n");
   fprintf(output, "  -q                       be more quiet on stdout (e.g. when using statistics)\n");
   fprintf(output, "  -X <key>:<value>         eXtension options, see the man page for details\n");
@@ -298,6 +296,36 @@ print_usage(gboolean print_ver)
   fprintf(output, "  -v                       display version info and exit\n");
   fprintf(output, "  -o <name>:<value> ...    override preference setting\n");
   fprintf(output, "  -K <keytab>              keytab file to use for kerberos decryption\n");
+  fprintf(output, "  -G [report]              dump one of several available reports and exit\n");
+  fprintf(output, "                           default report=\"fields\"\n");
+  fprintf(output, "                           use \"-G ?\" for more help\n");
+}
+
+static void
+glossary_option_help(void)
+{
+  FILE *output;
+
+  output = stdout;
+
+  fprintf(output, "TShark " VERSION "%s\n", wireshark_svnversion);
+
+  fprintf(output, "\n");
+  fprintf(output, "Usage: tshark -G [report]\n");
+  fprintf(output, "\n");
+  fprintf(output, "Glossary table reports:\n");
+  fprintf(output, "  -G [fields]              dump glossary in original format and exit\n");
+  fprintf(output, "  -G fields2               dump glossary in format 2 and exit\n");
+  fprintf(output, "  -G fields3               dump glossary in format 3 and exit\n");
+  fprintf(output, "  -G protocols             dump protocols in registration database and exit\n");
+  fprintf(output, "  -G values                dump value, range, true/false strings and exit\n");
+  fprintf(output, "  -G decodes               dump \"layer type\"/\"decode as\" associations and exit\n");
+  fprintf(output, "\n");
+  fprintf(output, "Preference reports:\n");
+  fprintf(output, "  -G defaultprefs          dump default preferences and exit\n");
+  fprintf(output, "  -G currentprefs          dump current preferences and exit\n");
+  fprintf(output, "\n");
+
 }
 
 /*
@@ -738,7 +766,6 @@ main(int argc, char *argv[])
 {
   char                *init_progfile_dir_error;
   int                  opt;
-  extern char         *optarg;
   gboolean             arg_error = FALSE;
 
 #ifdef _WIN32
@@ -752,10 +779,13 @@ main(int argc, char *argv[])
   int                  gdp_open_errno, gdp_read_errno;
   int                  dp_open_errno, dp_read_errno;
   int                  err;
+  int                  exit_status = 0;
 #ifdef HAVE_LIBPCAP
   gboolean             list_link_layer_types = FALSE;
   gboolean             start_capture = FALSE;
   int                  status;
+  GList               *if_list;
+  gchar               *err_str;
 #else
   gboolean             capture_option_specified = FALSE;
 #endif
@@ -766,23 +796,30 @@ main(int argc, char *argv[])
   struct bpf_program   fcode;
 #endif
   dfilter_t           *rfcode = NULL;
-  e_prefs             *prefs;
+  e_prefs             *prefs_p;
   char                 badopt;
   GLogLevelFlags       log_flags;
   int                  optind_initial;
 
-#define OPTSTRING_INIT "a:b:c:C:d:De:E:f:F:G:hi:K:lLnN:o:pqr:R:s:St:T:vVw:xX:y:z:"
 #ifdef HAVE_LIBPCAP
-#ifdef _WIN32
-#define OPTSTRING_WIN32 "B:"
+#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
+#define OPTSTRING_B "B:"
 #else
-#define OPTSTRING_WIN32 ""
-#endif  /* _WIN32 */
-#else
-#define OPTSTRING_WIN32 ""
+#define OPTSTRING_B ""
+#endif  /* _WIN32 or HAVE_PCAP_CREATE */
+#else /* HAVE_LIBPCAP */
+#define OPTSTRING_B ""
 #endif  /* HAVE_LIBPCAP */
 
-  static const char    optstring[] = OPTSTRING_INIT OPTSTRING_WIN32;
+#ifdef HAVE_PCAP_CREATE
+#define OPTSTRING_I "I"
+#else
+#define OPTSTRING_I ""
+#endif
+
+#define OPTSTRING "a:b:" OPTSTRING_B "c:C:d:De:E:f:F:G:hi:" OPTSTRING_I "K:lLnN:o:pPqr:R:s:St:T:u:vVw:xX:y:z:"
+
+  static const char    optstring[] = OPTSTRING;
 
   /*
    * Get credential information for later use.
@@ -812,7 +849,7 @@ main(int argc, char *argv[])
                                  set_profile_name (optarg);
                          } else {
                                  cmdarg_err("Configuration Profile \"%s\" does not exist", optarg);
-                                 exit(1);
+                                 return 1;
                          }
                          break;
                  case 'X':
@@ -863,6 +900,7 @@ main(int argc, char *argv[])
 
   timestamp_set_type(TS_RELATIVE);
   timestamp_set_precision(TS_PREC_AUTO);
+  timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
 
   /* Register all dissectors; we must do this before checking for the
      "-G" flag, as the "-G" flag dumps information registered by the
@@ -915,22 +953,26 @@ main(int argc, char *argv[])
         dissector_dump_decodes();
       else if (strcmp(argv[2], "defaultprefs") == 0)
         write_prefs(NULL);
+      else if (strcmp(argv[2], "?") == 0)
+        glossary_option_help();
+      else if (strcmp(argv[2], "-?") == 0)
+        glossary_option_help();
       else if (strcmp(argv[2], "currentprefs") == 0) {
         read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
             &pf_open_errno, &pf_read_errno, &pf_path);
         write_prefs(NULL);
       } else {
-        cmdarg_err("Invalid \"%s\" option for -G flag", argv[2]);
-        exit(1);
+        cmdarg_err("Invalid \"%s\" option for -G flag, enter -G ? for more help.", argv[2]);
+        return 1;
       }
     }
-    exit(0);
+    return 0;
   }
 
   /* Set the C-language locale to the native environment. */
   setlocale(LC_ALL, "");
 
-  prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
+  prefs_p = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
                      &pf_open_errno, &pf_read_errno, &pf_path);
   if (gpf_path != NULL) {
     if (gpf_open_errno != 0) {
@@ -956,7 +998,7 @@ main(int argc, char *argv[])
   }
 
   /* Set the name resolution code's flags from the preferences. */
-  g_resolv_flags = prefs->name_resolve;
+  g_resolv_flags = prefs_p->name_resolve;
 
   /* Read the disabled protocols file. */
   read_disabled_protos_list(&gdp_path, &gdp_open_errno, &gdp_read_errno,
@@ -988,7 +1030,7 @@ main(int argc, char *argv[])
 
   check_capture_privs();
 
-  init_cap_file(&cfile);
+  cap_file_init(&cfile);
 
   /* Print format defaults to this. */
   print_format = PR_FMT_TEXT;
@@ -1004,16 +1046,19 @@ main(int argc, char *argv[])
       case 'f':        /* capture filter */
       case 'i':        /* Use interface x */
       case 'p':        /* Don't capture in promiscuous mode */
+#ifdef HAVE_PCAP_CREATE
+      case 'I':        /* Capture in monitor mode, if available */
+#endif
       case 's':        /* Set the snapshot (capture) length */
       case 'w':        /* Write to capture file x */
       case 'y':        /* Set the pcap data link type */
-#ifdef _WIN32
+#if defined(_WIN32) || defined(HAVE_PCAP_CREATE)
       case 'B':        /* Buffer size */
-#endif /* _WIN32 */
+#endif /* _WIN32 or HAVE_PCAP_CREATE */
 #ifdef HAVE_LIBPCAP
         status = capture_opts_add_opt(&global_capture_opts, opt, optarg, &start_capture);
         if(status != 0) {
-            exit(status);
+            return status;
         }
 #else
         capture_option_specified = TRUE;
@@ -1025,7 +1070,7 @@ main(int argc, char *argv[])
        break;
       case 'd':        /* Decode as rule */
         if (!add_decode_as(optarg))
-          exit(1);
+          return 1;
         break;
 #if defined(HAVE_HEIMDAL_KERBEROS) || defined(HAVE_MIT_KERBEROS)
       case 'K':        /* Kerberos keytab file */
@@ -1034,8 +1079,23 @@ main(int argc, char *argv[])
 #endif
       case 'D':        /* Print a list of capture devices and exit */
 #ifdef HAVE_LIBPCAP
-        status = capture_opts_list_interfaces(FALSE);
-        exit(status);
+        if_list = capture_interface_list(&err, &err_str);
+        if (if_list == NULL) {
+          switch (err) {
+          case CANT_GET_INTERFACE_LIST:
+            cmdarg_err("%s", err_str);
+            g_free(err_str);
+            break;
+
+          case NO_INTERFACES_FOUND:
+            cmdarg_err("There are no interfaces on which a capture can be done");
+            break;
+          }
+          return 2;
+        }
+        capture_opts_print_interfaces(if_list);
+        free_interface_list(if_list);
+        return 0;
 #else
         capture_option_specified = TRUE;
         arg_error = TRUE;
@@ -1050,7 +1110,7 @@ main(int argc, char *argv[])
         if(!output_fields_set_option(output_fields, optarg)) {
           cmdarg_err("\"%s\" is not a valid field output option=value pair.", optarg);
           output_fields_list_options(stderr);
-          exit(1);
+          return 1;
         }
         break;
       case 'F':
@@ -1058,12 +1118,12 @@ main(int argc, char *argv[])
         if (out_file_type < 0) {
           cmdarg_err("\"%s\" isn't a valid capture file type", optarg);
           list_capture_types();
-          exit(1);
+          return 1;
         }
         break;
       case 'h':        /* Print help and exit */
         print_usage(TRUE);
-        exit(0);
+        return 0;
         break;
       case 'l':        /* "Line-buffer" standard output */
         /* This isn't line-buffering, strictly speaking, it's just
@@ -1088,6 +1148,11 @@ main(int argc, char *argv[])
         arg_error = TRUE;
 #endif
         break;
+#if GLIB_CHECK_VERSION(2,10,0)
+      case 'P':        /* Perform two pass analysis */
+        perform_two_pass_analysis = TRUE;
+        break;
+#endif
       case 'n':        /* No name resolution */
         g_resolv_flags = RESOLV_NONE;
         break;
@@ -1099,7 +1164,7 @@ main(int argc, char *argv[])
           cmdarg_err("-N specifies unknown resolving option '%c';",
                      badopt);
           cmdarg_err_cont( "           Valid options are 'm', 'n', 't', and 'C'");
-          exit(1);
+          return 1;
         }
         break;
       case 'o':        /* Override preference from command line */
@@ -1110,13 +1175,13 @@ main(int argc, char *argv[])
 
         case PREFS_SET_SYNTAX_ERR:
           cmdarg_err("Invalid -o flag \"%s\"", optarg);
-          exit(1);
+          return 1;
           break;
 
         case PREFS_SET_NO_SUCH_PREF:
         case PREFS_SET_OBSOLETE:
           cmdarg_err("-o flag \"%s\" specifies unknown preference", optarg);
-          exit(1);
+          return 1;
           break;
         }
         break;
@@ -1150,7 +1215,7 @@ main(int argc, char *argv[])
             optarg);
           cmdarg_err_cont("It must be \"r\" for relative, \"a\" for absolute,");
           cmdarg_err_cont("\"ad\" for absolute with date, or \"d\" for delta.");
-          exit(1);
+          return 1;
         }
         break;
       case 'T':        /* printing Type */
@@ -1172,7 +1237,18 @@ main(int argc, char *argv[])
         } else {
           cmdarg_err("Invalid -T parameter.");
           cmdarg_err_cont("It must be \"ps\", \"text\", \"pdml\", \"psml\" or \"fields\".");
-          exit(1);
+          return 1;
+        }
+        break;
+      case 'u':        /* Seconds type */
+        if (strcmp(optarg, "s") == 0)
+          timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
+        else if (strcmp(optarg, "hms") == 0)
+          timestamp_set_seconds_type(TS_SECONDS_HOUR_MIN_SEC);
+        else {
+          cmdarg_err("Invalid seconds type \"%s\"", optarg);
+          cmdarg_err_cont("It must be \"s\" for seconds or \"hms\" for hours, minutes and seconds.");
+          return 1;
         }
         break;
       case 'v':         /* Show version and exit */
@@ -1181,7 +1257,7 @@ main(int argc, char *argv[])
         GString             *runtime_info_str;
         /* Assemble the compile-time version information string */
         comp_info_str = g_string_new("Compiled ");
-        get_compiled_version_info(comp_info_str, get_epan_compiled_version_info);
+        get_compiled_version_info(comp_info_str, epan_get_compiled_version_info);
 
         /* Assemble the run-time version information string */
         runtime_info_str = g_string_new("Running ");
@@ -1189,17 +1265,25 @@ main(int argc, char *argv[])
         show_version(comp_info_str, runtime_info_str);
         g_string_free(comp_info_str, TRUE);
         g_string_free(runtime_info_str, TRUE);
-        exit(0);
+        return 0;
         break;
       }
       case 'V':        /* Verbose */
         verbose = TRUE;
+       /*  The user asked for a verbose output, so let's ensure they get it,
+        *  even if they're writing to a file.
+        */
+       print_packet_info = TRUE;
         break;
       case 'x':        /* Print packet data in hex (and ASCII) */
-          print_hex = TRUE;
-          break;
+        print_hex = TRUE;
+       /*  The user asked for hex output, so let's ensure they get it,
+        *  even if they're writing to a file.
+        */
+       print_packet_info = TRUE;
+        break;
       case 'X':
-          break;
+        break;
       case 'z':
         /* We won't call the init function for the stat this soon
            as it would disallow MATE's fields (which are registered
@@ -1210,7 +1294,7 @@ main(int argc, char *argv[])
           cmdarg_err("invalid -z argument.");
           cmdarg_err_cont("  -z argument must be one of :");
           list_stat_cmd_args();
-          exit(1);
+          return 1;
         }
         break;
       default:
@@ -1222,7 +1306,7 @@ main(int argc, char *argv[])
         default:
           print_usage(TRUE);
         }
-        exit(1);
+        return 1;
         break;
     }
   }
@@ -1231,12 +1315,12 @@ main(int argc, char *argv[])
   if(WRITE_FIELDS != output_action && 0 != output_fields_num_fields(output_fields)) {
         cmdarg_err("Output fields were specified with \"-e\", "
             "but \"-Tfields\" was not specified.");
-        exit(1);
+        return 1;
   } else if(WRITE_FIELDS == output_action && 0 == output_fields_num_fields(output_fields)) {
         cmdarg_err("\"-Tfields\" was specified, but no fields were "
                     "specified with \"-e\".");
 
-        exit(1);
+        return 1;
   }
 
   /* If no capture filter or read filter has been specified, and there are
@@ -1248,7 +1332,7 @@ main(int argc, char *argv[])
       if (rfilter != NULL) {
         cmdarg_err("Read filters were specified both with \"-R\" "
             "and with additional command-line arguments");
-        exit(1);
+        return 1;
       }
       rfilter = get_args_as_string(argc, argv, optind);
     } else {
@@ -1256,7 +1340,7 @@ main(int argc, char *argv[])
       if (global_capture_opts.has_cfilter) {
         cmdarg_err("Capture filters were specified both with \"-f\""
             " and with additional command-line arguments");
-        exit(1);
+        return 1;
       }
       global_capture_opts.has_cfilter = TRUE;
       global_capture_opts.cfilter = get_args_as_string(argc, argv, optind);
@@ -1281,7 +1365,7 @@ main(int argc, char *argv[])
     if (strcmp(global_capture_opts.save_file, "-") == 0 && print_packet_info) {
       cmdarg_err("You can't write both raw packet data and dissected packets"
           " to the standard output.");
-      exit(1);
+      return 1;
     }
   }
 #else
@@ -1297,7 +1381,7 @@ main(int argc, char *argv[])
 #endif
   if (arg_error) {
     print_usage(FALSE);
-    exit(1);
+    return 1;
   }
 
   /* We don't support capture filters when reading from a capture file
@@ -1308,7 +1392,7 @@ main(int argc, char *argv[])
     if (global_capture_opts.has_cfilter) {
       cmdarg_err("Only read filters, not capture filters, "
           "can be specified when reading a capture file.");
-      exit(1);
+      return 1;
     }
   }
 #endif
@@ -1316,7 +1400,7 @@ main(int argc, char *argv[])
   if (print_hex) {
     if (output_action != WRITE_TEXT) {
       cmdarg_err("Raw packet hex data can only be printed as text or PostScript");
-      exit(1);
+      return 1;
     }
   }
 
@@ -1327,12 +1411,12 @@ main(int argc, char *argv[])
     if (cf_name) {
       /* Yes - that's bogus. */
       cmdarg_err("You can't specify -L and a capture file to be read.");
-      exit(1);
+      return 1;
     }
     /* No - did they specify a ring buffer option? */
     if (global_capture_opts.multi_files_on) {
       cmdarg_err("Ring buffer requested, but a capture isn't being done.");
-      exit(1);
+      return 1;
     }
   } else {
     if (cf_name) {
@@ -1343,22 +1427,22 @@ main(int argc, char *argv[])
       if (global_capture_opts.multi_files_on) {
         cmdarg_err("Multiple capture files requested, but "
                    "a capture isn't being done.");
-        exit(1);
+        return 1;
       }
       if (global_capture_opts.has_file_duration) {
         cmdarg_err("Switching capture files after a time interval was specified, but "
                    "a capture isn't being done.");
-        exit(1);
+        return 1;
       }
       if (global_capture_opts.has_ring_num_files) {
         cmdarg_err("A ring buffer of capture files was specified, but "
           "a capture isn't being done.");
-        exit(1);
+        return 1;
       }
       if (global_capture_opts.has_autostop_files) {
         cmdarg_err("A maximum number of capture files was specified, but "
           "a capture isn't being done.");
-        exit(1);
+        return 1;
       }
 
       /* Note: TShark now allows the restriction of a _read_ file by packet count
@@ -1368,7 +1452,7 @@ main(int argc, char *argv[])
       if (global_capture_opts.has_autostop_duration) {
         cmdarg_err("A maximum capture time was specified, but "
           "a capture isn't being done.");
-        exit(1);
+        return 1;
       }
     } else {
       /*
@@ -1380,7 +1464,7 @@ main(int argc, char *argv[])
         /* When capturing, we only support writing libpcap format. */
         if (out_file_type != WTAP_FILE_PCAP) {
           cmdarg_err("Live captures can only be saved in libpcap format.");
-          exit(1);
+          return 1;
         }
         if (global_capture_opts.multi_files_on) {
           /* Multiple-file mode doesn't work under certain conditions:
@@ -1390,20 +1474,26 @@ main(int argc, char *argv[])
           if (strcmp(global_capture_opts.save_file, "-") == 0) {
             cmdarg_err("Multiple capture files requested, but "
               "the capture is being written to the standard output.");
-            exit(1);
+            return 1;
           }
           if (global_capture_opts.output_to_pipe) {
             cmdarg_err("Multiple capture files requested, but "
               "the capture file is a pipe.");
-            exit(1);
+            return 1;
           }
           if (!global_capture_opts.has_autostop_filesize &&
              !global_capture_opts.has_file_duration) {
             cmdarg_err("Multiple capture files requested, but "
               "no maximum capture file size or duration was specified.");
-            exit(1);
+            return 1;
           }
         }
+        /* Currently, we don't support read filters when capturing
+           and saving the packets. */
+        if (rfilter != NULL) {
+          cmdarg_err("Read filters aren't supported when capturing and saving the captured packets.");
+          return 1;
+        }
       } else {
         /* They didn't specify a "-w" flag, so we won't be saving to a
            capture file.  Check for options that only make sense if
@@ -1411,12 +1501,12 @@ main(int argc, char *argv[])
         if (global_capture_opts.has_autostop_filesize) {
           cmdarg_err("Maximum capture file size specified, but "
            "capture isn't being saved to a file.");
-          exit(1);
+          return 1;
         }
         if (global_capture_opts.multi_files_on) {
           cmdarg_err("Multiple capture files requested, but "
             "the capture isn't being saved to a file.");
-          exit(1);
+          return 1;
         }
       }
     }
@@ -1438,13 +1528,29 @@ main(int argc, char *argv[])
      of the filter.  We can now process all the "-z" arguments. */
   start_requested_stats();
 
+#ifdef HAVE_LIBPCAP
+  /* We currently don't support taps, or printing dissected packets,
+     if we're writing to a pipe. */
+  if (global_capture_opts.saving_to_file &&
+      global_capture_opts.output_to_pipe) {
+    if (have_tap_listeners()) {
+      cmdarg_err("Taps aren't supported when saving to a pipe.");
+      return 1;
+    }
+    if (print_packet_info) {
+      cmdarg_err("Printing dissected packets isn't supported when saving to a pipe.");
+      return 1;
+    }
+  }
+#endif
+
   /* disabled protocols as per configuration file */
   if (gdp_path == NULL && dp_path == NULL) {
     set_disabled_protos_list();
   }
 
   /* Build the column format array */
-  build_column_format_array(&cfile.cinfo, prefs->num_cols, TRUE);
+  build_column_format_array(&cfile.cinfo, prefs_p->num_cols, TRUE);
 
 #ifdef HAVE_LIBPCAP
   capture_opts_trim_snaplen(&global_capture_opts, MIN_PACKET_SIZE);
@@ -1471,7 +1577,7 @@ main(int argc, char *argv[])
         }
       }
 #endif
-      exit(2);
+      return 2;
     }
   }
   cfile.rfcode = rfcode;
@@ -1520,7 +1626,7 @@ main(int argc, char *argv[])
 
     if (cf_open(&cfile, cf_name, FALSE, &err) != CF_OK) {
       epan_cleanup();
-      exit(2);
+      return 2;
     }
 
     /* Set timestamp precision; there should arguably be a command-line
@@ -1557,10 +1663,10 @@ main(int argc, char *argv[])
     err = load_cap_file(&cfile, NULL, out_file_type, 0, 0);
 #endif
     if (err != 0) {
-      epan_cleanup();
-      exit(2);
+      /* We still dump out the results of taps, etc., as we might have
+         read some packets; however, we exit with an error status. */
+      exit_status = 2;
     }
-    cf_name[0] = '\0';
   } else {
     /* No capture file specified, so we're supposed to do a live capture
        (or get a list of link-layer types for a live capture device);
@@ -1575,27 +1681,43 @@ main(int argc, char *argv[])
       detailed_err = cant_load_winpcap_err("TShark");
       cmdarg_err_cont("%s", detailed_err);
       g_free(detailed_err);
-      exit(2);
+      return 2;
     }
 #endif
 
     /* trim the interface name and exit if that failed */
     if (!capture_opts_trim_iface(&global_capture_opts,
-        (prefs->capture_device) ? get_if_name(prefs->capture_device) : NULL)) {
-        exit(2);
+        (prefs_p->capture_device) ? get_if_name(prefs_p->capture_device) : NULL)) {
+        return 2;
     }
 
     /* if requested, list the link layer types and exit */
     if (list_link_layer_types) {
-        status = capture_opts_list_link_layer_types(&global_capture_opts, FALSE);
-        exit(status);
+        /* Get the list of link-layer types for the capture device. */
+        if_capabilities_t *caps;
+
+        caps = capture_get_if_capabilities(global_capture_opts.iface,
+                                           global_capture_opts.monitor_mode,
+                                           &err_str);
+        if (caps == NULL) {
+            cmdarg_err("%s", err_str);
+            g_free(err_str);
+            return 2;
+        }
+        if (caps->data_link_types == NULL) {
+            cmdarg_err("The capture device \"%s\" has no data link types.", global_capture_opts.iface);
+            return 2;
+        }
+        capture_opts_print_if_capabilities(caps,
+                                           global_capture_opts.monitor_mode);
+        free_if_capabilities(caps);
+        return 0;
     }
 
     if (print_packet_info) {
       if (!write_preamble(NULL)) {
-        err = errno;
-        show_print_file_io_error(err);
-        return err;
+        show_print_file_io_error(errno);
+        return 2;
       }
     } else if (!quiet) {
       /*
@@ -1614,7 +1736,18 @@ main(int argc, char *argv[])
     /* For now, assume libpcap gives microsecond precision. */
     timestamp_set_precision(TS_PREC_AUTO_USEC);
 
+    /*
+     * XXX - this returns FALSE if an error occurred, but it also
+     * returns FALSE if the capture stops because a time limit
+     * was reached (and possibly other limits), so we can't assume
+     * it means an error.
+     *
+     * The capture code is a bit twisty, so it doesn't appear to
+     * be an easy fix.  We just ignore the return value for now.
+     * Instead, pass on the exit status from the capture child.
+     */
     capture();
+    exit_status = global_capture_opts.fork_child_status;
 
     if (print_packet_info) {
       if (!write_finale()) {
@@ -1625,10 +1758,17 @@ main(int argc, char *argv[])
 #else
     /* No - complain. */
     cmdarg_err("This version of TShark was not built with support for capturing packets.");
-    exit(2);
+    return 2;
 #endif
   }
 
+  g_free(cf_name);
+
+#if GLIB_CHECK_VERSION(2,10,0)
+  if (cfile.plist_start != NULL)
+    g_slice_free_chain(frame_data, cfile.plist_start, next);
+#endif
+
   draw_tap_listeners(TRUE);
   funnel_dump_all_text_windows();
   epan_cleanup();
@@ -1636,7 +1776,7 @@ main(int argc, char *argv[])
   output_fields_free(output_fields);
   output_fields = NULL;
 
-  return 0;
+  return exit_status;
 }
 
 /*#define USE_BROKEN_G_MAIN_LOOP*/
@@ -1752,7 +1892,7 @@ pipe_input_set_handler(gint source, gpointer user_data, int *child_process, pipe
 
 
 #ifdef HAVE_LIBPCAP
-static int
+static gboolean
 capture(void)
 {
   gboolean ret;
@@ -1796,10 +1936,17 @@ capture(void)
   /* Catch a CTRL+C event and, if we get it, clean up and exit. */
   SetConsoleCtrlHandler(capture_cleanup, TRUE);
 #else /* _WIN32 */
-  /* Catch SIGINT and SIGTERM and, if we get either of them, clean up
-     and exit. */
+  /* Catch SIGINT and SIGTERM and, if we get either of them,
+     clean up and exit.  If SIGHUP isn't being ignored, catch
+     it too and, if we get it, clean up and exit.
+
+     We restart any read that was in progress, so that it doesn't
+     disrupt reading from the sync pipe.  The signal handler tells
+     the capture child to finish; it will report that it finished,
+     or will exit abnormally, so  we'll stop reading from the sync
+     pipe, pick up the exit status, and quit. */
   action.sa_handler = capture_cleanup;
-  action.sa_flags = 0;
+  action.sa_flags = SA_RESTART;
   sigemptyset(&action.sa_mask);
   sigaction(SIGTERM, &action, NULL);
   sigaction(SIGINT, &action, NULL);
@@ -1809,9 +1956,12 @@ capture(void)
 
 #ifdef SIGINFO
   /* Catch SIGINFO and, if we get it and we're capturing to a file in
-     quiet mode, report the number of packets we've captured. */
+     quiet mode, report the number of packets we've captured.
+
+     Again, restart any read that was in progress, so that it doesn't
+     disrupt reading from the sync pipe. */
   action.sa_handler = report_counts_siginfo;
-  action.sa_flags = 0;
+  action.sa_flags = SA_RESTART;
   sigemptyset(&action.sa_mask);
   sigaction(SIGINFO, &action, NULL);
 #endif /* SIGINFO */
@@ -1828,23 +1978,23 @@ capture(void)
   if (!ret)
     return FALSE;
 
-    /* the actual capture loop
-     *
-     * XXX - glib doesn't seem to provide any event based loop handling.
-     *
-     * XXX - for whatever reason,
-     * calling g_main_loop_new() ends up in 100% cpu load.
-     *
-     * But that doesn't matter: in UNIX we can use select() to find an input
-     * source with something to do.
-     *
-     * But that doesn't matter because we're in a CLI (that doesn't need to
-     * update a GUI or something at the same time) so it's OK if we block
-     * trying to read from the pipe.
-     *
-     * So all the stuff in USE_TSHARK_SELECT could be removed unless I'm
-     * wrong (but I leave it there in case I am...).
-     */
+  /* the actual capture loop
+   *
+   * XXX - glib doesn't seem to provide any event based loop handling.
+   *
+   * XXX - for whatever reason,
+   * calling g_main_loop_new() ends up in 100% cpu load.
+   *
+   * But that doesn't matter: in UNIX we can use select() to find an input
+   * source with something to do.
+   *
+   * But that doesn't matter because we're in a CLI (that doesn't need to
+   * update a GUI or something at the same time) so it's OK if we block
+   * trying to read from the pipe.
+   *
+   * So all the stuff in USE_TSHARK_SELECT could be removed unless I'm
+   * wrong (but I leave it there in case I am...).
+   */
 
 #ifdef USE_TSHARK_SELECT
   FD_ZERO(&readfds);
@@ -1995,9 +2145,9 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
 
 #ifdef SIGINFO
   /*
-   * Prevent a SIGINFO handler from writing to stdout while we're
-   * doing so; instead, have it just set a flag telling us to print
-   * that information when we're done.
+   * Prevent a SIGINFO handler from writing to the standard error while
+   * we're doing so or writing to the standard output; instead, have it
+   * just set a flag telling us to print that information when we're done.
    */
   infodelay = TRUE;
 #endif /* SIGINFO */
@@ -2010,6 +2160,7 @@ capture_input_new_packets(capture_options *capture_opts, int to_read)
 
   if(do_dissection) {
     while (to_read-- && cf->wth) {
+      wtap_cleareof(cf->wth);
       ret = wtap_read(cf->wth, &err, &err_info, &data_offset);
       if(ret == FALSE) {
         /* read from file failed, tell the capture child to stop */
@@ -2106,10 +2257,16 @@ capture_input_drops(capture_options *capture_opts _U_, guint32 dropped)
 }
 
 
-/* capture child closed its side of the pipe, do the required cleanup */
+/*
+ * Capture child closed its side of the pipe, report any error and
+ * do the required cleanup.
+ */
 void
-capture_input_closed(capture_options *capture_opts)
+capture_input_closed(capture_options *capture_opts, gchar *msg)
 {
+  if (msg != NULL)
+    fprintf(stderr, "tshark: %s\n", msg);
+
   report_counts();
 
   if(capture_opts->cf != NULL && ((capture_file *) capture_opts->cf)->wth != NULL) {
@@ -2139,10 +2296,8 @@ capture_cleanup(DWORD ctrltype _U_)
      and quit, just as we handle SIGINT, SIGHUP, and SIGTERM in that
      way on UNIX.
 
-     However, as handlers run in a new thread, we can't just longjmp
-     out; we have to set "ld.go" to FALSE, and must return TRUE so that
-     no other handler - such as one that would terminate the process -
-     gets called.
+     We must return TRUE so that no other handler - such as one that would
+     terminate the process - gets called.
 
      XXX - for some reason, typing ^C to TShark, if you run this in
      a Cygwin console window in at least some versions of Cygwin,
@@ -2168,10 +2323,189 @@ capture_cleanup(int signum _U_)
 {
   /* tell the capture child to stop */
   sync_pipe_stop(&global_capture_opts);
+
+  /* don't stop our own loop already here, otherwise status messages and
+   * cleanup wouldn't be done properly. The child will indicate the stop of
+   * everything by calling capture_input_closed() later */
 }
 #endif /* _WIN32 */
 #endif /* HAVE_LIBPCAP */
 
+#if GLIB_CHECK_VERSION(2,10,0)
+static gboolean
+process_packet_first_pass(capture_file *cf,
+               gint64 offset, const struct wtap_pkthdr *whdr,
+               union wtap_pseudo_header *pseudo_header, const guchar *pd)
+{
+  frame_data *fdata = g_slice_new(frame_data);
+  epan_dissect_t edt;
+  gboolean passed;
+
+  /* Count this packet. */
+  cf->count++;
+
+  /* If we're not running a display filter and we're not printing any
+     packet information, we don't need to do a dissection. This means
+     that all packets can be marked as 'passed'. */
+  passed = TRUE;
+
+  frame_data_init(fdata, cf->count, whdr, offset, cum_bytes);
+
+  /* If we're going to print packet information, or we're going to
+     run a read filter, or we're going to process taps, set up to
+     do a dissection and do so. */
+  if (do_dissection) {
+    if (g_resolv_flags)
+      /* Grab any resolved addresses */
+      host_name_lookup_process(NULL);
+
+    /* The protocol tree will be "visible", i.e., printed, only if we're
+       printing packet details, which is true if we're printing stuff
+       ("print_packet_info" is true) and we're in verbose mode ("verbose"
+       is true). */
+    epan_dissect_init(&edt, FALSE, FALSE);
+
+    /* If we're running a read filter, prime the epan_dissect_t with that
+       filter. */
+    if (cf->rfcode)
+      epan_dissect_prime_dfilter(&edt, cf->rfcode);
+
+    frame_data_set_before_dissect(fdata, &cf->elapsed_time,
+                                  &first_ts, &prev_dis_ts, &prev_cap_ts);
+
+    epan_dissect_run(&edt, pseudo_header, pd, fdata, NULL);
+
+    /* Run the read filter if we have one. */
+    if (cf->rfcode)
+      passed = dfilter_apply_edt(cf->rfcode, &edt);
+  }
+
+  if (passed) {
+    frame_data_set_after_dissect(fdata, &cum_bytes, &prev_dis_ts);
+    cap_file_add_fdata(cf, fdata);
+  }
+  else
+    g_slice_free(frame_data, fdata);
+
+  if (do_dissection)
+    epan_dissect_cleanup(&edt);
+
+  return passed;
+}
+
+static gboolean
+process_packet_second_pass(capture_file *cf, frame_data *fdata,
+               union wtap_pseudo_header *pseudo_header, const guchar *pd,
+               gboolean filtering_tap_listeners, guint tap_flags)
+{
+  gboolean create_proto_tree;
+  column_info *cinfo;
+  epan_dissect_t edt;
+  gboolean passed;
+
+  /* If we're not running a display filter and we're not printing any
+     packet information, we don't need to do a dissection. This means
+     that all packets can be marked as 'passed'. */
+  passed = TRUE;
+
+  /* If we're going to print packet information, or we're going to
+     run a read filter, or we're going to process taps, set up to
+     do a dissection and do so. */
+  if (do_dissection) {
+    if (g_resolv_flags)
+      /* Grab any resolved addresses */
+      host_name_lookup_process(NULL);
+
+    if (cf->rfcode || verbose || filtering_tap_listeners ||
+        (tap_flags & TL_REQUIRES_PROTO_TREE) || have_custom_cols(&cf->cinfo))
+      create_proto_tree = TRUE;
+    else
+      create_proto_tree = FALSE;
+
+    /* The protocol tree will be "visible", i.e., printed, only if we're
+       printing packet details, which is true if we're printing stuff
+       ("print_packet_info" is true) and we're in verbose mode ("verbose"
+       is true). */
+    epan_dissect_init(&edt, create_proto_tree, print_packet_info && verbose);
+
+    /* If we're running a read filter, prime the epan_dissect_t with that
+       filter. */
+    if (cf->rfcode)
+      epan_dissect_prime_dfilter(&edt, cf->rfcode);
+
+    col_custom_prime_edt(&edt, &cf->cinfo);
+
+    tap_queue_init(&edt);
+
+    /* We only need the columns if either
+
+         1) some tap needs the columns
+
+       or
+
+         2) we're printing packet info but we're *not* verbose; in verbose
+            mode, we print the protocol tree, not the protocol summary. */
+    if ((tap_flags & TL_REQUIRES_COLUMNS) || (print_packet_info && !verbose))
+      cinfo = &cf->cinfo;
+    else
+      cinfo = NULL;
+
+    epan_dissect_run(&edt, pseudo_header, pd, fdata, cinfo);
+
+    tap_push_tapped_queue(&edt);
+
+    /* Run the read filter if we have one. */
+    if (cf->rfcode)
+      passed = dfilter_apply_edt(cf->rfcode, &edt);
+  }
+
+  if (passed) {
+    /* Process this packet. */
+    if (print_packet_info) {
+      /* We're printing packet information; print the information for
+         this packet. */
+      if (do_dissection)
+        print_packet(cf, &edt);
+      else
+        print_packet(cf, NULL);
+
+      /* The ANSI C standard does not appear to *require* that a line-buffered
+         stream be flushed to the host environment whenever a newline is
+         written, it just says that, on such a stream, characters "are
+         intended to be transmitted to or from the host environment as a
+         block when a new-line character is encountered".
+
+         The Visual C++ 6.0 C implementation doesn't do what is intended;
+         even if you set a stream to be line-buffered, it still doesn't
+         flush the buffer at the end of every line.
+
+         So, if the "-l" flag was specified, we flush the standard output
+         at the end of a packet.  This will do the right thing if we're
+         printing packet summary lines, and, as we print the entire protocol
+         tree for a single packet without waiting for anything to happen,
+         it should be as good as line-buffered mode if we're printing
+         protocol trees.  (The whole reason for the "-l" flag in either
+         tcpdump or TShark is to allow the output of a live capture to
+         be piped to a program or script and to have that script see the
+         information for the packet as soon as it's printed, rather than
+         having to wait until a standard I/O buffer fills up. */
+      if (line_buffered)
+        fflush(stdout);
+
+      if (ferror(stdout)) {
+        show_print_file_io_error(errno);
+        exit(2);
+      }
+    }
+  }
+
+  if (do_dissection) {
+    epan_dissect_cleanup(&edt);
+  }
+  return passed;
+}
+#endif
+
 static int
 load_cap_file(capture_file *cf, char *save_file, int out_file_type,
     int max_packet_count, gint64 max_byte_count)
@@ -2248,36 +2582,124 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
   /* Get the union of the flags for all tap listeners. */
   tap_flags = union_of_tap_listener_flags();
 
-  while (wtap_read(cf->wth, &err, &err_info, &data_offset)) {
-    if (process_packet(cf, data_offset, wtap_phdr(cf->wth),
-                       wtap_pseudoheader(cf->wth), wtap_buf_ptr(cf->wth),
-                       filtering_tap_listeners, tap_flags)) {
-      /* Either there's no read filtering or this packet passed the
-         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_pseudoheader(cf->wth), wtap_buf_ptr(cf->wth),
-                       &err)) {
-          /* Error writing to a capture file */
-          show_capture_file_io_error(save_file, err, FALSE);
-          wtap_dump_close(pdh, &err);
-          exit(2);
+  if (perform_two_pass_analysis) {
+#if GLIB_CHECK_VERSION(2,10,0)
+    frame_data *fdata;
+    int old_max_packet_count = max_packet_count;
+
+    while (wtap_read(cf->wth, &err, &err_info, &data_offset)) {
+      if (process_packet_first_pass(cf, data_offset, wtap_phdr(cf->wth),
+                         wtap_pseudoheader(cf->wth), wtap_buf_ptr(cf->wth))) {
+        /* Stop reading if we have the maximum number of packets;
+         * When the -c option has not been used, max_packet_count
+         * starts at 0, which practically means, never stop reading.
+         * (unless we roll over max_packet_count ?)
+         */
+        if( (--max_packet_count == 0) || (max_byte_count != 0 && data_offset >= max_byte_count)) {
+          err = 0; /* This is not an error */
+          break;
+        }
+      }
+    }
+
+    /* Close the sequential I/O side, to free up memory it requires. */
+    wtap_sequential_close(cf->wth);
+
+    /* Allow the protocol dissectors to free up memory that they
+     * don't need after the sequential run-through of the packets. */
+    postseq_cleanup_all_protocols();
+
+    max_packet_count = old_max_packet_count;
+
+    for (fdata = cf->plist_start; err == 0 && fdata != NULL; fdata = fdata->next) {
+      if (wtap_seek_read(cf->wth, fdata->file_off, &cf->pseudo_header,
+          cf->pd, fdata->cap_len, &err, &err_info)) {
+        if (process_packet_second_pass(cf, fdata,
+                           &cf->pseudo_header, cf->pd,
+                           filtering_tap_listeners, tap_flags)) {
+          /* Either there's no read filtering or this packet passed the
+             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_pseudoheader(cf->wth), wtap_buf_ptr(cf->wth),
+                           &err)) {
+              /* Error writing to a capture file */
+              show_capture_file_io_error(save_file, err, FALSE);
+              wtap_dump_close(pdh, &err);
+              exit(2);
+            }
+          }
+          /* Stop reading if we have the maximum number of packets;
+           * When the -c option has not been used, max_packet_count
+           * starts at 0, which practically means, never stop reading.
+           * (unless we roll over max_packet_count ?)
+           */
+          if( (--max_packet_count == 0) || (max_byte_count != 0 && data_offset >= max_byte_count)) {
+            err = 0; /* This is not an error */
+            break;
+          }
         }
       }
-      /* Stop reading if we have the maximum number of packets;
-       * When the -c option has not been used, max_packet_count
-       * starts at 0, which practically means, never stop reading.
-       * (unless we roll over max_packet_count ?)
-       */
-      if( (--max_packet_count == 0) || (max_byte_count != 0 && data_offset >= max_byte_count)) {
-        err = 0; /* This is not an error */
-        break;
+    }
+#endif
+  }
+  else {
+    while (wtap_read(cf->wth, &err, &err_info, &data_offset)) {
+      if (process_packet(cf, data_offset, wtap_phdr(cf->wth),
+                         wtap_pseudoheader(cf->wth), wtap_buf_ptr(cf->wth),
+                         filtering_tap_listeners, tap_flags)) {
+        /* Either there's no read filtering or this packet passed the
+           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_pseudoheader(cf->wth), wtap_buf_ptr(cf->wth),
+                         &err)) {
+            /* Error writing to a capture file */
+            show_capture_file_io_error(save_file, err, FALSE);
+            wtap_dump_close(pdh, &err);
+            exit(2);
+          }
+        }
+        /* Stop reading if we have the maximum number of packets;
+         * When the -c option has not been used, max_packet_count
+         * starts at 0, which practically means, never stop reading.
+         * (unless we roll over max_packet_count ?)
+         */
+        if( (--max_packet_count == 0) || (max_byte_count != 0 && data_offset >= max_byte_count)) {
+          err = 0; /* This is not an error */
+          break;
+        }
       }
     }
   }
+
   if (err != 0) {
-    /* Print a message noting that the read failed somewhere along the line. */
+    /*
+     * Print a message noting that the read failed somewhere along the line.
+     *
+     * If we're printing packet data, and the standard output and error are
+     * going to the same place, flush the standard output, so everything
+     * buffered up is written, and then print a newline to the standard error
+     * before printing the error message, to separate it from the packet
+     * data.  (Alas, that only works on UN*X; st_dev is meaningless, and
+     * the _fstat() documentation at Microsoft doesn't indicate whether
+     * st_ino is even supported.)
+     */
+#ifndef _WIN32
+    if (print_packet_info) {
+      struct stat stat_stdout, stat_stderr;
+
+      if (fstat(1, &stat_stdout) == 0 && fstat(2, &stat_stderr) == 0) {
+        if (stat_stdout.st_dev == stat_stderr.st_dev &&
+            stat_stdout.st_ino == stat_stderr.st_ino) {
+          fflush(stdout);
+          fprintf(stderr, "\n");
+        }
+      }
+    }
+#endif
     switch (err) {
 
     case WTAP_ERR_UNSUPPORTED_ENCAP:
@@ -2350,27 +2772,27 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
   /* Count this packet. */
   cf->count++;
 
+  /* If we're not running a display filter and we're not printing any
+     packet information, we don't need to do a dissection. This means
+     that all packets can be marked as 'passed'. */
+  passed = TRUE;
+
+  frame_data_init(&fdata, cf->count, whdr, offset, cum_bytes);
+
   /* If we're going to print packet information, or we're going to
      run a read filter, or we're going to process taps, set up to
      do a dissection and do so. */
   if (do_dissection) {
-    frame_data_init(&fdata, cf, whdr, offset,
-                    &cum_bytes, &first_ts, &prev_dis_ts, &prev_cap_ts);
-
-    if (print_packet_info) {
+    if (print_packet_info && g_resolv_flags)
       /* Grab any resolved addresses */
+      host_name_lookup_process(NULL);
 
-      if (g_resolv_flags) {
-        host_name_lookup_process(NULL);
-      }
-    }
-
-    passed = TRUE;
     if (cf->rfcode || verbose || filtering_tap_listeners ||
         (tap_flags & TL_REQUIRES_PROTO_TREE) || have_custom_cols(&cf->cinfo))
       create_proto_tree = TRUE;
     else
       create_proto_tree = FALSE;
+
     /* The protocol tree will be "visible", i.e., printed, only if we're
        printing packet details, which is true if we're printing stuff
        ("print_packet_info" is true) and we're in verbose mode ("verbose"
@@ -2398,6 +2820,10 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
       cinfo = &cf->cinfo;
     else
       cinfo = NULL;
+
+    frame_data_set_before_dissect(&fdata, &cf->elapsed_time,
+                                  &first_ts, &prev_dis_ts, &prev_cap_ts);
+
     epan_dissect_run(&edt, pseudo_header, pd, &fdata, cinfo);
 
     tap_push_tapped_queue(&edt);
@@ -2405,20 +2831,10 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
     /* Run the read filter if we have one. */
     if (cf->rfcode)
       passed = dfilter_apply_edt(cf->rfcode, &edt);
-    else
-      passed = TRUE;
-  } else {
-    /* We're not running a display filter and we're not printing any
-       packet information, so we don't need to do a dissection, and all
-       packets are processed. */
-    passed = TRUE;
   }
 
   if (passed) {
-    /* Keep the time of the current packet if the packet passed
-       the read filter so that the delta time since last displayed
-       packet can be calculated */
-    prev_dis_ts = fdata.abs_ts;
+    frame_data_set_after_dissect(&fdata, &cum_bytes, &prev_dis_ts);
 
     /* Process this packet. */
     if (print_packet_info) {
@@ -2466,52 +2882,6 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
   return passed;
 }
 
-static void
-show_capture_file_io_error(const char *fname, int err, gboolean is_close)
-{
-  char *save_file_string;
-
-  save_file_string = output_file_description(fname);
-
-  switch (err) {
-
-  case ENOSPC:
-    cmdarg_err("Not all the packets could be written to the %s because there is "
-               "no space left on the file system.",
-               save_file_string);
-    break;
-
-#ifdef EDQUOT
-  case EDQUOT:
-    cmdarg_err("Not all the packets could be written to the %s because you are "
-               "too close to, or over your disk quota.",
-               save_file_string);
-  break;
-#endif
-
-  case WTAP_ERR_CANT_CLOSE:
-    cmdarg_err("The %s couldn't be closed for some unknown reason.",
-               save_file_string);
-    break;
-
-  case WTAP_ERR_SHORT_WRITE:
-    cmdarg_err("Not all the packets could be written to the %s.",
-               save_file_string);
-    break;
-
-  default:
-    if (is_close) {
-      cmdarg_err("The %s could not be closed: %s.", save_file_string,
-                 wtap_strerror(err));
-    } else {
-      cmdarg_err("An error occurred while writing to the %s: %s.",
-                 save_file_string, wtap_strerror(err));
-    }
-    break;
-  }
-  g_free(save_file_string);
-}
-
 static gboolean
 write_preamble(capture_file *cf)
 {
@@ -2881,6 +3251,111 @@ write_finale(void)
   }
 }
 
+cf_status_t
+cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
+{
+  wtap       *wth;
+  gchar       *err_info;
+  char        err_msg[2048+1];
+
+  wth = wtap_open_offline(fname, err, &err_info, perform_two_pass_analysis);
+  if (wth == NULL)
+    goto fail;
+
+  /* The open succeeded.  Fill in the information for this file. */
+
+  /* Cleanup all data structures used for dissection. */
+  cleanup_dissection();
+  /* Initialize all data structures used for dissection. */
+  init_dissection();
+
+  cf->wth = wth;
+  cf->f_datalen = 0; /* not used, but set it anyway */
+
+  /* Set the file name because we need it to set the follow stream filter.
+     XXX - is that still true?  We need it for other reasons, though,
+     in any case. */
+  cf->filename = g_strdup(fname);
+
+  /* Indicate whether it's a permanent or temporary file. */
+  cf->is_tempfile = is_tempfile;
+
+  /* If it's a temporary capture buffer file, mark it as not saved. */
+  cf->user_saved = !is_tempfile;
+
+  cf->cd_t      = wtap_file_type(cf->wth);
+  cf->count     = 0;
+  cf->drops_known = FALSE;
+  cf->drops     = 0;
+  cf->snap      = wtap_snapshot_length(cf->wth);
+  if (cf->snap == 0) {
+    /* Snapshot length not known. */
+    cf->has_snap = FALSE;
+    cf->snap = WTAP_MAX_PACKET_SIZE;
+  } else
+    cf->has_snap = TRUE;
+  nstime_set_zero(&cf->elapsed_time);
+  nstime_set_unset(&first_ts);
+  nstime_set_unset(&prev_dis_ts);
+  nstime_set_unset(&prev_cap_ts);
+
+  cf->state = FILE_READ_IN_PROGRESS;
+
+  return CF_OK;
+
+fail:
+  g_snprintf(err_msg, sizeof err_msg,
+             cf_open_error_message(*err, err_info, FALSE, cf->cd_t), fname);
+  cmdarg_err("%s", err_msg);
+  return CF_ERROR;
+}
+
+static void
+show_capture_file_io_error(const char *fname, int err, gboolean is_close)
+{
+  char *save_file_string;
+
+  save_file_string = output_file_description(fname);
+
+  switch (err) {
+
+  case ENOSPC:
+    cmdarg_err("Not all the packets could be written to the %s because there is "
+               "no space left on the file system.",
+               save_file_string);
+    break;
+
+#ifdef EDQUOT
+  case EDQUOT:
+    cmdarg_err("Not all the packets could be written to the %s because you are "
+               "too close to, or over your disk quota.",
+               save_file_string);
+  break;
+#endif
+
+  case WTAP_ERR_CANT_CLOSE:
+    cmdarg_err("The %s couldn't be closed for some unknown reason.",
+               save_file_string);
+    break;
+
+  case WTAP_ERR_SHORT_WRITE:
+    cmdarg_err("Not all the packets could be written to the %s.",
+               save_file_string);
+    break;
+
+  default:
+    if (is_close) {
+      cmdarg_err("The %s could not be closed: %s.", save_file_string,
+                 wtap_strerror(err));
+    } else {
+      cmdarg_err("An error occurred while writing to the %s: %s.",
+                 save_file_string, wtap_strerror(err));
+    }
+    break;
+  }
+  g_free(save_file_string);
+}
+
 static void
 show_print_file_io_error(int err)
 {
@@ -3015,65 +3490,6 @@ open_failure_message(const char *filename, int err, gboolean for_writing)
   fprintf(stderr, "\n");
 }
 
-cf_status_t
-cf_open(capture_file *cf, const char *fname, gboolean is_tempfile, int *err)
-{
-  wtap       *wth;
-  gchar       *err_info;
-  char        err_msg[2048+1];
-
-  wth = wtap_open_offline(fname, err, &err_info, FALSE);
-  if (wth == NULL)
-    goto fail;
-
-  /* The open succeeded.  Fill in the information for this file. */
-
-  /* Cleanup all data structures used for dissection. */
-  cleanup_dissection();
-  /* Initialize all data structures used for dissection. */
-  init_dissection();
-
-  cf->wth = wth;
-  cf->f_datalen = 0; /* not used, but set it anyway */
-
-  /* Set the file name because we need it to set the follow stream filter.
-     XXX - is that still true?  We need it for other reasons, though,
-     in any case. */
-  cf->filename = g_strdup(fname);
-
-  /* Indicate whether it's a permanent or temporary file. */
-  cf->is_tempfile = is_tempfile;
-
-  /* If it's a temporary capture buffer file, mark it as not saved. */
-  cf->user_saved = !is_tempfile;
-
-  cf->cd_t      = wtap_file_type(cf->wth);
-  cf->count     = 0;
-  cf->drops_known = FALSE;
-  cf->drops     = 0;
-  cf->snap      = wtap_snapshot_length(cf->wth);
-  if (cf->snap == 0) {
-    /* Snapshot length not known. */
-    cf->has_snap = FALSE;
-    cf->snap = WTAP_MAX_PACKET_SIZE;
-  } else
-    cf->has_snap = TRUE;
-  nstime_set_zero(&cf->elapsed_time);
-  nstime_set_unset(&first_ts);
-  nstime_set_unset(&prev_dis_ts);
-  nstime_set_unset(&prev_cap_ts);
-
-  cf->state = FILE_READ_IN_PROGRESS;
-
-  return CF_OK;
-
-fail:
-  g_snprintf(err_msg, sizeof err_msg,
-             cf_open_error_message(*err, err_info, FALSE, cf->cd_t), fname);
-  cmdarg_err("%s", err_msg);
-  return CF_ERROR;
-}
-
 
 /*
  * General errors are reported with an console message in TShark.
@@ -3106,6 +3522,26 @@ write_failure_message(const char *filename, int err)
           filename, strerror(err));
 }
 
+/*
+ * Print to the standard error.  This is a command-line tool, so there's
+ * no need to pop up a console.
+ */
+void
+vfprintf_stderr(const char *fmt, va_list ap)
+{
+  vfprintf(stderr, fmt, ap);
+}
+
+void
+fprintf_stderr(const char *fmt, ...)
+{
+  va_list ap;
+
+  va_start(ap, fmt);
+  vfprintf_stderr(fmt, ap);
+  va_end(ap);
+}
+
 /*
  * Report an error in command-line arguments.
  */