Add a few of our headers to code generated by pycrc (their faq says we can do
[metze/wireshark/wip.git] / editcap.c
index ec6c4f0ae185c4e69e77cf9be0e5bc6ee07d9a7f..3332d5df6aaff02dafc741d3806e58c2c220f2ae 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -1,16 +1,34 @@
 /* Edit capture files.  We can delete packets, adjust timestamps, or
  * simply convert from one format to another format.
  *
- * $Id$
- *
  * Originally written by Richard Sharpe.
  * Improved by Guy Harris.
  * Further improved by Richard Sharpe.
+ *
+ * Copyright 2013, Richard Sharpe <realrichardsharpe[AT]gmail.com>
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -34,8 +52,6 @@
 #include <unistd.h>
 #endif
 
-
-
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
 #endif
@@ -47,6 +63,7 @@
 #endif
 
 #ifdef _WIN32
+#include <wsutil/file_util.h>
 #include <wsutil/unicode-utils.h>
 #include <process.h>    /* getpid */
 #ifdef HAVE_WINSOCK2_H
 # include "wsutil/strptime.h"
 #endif
 
+#include <wsutil/privileges.h>
+
+/*
+ * The symbols declared in the below are exported from libwireshark,
+ * but we don't want to link whole libwireshark to editcap.
+ * We link the object directly instead and this needs a little trick
+ * with the WS_BUILD_DLL #define.
+ */
+#define WS_BUILD_DLL
+#define RESET_SYMBOL_EXPORT /* wsutil/wsgetopt.h set export behavior above. */
 #include "epan/crypt/md5.h"
 #include "epan/plugins.h"
 #include "epan/report_err.h"
 #include "epan/filesystem.h"
-#include <wsutil/privileges.h>
 #include "epan/nstime.h"
+#undef WS_BUILD_DLL
+#define RESET_SYMBOL_EXPORT
 
 #include "svnversion.h"
 
+#include "ringbuffer.h" /* For RINGBUFFER_MAX_NUM_FILES */
+
 /*
  * Some globals so we can pass things to various routines
  */
@@ -147,9 +177,9 @@ static gchar *
 abs_time_to_str_with_sec_resolution(const struct wtap_nstime *abs_time)
 {
     struct tm *tmp;
-    gchar *buf = g_malloc(16);
+    gchar *buf = (gchar *)g_malloc(16);
 
-#ifdef _MSC_VER
+#if (defined _WIN32) && (_MSC_VER < 1500)
     /* calling localtime() on MSVC 2005 with huge values causes it to crash */
     /* XXX - find the exact value that still does work */
     /* XXX - using _USE_32BIT_TIME_T might be another way to circumvent this problem */
@@ -181,7 +211,7 @@ fileset_get_filename_by_pattern(guint idx,    const struct wtap_nstime *time_val
     gchar *abs_str;
 
     timestr = abs_time_to_str_with_sec_resolution(time_val);
-    g_snprintf(filenum, sizeof(filenum), "%05u", idx);
+    g_snprintf(filenum, sizeof(filenum), "%05u", idx % RINGBUFFER_MAX_NUM_FILES);
     abs_str = g_strconcat(fprefix, "_", filenum, "_", timestr, fsuffix, NULL);
     g_free(timestr);
 
@@ -368,7 +398,7 @@ set_time_adjustment(char *optarg_str_p)
       frac_digits++;
     }
   }
-  time_adj.tv.tv_usec = val;
+  time_adj.tv.tv_usec = (int)val;
 }
 
 static void
@@ -443,7 +473,7 @@ set_strict_time_adj(char *optarg_str_p)
       frac_digits++;
     }
   }
-  strict_time_adj.tv.tv_usec = val;
+  strict_time_adj.tv.tv_usec = (int)val;
 }
 
 static void
@@ -513,7 +543,7 @@ set_rel_time(char *optarg_str_p)
       frac_digits++;
     }
   }
-  relative_time_window.nsecs = val;
+  relative_time_window.nsecs = (int)val;
 }
 
 static gboolean
