remove SHA-512 code, use libgcrypt implementation
[oweals/gnunet.git] / src / dht / dht_api.c
index c89a92dd53b12394b9d6a14d7bffbf0283a8845d..17a21568197d450841debff7a6e95edab88131bb 100644 (file)
@@ -137,7 +137,7 @@ struct GNUNET_DHT_RouteHandle
   GNUNET_HashCode key;
 
   /**
-   * Unique identifier for this request (for key collisions)
+   * Unique identifier for this request (for key collisions). FIXME: redundant!?
    */
   uint64_t uid;
 
@@ -231,6 +231,8 @@ try_connect (struct GNUNET_DHT_Handle *handle)
                  _("Failed to connect to the DHT service!\n"));
       return GNUNET_NO;
     }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Starting to process replies from DHT\n");
   GNUNET_CLIENT_receive (handle->client,
                          &service_message_handler,
                          handle, 
@@ -363,9 +365,10 @@ transmit_pending (void *cls,
     }
   if (NULL != head->cont)
     {
-      GNUNET_SCHEDULER_add_now (handle->sched,
-                               head->cont,
-                               head->cont_cls);                             
+      GNUNET_SCHEDULER_add_continuation (handle->sched,
+                                         head->cont,
+                                         head->cont_cls,
+                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
       head->cont = NULL;
       head->cont_cls = NULL;
     }
@@ -373,6 +376,9 @@ transmit_pending (void *cls,
   if (GNUNET_YES == head->free_on_send)
     GNUNET_free (head);
   process_pending_messages (handle);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Forwarded request of %u bytes to DHT service\n",
+             (unsigned int) tsize);
   return tsize;
 }
 
@@ -393,25 +399,76 @@ process_reply (void *cls,
   const struct GNUNET_MessageHeader *enc_msg;
   size_t enc_size;
   uint64_t uid;
+  const struct GNUNET_PeerIdentity **get_path;
+  const struct GNUNET_PeerIdentity **put_path;
+  const struct GNUNET_PeerIdentity *pos;
+  uint16_t gpl;
+  uint16_t ppl;
+  unsigned int i;
 
   uid = GNUNET_ntohll (dht_msg->unique_id);
   if (uid != rh->uid)
-    return GNUNET_YES;
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Reply UID did not match request UID\n");
+      return GNUNET_YES;
+    }
   enc_size = ntohs (dht_msg->header.size) - sizeof (struct GNUNET_DHT_RouteResultMessage);
   if (enc_size < sizeof (struct GNUNET_MessageHeader))
     {
       GNUNET_break (0);
       return GNUNET_NO;
     }
-  enc_msg = (const struct GNUNET_MessageHeader *) &dht_msg[1];
+  pos = (const struct GNUNET_PeerIdentity *) &dht_msg[1];
+  ppl = ntohs (dht_msg->put_path_length);
+  gpl = ntohs (dht_msg->get_path_length);
+  if ( (ppl + gpl) * sizeof (struct GNUNET_PeerIdentity) > enc_size)
+    {
+      GNUNET_break (0);
+      return GNUNET_NO;
+    }
+  if (ppl > 0)
+    {
+      put_path = GNUNET_malloc ((ppl+1) * sizeof (struct GNUNET_PeerIdentity*));
+      for (i=0;i<ppl;i++)
+       {
+         put_path[i] = pos;
+         pos++;
+       }
+      put_path[ppl] = NULL;
+    }
+  else
+    put_path = NULL;
+  if (gpl > 0)
+    {
+      get_path = GNUNET_malloc ((gpl+1) * sizeof (struct GNUNET_PeerIdentity*));
+      for (i=0;i<gpl;i++)
+       {
+         get_path[i] = pos;
+         pos++;
+       }
+      get_path[gpl] = NULL;
+    }
+  else
+    get_path = NULL;
+  enc_size -= (ppl + gpl) * sizeof (struct GNUNET_PeerIdentity);
+  enc_msg = (const struct GNUNET_MessageHeader *) pos;
   if (enc_size != ntohs (enc_msg->size))
     {
       GNUNET_break (0);
+      GNUNET_free_non_null (get_path);
+      GNUNET_free_non_null (put_path);
       return GNUNET_NO;
     }
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Processing reply.\n");
   rh->iter (rh->iter_cls, 
            &rh->key,
+           get_path,
+           put_path,
            enc_msg);
+  GNUNET_free_non_null (get_path);
+  GNUNET_free_non_null (put_path);
   return GNUNET_YES;
 }
 
