uncrustify as demanded.
[oweals/gnunet.git] / src / zonemaster / gnunet-service-zonemaster-monitor.c
index 9a6fa678e8a2b3ed1b836455071171f9901e6c62..3e15742380f6023d90f18e4fed483c30db253c88 100644 (file)
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      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/>.
-*/
+
+     SPDX-License-Identifier: AGPL3.0-or-later
+ */
 
 /**
  * @file zonemaster/gnunet-service-zonemaster-monitor.c
@@ -28,7 +30,7 @@
 #include "gnunet_statistics_service.h"
 
 
-#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util", syscall, filename)
 
 
 /**
@@ -57,8 +59,7 @@
 /**
  * Handle for DHT PUT activity triggered from the namestore monitor.
  */
-struct DhtPutActivity
-{
+struct DhtPutActivity {
   /**
    * Kept in a DLL.
    */
@@ -130,43 +131,43 @@ static int cache_keys;
  * @param tc unused
  */
 static void
-shutdown_task (void *cls)
+shutdown_task(void *cls)
 {
   struct DhtPutActivity *ma;
 
-  (void) cls;
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Shutting down!\n");
+  (void)cls;
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Shutting down!\n");
   while (NULL != (ma = ma_head))
-  {
-    GNUNET_DHT_put_cancel (ma->ph);
-    ma_queue_length--;
-    GNUNET_CONTAINER_DLL_remove (ma_head,
-                                 ma_tail,
-                                 ma);
-    GNUNET_free (ma);
-  }
+    {
+      GNUNET_DHT_put_cancel(ma->ph);
+      ma_queue_length--;
+      GNUNET_CONTAINER_DLL_remove(ma_head,
+                                  ma_tail,
+                                  ma);
+      GNUNET_free(ma);
+    }
   if (NULL != statistics)
-  {
-    GNUNET_STATISTICS_destroy (statistics,
-                               GNUNET_NO);
-    statistics = NULL;
-  }
+    {
+      GNUNET_STATISTICS_destroy(statistics,
+                                GNUNET_NO);
+      statistics = NULL;
+    }
   if (NULL != zmon)
-  {
-    GNUNET_NAMESTORE_zone_monitor_stop (zmon);
-    zmon = NULL;
-  }
+    {
+      GNUNET_NAMESTORE_zone_monitor_stop(zmon);
+      zmon = NULL;
+    }
   if (NULL != namestore_handle)
-  {
-    GNUNET_NAMESTORE_disconnect (namestore_handle);
-    namestore_handle = NULL;
-  }
+    {
+      GNUNET_NAMESTORE_disconnect(namestore_handle);
+      namestore_handle = NULL;
+    }
   if (NULL != dht_handle)
-  {
-    GNUNET_DHT_disconnect (dht_handle);
-    dht_handle = NULL;
-  }
+    {
+      GNUNET_DHT_disconnect(dht_handle);
+      dht_handle = NULL;
+    }
 }
 
 
@@ -177,17 +178,17 @@ shutdown_task (void *cls)
  * @param cls a `struct DhtPutActivity`
  */
 static void
-dht_put_monitor_continuation (void *cls)
+dht_put_monitor_continuation(void *cls)
 {
   struct DhtPutActivity *ma = cls;
 
-  GNUNET_NAMESTORE_zone_monitor_next (zmon,
-                                      1);
+  GNUNET_NAMESTORE_zone_monitor_next(zmon,
+                                     1);
   ma_queue_length--;
-  GNUNET_CONTAINER_DLL_remove (ma_head,
-                               ma_tail,
-                               ma);
-  GNUNET_free (ma);
+  GNUNET_CONTAINER_DLL_remove(ma_head,
+                              ma_tail,
+                              ma);
+  GNUNET_free(ma);
 }
 
 
@@ -202,24 +203,24 @@ dht_put_monitor_continuation (void *cls)
  * @return number of records written to @a rd_public
  */
 static unsigned int
-convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
-                            unsigned int rd_count,
-                            struct GNUNET_GNSRECORD_Data *rd_public)
+convert_records_for_export(const struct GNUNET_GNSRECORD_Data *rd,
+                           unsigned int rd_count,
+                           struct GNUNET_GNSRECORD_Data *rd_public)
 {
   struct GNUNET_TIME_Absolute now;
   unsigned int rd_public_count;
 
   rd_public_count = 0;
-  now = GNUNET_TIME_absolute_get ();
-  for (unsigned int i=0;i<rd_count;i++)
-  {
-    if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
-      continue;
-    if ( (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) &&
-         (rd[i].expiration_time < now.abs_value_us) )
-      continue;  /* record already expired, skip it */
-    rd_public[rd_public_count++] = rd[i];
-  }
+  now = GNUNET_TIME_absolute_get();
+  for (unsigned int i = 0; i < rd_count; i++)
+    {
+      if (0 != (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE))
+        continue;
+      if ((0 == (rd[i].flags & GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION)) &&
+          (rd[i].expiration_time < now.abs_value_us))
+        continue; /* record already expired, skip it */
+      rd_public[rd_public_count++] = rd[i];
+    }
   return rd_public_count;
 }
 
@@ -235,11 +236,11 @@ convert_records_for_export (const struct GNUNET_GNSRECORD_Data *rd,
  * @return DHT PUT handle, NULL on error
  */
 static struct GNUNET_DHT_PutHandle *
-perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
-                 const char *label,
-                 const struct GNUNET_GNSRECORD_Data *rd_public,
-                 unsigned int rd_public_count,
-                 struct DhtPutActivity *ma)
+perform_dht_put(const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
+                const char *label,
+                const struct GNUNET_GNSRECORD_Data *rd_public,
+                unsigned int rd_public_count,
+                struct DhtPutActivity *ma)
 {
   struct GNUNET_GNSRECORD_Block *block;
   struct GNUNET_HashCode query;
@@ -247,52 +248,52 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
   size_t block_size;
   struct GNUNET_DHT_PutHandle *ret;
 
-  expire = GNUNET_GNSRECORD_record_get_expiration_time (rd_public_count,
-                                                        rd_public);
+  expire = GNUNET_GNSRECORD_record_get_expiration_time(rd_public_count,
+                                                       rd_public);
   if (cache_keys)
-    block = GNUNET_GNSRECORD_block_create2 (key,
-                                            expire,
-                                            label,
-                                            rd_public,
-                                            rd_public_count);
-  else
-    block = GNUNET_GNSRECORD_block_create (key,
+    block = GNUNET_GNSRECORD_block_create2(key,
                                            expire,
                                            label,
                                            rd_public,
                                            rd_public_count);
+  else
+    block = GNUNET_GNSRECORD_block_create(key,
+                                          expire,
+                                          label,
+                                          rd_public,
+                                          rd_public_count);
   if (NULL == block)
-  {
-    GNUNET_break (0);
-    return NULL; /* whoops */
-  }
-  block_size = ntohl (block->purpose.size)
-    + sizeof (struct GNUNET_CRYPTO_EcdsaSignature)
-    + sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
-  GNUNET_GNSRECORD_query_from_private_key (key,
-                                           label,
-                                           &query);
-  GNUNET_STATISTICS_update (statistics,
-                            "DHT put operations initiated",
-                            1,
-                            GNUNET_NO);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
-              rd_public_count,
-              label,
-              GNUNET_STRINGS_absolute_time_to_string (expire),
-              GNUNET_h2s (&query));
-  ret = GNUNET_DHT_put (dht_handle,
-                        &query,
-                        DHT_GNS_REPLICATION_LEVEL,
-                        GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
-                        GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
-                        block_size,
-                        block,
-                        expire,
-                        &dht_put_monitor_continuation,
-                        ma);
-  GNUNET_free (block);
+    {
+      GNUNET_break(0);
+      return NULL; /* whoops */
+    }
+  block_size = ntohl(block->purpose.size)
+               + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)
+               + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey);
+  GNUNET_GNSRECORD_query_from_private_key(key,
+                                          label,
+                                          &query);
+  GNUNET_STATISTICS_update(statistics,
+                           "DHT put operations initiated",
+                           1,
+                           GNUNET_NO);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Storing %u record(s) for label `%s' in DHT with expiration `%s' under key %s\n",
+             rd_public_count,
+             label,
+             GNUNET_STRINGS_absolute_time_to_string(expire),
+             GNUNET_h2s(&query));
+  ret = GNUNET_DHT_put(dht_handle,
+                       &query,
+                       DHT_GNS_REPLICATION_LEVEL,
+                       GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
+                       GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
+                       block_size,
+                       block,
+                       expire,
+                       &dht_put_monitor_continuation,
+                       ma);
+  GNUNET_free(block);
   return ret;
 }
 
