-fix off-by-1
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_clients.c
index e5513f6a953f601fd5a1e9da2dcf0b341e4c146c..bc5504ebe07ce600b37b85e06c7d6154b231dd3e 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 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 DEBUG(...)                                           \
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
-#if ENABLE_MALICIOUS
-/**
- * Should this peer act malicious?
- */
-extern unsigned int malicious;
-#endif
-
 /**
  * Linked list of messages to send to clients.
  */
@@ -275,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;
 
 
 /**
@@ -325,13 +318,18 @@ send_reply_to_client (void *cls, size_t size, void *buf)
                                  reply);
     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;
 }
 
@@ -494,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));
@@ -526,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",
@@ -841,7 +844,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)
@@ -850,16 +853,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);
-  
-  GDS_NEIGHBOURS_handle_get (&cqr->key, cqr->type, cqr->msg_options, 
+
+  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);
@@ -873,18 +876,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;
@@ -948,8 +947,8 @@ handle_dht_local_put (void *cls, struct GNUNET_SERVER_Client *client,
                             ntohl (put_msg->type),
                             size - sizeof (struct GNUNET_DHT_ClientPutMessage),
                             &put_msg[1]);
-  GDS_NEIGHBOURS_handle_put (&put_msg->key, 
+
+  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),
@@ -1020,11 +1019,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),
@@ -1035,7 +1034,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);
@@ -1298,7 +1297,7 @@ handle_dht_local_monitor_stop (void *cls, struct GNUNET_SERVER_Client *client,
 
 #if ENABLE_MALICIOUS
 /**
- * Handler for act malicous message.
+ * Handler for act malicious message.
  *
  * @param cls closure for the service
  * @param client the client we received this message from
@@ -1310,11 +1309,24 @@ handle_dht_act_malicious (void *cls, struct GNUNET_SERVER_Client *client,
                           const struct GNUNET_MessageHeader *message)
 {
   const struct GNUNET_DHT_ActMaliciousMessage *msg;
-  unsigned int malicious;
-  
+  struct PendingMessage *pm;
+  struct GNUNET_DHT_ClientActMaliciousConfirmationMessage *conf;
+  unsigned int malicious_action;
+
   msg = (const struct GNUNET_DHT_ActMaliciousMessage *)message;
-  malicious = msg->action;
-  GDS_NEIGHBOURS_act_malicious(malicious);
+  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
 
@@ -1416,10 +1428,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)
   {