Also add the new dissectors
[obnox/wireshark/wip.git] / editcap.c
index 4aeb78ac4a9a6ae7723bf305097b19018abb29c5..50592854a456840c3701bd658113f87218ca9220 100644 (file)
--- a/editcap.c
+++ b/editcap.c
 #include "getopt.h"
 #endif
 
+#ifdef _WIN32
+#include <process.h>    /* getpid */
+#endif
+
 /*
  * Some globals so we can pass things to various routines
  */
@@ -48,11 +52,12 @@ struct select_item {
 /* Weights of different errors we can introduce */
 /* We should probably make these command-line arguments */
 /* XXX - Should we add a bit-level error? */
+#define ERR_WT_BIT   5  /* Flip a random bit */
 #define ERR_WT_BYTE  5  /* Substitute a random byte */
 #define ERR_WT_ALNUM 5  /* Substitute a random character in [A-Za-z0-9] */
 #define ERR_WT_FMT   2  /* Substitute "%s" */
 #define ERR_WT_AA    1  /* Fill the remainder of the buffer with 0xAA */
-#define ERR_WT_TOTAL (ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
+#define ERR_WT_TOTAL (ERR_WT_BIT + ERR_WT_BYTE + ERR_WT_ALNUM + ERR_WT_FMT + ERR_WT_AA)
 
 #define ALNUM_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
 #define ALNUM_LEN (sizeof(ALNUM_CHARS) - 1)
@@ -205,8 +210,8 @@ static void usage(void)
   int i;
   const char *string;
 
-  fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-F <capture type>]\n");
-  fprintf(stderr, "               [-s <snaplen>] [-t <time adjustment>]\n");
+  fprintf(stderr, "Usage: editcap [-r] [-h] [-v] [-T <encap type>] [-E <probability>]\n");
+  fprintf(stderr, "               [-F <capture type>] [-s <snaplen>] [-t <time adjustment>]\n");
   fprintf(stderr, "               <infile> <outfile> [ <record#>[-<record#>] ... ]\n");
   fprintf(stderr, "  where\n");
   fprintf(stderr, "       \t-E <probability> specifies the probability (between 0 and 1)\n");
@@ -368,7 +373,7 @@ int main(int argc, char *argv[])
       out_frame_type = wtap_file_encap(wth);
 
     pdh = wtap_dump_open(argv[optind + 1], out_file_type,
-                        out_frame_type, wtap_snapshot_length(wth), &err);
+                        out_frame_type, wtap_snapshot_length(wth), FALSE /* compressed */, &err);
     if (pdh == NULL) {
 
       fprintf(stderr, "editcap: Can't open or create %s: %s\n", argv[optind+1],
@@ -401,32 +406,32 @@ int main(int argc, char *argv[])
 
         /* assume that if the frame's tv_sec is 0, then
          * the timestamp isn't supported */
-        if (phdr->ts.tv_sec > 0 && time_adj.tv.tv_sec != 0) {
+        if (phdr->ts.secs > 0 && time_adj.tv.tv_sec != 0) {
           snap_phdr = *phdr;
           if (time_adj.is_negative)
-            snap_phdr.ts.tv_sec -= time_adj.tv.tv_sec;
+            snap_phdr.ts.secs -= time_adj.tv.tv_sec;
           else
-            snap_phdr.ts.tv_sec += time_adj.tv.tv_sec;
+            snap_phdr.ts.secs += time_adj.tv.tv_sec;
           phdr = &snap_phdr;
         }
 
         /* assume that if the frame's tv_sec is 0, then
          * the timestamp isn't supported */
-        if (phdr->ts.tv_sec > 0 && time_adj.tv.tv_usec != 0) {
+        if (phdr->ts.secs > 0 && time_adj.tv.tv_usec != 0) {
           snap_phdr = *phdr;
           if (time_adj.is_negative) { /* subtract */
-            if (snap_phdr.ts.tv_usec < time_adj.tv.tv_usec) { /* borrow */
-              snap_phdr.ts.tv_sec--;
-              snap_phdr.ts.tv_usec += ONE_MILLION;
+            if (snap_phdr.ts.nsecs/1000 < time_adj.tv.tv_usec) { /* borrow */
+              snap_phdr.ts.secs--;
+              snap_phdr.ts.nsecs += ONE_MILLION * 1000;
             }
-            snap_phdr.ts.tv_usec -= time_adj.tv.tv_usec;
+            snap_phdr.ts.nsecs -= time_adj.tv.tv_usec * 1000;
           } else {                  /* add */
-            if (snap_phdr.ts.tv_usec + time_adj.tv.tv_usec > ONE_MILLION) {
+            if (snap_phdr.ts.nsecs + time_adj.tv.tv_usec * 1000 > ONE_MILLION * 1000) {
               /* carry */
-              snap_phdr.ts.tv_sec++;
-              snap_phdr.ts.tv_usec += time_adj.tv.tv_usec - ONE_MILLION;
+              snap_phdr.ts.secs++;
+              snap_phdr.ts.nsecs += (time_adj.tv.tv_usec - ONE_MILLION) * 1000;
             } else {
-              snap_phdr.ts.tv_usec += time_adj.tv.tv_usec;
+              snap_phdr.ts.nsecs += time_adj.tv.tv_usec * 1000;
             }
           }
           phdr = &snap_phdr;
@@ -438,8 +443,14 @@ int main(int argc, char *argv[])
             if (rand() <= err_prob * RAND_MAX) {
               err_type = rand() / (RAND_MAX / ERR_WT_TOTAL + 1);
 
+              if (err_type < ERR_WT_BIT) {
+                buf[i] ^= 1 << (rand() / (RAND_MAX / 8 + 1));
+                err_type = ERR_WT_TOTAL;
+              } else {
+                err_type -= ERR_WT_BYTE;
+              }
+
               if (err_type < ERR_WT_BYTE) {
-printf("err_wt_byte: %d\n", i);
                 buf[i] = rand() / (RAND_MAX / 255 + 1);
                 err_type = ERR_WT_TOTAL;
               } else {
@@ -447,7 +458,6 @@ printf("err_wt_byte: %d\n", i);
               }
 
               if (err_type < ERR_WT_ALNUM) {
-printf("err_wt_alnum: %d\n", i);
                 buf[i] = ALNUM_CHARS[rand() / (RAND_MAX / ALNUM_LEN + 1)];
                 err_type = ERR_WT_TOTAL;
               } else {
@@ -455,8 +465,7 @@ printf("err_wt_alnum: %d\n", i);
               }
 
               if (err_type < ERR_WT_FMT) {
-printf("err_wt_fmt: %d\n", i);
-                if (i < phdr->caplen - 2)
+                if ((unsigned int)i < phdr->caplen - 2)
                   strcpy(&buf[i],  "%s");
                 err_type = ERR_WT_TOTAL;
               } else {
@@ -464,7 +473,6 @@ printf("err_wt_fmt: %d\n", i);
               }
 
               if (err_type < ERR_WT_AA) {
-printf("err_wt_aa: %d\n", i);
                 for (j = i; j < (int) phdr->caplen; j++) {
                   buf[j] = 0xAA;
                 }