refactor DHT for new service API
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_clients.c
index 8d877654a69ed1e241cc263ba95adaa461ed2e88..3185243b767bf920f219e9f0c1dde97a29a238f1 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 2010, 2011, 2016 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.
 */
 
 /**
 
 #define LOG(kind,...) GNUNET_log_from (kind, "dht-clients",__VA_ARGS__)
 
-#if ENABLE_MALICIOUS
-/**
- * Should this peer act malicious?
- */
-extern unsigned int malicious;
-#endif
+#define DEBUG(...)                                           \
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
 /**
  * Linked list of messages to send to clients.
@@ -68,7 +64,7 @@ struct PendingMessage
   /**
    * Actual message to be sent, allocated at the end of the struct:
    * // msg = (cast) &pm[1];
-   * // memcpy (&pm[1], data, len);
+   * // GNUNET_memcpy (&pm[1], data, len);
    */
   const struct GNUNET_MessageHeader *msg;
 
@@ -272,7 +268,7 @@ static struct GNUNET_CONTAINER_Heap *retry_heap;
 /**
  * Task that re-transmits requests (using retry_heap).
  */
-static GNUNET_SCHEDULER_TaskIdentifier retry_task;
+static struct GNUNET_SCHEDULER_Task * retry_task;
 
 
 /**
@@ -320,15 +316,20 @@ send_reply_to_client (void *cls, size_t size, void *buf)
   {
     GNUNET_CONTAINER_DLL_remove (client->pending_head, client->pending_tail,
                                  reply);
-    memcpy (&cbuf[off], reply->msg, msize);
+    GNUNET_memcpy (&cbuf[off], reply->msg, msize);
     GNUNET_free (reply);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitting %u bytes to client %p\n",
-                msize, client->client_handle);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Transmitting %u bytes to client %p\n",
+                (unsigned int) msize,
+                client->client_handle);
     off += msize;
   }
   process_pending_messages (client);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmitted %u/%u bytes to client %p\n",
-              (unsigned int) off, (unsigned int) size, client->client_handle);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Transmitted %u/%u bytes to client %p\n",
+              (unsigned int) off,
+              (unsigned int) size,
+              client->client_handle);
   return off;
 }
 
@@ -491,7 +492,7 @@ forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
   int do_free;
   struct GNUNET_HashCode ch;
   unsigned int i;
+
   LOG_TRAFFIC (GNUNET_ERROR_TYPE_DEBUG,
               "XVINE CLIENT-RESULT %s\n",
                GNUNET_h2s_full (key));
@@ -523,8 +524,13 @@ forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
       return GNUNET_YES;        /* duplicate */
     }
   eval =
-      GNUNET_BLOCK_evaluate (GDS_block_context, record->type, key, NULL, 0,
-                             record->xquery, record->xquery_size, frc->data,
+      GNUNET_BLOCK_evaluate (GDS_block_context,
+                             record->type,
+                             GNUNET_BLOCK_EO_NONE,
+                             key, NULL, 0,
+                             record->xquery,
+                             record->xquery_size,
+                             frc->data,
                              frc->data_size);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Evaluation result is %d for key %s for local client's query\n",
@@ -572,7 +578,7 @@ forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
     /* two clients waiting for same reply, must copy for queueing */
     pm = GNUNET_malloc (sizeof (struct PendingMessage) +
                         ntohs (frc->pm->msg->size));
-    memcpy (pm, frc->pm,
+    GNUNET_memcpy (pm, frc->pm,
             sizeof (struct PendingMessage) + ntohs (frc->pm->msg->size));
     pm->next = pm->prev = NULL;
     pm->msg = (struct GNUNET_MessageHeader *) &pm[1];
@@ -645,6 +651,8 @@ GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
                 _("Could not pass reply to client, message too big!\n"));
     return;
   }
+  DEBUG ("reply FOR DATA_SIZE = %u\n",
+        (unsigned int) msize);
   pm = GNUNET_malloc (msize + sizeof (struct PendingMessage));
   reply = (struct GNUNET_DHT_ClientResultMessage *) &pm[1];
   pm->msg = &reply->header;
@@ -657,17 +665,23 @@ GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
   reply->expiration = GNUNET_TIME_absolute_hton (expiration);
   reply->key = *key;
   paths = (struct GNUNET_PeerIdentity *) &reply[1];