@@ -764,7 +794,7 @@ list_capture_types(void) {
     struct string_elem *captypes;
     GSList *list = NULL;
 
-    captypes = g_malloc(sizeof(struct string_elem) * WTAP_NUM_FILE_TYPES);
+    captypes = g_new(struct string_elem,WTAP_NUM_FILE_TYPES);
     fprintf(stderr, "editcap: The available capture file types for the \"-F\" flag are:\n");
     for (i = 0; i < WTAP_NUM_FILE_TYPES; i++) {
       if (wtap_dump_can_open(i)) {
@@ -784,7 +814,7 @@ list_encap_types(void) {
     struct string_elem *encaps;
     GSList *list = NULL;
 
-    encaps = g_malloc(sizeof(struct string_elem) * WTAP_NUM_ENCAP_TYPES);
+    encaps = (struct string_elem *)g_malloc(sizeof(struct string_elem) * WTAP_NUM_ENCAP_TYPES);
     fprintf(stderr, "editcap: The available encapsulation types for the \"-T\" flag are:\n");
     for (i = 0; i < WTAP_NUM_ENCAP_TYPES; i++) {
         encaps[i].sstr = wtap_encap_short_string(i);
@@ -823,8 +853,8 @@ main(int argc, char *argv[])
   guint32 snaplen = 0;                  /* No limit               */
   int choplen = 0;                      /* No chop                */
   wtap_dumper *pdh = NULL;
-  int count = 1;
-  unsigned duplicate_count = 0;
+  unsigned int count = 1;
+  unsigned int duplicate_count = 0;
   gint64 data_offset;
   struct wtap_pkthdr snap_phdr;
   const struct wtap_pkthdr *phdr;
@@ -842,6 +872,7 @@ main(int argc, char *argv[])
   nstime_t block_start;
   gchar *fprefix = NULL;
   gchar *fsuffix = NULL;
+  char appname[100];
 
 #ifdef HAVE_PLUGINS
   char* init_progfile_dir_error;
@@ -849,6 +880,7 @@ main(int argc, char *argv[])
 
 #ifdef _WIN32
   arg_list_utf_16to8(argc, argv);
+  create_app_running_mutex();
 #endif /* _WIN32 */
 
   /*
@@ -859,7 +891,7 @@ main(int argc, char *argv[])
 #ifdef HAVE_PLUGINS
   /* Register wiretap plugins */
   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
-    g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
+    g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
     g_free(init_progfile_dir_error);
   } else {
     init_report_err(failure_message,NULL,NULL,NULL);
@@ -893,7 +925,7 @@ main(int argc, char *argv[])
       break;
 
     case 'c':
-      split_packet_count = strtol(optarg, &p, 10);
+      split_packet_count = (int)strtol(optarg, &p, 10);
       if (p == optarg || *p != '\0') {
         fprintf(stderr, "editcap: \"%s\" isn't a valid packet count\n",
             optarg);
@@ -907,7 +939,7 @@ main(int argc, char *argv[])
       break;
 
     case 'C':
-      choplen = strtol(optarg, &p, 10);
+      choplen = (int)strtol(optarg, &p, 10);
       if (p == optarg || *p != '\0') {
         fprintf(stderr, "editcap: \"%s\" isn't a valid chop length\n",
             optarg);
@@ -924,7 +956,7 @@ main(int argc, char *argv[])
     case 'D':
       dup_detect = TRUE;
       dup_detect_by_time = FALSE;
-      dup_window = strtol(optarg, &p, 10);
+      dup_window = (int)strtol(optarg, &p, 10);
       if (p == optarg || *p != '\0') {
         fprintf(stderr, "editcap: \"%s\" isn't a valid duplicate window value\n",
             optarg);
@@ -968,7 +1000,7 @@ main(int argc, char *argv[])
       break;
 
     case 's':
-      snaplen = strtol(optarg, &p, 10);
+      snaplen = (guint32)strtol(optarg, &p, 10);
       if (p == optarg || *p != '\0') {
         fprintf(stderr, "editcap: \"%s\" isn't a valid snapshot length\n",
                 optarg);
@@ -1145,9 +1177,16 @@ main(int argc, char *argv[])
         } else
           filename = g_strdup(argv[optind+1]);
 
+        /* If we don't have an application name add Editcap */
+        if(shb_hdr->shb_user_appl == NULL) {
+          g_snprintf(appname, sizeof(appname), "Editcap " VERSION);
+          shb_hdr->shb_user_appl = appname;
+        }
+
         pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
           snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
           FALSE /* compressed */, shb_hdr, idb_inf, &err);
+
         if (pdh == NULL) {
           fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
                   wtap_strerror(err));
@@ -1176,9 +1215,9 @@ main(int argc, char *argv[])
             fprintf(stderr, "Continuing writing in file %s\n", filename);
           }
 
-          pdh = wtap_dump_open(filename, out_file_type, out_frame_type,
+          pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
             snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
-            FALSE /* compressed */, &err);
+            FALSE /* compressed */, shb_hdr, idb_inf, &err);
 
           if (pdh == NULL) {
             fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
@@ -1207,9 +1246,9 @@ main(int argc, char *argv[])
             fprintf(stderr, "Continuing writing in file %s\n", filename);
           }
 
-          pdh = wtap_dump_open(filename, out_file_type, out_frame_type,
+          pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
             snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth),
-            FALSE /* compressed */, &err);
+            FALSE /* compressed */, shb_hdr, idb_inf, &err);
           if (pdh == NULL) {
             fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
                 wtap_strerror(err));
@@ -1450,7 +1489,7 @@ main(int argc, char *argv[])
           }
         }
 
-        if (!wtap_dump(pdh, phdr, wtap_pseudoheader(wth), buf, &err)) {
+        if (!wtap_dump(pdh, phdr, buf, &err)) {
           switch (err) {
 
           case WTAP_ERR_UNSUPPORTED_ENCAP:
@@ -1474,6 +1513,7 @@ main(int argc, char *argv[])
       count++;
     }
 
+
     g_free(fprefix);
     g_free(fsuffix);
 
@@ -1498,9 +1538,9 @@ main(int argc, char *argv[])
       g_free (filename);
       filename = g_strdup(argv[optind+1]);
 
-      pdh = wtap_dump_open(filename, out_file_type, out_frame_type,
+      pdh = wtap_dump_open_ng(filename, out_file_type, out_frame_type,
         snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth),
-        FALSE /* compressed */, &err);
+        FALSE /* compressed */, shb_hdr, idb_inf, &err);
       if (pdh == NULL) {
         fprintf(stderr, "editcap: Can't open or create %s: %s\n", filename,
         wtap_strerror(err));
@@ -1508,6 +1548,9 @@ main(int argc, char *argv[])
       }
     }
 
+    g_free(idb_inf);
+    idb_inf = NULL;
+
     if (!wtap_dump_close(pdh, &err)) {
 
       fprintf(stderr, "editcap: Error writing to %s: %s\n", filename,
@@ -1515,7 +1558,6 @@ main(int argc, char *argv[])
       exit(2);
 
     }
-    g_free(idb_inf);
     g_free(shb_hdr);
     g_free(filename);
   }