another core API simplification due to ATS introduction
[oweals/gnunet.git] / src / nse / gnunet-service-nse.c
index 5f880088fbc9353f445487f02099fcedf95ff4f1..7000ec602da6289bf7d6ebe6a18c7c3723181d30 100644 (file)
@@ -1,17 +1,17 @@
 /*
   This file is part of GNUnet.
   (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
-  
+
   GNUnet is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published
   by the Free Software Foundation; either version 3, or (at your
   option) any later version.
-  
+
   GNUnet is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
-  
+
   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,
@@ -36,6 +36,7 @@
  * this can calculate the expected number of peers in the network.
  */
 #include "platform.h"
+#include <math.h>
 #include "gnunet_util_lib.h"
 #include "gnunet_constants.h"
 #include "gnunet_protocols.h"
  */
 #define NSE_PRIORITY 5
 
+#if FREEBSD
+#define log2(a) (log(a)/log(2))
+#endif
+
 /**
  * Amount of work required (W-bit collisions) for NSE proofs, in collision-bits.
  */
@@ -148,7 +153,7 @@ struct GNUNET_NSE_FloodMessage
   /**
    * Number of hops this message has taken so far.
    */
-  uint32_t hop_count;
+  uint32_t hop_count GNUNET_PACKED;
 
   /**
    * Purpose.
@@ -166,7 +171,7 @@ struct GNUNET_NSE_FloodMessage
    * of timestamp and the initiator's public
    * key.
    */
-  uint32_t matching_bits;
+  uint32_t matching_bits GNUNET_PACKED;
 
   /**
    * Public key of the originator.
@@ -176,7 +181,7 @@ struct GNUNET_NSE_FloodMessage
   /**
    * Proof of work, causing leading zeros when hashed with pkey.
    */
-  uint64_t proof_of_work;
+  uint64_t proof_of_work GNUNET_PACKED;
 
   /**
    * Signature (over range specified in purpose).
@@ -207,7 +212,7 @@ static struct GNUNET_CONTAINER_MultiHashMap *peers;
 
 /**
  * The current network size estimate.  Number of bits matching on
- * average thus far. 
+ * average thus far.
  */
 static double current_size_estimate;
 
@@ -308,6 +313,7 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
   double q;
   double r;
   double temp;
+  double nsize;
 
   /* Weighted incremental algorithm for stddev according to West (1979) */
   mean = 0.0;
@@ -333,12 +339,15 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
   std_dev = sqrt (variance);
   current_std_dev = std_dev;
   current_size_estimate = mean;
-
+  
   em->header.size = htons (sizeof (struct GNUNET_NSE_ClientMessage));
   em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
   em->reserved = htonl (0);
   em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
-  em->size_estimate = mean - 1.0 / 3.0;
+  em->size_estimate = mean - 0.332747;
+  nsize = log2 (GNUNET_CONTAINER_multihashmap_size (peers) + 1);
+  if (em->size_estimate < nsize)
+    em->size_estimate = nsize;
   em->std_deviation = std_dev;
   GNUNET_STATISTICS_set (stats, "# nodes in the network (estimate)",
                          (uint64_t) pow (2, mean - 1.0 / 3.0), GNUNET_NO);
@@ -396,7 +405,7 @@ get_matching_bits_delay (uint32_t matching_bits)
  * What delay randomization should we apply for a given number of matching bits?
  *
  * @param matching_bits number of matching bits
- * @return random delay to apply 
+ * @return random delay to apply
  */
 static struct GNUNET_TIME_Relative
 get_delay_randomization (uint32_t matching_bits)