-  memcpy (paths, put_path,
-          sizeof (struct GNUNET_PeerIdentity) * put_path_length);
-  memcpy (&paths[put_path_length], get_path,
-          sizeof (struct GNUNET_PeerIdentity) * get_path_length);
-  memcpy (&paths[get_path_length + put_path_length], data, data_size);
+  GNUNET_memcpy (paths,
+                put_path,
+                sizeof (struct GNUNET_PeerIdentity) * put_path_length);
+  GNUNET_memcpy (&paths[put_path_length],
+                get_path,
+                sizeof (struct GNUNET_PeerIdentity) * get_path_length);
+  GNUNET_memcpy (&paths[get_path_length + put_path_length],
+                data,
+                data_size);
   frc.do_copy = GNUNET_NO;
   frc.pm = pm;
   frc.data = data;
   frc.data_size = data_size;
   frc.type = type;
-  GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, key, &forward_reply,
+  GNUNET_CONTAINER_multihashmap_get_multiple (forward_map,
+                                             key,
+                                             &forward_reply,
                                               &frc);
   if (GNUNET_NO == frc.do_copy)
   {
@@ -740,10 +754,10 @@ GDS_CLIENTS_process_get (uint32_t options,
       mmsg->hop_count = htonl(hop_count);
       mmsg->desired_replication_level = htonl(desired_replication_level);
       mmsg->get_path_length = htonl(path_length);
-      memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
+      GNUNET_memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
       msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
       if (path_length > 0)
-        memcpy (msg_path, path,
+        GNUNET_memcpy (msg_path, path,
                 path_length * sizeof (struct GNUNET_PeerIdentity));
       add_pending_message (m->client, pm);
     }
@@ -822,13 +836,13 @@ GDS_CLIENTS_process_put (uint32_t options,
       msg_path = (struct GNUNET_PeerIdentity *) &mmsg[1];
       if (path_length > 0)
       {
-        memcpy (msg_path, path,
+        GNUNET_memcpy (msg_path, path,
                 path_length * sizeof (struct GNUNET_PeerIdentity));
       }
       mmsg->expiration_time = GNUNET_TIME_absolute_hton(exp);
-      memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
+      GNUNET_memcpy (&mmsg->key, key, sizeof (struct GNUNET_HashCode));
       if (size > 0)
-        memcpy (&msg_path[path_length], data, size);
+        GNUNET_memcpy (&msg_path[path_length], data, size);
       add_pending_message (m->client, pm);
     }
   }
@@ -837,7 +851,7 @@ GDS_CLIENTS_process_put (uint32_t options,
 
 
 /**
- * Route the given request via the DHT.  
+ * Route the given request via the DHT.
  */
 static void
 transmit_request (struct ClientQueryRecord *cqr)
@@ -846,27 +860,16 @@ transmit_request (struct ClientQueryRecord *cqr)
                             gettext_noop
                             ("# GET requests from clients injected"), 1,
                             GNUNET_NO);
-  
+
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Initiating GET for %s, replication %u, already have %u replies\n",
        GNUNET_h2s (&cqr->key),
        cqr->replication,
        cqr->seen_replies_count);
-  struct GNUNET_PeerIdentity best_known_dest;
-  struct GNUNET_HashCode intermediate_trail_id;
-  
-  memset (&best_known_dest, 0, sizeof (struct GNUNET_PeerIdentity));
-  memset (&intermediate_trail_id, 0, sizeof (struct GNUNET_HashCode));
-
-  GDS_NEIGHBOURS_send_get (&cqr->key, cqr->type, cqr->msg_options, 
-                           cqr->replication, best_known_dest, 
-                           intermediate_trail_id , NULL,
-                           0, 0, NULL);
-#if 0
-  GDS_NEIGHBOURS_send_get (&cqr->key, cqr->type, cqr->msg_options, 
-                           cqr->replication, NULL, NULL , NULL,
-                           0, 0, NULL);
-#endif
+
+  GDS_NEIGHBOURS_handle_get (&cqr->key, cqr->type, cqr->msg_options,
+                              cqr->replication);
+
   /* exponential back-off for retries.
    * max GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD (15 min) */
   cqr->retry_frequency = GNUNET_TIME_STD_BACKOFF (cqr->retry_frequency);
@@ -880,18 +883,14 @@ transmit_request (struct ClientQueryRecord *cqr)
  * itself (unless the heap is empty).
  *
  * @param cls unused
- * @param tc scheduler context
  */
 static void
-transmit_next_request_task (void *cls,
-                            const struct GNUNET_SCHEDULER_TaskContext *tc)
+transmit_next_request_task (void *cls)
 {
   struct ClientQueryRecord *cqr;
   struct GNUNET_TIME_Relative delay;
 
-  retry_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+  retry_task = NULL;
   while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
   {
     cqr->hnode = NULL;
@@ -949,54 +948,19 @@ handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
        "Handling local PUT of %u-bytes for query %s\n",
        size - sizeof (struct GNUNET_DHT_ClientPutMessage),
        GNUNET_h2s (&put_msg->key));
-  
+  DEBUG("PUT doing put i = %s\n",GNUNET_h2s(&(put_msg->key)));
   GDS_CLIENTS_handle_reply (GNUNET_TIME_absolute_ntoh (put_msg->expiration),
                             &put_msg->key, 0, NULL, 0, NULL,
                             ntohl (put_msg->type),
                             size - sizeof (struct GNUNET_DHT_ClientPutMessage),
                             &put_msg[1]);
-  /* FIXME: Should we store locally? */
-/* GDS_DATACACHE_handle_put (GNUNET_TIME_absolute_ntoh (put_msg->expiration),
-                            &put_msg->key, 0, NULL, ntohl (put_msg->type),
-                            size - sizeof (struct GNUNET_DHT_ClientPutMessage),
-                            &put_msg[1]);*/
-  struct GNUNET_PeerIdentity my_identity =  GDS_NEIGHBOURS_get_my_id();
-  struct GNUNET_PeerIdentity best_known_destination;
-  struct GNUNET_HashCode intermediate_trail_id;
-  
-  memset(&best_known_destination, 0 , sizeof (struct GNUNET_PeerIdentity));
-  memset(&intermediate_trail_id, 0, sizeof (struct GNUNET_HashCode));
-
-  GDS_NEIGHBOURS_send_put (&put_msg->key, 
-                           ntohl (put_msg->type), ntohl (put_msg->options),
-                           ntohl (put_msg->desired_replication_level), 
-                           best_known_destination,
-                           intermediate_trail_id, NULL, 0, 0, NULL,
-                           GNUNET_TIME_absolute_ntoh (put_msg->expiration),
-                           &put_msg[1],
-                           size - sizeof (struct GNUNET_DHT_ClientPutMessage));
-#if 0
-  GDS_NEIGHBOURS_send_put (&put_msg->key, 
-                           ntohl (put_msg->type), ntohl (put_msg->options),
-                           ntohl (put_msg->desired_replication_level), NULL,
-                           NULL, NULL, 0, 0, NULL,
-                           GNUNET_TIME_absolute_ntoh (put_msg->expiration),
-                           &put_msg[1],
-                           size - sizeof (struct GNUNET_DHT_ClientPutMessage));
-  
-#endif
-  GDS_CLIENTS_process_put (ntohl (put_msg->options),
-                           ntohl (put_msg->type),
-                           0,
-                           ntohl (put_msg->desired_replication_level),
-                           1,
-                           &my_identity,
-                           GNUNET_TIME_absolute_ntoh (put_msg->expiration),
-                           &put_msg->key,
-                           &put_msg[1],
-                           size - sizeof (struct GNUNET_DHT_ClientPutMessage));
-  
+
+  GDS_NEIGHBOURS_handle_put (&put_msg->key,
+                              ntohl (put_msg->type), ntohl (put_msg->options),
+                              ntohl (put_msg->desired_replication_level),
+                              GNUNET_TIME_absolute_ntoh (put_msg->expiration),
+                              &put_msg[1],
+                              size - sizeof (struct GNUNET_DHT_ClientPutMessage));
   pm = GNUNET_malloc (sizeof (struct PendingMessage) +
                      sizeof (struct GNUNET_DHT_ClientPutConfirmationMessage));
   conf = (struct GNUNET_DHT_ClientPutConfirmationMessage *) &pm[1];
@@ -1053,7 +1017,7 @@ handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
   cqr->key = get->key;
   cqr->client = find_active_client (client);
   cqr->xquery = (void *) &cqr[1];
-  memcpy (&cqr[1], xquery, xquery_size);
+  GNUNET_memcpy (&cqr[1], xquery, xquery_size);
   cqr->hnode = GNUNET_CONTAINER_heap_insert (retry_heap, cqr, 0);
   cqr->retry_frequency = GNUNET_TIME_UNIT_SECONDS;
   cqr->retry_time = GNUNET_TIME_absolute_get ();
@@ -1062,11 +1026,11 @@ handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
   cqr->replication = ntohl (get->desired_replication_level);
   cqr->msg_options = ntohl (get->options);
   cqr->type = ntohl (get->type);
-  
+
   // FIXME use cqr->key, set multihashmap create to GNUNET_YES
   GNUNET_CONTAINER_multihashmap_put (forward_map, &get->key, cqr,
                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
-  
+
   struct GNUNET_PeerIdentity my_identity;
   my_identity = GDS_NEIGHBOURS_get_my_id();
   GDS_CLIENTS_process_get (ntohl (get->options),
@@ -1077,7 +1041,7 @@ handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
                            &my_identity,
                            &get->key);
   /* start remote requests */
-  if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+  if (NULL != retry_task)
     GNUNET_SCHEDULER_cancel (retry_task);
   retry_task = GNUNET_SCHEDULER_add_now (&transmit_next_request_task, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1175,7 +1139,7 @@ handle_dht_local_get_result_seen (void *cls, struct GNUNET_SERVER_Client *client
   GNUNET_array_grow (cqr->seen_replies,
                     cqr->seen_replies_count,
                     cqr->seen_replies_count + hash_count);
-  memcpy (&cqr->seen_replies[old_count],
+  GNUNET_memcpy (&cqr->seen_replies[old_count],
          hc,
          sizeof (struct GNUNET_HashCode) * hash_count);
 }
@@ -1245,7 +1209,8 @@ handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
                             GNUNET_NO);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Received GET STOP request for %s from local client %p\n",
-       client, GNUNET_h2s (&dht_stop_msg->key));
+       GNUNET_h2s (&dht_stop_msg->key),
+       client);
   ctx.client = find_active_client (client);
   ctx.unique_id = dht_stop_msg->unique_id;
   GNUNET_CONTAINER_multihashmap_get_multiple (forward_map, &dht_stop_msg->key,
@@ -1282,7 +1247,7 @@ handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
   else
   {
     r->key = GNUNET_new (struct GNUNET_HashCode);
-    memcpy (r->key, &msg->key, sizeof (struct GNUNET_HashCode));
+    GNUNET_memcpy (r->key, &msg->key, sizeof (struct GNUNET_HashCode));
   }
   GNUNET_CONTAINER_DLL_insert (monitor_head, monitor_tail, r);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
@@ -1338,30 +1303,6 @@ handle_dht_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client,
 }
 
 
-#if ENABLE_MALICIOUS
-/**
- * Handler for act malicous message.
- *
- * @param cls closure for the service
- * @param client the client we received this message from
- * @param message the actual message received
- *
- */
-static void
-handle_dht_act_malicious (void *cls, struct GNUNET_SERVER_Client *client,
-                          const struct GNUNET_MessageHeader *message)
-{
-  /* FIXME: parse message and set malicious */
-  const struct GNUNET_DHT_ActMaliciousMessage *msg;
-  unsigned int malicious;
-  
-  msg = (const struct GNUNET_DHT_ActMaliciousMessage *)message;
-  malicious = msg->action;
-  GDS_NEIGHBOURS_act_malicious(malicious);
-}
-#endif
-
-
 /**
  * Functions with this signature are called whenever a client
  * is disconnected on the network level.
@@ -1437,11 +1378,6 @@ GDS_CLIENTS_init (struct GNUNET_SERVER_Handle *server)
      sizeof (struct GNUNET_DHT_MonitorStartStopMessage)},
     {&handle_dht_local_get_result_seen, NULL,
      GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN, 0},
-    #if ENABLE_MALICIOUS
-    {&handle_dht_act_malicious, NULL,
-     GNUNET_MESSAGE_TYPE_DHT_ACT_MALICIOUS,
-     sizeof (struct GNUNET_DHT_ActMaliciousMessage)},
-    #endif
     {NULL, NULL, 0, 0}
   };
   forward_map = GNUNET_CONTAINER_multihashmap_create (1024, GNUNET_NO);
@@ -1459,10 +1395,10 @@ GDS_CLIENTS_done ()
 {
   GNUNET_assert (client_head == NULL);
   GNUNET_assert (client_tail == NULL);
-  if (GNUNET_SCHEDULER_NO_TASK != retry_task)
+  if (NULL != retry_task)
   {
     GNUNET_SCHEDULER_cancel (retry_task);
-    retry_task = GNUNET_SCHEDULER_NO_TASK;
+    retry_task = NULL;
   }
   if (NULL != retry_heap)
   {