-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / nse / gnunet-service-nse.c
index 317750b70acccd21d1df958dcb23b9fe2dae9156..3db33020469879f73069729c33617f5d633ccec9 100644 (file)
@@ -1,6 +1,6 @@
 /*
   This file is part of GNUnet.
-  (C) 2009, 2010, 2011, 2012, 2013 Christian Grothoff (and other contributing authors)
+  Copyright (C) 2009, 2010, 2011, 2012, 2013 GNUnet e.V.
 
   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.
  */
 
 /**
@@ -130,7 +130,7 @@ struct NSEPeerEntry
   /**
    * Task scheduled to send message to this peer.
    */
-  GNUNET_SCHEDULER_TaskIdentifier transmit_task;
+  struct GNUNET_SCHEDULER_Task * transmit_task;
 
   /**
    * Did we receive or send a message about the previous round
@@ -275,12 +275,12 @@ static unsigned int estimate_count;
 /**
  * Task scheduled to update our flood message for the next round.
  */
-static GNUNET_SCHEDULER_TaskIdentifier flood_task;
+static struct GNUNET_SCHEDULER_Task * flood_task;
 
 /**
  * Task scheduled to compute our proof.
  */
-static GNUNET_SCHEDULER_TaskIdentifier proof_task;
+static struct GNUNET_SCHEDULER_Task * proof_task;
 
 /**
  * Notification context, simplifies client broadcasts.
@@ -593,11 +593,9 @@ get_transmit_delay (int round_offset)
  * Task that triggers a NSE P2P transmission.
  *
  * @param cls the `struct NSEPeerEntry *`
- * @param tc scheduler context
  */
 static void
-transmit_task_cb (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc);
+transmit_task_cb (void *cls);
 
 
 /**
@@ -635,7 +633,7 @@ transmit_ready (void *cls,
                                     peer_entry);
   }
   if ((0 == ntohl (size_estimate_messages[idx].hop_count)) &&
-      (GNUNET_SCHEDULER_NO_TASK != proof_task))
+      (NULL != proof_task))
   {
     GNUNET_STATISTICS_update (stats,
                               "# flood messages not generated (no proof yet)",
@@ -650,7 +648,7 @@ transmit_ready (void *cls,
     return 0;
   }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "In round s, sending to `%s' estimate with %u bits\n",
+              "In round %s, sending to `%s' estimate with %u bits\n",
               GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_ntoh (size_estimate_messages[idx].timestamp)),
               GNUNET_i2s (&peer_entry->id),
               (unsigned int) ntohl (size_estimate_messages[idx].matching_bits));
@@ -673,15 +671,13 @@ transmit_ready (void *cls,
  * Task that triggers a NSE P2P transmission.
  *
  * @param cls the `struct NSEPeerEntry *`
- * @param tc scheduler context
  */
 static void
