avoid failing hard if 'gnunetcheck' db does not exist
[oweals/gnunet.git] / src / nse / gnunet-service-nse.c
index 2b6780c088a6934ddcb8bc5dec81d2edc658f505..b1f70214c1b574d2e9e2b6fe5af286e57329864d 100644 (file)
@@ -2,20 +2,20 @@
   This file is part of GNUnet.
   Copyright (C) 2009, 2010, 2011, 2012, 2013, 2016 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
-  by the Free Software Foundation; either version 3, or (at your
-  option) any later version.
+  GNUnet is free software: you can redistribute it and/or modify it
+  under the terms of the GNU Affero General Public License as published
+  by the Free Software Foundation, either version 3 of the License,
+  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.
+  Affero General Public License for more details.
+  You should have received a copy of the GNU Affero General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-  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., 51 Franklin Street, Fifth Floor,
-  Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
  */
 
 /**
@@ -276,17 +276,17 @@ static unsigned int estimate_count;
 /**
  * Task scheduled to update our flood message for the next round.
  */
-static struct GNUNET_SCHEDULER_Task * flood_task;
+static struct GNUNET_SCHEDULER_Task *flood_task;
 
 /**
  * Task scheduled to compute our proof.
  */
-static struct GNUNET_SCHEDULER_Task * proof_task;
+static struct GNUNET_SCHEDULER_Task *proof_task;
 
 /**
  * Notification context, simplifies client broadcasts.
  */
-static struct GNUNET_SERVER_NotificationContext *nc;
+static struct GNUNET_NotificationContext *nc;
 
 /**
  * The next major time.
@@ -313,11 +313,6 @@ static struct GNUNET_PeerIdentity my_identity;
  */
 static uint64_t my_proof;
 
-/**
- * Handle to this serivce's server.
- */
-static struct GNUNET_SERVER_Handle *srv;
-
 
 /**
  * Initialize a message to clients with the current network
@@ -328,8 +323,6 @@ static struct GNUNET_SERVER_Handle *srv;
 static void
 setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
 {
-  unsigned int i;
-  unsigned int j;
   double mean;
   double sum;
   double std_dev;
@@ -350,9 +343,10 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
   sum = 0.0;
   sumweight = 0.0;
   variance = 0.0;
-  for (i = 0; i < estimate_count; i++)
+  for (unsigned int i = 0; i < estimate_count; i++)
   {
-    j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
+    unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
+
     val = htonl (size_estimate_messages[j].matching_bits);
     weight = estimate_count + 1 - i;
 
@@ -375,9 +369,10 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
   variance = 0.0;
   mean = 0.0;
 
-  for (i = 0; i < estimate_count; i++)
+  for (unsigned int i = 0; i < estimate_count; i++)
   {
-    j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
+    unsigned int j = (estimate_index - i + HISTORY_SIZE) % HISTORY_SIZE;
+
     val = htonl (size_estimate_messages[j].matching_bits);
     sum += val;
     vsq += val * val;
@@ -399,20 +394,22 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
   em->header.type = htons (GNUNET_MESSAGE_TYPE_NSE_ESTIMATE);
   em->reserved = htonl (0);
   em->timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
-  double se = mean - 0.332747;
-  j = GNUNET_CONTAINER_multipeermap_size (peers);
-  if (0 == j)
-    j = 1; /* Avoid log2(0); can only happen if CORE didn't report
-              connection to self yet */
-  nsize = log2 (j);
-  em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se,
-                                                      nsize));
-  em->std_deviation = GNUNET_hton_double (std_dev);
-  GNUNET_STATISTICS_set (stats,
-                         "# nodes in the network (estimate)",
-                         (uint64_t) pow (2, GNUNET_MAX (se,
-                                                        nsize)),
-                         GNUNET_NO);
+  {
+    double se = mean - 0.332747;
+    unsigned int j = GNUNET_CONTAINER_multipeermap_size (peers);
+    if (0 == j)
+      j = 1; /* Avoid log2(0); can only happen if CORE didn't report
+               connection to self yet */
+    nsize = log2 (j);
+    em->size_estimate = GNUNET_hton_double (GNUNET_MAX (se,
+                                                       nsize));
+    em->std_deviation = GNUNET_hton_double (std_dev);
+    GNUNET_STATISTICS_set (stats,
+                          "# nodes in the network (estimate)",
+                          (uint64_t) pow (2, GNUNET_MAX (se,
+                                                         nsize)),
+                          GNUNET_NO);
+  }
 }
 
 
@@ -422,28 +419,28 @@ setup_estimate_message (struct GNUNET_NSE_ClientMessage *em)
  * Also, we remember the client for updates upon future
  * estimate measurements.
  *
- * @param cls unused
- * @param client who sent the message
+ * @param cls client who sent the message
  * @param message the message received
  */
 static void
