plugin datastore mysql
[oweals/gnunet.git] / src / rps / gnunet-service-rps.c
index 8f25b269daa4c9b31b1f9bf95243f7e5afe423ef..4e24c62315efb9288f9f259e4839dfd2247946e4 100644 (file)
@@ -252,11 +252,6 @@ static struct GNUNET_TIME_Relative request_deltas[REQUEST_DELTAS_SIZE];
  */
 static struct GNUNET_TIME_Relative request_rate;
 
-/**
- * Number of history update tasks.
- */
-static uint32_t num_hist_update_tasks;
-
 
 #ifdef ENABLE_MALICIOUS
 /**
@@ -441,6 +436,45 @@ T_relative_avg (const struct GNUNET_TIME_Relative *rel_array,
 }
 
 
+/**
+ * Insert PeerID in #view
+ *
+ * Called once we know a peer is live.
+ * Implements #PeerOp
+ *
+ * @return GNUNET_OK if peer was actually inserted
+ *         GNUNET_NO if peer was not inserted
+ */
+static void
+insert_in_view_op (void *cls,
+               const struct GNUNET_PeerIdentity *peer);
+
+/**
+ * Insert PeerID in #view
+ *
+ * Called once we know a peer is live.
+ *
+ * @return GNUNET_OK if peer was actually inserted
+ *         GNUNET_NO if peer was not inserted
+ */
+static int
+insert_in_view (const struct GNUNET_PeerIdentity *peer)
+{
+  int online;
+
+  online = Peers_check_peer_flag (peer, Peers_ONLINE);
+  if ( (GNUNET_NO == online) ||
+       (GNUNET_SYSERR == online) ) /* peer is not even known */
+  {
+    (void) Peers_issue_peer_liveliness_check (peer);
+    (void) Peers_schedule_operation (peer, insert_in_view_op);
+    return GNUNET_NO;
+  }
+  /* Open channel towards peer to keep connection open */
+  Peers_indicate_sending_intention (peer);
+  return View_put (peer);
+}
+
 /**
  * Put random peer from sampler into the view as history update.
  */
@@ -453,13 +487,11 @@ hist_update (void *cls,
 
   for (i = 0; i < num_peers; i++)
   {
-    View_put (&ids[i]);
+    (void) insert_in_view (&ids[i]);
     to_file (file_name_view_log,
              "+%s\t(hist)",
              GNUNET_i2s_full (ids));
   }
-  if (0 < num_hist_update_tasks)
-    num_hist_update_tasks--;
 }
 
 
@@ -649,11 +681,10 @@ insert_in_pull_map (void *cls,
  * Implements #PeerOp
  */
 static void
-insert_in_view (void *cls,
+insert_in_view_op (void *cls,
                const struct GNUNET_PeerIdentity *peer)
 {
-  GNUNET_assert (GNUNET_YES == Peers_check_peer_flag (peer, Peers_ONLINE));
-  View_put (peer);
+  (void) insert_in_view (peer);
 }
 
 
@@ -673,11 +704,10 @@ insert_in_sampler (void *cls,
   if (0 < RPS_sampler_count_id (prot_sampler, peer))
   {
     /* Make sure we 'know' about this peer */
-    (void) Peers_insert_peer_check_liveliness (peer);
+    (void) Peers_issue_peer_liveliness_check (peer);
     /* Establish a channel towards that peer to indicate we are going to send
      * messages to it */
-    Peers_indicate_sending_intention (peer);
-    //Peers_issue_peer_liveliness_check (peer);
+    //Peers_indicate_sending_intention (peer);
   }
 }
 
@@ -691,14 +721,13 @@ static void
 got_peer (const struct GNUNET_PeerIdentity *peer)
 {
   /* If we did not know this peer already, insert it into sampler and view */
-  if (GNUNET_YES == Peers_insert_peer_check_liveliness (peer))
+  if (GNUNET_YES == Peers_issue_peer_liveliness_check (peer))
   {
     Peers_schedule_operation (peer, insert_in_sampler);
-    Peers_schedule_operation (peer, insert_in_view);
+    Peers_schedule_operation (peer, insert_in_view_op);
   }
 }
 
-
 /**
  * @brief Checks if there is a sending channel and if it is needed
  *
@@ -892,6 +921,10 @@ static void
 destroy_cli_ctx (struct ClientContext *cli_ctx)
 {
   GNUNET_assert (NULL != cli_ctx);
+  if (NULL != cli_ctx->mq)
+  {
+    GNUNET_MQ_destroy (cli_ctx->mq);
+  }
   if (NULL != cli_ctx->rep_cls_head)
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -966,7 +999,7 @@ client_respond (void *cls,
   for (i = 0; i < num_peers; i++)
   {
     LOG (GNUNET_ERROR_TYPE_DEBUG,
-         "  %lu: %s\n",
+         "  %" PRIu32 ": %s\n",
          i,
          GNUNET_i2s (&peer_ids[i]));
   }
@@ -1074,7 +1107,7 @@ handle_client_request_cancel (void *cls,
   GNUNET_assert (NULL != cli_ctx->rep_cls_head);
   rep_cls = cli_ctx->rep_cls_head;
   LOG (GNUNET_ERROR_TYPE_DEBUG,
-      "Client cancels request with id %lu\n",
+      "Client cancels request with id %" PRIu32 "\n",
       ntohl (msg->id));
   while ( (NULL != rep_cls->next) &&
           (rep_cls->id != ntohl (msg->id)) )
@@ -1311,7 +1344,7 @@ handle_peer_pull_reply (void *cls,
       sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
-        "message says it sends %" PRIu32 " peers, have space for %i peers\n",
+        "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
         ntohl (in_msg->num_peers),
         (ntohs (msg->size) - sizeof (struct GNUNET_RPS_P2P_PullReplyMessage)) /
             sizeof (struct GNUNET_PeerIdentity));
@@ -1383,16 +1416,16 @@ handle_peer_pull_reply (void *cls,
                                               &peers[i]))
     {
       /* Make sure we 'know' about this peer */
-      (void) Peers_insert_peer_check_liveliness (&peers[i]);
+      (void) Peers_insert_peer (&peers[i]);
 
-      if (GNUNET_YES == Peers_check_peer_flag (&peers[i], Peers_VALID))
+      if (GNUNET_YES == Peers_check_peer_valid (&peers[i]))
       {
         CustomPeerMap_put (pull_map, &peers[i]);
       }
       else
       {
         Peers_schedule_operation (&peers[i], insert_in_pull_map);
-        Peers_issue_peer_liveliness_check (&peers[i]);
+        (void) Peers_issue_peer_liveliness_check (&peers[i]);
       }
     }
   }
