-fix off-by-1
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_neighbours.c
index 2b679db0b695219cfc2a976cc0b57d6170420c96..6f3137f8a4b47d90b4191757558930ae7f27b29b 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 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -41,7 +41,6 @@
 #include "gnunet-service-xdht_datacache.h"
 #include "gnunet-service-xdht_neighbours.h"
 #include "gnunet-service-xdht_routing.h"
-#include <fenv.h>
 #include "dht.h"
 
 /**
@@ -285,7 +284,7 @@ struct PeerGetResultMessage
   /**
    * When does the content expire?
    */
-  struct GNUNET_TIME_Absolute expiration_time;
+  struct GNUNET_TIME_AbsoluteNBO expiration_time;
 
   /**
    * The key of the corresponding GET request.
@@ -854,25 +853,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.
@@ -2182,11 +2181,11 @@ find_local_best_known_next_hop (uint64_t destination_finger_value,
 void
 GDS_NEIGHBOURS_send_put (const struct GNUNET_HashCode *key,
                          enum GNUNET_BLOCK_Type block_type,
-                                          enum GNUNET_DHT_RouteOption options,
-                                          uint32_t desired_replication_level,
-                                          struct GNUNET_PeerIdentity best_known_dest,
-                                          struct GNUNET_HashCode intermediate_trail_id,
-                                          struct GNUNET_PeerIdentity *target_peer,
+                         enum GNUNET_DHT_RouteOption options,
+                         uint32_t desired_replication_level,
+                         struct GNUNET_PeerIdentity best_known_dest,
+                         struct GNUNET_HashCode intermediate_trail_id,
+                         struct GNUNET_PeerIdentity *target_peer,
                          uint32_t hop_count,
                          uint32_t put_path_length,
                          struct GNUNET_PeerIdentity *put_path,
@@ -2558,7 +2557,7 @@ GDS_NEIGHBOURS_send_get_result (const struct GNUNET_HashCode *key,
   get_result->header.type = htons (GNUNET_MESSAGE_TYPE_XDHT_P2P_GET_RESULT);
   get_result->key = *key;
   get_result->querying_peer = *source_peer;
-  get_result->expiration_time = expiration;
+  get_result->expiration_time = GNUNET_TIME_absolute_hton (expiration);
   get_result->get_path_length = htonl (get_path_length);
   get_result->put_path_length = htonl (put_path_length);
   paths = (struct GNUNET_PeerIdentity *)&get_result[1];
@@ -2675,11 +2674,9 @@ compute_finger_identity_value (unsigned int finger_index)
  * finger identity through this random friend.
  *
  * @param cls closure for this task
- * @param tc the context under which the task is running
  */
 static void
-send_find_finger_trail_message (void *cls,
-                                const struct GNUNET_SCHEDULER_TaskContext *tc)
+send_find_finger_trail_message (void *cls)
 {
   struct FriendInfo *target_friend;
   struct GNUNET_HashCode trail_id;
@@ -2917,7 +2914,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++)
   {
@@ -3220,12 +3217,11 @@ add_new_finger (struct GNUNET_PeerIdentity finger_identity,
  * Periodic task to verify current successor. There can be multiple trails to reach
  * to successor, choose the shortest one and send verify successor message
  * across that trail.
+ *
  * @param cls closure for this task
- * @param tc the context under which the task is running
  */
 static void
-send_verify_successor_message (void *cls,
-                               const struct GNUNET_SCHEDULER_TaskContext *tc)
+send_verify_successor_message (void *cls)
 {
   struct FriendInfo *target_friend;
   struct GNUNET_HashCode trail_id;
@@ -3238,7 +3234,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
@@ -3259,7 +3255,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
@@ -3313,8 +3309,10 @@ send_verify_successor_message (void *cls,
   trail_id = trail->trail_id;
   if (NULL == GDS_ROUTING_get_next_hop(trail_id,GDS_ROUTING_SRC_TO_DEST))
   {
-    DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
-            GNUNET_i2s(&my_identity), GNUNET_h2s(&trail->trail_id), __LINE__);
+    DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line %u",
+          GNUNET_i2s (&my_identity),
+          GNUNET_h2s (&trail->trail_id),
+          __LINE__);
     GNUNET_break(0);
     return;
   }
@@ -3374,7 +3372,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);
@@ -3736,6 +3734,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 */
@@ -4097,7 +4096,8 @@ handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
 
   if (0 == (GNUNET_CRYPTO_cmp_peer_identity (&my_identity, &(get_path[0]))))
   {
-    GDS_CLIENTS_handle_reply (get_result->expiration_time, &(get_result->key),
+    GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (get_result->expiration_time),
+                              &(get_result->key),
                               getlen, get_path, putlen,
                               put_path, get_result->type, payload_size, payload);
     return GNUNET_YES;
@@ -4120,7 +4120,8 @@ handle_dht_p2p_get_result (void *cls, const struct GNUNET_PeerIdentity *peer,
     GDS_NEIGHBOURS_send_get_result (&(get_result->key), get_result->type,
                                     &get_path[current_path_index - 1],
                                     &(get_result->querying_peer), putlen, put_path,
-                                    getlen, get_path, get_result->expiration_time,
+                                    getlen, get_path,
+                                    GNUNET_TIME_absolute_ntoh (get_result->expiration_time),
                                     payload, payload_size);
     return GNUNET_YES;
   }
@@ -5075,10 +5076,10 @@ struct SendNotifyContext
   unsigned int num_retries_scheduled;
 };
 
+
 void
-send_notify_new_successor (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext
-                           * tc);
+send_notify_new_successor (void *cls);
+
 
 /**
  * Check if the peer which sent us verify successor result message is still ours
@@ -5133,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,
@@ -5173,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,
@@ -5248,11 +5249,9 @@ compare_and_update_successor (struct GNUNET_PeerIdentity curr_succ,
 
 
 void
-send_notify_new_successor (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext
-                           * tc)
+send_notify_new_successor (void *cls)
 {
-  struct SendNotifyContext *ctx = (struct SendNotifyContext *) cls;
+  struct SendNotifyContext *ctx = cls;
 
   GDS_NEIGHBOURS_send_notify_new_successor (ctx->source_peer,
                                             ctx->successor,
@@ -5262,7 +5261,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.
@@ -5271,7 +5270,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++;
@@ -5337,12 +5336,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);
@@ -5355,8 +5354,10 @@ handle_dht_p2p_verify_successor_result(void *cls,
   {
     /* Here it may happen that source peer has found a new successor, and removed
      the trail, Hence no entry found in the routing table. Fail silently.*/
-    DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
-            GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
+    DEBUG (" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line %u",
+           GNUNET_i2s (&my_identity),
+           GNUNET_h2s (&trail_id),
+           __LINE__);
     GNUNET_break_op(0);
     return GNUNET_OK;
   }
@@ -5533,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 +
@@ -5776,8 +5777,10 @@ handle_dht_p2p_trail_teardown (void *cls, const struct GNUNET_PeerIdentity *peer
   next_hop = GDS_ROUTING_get_next_hop (trail_id, trail_direction);
   if (NULL == next_hop)
   {
-    DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line",
-            GNUNET_i2s(&my_identity), GNUNET_h2s(&trail_id), __LINE__);
+    DEBUG(" NO ENTRY FOUND IN %s ROUTING TABLE for trail id %s, line %u",
+          GNUNET_i2s (&my_identity),
+          GNUNET_h2s(&trail_id),
+          __LINE__);
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
@@ -6063,10 +6066,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);
@@ -6109,7 +6112,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);
   }
@@ -6244,33 +6247,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;
   }
 }