- remove adjust
[oweals/gnunet.git] / src / experimentation / gnunet-daemon-experimentation_experiments.c
index d0dda1b1b47e5a2eca264eadb2771ff7fec14376..471a7bfdc01e2962b24c8272b6246b22bae7fc80 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2009 Christian Grothoff (and other contributing authors)
+     (C) 2012,2013 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
 
 
 /**
- * Struct to store information about an experiment issuer
+ * Hashmap containing valid experiment issuers.
  */
-struct Issuer
-{
-  struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
-};
-
-
-/**
- * Hashmap containing valid experiment issuer
- */
-static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
+struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
 
 /**
  * Hashmap containing valid experiments
@@ -51,36 +42,26 @@ static struct GNUNET_CONTAINER_MultiHashMap *valid_issuers;
 static struct GNUNET_CONTAINER_MultiHashMap *experiments;
 
 
-static uint32_t GSE_my_issuer_count;
-
-/**
- * Valid experiment issuer for this daemon
- *
- * Array Experimentation_Issuer with GSE_my_issuer_count elements
- */
-static struct Experimentation_Issuer *GSE_my_issuer;
-
-
 /**
  * Verify experiment signature
  *
  * @param i issuer
  * @param e experiment
- * @return GNUNET_OK or GNUNET_SYSERR
+ * @return #GNUNET_OK or #GNUNET_SYSERR
  */
 static int
 experiment_verify (struct Issuer *i, struct Experiment *e)
 {
   GNUNET_assert (NULL != i);
   GNUNET_assert (NULL != e);
-  
+
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
              "Verification: to be implemented\n");
   return GNUNET_OK;
 }
 
 
-static int 
+static int
 free_experiment (void *cls,
                 const struct GNUNET_HashCode * key,
                 void *value)
@@ -103,77 +84,58 @@ free_experiment (void *cls,
  * @param value the issuer element to free
  * @return GNUNET_OK to continue
  */
-static int 
+static int
 free_issuer (void *cls,
             const struct GNUNET_HashCode * key,
             void *value)
 {
   struct Issuer *i = value;
 
-  GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (valid_issuers, key, value));
+  GNUNET_break (0 == GNUNET_CONTAINER_multihashmap_remove (valid_issuers,
+                                                          key,
+                                                          i));
   GNUNET_free (i);
   return GNUNET_OK;
 }
 
 
-static int
-create_issuer (void *cls,
-              const struct GNUNET_HashCode * key,
-              void *value)
-{
-  static int i = 0;
-
-  GNUNET_assert (i < GSE_my_issuer_count);
-  GSE_my_issuer[i].issuer_id.hashPubKey = *key;
-  
-  i++;
-  return GNUNET_OK;
-}
-
-
-
 /**
  * Is peer a valid issuer
  *
- * @return GNUNET_YES or GNUNET_NO
+ * @return #GNUNET_YES or #GNUNET_NO
  */
 int
-GED_experiments_issuer_accepted (struct GNUNET_PeerIdentity *issuer_ID)
+GED_experiments_issuer_accepted (const struct GNUNET_CRYPTO_EddsaPublicKey *issuer_id)
 {
-  if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &issuer_ID->hashPubKey))
+  struct GNUNET_HashCode hash;
+
+  GNUNET_CRYPTO_hash (issuer_id, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey), &hash);
+  if (GNUNET_CONTAINER_multihashmap_contains (valid_issuers, &hash))
     return GNUNET_YES;
-  else
-    return GNUNET_NO;
+  return GNUNET_NO;
 }
 
 
