-more datacache integration work
[oweals/gnunet.git] / src / testbed / gnunet-daemon-testbed-underlay.c
index 8cdfe444071dec2668953ec5a204279226ce3810..e41cf4a4db13ee0fe06db4a519d0b21328a2b769 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2013 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2008--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
 #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))
-
-
 /**
- * Allow access from the peers read from the whitelist
+ * 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 ACCESS_ALLOW 1
+#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)
 
-/**
- * Deny access from the peers read from the blacklist
- */
-#define ACCESS_DENY 0
 
 /**
  * The map to store the peer identities to allow/deny
  */
 static struct GNUNET_CONTAINER_MultiPeerMap *map;
 
-
-/**
- * The map to store the peer identities to allow/deny
- */
-static struct GNUNET_CONTAINER_MultiPeerMap *blacklist_map;
-
 /**
  * The database connection
  */
@@ -83,14 +77,19 @@ static struct sqlite3 *db;
 struct GNUNET_TRANSPORT_Blacklist *bh;
 
 /**
- * The peer ID map
+ * The hostkeys file
+ */
+struct GNUNET_DISK_FileHandle *hostkeys_fd;
+
+/**
+ * The hostkeys map
  */
-static struct GNUNET_DISK_MapHandle *idmap;
+static struct GNUNET_DISK_MapHandle *hostkeys_map;
 
 /**
  * The hostkeys data
  */
-static char *hostkeys_data;
+static void *hostkeys_data;
 
 /**
  * Handle to the transport service.  This is used for setting link metrics
@@ -105,12 +104,7 @@ static unsigned int num_hostkeys;
 /**
  * Task for shutdown
  */
-static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
-
-/**
- * Are we allowing or denying access from peers
- */
-static int mode;
+static struct GNUNET_SCHEDULER_Task * shutdown_task;
 
 
 /**
@@ -150,21 +144,6 @@ cleanup_map ()
 }
 
 
-/**
- * Shutdown task to cleanup our resources and exit.
- *
- * @param cls NULL
- * @param tc scheduler task context
- */
-static void
-do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
-{
-  cleanup_map ();
-  if (NULL != bh)
-    GNUNET_TRANSPORT_blacklist_cancel (bh);
-}
-
-
 /**
  * Function that decides if a connection is acceptable or not.
  *
@@ -177,13 +156,15 @@ check_access (void *cls, const struct GNUNET_PeerIdentity * pid)
 {
   int contains;
 
-  if (NULL != map)
-    contains = GNUNET_CONTAINER_multipeermap_contains (map, pid);
-  else
-    contains = GNUNET_NO;
-  if (ACCESS_DENY == mode)
-    return (contains) ? GNUNET_SYSERR : GNUNET_OK;
-  return (contains) ? GNUNET_OK : GNUNET_SYSERR;
+  GNUNET_assert (NULL != map);
+  contains = GNUNET_CONTAINER_multipeermap_contains (map, pid);
+  if (GNUNET_YES == contains)
+  {
+    DEBUG ("Permitting `%s'\n", GNUNET_i2s (pid));
+    return GNUNET_OK;
+  }
+  DEBUG ("Not permitting `%s'\n", GNUNET_i2s (pid));
+  return GNUNET_SYSERR;
 }
 
 
@@ -202,41 +183,6 @@ get_identity (unsigned int offset, struct GNUNET_PeerIdentity *id)
 }
 
 
-/**
- * Function to blacklist a peer
- *
- * @param offset the offset where to find the peer's hostkey in the array of hostkeys
- */
-static void
-blacklist_peer (unsigned int offset)
-{
-  struct GNUNET_PeerIdentity id;
-
-  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 BlackListRow
-{
-  /**
-   * Next ptr
-   */
-  struct BlackListRow *next;
-
-  /**
-   * The offset where to find the hostkey for the peer
-   */
-  unsigned int id;
-};
-
-
 /**
  * Whilelist entry
  */
@@ -246,26 +192,17 @@ struct WhiteListRow
    * 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;
 
   /**
    * Latency to be assigned to the link
    */
   int latency;
 
-  /**
-   * Loss to be assigned to the link
-   */
-  int loss;
 };
 
 
