Enhance Windows ASN1 Makefiles:
[obnox/wireshark/wip.git] / rawshark.c
index a3aa3bcb8231d8b2407939c80c7ec35c965fa566..cdc293f741539661c030c248db7014e3e7440e1e 100644 (file)
 #include "strerror.h"
 #endif
 
-#ifdef NEED_GETOPT_H
-#include "getopt.h"
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#else
+#include "wsgetopt.h"
 #endif
 
 #include <glib.h>
 #include "epan/proto.h"
 #include <epan/tap.h>
 
-#include <wtap.h>
-#include <libpcap.h>
+#include <wiretap/wtap.h>
+#include <wiretap/libpcap.h>
+#include <wiretap/pcap-encap.h>
 
 #ifdef HAVE_LIBPCAP
-#include <pcap.h>
 #include <setjmp.h>
 #include "capture-pcap-util.h"
 #include "pcapio.h"
  */
 static const gchar decode_as_arg_template[] = "<layer_type>==<selector>,<decode_as_protocol>";
 
+static guint32 cum_bytes;
 static nstime_t first_ts;
 static nstime_t prev_dis_ts;
 static nstime_t prev_cap_ts;
-static GString *comp_info_str, *runtime_info_str;
 
 /*
  * The way the packet decode is to be written.
@@ -134,8 +136,8 @@ typedef enum {
        WRITE_XML       /* PDML or PSML */
        /* Add CSV and the like here */
 } output_action_e;
+
 static gboolean line_buffered;
-static guint32 cum_bytes = 0;
 static print_format_e print_format = PR_FMT_TEXT;
 
 /*
@@ -409,12 +411,25 @@ set_link_type(const char *lt_arg) {
   return FALSE;
 }
 
+static void
+show_version(GString *comp_info_str, GString *runtime_info_str)
+{
+  printf("Rawshark " VERSION "%s\n"
+         "\n"
+         "%s"
+         "\n"
+         "%s"
+         "\n"
+         "%s",
+         wireshark_svnversion, get_copyright_info(), comp_info_str->str,
+         runtime_info_str->str);
+}
+
 int
 main(int argc, char *argv[])
 {
   char                *init_progfile_dir_error;
   int                  opt, i;
-  extern char         *optarg;
   gboolean             arg_error = FALSE;
 
 #ifdef _WIN32
@@ -430,7 +445,7 @@ main(int argc, char *argv[])
   int                  err;
   gchar               *pipe_name = NULL;
   gchar               *rfilters[64];
-  e_prefs             *prefs;
+  e_prefs             *prefs_p;
   char                 badopt;
   GLogLevelFlags       log_flags;
   GPtrArray           *disp_fields = g_ptr_array_new();
@@ -505,7 +520,7 @@ main(int argc, char *argv[])
   /* 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) {
@@ -531,7 +546,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,
@@ -566,15 +581,7 @@ main(int argc, char *argv[])
   load_wpcap();
 #endif
 
-  init_cap_file(&cfile);
-
-  /* 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);
-
-  /* Assemble the run-time version information string */
-  runtime_info_str = g_string_new("Running ");
-  get_runtime_version_info(runtime_info_str, NULL);
+  cap_file_init(&cfile);
 
   /* Print format defaults to this. */
   print_format = PR_FMT_TEXT;
@@ -688,18 +695,22 @@ main(int argc, char *argv[])
         }
         break;
       case 'v':        /* Show version and exit */
-        printf("Rawshark " VERSION "%s\n"
-               "\n"
-               "%s"
-               "\n"
-               "%s"
-               "\n"
-               "%s",
-               wireshark_svnversion, get_copyright_info(), comp_info_str->str,
-               runtime_info_str->str);
+      {
+        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, get_epan_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);
         exit(0);
         break;
-
+      }
       default:
       case '?':        /* Bad flag - print usage message */
         print_usage(TRUE);
@@ -717,7 +728,7 @@ main(int argc, char *argv[])
 
   /* Initialize our display fields */
   for (fc = 0; fc < disp_fields->len; fc++) {
-       protocolinfo_init(g_ptr_array_index(disp_fields, fc));
+       protocolinfo_init((char *)g_ptr_array_index(disp_fields, fc));
   }
   g_ptr_array_free(disp_fields, TRUE);
   printf("\n");
