/*
This file is part of GNUnet
- (C) 2008--2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2008--2013 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
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., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
} while(0)
-/**
- * Allow access from the peers read from the whitelist
- */
-#define ACCESS_ALLOW 1
-
-/**
- * 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
*/
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
*/
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;
-
/**
* @ingroup hashmap
}
-/**
- * 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.
*
{
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;
}
}
-/**
- * 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
*/
* 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;
};
{
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);
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);
}
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 (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
- 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 (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_bind_int");
- 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
+ */
+static void
+do_shutdown (void *cls)
+{
+ 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);
}
* @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 (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_prepare_v2");
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);
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);
if (NULL != db)
{
LOG_SQLITE (db, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_open_v2");
- sqlite3_close (db);
+ GNUNET_break (SQLITE_OK == sqlite3_close (db));
}
else
LOG (GNUNET_ERROR_TYPE_ERROR, "Cannot open sqlite file %s\n", dbfile);
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);
+ GNUNET_SCHEDULER_add_shutdown (&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));
- return;
+ GNUNET_break (SQLITE_OK == sqlite3_close (db));
}
#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;