Check for clang before trying to add -Q flag
[metze/wireshark/wip.git] / reordercap.c
index 7a9890e17789eb87aebe4f38ffd2cb3d177b197b..0c29ea59c804c16d970d6fa4054dd9855311e20c 100644 (file)
  *
  */
 
-#include "config.h"
+#include <config.h>
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
 
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
 #endif
 
-#include "wtap.h"
-
-#ifndef HAVE_GETOPT
-#include "wsutil/wsgetopt.h"
+#ifdef HAVE_LIBZ
+#include <zlib.h>      /* to get the libz version number */
 #endif
 
-#include "version.h"
+#include <wiretap/wtap.h>
 
-static void
-print_version(FILE *output)
-{
-  fprintf(output, "Reordercap %s"
-#ifdef GITVERSION
-      " (" GITVERSION " from " GITBRANCH ")"
+#ifndef HAVE_GETOPT_LONG
+#include "wsutil/wsgetopt.h"
 #endif
-      "\n", VERSION);
-}
+
+#include <wsutil/crash_info.h>
+#include <wsutil/file_util.h>
+#include <wsutil/ws_diag_control.h>
+#include <wsutil/ws_version_info.h>
+#include <wiretap/wtap_opttypes.h>
 
 /* Show command-line usage */
 static void