@@ -277,12 +214,10 @@ load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
 {
   char *data_dir;
   char *idfile;
-  struct GNUNET_DISK_FileHandle *fd;
   uint64_t fsize;
 
   data_dir = NULL;
   idfile = NULL;
-  fd = NULL;
   fsize = 0;
   data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
   GNUNET_asprintf (&idfile, "%s/testing_hostkeys.ecc", data_dir);
@@ -301,9 +236,9 @@ load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
     GNUNET_free (idfile);
     return GNUNET_SYSERR;
   }
-  fd = GNUNET_DISK_file_open (idfile, GNUNET_DISK_OPEN_READ,
-                              GNUNET_DISK_PERM_NONE);
-  if (NULL == fd)
+  hostkeys_fd = GNUNET_DISK_file_open (idfile, GNUNET_DISK_OPEN_READ,
+                                       GNUNET_DISK_PERM_NONE);
+  if (NULL == hostkeys_fd)
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", idfile);
     GNUNET_free (idfile);
@@ -311,61 +246,60 @@ load_keys (const struct GNUNET_CONFIGURATION_Handle *c)
   }
   GNUNET_free (idfile);
   idfile = NULL;
-  hostkeys_data = GNUNET_DISK_file_map (fd,
-                                        &idmap,
+  hostkeys_data = GNUNET_DISK_file_map (hostkeys_fd,
+                                        &hostkeys_map,
                                         GNUNET_DISK_MAP_TYPE_READ,
                                         fsize);
-  if (NULL != hostkeys_data)
-    num_hostkeys = fsize / GNUNET_TESTING_HOSTKEYFILESIZE;
+  if (NULL == hostkeys_data)
+  {
+
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "mmap");
+    return GNUNET_SYSERR;
+  }
+  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
+ * Function to unload keys
  */
-static int
-db_read_blacklist (struct sqlite3 *db, unsigned int pid, struct BlackListRow **bl_rows)
+static void
+unload_keys ()
 {
-  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)))
+  if (NULL != hostkeys_map)
   {
-    LOG_SQLITE_ERROR (ret);
-    return GNUNET_SYSERR;
+    GNUNET_assert (NULL != hostkeys_data);
+    GNUNET_DISK_file_unmap (hostkeys_map);
+    hostkeys_map = NULL;
+    hostkeys_data = NULL;
   }
-  if (SQLITE_OK != (ret = sqlite3_bind_int (stmt_bl, 1, pid)))
+  if (NULL != hostkeys_fd)
   {
-    LOG_SQLITE_ERROR (ret);
-    sqlite3_finalize (stmt_bl);
-    return GNUNET_SYSERR;
+    GNUNET_DISK_file_close (hostkeys_fd);
+    hostkeys_fd = NULL;
   }
-  nrows = 0;
-  do
+}
+
+
+/**
+ * Shutdown task to cleanup our resources and exit.
+ *
+ * @param cls NULL
+ * @param tc scheduler task context
+ */
+static void
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (NULL != transport)
   {
-    ret = sqlite3_step (stmt_bl);
-    if (SQLITE_ROW != ret)
-      break;
-    peer_id = sqlite3_column_int (stmt_bl, 1);
-    lr = GNUNET_new (struct BlackListRow);
-    lr->id = peer_id;
-    lr->next = *bl_rows;
-    *bl_rows = lr;
-    nrows++;
-  } while (1);
-  sqlite3_finalize (stmt_bl);
-  stmt_bl = NULL;
-  return nrows;
+    GNUNET_TRANSPORT_disconnect (transport);
+    transport = NULL;
+  }
+  cleanup_map ();
+  unload_keys ();
+  if (NULL != bh)
+    GNUNET_TRANSPORT_blacklist_cancel (bh);
 }
 
 
@@ -378,22 +312,22 @@ db_read_blacklist (struct sqlite3 *db, unsigned int pid, struct BlackListRow **b
  * @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)
+db_read_whitelist (struct sqlite3 *db, int pid, struct WhiteListRow **wl_rows)
 {
-  static const char *query_wl = "SELECT (oid, bandwidth, latency, loss) FROM whitelist WHERE (id == ?);";
+  static const char *query_wl = "SELECT oid, latency 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_ERROR (ret);
+    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_ERROR (ret);
+    LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
     sqlite3_finalize (stmt_wl);
     return GNUNET_SYSERR;
   }