-transmit_task_cb (void *cls,
-                 const struct GNUNET_SCHEDULER_TaskContext *tc)
+transmit_task_cb (void *cls)
 {
   struct NSEPeerEntry *peer_entry = cls;
 
-  peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
+  peer_entry->transmit_task = NULL;
 
   GNUNET_assert (NULL == peer_entry->th);
   peer_entry->th =
@@ -772,7 +768,7 @@ schedule_current_round (void *cls,
     peer_entry->previous_round = GNUNET_NO;
     return GNUNET_OK;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != peer_entry->transmit_task)
+  if (NULL != peer_entry->transmit_task)
   {
     GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
     peer_entry->previous_round = GNUNET_NO;
@@ -788,7 +784,9 @@ schedule_current_round (void *cls,
   delay =
       get_transmit_delay ((peer_entry->previous_round == GNUNET_NO) ? -1 : 0);
   peer_entry->transmit_task =
-      GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
+      GNUNET_SCHEDULER_add_delayed (delay,
+                                   &transmit_task_cb,
+                                   peer_entry);
   return GNUNET_OK;
 }
 
@@ -797,24 +795,21 @@ schedule_current_round (void *cls,
  * Update our flood message to be sent (and our timestamps).
  *
  * @param cls unused
- * @param tc context for this message
  */
 static void
-update_flood_message (void *cls,
-                     const struct GNUNET_SCHEDULER_TaskContext *tc)
+update_flood_message (void *cls)
 {
   struct GNUNET_TIME_Relative offset;
   unsigned int i;
 
-  flood_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
+  flood_task = NULL;
   offset = GNUNET_TIME_absolute_get_remaining (next_timestamp);
   if (0 != offset.rel_value_us)
   {
     /* somehow run early, delay more */
     flood_task =
-        GNUNET_SCHEDULER_add_delayed (offset, &update_flood_message, NULL);
+        GNUNET_SCHEDULER_add_delayed (offset,
+                                     &update_flood_message, NULL);
     return;
   }
   estimate_index = (estimate_index + 1) % HISTORY_SIZE;
@@ -845,7 +840,8 @@ update_flood_message (void *cls,
                                          NULL);
   flood_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (next_timestamp), &update_flood_message,
+                                    (next_timestamp),
+                                   &update_flood_message,
                                     NULL);
 }
 
@@ -918,11 +914,9 @@ write_proof ()
  * Find our proof of work.
  *
  * @param cls closure (unused)
- * @param tc task context
  */
 static void
-find_proof (void *cls,
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+find_proof (void *cls)
 {
 #define ROUND_SIZE 10
   uint64_t counter;
@@ -931,7 +925,7 @@ find_proof (void *cls,
   struct GNUNET_HashCode result;
   unsigned int i;
 
-  proof_task = GNUNET_SCHEDULER_NO_TASK;
+  proof_task = NULL;
   memcpy (&buf[sizeof (uint64_t)], &my_identity,
           sizeof (struct GNUNET_PeerIdentity));
   i = 0;
@@ -954,7 +948,8 @@ find_proof (void *cls,
   }
   if (my_proof / (100 * ROUND_SIZE) < counter / (100 * ROUND_SIZE))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Testing proofs currently at %llu\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Testing proofs currently at %llu\n",
                 (unsigned long long) counter);
     /* remember progress every 100 rounds */
     my_proof = counter;
@@ -1034,21 +1029,22 @@ update_flood_times (void *cls,
   {
     /* still stuck in previous round, no point to update, check that
      * we are active here though... */
-    if ( (GNUNET_SCHEDULER_NO_TASK == peer_entry->transmit_task) &&
+    if ( (NULL == peer_entry->transmit_task) &&
         (NULL == peer_entry->th) )
     {
         GNUNET_break (0);
     }
     return GNUNET_OK;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != peer_entry->transmit_task)
+  if (NULL != peer_entry->transmit_task)
   {
     GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
-    peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
+    peer_entry->transmit_task = NULL;
   }
   delay = get_transmit_delay (0);
   peer_entry->transmit_task =
-      GNUNET_SCHEDULER_add_delayed (delay, &transmit_task_cb, peer_entry);
+      GNUNET_SCHEDULER_add_delayed (delay,
+                                   &transmit_task_cb, peer_entry);
   return GNUNET_OK;
 }
 
@@ -1132,6 +1128,9 @@ handle_p2p_size_estimate (void *cls,
       return GNUNET_OK;         /* ignore, simply too early/late */
     if (GNUNET_YES != verify_message_crypto (incoming_flood))
     {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Peer %s is likely ill-configured!\n",
+                  GNUNET_i2s (peer));
       GNUNET_break_op (0);
       return GNUNET_OK;
     }
@@ -1169,10 +1168,10 @@ handle_p2p_size_estimate (void *cls,
     }
     /* got up-to-date information for current round, cancel transmission to
      * this peer altogether */
-    if (GNUNET_SCHEDULER_NO_TASK != peer_entry->transmit_task)
+    if (NULL != peer_entry->transmit_task)
     {
       GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
-      peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
+      peer_entry->transmit_task = NULL;
     }
     if (NULL != peer_entry->th)
     {
@@ -1189,7 +1188,7 @@ handle_p2p_size_estimate (void *cls,
     /* push back our result now, that peer is spreading bad information... */
     if (NULL == peer_entry->th)
     {
-      if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
+      if (peer_entry->transmit_task != NULL)
         GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
       peer_entry->transmit_task =
           GNUNET_SCHEDULER_add_now (&transmit_task_cb, peer_entry);
@@ -1214,10 +1213,10 @@ handle_p2p_size_estimate (void *cls,
   if (idx == estimate_index)
   {
       /* cancel any activity for current round */
-      if (peer_entry->transmit_task != GNUNET_SCHEDULER_NO_TASK)
+      if (peer_entry->transmit_task != NULL)
       {
         GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
-        peer_entry->transmit_task = GNUNET_SCHEDULER_NO_TASK;
+        peer_entry->transmit_task = NULL;
       }
       if (peer_entry->th != NULL)
       {
@@ -1303,9 +1302,9 @@ handle_core_disconnect (void *cls,
   GNUNET_assert (GNUNET_YES ==
                  GNUNET_CONTAINER_multipeermap_remove (peers, peer,
                                                        pos));
-  if (pos->transmit_task != GNUNET_SCHEDULER_NO_TASK) {
+  if (pos->transmit_task != NULL) {
     GNUNET_SCHEDULER_cancel (pos->transmit_task);
-    pos->transmit_task = GNUNET_SCHEDULER_NO_TASK;
+    pos->transmit_task = NULL;
   }
   if (NULL != pos->th)
   {
@@ -1339,21 +1338,19 @@ flush_comp_cb (void *cls,
  * Task run during shutdown.
  *
  * @param cls unused
- * @param tc unused
  */
 static void
-shutdown_task (void *cls,
-              const struct GNUNET_SCHEDULER_TaskContext *tc)
+shutdown_task (void *cls)
 {
-  if (GNUNET_SCHEDULER_NO_TASK != flood_task)
+  if (NULL != flood_task)
   {
     GNUNET_SCHEDULER_cancel (flood_task);
-    flood_task = GNUNET_SCHEDULER_NO_TASK;
+    flood_task = NULL;
   }
-  if (GNUNET_SCHEDULER_NO_TASK != proof_task)
+  if (NULL != proof_task)
   {
     GNUNET_SCHEDULER_cancel (proof_task);
-    proof_task = GNUNET_SCHEDULER_NO_TASK;
+    proof_task = NULL;
     write_proof ();             /* remember progress */
   }
   if (NULL != nc)
@@ -1389,9 +1386,9 @@ shutdown_task (void *cls,
   }
   if (NULL != lh)
   {
-    struct GNUNET_TIME_Relative timeout;
-    timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30);
-    GNUNET_TESTBED_LOGGER_flush (lh, timeout, &flush_comp_cb, NULL);
+    GNUNET_TESTBED_LOGGER_flush (lh,
+                                 &flush_comp_cb,
+                                 NULL);
   }
   if (NULL != histogram)
   {
@@ -1443,7 +1440,8 @@ core_init (void *cls,
   }
   flood_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (next_timestamp), &update_flood_message,
+                                    (next_timestamp),
+                                   &update_flood_message,
                                     NULL);
 }
 
@@ -1564,8 +1562,8 @@ run (void *cls,
   }
 #endif
 
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
-                                NULL);
+  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
+                                NULL);
   pk = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
   GNUNET_assert (NULL != pk);
   my_private_key = pk;
@@ -1629,7 +1627,7 @@ main (int argc,
 }
 
 
-#ifdef LINUX
+#if defined(LINUX) && defined(__GLIBC__)
 #include <malloc.h>
 
 /**