@@ -308,69 +309,69 @@ perform_dht_put (const struct GNUNET_CRYPTO_EcdsaPrivateKey *key,
  * @param rd array of records with data to store
  */
 static void
-handle_monitor_event (void *cls,
-                      const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
-                      const char *label,
-                      unsigned int rd_count,
-                      const struct GNUNET_GNSRECORD_Data *rd)
+handle_monitor_event(void *cls,
+                     const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
+                     const char *label,
+                     unsigned int rd_count,
+                     const struct GNUNET_GNSRECORD_Data *rd)
 {
   struct GNUNET_GNSRECORD_Data rd_public[rd_count];
   unsigned int rd_public_count;
   struct DhtPutActivity *ma;
 
-  (void) cls;
-  GNUNET_STATISTICS_update (statistics,
-                            "Namestore monitor events received",
-                            1,
-                            GNUNET_NO);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Received %u records for label `%s' via namestore monitor\n",
-              rd_count,
-              label);
+  (void)cls;
+  GNUNET_STATISTICS_update(statistics,
+                           "Namestore monitor events received",
+                           1,
+                           GNUNET_NO);
+  GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+             "Received %u records for label `%s' via namestore monitor\n",
+             rd_count,
+             label);
   /* filter out records that are not public, and convert to
      absolute expiration time. */
-  rd_public_count = convert_records_for_export (rd,
-                                                rd_count,
-                                                rd_public);
+  rd_public_count = convert_records_for_export(rd,
+                                               rd_count,
+                                               rd_public);
   if (0 == rd_public_count)
-  {
-    GNUNET_NAMESTORE_zone_monitor_next (zmon,
-                                        1);
-    return; /* nothing to do */
-  }
-  ma = GNUNET_new (struct DhtPutActivity);
-  ma->start_date = GNUNET_TIME_absolute_get ();
-  ma->ph = perform_dht_put (zone,
-                            label,
-                            rd,
-                            rd_count,
-                            ma);
+    {
+      GNUNET_NAMESTORE_zone_monitor_next(zmon,
+                                         1);
+      return; /* nothing to do */
+    }
+  ma = GNUNET_new(struct DhtPutActivity);
+  ma->start_date = GNUNET_TIME_absolute_get();
+  ma->ph = perform_dht_put(zone,
+                           label,
+                           rd,
+                           rd_count,
+                           ma);
   if (NULL == ma->ph)
-  {
-    /* PUT failed, do not remember operation */
-    GNUNET_free (ma);
-    GNUNET_NAMESTORE_zone_monitor_next (zmon,
-                                        1);
-    return;
-  }
-  GNUNET_CONTAINER_DLL_insert_tail (ma_head,
-                                   ma_tail,
-                                   ma);
+    {
+      /* PUT failed, do not remember operation */
+      GNUNET_free(ma);
+      GNUNET_NAMESTORE_zone_monitor_next(zmon,
+                                         1);
+      return;
+    }
+  GNUNET_CONTAINER_DLL_insert_tail(ma_head,
+                                   ma_tail,
+                                   ma);
   ma_queue_length++;
   if (ma_queue_length > DHT_QUEUE_LIMIT)
-  {
-    ma = ma_head;
-    GNUNET_CONTAINER_DLL_remove (ma_head,
-                                 ma_tail,
-                                 ma);
-    GNUNET_DHT_put_cancel (ma->ph);
-    ma_queue_length--;
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                "DHT PUT unconfirmed after %s, aborting PUT\n",
-                GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (ma->start_date),
+    {
+      ma = ma_head;
+      GNUNET_CONTAINER_DLL_remove(ma_head,
+                                  ma_tail,
+                                  ma);
+      GNUNET_DHT_put_cancel(ma->ph);
+      ma_queue_length--;
+      GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+                 "DHT PUT unconfirmed after %s, aborting PUT\n",
+                 GNUNET_STRINGS_relative_time_to_string(GNUNET_TIME_absolute_get_duration(ma->start_date),
                                                         GNUNET_YES));
-    GNUNET_free (ma);
-  }
+      GNUNET_free(ma);
+    }
 }
 
 
