- begin work on enhanced multipart receiving
[oweals/gnunet.git] / src / revocation / gnunet-service-revocation.c
index 8ab8f8ae19fe72a1a2b9f5140d89646b344b697f..f0b33a459e0bc19971dd521c2be6b9f239305c6b 100644 (file)
  * peers that connect.
  *
  * TODO:
- * - load revocations from disk
- * - store revocations to disk
- * - handle p2p revocations
- * - handle p2p connect (trigger SET union)
- * - handle client revoke message
- * - handle client query message
+ * - optimization: avoid sending revocation back to peer that we got it from;
+ * - optimization: have randomized delay in sending revocations to other peers
+ *                 to make it rare to traverse each link twice (NSE-style)
  */
 #include "platform.h"
 #include <math.h>
@@ -59,9 +56,9 @@ struct PeerEntry
 {
 
   /**
-   * Core handle for sending messages to this peer.
+   * Queue for sending messages to this peer.
    */
-  struct GNUNET_CORE_TransmitHandle *th;
+  struct GNUNET_MQ_Handle *mq;
 
   /**
    * What is the identity of the peer?
@@ -69,10 +66,15 @@ struct PeerEntry
   struct GNUNET_PeerIdentity id;
 
   /**
-   * Task scheduled to send message to this peer.
+   * Tasked used to trigger the set union operation.
    */
   GNUNET_SCHEDULER_TaskIdentifier transmit_task;
 
+  /**
+   * Handle to active set union operation (over revocation sets).
+   */
+  struct GNUNET_SET_OperationHandle *so;
+
 };
 
 
@@ -81,6 +83,12 @@ struct PeerEntry
  */
 static struct GNUNET_SET_Handle *revocation_set;
 
+/**
+ * Hash map with all revoked keys, maps the hash of the public key
+ * to the respective `struct RevokeMessage`.
+ */
+static struct GNUNET_CONTAINER_MultiHashMap *revocation_map;
+
 /**
  * Handle to our current configuration.
  */
@@ -94,12 +102,12 @@ static struct GNUNET_STATISTICS_Handle *stats;
 /**
  * Handle to the core service (for flooding)
  */
-static struct GNUNET_CORE_Handle *coreAPI;
+static struct GNUNET_CORE_Handle *core_api;
 
 /**
  * Map of all connected peers.
  */
-static struct GNUNET_CONTAINER_MultiHashMap *peers;
+static struct GNUNET_CONTAINER_MultiPeerMap *peers;
 
 /**
  * The peer identity of this peer.
@@ -111,11 +119,33 @@ static struct GNUNET_PeerIdentity my_identity;
  */
 static struct GNUNET_SERVER_Handle *srv;
 
+/**
+ * Notification context for convenient sending of replies to the clients.
+ */
+static struct GNUNET_SERVER_NotificationContext *nc;
+
+/**
+ * File handle for the revocation database.
+ */
+static struct GNUNET_DISK_FileHandle *revocation_db;
+
+/**
+ * Handle for us listening to incoming revocation set union requests.
+ */
+static struct GNUNET_SET_ListenHandle *revocation_union_listen_handle;
+
 /**
  * Amount of work required (W-bit collisions) for REVOCATION proofs, in collision-bits.
  */
 static unsigned long long revocation_work_required;
 
+/**
+ * Our application ID for set union operations.  Must be the
+ * same for all (compatible) peers.
+ */
+static struct GNUNET_HashCode revocation_set_union_app_id;
+
+
 
 /**
  * An revoke message has been received, check that it is well-formed.
@@ -125,22 +155,20 @@ static unsigned long long revocation_work_required;
  *         #GNUNET_NO if the key/signature don't verify
  */
 static int