@@ -429,9 +486,14 @@ service_message_handler (void *cls,
 {
   struct GNUNET_DHT_Handle *handle = cls;
   const struct GNUNET_DHT_RouteResultMessage *dht_msg;
-  
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "%s called\n",
+             __FUNCTION__);  
   if (msg == NULL)
     {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                 "Error receiving data from DHT service, reconnecting\n");
       reconnect (handle);
       return;
     }
@@ -448,10 +510,16 @@ service_message_handler (void *cls,
       return;
     }
   dht_msg = (const struct GNUNET_DHT_RouteResultMessage *) msg;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Comparing reply `%s' against %u pending requests.\n",
+             GNUNET_h2s (&dht_msg->key),
+             GNUNET_CONTAINER_multihashmap_size (handle->active_requests));
   GNUNET_CONTAINER_multihashmap_get_multiple (handle->active_requests,
                                              &dht_msg->key,
                                              &process_reply,
                                              (void*) dht_msg);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "Continuing to process replies from DHT\n");
   GNUNET_CLIENT_receive (handle->client,
                          &service_message_handler,
                          handle, GNUNET_TIME_UNIT_FOREVER_REL);
@@ -535,7 +603,7 @@ GNUNET_DHT_disconnect (struct GNUNET_DHT_Handle *handle)
 
 
 
-/* ***** Special low-level API providing generic routeing abstraction ***** */
+/* ***** Special low-level API providing generic routing abstraction ***** */
 
 
 /**
@@ -591,7 +659,7 @@ timeout_route_request (void *cls,
  */
 struct GNUNET_DHT_RouteHandle *
 GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
-                        const GNUNET_HashCode * key,
+                        const GNUNET_HashCode *key,
                         uint32_t desired_replication_level,
                         enum GNUNET_DHT_RouteOption options,
                         const struct GNUNET_MessageHeader *enc,
@@ -626,7 +694,10 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
   message->key = *key;
   message->options = htonl ((uint32_t) options);
   message->desired_replication_level = htonl (desired_replication_level);
+  handle->uid_gen++;
+  message->unique_id = GNUNET_htonll (handle->uid_gen);
   memcpy (&message[1], enc, esize);
+
   if (iter != NULL)
     {
       route_handle = GNUNET_malloc (sizeof (struct GNUNET_DHT_RouteHandle));
@@ -634,9 +705,8 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
       route_handle->iter = iter;
       route_handle->iter_cls = iter_cls;
       route_handle->dht_handle = handle;
-      route_handle->uid = handle->uid_gen++;
+      route_handle->uid = handle->uid_gen;
       route_handle->message = pending;
-      message->unique_id = GNUNET_htonll (route_handle->uid);
       GNUNET_CONTAINER_multihashmap_put (handle->active_requests,
                                          key,
                                         route_handle,
@@ -656,6 +726,9 @@ GNUNET_DHT_route_start (struct GNUNET_DHT_Handle *handle,
                               pending);
   pending->in_pending_queue = GNUNET_YES;
   process_pending_messages (handle);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "DHT route start request processed, returning %p\n",
+             route_handle);
   return route_handle;
 }
 
@@ -708,6 +781,8 @@ GNUNET_DHT_route_stop (struct GNUNET_DHT_RouteHandle *route_handle)
                                                       route_handle));
   GNUNET_free(route_handle->message);
   GNUNET_free(route_handle);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+             "DHT route stop request processed\n");
 }