@@ -381,13 +382,13 @@ handle_monitor_event (void *cls,
  * @param cls NULL
  */
 static void
-handle_monitor_error (void *cls)
+handle_monitor_error(void *cls)
 {
-  (void) cls;
-  GNUNET_STATISTICS_update (statistics,
-                            "Namestore monitor errors encountered",
-                            1,
-                            GNUNET_NO);
+  (void)cls;
+  GNUNET_STATISTICS_update(statistics,
+                           "Namestore monitor errors encountered",
+                           1,
+                           GNUNET_NO);
 }
 
 
@@ -399,65 +400,65 @@ handle_monitor_error (void *cls)
  * @param c configuration to use
  */
 static void
-run (void *cls,
-     const struct GNUNET_CONFIGURATION_Handle *c,
-     struct GNUNET_SERVICE_Handle *service)
+run(void *cls,
+    const struct GNUNET_CONFIGURATION_Handle *c,
+    struct GNUNET_SERVICE_Handle *service)
 {
   unsigned long long max_parallel_bg_queries = 128;
 
-  (void) cls;
-  (void) service;
-  namestore_handle = GNUNET_NAMESTORE_connect (c);
+  (void)cls;
+  (void)service;
+  namestore_handle = GNUNET_NAMESTORE_connect(c);
   if (NULL == namestore_handle)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Failed to connect to the namestore!\n"));
-    GNUNET_SCHEDULER_shutdown ();
-    return;
-  }
-  cache_keys = GNUNET_CONFIGURATION_get_value_yesno (c,
-                                                     "namestore",
-                                                     "CACHE_KEYS");
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+                 _("Failed to connect to the namestore!\n"));
+      GNUNET_SCHEDULER_shutdown();
+      return;
+    }
+  cache_keys = GNUNET_CONFIGURATION_get_value_yesno(c,
+                                                    "namestore",
+                                                    "CACHE_KEYS");
   if (GNUNET_OK ==
-      GNUNET_CONFIGURATION_get_value_number (c,
-                                            "zonemaster",
-                                             "MAX_PARALLEL_BACKGROUND_QUERIES",
-                                             &max_parallel_bg_queries))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Number of allowed parallel background queries: %llu\n",
-                max_parallel_bg_queries);
-  }
+      GNUNET_CONFIGURATION_get_value_number(c,
+                                            "zonemaster",
+                                            "MAX_PARALLEL_BACKGROUND_QUERIES",
+                                            &max_parallel_bg_queries))
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
+                 "Number of allowed parallel background queries: %llu\n",
+                 max_parallel_bg_queries);
+    }
   if (0 == max_parallel_bg_queries)
     max_parallel_bg_queries = 1;
