- Act malicious API complete
[oweals/gnunet.git] / src / dht / gnunet_dht_profiler.c
index 98ea16becdfb1f9c05038ae9f49be41650b42f69..592e31bfea482f3b01534969216935c726211382 100644 (file)
 /**
  * Number of peers which should perform a PUT out of 100 peers
  */
-#define PUT_PROBABILITY 100
+#define PUT_PROBABILITY 50
 
+#if ENABLE_MALICIOUS
 /**
- * Percentage of peers that should act maliciously.
- * These peers will never start PUT/GET request.
- * n_active and n_malicious should not intersect.
+ * Number of peers which should act as malicious peers
  */
-#define MALICIOUS_PEERS 0
+#define MALICIOUS_PROBABILITY 20
+
+/**
+ * Context for a peer which should act maliciously. 
+ */
+struct MaliciousContext;
+#endif
+
 
 /**
  * Configuration
@@ -67,7 +73,6 @@ struct ActiveContext;
  */
 struct Context
 {
-
   /**
    * The testbed peer this context belongs to
    */
@@ -82,6 +87,13 @@ struct Context
    * Active context; NULL if this peer is not an active peer
    */
   struct ActiveContext *ac;
+  
+#if ENABLE_MALICIOUS
+  /**
+   * Malicious context; NULL if this peer is NOT malicious. 
+   */
+  struct MaliciousContext *mc;
+#endif
 };
 
 
@@ -100,6 +112,11 @@ struct MaliciousContext
    * Handler to the DHT service
    */
   struct GNUNET_DHT_Handle *dht;
+  
+  /**
+   * Handler to malicious api
+   */
+  struct GNUNET_DHT_ActMaliciousHandle *dht_malicious;
 };
 
 /**
@@ -107,6 +124,32 @@ struct MaliciousContext
  */
 struct Context **malicious_peer_contexts = NULL;
 
+/**
+ * Context for a peer which should act maliciously.
+ */
+struct Malicious_Context
+{
+  /**
+   * The linked peer context
+   */
+  struct Context *ctx;
+
+  /**
+   * Handler to the DHT service
+   */
+  struct GNUNET_DHT_Handle *dht;
+};
+
+/**
+ * Array of malicious peers.
+ */
+static struct MaliciousContext *a_mc;
+
+/**
+ * Number or malicious peers.
+ */
+static unsigned int n_malicious;
+
 #endif
 
 /**
@@ -201,13 +244,6 @@ static struct GNUNET_TIME_Relative timeout;
  */
 static unsigned int num_peers;
 
-#if ENABLE_MALICIOUS
-/**
- * Number or malicious peers.
- */
-static unsigned int n_malicious;
-#endif
-
 /**
  * Number of active peers
  */
@@ -365,6 +401,11 @@ static enum
  */
 static int in_shutdown = 0;
 
+/**
+ * Total number of times to check if circle is formed or not.
+ */
+static unsigned int tries;
+
 /**
  * Task that collects successor statistics from all the peers. 
  * @param cls
@@ -373,6 +414,11 @@ static int in_shutdown = 0;
 static void
 collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
+/**
+ * Connect to DHT services of active peers
+ */
+static void
+start_profiling();
 
 /**
  * Shutdown task.  Cleanup all resources and operations.
@@ -436,7 +482,7 @@ bandwidth_stats_cont (void *cls,
 {
   INFO ("# Outgoing bandwidth: %u\n", outgoing_bandwidth);
   INFO ("# Incoming bandwidth: %u\n", incoming_bandwidth);
-  GNUNET_SCHEDULER_shutdown (); 
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -519,8 +565,11 @@ cancel_get (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   /* If profiling is complete, summarize */
   if (n_active == n_gets_fail + n_gets_ok)
+  {
+    average_put_path_length = (double)total_put_path_length/(double)n_active;
+    average_get_path_length = (double)total_get_path_length/(double )n_gets_ok;
     summarize ();
-
+  }
 }
 
 
