remove SHA-512 code, use libgcrypt implementation
[oweals/gnunet.git] / src / dht / dht_api.c
index 5abc228a3bd79c2cbdbf7e153bdebcd46e8ae4d0..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;
 
@@ -365,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;
     }
@@ -398,6 +399,12 @@ 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)
@@ -412,17 +419,56 @@ process_reply (void *cls,
       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;
 }
 
@@ -557,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 ***** */
 
 
 /**
@@ -613,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,
@@ -648,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));
@@ -656,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,