@@ -766,7 +777,7 @@ main(int argc, char *argv[])
   }
 
   /* 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);
 
   if (n_rfilters != 0) {
     for (i = 0; i < n_rfilters; i++) {
@@ -869,7 +880,7 @@ main(int argc, char *argv[])
  * @return TRUE on success, FALSE on failure.
  */
 static gboolean
-raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, gchar **err_info, gint64 *data_offset) {
+raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, const gchar **err_info, gint64 *data_offset) {
   struct pcaprec_hdr hdr;
   int bytes_read = 0;
   int bytes_needed = sizeof(struct pcaprec_hdr);
@@ -892,7 +903,6 @@ raw_pipe_read(struct wtap_pkthdr *phdr, guchar * pd, int *err, gchar **err_info,
     ptr += bytes_read;
   }
 
-  bytes_read = 0;
   phdr->ts.secs = hdr.ts_sec;
   phdr->ts.nsecs = hdr.ts_usec * 1000;
   phdr->caplen = bytes_needed = hdr.incl_len;
@@ -935,7 +945,7 @@ static int
 load_cap_file(capture_file *cf)
 {
   int          err;
-  gchar        *err_info;
+  const gchar  *err_info;
   gint64       data_offset = 0;
   struct wtap_pkthdr  phdr;
   guchar       pd[WTAP_MAX_PACKET_SIZE];
@@ -978,82 +988,6 @@ load_cap_file(capture_file *cf)
   return err;
 }
 
-static void
-fill_in_fdata(frame_data *fdata, capture_file *cf,
-              const struct wtap_pkthdr *phdr, gint64 offset)
-{
-  fdata->next = NULL;
-  fdata->prev = NULL;
-  fdata->pfd = NULL;
-  fdata->num = cf->count;
-  fdata->pkt_len = phdr->len;
-  cum_bytes += phdr->len;
-  fdata->cum_bytes  = cum_bytes;
-  fdata->cap_len = phdr->caplen;
-  fdata->file_off = offset;
-  /* To save some memory, we coarcese it into a gint8 */
-  g_assert(phdr->pkt_encap <= G_MAXINT8);
-  fdata->lnk_t = (gint8) phdr->pkt_encap;
-  fdata->abs_ts.secs = phdr->ts.secs;
-  fdata->abs_ts.nsecs = phdr->ts.nsecs;
-  fdata->flags.passed_dfilter = 0;
-  fdata->flags.encoding = CHAR_ASCII;
-  fdata->flags.visited = 0;
-  fdata->flags.marked = 0;
-  fdata->flags.ref_time = 0;
-  fdata->color_filter = NULL;
-
-  /* If we don't have the time stamp of the first packet in the
-     capture, it's because this is the first packet.  Save the time
-     stamp of this packet as the time stamp of the first packet. */
-  if (nstime_is_unset(&first_ts)) {
-    first_ts = fdata->abs_ts;
-  }
-
-  /* If we don't have the time stamp of the previous captured packet,
-     it's because this is the first packet.  Save the time
-     stamp of this packet as the time stamp of the previous captured
-     packet. */
-  if (nstime_is_unset(&prev_cap_ts)) {
-    prev_cap_ts = fdata->abs_ts;
-  }
-
-  /* Get the time elapsed between the first packet and this packet. */
-  nstime_delta(&fdata->rel_ts, &fdata->abs_ts, &first_ts);
-
-  /* If it's greater than the current elapsed time, set the elapsed time
-     to it (we check for "greater than" so as not to be confused by
-     time moving backwards). */
-  if ((gint32)cf->elapsed_time.secs < fdata->rel_ts.secs
-       || ((gint32)cf->elapsed_time.secs == fdata->rel_ts.secs && (gint32)cf->elapsed_time.nsecs < fdata->rel_ts.nsecs)) {
-    cf->elapsed_time = fdata->rel_ts;
-  }
-
-  /* If we don't have the time stamp of the previous displayed packet,
-     it's because this is the first packet that's being displayed.  Save the time
-     stamp of this packet as the time stamp of the previous displayed
-     packet. */
-  if (nstime_is_unset(&prev_dis_ts))
-    prev_dis_ts = fdata->abs_ts;
-
-  /* Get the time elapsed between the previous displayed packet and
-     this packet. */
-  nstime_delta(&fdata->del_dis_ts, &fdata->abs_ts, &prev_dis_ts);
-
-  /* Get the time elapsed between the previous captured packet and
-     this packet. */
-  nstime_delta(&fdata->del_cap_ts, &fdata->abs_ts, &prev_cap_ts);
-  prev_cap_ts = fdata->abs_ts;
-}
-
-/* Free up all data attached to a "frame_data" structure. */
-static void
-clear_fdata(frame_data *fdata)
-{
-  if (fdata->pfd)
-    g_slist_free(fdata->pfd);
-}
-
 static gboolean
 process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
                const guchar *pd)