@@ -559,7 +608,7 @@ get_iter (void *cls,
   /* Check the keys of put and get match or not. */
   GNUNET_assert (0 == memcmp (key, &get_ac->hash, sizeof (struct GNUNET_HashCode)));
   /* we found the data we are looking for */
-  DEBUG ("We found a GET request; %u remaining\n", n_gets - (n_gets_fail + n_gets_ok));
+  DEBUG ("We found a GET request; %u remaining\n", n_gets - (n_gets_fail + n_gets_ok)); //FIXME: It always prints 1.
   n_gets_ok++;
   get_ac->nrefs--;
   GNUNET_DHT_get_stop (ac->dht_get);
@@ -571,14 +620,14 @@ get_iter (void *cls,
   GNUNET_TESTBED_operation_done (ctx->op);
   ctx->op = NULL;
   
-  total_put_path_length = total_put_path_length + put_path_length;
-  total_get_path_length = total_get_path_length + get_path_length;
-  
+  total_put_path_length = total_put_path_length + (double)put_path_length;
+  total_get_path_length = total_get_path_length + (double)get_path_length;
+  DEBUG ("total_put_path_length = %f,put_path \n",total_put_path_length);
   /* Summarize if profiling is complete */
   if (n_active == n_gets_fail + n_gets_ok)
   {
     average_put_path_length = (double)total_put_path_length/(double)n_active;
-    average_get_path_length = (double)total_get_path_length/(double )n_active;
+    average_get_path_length = (double)total_get_path_length/(double )n_gets_ok;
     summarize ();
   }
 }
@@ -698,7 +747,7 @@ delayed_put (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   DEBUG ("PUT_REQUEST_START key %s \n", GNUNET_h2s((struct GNUNET_HashCode *)ac->put_data));
   ac->dht_put = GNUNET_DHT_put (ac->dht, &ac->hash,
                                 replication,
-                                GNUNET_DHT_RO_NONE,
+                                GNUNET_DHT_RO_RECORD_ROUTE,
                                 GNUNET_BLOCK_TYPE_TEST,
                                 ac->put_data_size,
                                 ac->put_data,
@@ -741,12 +790,25 @@ dht_connected (void *cls,
   switch (mode)
   {
   case MODE_PUT:
-    ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay_put, &delayed_put, ac);
+  {
+    struct GNUNET_TIME_Relative peer_delay_put;
+    peer_delay_put.rel_value_us =
+      GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                delay_put.rel_value_us);
+    ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_put, &delayed_put, ac);
     break;
+  }
   case MODE_GET:
-    ac->delay_task = GNUNET_SCHEDULER_add_delayed (delay_get, &delayed_get, ac);
+  {
+    struct GNUNET_TIME_Relative peer_delay_get;
+    peer_delay_get.rel_value_us =
+      delay_get.rel_value_us +
+      GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                delay_get.rel_value_us);
+    ac->delay_task = GNUNET_SCHEDULER_add_delayed (peer_delay_get, &delayed_get, ac);
     break;
   }
+  }
 }
 
 
@@ -763,17 +825,11 @@ static void *
 dht_connect (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   n_dht++;
+  DEBUG("\n Inside dht_connect , n_dht = %u",n_dht);
   return GNUNET_DHT_connect (cfg, 10);
 }
 
 
-/**
- * Connect to DHT services of active peers
- */
-static void
-start_profiling();
-
-
 /**
  * Adapter function called to destroy a connection to
  * a service.
@@ -810,10 +866,8 @@ dht_disconnect (void *cls, void *op_result)
       return;
     break;
   }
-  GNUNET_SCHEDULER_shutdown ();
 }
 
-
 /**
  * Connect to DHT services of active peers
  */
@@ -822,7 +876,7 @@ start_profiling()
 {
   struct Context *ctx;
   unsigned int i;
-
+   
   DEBUG("GNUNET_TESTBED_service_connect \n");
   GNUNET_break (GNUNET_YES != in_shutdown);
   for(i = 0; i < n_active; i++)
@@ -841,18 +895,157 @@ start_profiling()
   }
 }
 
