-fix leak
[oweals/gnunet.git] / src / revocation / gnunet-service-revocation.c
index a410f7b4ef50f49cd2d42235369f826ee05046b6..c6e3152bab47c8c803eca71f9dfce4d272d0b765 100644 (file)
  * peers that connect.
  *
  * TODO:
- * - store revocations to disk
- * - handle p2p revocations
- * - handle p2p connect (trigger SET union)
- * - handle client revoke message
+ * - handle p2p connect (trigger SET union, #3057)
+ * - 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>
@@ -57,20 +57,15 @@ 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?
    */
   struct GNUNET_PeerIdentity id;
 
-  /**
-   * Task scheduled to send message to this peer.
-   */
-  GNUNET_SCHEDULER_TaskIdentifier transmit_task;
-
 };
 
 
@@ -82,7 +77,7 @@ 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;
 
 /**
@@ -98,7 +93,7 @@ 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.
@@ -146,10 +141,8 @@ verify_revoke_message (const struct RevokeMessage *rm)
                                   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;
   }
@@ -174,7 +167,7 @@ verify_revoke_message (const struct 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)
 {
@@ -187,21 +180,123 @@ handle_query_message (void *cls,
                       sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
                       &hc);
   res = GNUNET_CONTAINER_multihashmap_contains (revocation_map, &hc);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-              (GNUNET_NO == res) 
+  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 RevocationResponseMessage));
+  qrm.header.size = htons (sizeof (struct QueryResponseMessage));
   qrm.header.type = htons (GNUNET_MESSAGE_TYPE_REVOCATION_QUERY_RESPONSE);
-  qrm.is_valid = htons ((GNUNET_YES == res) ? GNUNET_NO : GNUNET_YES);
-  GNUNET_SERVER_notification_context_add (nc, 
+  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_SYSERR);
+  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_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_EccPublicSignKey),
+                      &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;
+  }
+  /* flood to neighbours */
+  GNUNET_CONTAINER_multipeermap_iterate (peers,
+                                        &do_flood,
+                                         cp);
+  return GNUNET_OK;
 }
 
 
@@ -213,28 +308,27 @@ 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 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 RevokeMessage *) message;
-  if (GNUNET_OK != 
-      verify_revoke_message (rm))
+  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 = htons (GNUNET_NO);
-  GNUNET_SERVER_notification_context_add (nc, 
+  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,
@@ -252,30 +346,16 @@ 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 RevokeMessage *rm;
 
-
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received REVOKE message from peer\n");
   rm = (const struct RevokeMessage *) message;
-  if (GNUNET_OK != 
-      verify_revoke_message (rm))
-  {
-    GNUNET_break_op (0);
-    return GNUNET_SYSERR;
-  }
-  GNUNET_break (0); // FIXME: TBD
-
-#if 0
-  /* flood to rest */
-  GNUNET_CONTAINER_multipeermap_iterate (peers, 
-                                        &do_flood,
-                                         &ctx);
-#endif
+  GNUNET_break_op (GNUNET_SYSERR != publicize_rm (rm));
   return GNUNET_OK;
 }
 
@@ -297,10 +377,12 @@ handle_core_connect (void *cls,
               GNUNET_i2s (peer));
   peer_entry = GNUNET_new (struct PeerEntry);
   peer_entry->id = *peer;
+  peer_entry->mq = GNUNET_CORE_mq_create (core_api, peer);
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_CONTAINER_multipeermap_put (peers, peer,
                                                     peer_entry,
                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
+  // GNUNET_break (0); // FIXME: implement revocation set union on connect!
 #if 0
   peer_entry->transmit_task =
       GNUNET_SCHEDULER_add_delayed (get_transmit_delay (-1), &transmit_task_cb,
@@ -318,7 +400,7 @@ 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;
@@ -335,17 +417,13 @@ handle_core_disconnect (void *cls,
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multipeermap_remove (peers, peer,
                                                        pos));
+  GNUNET_MQ_destroy (pos->mq);
 #if 0
-  if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK) 
+  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)
-  {
-    GNUNET_CORE_notify_transmit_ready_cancel (pos->th);
-    pos->th = NULL;
-  }
 #endif
   GNUNET_free (pos);
   GNUNET_STATISTICS_update (stats, "# peers connected", -1, GNUNET_NO);
@@ -354,7 +432,7 @@ handle_core_disconnect (void *cls,
 
 /**
  * Free all values in a hash map.
- * 
+ *
  * @param cls NULL
  * @param key the key
  * @param value value to free
@@ -385,10 +463,10 @@ shutdown_task (void *cls,
     GNUNET_SET_destroy (revocation_set);
     revocation_set = NULL;
   }
-  if (NULL != coreAPI)
+  if (NULL != core_api)
   {
-    GNUNET_CORE_disconnect (coreAPI);
-    coreAPI = NULL;
+    GNUNET_CORE_disconnect (core_api);
+    core_api = NULL;
   }
   if (NULL != stats)
   {
@@ -424,12 +502,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;
@@ -446,7 +524,7 @@ 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)
 {
@@ -480,7 +558,7 @@ run (void *cls,
     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 !=
@@ -506,7 +584,7 @@ run (void *cls,
   }
   revocation_set = GNUNET_SET_create (cfg,
                                      GNUNET_SET_OPERATION_UNION);
-  
+
   revocation_db = GNUNET_DISK_file_open (fn,
                                          GNUNET_DISK_OPEN_READWRITE |
                                          GNUNET_DISK_OPEN_CREATE,
@@ -525,10 +603,10 @@ run (void *cls,
   }
   if (GNUNET_OK !=
       GNUNET_DISK_file_size (fn, &left, GNUNET_YES, GNUNET_YES))
-    left = 0;                         
+    left = 0;
   while (left > sizeof (struct RevokeMessage))
   {
-    rm = GNUNET_new (struct RevokeMessage);    
+    rm = GNUNET_new (struct RevokeMessage);
     if (sizeof (struct RevokeMessage) !=
         GNUNET_DISK_file_read (revocation_db,
                                rm,
@@ -550,16 +628,16 @@ run (void *cls,
                   GNUNET_CONTAINER_multihashmap_put (revocation_map,
                                                      &hc,
                                                      rm,
-                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));    
+                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
   }
   GNUNET_free (fn);
-  
+
   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
                                 NULL);
   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 */
@@ -569,7 +647,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;
@@ -586,7 +664,7 @@ run (void *cls,
  * @return 0 ok, 1 on error
  */
 int
-main (int argc, 
+main (int argc,
       char *const *argv)
 {
   return (GNUNET_OK ==
@@ -601,7 +679,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);