-usage(gboolean is_error)
+print_usage(FILE *output)
 {
-    FILE *output;
-
-    if (!is_error) {
-        output = stdout;
-    }
-    else {
-        output = stderr;
-    }
-
-    print_version(output);
-    fprintf(output, "Reorder timestamps of input file frames into output file.\n");
-    fprintf(output, "See http://www.wireshark.org for more information.\n");
     fprintf(output, "\n");
     fprintf(output, "Usage: reordercap [options] <infile> <outfile>\n");
     fprintf(output, "\n");
@@ -83,7 +65,7 @@ typedef struct FrameRecord_t {
     gint64       offset;
     guint        num;
 
-    nstime_t     time;
+    nstime_t     frame_time;
 } FrameRecord_t;
 
 
@@ -102,47 +84,44 @@ typedef struct FrameRecord_t {
 
 
 static void
-frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, Buffer *buf,
-            const char *infile)
+frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh,
+            struct wtap_pkthdr *phdr, Buffer *buf, const char *infile)
 {
     int    err;
     gchar  *err_info;
-    struct wtap_pkthdr phdr;
-
-    memset(&phdr, 0, sizeof(struct wtap_pkthdr));
 
     DEBUG_PRINT("\nDumping frame (offset=%" G_GINT64_MODIFIER "u)\n",
                 frame->offset);
 
 
-    /* Re-read the first frame from the stored location */
-    if (!wtap_seek_read(wth, frame->offset, &phdr, buf, &err, &err_info)) {
+    /* Re-read the frame from the stored location */
+    if (!wtap_seek_read(wth, frame->offset, phdr, buf, &err, &err_info)) {
         if (err != 0) {
             /* Print a message noting that the read failed somewhere along the line. */
             fprintf(stderr,
                     "reordercap: An error occurred while re-reading \"%s\": %s.\n",
                     infile, wtap_strerror(err));
-            switch (err) {
-
-            case WTAP_ERR_UNSUPPORTED:
-            case WTAP_ERR_UNSUPPORTED_ENCAP:
-            case WTAP_ERR_BAD_FILE:
+            if (err_info != NULL) {
                 fprintf(stderr, "(%s)\n", err_info);
                 g_free(err_info);
-                break;
             }
             exit(1);
         }
     }
 
     /* Copy, and set length and timestamp from item. */
-    /* TODO: remove when wtap_seek_read() will read phdr */
-    phdr.ts = frame->time;
+    /* TODO: remove when wtap_seek_read() fills in phdr,
+       including time stamps, for all file types  */
+    phdr->ts = frame->frame_time;
 
     /* Dump frame to outfile */
-    if (!wtap_dump(pdh, &phdr, buffer_start_ptr(buf), &err)) {
+    if (!wtap_dump(pdh, phdr, ws_buffer_start_ptr(buf), &err, &err_info)) {
         fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n",
                 wtap_strerror(err));
+        if (err_info != NULL) {
+            fprintf(stderr, "(%s)\n", err_info);
+            g_free(err_info);
+        }
         exit(1);
     }
 }
@@ -158,12 +137,37 @@ frames_compare(gconstpointer a, gconstpointer b)
     const FrameRecord_t *frame1 = *(const FrameRecord_t *const *) a;
     const FrameRecord_t *frame2 = *(const FrameRecord_t *const *) b;
 
-    const nstime_t *time1 = &frame1->time;
-    const nstime_t *time2 = &frame2->time;
+    const nstime_t *time1 = &frame1->frame_time;
+    const nstime_t *time2 = &frame2->frame_time;
 
     return nstime_cmp(time1, time2);
 }
 
+static void
+get_reordercap_compiled_info(GString *str)
+{
+    /* LIBZ */
+    g_string_append(str, ", ");
+#ifdef HAVE_LIBZ
+    g_string_append(str, "with libz ");
+#ifdef ZLIB_VERSION
+    g_string_append(str, ZLIB_VERSION);
+#else /* ZLIB_VERSION */
+    g_string_append(str, "(version unknown)");
+#endif /* ZLIB_VERSION */
+#else /* HAVE_LIBZ */
+    g_string_append(str, "without libz");
+#endif /* HAVE_LIBZ */
+}
+
+static void
+get_reordercap_runtime_info(GString *str)
+{
+    /* zlib */
+#if defined(HAVE_LIBZ) && !defined(_WIN32)
+    g_string_append_printf(str, ", with libz %s", zlibVersion());
+#endif
+}
 
 /********************************************************************/
 /* Main function.                                                   */
@@ -171,8 +175,11 @@ frames_compare(gconstpointer a, gconstpointer b)
 int
 main(int argc, char *argv[])
 {
+    GString *comp_info_str;
+    GString *runtime_info_str;
     wtap *wth = NULL;
     wtap_dumper *pdh = NULL;
+    struct wtap_pkthdr dump_phdr;
     Buffer buf;
     int err;
     gchar *err_info;
@@ -181,21 +188,36 @@ main(int argc, char *argv[])
     guint wrong_order_count = 0;
     gboolean write_output_regardless = TRUE;
     guint i;
-    wtapng_section_t            *shb_hdr;
-    wtapng_iface_descriptions_t *idb_inf;
+    wtap_optionblock_t           shb_hdr = NULL;
+    wtapng_iface_descriptions_t *idb_inf = NULL;
+    wtap_optionblock_t           nrb_hdr = NULL;
 
     GPtrArray *frames;
     FrameRecord_t *prevFrame = NULL;
 
     int opt;
     static const struct option long_options[] = {
-        {(char *)"help", no_argument, NULL, 'h'},
-        {(char *)"version", no_argument, NULL, 'v'},
+        {"help", no_argument, NULL, 'h'},
+        {"version", no_argument, NULL, 'v'},
         {0, 0, 0, 0 }
     };
     int file_count;
     char *infile;
-    char *outfile;
+    const char *outfile;
+
+    /* Get the compile-time version information string */
+    comp_info_str = get_compiled_version_info(NULL, get_reordercap_compiled_info);
+
+    /* Get the run-time version information string */
+    runtime_info_str = get_runtime_version_info(get_reordercap_runtime_info);
+
+    /* Add it to the information to be reported on a crash. */
+    ws_add_crash_info("Reordercap (Wireshark) %s\n"
+         "\n"
+         "%s"
+         "\n"
+         "%s",
+      get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
 
     /* Process the options first */
     while ((opt = getopt_long(argc, argv, "hnv", long_options, NULL)) != -1) {
@@ -204,13 +226,19 @@ main(int argc, char *argv[])
                 write_output_regardless = FALSE;
                 break;
             case 'h':
-                usage(FALSE);
+                printf("Reordercap (Wireshark) %s\n"
+                       "Reorder timestamps of input file frames into output file.\n"
+                       "See https://www.wireshark.org for more information.\n",
+                       get_ws_vcs_version_info());
+                print_usage(stdout);
                 exit(0);
             case 'v':
-                print_version(stdout);
+                show_version("Reordercap (Wireshark)", comp_info_str, runtime_info_str);
+                g_string_free(comp_info_str, TRUE);
+                g_string_free(runtime_info_str, TRUE);
                 exit(0);
             case '?':
-                usage(TRUE);
+                print_usage(stderr);
                 exit(1);
         }
     }
@@ -222,7 +250,7 @@ main(int argc, char *argv[])
         outfile = argv[optind+1];
     }
     else {
-        usage(TRUE);
+        print_usage(stderr);
         exit(1);
     }
 
@@ -233,30 +261,35 @@ main(int argc, char *argv[])
     if (wth == NULL) {
         fprintf(stderr, "reordercap: Can't open %s: %s\n", infile,
                 wtap_strerror(err));
-        switch (err) {
-
-        case WTAP_ERR_UNSUPPORTED:
-        case WTAP_ERR_UNSUPPORTED_ENCAP:
-        case WTAP_ERR_BAD_FILE:
+        if (err_info != NULL) {
             fprintf(stderr, "(%s)\n", err_info);
             g_free(err_info);
-            break;
         }
         exit(1);
     }
-    DEBUG_PRINT("file_type_subtype is %u\n", wtap_file_type_subtype(wth));
+    DEBUG_PRINT("file_type_subtype is %d\n", wtap_file_type_subtype(wth));
 
-    shb_hdr = wtap_file_get_shb_info(wth);
+    shb_hdr = wtap_file_get_shb_for_new_file(wth);
     idb_inf = wtap_file_get_idb_info(wth);
+    nrb_hdr = wtap_file_get_nrb_for_new_file(wth);
 
     /* Open outfile (same filetype/encap as input file) */
-    pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
-                            65535, FALSE, shb_hdr, idb_inf, &err);
+    if (strcmp(outfile, "-") == 0) {
+      pdh = wtap_dump_open_stdout_ng(wtap_file_type_subtype(wth), wtap_file_encap(wth),
+                                     65535, FALSE, shb_hdr, idb_inf, nrb_hdr, &err);
+      outfile = "standard output";
+    } else {
+      pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth),
+                              65535, FALSE, shb_hdr, idb_inf, nrb_hdr, &err);
+    }
     g_free(idb_inf);
