Start implementation of some functions.
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_neighbours.c
index 52b410aff4ebeed359ddc218c12e8ab7f6e08b79..8339f9ce149b9c0b79c37cc598c20ba1ffe18ecf 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009-2014 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009-2014 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -854,25 +854,25 @@ struct VerifySuccessorContext
  * Task that sends FIND FINGER TRAIL requests. This task is started when we have
  * get our first friend.
  */
-static GNUNET_SCHEDULER_TaskIdentifier find_finger_trail_task;
+static struct GNUNET_SCHEDULER_Task * find_finger_trail_task;
 
 /**
  * Task that sends verify successor message. This task is started when we get
  * our successor for the first time.
  */
-static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_task;
+static struct GNUNET_SCHEDULER_Task * send_verify_successor_task;
 
 /**
  * Task that sends verify successor message. This task is started when we get
  * our successor for the first time.
  */
-static GNUNET_SCHEDULER_TaskIdentifier send_verify_successor_retry_task;
+static struct GNUNET_SCHEDULER_Task * send_verify_successor_retry_task;
 
 /**
  * Task that sends verify successor message. This task is started when we get
  * our successor for the first time.
  */
-static GNUNET_SCHEDULER_TaskIdentifier send_notify_new_successor_retry_task;
+static struct GNUNET_SCHEDULER_Task * send_notify_new_successor_retry_task;
 
 /**
  * Identity of this peer.
@@ -979,11 +979,18 @@ core_transmit_notify (void *cls, size_t size, void *buf)
   size_t msize;
 
   peer->th = NULL;
-  while ((NULL != (pending = peer->head)) &&
-         (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us))
+  while ( (NULL != (pending = peer->head)) &&
+          (0 == GNUNET_TIME_absolute_get_remaining (pending->timeout).rel_value_us) )
   {
+    GNUNET_STATISTICS_update (GDS_stats,
+                              gettext_noop
+                              ("# Messages dropped (CORE timeout)"),
+                              1,
+                              GNUNET_NO);
     peer->pending_count--;
-    GNUNET_CONTAINER_DLL_remove (peer->head, peer->tail, pending);
+    GNUNET_CONTAINER_DLL_remove (peer->head,
+                                 peer->tail,
+                                 pending);
     GNUNET_free (pending);
   }
   if (NULL == pending)
@@ -1009,7 +1016,8 @@ core_transmit_notify (void *cls, size_t size, void *buf)
   {
     GNUNET_STATISTICS_update (GDS_stats,
                               gettext_noop
-                              ("# Bytes transmitted to other peers"), msize,
+                              ("# Bytes transmitted to other peers"),
+                              msize,
                               GNUNET_NO);
     memcpy (&cbuf[off], pending->msg, msize);
     off += msize;
@@ -2469,6 +2477,7 @@ GDS_NEIGHBOURS_handle_get(const struct GNUNET_HashCode *key,
 
 /**
  * Send the get result to requesting client.
+ *
  * @param key Key of the requested data.
  * @param type Block type
  * @param target_peer Next peer to forward the message to.
@@ -2507,8 +2516,7 @@ GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
   {
     put_path_length = 0;
-    msize = msize - put_path_length;
-    return;
+    msize = msize - put_path_length * sizeof (struct GNUNET_PeerIdentity);
   }
 
   if (msize >= GNUNET_CONSTANTS_MAX_ENCRYPTED_MESSAGE_SIZE)
@@ -2909,7 +2917,7 @@ add_new_trail (struct FingerInfo *existing_finger,
     friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap,
                                                 &new_trail[0]);
   }
-
+  GNUNET_assert (NULL != friend);
   friend->trails_count++;
   for (i = 0; i < new_trail_length; i++)
   {
@@ -3230,7 +3238,7 @@ send_verify_successor_message (void *cls,
   successor = &finger_table[0];
 
   /* This task will be scheduled when the result for Verify Successor is received. */
-  send_verify_successor_task = GNUNET_SCHEDULER_NO_TASK;
+  send_verify_successor_task = NULL;
 
   /* When verify successor is being called for first time *for current context*
    * cls will be NULL. If send_verify_successor_retry_task is not NO_TASK, we
@@ -3251,7 +3259,7 @@ send_verify_successor_message (void *cls,
      * --> Waiting for notify confirmation. again don't wait for it. notify
      *    confirmation will not succeded.
      */