-struct FindCtx
-{
-  const char *name;
-  struct GNUNET_TIME_Absolute version;
-  struct Experiment *res;
-};
-
-
-static int
-find_it (void *cls,
-        const struct GNUNET_HashCode * key,
-        void *value)
+/**
+ * Get the key under which the given experiment is stored in the
+ * experiment map.
+ */
+static void
+get_experiment_key (const struct GNUNET_CRYPTO_EddsaPublicKey *issuer,
+                   const char *name,
+                   const struct GNUNET_TIME_Absolute version,
+                   struct GNUNET_HashCode *key)
 {
-  struct FindCtx *find_ctx = cls;
-  struct Experiment *e = (struct Experiment *) value;
-  
-  if (0 != strcmp(e->name, find_ctx->name))
-    return GNUNET_OK;
-  if (e->version.abs_value_us != find_ctx->version.abs_value_us)
-    return GNUNET_OK;
-  
-  find_ctx->res = e;
-  return GNUNET_NO;
+  GNUNET_assert (GNUNET_YES ==
+                GNUNET_CRYPTO_kdf (key, sizeof (struct GNUNET_HashCode),
+                                   issuer, sizeof (struct GNUNET_CRYPTO_EddsaPublicKey),
+                                   name, strlen (name),
+                                   &version, sizeof (version),
+                                   NULL, 0));
 }
 
 
-/*
+/**
  * Find an experiment based on issuer name and version
  *
  * @param issuer the issuer
@@ -182,47 +144,50 @@ find_it (void *cls,
  * @return the experiment or NULL if not found
  */
 struct Experiment *
-GED_experiments_find (const struct GNUNET_PeerIdentity *issuer,
+GED_experiments_find (const struct GNUNET_CRYPTO_EddsaPublicKey *issuer,
                      const char *name,
                      const struct GNUNET_TIME_Absolute version)
 {
-  struct FindCtx find_ctx;
-  
-  find_ctx.name = name;
-  find_ctx.version = version;
-  find_ctx.res = NULL;
-  
-  GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
-                                             &issuer->hashPubKey, 
-                                             &find_it, &find_ctx);
-  return find_ctx.res;
+  struct GNUNET_HashCode hc;
+
+  get_experiment_key (issuer,
+                     name,
+                     version,
+                     &hc);
+  return GNUNET_CONTAINER_multihashmap_get (experiments,
+                                           &hc);
 }
 
 
 struct GetCtx
 {
   struct Node *n;
+
   GNUNET_EXPERIMENTATION_experiments_get_cb get_cb;
+
+  struct GNUNET_CRYPTO_EddsaPublicKey *issuer;
 };
 
 
 static int
 get_it (void *cls,
-       const struct GNUNET_HashCode * key,
+       const struct GNUNET_HashCode *key,
        void *value)
 {
   struct GetCtx *get_ctx = cls;
   struct Experiment *e = value;
 
-  get_ctx->get_cb (get_ctx->n, e);
-  
+  if (0 == memcmp (&e->issuer,
+                  get_ctx->issuer,
+                  sizeof (struct GNUNET_CRYPTO_EddsaPublicKey)))
+    get_ctx->get_cb (get_ctx->n, e);
   return GNUNET_OK;
 }
 
 
 void
 GED_experiments_get (struct Node *n,
-                    struct GNUNET_PeerIdentity *issuer,
+                    struct GNUNET_CRYPTO_EddsaPublicKey *issuer,
                     GNUNET_EXPERIMENTATION_experiments_get_cb get_cb)
 {
   struct GetCtx get_ctx;
@@ -230,14 +195,12 @@ GED_experiments_get (struct Node *n,
   GNUNET_assert (NULL != n);
   GNUNET_assert (NULL != experiments);
   GNUNET_assert (NULL != get_cb);
-  
   get_ctx.n = n;
   get_ctx.get_cb = get_cb;
-
-  GNUNET_CONTAINER_multihashmap_get_multiple (experiments,
-                                             &issuer->hashPubKey, &get_it, &get_ctx);
-  
-  get_cb (n, NULL);
+  get_ctx.issuer = issuer;
+  GNUNET_CONTAINER_multihashmap_iterate (experiments,
+                                        &get_it, &get_ctx);
+  get_cb (n, NULL); // FIXME: ugly, end is easily signalled as we return: synchronous API!
 }
 
 
@@ -247,7 +210,7 @@ GED_experiments_get (struct Node *n,
 int
 GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
                                        const char *name,
-                                       struct GNUNET_PeerIdentity issuer_id,
+                                       const struct GNUNET_CRYPTO_EddsaPublicKey *issuer_id,
                                        struct GNUNET_TIME_Absolute version,
                                        char *description,
                                        uint32_t required_capabilities,
@@ -257,10 +220,11 @@ GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
                                        struct GNUNET_TIME_Absolute stop)
 {
   struct Experiment *e;
+  struct GNUNET_HashCode hc;
 
-  e = GNUNET_new (struct Experiment);  
+  e = GNUNET_new (struct Experiment);
   e->name = GNUNET_strdup (name);
-  e->issuer = issuer_id;
+  e->issuer = *issuer_id;
   e->version = version;
   if (NULL != description)
     e->description = GNUNET_strdup (description);
@@ -269,34 +233,38 @@ GNUNET_EXPERIMENTATION_experiments_add (struct Issuer *i,
   e->frequency = frequency;
   e->duration = duration;
   e->stop = stop;
-  
+
   /* verify experiment */
   if (GNUNET_SYSERR == experiment_verify (i, e))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("Experiment `%s': Experiment signature is invalid\n"), 
+               _("Experiment `%s': Experiment signature is invalid\n"),
                name);
     GNUNET_free (e);
     GNUNET_free_non_null (e->name);
     GNUNET_free_non_null (e->description);
     return GNUNET_SYSERR;
   }
-  
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              _("Adding experiment `%s' running from `%s' to `%s' every %llu sec. for %llu sec. \n"),
              e->name,
              GNUNET_STRINGS_absolute_time_to_string (start),
              GNUNET_STRINGS_absolute_time_to_string (stop),
              (long long unsigned int) frequency.rel_value_us / 1000000LL,
              (long long unsigned int) duration.rel_value_us / 1000000LL);