@@ -1530,7 +1563,7 @@ handle_client_act_malicious (void *cls,
       sizeof (struct GNUNET_PeerIdentity) != ntohl (in_msg->num_peers))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
-        "message says it sends %" PRIu64 " peers, have space for %i peers\n",
+        "message says it sends %" PRIu32 " peers, have space for %lu peers\n",
         ntohl (in_msg->num_peers),
         (ntohs (msg->size) - sizeof (struct GNUNET_RPS_CS_ActMaliciousMessage)) /
             sizeof (struct GNUNET_PeerIdentity));
@@ -1602,8 +1635,7 @@ handle_client_act_malicious (void *cls,
     /* Set the flag of the attacked peer to valid to avoid problems */
     if (GNUNET_NO == Peers_check_peer_known (&attacked_peer))
     {
-      Peers_insert_peer_check_liveliness (&attacked_peer);
-      Peers_issue_peer_liveliness_check (&attacked_peer);
+      (void) Peers_issue_peer_liveliness_check (&attacked_peer);
     }
 
     LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -1693,8 +1725,8 @@ do_mal_round (void *cls)
      * Send as many pushes to the attacked peer as possible
      * That is one push per round as it will ignore more.
      */
-    Peers_insert_peer_check_liveliness (&attacked_peer);
-    if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_VALID))
+    (void) Peers_issue_peer_liveliness_check (&attacked_peer);
+    if (GNUNET_YES == Peers_check_peer_valid (&attacked_peer))
       send_push (&attacked_peer);
   }
 
@@ -1705,20 +1737,16 @@ do_mal_round (void *cls)
     /* Send PUSH to attacked peers */
     if (GNUNET_YES == Peers_check_peer_known (&attacked_peer))
     {
-      Peers_insert_peer_check_liveliness (&attacked_peer);
-      if (GNUNET_YES == Peers_check_peer_flag (&attacked_peer, Peers_VALID))
+      (void) Peers_issue_peer_liveliness_check (&attacked_peer);
+      if (GNUNET_YES == Peers_check_peer_valid (&attacked_peer))
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
             "Goding to send push to attacked peer (%s)\n",
             GNUNET_i2s (&attacked_peer));
         send_push (&attacked_peer);
       }
-      else
-        Peers_issue_peer_liveliness_check (&attacked_peer);
     }