@@ -439,7 +448,7 @@ get_matching_bits (struct GNUNET_TIME_Absolute timestamp,
 
 
 /**
- * Get the transmission delay that should be applied for a 
+ * Get the transmission delay that should be applied for a
  * particular round.
  *
  * @param round_offset -1 for the previous round (random delay between 0 and 50ms)
@@ -498,8 +507,8 @@ get_transmit_delay (int round_offset)
  * @param cls the 'struct NSEPeerEntry'
  * @param tc scheduler context
  */
-static void transmit_task (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc);
+static void
+transmit_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
 
 
 /**
@@ -545,8 +554,9 @@ transmit_ready (void *cls, size_t size, void *buf)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "In round %llu, sending to `%s' estimate with %u bits\n",
               (unsigned long long)
-              GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].timestamp).
-              abs_value, GNUNET_i2s (&peer_entry->id),
+              GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].
+                                         timestamp).abs_value,
+              GNUNET_i2s (&peer_entry->id),
               (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
 #endif
   if (ntohl (size_estimate_messages[idx].hop_count) == 0)
@@ -626,7 +636,8 @@ setup_flood_message (unsigned int slot, struct GNUNET_TIME_Absolute ts)
   fm->timestamp = GNUNET_TIME_absolute_hton (ts);
   fm->pkey = my_public_key;
   fm->proof_of_work = my_proof;
-  GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose, &fm->signature);
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_CRYPTO_rsa_sign (my_private_key, &fm->purpose, &fm->signature));
 }
 
 
@@ -814,9 +825,10 @@ find_proof (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         if (ntohl (size_estimate_messages[i].hop_count) == 0)
         {
           size_estimate_messages[i].proof_of_work = my_proof;
-          GNUNET_CRYPTO_rsa_sign (my_private_key,
-                                  &size_estimate_messages[i].purpose,
-                                  &size_estimate_messages[i].signature);
+          GNUNET_assert (GNUNET_OK ==
+                        GNUNET_CRYPTO_rsa_sign (my_private_key,
+                                                &size_estimate_messages[i].purpose,
+                                                &size_estimate_messages[i].signature));
         }
       write_proof ();
       return;
@@ -861,8 +873,8 @@ verify_message_crypto (const struct GNUNET_NSE_FloodMessage *incoming_flood)
                            incoming_flood->proof_of_work))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Proof of work invalid: %llu!\n"),
-                (unsigned long long) GNUNET_ntohll (incoming_flood->
-                                                    proof_of_work));
+                (unsigned long long)
+                GNUNET_ntohll (incoming_flood->proof_of_work));
     GNUNET_break_op (0);
     return GNUNET_NO;
   }
@@ -901,7 +913,7 @@ update_flood_times (void *cls, const GNUNET_HashCode * key, void *value)
     return GNUNET_OK;           /* trigger of the update */
   if (peer_entry->previous_round == GNUNET_NO)
   {
-    /* still stuck in previous round, no point to update, check that 
+    /* still stuck in previous round, no point to update, check that
      * we are active here though... */
     GNUNET_break ((peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK) ||
                   (peer_entry->th != NULL));
@@ -959,9 +971,9 @@ handle_p2p_size_estimate (void *cls, const struct GNUNET_PeerIdentity *peer,
     GNUNET_snprintf (pred, sizeof (pred), "%s", GNUNET_i2s (peer));
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Flood at %llu from `%s' via `%s' at `%s' with bits %u\n",
-                (unsigned long long) GNUNET_TIME_absolute_ntoh (incoming_flood->
-                                                                timestamp).
-                abs_value, origin, pred, GNUNET_i2s (&my_identity),
+                (unsigned long long)
+                GNUNET_TIME_absolute_ntoh (incoming_flood->timestamp).abs_value,
+                origin, pred, GNUNET_i2s (&my_identity),
                 (unsigned int) matching_bits);
   }
 #endif
@@ -1199,12 +1211,10 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cls service closure
  * @param server handle to the server for this service
  * @param identity the public identity of this peer
- * @param publicKey the public key of this peer
  */
 static void
 core_init (void *cls, struct GNUNET_CORE_Handle *server,
-           const struct GNUNET_PeerIdentity *identity,
-           const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
+           const struct GNUNET_PeerIdentity *identity)
 {
   struct GNUNET_TIME_Absolute now;
   struct GNUNET_TIME_Absolute prev_time;
@@ -1350,7 +1360,6 @@ run (void *cls, struct GNUNET_SERVER_Handle *server,
                                  &core_init,    /* Call core_init once connected */
                                  &handle_core_connect,  /* Handle connects */
                                  &handle_core_disconnect,       /* Handle disconnects */
-                                 NULL,  /* Do we care about "status" updates? */
                                  NULL,  /* Don't want notified about all incoming messages */
                                  GNUNET_NO,     /* For header only inbound notification */
                                  NULL,  /* Don't want notified about all outbound messages */