+#if ENABLE_MALICIOUS
+/**
+ * Count of total number of malicious peers.
+ */
+static unsigned int count_malicious;
+
+/**
+ * Continuation of GNUNET_DHT_act_malicious
+ * @param cls Malicious context
+  * @param success #GNUNET_OK if the ACT_MALICIOUS was transmitted,
+ *                 #GNUNET_NO on timeout,
+ *                 #GNUNET_SYSERR on disconnect from service
+ *                 after the ACT_MALICIOUS message was transmitted
+ *                 (so we don't know if it was received or not)
+ */
+static void
+act_malicious_cont (void *cls, int success)
+{
+  struct MaliciousContext *mc = cls;
+  struct Context *ctx = mc->ctx;
+  
+  GNUNET_TESTBED_operation_done (ctx->op);
+  ctx->op = NULL;
+  return;
+}
+
+
+/**
+ * Call malicious API for all the malicious peers.
+ * @param cls the malicious context.
+ * @param op the operation that has been finished
+ * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
+ * @param emsg error message in case the operation has failed; will be NULL if
+ *          operation has executed successfully.
+ */
+void 
+dht_set_malicious(void *cls,
+                  struct GNUNET_TESTBED_Operation *op,
+                  void *ca_result,
+                  const char *emsg)
+{
+  struct MaliciousContext *mc = cls;
+  struct Context *ctx = mc->ctx;
+
+  GNUNET_assert (NULL != ctx);
+  GNUNET_assert (NULL != ctx->op);
+  GNUNET_assert (ctx->op == op);
+  mc->dht = (struct GNUNET_DHT_Handle *) ca_result;
+  if (NULL != emsg)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connection to DHT service failed: %s\n", emsg);
+    GNUNET_TESTBED_operation_done (ctx->op); /* Calls dht_disconnect_malicious() */
+    ctx->op = NULL;
+    return;
+  }
+  mc->dht_malicious = GNUNET_DHT_act_malicious(mc->dht, 1, act_malicious_cont, mc);
+}
+
+
+/**
+ * Adapter function called to destroy a connection to
+ * a service.
+ *
+ * @param cls the active context
+ * @param op_result service handle returned from the connect adapter
+ */
+static void
+dht_disconnect_malicious (void *cls, void *op_result)
+{
+  struct MaliciousContext *mc = cls;
+  count_malicious++;
+  GNUNET_assert (NULL != mc->dht);
+  GNUNET_assert (mc->dht == op_result);
+  GNUNET_DHT_disconnect (mc->dht);
+  mc->dht = NULL;
+  mc->ctx->op = NULL;
+  n_dht--;
+
+  if (0 != n_dht)
+    return;
+  if(n_malicious == count_malicious)
+  {
+    DEBUG("\n Call start_profiling()");
+    start_profiling();
+  }
+}
+
+
+/**
+ * Set the malicious variable in peer malicious context.
+ */
+static void
+set_malicious()
+{
+  unsigned int i;
+  DEBUG ("Setting %u peers malicious",n_malicious);
+  
+  for(i = 0; i < n_malicious; i++)
+  {
+    DEBUG("\n Inside loop , i = %u",i);
+    struct MaliciousContext *mc = &a_mc[i];
+    mc->ctx->op =
+        GNUNET_TESTBED_service_connect (mc->ctx,
+                                        mc->ctx->peer,
+                                        "dht",
+                                        &dht_set_malicious, mc,
+                                        &dht_connect,
+                                        &dht_disconnect_malicious,
+                                        mc);
+  }
+}
+
+#endif
+
+
+/**
+ * Start collecting relevant statistics. If ENABLE_MALICIOUS set, first
+ * set the malicious peers. If not, then start with PUT operation on active
+ * peers.
+ */
+static void
+start_func()
+{
+#if ENABLE_MALICIOUS
+  set_malicious();
+  return;
+#endif   
+  start_profiling();
+}
+
+
+/**
+ * Remove entry from successor peer hashmap.
+ * @param cls closure
+ * @param key current public key
+ * @param value value in the hash map
+ * @return #GNUNET_YES if we should continue to iterate,
+ *         #GNUNET_NO if not.
+ */
 static int 
 hashmap_iterate_remove(void *cls, 
                        const struct GNUNET_HashCode *key, 
                        void *value)
 {
-  GNUNET_assert(GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove(successor_peer_hashmap, key, value));
+  GNUNET_assert (GNUNET_YES == 
+                GNUNET_CONTAINER_multihashmap_remove(successor_peer_hashmap, key, value));
   return GNUNET_YES;
 }
 
 