-verify_revoke_message (const struct GNUNET_REVOCATION_RevokeMessage *rm)
+verify_revoke_message (const struct RevokeMessage *rm)
 {
   if (GNUNET_YES !=
       GNUNET_REVOCATION_check_pow (&rm->public_key,
                                   rm->proof_of_work,
                                   (unsigned int) revocation_work_required))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-               "Proof of work invalid: %llu!\n",
-                (unsigned long long)
-                GNUNET_ntohll (rm->proof_of_work));
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Proof of work invalid!\n");
     GNUNET_break_op (0);
     return GNUNET_NO;
   }
   if (GNUNET_OK !=
-      GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
+      GNUNET_CRYPTO_ecdsa_verify (GNUNET_SIGNATURE_PURPOSE_REVOCATION,
                                &rm->purpose,
                                &rm->signature,
                                &rm->public_key))
@@ -160,14 +188,142 @@ verify_revoke_message (const struct GNUNET_REVOCATION_RevokeMessage *rm)
  * @param message the message received
  */
 static void
-handle_query_message (void *cls, 
+handle_query_message (void *cls,
                      struct GNUNET_SERVER_Client *client,
                       const struct GNUNET_MessageHeader *message)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-             "Received QUERY message from client\n");
-  GNUNET_break (0);
-  GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+  const struct QueryMessage *qm = (const struct QueryMessage *) message;
+  struct QueryResponseMessage qrm;
+  struct GNUNET_HashCode hc;
+  int res;
+
+  GNUNET_CRYPTO_hash (&qm->key,
+                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
+                      &hc);
+  res = GNUNET_CONTAINER_multihashmap_contains (revocation_map, &hc);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              (GNUNET_NO == res)
+             ? "Received revocation check for valid key `%s' from client\n"
+              : "Received revocation check for revoked key `%s' from client\n",
+              GNUNET_h2s (&hc));
+  qrm.header.size = htons (sizeof (struct QueryResponseMessage));
+  qrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE);
+  qrm.is_valid = htonl ((GNUNET_YES == res) ? GNUNET_NO : GNUNET_YES);
+  GNUNET_SERVER_notification_context_add (nc,
+                                          client);
+  GNUNET_SERVER_notification_context_unicast (nc,
+                                              client,
+                                              &qrm.header,
+                                              GNUNET_NO);
+  GNUNET_SERVER_receive_done (client, GNUNET_OK);
+}
+
+
+/**
+ * Flood the given revocation message to all neighbours.
+ *
+ * @param cls the `struct RevokeMessage` to flood
+ * @param target a neighbour
+ * @param value our `struct PeerEntry` for the neighbour
+ * @return #GNUNET_OK (continue to iterate)
+ */
+static int
+do_flood (void *cls,
+          const struct GNUNET_PeerIdentity *target,
+          void *value)
+{
+  const struct RevokeMessage *rm = cls;
+  struct PeerEntry *pe = value;
+  struct GNUNET_MQ_Envelope *e;
+  struct RevokeMessage *cp;
+
+  e = GNUNET_MQ_msg (cp, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE);
+  *cp = *rm;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Flooding revocation to `%s'\n", GNUNET_i2s (target));
+  GNUNET_MQ_send (pe->mq, e);
+  return GNUNET_OK;
+}
+
+
+/**
+ * Publicize revocation message.   Stores the message locally in the
+ * database and passes it to all connected neighbours (and adds it to
+ * the set for future connections).
+ *
+ * @param rm message to publicize
+ * @return #GNUNET_OK on success, #GNUNET_NO if we encountered an error,
+ *         #GNUNET_SYSERR if the message was malformed
+ */
+static int
+publicize_rm (const struct RevokeMessage *rm)
+{
+  struct RevokeMessage *cp;
+  struct GNUNET_HashCode hc;
+  struct GNUNET_SET_Element e;
+
+  GNUNET_CRYPTO_hash (&rm->public_key,
+                      sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
+                      &hc);
+  if (GNUNET_YES ==
+      GNUNET_CONTAINER_multihashmap_contains (revocation_map,
+                                              &hc))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                _("Duplicate revocation received from peer. Ignored.\n"));
+    return GNUNET_OK;
+  }
+  if (GNUNET_OK !=
+      verify_revoke_message (rm))
+  {
+    GNUNET_break_op (0);
+    return GNUNET_SYSERR;
+  }
+  /* write to disk */
+  if (sizeof (struct RevokeMessage) !=
+      GNUNET_DISK_file_write (revocation_db,
+                              rm,
+                              sizeof (struct RevokeMessage)))
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                         "write");
+    return GNUNET_NO;
+  }
+  if (GNUNET_OK !=
+      GNUNET_DISK_file_sync (revocation_db))
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                         "sync");
+    return GNUNET_NO;
+  }
+  /* keep copy in memory */
+  cp = (struct RevokeMessage *) GNUNET_copy_message (&rm->header);
+  GNUNET_break (GNUNET_OK ==
+                GNUNET_CONTAINER_multihashmap_put (revocation_map,
+                                                   &hc,
+                                                   cp,
+                                                   GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+  /* add to set for future connections */
+  e.size = htons (rm->header.size);
+  e.type = 0;
+  e.data = rm;
+  if (GNUNET_OK !=
+      GNUNET_SET_add_element (revocation_set,
+                              &e,
+                              NULL, NULL))
+  {
+    GNUNET_break (0);
+    return GNUNET_OK;
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Added revocation info to SET\n");
+  }
+  /* flood to neighbours */
+  GNUNET_CONTAINER_multipeermap_iterate (peers,
+                                        &do_flood,
+                                         cp);
+  return GNUNET_OK;
 }
 
 
@@ -179,22 +335,32 @@ handle_query_message (void *cls,
  * @param message the message received
  */
 static void
-handle_revoke_message (void *cls, 
-                     struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
+handle_revoke_message (void *cls,
+                       struct GNUNET_SERVER_Client *client,
+                       const struct GNUNET_MessageHeader *message)
 {
-  const struct GNUNET_REVOCATION_RevokeMessage *rm;
+  const struct RevokeMessage *rm;
+  struct RevocationResponseMessage rrm;
+  int ret;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received REVOKE message from client\n");
-  rm = (const struct GNUNET_REVOCATION_RevokeMessage *) message;
-  if (GNUNET_OK != 
-      verify_revoke_message (rm))
+  rm = (const struct RevokeMessage *) message;
+  if (GNUNET_SYSERR == (ret = publicize_rm (rm)))
   {
-    GNUNET_break (0);
+    GNUNET_break_op (0);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
+    return;
   }
-  GNUNET_break (0); // FIXME: TBD
+  rrm.header.size = htons (sizeof (struct RevocationResponseMessage));
+  rrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE_RESPONSE);
+  rrm.is_valid = htonl ((GNUNET_OK == ret) ? GNUNET_NO : GNUNET_YES);
+  GNUNET_SERVER_notification_context_add (nc,
+                                          client);
+  GNUNET_SERVER_notification_context_unicast (nc,
+                                              client,
+                                              &rrm.header,
+                                              GNUNET_NO);
   GNUNET_SERVER_receive_done (client, GNUNET_OK);
 }
 
@@ -207,31 +373,120 @@ handle_revoke_message (void *cls,
  * @param peer peer identity this message is from (ignored)
  */
 static int
-handle_p2p_revoke_message (void *cls, 
+handle_p2p_revoke_message (void *cls,
                           const struct GNUNET_PeerIdentity *peer,
                           const struct GNUNET_MessageHeader *message)
 {
-  const struct GNUNET_REVOCATION_RevokeMessage *rm;
-
+  const struct RevokeMessage *rm;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received REVOKE message from peer\n");
-  rm = (const struct GNUNET_REVOCATION_RevokeMessage *) message;
-  if (GNUNET_OK != 
-      verify_revoke_message (rm))
+  rm = (const struct RevokeMessage *) message;
+  GNUNET_break_op (GNUNET_SYSERR != publicize_rm (rm));
+  return GNUNET_OK;
+}
+
+
+
+/**
+ * Callback for set operation results. Called for each element in the
+ * result set.  Each element contains a revocation, which we should
+ * validate and then add to our revocation list (and set).
+ *
+ * @param cls closure
+ * @param element a result element, only valid if status is #GNUNET_SET_STATUS_OK
+ * @param status see `enum GNUNET_SET_Status`
+ */
+static void
+add_revocation (void *cls,
+                const struct GNUNET_SET_Element *element,
+                enum GNUNET_SET_Status status)
+{
+  struct PeerEntry *peer_entry = cls;
+  const struct RevokeMessage *rm;
+
+  switch (status)
   {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  GNUNET_break (0); // FIXME: TBD
+  case GNUNET_SET_STATUS_OK:
+    if (element->size != sizeof (struct RevokeMessage))
+    {
+      GNUNET_break_op (0);
+      return;
+    }
+    if (0 != element->type)
+    {
+      GNUNET_STATISTICS_update (stats,
+                                "# unsupported revocations received via set union",
+                                1, GNUNET_NO);
+      return;
+    }
+    rm = element->data;
+    (void) handle_p2p_revoke_message (NULL,
+                                      &peer_entry->id,
+                                      &rm->header);
+    GNUNET_STATISTICS_update (stats,
+                              "# revocation messages received via set union",
+                              1, GNUNET_NO);
+    break;
+  case GNUNET_SET_STATUS_TIMEOUT:
+  case GNUNET_SET_STATUS_FAILURE:
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                _("Error computing revocation set union with %s\n"),
+                GNUNET_i2s (&peer_entry->id));
+    peer_entry->so = NULL;
+    GNUNET_STATISTICS_update (stats, "# revocation set unions failed",
+                              1, GNUNET_NO);
+    break;
+  case GNUNET_SET_STATUS_HALF_DONE:
+    break;
+  case GNUNET_SET_STATUS_DONE:
+    peer_entry->so = NULL;
+    GNUNET_STATISTICS_update (stats, "# revocation set unions completed",
+                              1, GNUNET_NO);
+    break;
+  default:
+    GNUNET_break (0);
+    break;
+ }
+}
 
-#if 0
-  /* flood to rest */
-  GNUNET_CONTAINER_multihashmap_iterate (peers, 
-                                        &do_flood,
-                                         &ctx);
-#endif
-  return GNUNET_OK;
+
+/**
+ * The timeout for performing the set union has expired,
+ * run the set operation on the revocation certificates.
+ *
+ * @param cls NULL
+ * @param tc scheduler context (unused)
+ */
+static void
+transmit_task_cb (void *cls,
+                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct PeerEntry *peer_entry = cls;
+  uint16_t salt;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Starting set exchange with peer `%s'\n",
+              GNUNET_i2s (&peer_entry->id));
+
+  salt = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
+                                              UINT16_MAX);
+  peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
+  peer_entry->so = GNUNET_SET_prepare (&peer_entry->id,
+                                       &revocation_set_union_app_id,
+                                       NULL, salt,
+                                       GNUNET_SET_RESULT_ADDED,
+                                       &add_revocation,
+                                       peer_entry);
+  if (GNUNET_OK !=
+      GNUNET_SET_commit (peer_entry->so,
+                         revocation_set))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                _("SET service crashed, terminating revocation service\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
 }
 
 
