expert_add_info(): we can't use va_start()/va_end() so don't try to use a va_list...
[metze/wireshark/wip.git] / rawshark.c
index 2e68a970dae656c76dab9650492c21440b591723..3634d2317ad94c7970f4ac8722f2f9e67bf155f6 100644 (file)
@@ -34,9 +34,7 @@
  * - Prints a status line, followed by fields from a specified list.
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -68,6 +66,7 @@
 #include <glib.h>
 #include <epan/epan.h>
 #include <epan/filesystem.h>
+#include <wsutil/crash_info.h>
 #include <wsutil/privileges.h>
 #include <wsutil/file_util.h>
 
 #ifdef HAVE_LIBPCAP
 #include <setjmp.h>
 #include "capture-pcap-util.h"
-#include "pcapio.h"
 #ifdef _WIN32
 #include "capture-wpcap.h"
 #endif /* _WIN32 */
@@ -122,8 +120,10 @@ static const gchar decode_as_arg_template[] = "<layer_type>==<selector>,<decode_
 
 static guint32 cum_bytes;
 static nstime_t first_ts;
-static nstime_t prev_dis_ts;
-static nstime_t prev_cap_ts;
+static frame_data *prev_dis;
+static frame_data prev_dis_frame;
+static frame_data *prev_cap;
+static frame_data prev_cap_frame;
 
 /*
  * The way the packet decode is to be written.
@@ -142,7 +142,7 @@ static gboolean want_pcap_pkthdr;
 cf_status_t raw_cf_open(capture_file *cf, const char *fname);
 static int load_cap_file(capture_file *cf);
 static gboolean process_packet(capture_file *cf, gint64 offset,
-                               const struct wtap_pkthdr *whdr, const guchar *pd);
+                               struct wtap_pkthdr *whdr, const guchar *pd);
 static void show_print_file_io_error(int err);
 
 static void open_failure_message(const char *filename, int err,
@@ -244,7 +244,7 @@ raw_pipe_open(const char *pipe_name)
 #ifndef _WIN32
     ws_statb64 pipe_stat;
 #else
-    char *pncopy, *pos;
+    char *pncopy, *pos = NULL;
     DWORD err;
     wchar_t *err_str;
     HANDLE hPipe = NULL;
@@ -424,6 +424,8 @@ show_version(GString *comp_info_str, GString *runtime_info_str)
 int
 main(int argc, char *argv[])
 {
+    GString             *comp_info_str;
+    GString             *runtime_info_str;
     char                *init_progfile_dir_error;
     int                  opt, i;
     gboolean             arg_error = FALSE;
@@ -443,7 +445,7 @@ main(int argc, char *argv[])
     gchar               *rfilters[64];
     e_prefs             *prefs_p;
     char                 badopt;
-    GLogLevelFlags       log_flags;
+    int                  log_flags;
     GPtrArray           *disp_fields = g_ptr_array_new();
     guint                fc;
     gboolean             skip_pcap_header = FALSE;
@@ -452,8 +454,25 @@ main(int argc, char *argv[])
 
     static const char    optstring[] = OPTSTRING_INIT;
 
+    /* Assemble the compile-time version information string */
+    comp_info_str = g_string_new("Compiled ");
+    get_compiled_version_info(comp_info_str, NULL, 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, NULL);
+
+    /* Add it to the information to be reported on a crash. */
+    ws_add_crash_info("Rawshark " VERSION "%s\n"
+           "\n"
+           "%s"
+           "\n"
+           "%s",
+        wireshark_svnversion, comp_info_str->str, runtime_info_str->str);
+
 #ifdef _WIN32
     arg_list_utf_16to8(argc, argv);
+    create_app_running_mutex();
 #endif /* _WIN32 */
 
     /*
@@ -496,10 +515,10 @@ main(int argc, char *argv[])
         G_LOG_LEVEL_DEBUG;
 
     g_log_set_handler(NULL,
-                      log_flags,
+                      (GLogLevelFlags)log_flags,
                       log_func_ignore, NULL /* user_data */);
     g_log_set_handler(LOG_DOMAIN_CAPTURE_CHILD,
-                      log_flags,
+                      (GLogLevelFlags)log_flags,
                       log_func_ignore, NULL /* user_data */);
 
     timestamp_set_type(TS_RELATIVE);