-handle_start_message (void *cls,
-                     struct GNUNET_SERVER_Client *client,
-                      const struct GNUNET_MessageHeader *message)
+handle_start (void *cls,
+             const struct GNUNET_MessageHeader *message)
 {
+  struct GNUNET_SERVICE_Client *client = cls;
+  struct GNUNET_MQ_Handle *mq;
   struct GNUNET_NSE_ClientMessage em;
+  struct GNUNET_MQ_Envelope *env;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Received START message from client\n");
-  GNUNET_SERVER_notification_context_add (nc,
-                                         client);
+  mq = GNUNET_SERVICE_client_get_mq (client);
+  GNUNET_notification_context_add (nc,
+                                  mq);
   setup_estimate_message (&em);
-  GNUNET_SERVER_notification_context_unicast (nc,
-                                             client,
-                                             &em.header,
-                                              GNUNET_YES);
-  GNUNET_SERVER_receive_done (client,
-                             GNUNET_OK);
+  env = GNUNET_MQ_msg_copy (&em.header);
+  GNUNET_MQ_send (mq,
+                 env);
+  GNUNET_SERVICE_client_continue (client);
 }
 
 
@@ -610,14 +607,14 @@ transmit_task_cb (void *cls)
   struct NSEPeerEntry *peer_entry = cls;
   unsigned int idx;
   struct GNUNET_MQ_Envelope *env;
-  
+
   peer_entry->transmit_task = NULL;
   idx = estimate_index;
   if (GNUNET_NO == peer_entry->previous_round)
   {
     idx = (idx + HISTORY_SIZE - 1) % HISTORY_SIZE;
     peer_entry->previous_round = GNUNET_YES;
-    peer_entry->transmit_task 
+    peer_entry->transmit_task
       = GNUNET_SCHEDULER_add_delayed (get_transmit_delay (0),
                                      &transmit_task_cb,
                                      peer_entry);
@@ -655,12 +652,12 @@ transmit_task_cb (void *cls)
                             GNUNET_NO);
 #if ENABLE_NSE_HISTOGRAM
   peer_entry->transmitted_messages++;
-  peer_entry->last_transmitted_size 
+  peer_entry->last_transmitted_size
     = ntohl(size_estimate_messages[idx].matching_bits);
 #endif
   env = GNUNET_MQ_msg_copy (&size_estimate_messages[idx].header);
   GNUNET_MQ_send (peer_entry->mq,
-                 env);  
+                 env);
 }
 
 
@@ -676,9 +673,9 @@ update_network_size_estimate ()
   struct GNUNET_NSE_ClientMessage em;
 
   setup_estimate_message (&em);
-  GNUNET_SERVER_notification_context_broadcast (nc,
-                                               &em.header,
-                                               GNUNET_YES);
+  GNUNET_notification_context_broadcast (nc,
+                                        &em.header,
+                                        GNUNET_YES);
 }
 
 
@@ -814,11 +811,10 @@ update_flood_message (void *cls)
   GNUNET_CONTAINER_multipeermap_iterate (peers,
                                          &schedule_current_round,
                                          NULL);
-  flood_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (next_timestamp),
-                                   &update_flood_message,
-                                    NULL);
+  flood_task
+    = GNUNET_SCHEDULER_add_at (next_timestamp,
+                               &update_flood_message,
+                               NULL);
 }
 
 
