More API function tests...
[oweals/gnunet.git] / src / dht / gnunet-service-wdht_clients.c
index 2e4d4ef0a0b4b26d68f99876b6f207d48087c96f..7ad0d2904959674746267fae7db5c0ac2de5f92e 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     Copyright (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
 
      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.
 */
 
 /**
- * @file dht/gnunet-service-dht_clients.c
+ * @file dht/gnunet-service-wdht_clients.c
  * @brief GNUnet DHT service's client management code
  * @author Christian Grothoff
  * @author Nathan Evans
@@ -31,7 +31,7 @@
 #include "gnunet_statistics_service.h"
 #include "gnunet-service-wdht.h"
 #include "gnunet-service-wdht_clients.h"
-#include "gnunet-service-wdht_datacache.h"
+#include "gnunet-service-dht_datacache.h"
 #include "gnunet-service-wdht_neighbours.h"
 #include "dht.h"
 
@@ -64,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;
 
@@ -165,7 +165,7 @@ struct ClientQueryRecord
   size_t xquery_size;
 
   /**
-   * Number of entries in 'seen_replies'.
+   * Number of entries in @e seen_replies.
    */
   unsigned int seen_replies_count;
 
@@ -281,7 +281,7 @@ process_pending_messages (struct ClientList *client);
 
 
 /**
- * Callback called as a result of issuing a GNUNET_SERVER_notify_transmit_ready
+ * Callback called as a result of issuing a #GNUNET_SERVER_notify_transmit_ready()
  * request.  A ClientList is passed as closure, take the head of the list
  * and copy it into buf, which has the result of sending the message to the
  * client.
@@ -316,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;
 }
 
@@ -375,7 +380,7 @@ add_pending_message (struct ClientList *client,
 
 
 /**
- * Closure for 'forward_reply'
+ * Closure for #forward_reply()
  */
 struct ForwardReplyContext
 {
@@ -401,7 +406,7 @@ struct ForwardReplyContext
   size_t data_size;
 
   /**
-   * Do we need to copy 'pm' because it was already used?
+   * Do we need to copy @e pm because it was already used?
    */
   int do_copy;
 
@@ -470,14 +475,16 @@ remove_client_records (void *cls, const struct GNUNET_HashCode * key, void *valu
  * each of the matching clients.  With some tricky recycling
  * of the buffer.
  *
- * @param cls the 'struct ForwardReplyContext'
+ * @param cls the `struct ForwardReplyContext`
  * @param key current key
  * @param value value in the hash map, a ClientQueryRecord
- * @return GNUNET_YES (we should continue to iterate),
- *         if the result is mal-formed, GNUNET_NO
+ * @return #GNUNET_YES (we should continue to iterate),
+ *         if the result is mal-formed, #GNUNET_NO
  */
 static int
-forward_reply (void *cls, const struct GNUNET_HashCode * key, void *value)
+forward_reply (void *cls,
+               const struct GNUNET_HashCode *key,
+               void *value)
 {
   struct ForwardReplyContext *frc = cls;
   struct ClientQueryRecord *record = value;
@@ -573,7 +580,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];
@@ -646,7 +653,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 = %lu\n",msize);
+  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;
@@ -659,17 +667,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)
   {
@@ -682,6 +696,7 @@ GDS_CLIENTS_handle_reply (struct GNUNET_TIME_Absolute expiration,
   }
 }
 
+
 /**
  * Check if some client is monitoring GET messages and notify
  * them in that case.
@@ -701,7 +716,7 @@ GDS_CLIENTS_process_get (uint32_t options,
                          uint32_t desired_replication_level,
                          unsigned int path_length,
                          const struct GNUNET_PeerIdentity *path,
-                         const struct GNUNET_HashCode * key)
+                         const struct GNUNET_HashCode *key)
 {
   struct ClientMonitorRecord *m;
   struct ClientList **cl;
@@ -742,10 +757,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);
     }
@@ -758,16 +773,16 @@ GDS_CLIENTS_process_get (uint32_t options,
  * Check if some client is monitoring PUT messages and notify
  * them in that case.
  *
- * @param options Options, for instance RecordRoute, DemultiplexEverywhere.
- * @param type The type of data in the request.
- * @param hop_count Hop count so far.
- * @param path_length number of entries in path (or 0 if not recorded).
+ * @param options options, for instance RecordRoute, DemultiplexEverywhere.
+ * @param type type of data in the request.
+ * @param hop_count hop count so far.
+ * @param path_length number of entries in @a path (or 0 if not recorded).
  * @param path peers on the PUT path (or NULL if not recorded).
- * @param desired_replication_level Desired replication level.
- * @param exp Expiration time of the data.
- * @param key Key under which data is to be stored.
- * @param data Pointer to the data carried.
- * @param size Number of bytes in data.
+ * @param desired_replication_level desired replication level.
+ * @param exp expiration time of the data.
+ * @param key key under which @a data is to be stored.
+ * @param data pointer to the data carried.
+ * @param size number of bytes in @a data.
  */
 void
 GDS_CLIENTS_process_put (uint32_t options,
@@ -777,7 +792,7 @@ GDS_CLIENTS_process_put (uint32_t options,
                          unsigned int path_length,
                          const struct GNUNET_PeerIdentity *path,
                          struct GNUNET_TIME_Absolute exp,
-                         const struct GNUNET_HashCode * key,
+                         const struct GNUNET_HashCode *key,
                          const void *data,
                          size_t size)
 {
@@ -824,13 +839,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);
     }
   }
@@ -871,18 +886,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 = NULL;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
   while (NULL != (cqr = GNUNET_CONTAINER_heap_remove_root (retry_heap)))
   {
     cqr->hnode = NULL;
@@ -913,7 +924,8 @@ transmit_next_request_task (void *cls,
  * @param message the actual message received
  */
 static void
-handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_put (void *cls,
+                      struct GNUNET_SERVER_Client *client,
                       const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ClientPutMessage *put_msg;
@@ -974,7 +986,8 @@ handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
  * @param message the actual message received
  */
 static void
-handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_get (void *cls,
+                      struct GNUNET_SERVER_Client *client,
                       const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ClientGetMessage *get;
@@ -1009,7 +1022,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 ();
@@ -1041,7 +1054,7 @@ handle_dht_local_get (void *cls, struct GNUNET_SERVER_Client *client,
 
 
 /**
- * Closure for 'find_by_unique_id'.
+ * Closure for #find_by_unique_id().
  */
 struct FindByUniqueIdContext
 {
@@ -1050,6 +1063,9 @@ struct FindByUniqueIdContext
    */
   struct ClientQueryRecord *cqr;
 
+  /**
+   * Which ID are we looking for?
+   */
   uint64_t unique_id;
 };
 
@@ -1061,8 +1077,8 @@ struct FindByUniqueIdContext
  *
  * @param cls the search context
  * @param key query for the lookup (not used)
- * @param value the 'struct ClientQueryRecord'
- * @return GNUNET_YES to continue iteration (result not yet found)
+ * @param value the `struct ClientQueryRecord`
+ * @return #GNUNET_YES to continue iteration (result not yet found)
  */
 static int
 find_by_unique_id (void *cls,
@@ -1087,7 +1103,8 @@ find_by_unique_id (void *cls,
  * @param message the actual message received
  */
 static void
-handle_dht_local_get_result_seen (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_get_result_seen (void *cls,
+                                  struct GNUNET_SERVER_Client *client,
                                  const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ClientGetResultSeenMessage *seen;
@@ -1131,14 +1148,14 @@ 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);
 }
 
 
 /**
- * Closure for 'remove_by_unique_id'.
+ * Closure for #remove_by_unique_id().
  */
 struct RemoveByUniqueIdContext
 {
@@ -1161,10 +1178,12 @@ struct RemoveByUniqueIdContext
  * @param cls unique ID and client to search for in source routes
  * @param key current key code
  * @param value value in the hash map, a ClientQueryRecord
- * @return GNUNET_YES (we should continue to iterate)
+ * @return #GNUNET_YES (we should continue to iterate)
  */
 static int
-remove_by_unique_id (void *cls, const struct GNUNET_HashCode * key, void *value)
+remove_by_unique_id (void *cls,
+                     const struct GNUNET_HashCode *key,
+                     void *value)
 {
   const struct RemoveByUniqueIdContext *ctx = cls;
   struct ClientQueryRecord *record = value;
@@ -1188,7 +1207,8 @@ remove_by_unique_id (void *cls, const struct GNUNET_HashCode * key, void *value)
  *
  */
 static void
-handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_get_stop (void *cls,
+                           struct GNUNET_SERVER_Client *client,
                            const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ClientGetStopMessage *dht_stop_msg =
@@ -1201,7 +1221,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,
@@ -1219,7 +1240,8 @@ handle_dht_local_get_stop (void *cls, struct GNUNET_SERVER_Client *client,
  *
  */
 static void
-handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_monitor (void *cls,
+                          struct GNUNET_SERVER_Client *client,
                           const struct GNUNET_MessageHeader *message)
 {
   struct ClientMonitorRecord *r;
@@ -1238,7 +1260,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);
@@ -1251,10 +1273,10 @@ handle_dht_local_monitor (void *cls, struct GNUNET_SERVER_Client *client,
  * @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_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client,
+handle_dht_local_monitor_stop (void *cls,
+                               struct GNUNET_SERVER_Client *client,
                                const struct GNUNET_MessageHeader *message)
 {
   struct ClientMonitorRecord *r;
@@ -1289,47 +1311,10 @@ handle_dht_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client,
     }
     r = r->next;
   }
-
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
-#if ENABLE_MALICIOUS
-/**
- * Handler for act malicious 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)
-{
-  const struct GNUNET_DHT_ActMaliciousMessage *msg;
-  struct PendingMessage *pm;
-  struct GNUNET_DHT_ClientActMaliciousConfirmationMessage *conf;
-  unsigned int malicious_action;
-
-  msg = (const struct GNUNET_DHT_ActMaliciousMessage *)message;
-  malicious_action = msg->action;
-
-  if(GNUNET_OK == GDS_NEIGHBOURS_act_malicious (malicious_action))
-  {
-    pm = GNUNET_malloc (sizeof (struct PendingMessage) +
-                     sizeof (struct GNUNET_DHT_ClientActMaliciousConfirmationMessage));
-    conf = (struct GNUNET_DHT_ClientActMaliciousConfirmationMessage *) &pm[1];
-    conf->header.size = htons (sizeof (struct GNUNET_DHT_ClientActMaliciousConfirmationMessage));
-    conf->header.type = htons (GNUNET_MESSAGE_TYPE_DHT_CLIENT_ACT_MALICIOUS_OK);
-    pm->msg = &conf->header;
-    add_pending_message (find_active_client (client), pm);
-    GNUNET_SERVER_receive_done (client, GNUNET_OK);
-  }
-}
-#endif
-
-
 /**
  * Functions with this signature are called whenever a client
  * is disconnected on the network level.
@@ -1380,7 +1365,6 @@ handle_client_disconnect (void *cls,
 }
 
 
-
 /**
  * Initialize client subsystem.
  *
@@ -1405,11 +1389,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);
@@ -1446,4 +1425,4 @@ GDS_CLIENTS_done ()
   }
 }
 
-/* end of gnunet-service-dht_clients.c */
+/* end of gnunet-service-wdht_clients.c */