- unique constraint
[oweals/gnunet.git] / src / testbed / gnunet-daemon-testbed-underlay.c
index 9fe59e3fb9bd18615cfadcfb1ffde6913fc95654..d430b7fd169c02eb24cca60b7041c76aac238760 100644 (file)
@@ -29,6 +29,7 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_transport_service.h"
+#include "gnunet_ats_service.h"
 #include "gnunet_testing_lib.h"
 #include <sqlite3.h>
 
 #define DEBUG(...)                              \
   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
 
-
-#define LOG_SQLITE_ERROR(ret)                                           \
-  LOG (GNUNET_ERROR_TYPE_ERROR, "sqlite error: %s", sqlite3_errstr (ret))
+/**
+ * Log an error message at log-level 'level' that indicates
+ * a failure of the command 'cmd' on file 'filename'
+ * with the message given by strerror(errno).
+ */
+#define LOG_SQLITE(db, msg, level, cmd)                                 \
+  do {                                                                  \
+    GNUNET_log_from (level, "sqlite", _("`%s' failed at %s:%d with error: %s\n"), \
+                     cmd, __FILE__,__LINE__, sqlite3_errmsg(db));  \
+    if (msg != NULL)                                                    \
+      GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, \
+                      __FILE__, __LINE__, sqlite3_errmsg(db));     \
+  } while(0)
 
 
 /**
@@ -75,11 +86,6 @@ static struct GNUNET_CONTAINER_MultiPeerMap *blacklist_map;
  */
 static struct sqlite3 *db;
 
-/**
- * The array of peer identities we read from whitelist/blacklist
- */
-static struct GNUNET_PeerIdentity *ilist;
-
 /**
  * The blacklist handle we obtain from transport when we register ourselves for
  * access control
@@ -94,7 +100,12 @@ static struct GNUNET_DISK_MapHandle *idmap;
 /**
  * The hostkeys data
  */
-static struct GNUNET_PeerIdentity *hostkeys;
+static char *hostkeys_data;
+
+/**
+ * Handle to the transport service.  This is used for setting link metrics
+ */
+static struct GNUNET_TRANSPORT_Handle *transport;
 
 /**
  * The number of hostkeys in the hostkeys array
@@ -186,48 +197,18 @@ check_access (void *cls, const struct GNUNET_PeerIdentity * pid)
 }
 
 
-/**
- * Setup the access control by reading the given file containing peer identities
- * and then establishing blacklist handler with the peer's transport service
- *
- * @param fname the filename to read the list of peer identities
- * @param cfg the configuration for connecting to the peer's transport service
- */
-static void
-setup_ac (const char *fname, const struct GNUNET_CONFIGURATION_Handle *cfg)
+static int
+get_identity (unsigned int offset, struct GNUNET_PeerIdentity *id)
 {
-  uint64_t fsize;
-  unsigned int npeers;
-  unsigned int cnt;
+  struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
 
-  GNUNET_assert (GNUNET_OK != GNUNET_DISK_file_size (fname, &fsize, GNUNET_NO,
-                                                     GNUNET_YES));
-  if (0 != (fsize % sizeof (struct GNUNET_PeerIdentity)))
-  {
-    GNUNET_break (0);
-    return;
-  }
-  npeers = fsize / sizeof (struct GNUNET_PeerIdentity);
-  if (0 != npeers)
-  {
-    map = GNUNET_CONTAINER_multipeermap_create (npeers, GNUNET_YES);
-    ilist = GNUNET_malloc_large (fsize);
-    GNUNET_assert (fsize == GNUNET_DISK_fn_read (fname, ilist, fsize));
-  }
-  for (cnt = 0; cnt < npeers; cnt++)
-  {
-    if (GNUNET_SYSERR == GNUNET_CONTAINER_multipeermap_put (map, &ilist[cnt],
-                                                            &ilist[cnt],
-                                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
-    {
-      cleanup_map ();
-      GNUNET_free (ilist);
-      return;
-    }
-  }
-  shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                                &do_shutdown, NULL);
-  bh = GNUNET_TRANSPORT_blacklist (cfg, &check_access, NULL);
+  if (offset >= num_hostkeys)
+    return GNUNET_SYSERR;
+  (void) memcpy (&private_key,
+                 hostkeys_data + (offset * GNUNET_TESTING_HOSTKEYFILESIZE),
+                 GNUNET_TESTING_HOSTKEYFILESIZE);
+  GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &id->public_key);
+  return GNUNET_OK;
 }
 
 