-    if (send_verify_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
+    if (send_verify_successor_retry_task != NULL)
     {
       /* FIXME: Are we scheduling retry task as soon as we send verify message.
        If yes then here before making this task, first check if the message
@@ -3366,7 +3374,7 @@ update_current_search_finger_index (unsigned int finger_table_index)
     current_search_finger_index = PREDECESSOR_FINGER_ID;
     if (0 != GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &successor->finger_identity))
     {
-      if (GNUNET_SCHEDULER_NO_TASK == send_verify_successor_task)
+      if (NULL == send_verify_successor_task)
       {
         send_verify_successor_task =
                 GNUNET_SCHEDULER_add_now (&send_verify_successor_message, NULL);
@@ -3651,7 +3659,7 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_PeerIdentity best_known_dest;
   struct GNUNET_HashCode received_intermediate_trail_id;
   struct GNUNET_HashCode intermediate_trail_id;
-  struct GNUNET_PeerIdentity *next_hop;
+  struct GNUNET_PeerIdentity next_hop;
   struct GNUNET_PeerIdentity *next_routing_hop;
   enum GNUNET_DHT_RouteOption options;
   struct GNUNET_HashCode test_key;
@@ -3728,6 +3736,7 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
   {
     switch (GNUNET_BLOCK_evaluate (GDS_block_context,
                                    ntohl (put->block_type),
+                                   GNUNET_BLOCK_EO_NONE,
                                    NULL,    /* query */
                                    NULL, 0, /* bloom filer */
                                    NULL, 0, /* xquery */
@@ -3777,8 +3786,7 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
   key_value = GNUNET_ntohll (key_value);
   successor = find_local_best_known_next_hop (key_value,
                                               GDS_FINGER_TYPE_NON_PREDECESSOR);
-  next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
-  *next_hop = successor.next_hop;
+  next_hop = successor.next_hop;
   intermediate_trail_id = successor.trail_id;
   best_known_dest = successor.best_known_destination;
 
@@ -3788,7 +3796,7 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
                                                  GDS_ROUTING_SRC_TO_DEST);
     if (NULL != next_routing_hop)
     {
-      next_hop = next_routing_hop;
+      next_hop = *next_routing_hop;
       intermediate_trail_id = received_intermediate_trail_id;
       best_known_dest = current_best_known_dest;
     }
@@ -3831,19 +3839,19 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
           GDS_NEIGHBOURS_send_put (&put->key,
                                   ntohl (put->block_type),ntohl (put->options),
                                   ntohl (put->desired_replication_level),
-                                  best_known_dest, intermediate_trail_id, next_hop,
+                                  best_known_dest, intermediate_trail_id, &next_hop,
                                   hop_count, putlen, pp,
                                   GNUNET_TIME_absolute_ntoh (put->expiration_time),
                                   payload, payload_size);
           return GNUNET_OK;
         }
-        next_hop = &next_hop_finger->trail_list[i].trail_head->peer;
+        next_hop = next_hop_finger->trail_list[i].trail_head->peer;
         GDS_NEIGHBOURS_send_put (&put->key,
                                  ntohl (put->block_type),ntohl (put->options),
                                  ntohl (put->desired_replication_level),
                                  best_known_dest,
                                  next_hop_finger->trail_list[i].trail_id,
-                                 next_hop, hop_count, putlen, pp,
+                                 &next_hop, hop_count, putlen, pp,
                                  GNUNET_TIME_absolute_ntoh (put->expiration_time),
                                  payload, payload_size);
        }
@@ -3854,7 +3862,7 @@ handle_dht_p2p_put (void *cls, const struct GNUNET_PeerIdentity *peer,
   GDS_NEIGHBOURS_send_put (&put->key,
                            ntohl (put->block_type),ntohl (put->options),
                            ntohl (put->desired_replication_level),
-                           best_known_dest, intermediate_trail_id, next_hop,
+                           best_known_dest, intermediate_trail_id, &next_hop,
                            hop_count, putlen, pp,
                            GNUNET_TIME_absolute_ntoh (put->expiration_time),
                            payload, payload_size);
@@ -3885,7 +3893,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
   struct GNUNET_HashCode intermediate_trail_id;
   struct GNUNET_HashCode received_intermediate_trail_id;
   struct Closest_Peer successor;
-  struct GNUNET_PeerIdentity *next_hop;
+  struct GNUNET_PeerIdentity next_hop;
   struct GNUNET_PeerIdentity *next_routing_hop;
   uint32_t get_length;
   uint64_t key_value;
@@ -3956,8 +3964,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
 
   successor = find_local_best_known_next_hop (key_value,
                                                 GDS_FINGER_TYPE_NON_PREDECESSOR);
-  next_hop = GNUNET_new (struct GNUNET_PeerIdentity);
-  *next_hop = successor.next_hop;
+  next_hop = successor.next_hop;
   best_known_dest = successor.best_known_destination;
   intermediate_trail_id = successor.trail_id;
   /* I am not the final destination. I am part of trail to reach final dest. */
@@ -3967,7 +3974,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
                                                   GDS_ROUTING_SRC_TO_DEST);
     if (NULL != next_routing_hop)
     {
-      next_hop = next_routing_hop;
+      next_hop = *next_routing_hop;
       best_known_dest = current_best_known_dest;
       intermediate_trail_id = received_intermediate_trail_id;
     }
