making GNUNET_SCHEDULER_cancel() perform in O(1) instead of O(n) to help or even...
[oweals/gnunet.git] / src / psycstore / gnunet-service-psycstore.c
index 8a21167e1b80f6c5023d1f74c58f1a7dd54314ea..3228659cc013e30e9d03571fb8024e465dbc1fec 100644 (file)
  * @brief PSYCstore service
  * @author Gabor X Toth
  * @author Christian Grothoff
- *
- * The purpose of this service is to manage private keys that
- * represent the various egos/pseudonyms/identities of a GNUnet user.
- *
  */
+
+#include <inttypes.h>
+
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_constants.h"
@@ -89,36 +88,41 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-/** 
+/**
  * Send a result code back to the client.
  *
- * @param client Client that should receive the result code.
- * @param result_code Code to transmit.
- * @param op_id Operation ID.
- * @param err_msg Error message to include (or NULL for none).
+ * @param client
+ *        Client that should receive the result code.
+ * @param result_code
+ *        Code to transmit.
+ * @param op_id
+ *        Operation ID in network byte order.
+ * @param err_msg
+ *        Error message to include (or NULL for none).
  */
 static void
-send_result_code (struct GNUNET_SERVER_Client *client, uint32_t result_code,
-                  uint32_t op_id, const char *err_msg)
+send_result_code (struct GNUNET_SERVER_Client *client, uint64_t op_id,
+                  int64_t result_code, const char *err_msg)
 {
   struct OperationResult *res;
-  size_t err_len;
+  size_t err_size = 0;
 
-  if (NULL == err_msg)
-    err_len = 0;
-  else
-    err_len = strlen (err_msg) + 1;
-  res = GNUNET_malloc (sizeof (struct OperationResult) + err_len);
+  if (NULL != err_msg)
+    err_size = strnlen (err_msg,
+                        GNUNET_SERVER_MAX_MESSAGE_SIZE - sizeof (*res)) + 1;
+  res = GNUNET_malloc (sizeof (struct OperationResult) + err_size);
   res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_CODE);
-  res->header.size = htons (sizeof (struct OperationResult) + err_len);
-  res->result_code = htonl (result_code);
+  res->header.size = htons (sizeof (struct OperationResult) + err_size);
+  res->result_code = GNUNET_htonll (result_code - INT64_MIN);
   res->op_id = op_id;
-  if (0 < err_len)
-    memcpy (&res[1], err_msg, err_len);
+  if (0 < err_size)
+  {
+    memcpy (&res[1], err_msg, err_size);
+    ((char *) &res[1])[err_size - 1] = '\0';
+  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Sending result %d (%s) to client\n",
-             (int) result_code,
-             err_msg);
+             "Sending result to client: %" PRId64 " (%s)\n",
+             result_code, err_msg);
   GNUNET_SERVER_notification_context_add (nc, client);
   GNUNET_SERVER_notification_context_unicast (nc, client, &res->header,
                                               GNUNET_NO);
@@ -126,10 +130,43 @@ send_result_code (struct GNUNET_SERVER_Client *client, uint32_t result_code,
 }
 
 
+enum
+{
+  MEMBERSHIP_TEST_NOT_NEEDED = 0,
+  MEMBERSHIP_TEST_NEEDED = 1,
+  MEMBERSHIP_TEST_DONE = 2,
+} MessageMembershipTest;
+
+
 struct SendClosure
 {
   struct GNUNET_SERVER_Client *client;
+
+  /**
+   * Channel's public key.
+   */
+  struct GNUNET_CRYPTO_EddsaPublicKey channel_key;
+
+  /**
+   * Slave's public key.
+   */
+  struct GNUNET_CRYPTO_EcdsaPublicKey slave_key;
+
+  /**
+   * Operation ID.
+   */
   uint64_t op_id;
+
+  /**
+   * Membership test result.
+   */
+  int membership_test_result;
+
+  /**
+   * Do membership test with @a slave_key before returning fragment?
+   * @see enum MessageMembershipTest
+   */
+  uint8_t membership_test;
 };
 
 