@@ -247,20 +502,44 @@ handle_core_connect (void *cls,
                     const struct GNUNET_PeerIdentity *peer)
 {
   struct PeerEntry *peer_entry;
+  struct GNUNET_HashCode my_hash;
+  struct GNUNET_HashCode peer_hash;
+
+  if (0 == memcmp(peer, &my_identity, sizeof (my_identity)))
+      return;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected to us\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Peer `%s' connected to us\n",
               GNUNET_i2s (peer));
-  peer_entry = GNUNET_new (struct PeerEntry);
-  peer_entry->id = *peer;
-  GNUNET_assert (GNUNET_OK ==
-                 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey,
-                                                    peer_entry,
-                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-#if 0
-  peer_entry->transmit_task =
-      GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), &transmit_task_cb,
+  peer_entry = GNUNET_CONTAINER_multipeermap_get (peers,
+                                                  peer);
+  if (NULL == peer_entry)
+  {
+    peer_entry = GNUNET_new (struct PeerEntry);
+    peer_entry->id = *peer;
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_CONTAINER_multipeermap_put (peers, peer,
+                                                      peer_entry,
+                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+  }
+  else
+  {
+    GNUNET_assert (NULL == peer_entry->mq);
+  }
+  peer_entry->mq = GNUNET_CORE_mq_create (core_api, peer);
+  GNUNET_CRYPTO_hash (&my_identity, sizeof (my_identity), &my_hash);
+  GNUNET_CRYPTO_hash (peer, sizeof (*peer), &peer_hash);
+  if (0 < GNUNET_CRYPTO_hash_cmp (&my_hash,
+                                  &peer_hash))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Starting SET operation with peer `%s'\n",
+                GNUNET_i2s (peer));
+    peer_entry->transmit_task =
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
+                                    &transmit_task_cb,
                                     peer_entry);