@@ -405,10 +339,8 @@ db_read_whitelist (struct sqlite3 *db, unsigned int pid, struct WhiteListRow **w
       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->id = sqlite3_column_int (stmt_wl, 0);
+    lr->latency = sqlite3_column_int (stmt_wl, 1);
     lr->next = *wl_rows;
     *wl_rows = lr;
   } while (1);
@@ -430,30 +362,24 @@ run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 {
   char *dbfile;
-  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];
+  struct GNUNET_ATS_Properties prop;
+  struct GNUNET_TIME_Relative delay;
   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)
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_get_value_number (c, "TESTBED",
+                                             "PEERID", &pid))
   {
     GNUNET_break (0);
     return;
   }
-  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED",
-                                                            "UNDERLAY_DB",
+  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (c, "TESTBED-UNDERLAY",
+                                                            "DBFILE",
                                                             &dbfile))
   {
     GNUNET_break (0);
@@ -461,67 +387,64 @@ 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");
+      GNUNET_break (SQLITE_OK == 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;
-  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))
+  if (GNUNET_OK != load_keys (c))
       goto close_db;
-  }
-  while (NULL != (bl_entry = bl_head))
-  {
-    bl_head = bl_entry->next;
-    blacklist_peer (bl_entry->id);
-    GNUNET_free (bl_entry);
-  }
-  if (NULL != blacklist_map)
+
+  transport = GNUNET_TRANSPORT_connect (c, NULL, NULL, NULL, NULL, NULL);
+  if (NULL == transport)
   {
-    bh = GNUNET_TRANSPORT_blacklist (c, &check_access, NULL);
-    shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
-                                                  &do_shutdown, NULL);
+    GNUNET_break (0);
+    return;
   }
   /* read and process whitelist */
   nrows = 0;
   wl_head = NULL;
   nrows = db_read_whitelist (db, pid, &wl_head);
   if ((GNUNET_SYSERR == nrows) || (0 == nrows))
+  {
+    GNUNET_TRANSPORT_disconnect (transport);
     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;
+  }
+  map = GNUNET_CONTAINER_multipeermap_create (nrows, GNUNET_NO);
   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;
+    delay.rel_value_us = wl_entry->latency;
+    memset (&prop, 0, sizeof (prop));
     GNUNET_assert (GNUNET_OK == get_identity (wl_entry->id, &identity));
+    GNUNET_break (GNUNET_OK ==
+                  GNUNET_CONTAINER_multipeermap_put (map, &identity, &identity,
+                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
+    DEBUG ("Setting %u ms latency to peer `%s'\n",
+           wl_entry->latency,
+           GNUNET_i2s (&identity));
     GNUNET_TRANSPORT_set_traffic_metric (transport,
                                          &identity,
-                                         GNUNET_YES,
-                                         GNUNET_YES, /* FIXME: Separate inbound, outboud metrics */
-                                         triplet, 3);
+                                         &prop,
+                                         delay,
+                                         delay);
     GNUNET_free (wl_entry);
   }
+  bh = GNUNET_TRANSPORT_blacklist (c, &check_access, NULL);
+  shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
+                                                &do_shutdown, NULL);
 
  close_db:
-  while (NULL != (bl_entry = bl_head))
-  {
-    bl_head = bl_entry->next;
-    GNUNET_free (bl_entry);
-  }
-  GNUNET_break (GNUNET_OK == sqlite3_close (db));
+  GNUNET_break (SQLITE_OK == sqlite3_close (db));
   return;
 }
 
@@ -548,7 +471,7 @@ main (int argc, char *const *argv)
 #endif
   ret =
       (GNUNET_OK ==
-       GNUNET_PROGRAM_run (argc, argv, "gnunet-daemon-testbed-underlay",
+       GNUNET_PROGRAM_run (argc, argv, "testbed-underlay",
                            _
                            ("Daemon to restrict underlay network in testbed deployments"),
                            options, &run, NULL)) ? 0 : 1;