@@ -1087,7 +1021,7 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
   /* 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. */
-  fill_in_fdata(&fdata, cf, whdr, offset);
+  frame_data_init(&fdata, cf->count, whdr, offset, cum_bytes);
 
   passed = TRUE;
   create_proto_tree = TRUE;
@@ -1107,7 +1041,10 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
 
   tap_queue_init(&edt);
 
-  printf("%lu", (unsigned long int)cf->count);
+  printf("%lu", (unsigned long int) cf->count);
+
+  frame_data_set_before_dissect(&fdata, &cf->elapsed_time,
+                                &first_ts, &prev_dis_ts, &prev_cap_ts);
 
   /* 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
@@ -1116,6 +1053,8 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
 
   tap_push_tapped_queue(&edt);
 
+  frame_data_set_after_dissect(&fdata, &cum_bytes, &prev_dis_ts);
+
   for(i = 0; i < n_rfilters; i++) {
     /* Run the read filter if we have one. */
     if (rfcodes[i])
@@ -1158,7 +1097,7 @@ process_packet(capture_file *cf, gint64 offset, const struct wtap_pkthdr *whdr,
   }
 
   epan_dissect_cleanup(&edt);
-  clear_fdata(&fdata);
+  frame_data_cleanup(&fdata);
 
   return passed;
 }
@@ -1172,7 +1111,7 @@ typedef struct _pci_t {
        int cmd_line_index;
 } pci_t;
 
-char* ftenum_to_string(header_field_info *hfi)
+static const char* ftenum_to_string(header_field_info *hfi)
 {
        if (!hfi) {
                return "n.a.";
@@ -1250,7 +1189,19 @@ char* ftenum_to_string(header_field_info *hfi)
        };
 }
 
