From Marko Hrastovec via
[metze/wireshark/wip.git] / editcap.c
index e28f8b2676a51e67951f5307988ea0ea731c3fff..8c8cee9fb4e83f743f9f1973b9ef2104da341299 100644 (file)
--- a/editcap.c
+++ b/editcap.c
 #include <wsutil/report_err.h>
 #include <wsutil/strnatcmp.h>
 #include <wsutil/md5.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/plugins.h"
-#undef WS_BUILD_DLL
-#define RESET_SYMBOL_EXPORT
+#include <wsutil/plugins.h>
 
 #include "svnversion.h"
 
@@ -111,18 +100,18 @@ struct select_item {
  */
 typedef struct _fd_hash_t {
     md5_byte_t digest[16];
-    guint32 len;
-    nstime_t time;
+    guint32    len;
+    nstime_t   time;
 } fd_hash_t;
 
-#define DEFAULT_DUP_DEPTH 5     /* Used with -d */
-#define MAX_DUP_DEPTH 1000000   /* the maximum window (and actual size of fd_hash[]) for de-duplication */
+#define DEFAULT_DUP_DEPTH       5   /* Used with -d */
+#define MAX_DUP_DEPTH     1000000   /* the maximum window (and actual size of fd_hash[]) for de-duplication */
 
 static fd_hash_t fd_hash[MAX_DUP_DEPTH];
-static int dup_window = DEFAULT_DUP_DEPTH;
-static int cur_dup_entry = 0;
+static int       dup_window    = DEFAULT_DUP_DEPTH;
+static int       cur_dup_entry = 0;
 
-#define ONE_MILLION 1000000
+#define ONE_MILLION    1000000
 #define ONE_BILLION 1000000000
 
 /* Weights of different errors we can introduce */
@@ -152,29 +141,29 @@ typedef struct _chop_t {
     int off_end_neg;
 } chop_t;
 
-#define MAX_SELECTIONS  512
-static struct select_item selectfrm[MAX_SELECTIONS];
-static int max_selected = -1;
-static int keep_em = 0;
+#define MAX_SELECTIONS 512
+static struct select_item     selectfrm[MAX_SELECTIONS];
+static int                    max_selected              = -1;
+static int                    keep_em                   = 0;
 #ifdef PCAP_NG_DEFAULT
-static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng   */
+static int                    out_file_type_subtype     = WTAP_FILE_TYPE_SUBTYPE_PCAPNG; /* default to pcapng   */
 #else
-static int out_file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_PCAP;   /* default to pcap     */
+static int                    out_file_type_subtype     = WTAP_FILE_TYPE_SUBTYPE_PCAP; /* default to pcap     */
 #endif
-static int out_frame_type = -2;              /* Leave frame type alone */
-static int verbose = 0;                      /* Not so verbose         */
-static struct time_adjustment time_adj = {{0, 0}, 0}; /* no adjustment */
-static nstime_t relative_time_window = {0, 0}; /* de-dup time window */
-static double err_prob = 0.0;
-static time_t starttime = 0;
-static time_t stoptime = 0;
-static gboolean check_startstop = FALSE;
-static gboolean dup_detect = FALSE;
-static gboolean dup_detect_by_time = FALSE;
-
-static int do_strict_time_adjustment = FALSE;
-static struct time_adjustment strict_time_adj = {{0, 0}, 0}; /* strict time adjustment */
-static nstime_t previous_time = {0, 0}; /* previous time */
+static int                    out_frame_type            = -2; /* Leave frame type alone */
+static int                    verbose                   = 0;  /* Not so verbose         */
+static struct time_adjustment time_adj                  = {{0, 0}, 0}; /* no adjustment */
+static nstime_t               relative_time_window      = {0, 0}; /* de-dup time window */
+static double                 err_prob                  = 0.0;
+static time_t                 starttime                 = 0;
+static time_t                 stoptime                  = 0;
+static gboolean               check_startstop           = FALSE;
+static gboolean               dup_detect                = FALSE;
+static gboolean               dup_detect_by_time        = FALSE;
+
+static int                    do_strict_time_adjustment = FALSE;
+static struct time_adjustment strict_time_adj           = {{0, 0}, 0}; /* strict time adjustment */
+static nstime_t               previous_time             = {0, 0}; /* previous time */
 
 static int find_dct2000_real_data(guint8 *buf);
 static void handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
@@ -185,17 +174,9 @@ static gchar *
 abs_time_to_str_with_sec_resolution(const nstime_t *abs_time)
 {
     struct tm *tmp;
-    gchar *buf = (gchar *)g_malloc(16);
-
-#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 */
-    if (abs_time->secs > 2000000000)
-        tmp = NULL;
-    else
-#endif
-        tmp = localtime(&abs_time->secs);
+    gchar     *buf = (gchar *)g_malloc(16);
+
+    tmp = localtime(&abs_time->secs);
 
     if (tmp) {
         g_snprintf(buf, 16, "%d%02d%02d%02d%02d%02d",
@@ -212,11 +193,11 @@ abs_time_to_str_with_sec_resolution(const nstime_t *abs_time)
     return buf;
 }
 
-static gchar*
+static gchar *
 fileset_get_filename_by_pattern(guint idx, const nstime_t *time_val,
                                 gchar *fprefix, gchar *fsuffix)
 {
-    gchar filenum[5+1];
+    gchar  filenum[5+1];
     gchar *timestr;
     gchar *abs_str;
 
@@ -333,7 +314,7 @@ selected(int recno)
 static gboolean
 check_timestamp(wtap *wth)
 {
-    struct wtap_pkthdrpkthdr = wtap_phdr(wth);
+    struct wtap_pkthdr *pkthdr = wtap_phdr(wth);
 
     return (pkthdr->ts.secs >= starttime) && (pkthdr->ts.secs < stoptime);
 }
@@ -341,9 +322,9 @@ check_timestamp(wtap *wth)
 static void
 set_time_adjustment(char *optarg_str_p)
 {
-    char *frac, *end;
-    long val;
-    size_t frac_digits;
+    char   *frac, *end;
+    long    val;
+    size_t  frac_digits;
 
     if (!optarg_str_p)
         return;
@@ -411,9 +392,9 @@ set_time_adjustment(char *optarg_str_p)
 static void
 set_strict_time_adj(char *optarg_str_p)
 {
-    char *frac, *end;
-    long val;
-    size_t frac_digits;
+    char   *frac, *end;
+    long    val;
+    size_t  frac_digits;
 
     if (!optarg_str_p)
         return;
@@ -485,9 +466,9 @@ set_strict_time_adj(char *optarg_str_p)
 static void
 set_rel_time(char *optarg_str_p)
 {
-    char *frac, *end;
-    long val;
-    size_t frac_digits;
+    char   *frac, *end;
+    long    val;
+    size_t  frac_digits;
 
     if (!optarg_str_p)
         return;
@@ -613,7 +594,7 @@ is_duplicate_rel_time(guint8* fd, guint32 len, const nstime_t *current) {
      * in strict chronologically increasing order (which is NOT
      * always the case!!).
      *
-     * The fd_hash[] table was deliberatly created large (1,000,000).
+     * The fd_hash[] table was deliberately created large (1,000,000).
      * Looking for time related duplicates in large trace files with
      * non-fractional dup time window values can potentially take
      * a long time to complete.
@@ -773,7 +754,7 @@ usage(gboolean is_error)
     fprintf(output, "  -v                     verbose output.\n");
     fprintf(output, "                         If -v is used with any of the 'Duplicate Packet\n");
     fprintf(output, "                         Removal' options (-d, -D or -w) then Packet lengths\n");
-    fprintf(output, "                         and MD5 hashes are printed to standard-out.\n");
+    fprintf(output, "                         and MD5 hashes are printed to standard-error.\n");
     fprintf(output, "\n");
 }
 
@@ -859,36 +840,36 @@ failure_message(const char *msg_format _U_, va_list ap _U_)
 int
 main(int argc, char *argv[])
 {
-    wtap *wth;
-    int i, j, err;
-    gchar *err_info;
-    int opt;
-
-    char *p;
-    guint32 snaplen = 0;                /* No limit               */
-    chop_t chop = {0, 0, 0, 0, 0, 0};   /* No chop */
-    gboolean adjlen = FALSE;
-    wtap_dumper *pdh = NULL;
-    unsigned int count = 1;
-    unsigned int duplicate_count = 0;
-    gint64 data_offset;
-    struct wtap_pkthdr snap_phdr;
-    const struct wtap_pkthdr *phdr;
-    int err_type;
-    wtapng_section_t *shb_hdr;
+    wtap         *wth;
+    int           i, j, err;
+    gchar        *err_info;
+    int           opt;
+
+    char         *p;
+    guint32       snaplen            = 0; /* No limit               */
+    chop_t        chop               = {0, 0, 0, 0, 0, 0}; /* No chop */
+    gboolean      adjlen             = FALSE;
+    wtap_dumper  *pdh                = NULL;
+    unsigned int  count              = 1;
+    unsigned int  duplicate_count    = 0;
+    gint64        data_offset;
+    int           err_type;
+    guint8       *buf;
+    guint32       read_count         = 0;
+    int           split_packet_count = 0;
+    int           written_count      = 0;
+    char         *filename           = NULL;
+    gboolean      ts_okay            = TRUE;
+    int           secs_per_block     = 0;
+    int           block_cnt          = 0;
+    nstime_t      block_start;
+    gchar        *fprefix            = NULL;
+    gchar        *fsuffix            = NULL;
+
+    const struct wtap_pkthdr    *phdr;
+    struct wtap_pkthdr           snap_phdr;
     wtapng_iface_descriptions_t *idb_inf;
-    guint8 *buf;
-    guint32 read_count = 0;
-    int split_packet_count = 0;
-    int written_count = 0;
-    char *filename = NULL;
-    gboolean ts_okay = TRUE;
-    int secs_per_block = 0;
-    int block_cnt = 0;
-    nstime_t block_start;
-    gchar *fprefix = NULL;
-    gchar *fsuffix = NULL;
-    char appname[100];
+    wtapng_section_t            *shb_hdr;
 
 #ifdef HAVE_PLUGINS
     char* init_progfile_dir_error;
@@ -910,8 +891,17 @@ main(int argc, char *argv[])
         g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
         g_free(init_progfile_dir_error);
     } else {
+        /* Register all the plugin types we have. */
+        wtap_register_plugin_types(); /* Types known to libwiretap */
+
         init_report_err(failure_message,NULL,NULL,NULL);
-        init_plugins();
+
+        /* Scan for plugins.  This does *not* call their registration routines;
+           that's done later. */
+        scan_plugins();
+
+        /* Register all libwiretap plugin modules. */
+        register_all_wiretap_modules();
     }
 #endif
 
@@ -1048,7 +1038,7 @@ main(int argc, char *argv[])
 
         case 'h':
             usage(FALSE);
-            exit(1);
+            exit(0);
             break;
 
         case 'i': /* break capture file based on time interval */
@@ -1224,8 +1214,7 @@ main(int argc, char *argv[])
 
                 /* 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;
+                    shb_hdr->shb_user_appl = "Editcap " VERSION;
                 }
 
                 pdh = wtap_dump_open_ng(filename, out_file_type_subtype, out_frame_type,
@@ -1351,7 +1340,7 @@ main(int argc, char *argv[])
                                  * situation since trace files usually have packets in
                                  * chronological order (oldest to newest).
                                  */
-                                /* printf("++out of order, need to adjust this packet!\n"); */
+                                /* fprintf(stderr, "++out of order, need to adjust this packet!\n"); */
                                 snap_phdr = *phdr;
                                 snap_phdr.ts.secs = previous_time.secs + strict_time_adj.tv.tv_sec;
                                 snap_phdr.ts.nsecs = previous_time.nsecs;
@@ -1424,24 +1413,24 @@ main(int argc, char *argv[])
                 if (dup_detect) {
                     if (is_duplicate(buf, phdr->caplen)) {
                         if (verbose) {
-                            fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                         duplicate_count++;
                         count++;
                         continue;
                     } else {
                         if (verbose) {
-                            fprintf(stdout, "Packet: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                     }
                 }
@@ -1450,29 +1439,29 @@ main(int argc, char *argv[])
                 if (dup_detect_by_time) {
                     nstime_t current;
 
-                    current.secs = phdr->ts.secs;
+                    current.secs  = phdr->ts.secs;
                     current.nsecs = phdr->ts.nsecs;
 
                     if (is_duplicate_rel_time(buf, phdr->caplen, &current)) {
                         if (verbose) {
-                            fprintf(stdout, "Skipped: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Skipped: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                         duplicate_count++;
                         count++;
                         continue;
                     } else {
                         if (verbose) {
-                            fprintf(stdout, "Packet: %u, Len: %u, MD5 Hash: ",
+                            fprintf(stderr, "Packet: %u, Len: %u, MD5 Hash: ",
                                     count, phdr->caplen);
                             for (i = 0; i < 16; i++)
-                                fprintf(stdout, "%02x",
+                                fprintf(stderr, "%02x",
                                         (unsigned char)fd_hash[cur_dup_entry].digest[i]);
-                            fprintf(stdout, "\n");
+                            fprintf(stderr, "\n");
                         }
                     }
                 }
@@ -1532,11 +1521,27 @@ main(int argc, char *argv[])
                     case WTAP_ERR_UNSUPPORTED_ENCAP:
                         /*
                          * This is a problem with the particular frame we're
-                         * writing; note that, and give the frame number.
+                         * writing and the file type and subtype we're
+                         * writing; note that, and report the frame number
+                         * and file type/subtype.
+                         */
+                        fprintf(stderr,
+                                "editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file\n.",
+                                read_count, argv[optind],
+                                wtap_file_type_subtype_string(out_file_type_subtype));
+                        break;
+
+                    case WTAP_ERR_PACKET_TOO_LARGE:
+                        /*
+                         * This is a problem with the particular frame we're
+                         * writing and the file type and subtype we're
+                         * writing; note that, and report the frame number
+                         * and file type/subtype.
                          */
                         fprintf(stderr,
-                                "editcap: Frame %u of \"%s\" has a network type that can't be saved in a file with that format\n.",
-                                read_count, argv[optind]);
+                                "editcap: Frame %u of \"%s\" is too large for a \"%s\" file\n.",
+                                read_count, argv[optind],
+                                wtap_file_type_subtype_string(out_file_type_subtype));
                         break;
 
                     default:
@@ -1599,11 +1604,11 @@ main(int argc, char *argv[])
     }
 
     if (dup_detect) {
-        fprintf(stdout, "%u packet%s seen, %u packet%s skipped with duplicate window of %u packets.\n",
+        fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate window of %i packets.\n",
                 count - 1, plurality(count - 1, "", "s"), duplicate_count,
                 plurality(duplicate_count, "", "s"), dup_window);
     } else if (dup_detect_by_time) {
-        fprintf(stdout, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
+        fprintf(stderr, "%u packet%s seen, %u packet%s skipped with duplicate time window equal to or less than %ld.%09ld seconds.\n",
                 count - 1, plurality(count - 1, "", "s"), duplicate_count,
                 plurality(duplicate_count, "", "s"),
                 (long)relative_time_window.secs,
@@ -1637,7 +1642,7 @@ find_dct2000_real_data(guint8 *buf)
 }
 
 /*
- * We support up to 2 chopping regions in a single pas, one specified by the
+ * We support up to 2 chopping regions in a single pass: one specified by the
  * positive chop length, and one by the negative chop length.
  */
 static void
@@ -1738,11 +1743,11 @@ handle_chopping(chop_t chop, struct wtap_pkthdr *out_phdr,
  *
  * Local variables:
  * c-basic-offset: 4
- * tab-width: 4
+ * tab-width: 8
  * indent-tabs-mode: nil
  * End:
  *
- * vi: set shiftwidth=4 tabstop=4 expandtab:
- * :indentSize=4:tabSize=4:noTabs=true:
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
  */