@@ -239,27 +220,26 @@ setup_ac (const char *fname, const struct GNUNET_CONFIGURATION_Handle *cfg)
 static void
 blacklist_peer (unsigned int offset)
 {
-  struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
   struct GNUNET_PeerIdentity id;
 
-  (void) memcpy (&private_key, &hostkeys[offset], sizeof (private_key));
-  GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &id.public_key);
-  GNUNET_break (GNUNET_OK == 
+  GNUNET_assert (offset < num_hostkeys);
+  GNUNET_assert (GNUNET_OK == get_identity (offset, &id));
+  GNUNET_break (GNUNET_OK ==
                 GNUNET_CONTAINER_multipeermap_put (map, &id, &id,
                                                    GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
-  
+
 }
 
 /**
  * Blacklist peer
  */
-struct ListRow
+struct BlackListRow
 {
   /**
    * Next ptr
    */
-  struct ListRow *next;
-  
+  struct BlackListRow *next;
+
   /**
    * The offset where to find the hostkey for the peer
    */
@@ -268,50 +248,35 @@ struct ListRow
 
 
 /**
- * Function to add a peer to the blacklist
- *
- * @param head the head of the list
- * @param id the id of the peer to add
+ * Whilelist entry
  */
-static void
-listrow_add (struct ListRow *head, unsigned int id)
+struct WhiteListRow
 {
-  struct ListRow *bp;
-                                               
-  bp = GNUNET_new (struct ListRow);
-  bp->id = id;
-  bp->next = head;
-  head = bp;
-}
-
+  /**
+   * Next ptr
+   */
+  struct WhiteListRow *next;
+  
+  /**
+   * The offset where to find the hostkey for the peer
+   */
+  unsigned int id;
+  
+  /**
+   * Bandwidth to be assigned to the link
+   */
+  int bandwidth;
 
-/**
- * Add peers in the blacklist to the blacklist map
- */
-static int
-map_populate (struct ListRow *head,
-              struct GNUNET_CONTAINER_MultiPeerMap *map,
-              const struct GNUNET_PeerIdentity *hostkeys)
-{
-  struct ListRow *row;
-  int ret;
+  /**
+   * Latency to be assigned to the link
+   */
+  int latency;
 
-  while (NULL != (row = head))
-  {
-    if (head->id >= num_hostkeys)
-    {
-      LOG (GNUNET_ERROR_TYPE_WARNING, "Hostkey index %u out of max range %u\n",
-           row->id, num_hostkeys);
-    }
-    head = row->next;
-    ret = GNUNET_CONTAINER_multipeermap_put (blacklist_map, &hostkeys[row->id],
-                                       (void *) &hostkeys[row->id],
-                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
-    if (GNUNET_OK != ret)
-      return GNUNET_SYSERR;
-  }
-  return GNUNET_OK;
-}
+  /**
+   * Loss to be assigned to the link
+   */
+  int loss;
+};
 
 
 /**
@@ -324,7 +289,7 @@ load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
   char *idfile;
   struct GNUNET_DISK_FileHandle *fd;
   uint64_t fsize;
-  
+
   data_dir = NULL;
   idfile = NULL;
   fd = NULL;
@@ -356,31 +321,42 @@ load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
   }
   GNUNET_free (idfile);
   idfile = NULL;
-  hostkeys = (struct GNUNET_PeerIdentity *)
-      GNUNET_DISK_file_map (fd, &idmap, GNUNET_DISK_MAP_TYPE_READ, fsize);
-  if (NULL == hostkeys)
+  hostkeys_data = GNUNET_DISK_file_map (fd,
+                                        &idmap,
+                                        GNUNET_DISK_MAP_TYPE_READ,
+                                        fsize);
+  if (NULL != hostkeys_data)
     num_hostkeys = fsize / GNUNET_TESTING_HOSTKEYFILESIZE;
   return GNUNET_OK;
 }
 
 
+/**
+ * Function to read blacklist rows from the database
+ *
+ * @param db the database connection
+ * @param pid the identity of this peer
+ * @param bl_rows where to store the retrieved blacklist rows
+ * @return GNUNET_SYSERR upon error OR the number of rows retrieved
+ */
 static int
-db_read_blacklist (sqlite3 *dbfile, unsigned int pid, struct ListRow **blacklist_rows)
+db_read_blacklist (struct sqlite3 *db, unsigned int pid, struct BlackListRow **bl_rows)
 {
-  static const char *query_bl = "SELECT (id, oid) FROM blacklist WHERE (id == ?);";
-  static struct sqlite3_stmt *stmt_bl;
+  static const char *query_bl = "SELECT (oid) FROM blacklist WHERE (id == ?);";
+  struct sqlite3_stmt *stmt_bl;
+  struct BlackListRow *lr;
   int nrows;
   int peer_id;
   int ret;
 
   if (SQLITE_OK != (ret = sqlite3_prepare_v2 (db, query_bl, -1, &stmt_bl, NULL)))
   {
-    LOG_SQLITE_ERROR (ret);
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
     return GNUNET_SYSERR;
   }
   if (SQLITE_OK != (ret = sqlite3_bind_int (stmt_bl, 1, pid)))
   {
-    LOG_SQLITE_ERROR (ret);
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
     sqlite3_finalize (stmt_bl);
     return GNUNET_SYSERR;
   }
@@ -391,7 +367,10 @@ db_read_blacklist (sqlite3 *dbfile, unsigned int pid, struct ListRow **blacklist
     if (SQLITE_ROW != ret)
       break;
     peer_id = sqlite3_column_int (stmt_bl, 1);
-    listrow_add (*blacklist_rows, peer_id);
+    lr = GNUNET_new (struct BlackListRow);
+    lr->id = peer_id;
+    lr->next = *bl_rows;
+    *bl_rows = lr;
     nrows++;
   } while (1);
   sqlite3_finalize (stmt_bl);
@@ -400,6 +379,54 @@ db_read_blacklist (sqlite3 *dbfile, unsigned int pid, struct ListRow **blacklist
 }
 
 
+/**
+ * Function to read whitelist rows from the database
+ *
+ * @param db the database connection
+ * @param pid the identity of this peer
+ * @param wl_rows where to store the retrieved whitelist rows
+ * @return GNUNET_SYSERR upon error OR the number of rows retrieved
+ */
+static int
+db_read_whitelist (struct sqlite3 *db, unsigned int pid, struct WhiteListRow **wl_rows)
+{
+  static const char *query_wl = "SELECT (oid, bandwidth, latency, loss) FROM whitelist WHERE (id == ?);";
+  struct sqlite3_stmt *stmt_wl;
+  struct WhiteListRow *lr;
+  int nrows;
+  int ret;
+  
+  if (SQLITE_OK != (ret = sqlite3_prepare_v2 (db, query_wl, -1, &stmt_wl, NULL)))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
+    return GNUNET_SYSERR;
+  }
+  if (SQLITE_OK != (ret = sqlite3_bind_int (stmt_wl, 1, pid)))
+  {
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
+    sqlite3_finalize (stmt_wl);
+    return GNUNET_SYSERR;
+  }
+  nrows = 0;
+  do
+  {
+    ret = sqlite3_step (stmt_wl);
+    if (SQLITE_ROW != ret)
+      break;
+    nrows++;
+    lr = GNUNET_new (struct WhiteListRow);
+    lr->id = sqlite3_column_int (stmt_wl, 1);
+    lr->bandwidth = sqlite3_column_int (stmt_wl, 2);
+    lr->latency = sqlite3_column_int (stmt_wl, 3);
+    lr->loss = sqlite3_column_int (stmt_wl, 4);
+    lr->next = *wl_rows;
+    *wl_rows = lr;
+  } while (1);
+  sqlite3_finalize (stmt_wl);
+  return nrows;
+}
+
+
 /**
  * Main function that will be run.
  *
@@ -413,17 +440,28 @@ run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   char *dbfile;
-  struct ListRow *blacklist_rows;
+  struct BlackListRow *bl_head;
+  struct BlackListRow *bl_entry;
+  struct WhiteListRow *wl_head;
+  struct WhiteListRow *wl_entry;
+  struct GNUNET_PeerIdentity identity;
+  struct GNUNET_ATS_Information triplet[3];
   unsigned long long pid;
   unsigned int nrows;
   int ret;
-  
+
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (c, "TESTBED",
                                                             "PEERID", &pid))
   {
     GNUNET_break (0);
     return;
   }
+  transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
+  if (NULL == transport)
+  {
+    GNUNET_break (0);
+    return;
+  }
   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED",
                                                             "UNDERLAY_DB",
                                                             &dbfile))
@@ -433,29 +471,72 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   if (SQLITE_OK != (ret = sqlite3_open_v2 (dbfile, &db, SQLITE_OPEN_READONLY, NULL)))
   {
-    LOG_SQLITE_ERROR (ret);
+    if (NULL != db)
+    {
+      LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
+      sqlite3_close (db);
+    }
+    else
+      LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
     GNUNET_free (dbfile);
     return;
   }
   DEBUG ("Opened database %s\n", dbfile);
   GNUNET_free (dbfile);
   dbfile = NULL;
-  blacklist_rows = NULL;
-  nrows = db_read_blacklist (db, pid, &blacklist_rows);
-  if (-1 == nrows)
+  bl_head = NULL;
+  wl_head = NULL;
+  nrows = db_read_blacklist (db, pid, &bl_head);
+  if (GNUNET_SYSERR == nrows)
     goto close_db;
   if (nrows > 0)
   {
     blacklist_map = GNUNET_CONTAINER_multipeermap_create (nrows, GNUNET_YES);
     if (GNUNET_OK != load_keys (c))
-    {
       goto close_db;
-    }
   }
-  /* process whitelist */
-  GNUNET_break (0);             /* TODO */
+  while (NULL != (bl_entry = bl_head))
+  {
+    bl_head = bl_entry->next;
+    blacklist_peer (bl_entry->id);
+    GNUNET_free (bl_entry);
+  }
+  if (NULL != blacklist_map)
+  {
+    bh = GNUNET_TRANSPORT_blacklist (c, &check_access, NULL);
+    shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                                                  &do_shutdown, NULL);
+  }
+  /* read and process whitelist */
+  nrows = 0;
+  wl_head = NULL;
+  nrows = db_read_whitelist (db, pid, &wl_head);
+  if ((GNUNET_SYSERR == nrows) || (0 == nrows))
+    goto close_db;
+  triplet[0].type = 0; //FIXME: not implemented: GNUNET_ATS_QUALITY_NET_THROUGHPUT
+  triplet[1].type = GNUNET_ATS_QUALITY_NET_DELAY;
+  triplet[2].type =  0; //FIXME: not implemented: GNUNET_ATS_QUALITY_NET_LOSSRATE;
+  while (NULL != (wl_entry = wl_head))
+  {
+    wl_head = wl_entry->next;
+    triplet[0].value = wl_entry->bandwidth; //FIXME: bandwidth != throughput !!
+    triplet[1].value = wl_entry->latency;
+    triplet[2].value = wl_entry->loss;
+    GNUNET_assert (GNUNET_OK == get_identity (wl_entry->id, &identity));
+    GNUNET_TRANSPORT_set_traffic_metric (transport,
+                                         &identity,
+                                         GNUNET_YES,
+                                         GNUNET_YES, /* FIXME: Separate inbound, outboud metrics */
+                                         triplet, 3);
+    GNUNET_free (wl_entry);
+  }
 
  close_db:
+  while (NULL != (bl_entry = bl_head))
+  {
+    bl_head = bl_entry->next;
+    GNUNET_free (bl_entry);
+  }
   GNUNET_break (GNUNET_OK == sqlite3_close (db));
   return;
 }
@@ -478,7 +559,9 @@ main (int argc, char *const *argv)
 
   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
     return 2;
+#ifdef SQLITE_CONFIG_MMAP_SIZE
   (void) sqlite3_config (SQLITE_CONFIG_MMAP_SIZE, 512000, 256000000);
+#endif
   ret =
       (GNUNET_OK ==
        GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-testbed-underlay",