-  dht_handle = GNUNET_DHT_connect (c,
-                                   (unsigned int) max_parallel_bg_queries);
+  dht_handle = GNUNET_DHT_connect(c,
+                                  (unsigned int)max_parallel_bg_queries);
   if (NULL == dht_handle)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Could not connect to DHT!\n"));
-    GNUNET_SCHEDULER_add_now (&shutdown_task,
-                             NULL);
-    return;
-  }
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+                 _("Could not connect to DHT!\n"));
+      GNUNET_SCHEDULER_add_now(&shutdown_task,
+                               NULL);
+      return;
+    }
 
   /* Schedule periodic put for our records. */
-  statistics = GNUNET_STATISTICS_create ("zonemaster-mon",
-                                         c);
-  zmon = GNUNET_NAMESTORE_zone_monitor_start (c,
-                                              NULL,
-                                              GNUNET_NO,
-                                              &handle_monitor_error,
-                                              NULL,
-                                              &handle_monitor_event,
-                                              NULL,
-                                              NULL /* sync_cb */,
-                                              NULL);
-  GNUNET_NAMESTORE_zone_monitor_next (zmon,
-                                      NAMESTORE_QUEUE_LIMIT - 1);
-  GNUNET_break (NULL != zmon);
-  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
-                                NULL);
+  statistics = GNUNET_STATISTICS_create("zonemaster-mon",
+                                        c);
+  zmon = GNUNET_NAMESTORE_zone_monitor_start(c,
+                                             NULL,
+                                             GNUNET_NO,
+                                             &handle_monitor_error,
+                                             NULL,
+                                             &handle_monitor_event,
+                                             NULL,
+                                             NULL /* sync_cb */,
+                                             NULL);
+  GNUNET_NAMESTORE_zone_monitor_next(zmon,
+                                     NAMESTORE_QUEUE_LIMIT - 1);
+  GNUNET_break(NULL != zmon);
+  GNUNET_SCHEDULER_add_shutdown(&shutdown_task,
+                                NULL);
 }
 
 
@@ -465,13 +466,13 @@ run (void *cls,
  * Define "main" method using service macro.
  */
 GNUNET_SERVICE_MAIN
-("zonemaster-monitor",
- GNUNET_SERVICE_OPTION_NONE,
- &run,
- NULL,
- NULL,
- NULL,
- GNUNET_MQ_handler_end());
+  ("zonemaster-monitor",
 GNUNET_SERVICE_OPTION_NONE,
 &run,
 NULL,
 NULL,
 NULL,
 GNUNET_MQ_handler_end());
 
 
 /* end of gnunet-service-zonemaster-monitor.c */