@@ -1015,7 +1011,7 @@ update_flood_times (void *cls,
   {
     /* still stuck in previous round, no point to update, check that
      * we are active here though... */
-    if (NULL == peer_entry->transmit_task) 
+    if (NULL == peer_entry->transmit_task)
     {
       GNUNET_break (0);
     }
@@ -1037,7 +1033,7 @@ update_flood_times (void *cls,
 /**
  * Core handler for size estimate flooding messages.
  *
- * @param cls peer this message is from 
+ * @param cls peer this message is from
  * @param incoming_flood received message
  */
 static void
@@ -1172,7 +1168,7 @@ handle_p2p_estimate (void *cls,
     /* push back our result now, that peer is spreading bad information... */
     if (NULL != peer_entry->transmit_task)
       GNUNET_SCHEDULER_cancel (peer_entry->transmit_task);
-    peer_entry->transmit_task 
+    peer_entry->transmit_task
       = GNUNET_SCHEDULER_add_now (&transmit_task_cb,
                                  peer_entry);
     /* Not closer than our most recent message, no need to do work here */
@@ -1292,7 +1288,7 @@ handle_core_disconnect (void *cls,
                  GNUNET_CONTAINER_multipeermap_remove (peers,
                                                       peer,
                                                        pos));
-  if (pos->transmit_task != NULL)
+  if (NULL != pos->transmit_task)
   {
     GNUNET_SCHEDULER_cancel (pos->transmit_task);
     pos->transmit_task = NULL;
@@ -1344,12 +1340,12 @@ shutdown_task (void *cls)
   }
   if (NULL != nc)
   {
-    GNUNET_SERVER_notification_context_destroy (nc);
+    GNUNET_notification_context_destroy (nc);
     nc = NULL;
   }
   if (NULL != core_api)
   {
-    GNUNET_CORE_disconnecT (core_api);
+    GNUNET_CORE_disconnect (core_api);
     core_api = NULL;
   }
   if (NULL != stats)
@@ -1434,11 +1430,10 @@ core_init (void *cls,
                         current_timestamp);
     estimate_count++;
   }
-  flood_task =
-      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
-                                    (next_timestamp),
-                                   &update_flood_message,
-                                    NULL);
+  flood_task
+    = GNUNET_SCHEDULER_add_at (next_timestamp,
+                               &update_flood_message,
+                               NULL);
 }
 
 
@@ -1476,31 +1471,25 @@ status_cb (void *cls,
  * Handle network size estimate clients.
  *
  * @param cls closure
- * @param server the initialized server
  * @param c configuration to use
+ * @param service the initialized service
  */
 static void
 run (void *cls,
-     struct GNUNET_SERVER_Handle *server,
-     const struct GNUNET_CONFIGURATION_Handle *c)
+     const struct GNUNET_CONFIGURATION_Handle *c,
+     struct GNUNET_SERVICE_Handle *service)
 {
-  GNUNET_MQ_hd_fixed_size (p2p_estimate,
-                          GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD,
-                          struct GNUNET_NSE_FloodMessage);
-  static const struct GNUNET_SERVER_MessageHandler handlers[] = {
-    {&handle_start_message, NULL, GNUNET_MESSAGE_TYPE_NSE_START,
-     sizeof (struct GNUNET_MessageHeader)},
-    {NULL, NULL, 0, 0}
-  };
   struct GNUNET_MQ_MessageHandler core_handlers[] = {
-    make_p2p_estimate_handler (NULL),
+    GNUNET_MQ_hd_fixed_size (p2p_estimate,
+                             GNUNET_MESSAGE_TYPE_NSE_P2P_FLOOD,
+                             struct GNUNET_NSE_FloodMessage,
+                             NULL),
     GNUNET_MQ_handler_end ()
   };
   char *proof;
   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
 
   cfg = c;
-  srv = server;
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_time (cfg,
                                           "NSE",
@@ -1614,11 +1603,9 @@ run (void *cls,
 
   peers = GNUNET_CONTAINER_multipeermap_create (128,
                                                GNUNET_YES);
-  GNUNET_SERVER_add_handlers (srv,
-                             handlers);
-  nc = GNUNET_SERVER_notification_context_create (srv, 1);
+  nc = GNUNET_notification_context_create (1);
   /* Connect to core service and register core handlers */
-  core_api = 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 */
@@ -1635,26 +1622,55 @@ run (void *cls,
 
 
 /**
- * The main function for the network size estimation service.
+ * Callback called when a client connects to the service.
+ *
+ * @param cls closure for the service
+ * @param c the new client that connected to the service
+ * @param mq the message queue used to send messages to the client
+ * @return @a c
+ */
+static void *
+client_connect_cb (void *cls,
+                  struct GNUNET_SERVICE_Client *c,
+                  struct GNUNET_MQ_Handle *mq)
+{
+  return c;
+}
+
+
+/**
+ * Callback called when a client disconnected from the service
  *
- * @param argc number of arguments from the command line
- * @param argv command line arguments
- * @return 0 ok, 1 on error
+ * @param cls closure for the service
+ * @param c the client that disconnected
+ * @param internal_cls should be equal to @a c
  */
-int
-main (int argc,
-      char *const *argv)
+static void
+client_disconnect_cb (void *cls,
+                     struct GNUNET_SERVICE_Client *c,
+                     void *internal_cls)
 {
-  return (GNUNET_OK ==
-          GNUNET_SERVICE_run (argc,
-                             argv,
-                             "nse",
-                             GNUNET_SERVICE_OPTION_NONE,
-                              &run,
-                             NULL)) ? 0 : 1;
+  GNUNET_assert (c == internal_cls);
 }
 
 
+/**
+ * Define "main" method using service macro.
+ */
+GNUNET_SERVICE_MAIN
+("nse",
+ GNUNET_SERVICE_OPTION_NONE,
+ &run,
+ &client_connect_cb,
+ &client_disconnect_cb,
+ NULL,
+ GNUNET_MQ_hd_fixed_size (start,
+                         GNUNET_MESSAGE_TYPE_NSE_START,
+                         struct GNUNET_MessageHeader,
+                         NULL),
+ GNUNET_MQ_handler_end ());
+
+
 #if defined(LINUX) && defined(__GLIBC__)
 #include <malloc.h>