editcap: add seed option
authorNils Björklund <nils.bjorklund@effnet.com>
Wed, 22 Aug 2018 08:17:02 +0000 (10:17 +0200)
committerAnders Broman <a.broman58@gmail.com>
Fri, 31 Aug 2018 05:53:50 +0000 (05:53 +0000)
Implement a --seed option to be used in conjunction with -E. The option
allows the user to set the seed for the pseudo-random number generator,
which can be useful for recreating a particular sequence of errors.

Change-Id: Id427ab5fd7711652ad56c72271b2e0acb7380858
Reviewed-on: https://code.wireshark.org/review/29306
Reviewed-by: Gerald Combs <gerald@wireshark.org>
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
doc/editcap.pod
editcap.c

index 0da3cf3c593570eae7733f68766e127cf188e35a..7f65c6268b98ef3d8f96b98526898fdaf258e156 100644 (file)
@@ -224,6 +224,11 @@ appear to reject Ethernet packets larger than the standard Ethernet MTU,
 making them incapable of handling gigabit Ethernet captures if jumbo
 packets were used).
 
+=item --seed  E<lt>seedE<gt>
+
+When used in conjunction with -E, set the seed for the pseudo-random number generator.
+This is useful for recreating a particular sequence of errors.
+
 =item --skip-radiotap-header
 
 Skip the readiotap header of each frame when checking for packet duplicates. This is useful
index 60aa64647433dbd939f2e57c8e1c98c77a8f584e..7dbb52a47a8ca6a522afd65e91afd2bb9f86a458 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -162,7 +162,7 @@ static int                    out_frame_type            = -2; /* Leave frame typ
 static int                    verbose                   = 0;  /* Not so verbose         */
 static struct time_adjustment time_adj                  = {NSTIME_INIT_ZERO, 0}; /* no adjustment */
 static nstime_t               relative_time_window      = NSTIME_INIT_ZERO; /* de-dup time window */
-static double                 err_prob                  = 0.0;
+static double                 err_prob                  = -1.0;
 static time_t                 starttime                 = 0;
 static time_t                 stoptime                  = 0;
 static gboolean               check_startstop           = FALSE;
@@ -799,6 +799,9 @@ print_usage(FILE *output)
     fprintf(output, "  -o <change offset>     When used in conjunction with -E, skip some bytes from the\n");
     fprintf(output, "                         beginning of the packet. This allows one to preserve some\n");
     fprintf(output, "                         bytes, in order to have some headers untouched.\n");
+    fprintf(output, "  --seed <seed>          When used in conjunction with -E, set the seed to use for\n");
+    fprintf(output, "                         the pseudo-random number generator. This allows one to\n");
+    fprintf(output, "                         repeat a particular sequence of errors.\n");
     fprintf(output, "  -I <bytes to ignore>   ignore the specified number of bytes at the beginning\n");
     fprintf(output, "                         of the frame during MD5 hash calculation, unless the\n");
     fprintf(output, "                         frame is too short, then the full frame is used.\n");
@@ -965,6 +968,7 @@ main(int argc, char *argv[])
     static const struct option long_options[] = {
         {"novlan", no_argument, NULL, 0x8100},
         {"skip-radiotap-header", no_argument, NULL, 0x8101},
+        {"seed", required_argument, NULL, 0x8102},
         {"help", no_argument, NULL, 'h'},
         {"version", no_argument, NULL, 'V'},
         {0, 0, 0, 0 }
@@ -1001,6 +1005,8 @@ main(int argc, char *argv[])
     gboolean                     do_mutation;
     guint32                      caplen;
     int                          ret = EXIT_SUCCESS;
+    gboolean                     valid_seed = FALSE;
+    unsigned int                 seed = 0;
 
     cmdarg_err_init(failure_warning_message, failure_message_cont);
 
@@ -1062,6 +1068,18 @@ main(int argc, char *argv[])
             break;
         }
 
+        case 0x8102:
+        {
+            if (sscanf(optarg, "%u", &seed) != 1) {
+                fprintf(stderr, "editcap: \"%s\" isn't a valid seed\n\n",
+                        optarg);
+                ret = INVALID_OPTION;
+                goto clean_exit;
+            }
+            valid_seed = TRUE;
+            break;
+        }
+
         case 'a':
         {
             guint frame_number;
@@ -1189,7 +1207,6 @@ main(int argc, char *argv[])
                 ret = INVALID_OPTION;
                 goto clean_exit;
             }
-            srand( (unsigned int) (time(NULL) + ws_getpid()) );
             break;
 
         case 'F':
@@ -1317,6 +1334,17 @@ main(int argc, char *argv[])
         print_usage(stderr);
         ret = INVALID_OPTION;
         goto clean_exit;
+
+    }
+
+    if (err_prob >= 0.0) {
+        if (!valid_seed) {
+            seed = (unsigned int) (time(NULL) + ws_getpid());
+        }
+        if (verbose) {
+            fprintf(stderr, "Using seed %u\n", seed);
+        }
+        srand(seed);
     }
 
     if (check_startstop && !stoptime) {