@@ -4006,17 +4013,17 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
       {
         GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
                                 get->desired_replication_level, best_known_dest,
-                                intermediate_trail_id, next_hop, hop_count,
+                                intermediate_trail_id, &next_hop, hop_count,
                                 get_length, gp);
         return GNUNET_OK;
       }
       if (GNUNET_YES == next_hop_finger->trail_list[i].is_present)
       {
-        next_hop = &next_hop_finger->trail_list[i].trail_head->peer;
+        next_hop = next_hop_finger->trail_list[i].trail_head->peer;
         GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
                                  get->desired_replication_level, best_known_dest,
                                  next_hop_finger->trail_list[i].trail_id,
-                                 next_hop, hop_count,
+                                 &next_hop, hop_count,
                                  get_length, gp);
        }
     }
@@ -4025,7 +4032,7 @@ handle_dht_p2p_get (void *cls, const struct GNUNET_PeerIdentity *peer,
 #endif
     GDS_NEIGHBOURS_send_get (&(get->key), get->block_type, get->options,
                              get->desired_replication_level, best_known_dest,
-                             intermediate_trail_id, next_hop, hop_count,
+                             intermediate_trail_id, &next_hop, hop_count,
                              get_length, gp);
   }
   return GNUNET_YES;
@@ -4550,8 +4557,6 @@ get_shortest_trail (struct FingerInfo *finger,
   struct GNUNET_PeerIdentity *trail_list;
   unsigned int i;
 
-  trail = GNUNET_new (struct Trail);
-
   /* Get the shortest trail to reach to current successor. */
   for (i = 0; i < finger->trails_count; i++)
   {
@@ -5129,7 +5134,7 @@ compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
       GNUNET_STATISTICS_set (GDS_stats, key, succ, 0);
       GNUNET_free (key);
     }
-    if (send_verify_successor_task == GNUNET_SCHEDULER_NO_TASK)
+    if (send_verify_successor_task == NULL)
       send_verify_successor_task =
               GNUNET_SCHEDULER_add_delayed(verify_successor_next_send_time,
                                            &send_verify_successor_message,
@@ -5169,7 +5174,7 @@ compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
       successor_times--;
 
 
-    if (send_verify_successor_task == GNUNET_SCHEDULER_NO_TASK)
+    if (send_verify_successor_task == NULL)
       send_verify_successor_task =
               GNUNET_SCHEDULER_add_delayed(verify_successor_next_send_time,
                                            &send_verify_successor_message,
@@ -5235,11 +5240,10 @@ compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
   notify_ctx->succesor_trail_id = trail_id;
   notify_ctx->target_friend = target_friend;
   notify_ctx->num_retries_scheduled = 0;
+  GNUNET_free_non_null (trail_me_to_probable_succ);
 
   // TODO: Check if we should verify before schedule if already scheduled.
   GNUNET_SCHEDULER_add_now(&send_notify_new_successor, (void*)notify_ctx);
-
-  return;
 }
 
 
@@ -5259,7 +5263,7 @@ send_notify_new_successor (void *cls,
                                             ctx->target_friend);
 
   if (0 == ctx->num_retries_scheduled &&
-          send_notify_new_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
+          send_notify_new_successor_retry_task != NULL)
   {
     // Result from previous notify successos hasn't arrived, so the retry task
     // hasn't been cancelled! Already a new notify successor must be called.
@@ -5268,7 +5272,7 @@ send_notify_new_successor (void *cls,
     old_notify_ctx = GNUNET_SCHEDULER_cancel(send_notify_new_successor_retry_task);
     GNUNET_free (old_notify_ctx->successor_trail);
     GNUNET_free (old_notify_ctx);
-    send_notify_new_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
+    send_notify_new_successor_retry_task = NULL;
   }
 
   ctx->num_retries_scheduled++;
@@ -5334,12 +5338,12 @@ handle_dht_p2p_verify_successor_result(void *cls,
   if(0 == (GNUNET_CRYPTO_cmp_peer_identity (&querying_peer, &my_identity)))
   {
     /* Cancel Retry Task */
-    if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_retry_task)
+    if (NULL != send_verify_successor_retry_task)
     {
       struct VerifySuccessorContext *ctx;
       ctx = GNUNET_SCHEDULER_cancel(send_verify_successor_retry_task);
       GNUNET_free(ctx);
-      send_verify_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
+      send_verify_successor_retry_task = NULL;
     }
     compare_and_update_successor (current_successor,
                                   probable_successor, trail, trail_length);
@@ -5432,6 +5436,7 @@ handle_dht_p2p_notify_new_successor(void *cls,
 
     compare_and_update_predecessor (source, trail, trail_length);
     target_friend = GNUNET_CONTAINER_multipeermap_get (friend_peermap, peer);
+    GNUNET_assert (NULL != target_friend);
     GDS_NEIGHBOURS_send_notify_succcessor_confirmation (trail_id,
                                                         GDS_ROUTING_DEST_TO_SRC,
                                                         target_friend);
@@ -5529,15 +5534,15 @@ handle_dht_p2p_notify_succ_confirmation (void *cls,
     */
 
     // TODO: cancel schedule of notify_successor_retry_task
-    if (send_notify_new_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
+    if (send_notify_new_successor_retry_task != NULL)
     {
       struct SendNotifyContext *notify_ctx;
       notify_ctx = GNUNET_SCHEDULER_cancel(send_notify_new_successor_retry_task);
       GNUNET_free (notify_ctx->successor_trail);
       GNUNET_free (notify_ctx);
-      send_notify_new_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
+      send_notify_new_successor_retry_task = NULL;
     }
-    if (send_verify_successor_task == GNUNET_SCHEDULER_NO_TASK)
+    if (send_verify_successor_task == NULL)
     {
       verify_successor_next_send_time.rel_value_us =
       DHT_SEND_VERIFY_SUCCESSOR_INTERVAL.rel_value_us +
@@ -6059,10 +6064,10 @@ handle_core_disconnect (void *cls,
   if (0 != GNUNET_CONTAINER_multipeermap_size (friend_peermap))
     return;
 
-  if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
+  if (NULL != find_finger_trail_task)
   {
       GNUNET_SCHEDULER_cancel (find_finger_trail_task);
-      find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
+      find_finger_trail_task = NULL;
   }
   else
     GNUNET_break (0);
@@ -6105,7 +6110,7 @@ handle_core_connect (void *cls, const struct GNUNET_PeerIdentity *peer_identity)
    * selected after some time out. This is to ensure that both peers have added
    * each other as their friend. */
   /* Got a first connection, good time to start with FIND FINGER TRAIL requests...*/
-  if (GNUNET_SCHEDULER_NO_TASK == find_finger_trail_task)
+  if (NULL == find_finger_trail_task)
   {
     find_finger_trail_task = GNUNET_SCHEDULER_add_now (&send_find_finger_trail_message, NULL);
   }
@@ -6240,33 +6245,33 @@ GDS_NEIGHBOURS_done (void)
   GNUNET_CONTAINER_multipeermap_destroy (friend_peermap);
   friend_peermap = NULL;
 
-  if (GNUNET_SCHEDULER_NO_TASK != find_finger_trail_task)
+  if (NULL != find_finger_trail_task)
   {
     GNUNET_SCHEDULER_cancel (find_finger_trail_task);
-    find_finger_trail_task = GNUNET_SCHEDULER_NO_TASK;
+    find_finger_trail_task = NULL;
   }
 
-  if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_task)
+  if (NULL != send_verify_successor_task)
   {
     GNUNET_SCHEDULER_cancel (send_verify_successor_task);
-    send_verify_successor_task = GNUNET_SCHEDULER_NO_TASK;
+    send_verify_successor_task = NULL;
   }
 
-  if (GNUNET_SCHEDULER_NO_TASK != send_verify_successor_retry_task)
+  if (NULL != send_verify_successor_retry_task)
   {
     struct VerifySuccessorContext *ctx;
     ctx = GNUNET_SCHEDULER_cancel (send_verify_successor_retry_task);
     GNUNET_free(ctx);
-    send_verify_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
+    send_verify_successor_retry_task = NULL;
   }
 
-  if (send_notify_new_successor_retry_task != GNUNET_SCHEDULER_NO_TASK)
+  if (send_notify_new_successor_retry_task != NULL)
   {
     struct SendNotifyContext *notify_ctx;
     notify_ctx = GNUNET_SCHEDULER_cancel(send_notify_new_successor_retry_task);
     GNUNET_free (notify_ctx->successor_trail);
     GNUNET_free (notify_ctx);
-    send_notify_new_successor_retry_task = GNUNET_SCHEDULER_NO_TASK;
+    send_notify_new_successor_retry_task = NULL;
   }
 }