-keep track of messages passed to mq
[oweals/gnunet.git] / src / sensor / sensor_util_lib_crypto.c
index e097ed3ae226a87a927e9353faa32c44b4bebd97..0586879c76ab7216e0b24ca8335605e970678cbf 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C)
+     Copyright (C)
 
      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.
 */
 
 /**
@@ -65,7 +65,7 @@ struct GNUNET_SENSOR_crypto_pow_context
   /**
    * Task that calculates the proof-of-work
    */
-  GNUNET_SCHEDULER_TaskIdentifier calculate_pow_task;
+  struct GNUNET_SCHEDULER_Task * calculate_pow_task;
 
   /**
    * Size of msg (allocated after this struct)
@@ -128,7 +128,6 @@ check_pow (void *msg, size_t msg_size, uint64_t pow, int matching_bits)
   char buf[msg_size + sizeof (pow)] GNUNET_ALIGN;
   struct GNUNET_HashCode result;
 
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "Msg size: %" PRIu64 ".\n", msg_size);
   memcpy (buf, &pow, sizeof (pow));
   memcpy (&buf[sizeof (pow)], msg, msg_size);
   pow_hash (buf, sizeof (buf), &result);
@@ -149,13 +148,16 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   void *callback_cls;
   int sign_result;
 
+  if (0 == cx->pow % 1000)
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking pow %" PRIu64 ".\n", cx->pow);
   if (GNUNET_YES ==
       check_pow (&cx->timestamp,
                  sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
                  sizeof (struct GNUNET_TIME_Absolute) + cx->msg_size, cx->pow,
                  cx->matching_bits))
   {
-    cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK;
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Found pow %" PRIu64 ".\n", cx->pow);
+    cx->calculate_pow_task = NULL;
     result_block =
         GNUNET_malloc (sizeof (struct GNUNET_SENSOR_crypto_pow_block) +
                        cx->msg_size);
@@ -178,6 +180,7 @@ calculate_pow (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_SENSOR_crypto_pow_sign_cancel (cx);
     if (NULL != callback)
       callback (callback_cls, (GNUNET_OK == sign_result) ? result_block : NULL);
+    GNUNET_free (result_block);
     return;
   }
   cx->pow++;
@@ -194,10 +197,10 @@ void
 GNUNET_SENSOR_crypto_pow_sign_cancel (struct GNUNET_SENSOR_crypto_pow_context
                                       *cx)
 {
-  if (GNUNET_SCHEDULER_NO_TASK != cx->calculate_pow_task)
+  if (NULL != cx->calculate_pow_task)
   {
     GNUNET_SCHEDULER_cancel (cx->calculate_pow_task);
-    cx->calculate_pow_task = GNUNET_SCHEDULER_NO_TASK;
+    cx->calculate_pow_task = NULL;
   }
   GNUNET_free (cx);
   cx = NULL;
@@ -257,16 +260,14 @@ GNUNET_SENSOR_crypto_pow_sign (void *msg, size_t msg_size,
  * @param block The block received and needs to be verified
  * @param matching_bits Number of leading zeros in the hash used to verify pow
  * @param public_key Public key of the peer that sent this block
- * @param purpose Expected signing purpose
  * @param payload Where to store the pointer to the payload
- * @return Size of the payload
+ * @return Size of the payload, 0 on error
  */
 size_t
 GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block *
                                       block, int matching_bits,
                                       struct GNUNET_CRYPTO_EddsaPublicKey *
-                                      public_key, uint32_t purpose,
-                                      void **payload)
+                                      public_key, void **payload)
 {
   /* Check public key */
   if (0 !=
@@ -278,8 +279,9 @@ GNUNET_SENSOR_crypto_verify_pow_sign (struct GNUNET_SENSOR_crypto_pow_block *
   }
   /* Check signature */
   if (GNUNET_OK !=
-      GNUNET_CRYPTO_eddsa_verify (purpose, &block->purpose, &block->signature,
-                                  public_key))
+      GNUNET_CRYPTO_eddsa_verify
+      (GNUNET_SIGNATURE_PURPOSE_SENSOR_ANOMALY_REPORT, &block->purpose,
+       &block->signature, public_key))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING, "Invalid signature.\n");
     return 0;