+    idb_inf = NULL;
+
     if (pdh == NULL) {
         fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n",
                 outfile, wtap_strerror(err));
-        g_free(shb_hdr);
+        wtap_optionblock_free(shb_hdr);
+        wtap_optionblock_free(nrb_hdr);
         exit(1);
     }
 
@@ -273,9 +306,9 @@ main(int argc, char *argv[])
         newFrameRecord->num = frames->len + 1;
         newFrameRecord->offset = data_offset;
         if (phdr->presence_flags & WTAP_HAS_TS) {
-            newFrameRecord->time = phdr->ts;
+            newFrameRecord->frame_time = phdr->ts;
         } else {
-            nstime_set_unset(&newFrameRecord->time);
+            nstime_set_unset(&newFrameRecord->frame_time);
         }
 
         if (prevFrame && frames_compare(&newFrameRecord, &prevFrame) < 0) {
@@ -290,14 +323,9 @@ main(int argc, char *argv[])
       fprintf(stderr,
               "reordercap: An error occurred while reading \"%s\": %s.\n",
               infile, wtap_strerror(err));
-      switch (err) {
-
-      case WTAP_ERR_UNSUPPORTED:
-      case WTAP_ERR_UNSUPPORTED_ENCAP:
-      case WTAP_ERR_BAD_FILE:
+      if (err_info != NULL) {
           fprintf(stderr, "(%s)\n", err_info);
           g_free(err_info);
-          break;
       }
     }
 
@@ -309,17 +337,19 @@ main(int argc, char *argv[])
     }
 
     /* Write out each sorted frame in turn */
-    buffer_init(&buf, 1500);
+    wtap_phdr_init(&dump_phdr);
+    ws_buffer_init(&buf, 1500);
     for (i = 0; i < frames->len; i++) {
         FrameRecord_t *frame = (FrameRecord_t *)frames->pdata[i];
 
         /* Avoid writing if already sorted and configured to */
         if (write_output_regardless || (wrong_order_count > 0)) {
-            frame_write(frame, wth, pdh, &buf, infile);
+            frame_write(frame, wth, pdh, &dump_phdr, &buf, infile);
         }
         g_slice_free(FrameRecord_t, frame);
     }
-    buffer_free(&buf);
+    wtap_phdr_cleanup(&dump_phdr);
+    ws_buffer_free(&buf);
 
     if (!write_output_regardless && (wrong_order_count == 0)) {
         printf("Not writing output file because input file is already in order!\n");
@@ -332,10 +362,12 @@ main(int argc, char *argv[])
     if (!wtap_dump_close(pdh, &err)) {
         fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile,
                 wtap_strerror(err));
-        g_free(shb_hdr);
+        wtap_optionblock_free(shb_hdr);
+        wtap_optionblock_free(nrb_hdr);
         exit(1);
     }
-    g_free(shb_hdr);
+    wtap_optionblock_free(shb_hdr);
+    wtap_optionblock_free(nrb_hdr);
 
     /* Finally, close infile */
     wtap_fdclose(wth);