@@ -542,9 +561,6 @@ main(int argc, char *argv[])
         pf_path = NULL;
     }
 
-    /* Set the name resolution code's flags from the preferences. */
-    gbl_resolv_flags = prefs_p->name_resolve;
-
     /* Read the disabled protocols file. */
     read_disabled_protos_list(&gdp_path, &gdp_open_errno, &gdp_read_errno,
                               &dp_path, &dp_open_errno, &dp_read_errno);
@@ -619,11 +635,12 @@ main(int argc, char *argv[])
                 line_buffered = TRUE;
                 break;
             case 'n':        /* No name resolution */
-                gbl_resolv_flags = RESOLV_NONE;
+                gbl_resolv_flags.mac_name = FALSE;
+                gbl_resolv_flags.network_name = FALSE;
+                gbl_resolv_flags.transport_name = FALSE;
+                gbl_resolv_flags.concurrent_dns = FALSE;
                 break;
             case 'N':        /* Select what types of addresses/port #s to resolve */
-                if (gbl_resolv_flags == RESOLV_ALL)
-                    gbl_resolv_flags = RESOLV_NONE;
                 badopt = string_to_name_resolve(optarg, &gbl_resolv_flags);
                 if (badopt != '\0') {
                     cmdarg_err("-N specifies unknown resolving option '%c'; valid options are 'm', 'n', and 't'",
@@ -700,15 +717,6 @@ main(int argc, char *argv[])
                 break;
             case 'v':        /* Show version and exit */
             {
-                GString             *comp_info_str;
-                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, NULL, 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, NULL);
                 show_version(comp_info_str, runtime_info_str);
                 g_string_free(comp_info_str, TRUE);
                 g_string_free(runtime_info_str, TRUE);
@@ -813,10 +821,10 @@ main(int argc, char *argv[])
 
         /* Do we need to PCAP header and magic? */
         if (skip_pcap_header) {
-            guint bytes_left = sizeof(struct pcap_hdr) + sizeof(guint32);
+            size_t bytes_left = sizeof(struct pcap_hdr) + sizeof(guint32);
             gchar buf[sizeof(struct pcap_hdr) + sizeof(guint32)];
-            while (bytes_left > 0) {
-                guint bytes = read(fd, buf, bytes_left);
+            while (bytes_left != 0) {
+                ssize_t bytes = read(fd, buf, (int)bytes_left);
                 if (bytes <= 0) {
                     cmdarg_err("Not enough bytes for pcap header.");
                     exit(2);
@@ -887,8 +895,8 @@ static gboolean
 raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err_info, gint64 *data_offset) {
     struct pcap_pkthdr mem_hdr;
     struct pcaprec_hdr disk_hdr;
-    int bytes_read = 0;
-    int bytes_needed = sizeof(disk_hdr);
+    ssize_t bytes_read = 0;
+    size_t bytes_needed = sizeof(disk_hdr);
     guchar *ptr = (guchar*) &disk_hdr;
     static gchar err_str[100];
 
@@ -899,7 +907,7 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err
 
     /* Copied from capture_loop.c */
     while (bytes_needed > 0) {
-        bytes_read = read(fd, ptr, bytes_needed);
+        bytes_read = read(fd, ptr, (int)bytes_needed);
         if (bytes_read == 0) {
             *err = 0;
             return FALSE;
@@ -916,14 +924,15 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err
     if (want_pcap_pkthdr) {
         phdr->ts.secs = mem_hdr.ts.tv_sec;
         phdr->ts.nsecs = mem_hdr.ts.tv_usec * 1000;
-        phdr->caplen = bytes_needed = mem_hdr.caplen;
+        phdr->caplen = mem_hdr.caplen;
         phdr->len = mem_hdr.len;
     } else {
         phdr->ts.secs = disk_hdr.ts_sec;
         phdr->ts.nsecs = disk_hdr.ts_usec * 1000;
-        phdr->caplen = bytes_needed = disk_hdr.incl_len;
+        phdr->caplen = disk_hdr.incl_len;
         phdr->len = disk_hdr.orig_len;
     }
+    bytes_needed = phdr->caplen;
 
     phdr->pkt_encap = encap;
 
@@ -935,14 +944,15 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err
 #endif
     if (bytes_needed > WTAP_MAX_PACKET_SIZE) {
         *err = WTAP_ERR_BAD_FILE;
-        g_snprintf(err_str, 100, "Bad packet length: %d (%04x)", bytes_needed, bytes_needed);
+        g_snprintf(err_str, 100, "Bad packet length: %lu\n",
+                   (unsigned long) bytes_needed);
         *err_info = err_str;
         return FALSE;
     }
 
     ptr = pd;
     while (bytes_needed > 0) {
-        bytes_read = read(fd, ptr, bytes_needed);
+        bytes_read = read(fd, ptr, (int)bytes_needed);
         if (bytes_read == 0) {
             *err = WTAP_ERR_SHORT_READ;
             *err_info = "Got zero bytes reading data from pipe";
@@ -1012,14 +1022,13 @@ load_cap_file(capture_file *cf)
 }
 
 static gboolean
-process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
+process_packet(capture_file *cf, gint64 offset, struct wtap_pkthdr *whdr,
                const guchar *pd)
 {
     frame_data fdata;
     gboolean create_proto_tree;
     epan_dissect_t edt;
     gboolean passed;
-    union wtap_pseudo_header pseudo_header;
     int i;
 
     if(whdr->len == 0)
@@ -1036,8 +1045,6 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
         return FALSE;
     }
 
-    memset(&pseudo_header, 0, sizeof(pseudo_header));
-
     /* Count this packet. */
     cf->count++;
 
@@ -1062,21 +1069,22 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
         }
     }
 
-    tap_queue_init(&edt);
-
     printf("%lu", (unsigned long int) cf->count);
 
     frame_data_set_before_dissect(&fdata, &cf->elapsed_time,
-                                  &first_ts, &prev_dis_ts, &prev_cap_ts);
+                                  &first_ts, prev_dis, prev_cap);
 
     /* We only need the columns if we're printing packet info but we're
      *not* verbose; in verbose mode, we print the protocol tree, not
      the protocol summary. */
-    epan_dissect_run(&edt, &pseudo_header, pd, &fdata, &cf->cinfo);
+    epan_dissect_run_with_taps(&edt, whdr, pd, &fdata, &cf->cinfo);
 
-    tap_push_tapped_queue(&edt);
+    frame_data_set_after_dissect(&fdata, &cum_bytes);
+    prev_dis_frame = fdata;
+    prev_dis = &prev_dis_frame;
 
-    frame_data_set_after_dissect(&fdata, &cum_bytes, &prev_dis_ts);
+    prev_cap_frame = fdata;
+    prev_cap = &prev_cap_frame;
 
     for(i = 0; i < n_rfilters; i++) {
         /* Run the read filter if we have one. */
@@ -1120,7 +1128,7 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
     }
 
     epan_dissect_cleanup(&edt);
-    frame_data_cleanup(&fdata);
+    frame_data_destroy(&fdata);
 
     return passed;
 }
@@ -1419,14 +1427,14 @@ protocolinfo_init(char *field)
             printf("%u %s %s - ",
                    g_cmd_line_index,
                    ftenum_to_string(hfi),
-                   absolute_time_display_e_to_string(hfi->display));
+                   absolute_time_display_e_to_string((absolute_time_display_e)hfi->display));
             break;
 
         default:
             printf("%u %s %s - ",
                    g_cmd_line_index,
                    ftenum_to_string(hfi),
-                   base_display_e_to_string(hfi->display));
+                   base_display_e_to_string((base_display_e)hfi->display));
             break;
     }
 
@@ -1591,8 +1599,8 @@ raw_cf_open(capture_file *cf, const char *fname)
     cf->snap = WTAP_MAX_PACKET_SIZE;
     nstime_set_zero(&cf->elapsed_time);
     nstime_set_unset(&first_ts);
-    nstime_set_unset(&prev_dis_ts);
-    nstime_set_unset(&prev_cap_ts);
+    prev_dis = NULL;
+    prev_cap = NULL;
 
     return CF_OK;
 }