Add a Buffer to wtap_pkthdr to hold file-type-specific packet metadata.
[metze/wireshark/wip.git] / tshark.c
index d2bd31f288751bffbcee7984e5b807a0643eeded..d3f31f181d21bedf8f559e5a7f2825541bc585e3 100644 (file)
--- a/tshark.c
+++ b/tshark.c
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <ctype.h>
 #include <locale.h>
 #include <limits.h>
 
 #include "register.h"
 #include <epan/epan_dissect.h>
 #include <epan/tap.h>
-#include <epan/stat_cmd_args.h>
+#include <epan/stat_tap_ui.h>
 #include <epan/timestamp.h>
 #include <epan/conversation_table.h>
 #include <epan/ex-opt.h>
@@ -226,6 +225,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 */
@@ -418,6 +419,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");
@@ -1201,6 +1203,7 @@ 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 */
 
   /* 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 +1230,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)
@@ -1645,12 +1650,12 @@ main(int argc, char *argv[])
          by the preferences set callback) from being used as
          part of a tap filter.  Instead, we just add the argument
          to a list of stat arguments. */
+      if (strcmp("help", optarg) == 0) {
+        fprintf(stderr, "tshark: The available statistics for the \"-z\" option are:\n");
+        list_stat_cmd_args();
+        return 0;
+      }
       if (!process_stat_cmd_arg(optarg)) {
-        if (strcmp("help", optarg)==0) {
-          fprintf(stderr, "tshark: The available statistics for the \"-z\" option are:\n");
-          list_stat_cmd_args();
-          return 0;
-        }
         cmdarg_err("Invalid -z argument \"%s\"; it must be one of:", optarg);
         list_stat_cmd_args();
         return 1;
@@ -2081,31 +2086,6 @@ main(int argc, char *argv[])
       return 2;
     }
 
-    /* Set timestamp precision; there should arguably be a command-line
-       option to let the user set this. */
-    switch(wtap_file_tsprecision(cfile.wth)) {
-    case(WTAP_FILE_TSPREC_SEC):
-      timestamp_set_precision(TS_PREC_AUTO_SEC);
-      break;
-    case(WTAP_FILE_TSPREC_DSEC):
-      timestamp_set_precision(TS_PREC_AUTO_DSEC);
-      break;
-    case(WTAP_FILE_TSPREC_CSEC):
-      timestamp_set_precision(TS_PREC_AUTO_CSEC);
-      break;
-    case(WTAP_FILE_TSPREC_MSEC):
-      timestamp_set_precision(TS_PREC_AUTO_MSEC);
-      break;
-    case(WTAP_FILE_TSPREC_USEC):
-      timestamp_set_precision(TS_PREC_AUTO_USEC);
-      break;
-    case(WTAP_FILE_TSPREC_NSEC):
-      timestamp_set_precision(TS_PREC_AUTO_NSEC);
-      break;
-    default:
-      g_assert_not_reached();
-    }
-
     /* Process the packets in the file */
     TRY {
 #ifdef HAVE_LIBPCAP
@@ -2118,11 +2098,11 @@ main(int argc, char *argv[])
     }
     CATCH(OutOfMemoryError) {
       fprintf(stderr,
-              "Out Of Memory!\n"
+              "Out Of Memory.\n"
               "\n"
-              "Sorry, but TShark has to terminate now!\n"
+              "Sorry, but TShark has to terminate now.\n"
               "\n"
-              "Some infos / workarounds can be found at:\n"
+              "More information and workarounds can be found at\n"
               "http://wiki.wireshark.org/KnownBugs/OutOfMemory\n");
       err = ENOMEM;
     }
@@ -2202,15 +2182,12 @@ 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;
       }
     }
 
-    /* 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
@@ -2551,11 +2528,11 @@ capture(void)
   }
   CATCH(OutOfMemoryError) {
     fprintf(stderr,
-            "Out Of Memory!\n"
+            "Out Of Memory.\n"
             "\n"
-            "Sorry, but TShark has to terminate now!\n"
+            "Sorry, but TShark has to terminate now.\n"
             "\n"
-            "Some infos / workarounds can be found at:\n"
+            "More information and workarounds can be found at\n"
             "http://wiki.wireshark.org/KnownBugs/OutOfMemory\n");
     exit(1);
   }
@@ -2585,7 +2562,7 @@ capture_input_cfilter_error_message(capture_session *cap_session, guint i, char
 
   if (dfilter_compile(interface_opts.cfilter, &rfcode) && rfcode != NULL) {
     cmdarg_err(
-      "Invalid capture filter \"%s\" for interface '%s'!\n"
+      "Invalid capture filter \"%s\" for interface '%s'.\n"
       "\n"
       "That string looks like a valid display filter; however, it isn't a valid\n"
       "capture filter (%s).\n"
@@ -2598,7 +2575,7 @@ capture_input_cfilter_error_message(capture_session *cap_session, guint i, char
     dfilter_free(rfcode);
   } else {
     cmdarg_err(
-      "Invalid capture filter \"%s\" for interface '%s'!\n"
+      "Invalid capture filter \"%s\" for interface '%s'.\n"
       "\n"
       "That string isn't a valid capture filter (%s).\n"
       "See the User's Guide for a description of the capture filter syntax.",
@@ -2617,7 +2594,7 @@ capture_input_new_file(capture_session *cap_session, gchar *new_file)
   int      err;
 
   if (cap_session->state == CAPTURE_PREPARING) {
-    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started!");
+    g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "Capture started.");
   }
   g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_MESSAGE, "File: \"%s\"", new_file);
 
@@ -3095,7 +3072,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
   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);
@@ -3179,13 +3156,6 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
     pdh = NULL;
   }
 
-  if (pdh && out_file_name_res) {
-    if (!wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list())) {
-      cmdarg_err("The file format \"%s\" doesn't support name resolution information.",
-                 wtap_file_type_subtype_short_string(out_file_type));
-    }
-  }
-
   /* Do we have any tap listeners with filters? */
   filtering_tap_listeners = have_filtering_tap_listeners();
 
@@ -3406,6 +3376,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.
@@ -3445,11 +3417,6 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
       g_free(err_info);
       break;
 
-    case WTAP_ERR_CANT_READ:
-      cmdarg_err("An attempt to read from the file \"%s\" failed for some unknown reason.",
-                 cf->filename);
-      break;
-
     case WTAP_ERR_SHORT_READ:
       cmdarg_err("The file \"%s\" appears to have been cut short in the middle of a packet.",
                  cf->filename);
@@ -3464,6 +3431,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
     case WTAP_ERR_DECOMPRESS:
       cmdarg_err("The compressed file \"%s\" appears to be damaged or corrupt.\n"
                  "(%s)", cf->filename, err_info);
+      g_free(err_info);
       break;
 
     default:
@@ -3478,6 +3446,12 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type,
     }
   } else {
     if (save_file != NULL) {
+      if (pdh && out_file_name_res) {
+        if (!wtap_dump_set_addrinfo_list(pdh, get_addrinfo_list())) {
+          cmdarg_err("The file format \"%s\" doesn't support name resolution information.",
+                     wtap_file_type_subtype_short_string(out_file_type));
+        }
+      }
       /* Now close the capture file. */
       if (!wtap_dump_close(pdh, &err))
         show_capture_file_io_error(save_file, err, TRUE);
@@ -3620,13 +3594,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:
@@ -3931,7 +3905,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();
@@ -3956,7 +3930,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))
@@ -3965,11 +3939,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);
     }