-char* base_display_e_to_string(base_display_e bd)
+static const char* absolute_time_display_e_to_string(absolute_time_display_e atd)
+{
+       switch(atd) {
+       case ABSOLUTE_TIME_LOCAL:
+                       return "ABSOLUTE_TIME_LOCAL";
+       case ABSOLUTE_TIME_UTC:
+                       return "ABSOLUTE_TIME_UTC";
+       default:
+                       return "n.a.";
+       }
+}
+
+static const char* base_display_e_to_string(base_display_e bd)
 {
        switch(bd) {
        case BASE_NONE:
@@ -1291,7 +1242,7 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
        hfinfo = finfo->hfinfo;
 
        if (!fs_buf) {
-               fs_buf = g_malloc(fs_buf_len + 1);
+               fs_buf = (char *)g_malloc(fs_buf_len + 1);
                fs_ptr = fs_buf;
        }
 
@@ -1308,7 +1259,7 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
                fs_len = fvalue_string_repr_len(&finfo->value, FTREPR_DFILTER);
                while (fs_buf_len < fs_len) {
                        fs_buf_len *= 2;
-                       fs_buf = g_realloc(fs_buf, fs_buf_len + 1);
+                       fs_buf = (char *)g_realloc(fs_buf, fs_buf_len + 1);
                        fs_ptr = fs_buf;
                }
                fvalue_to_string_repr(&finfo->value,
@@ -1325,7 +1276,7 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
        if (string_fmts->len > 0 && finfo->hfinfo->strings) {
                g_string_truncate(label_s, 0);
                for (i = 0; i < string_fmts->len; i++) {
-                       sf = g_ptr_array_index(string_fmts, i);
+                       sf = (string_fmt_t *)g_ptr_array_index(string_fmts, i);
                        if (sf->plain) {
                                g_string_append(label_s, sf->plain);
                        } else {
@@ -1350,17 +1301,18 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
                                                                DISSECTOR_ASSERT(!hfinfo->bitmask);
                                                                svalue = fvalue_get_sinteger(&finfo->value);
                                                                if (hfinfo->display & BASE_RANGE_STRING) {
-                                                                 g_string_append(label_s, rval_to_str(svalue, hfinfo->strings, "Unknown"));
+                                                                 g_string_append(label_s, rval_to_str(svalue, RVALS(hfinfo->strings), "Unknown"));
                                                                } else {
                                                                  g_string_append(label_s, val_to_str(svalue, cVALS(hfinfo->strings), "Unknown"));
                                                                }
+                                                               break;
                                                        case FT_UINT8:
                                                        case FT_UINT16:
                                                        case FT_UINT24:
                                                        case FT_UINT32:
                                                                uvalue = fvalue_get_uinteger(&finfo->value);
                                                                if (!hfinfo->bitmask && hfinfo->display & BASE_RANGE_STRING) {
-                                                                 g_string_append(label_s, rval_to_str(uvalue, hfinfo->strings, "Unknown"));
+                                                                 g_string_append(label_s, rval_to_str(uvalue, RVALS(hfinfo->strings), "Unknown"));
                                                                } else {
                                                                  g_string_append(label_s, val_to_str(uvalue, cVALS(hfinfo->strings), "Unknown"));
                                                                }
@@ -1396,7 +1348,7 @@ static gboolean print_field_value(field_info *finfo, int cmd_line_index)
 static int
 protocolinfo_packet(void *prs, packet_info *pinfo _U_, epan_dissect_t *edt, const void *dummy _U_)
 {
-       pci_t *rs=prs;
+       pci_t *rs=(pci_t *)prs;
        GPtrArray *gp;
        guint i;
 
@@ -1410,7 +1362,7 @@ protocolinfo_packet(void *prs, packet_info *pinfo _U_, epan_dissect_t *edt, cons
         * Print each occurrence of the field
         */
        for (i = 0; i < gp->len; i++) {
-               print_field_value(gp->pdata[i], rs->cmd_line_index);
+               print_field_value((field_info *)gp->pdata[i], rs->cmd_line_index);
        }
 
        return 0;
@@ -1434,12 +1386,25 @@ protocolinfo_init(char *field)
                exit(1);
        }
 
-       printf("%u %s %s - ",
-               g_cmd_line_index,
-               ftenum_to_string(hfi),
-               base_display_e_to_string(hfi->display));
+       switch (hfi->type) {
+
+       case FT_ABSOLUTE_TIME:
+               printf("%u %s %s - ",
+                       g_cmd_line_index,
+                       ftenum_to_string(hfi),
+                       absolute_time_display_e_to_string(hfi->display));
+               break;
+               break;
+
+       default:
+               printf("%u %s %s - ",
+                       g_cmd_line_index,
+                       ftenum_to_string(hfi),
+                       base_display_e_to_string(hfi->display));
+               break;
+       }
 
-       rs=g_malloc(sizeof(pci_t));
+       rs=(pci_t *)g_malloc(sizeof(pci_t));
        rs->hf_index=hfi->id;
        rs->filter=field;
        rs->cmd_line_index = g_cmd_line_index++;
@@ -1466,7 +1431,7 @@ protocolinfo_init(char *field)
 
 static void
 add_string_fmt(string_fmt_e format, gchar *plain) {
-       string_fmt_t *sf = g_malloc(sizeof(string_fmt_t));
+       string_fmt_t *sf = (string_fmt_t *)g_malloc(sizeof(string_fmt_t));
 
        sf->format = format;
        sf->plain = g_strdup(plain);