-    else
-      Peers_insert_peer_check_liveliness (&attacked_peer);
-    Peers_issue_peer_liveliness_check (&attacked_peer);
+    (void) Peers_issue_peer_liveliness_check (&attacked_peer);
 
     /* The maximum of pushes we're going to send this round */
     num_pushes = GNUNET_MIN (GNUNET_MIN (push_limit - 1,
@@ -1887,7 +1915,8 @@ do_round (void *cls)
                                            CustomPeerMap_size (push_map));
     for (i = 0; i < first_border; i++)
     {
-      View_put (CustomPeerMap_get_peer_by_index (push_map, permut[i]));
+      (void) insert_in_view (CustomPeerMap_get_peer_by_index (push_map,
+                                                              permut[i]));
       to_file (file_name_view_log,
                "+%s\t(push list)",
                GNUNET_i2s_full (&view_array[i]));
@@ -1901,8 +1930,8 @@ do_round (void *cls)
                                            CustomPeerMap_size (pull_map));
     for (i = first_border; i < second_border; i++)
     {
-      View_put (CustomPeerMap_get_peer_by_index (pull_map,
-                                                 permut[i - first_border]));
+      (void) insert_in_view (CustomPeerMap_get_peer_by_index (pull_map,
+            permut[i - first_border]));
       to_file (file_name_view_log,
                "+%s\t(pull list)",
                GNUNET_i2s_full (&view_array[i]));
@@ -1916,7 +1945,6 @@ do_round (void *cls)
                                   hist_update,
                                   NULL,
                                   final_size - second_border);
-    num_hist_update_tasks = final_size - second_border;
     // TODO change the peer_flags accordingly
 
     for (i = 0; i < View_size (); i++)
@@ -2015,6 +2043,31 @@ init_peer_cb (void *cls,
   }
 }
 
+/**
+ * @brief Iterator function over stored, valid peers.
+ *
+ * We initialise the sampler with those.
+ *
+ * @param cls the closure
+ * @param peer the peer id
+ * @return #GNUNET_YES if we should continue to
+ *         iterate,
+ *         #GNUNET_NO if not.
+ */
+static int
+valid_peers_iterator (void *cls,
+                      const struct GNUNET_PeerIdentity *peer)
+{
+  if (NULL != peer)
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Got stored, valid peer %s\n",
+         GNUNET_i2s (peer));
+    got_peer (peer);
+  }
+  return GNUNET_YES;
+}
+
 
 /**
  * Iterator over peers from peerinfo.
@@ -2070,6 +2123,7 @@ shutdown_task (void *cls)
   CustomPeerMap_destroy (pull_map);
   #ifdef ENABLE_MALICIOUS
   struct AttackedPeer *tmp_att_peer;
+  GNUNET_free (file_name_view_log);
   GNUNET_array_grow (mal_peers, num_mal_peers, 0);
   if (NULL != mal_peer_set)
     GNUNET_CONTAINER_multipeermap_destroy (mal_peer_set);
@@ -2183,6 +2237,7 @@ run (void *cls,
 {
   int size;
   int out_size;
+  char* fn_valid_peers;
 
   GNUNET_log_setup ("rps", GNUNET_error_type_to_string (GNUNET_ERROR_TYPE_DEBUG), NULL);
   cfg = c;
@@ -2221,7 +2276,17 @@ run (void *cls,
     GNUNET_SCHEDULER_shutdown ();
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %" PRIu64 "\n", sampler_size_est_need);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "INITSIZE is %u\n", sampler_size_est_need);
+
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_filename (cfg,
+                                               "rps",
+                                               "FILENAME_VALID_PEERS",
+                                               &fn_valid_peers))
+  {
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+                              "rps", "FILENAME_VALID_PEERS");
+  }
 
 
   View_create (4);
@@ -2266,7 +2331,6 @@ run (void *cls,
     {&handle_peer_pull_reply  , GNUNET_MESSAGE_TYPE_RPS_PP_PULL_REPLY  , 0},
     {NULL, 0, 0}
   };
-
   const uint32_t ports[] = {GNUNET_RPS_CADET_PORT, 0}; // _PORT specified in src/rps/rps.h
   cadet_handle = GNUNET_CADET_connect (cfg,
                                        cls,
@@ -2274,8 +2338,10 @@ run (void *cls,
                                        &cleanup_destroyed_channel,
                                        cadet_handlers,
                                        ports);
+
   peerinfo_handle = GNUNET_PEERINFO_connect (cfg);
-  Peers_initialise (cadet_handle, &own_identity);
+  Peers_initialise (fn_valid_peers, cadet_handle, &own_identity);
+  GNUNET_free (fn_valid_peers);
 
   /* Initialise sampler */
   struct GNUNET_TIME_Relative half_round_interval;
@@ -2292,12 +2358,12 @@ run (void *cls,
   pull_map = CustomPeerMap_create (4);
 
 
-  num_hist_update_tasks = 0;
-
-
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting peers from CADET\n");
   GNUNET_CADET_get_peers (cadet_handle, &init_peer_cb, NULL);
   // TODO send push/pull to each of those peers?
+  // TODO read stored valid peers from last run
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Requesting stored valid peers\n");
+  Peers_get_valid_peers (valid_peers_iterator, NULL);
 
   peerinfo_notify_handle = GNUNET_PEERINFO_notify (cfg,
                                                    GNUNET_NO,