+  get_experiment_key (&e->issuer,
+                     name,
+                     version,
+                     &hc);
   GNUNET_CONTAINER_multihashmap_put (experiments,
-                                    &e->issuer.hashPubKey, 
-                                    e, 
+                                    &hc,
+                                    e,
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
-  GNUNET_STATISTICS_set (GED_stats, 
-                        "# experiments", 
+  GNUNET_STATISTICS_set (GED_stats,
+                        "# experiments",
                         GNUNET_CONTAINER_multihashmap_size (experiments), GNUNET_NO);
-  
+
   return GNUNET_OK;
 }
 
@@ -316,7 +284,7 @@ exp_file_iterator (void *cls,
   char *val;
   unsigned long long number;
   /* Experiment values */
-  struct GNUNET_PeerIdentity issuer;
+  struct GNUNET_CRYPTO_EddsaPublicKey issuer;
   struct GNUNET_TIME_Absolute version;
   char *description;
   uint32_t required_capabilities;
@@ -324,9 +292,10 @@ exp_file_iterator (void *cls,
   struct GNUNET_TIME_Absolute stop;
   struct GNUNET_TIME_Relative frequency;
   struct GNUNET_TIME_Relative duration;
-  
+  struct GNUNET_HashCode phash;
+
   /* Mandatory fields */
-  
+
   /* Issuer */
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "ISSUER", &val))
   {
@@ -334,14 +303,18 @@ exp_file_iterator (void *cls,
                _("Experiment `%s': Issuer missing\n"), name);
     return;
   }
-  if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string (val, &issuer.hashPubKey))
+  if (GNUNET_SYSERR ==
+      GNUNET_CRYPTO_eddsa_public_key_from_string (val,
+                                                    strlen (val),
+                                                    &issuer))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Experiment `%s': Issuer invalid\n"), name);
     GNUNET_free (val);
     return;
   }
-  if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &issuer.hashPubKey)))
+  GNUNET_CRYPTO_hash (&issuer, sizeof (issuer), &phash);
+  if (NULL == (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &phash)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Experiment `%s': Issuer not accepted!\n"), name);
@@ -349,20 +322,20 @@ exp_file_iterator (void *cls,
     return;
   }
   GNUNET_free (val);