@@ -139,6 +176,24 @@ send_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
 {
   struct SendClosure *sc = cls;
   struct FragmentResult *res;
+
+  if (MEMBERSHIP_TEST_NEEDED == sc->membership_test)
+  {
+    sc->membership_test = MEMBERSHIP_TEST_DONE;
+    sc->membership_test_result
+      = db->membership_test (db->cls, &sc->channel_key, &sc->slave_key,
+                             GNUNET_ntohll (msg->message_id));
+    switch (sc->membership_test_result)
+    {
+    case GNUNET_YES:
+      break;
+
+    case GNUNET_NO:
+    case GNUNET_SYSERR:
+      return GNUNET_NO;
+    }
+  }
+
   size_t msg_size = ntohs (msg->header.size);
 
   res = GNUNET_malloc (sizeof (struct FragmentResult) + msg_size);
@@ -155,7 +210,7 @@ send_fragment (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg,
   GNUNET_SERVER_notification_context_unicast (nc, sc->client, &res->header,
                                               GNUNET_NO);
   GNUNET_free (res);
-  return GNUNET_OK;
+  return GNUNET_YES;
 }
 
 
@@ -167,13 +222,15 @@ send_state_var (void *cls, const char *name,
   struct StateResult *res;
   size_t name_size = strlen (name) + 1;
 
+  /* FIXME: split up value into 64k chunks */
+
   res = GNUNET_malloc (sizeof (struct StateResult) + name_size + value_size);
   res->header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_STATE);
   res->header.size = htons (sizeof (struct StateResult) + name_size + value_size);
   res->op_id = sc->op_id;
   res->name_size = htons (name_size);
   memcpy (&res[1], name, name_size);
-  memcpy ((void *) &res[1] + name_size, value, value_size);
+  memcpy ((char *) &res[1] + name_size, value, value_size);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Sending state variable %s to client\n", name);
   GNUNET_SERVER_notification_context_add (nc, sc->client);
@@ -193,7 +250,7 @@ handle_membership_store (void *cls,
     (const struct MembershipStoreRequest *) msg;
 
   int ret = db->membership_store (db->cls, &req->channel_key, &req->slave_key,
-                                  ntohl (req->did_join),
+                                  req->did_join,
                                   GNUNET_ntohll (req->announced_at),
                                   GNUNET_ntohll (req->effective_since),
                                   GNUNET_ntohll (req->group_generation));
@@ -202,7 +259,7 @@ handle_membership_store (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to store membership information!\n"));
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -227,7 +284,7 @@ handle_membership_test (void *cls,
                 _("Failed to test membership!\n"));
   }
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -248,7 +305,7 @@ handle_fragment_store (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to store fragment!\n"));
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -258,24 +315,53 @@ handle_fragment_get (void *cls,
                      struct GNUNET_SERVER_Client *client,
                      const struct GNUNET_MessageHeader *msg)
 {
-  const struct FragmentGetRequest *req
-    = (const struct FragmentGetRequest *) msg;
-  struct SendClosure sc = { .op_id = req->op_id, .client = client };
+  const struct FragmentGetRequest *
+    req = (const struct FragmentGetRequest *) msg;
+  struct SendClosure
+    sc = { .op_id = req->op_id, .client = client,
+           .channel_key = req->channel_key, .slave_key = req->slave_key,
+           .membership_test = req->do_membership_test };
+
+  int64_t ret;
+  uint64_t ret_frags = 0;
+  uint64_t first_fragment_id = GNUNET_ntohll (req->first_fragment_id);
+  uint64_t last_fragment_id = GNUNET_ntohll (req->last_fragment_id);
+  uint64_t limit = GNUNET_ntohll (req->fragment_limit);
+
+  if (0 == limit)
+    ret = db->fragment_get (db->cls, &req->channel_key,
+                            first_fragment_id, last_fragment_id,
+                            &ret_frags, &send_fragment, &sc);
+  else
+    ret = db->fragment_get_latest (db->cls, &req->channel_key, limit, 
+                                   &ret_frags, &send_fragment, &sc);
 
-  int ret = db->fragment_get (db->cls, &req->channel_key,
-                              GNUNET_ntohll (req->fragment_id),
-                              &send_fragment, &sc);
   switch (ret)
   {
   case GNUNET_YES:
   case GNUNET_NO:
+    if (MEMBERSHIP_TEST_DONE == sc.membership_test)
+    {
+      switch (sc.membership_test_result)
+      {
+      case GNUNET_YES:
+        break;
+
+      case GNUNET_NO:
+        ret = GNUNET_PSYCSTORE_MEMBERSHIP_TEST_FAILED;
+        break;
+
+      case GNUNET_SYSERR:
+        ret = GNUNET_SYSERR;
+        break;
+      }
+    }
     break;
   default:
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to get fragment!\n"));
   }
-
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -285,24 +371,38 @@ handle_message_get (void *cls,
                     struct GNUNET_SERVER_Client *client,
                     const struct GNUNET_MessageHeader *msg)
 {
-  const struct MessageGetRequest *req = (const struct MessageGetRequest *) msg;
-  struct SendClosure sc = { .op_id = req->op_id, .client = client };
+  const struct MessageGetRequest *
+    req = (const struct MessageGetRequest *) msg;
+  struct SendClosure
+    sc = { .op_id = req->op_id, .client = client,
+           .channel_key = req->channel_key, .slave_key = req->slave_key,
+           .membership_test = req->do_membership_test };
+
+  int64_t ret;
   uint64_t ret_frags = 0;
-  int64_t ret = db->message_get (db->cls, &req->channel_key,
-                                 GNUNET_ntohll (req->message_id),
-                                 &ret_frags, &send_fragment, &sc);
+  uint64_t first_message_id = GNUNET_ntohll (req->first_message_id);
+  uint64_t last_message_id = GNUNET_ntohll (req->last_message_id);
+  uint64_t limit = GNUNET_ntohll (req->message_limit);
+
+  if (0 == limit)
+    ret = db->message_get (db->cls, &req->channel_key,
+                           first_message_id, last_message_id,
+                           &ret_frags, &send_fragment, &sc);
+  else
+    ret = db->message_get_latest (db->cls, &req->channel_key, limit,
+                                  &ret_frags, &send_fragment, &sc);
+
   switch (ret)
   {
   case GNUNET_YES:
   case GNUNET_NO:
     break;
   default:
-    ret_frags = ret;
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to get message!\n"));
   }
 
-  send_result_code (client, ret_frags, req->op_id, NULL);
+  send_result_code (client, req->op_id, (ret < 0) ? ret : ret_frags, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -312,10 +412,12 @@ handle_message_get_fragment (void *cls,
                              struct GNUNET_SERVER_Client *client,
                              const struct GNUNET_MessageHeader *msg)
 {
-  const struct MessageGetFragmentRequest *req =
-    (const struct MessageGetFragmentRequest *) msg;
-
-  struct SendClosure sc = { .op_id = req->op_id, .client = client };
+  const struct MessageGetFragmentRequest *
+    req = (const struct MessageGetFragmentRequest *) msg;
+  struct SendClosure
+    sc = { .op_id = req->op_id, .client = client,
+           .channel_key = req->channel_key, .slave_key = req->slave_key,
+           .membership_test = req->do_membership_test };
 
   int ret = db->message_get_fragment (db->cls, &req->channel_key,
                                       GNUNET_ntohll (req->message_id),
@@ -331,25 +433,27 @@ handle_message_get_fragment (void *cls,
                 _("Failed to get message fragment!\n"));
   }
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
 static void
-handle_counters_get_master (void *cls,
-                            struct GNUNET_SERVER_Client *client,
-                            const struct GNUNET_MessageHeader *msg)
+handle_counters_get (void *cls,
+                     struct GNUNET_SERVER_Client *client,
+                     const struct GNUNET_MessageHeader *msg)
 {
   const struct OperationRequest *req = (const struct OperationRequest *) msg;
-  struct MasterCountersResult res = { {0} };
+  struct CountersResult res = { {0} };
 
-  int ret = db->counters_get_master (db->cls, &req->channel_key,
-                                     &res.fragment_id, &res.message_id,
-                                     &res.group_generation);
+  int ret = db->counters_message_get (db->cls, &req->channel_key,
+                                      &res.max_fragment_id, &res.max_message_id,
+                                      &res.max_group_generation);
   switch (ret)
   {
-  case GNUNET_YES:
+  case GNUNET_OK:
+    ret = db->counters_state_get (db->cls, &req->channel_key,
+                                  &res.max_state_message_id);
   case GNUNET_NO:
     break;
   default:
@@ -357,50 +461,14 @@ handle_counters_get_master (void *cls,
                 _("Failed to get master counters!\n"));
   }
 
-  res.header.type
-    = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS_MASTER);
+  res.header.type = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS);
   res.header.size = htons (sizeof (res));
-  res.result_code = htonl (ret);
+  res.result_code = htonl (ret - INT32_MIN);
   res.op_id = req->op_id;
-  res.fragment_id = GNUNET_htonll (res.fragment_id);
-  res.message_id = GNUNET_htonll (res.message_id);
-  res.group_generation = GNUNET_htonll (res.group_generation);
-
-  GNUNET_SERVER_notification_context_add (nc, client);
-  GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
-                                              GNUNET_NO);
-
-  GNUNET_SERVER_receive_done (client, GNUNET_OK);
-}
-
-
-static void
-handle_counters_get_slave (void *cls,
-                           struct GNUNET_SERVER_Client *client,
-                           const struct GNUNET_MessageHeader *msg)
-{
-  const struct OperationRequest *req = (const struct OperationRequest *) msg;
-  struct SlaveCountersResult res = { {0} };
-
-  int ret = db->counters_get_slave (db->cls, &req->channel_key,
-                                    &res.max_known_msg_id);
-
-  switch (ret)
-  {
-  case GNUNET_YES:
-  case GNUNET_NO:
-    break;
-  default:
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Failed to get slave counters!\n"));
-  }
-
-  res.header.type
-    = htons (GNUNET_MESSAGE_TYPE_PSYCSTORE_RESULT_COUNTERS_SLAVE);
-  res.header.size = htons (sizeof (res));
-  res.result_code = htonl (ret);
-  res.op_id = req->op_id;
-  res.max_known_msg_id = GNUNET_htonll (res.max_known_msg_id);
+  res.max_fragment_id = GNUNET_htonll (res.max_fragment_id);
+  res.max_message_id = GNUNET_htonll (res.max_message_id);
+  res.max_group_generation = GNUNET_htonll (res.max_group_generation);
+  res.max_state_message_id = GNUNET_htonll (res.max_state_message_id);
 
   GNUNET_SERVER_notification_context_add (nc, client);
   GNUNET_SERVER_notification_context_unicast (nc, client, &res.header,
@@ -478,7 +546,7 @@ handle_state_modify (void *cls,
                     _("Failed to end modifying state!\n"));
     }
   }
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -532,7 +600,7 @@ handle_state_sync (void *cls,
                     _("Failed to end synchronizing state!\n"));
     }
   }
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -551,7 +619,7 @@ handle_state_reset (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to reset state!\n"));
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -570,7 +638,7 @@ handle_state_hash_update (void *cls,
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 _("Failed to reset state!\n"));
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -621,7 +689,7 @@ handle_state_get (void *cls,
                 _("Failed to get state variable!\n"));
   }
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -660,21 +728,20 @@ handle_state_get_prefix (void *cls,
                 _("Failed to get state variable!\n"));
   }
 
-  send_result_code (client, ret, req->op_id, NULL);
+  send_result_code (client, req->op_id, ret, NULL);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
 
 /**
- * Handle PSYCstore clients.
+ * Initialize the PSYCstore service.
  *
- * @param cls closure
- * @param server the initialized server
- * @param c configuration to use
+ * @param cls Closure.
+ * @param server The initialized server.
+ * @param c Configuration to use.
  */
 static void
-run (void *cls,
-     struct GNUNET_SERVER_Handle *server,
+run (void *cls, struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
@@ -701,12 +768,8 @@ run (void *cls,
       GNUNET_MESSAGE_TYPE_PSYCSTORE_MESSAGE_GET_FRAGMENT,
       sizeof (struct MessageGetFragmentRequest) },
 
-    { &handle_counters_get_master, NULL,
-      GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET_MASTER,
-      sizeof (struct OperationRequest) },
-
-    { &handle_counters_get_slave, NULL,
-      GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET_SLAVE,
+    { &handle_counters_get, NULL,
+      GNUNET_MESSAGE_TYPE_PSYCSTORE_COUNTERS_GET,
       sizeof (struct OperationRequest) },
 
     { &handle_state_modify, NULL,
@@ -766,7 +829,7 @@ run (void *cls,
 
 
 /**
- * The main function for the network size estimation service.
+ * The main function for the service.
  *
  * @param argc number of arguments from the command line
  * @param argv command line arguments