-static unsigned int tries;
-
 /**
  * Stats callback. Iterate over the hashmap and check if all th peers form
  * a virtual ring topology.
@@ -876,11 +1069,17 @@ successor_stats_cont (void *cls,
   successor_stats_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_TESTBED_operation_done (successor_stats_op);
   successor_stats_op = NULL;
-  
+  if (0 == max_searches)
+  {
+    start_func(); 
+    return;
+  }
+
+  GNUNET_assert (NULL != start_key);
   start_val =
           (struct GNUNET_HashCode *) GNUNET_CONTAINER_multihashmap_get(successor_peer_hashmap,
                                                 start_key);
-
+  
   val = start_val;
   for (count = 0; count < num_peers; count++)
   {
@@ -912,17 +1111,14 @@ successor_stats_cont (void *cls,
                                                  NULL));
   
   successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers, 
-                                                                    GNUNET_NO);
-  
+                                                                 GNUNET_NO);
   if ((start_val == val) && (count == num_peers))
   {
     DEBUG("CIRCLE COMPLETED after %u tries", tries);
-    //FIXME: FREE HASHMAP.
-    //FIXME: If circle is done, then check that finger table of all the peers
-    //are fill atleast O(log N) and then start with the experiments.
     if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
-      start_profiling();
-    
+    {
+      start_func();
+    }
     return;
   }
   else
@@ -931,7 +1127,7 @@ successor_stats_cont (void *cls,
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Maximum tries %u exceeded while checking successor TOTAL TRIES %u"
-                  " cirle formation.  Exiting\n",
+                  " circle formation.  Exiting\n",
                   max_searches,tries);
       if (GNUNET_SCHEDULER_NO_TASK != successor_stats_task)
       {
@@ -939,7 +1135,7 @@ successor_stats_cont (void *cls,
       }
       if(GNUNET_SCHEDULER_NO_TASK == successor_stats_task)
       {
-        start_profiling();
+        start_func();
       }
       
       return;
@@ -973,6 +1169,9 @@ successor_stats_iterator (void *cls,
                           int is_persistent)
 {
   static const char *key_string = "XDHT";
+  if (0 == max_searches)
+    return GNUNET_OK;
+
   if (0 == strncmp (key_string, name, strlen (key_string)))
   {
     char *my_id_str;
@@ -995,18 +1194,19 @@ successor_stats_iterator (void *cls,
    
     succ_key = GNUNET_new(struct GNUNET_HashCode);
     GNUNET_CRYPTO_hash (truncated_successor_str, sizeof(truncated_successor_str),succ_key);
-    
+
     if (0 == flag)
     {
+      GNUNET_assert(NULL != my_id_key);
       start_key = my_id_key;
+      GNUNET_assert(NULL != start_key);
       flag = 1;
     }
-    /* FIXME: GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE do not free the value
-     which is replaced, need to free it. */
     GNUNET_CONTAINER_multihashmap_put (successor_peer_hashmap,
                                        my_id_key, (void *)succ_key,
                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
   }
+
   return GNUNET_OK;
 }
 
@@ -1025,6 +1225,8 @@ collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Start collecting statistics...\n");
   GNUNET_assert(NULL != testbed_handles);
+  
+  if (0 != max_searches)
   successor_peer_hashmap = GNUNET_CONTAINER_multihashmap_create (num_peers, 
                                                                     GNUNET_NO);
   successor_stats_op = 