-  
+
   /* Version */
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "VERSION", &number))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Experiment `%s': Version missing or invalid \n"), name);
     return;
   }
   version.abs_value_us = number; // FIXME: what is this supposed to be? Version != TIME!???
-  
+
   /* Required capabilities */
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "CAPABILITIES", &number))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Experiment `%s': Required capabilities missing \n"), name);
     return;
   }
@@ -373,24 +346,24 @@ exp_file_iterator (void *cls,
     return;
   }
   required_capabilities = number;
-  
+
   /* Optional fields */
-  
+
   /* Description */
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (exp, name, "DESCRIPTION", &description))
     description = NULL;
-  
+
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "START", (long long unsigned int *) &start.abs_value_us))
     start = GNUNET_TIME_UNIT_ZERO_ABS;
-  
+
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "FREQUENCY", &frequency))
     frequency = EXP_DEFAULT_EXP_FREQ;
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_time (exp, name, "DURATION", &duration))
     duration = EXP_DEFAULT_EXP_DUR;
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_number (exp, name, "STOP", (long long unsigned int *)&stop.abs_value_us))
     stop = GNUNET_TIME_UNIT_FOREVER_ABS;
-  
-  GNUNET_EXPERIMENTATION_experiments_add (i, name, issuer, version,
+
+  GNUNET_EXPERIMENTATION_experiments_add (i, name, &issuer, version,
                                          description, required_capabilities,
                                          start, frequency, duration, stop);
   GNUNET_free_non_null (description);
@@ -409,12 +382,12 @@ load_file (const char * file)
 
   if (NULL == exp)
     return;
-  
+
   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_parse (exp, file))
   {
     GNUNET_CONFIGURATION_destroy (exp);
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-               _("Failed to parse file `%s'\n"), 
+               _("Failed to parse file `%s'\n"),
                file);
     return;
   }
@@ -432,103 +405,68 @@ GED_experiments_start ()
   struct Issuer *i;
   char *issuers;
   char *file;
-  char *pubkey;
   char *pos;
-  struct GNUNET_PeerIdentity issuer_ID;
-  struct GNUNET_CRYPTO_EccPublicSignKey pub;
+  struct GNUNET_CRYPTO_EddsaPublicKey issuer_ID;
   struct GNUNET_HashCode hash;
-  
+
   /* Load valid issuer */
-  if (GNUNET_SYSERR == 
-      GNUNET_CONFIGURATION_get_value_string (GED_cfg, "EXPERIMENTATION", "ISSUERS", &issuers))
+  if (GNUNET_SYSERR ==
+      GNUNET_CONFIGURATION_get_value_string (GED_cfg,
+                                            "EXPERIMENTATION",
+                                            "ISSUERS",
+                                            &issuers))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-               _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("No valid experiment issuers configured! Set value to public keys of issuers! Exiting.\n"));
+    GED_experiments_stop ();
     return GNUNET_SYSERR;
   }
-  
+
   valid_issuers = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
   for (pos = strtok (issuers, " "); pos != NULL; pos = strtok (NULL, " "))
-  {   
-    if (GNUNET_SYSERR == GNUNET_CRYPTO_ecc_public_sign_key_from_string (pos, 
+  {
+    if (GNUNET_SYSERR == GNUNET_CRYPTO_eddsa_public_key_from_string (pos,
                                                                        strlen (pos),
                                                                        &issuer_ID))
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-                 _("Invalid value `%s'\n"), 
-                 pos);
-    }
-    else
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
-                 "`%s' is a valid issuer \n", 
-                 GNUNET_i2s (&issuer_ID));
-      i = GNUNET_new (struct Issuer);
-      i->pubkey = issuer_ID;
-      GNUNET_CRYPTO_hash( &issuer_ID, sizeof (issuer_ID), &hash);
-      GNUNET_CONTAINER_multihashmap_put (valid_issuers, 
-                                        &hash,
-                                        i, 
-                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
+      GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                                "EXPERIMENTATION",
+                                "ISSUERS",
+                                _("Invalid value for public key\n"));
+      GED_experiments_stop ();
+      GNUNET_free (issuers);
+      return GNUNET_SYSERR;
     }