-#endif
+  }
   GNUNET_STATISTICS_update (stats, "# peers connected", 1, GNUNET_NO);
 }
 
@@ -273,40 +552,60 @@ handle_core_connect (void *cls,
  * @param peer peer identity this notification is about
  */
 static void
-handle_core_disconnect (void *cls, 
+handle_core_disconnect (void *cls,
                        const struct GNUNET_PeerIdentity *peer)
 {
   struct PeerEntry *pos;
 
+  if (0 == memcmp(peer, &my_identity, sizeof (my_identity)))
+      return;
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Peer `%s' disconnected from us\n",
               GNUNET_i2s (peer));
-  pos = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
+  pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
   if (NULL == pos)
   {
     GNUNET_break (0);
     return;
   }
   GNUNET_assert (GNUNET_YES ==
-                 GNUNET_CONTAINER_multihashmap_remove (peers, &peer->hashPubKey,
+                 GNUNET_CONTAINER_multipeermap_remove (peers, peer,
                                                        pos));
-#if 0
-  if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK) 
+  GNUNET_MQ_destroy (pos->mq);
+  if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK)
   {
     GNUNET_SCHEDULER_cancel (pos->transmit_task);
     pos->transmit_task = GNUNET_SCHEDULER_NO_TASK;
   }
-  if (NULL != pos->th)
+  if (NULL != pos->so)
   {
-    GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
-    pos->th = NULL;
+    GNUNET_SET_operation_cancel (pos->so);
+    pos->so = NULL;
   }
-#endif
   GNUNET_free (pos);
   GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
 }
 
 
+/**
+ * Free all values in a hash map.
+ *
+ * @param cls NULL
+ * @param key the key
+ * @param value value to free
+ * @return #GNUNET_OK (continue to iterate)
+ */
+static int
+free_entry (void *cls,
+            const struct GNUNET_HashCode *key,
+            void *value)
+{
+  GNUNET_free (value);
+  return GNUNET_OK;
+}
+
+
 /**
  * Task run during shutdown.
  *
@@ -322,10 +621,15 @@ shutdown_task (void *cls,
     GNUNET_SET_destroy (revocation_set);
     revocation_set = NULL;
   }
-  if (NULL != coreAPI)
+  if (NULL != revocation_union_listen_handle)
+  {
+    GNUNET_SET_listen_cancel (revocation_union_listen_handle);
+    revocation_union_listen_handle = NULL;
+  }
+  if (NULL != core_api)
   {
-    GNUNET_CORE_disconnect (coreAPI);
-    coreAPI = NULL;
+    GNUNET_CORE_disconnect (core_api);
+    core_api = NULL;
   }
   if (NULL != stats)
   {
@@ -334,9 +638,23 @@ shutdown_task (void *cls,
   }
   if (NULL != peers)
   {
-    GNUNET_CONTAINER_multihashmap_destroy (peers);
+    GNUNET_CONTAINER_multipeermap_destroy (peers);
     peers = NULL;
   }
+  if (NULL != nc)
+  {
+    GNUNET_SERVER_notification_context_destroy (nc);
+    nc = NULL;
+  }
+  if (NULL != revocation_db)
+  {
+    GNUNET_DISK_file_close (revocation_db);
+    revocation_db = NULL;
+  }
+  GNUNET_CONTAINER_multihashmap_iterate (revocation_map,
+                                         &free_entry,
+                                         NULL);
+  GNUNET_CONTAINER_multihashmap_destroy (revocation_map);
 }
 
 
@@ -347,12 +665,12 @@ shutdown_task (void *cls,
  * @param identity the public identity of this peer
  */
 static void
-core_init (void *cls, 
+core_init (void *cls,
            const struct GNUNET_PeerIdentity *identity)
 {
   if (NULL == identity)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Connection to core FAILED!\n");
     GNUNET_SCHEDULER_shutdown ();
     return;
@@ -361,6 +679,52 @@ core_init (void *cls,
 }
 
 
+/**
+ * Called when another peer wants to do a set operation with the
+ * local peer. If a listen error occurs, the 'request' is NULL.
+ *
+ * @param cls closure
+ * @param other_peer the other peer
+ * @param context_msg message with application specific information from
+ *        the other peer
+ * @param request request from the other peer (never NULL), use GNUNET_SET_accept()
+ *        to accept it, otherwise the request will be refused
+ *        Note that we can't just return value from the listen callback,
+ *        as it is also necessary to specify the set we want to do the
+ *        operation with, whith sometimes can be derived from the context
+ *        message. It's necessary to specify the timeout.
+ */
+static void
+handle_revocation_union_request (void *cls,
+                                 const struct GNUNET_PeerIdentity *other_peer,
+                                 const struct GNUNET_MessageHeader *context_msg,
+                                 struct GNUNET_SET_Request *request)
+{
+  struct PeerEntry *peer_entry;
+
+  if (NULL == request)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  peer_entry = GNUNET_CONTAINER_multipeermap_get (peers,
+                                                  other_peer);
+  if (NULL == peer_entry)
+  {
+    peer_entry = GNUNET_new (struct PeerEntry);
+    peer_entry->id = *other_peer;
+    GNUNET_assert (GNUNET_OK ==
+                   GNUNET_CONTAINER_multipeermap_put (peers, other_peer,
+                                                      peer_entry,
+                                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+  }
+  peer_entry->so = GNUNET_SET_accept (request,
+                                      GNUNET_SET_RESULT_ADDED,
+                                      &add_revocation,
+                                      peer_entry);
+}
+
+
 /**
  * Handle network size estimate clients.
  *
@@ -369,25 +733,43 @@ core_init (void *cls,
  * @param c configuration to use
  */
 static void
-run (void *cls, 
+run (void *cls,
      struct GNUNET_SERVER_Handle *server,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   static const struct GNUNET_SERVER_MessageHandler handlers[] = {
     {&handle_query_message, NULL, GNUNET_MESSAGE_TYPE_REVOCATION_QUERY,
-     sizeof (struct GNUNET_REVOCATION_QueryMessage)},
+     sizeof (struct QueryMessage)},
     {&handle_revoke_message, NULL, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
-     sizeof (struct GNUNET_REVOCATION_RevokeMessage)},
+     sizeof (struct RevokeMessage)},
     {NULL, NULL, 0, 0}
   };
   static const struct GNUNET_CORE_MessageHandler core_handlers[] = {
     {&handle_p2p_revoke_message, GNUNET_MESSAGE_TYPE_REVOCATION_REVOKE,
-     sizeof (struct GNUNET_REVOCATION_RevokeMessage)},
+     sizeof (struct RevokeMessage)},
     {NULL, 0, 0}
   };
+  char *fn;
+  uint64_t left;
+  struct RevokeMessage *rm;
+  struct GNUNET_HashCode hc;
 
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_filename (c,
+                                               "REVOCATION",
+                                               "DATABASE",
+                                               &fn))
+  {
+    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
+                               "REVOCATION",
+                               "DATABASE");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
   cfg = c;
-  srv = server;  
+  srv = server;
+  revocation_map = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO);
+  nc = GNUNET_SERVER_notification_context_create (server, 1);
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (cfg, "REVOCATION", "WORKBITS",
                                             &revocation_work_required))
@@ -396,6 +778,7 @@ run (void *cls,
                               "REVOCATION",
                               "WORKBITS");
     GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (fn);
     return;
   }
   if (revocation_work_required >= sizeof (struct GNUNET_HashCode) * 8)
@@ -405,16 +788,69 @@ run (void *cls,
                               "WORKBITS",
                               _("Value is too large.\n"));
     GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (fn);
     return;
   }
   revocation_set = GNUNET_SET_create (cfg,
                                      GNUNET_SET_OPERATION_UNION);
+  revocation_union_listen_handle = GNUNET_SET_listen (cfg,
+                                                      GNUNET_SET_OPERATION_UNION,
+                                                      &revocation_set_union_app_id,
+                                                      &handle_revocation_union_request,
+                                                      NULL);
+  revocation_db = GNUNET_DISK_file_open (fn,
+                                         GNUNET_DISK_OPEN_READWRITE |
+                                         GNUNET_DISK_OPEN_CREATE,
+                                         GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE |
+                                         GNUNET_DISK_PERM_GROUP_READ |
+                                         GNUNET_DISK_PERM_OTHER_READ);
+  if (NULL == revocation_db)
+  {
+    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                              "REVOCATION",
+                              "DATABASE",
+                               _("Could not open revocation database file!"));
+    GNUNET_SCHEDULER_shutdown ();
+    GNUNET_free (fn);
+    return;
+  }
+  if (GNUNET_OK !=
+      GNUNET_DISK_file_size (fn, &left, GNUNET_YES, GNUNET_YES))
+    left = 0;
+  while (left > sizeof (struct RevokeMessage))
+  {
+    rm = GNUNET_new (struct RevokeMessage);
+    if (sizeof (struct RevokeMessage) !=
+        GNUNET_DISK_file_read (revocation_db,
+                               rm,
+                               sizeof (struct RevokeMessage)))
+    {
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
+                                "read",
+                                fn);
+      GNUNET_free (rm);
+      GNUNET_SCHEDULER_shutdown ();
+      GNUNET_free (fn);
+      return;
+    }
+    GNUNET_break (0 == ntohl (rm->reserved));
+    GNUNET_CRYPTO_hash (&rm->public_key,
+                        sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
+                        &hc);
+    GNUNET_break (GNUNET_OK ==
+                  GNUNET_CONTAINER_multihashmap_put (revocation_map,
+                                                     &hc,
+                                                     rm,
+                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+  }
+  GNUNET_free (fn);
+
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
                                 NULL);
-  peers = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
+  peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
   GNUNET_SERVER_add_handlers (srv, handlers);
    /* Connect to core service and register core handlers */
-  coreAPI = GNUNET_CORE_connect (cfg,   /* Main configuration */
+  core_api = GNUNET_CORE_connect (cfg,   /* Main configuration */
                                  NULL,       /* Closure passed to functions */
                                  &core_init,    /* Call core_init once connected */
                                  &handle_core_connect,  /* Handle connects */
@@ -424,7 +860,7 @@ run (void *cls,
                                  NULL,  /* Don't want notified about all outbound messages */
                                  GNUNET_NO,     /* For header only outbound notification */
                                  core_handlers);        /* Register these handlers */
-  if (NULL == coreAPI)
+  if (NULL == core_api)
   {
     GNUNET_SCHEDULER_shutdown ();
     return;
@@ -441,9 +877,12 @@ run (void *cls,
  * @return 0 ok, 1 on error
  */
 int
-main (int argc, 
+main (int argc,
       char *const *argv)
 {
+  GNUNET_CRYPTO_hash ("revocation-set-union-application-id",
+                      strlen ("revocation-set-union-application-id"),
+                      &revocation_set_union_app_id);
   return (GNUNET_OK ==
           GNUNET_SERVICE_run (argc, argv, "revocation", GNUNET_SERVICE_OPTION_NONE,
                               &run, NULL)) ? 0 : 1;
@@ -456,7 +895,7 @@ main (int argc,
 /**
  * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  */
-void __attribute__ ((constructor)) 
+void __attribute__ ((constructor))
 GNUNET_ARM_memory_init ()
 {
   mallopt (M_TRIM_THRESHOLD, 4 * 1024);