@@ -1037,29 +1239,6 @@ collect_stats (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-#if ENABLE_MALICIOUS
-/**
- * Set the malicious variable in peer malicious context.
- */
-static void
-set_malicious()
-{
-  unsigned int i;
-  DEBUG ("Setting %u peers malicious");
-  for(i = 0; i < n_malicious; i++)
-  {
-    struct MaliciousContext *mc = &a_mc[i];
-    mc->ctx->op =
-        GNUNET_TESTBED_service_connect (ac->ctx,
-                                        ac->ctx->peer,
-                                        "dht",
-                                        &dht_set_malicious, mc,
-                                        &dht_connect,
-                                        &dht_finish,
-                                        mc);
-  }
-}
-#endif
 /**
  * Callback called when DHT service on the peer is started
  *
@@ -1083,13 +1262,11 @@ service_started (void *cls,
   DEBUG("Peers Started = %d; num_peers = %d \n", peers_started, num_peers);
   if (GNUNET_SCHEDULER_NO_TASK == successor_stats_task && peers_started == num_peers)
   {
-#if ENABLE_MALICIOUS
-    set_malicious();
-#endif
      DEBUG("successor_stats_task \n");
      struct Collect_Stat_Context *collect_stat_cls = GNUNET_new(struct Collect_Stat_Context);
      collect_stat_cls->service_connect_ctx = cls;
      collect_stat_cls->op = op;
      successor_stats_task = GNUNET_SCHEDULER_add_delayed (delay_stats,
                                                           &collect_stats,
                                                           collect_stat_cls);
@@ -1117,7 +1294,7 @@ test_run (void *cls,
 {
   unsigned int cnt;
   unsigned int ac_cnt;
-  
+  unsigned int malicious_peers;
   testbed_handles = peers;  
   if (NULL == peers)
   {
@@ -1136,9 +1313,12 @@ test_run (void *cls,
     return;
   }
   
+  a_ac = GNUNET_malloc (n_active * sizeof (struct ActiveContext));
+  ac_cnt = 0;
+  
 #if ENABLE_MALICIOUS
 
-  if(PUT_PROBABILITY + MALICIOUS_PEERS > 100)
+  if(PUT_PROBABILITY + MALICIOUS_PROBABILITY > 100)
   {
     DEBUG ("Reduce either number of malicious peer or active peers. ");
     GNUNET_SCHEDULER_shutdown ();
@@ -1147,11 +1327,22 @@ test_run (void *cls,
   }
   
   /* Select the peers which should act maliciously. */
-  n_malicious = num_peers * MALICIOUS_PEERS / 100;
+  n_malicious = num_peers * MALICIOUS_PROBABILITY / 100;
   
-  /* Select n_malicious peers and ensure that those are not active peers. 
-     keep all malicious peer at one place, and call act malicious for all
-     those peers. */
+  a_mc = GNUNET_malloc (n_malicious * sizeof (struct MaliciousContext));
+  malicious_peers = 0;
+  
+  for (cnt = 0; cnt < num_peers && malicious_peers < n_malicious; cnt++)
+  {
+    if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
+        MALICIOUS_PROBABILITY)
+      continue;
+    a_ctx[cnt].mc = &a_mc[malicious_peers];
+    a_mc[malicious_peers].ctx = &a_ctx[cnt];
+    malicious_peers++;
+  } 
+  n_malicious = malicious_peers;
+  INFO ("Malicious Peers: %u\n",malicious_peers);
   
 #endif
   
@@ -1159,9 +1350,10 @@ test_run (void *cls,
   ac_cnt = 0;
   for (cnt = 0; cnt < num_peers && ac_cnt < n_active; cnt++)
   {
-    if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
-        PUT_PROBABILITY)
+    if ((GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100) >=
+        PUT_PROBABILITY) || (a_ctx[ac_cnt].mc != NULL))
       continue;
+    
     a_ctx[cnt].ac = &a_ac[ac_cnt];
     a_ac[ac_cnt].ctx = &a_ctx[cnt];
     ac_cnt++;
@@ -1227,7 +1419,7 @@ main (int argc, char *const *argv)
      gettext_noop ("number of peers to start"),
      1, &GNUNET_GETOPT_set_uint, &num_peers},
     {'s', "searches", "COUNT",
-     gettext_noop ("maximum number of times we try to search for successor circle formation (default is 1)"),
+     gettext_noop ("maximum number of times we try to search for successor circle formation (0 for R5N)"),
      1, &GNUNET_GETOPT_set_uint, &max_searches},
     {'H', "hosts", "FILENAME",
      gettext_noop ("name of the file with the login information for the testbed"),
@@ -1254,10 +1446,10 @@ main (int argc, char *const *argv)
   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
     return 2;
   /* set default delays */
-  delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
-  delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1);
-  delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5);
-  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1);
+  delay_stats = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
+  delay_put = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
+  delay_get = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
+  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10);
   replication = 1;      /* default replication */
   rc = 0;
   if (GNUNET_OK !=