+    i = GNUNET_new (struct Issuer);
+    i->pubkey = issuer_ID;
+    GNUNET_CRYPTO_hash( &issuer_ID, sizeof (issuer_ID), &hash);
+    GNUNET_CONTAINER_multihashmap_put (valid_issuers,
+                                      &hash,
+                                      i,
+                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
   }
   GNUNET_free (issuers);
-
   if (0 == GNUNET_CONTAINER_multihashmap_size (valid_issuers))
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
-               _("No valid experiment issuers configured! Set value to peer id of issuer! Exit...\n"));
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+               _("No valid experiment issuers configured! Set value to public keys of issuers! Exiting.\n"));
     GED_experiments_stop ();
     return GNUNET_SYSERR;
   }
   GNUNET_STATISTICS_set (GED_stats,
-                        "# issuer", 
-                        GNUNET_CONTAINER_multihashmap_size (valid_issuers), 
+                        "# issuer",
+                        GNUNET_CONTAINER_multihashmap_size (valid_issuers),
                         GNUNET_NO);
-  if (GNUNET_OK == 
-      GNUNET_CONFIGURATION_get_value_string (GED_cfg, 
-                                            "EXPERIMENTATION",
-                                            "PUBKEY", 
-                                            &pubkey))
-  {
-    if (GNUNET_OK != 
-       GNUNET_CRYPTO_ecc_public_sign_key_from_string (pubkey, strlen (pubkey), &pub))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("Invalid public key `%s'\n"), 
-                 pubkey);
-      GED_experiments_stop ();
-      return GNUNET_SYSERR;
-    }
-    GNUNET_CRYPTO_hash( &pub, sizeof (pub), &hash);
-    if (NULL != (i = GNUNET_CONTAINER_multihashmap_get (valid_issuers, &hash)))
-    {
-      i->pubkey = pub;
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Found issuer for public key `%s'\n"), pubkey);
-    }
-    else
-    {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("No issuer for public key `%s'\n"), pubkey);
-    }
-    GNUNET_free (pubkey);
-  }
-
-  GSE_my_issuer_count = GNUNET_CONTAINER_multihashmap_size (valid_issuers);
-  GSE_my_issuer = GNUNET_malloc (GSE_my_issuer_count * sizeof (struct Experimentation_Issuer));
-  GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &create_issuer, GSE_my_issuer);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-             "Daemon has %u issuers\n", 
-             GSE_my_issuer_count);
 
   experiments = GNUNET_CONTAINER_multihashmap_create (10, GNUNET_NO);
   /* Load experiments from file */
   if (GNUNET_SYSERR ==
       GNUNET_CONFIGURATION_get_value_string (GED_cfg,
-                                            "EXPERIMENTATION", 
+                                            "EXPERIMENTATION",
                                             "EXPERIMENTS",
                                             &file))
     return GNUNET_OK;
-  
+
   if (GNUNET_YES != GNUNET_DISK_file_test (file))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -548,12 +486,6 @@ GED_experiments_start ()
 void
 GED_experiments_stop ()
 {
-  if (NULL != GSE_my_issuer)
-  {
-    GNUNET_free (GSE_my_issuer);
-    GSE_my_issuer = NULL;
-    GSE_my_issuer_count = 0;
-  }
   if (NULL != valid_issuers)
   {
     GNUNET_CONTAINER_multihashmap_iterate (valid_issuers, &free_issuer, NULL);