#define VERBOSE_TESTING GNUNET_NO
+#define DEBUG_CHURN GNUNET_NO
+
/**
* Lowest port used for GNUnet testing. Should be high enough to not
* conflict with other applications running on the hosts but be low
#define MAX_OUTSTANDING_CONNECTIONS 50
-#define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 160)
+#define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
#define CONNECT_ATTEMPTS 8
typedef int (*GNUNET_TESTING_ConnectionProcessor)
(struct GNUNET_TESTING_PeerGroup *pg, unsigned int first, unsigned int second);
+/**
+ * Context for handling churning a peer group
+ */
+struct ChurnContext
+{
+ /**
+ * Callback used to notify of churning finished
+ */
+ GNUNET_TESTING_NotifyCompletion cb;
+
+ /**
+ * Closure for callback
+ */
+ void *cb_cls;
+
+ /**
+ * Number of peers that still need to be started
+ */
+ unsigned int num_to_start;
+
+ /**
+ * Number of peers that still need to be stopped
+ */
+ unsigned int num_to_stop;
+
+ /**
+ * Number of peers that failed to start
+ */
+ unsigned int num_failed_start;
+
+ /**
+ * Number of peers that failed to stop
+ */
+ unsigned int num_failed_stop;
+};
+
struct RestartContext
{
/**
return GNUNET_YES;
}
+struct BlacklistContext
+{
+ /*
+ * The (open) file handle to write to
+ */
+ FILE *temp_file_handle;
+
+ /*
+ * The transport that this peer will be blacklisted on.
+ */
+ char *transport;
+};
/**
* Iterator for writing blacklist data to appropriate files.
const GNUNET_HashCode * key,
void *value)
{
- FILE *temp_blacklist_handle = cls;
+ struct BlacklistContext *blacklist_ctx = cls;
+ //FILE *temp_blacklist_handle = cls;
struct GNUNET_TESTING_Daemon *peer = value;
struct GNUNET_PeerIdentity *temppeer;
struct GNUNET_CRYPTO_HashAsciiEncoded peer_enc;
temppeer = &peer->id;
GNUNET_CRYPTO_hash_to_enc(&temppeer->hashPubKey, &peer_enc);
- fprintf(temp_blacklist_handle, "tcp:%s\n", (char *)&peer_enc);
+ fprintf(blacklist_ctx->temp_file_handle, "%s:%s\n", blacklist_ctx->transport, (char *)&peer_enc);
return GNUNET_YES;
}
count++;
if (ret == GNUNET_SYSERR)
{
+ /* FIXME: why sleep here? -CG */
sleep(1);
}
}
* to the appropriate place.
*
* @param pg the peer group we are dealing with
+ * @param transports space delimited list of transports to blacklist
*/
static int
-create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg)
+create_and_copy_blacklist_files (struct GNUNET_TESTING_PeerGroup *pg, char *transports)
{
- FILE *temp_friend_handle;
+ FILE *temp_file_handle;
+ static struct BlacklistContext blacklist_ctx;
unsigned int pg_iter;
char *temp_service_path;
pid_t *pidarr;
int count;
int ret;
int max_wait = 10;
+ int transport_len;
+ unsigned int i;
+ char *pos;
+ char *temp_transports;
pidarr = GNUNET_malloc(sizeof(pid_t) * pg->total);
for (pg_iter = 0; pg_iter < pg->total; pg_iter++)
{
mytemp = GNUNET_DISK_mktemp("blacklist");
GNUNET_assert(mytemp != NULL);
- temp_friend_handle = fopen (mytemp, "wt");
- GNUNET_assert(temp_friend_handle != NULL);
- GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, temp_friend_handle);
- fclose(temp_friend_handle);
+ temp_file_handle = fopen (mytemp, "wt");
+ GNUNET_assert(temp_file_handle != NULL);
+ temp_transports = GNUNET_strdup(transports);
+ blacklist_ctx.temp_file_handle = temp_file_handle;
+ transport_len = strlen(temp_transports) + 1;
+ pos = NULL;
+
+ for (i = 0; i < transport_len; i++)
+ {
+ if ((temp_transports[i] == ' ') && (pos == NULL))
+ continue; /* At start of string (whitespace) */
+ else if ((temp_transports[i] == ' ') || (temp_transports[i] == '\0')) /* At end of string */
+ {
+ temp_transports[i] = '\0';
+ blacklist_ctx.transport = pos;
+ GNUNET_CONTAINER_multihashmap_iterate(pg->peers[pg_iter].blacklisted_peers, &blacklist_file_iterator, &blacklist_ctx);
+ pos = NULL;
+ } /* At beginning of actual string */
+ else if (pos == NULL)
+ {
+ pos = &temp_transports[i];
+ }
+ }
+
+ GNUNET_free_non_null(temp_transports);
+ fclose(temp_file_handle);
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_string(pg->peers[pg_iter].daemon->cfg, "PATHS", "SERVICEHOME", &temp_service_path))
count++;
if (ret == GNUNET_SYSERR)
{
+ /* FIXME: why sleep here? -CG */
sleep(1);
}
}
}
-/*
+/**
* Connect the topology as specified by the PeerConnection's
* of each peer in the peer group
*
* @param pg the peer group we are dealing with
- *
* @return the number of connections that will be attempted
*/
static int
}
-/*
+/**
* Takes a peer group and creates a topology based on the
* one specified. Creates a topology means generates friend
* files for the peers so they can only connect to those allowed
* @param topology which topology to connect the peers in
* @param restrict_topology allow only direct TCP connections in this topology
* use GNUNET_TESTING_TOPOLOGY_NONE for no restrictions
+ * @param restrict_transports space delimited list of transports to blacklist
+ * to create restricted topology
*
* @return the maximum number of connections were all allowed peers
* connected to each other
int
GNUNET_TESTING_create_topology (struct GNUNET_TESTING_PeerGroup *pg,
enum GNUNET_TESTING_Topology topology,
- enum GNUNET_TESTING_Topology restrict_topology)
+ enum GNUNET_TESTING_Topology restrict_topology,
+ char *restrict_transports)
{
int ret;
int num_connections;
#endif
}
- /**
- * Use the create clique method to initially set all connections
- * as blacklisted.
- */
+ /* Use the create clique method to initially set all connections as blacklisted. */
create_clique (pg, &blacklist_connections);
unblacklisted_connections = 0;
- /**
- * Un-blacklist connections as per the topology specified
- */
+ /* Un-blacklist connections as per the topology specified */
switch (restrict_topology)
{
case GNUNET_TESTING_TOPOLOGY_CLIQUE:
break;
}
- if (unblacklisted_connections > 0)
+ if ((unblacklisted_connections > 0) && (restrict_transports != NULL))
{
- ret = create_and_copy_blacklist_files(pg);
+ ret = create_and_copy_blacklist_files(pg, restrict_transports);
if (ret != GNUNET_OK)
{
#if VERBOSE_TESTING
#endif
}
}
-
-
return num_connections;
}
}
-/*
- * @param pg the peer group struct representing the running peers
- * @param topology which topology to connect the peers in
- * @param options options for connecting the topology
- * @param option_modifier modifier for options that take a parameter
- *
+/**
* There are many ways to connect peers that are supported by this function.
* To connect peers in the same topology that was created via the
* GNUNET_TESTING_create_topology, the topology variable must be set to
* connected. This could result in some connections being impossible,
* because some topologies are non-deterministic.
*
+ * @param pg the peer group struct representing the running peers
+ * @param topology which topology to connect the peers in
+ * @param options options for connecting the topology
+ * @param option_modifier modifier for options that take a parameter
+ * @return the number of connections that will be attempted, GNUNET_SYSERR on error
*/
int
GNUNET_TESTING_connect_topology (struct GNUNET_TESTING_PeerGroup *pg,
enum GNUNET_TESTING_TopologyOption options,
double option_modifier)
{
- int num_connections;
-
switch (topology)
{
case GNUNET_TESTING_TOPOLOGY_CLIQUE:
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating clique topology\n"));
#endif
- num_connections = create_clique (pg, &add_actual_connections);
+ create_clique (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD_RING:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating small world (ring) topology\n"));
#endif
- num_connections = create_small_world_ring (pg, &add_actual_connections);
+ create_small_world_ring (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_SMALL_WORLD:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating small world (2d-torus) topology\n"));
#endif
- num_connections = create_small_world (pg, &add_actual_connections);
+ create_small_world (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_RING:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating ring topology\n"));
#endif
- num_connections = create_ring (pg, &add_actual_connections);
+ create_ring (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_2D_TORUS:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating 2d torus topology\n"));
#endif
- num_connections = create_2d_torus (pg, &add_actual_connections);
+ create_2d_torus (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_ERDOS_RENYI:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating Erdos-Renyi topology\n"));
#endif
- num_connections = create_erdos_renyi (pg, &add_actual_connections);
+ create_erdos_renyi (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_INTERNAT:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating InterNAT topology\n"));
#endif
- num_connections = create_nated_internet (pg, &add_actual_connections);
+ create_nated_internet (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_SCALE_FREE:
#if VERBOSE_TESTING
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
_("Creating Scale Free topology\n"));
#endif
- num_connections = create_scale_free (pg, &add_actual_connections);
+ create_scale_free (pg, &add_actual_connections);
break;
case GNUNET_TESTING_TOPOLOGY_NONE:
- num_connections = copy_allowed_topology(pg);
+ copy_allowed_topology(pg);
break;
default:
- GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Unknown topology specification, can't connect peers!\n");
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
+ _("Unknown topology specification, can't connect peers!\n"));
return GNUNET_SYSERR;
}
switch (options)
{
- case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM: /* Create a random subset of total connections based on parameter */
+ case GNUNET_TESTING_TOPOLOGY_OPTION_RANDOM:
choose_random_connections(pg, option_modifier);
break;
- case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM: /* Create at least X connections per peer (if possible!) */
+ case GNUNET_TESTING_TOPOLOGY_OPTION_MINIMUM:
choose_minimum(pg, (unsigned int)option_modifier);
break;
- case GNUNET_TESTING_TOPOLOGY_OPTION_DFS: /* Choose a random starting point, randomly walk graph, try to get each peer X connections */
+ case GNUNET_TESTING_TOPOLOGY_OPTION_DFS:
perform_dfs(pg, (int)option_modifier);
break;
case GNUNET_TESTING_TOPOLOGY_OPTION_NONE:
- /* Fall through */
+ break;
case GNUNET_TESTING_TOPOLOGY_OPTION_ALL:
- /* Fall through */
+ break;
default:
break;
}
}
+/**
+ * Callback for informing us about a successful
+ * or unsuccessful churn stop call.
+ *
+ * @param cls a ChurnContext
+ * @param emsg NULL on success, non-NULL on failure
+ *
+ */
+void
+churn_stop_callback (void *cls, const char *emsg)
+{
+ struct ChurnContext *churn_ctx = cls;
+ unsigned int total_left;
+ char *error_message;
+
+ error_message = NULL;
+ if (emsg != NULL)
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Churn stop callback failed with error `%s'\n", emsg);
+ churn_ctx->num_failed_stop++;
+ }
+ else
+ {
+ churn_ctx->num_to_stop--;
+ }
+
+#if DEBUG_CHURN
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopped peer, %d left.\n", churn_ctx->num_to_stop);
+#endif
+ total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
+
+ if (total_left == 0)
+ {
+ if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
+ {
+ GNUNET_asprintf(&error_message, "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!", churn_ctx->num_failed_start, churn_ctx->num_failed_stop);
+ }
+ churn_ctx->cb(churn_ctx->cb_cls, error_message);
+ GNUNET_free_non_null(error_message);
+ GNUNET_free(churn_ctx);
+ }
+}
+
+/**
+ * Callback for informing us about a successful
+ * or unsuccessful churn start call.
+ *
+ * @param cls a ChurnContext
+ * @param id the peer identity of the started peer
+ * @param cfg the handle to the configuration of the peer
+ * @param d handle to the daemon for the peer
+ * @param emsg NULL on success, non-NULL on failure
+ *
+ */
+void
+churn_start_callback (void *cls,
+ const struct GNUNET_PeerIdentity *id,
+ const struct GNUNET_CONFIGURATION_Handle *cfg,
+ struct GNUNET_TESTING_Daemon *d,
+ const char *emsg)
+{
+ struct ChurnContext *churn_ctx = cls;
+ unsigned int total_left;
+ char *error_message;
+
+ error_message = NULL;
+ if (emsg != NULL)
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Churn stop callback failed with error `%s'\n", emsg);
+ churn_ctx->num_failed_start++;
+ }
+ else
+ {
+ churn_ctx->num_to_start--;
+ }
+
+#if DEBUG_CHURN
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Started peer, %d left.\n", churn_ctx->num_to_start);
+#endif
+
+ total_left = (churn_ctx->num_to_stop - churn_ctx->num_failed_stop) + (churn_ctx->num_to_start - churn_ctx->num_failed_start);
+
+ if (total_left == 0)
+ {
+ if ((churn_ctx->num_failed_stop > 0) || (churn_ctx->num_failed_start > 0))
+ GNUNET_asprintf(&error_message, "Churn didn't complete successfully, %u peers failed to start %u peers failed to be stopped!", churn_ctx->num_failed_start, churn_ctx->num_failed_stop);
+ churn_ctx->cb(churn_ctx->cb_cls, error_message);
+ GNUNET_free_non_null(error_message);
+ GNUNET_free(churn_ctx);
+ }
+
+}
+
+/**
+ * Simulate churn by stopping some peers (and possibly
+ * re-starting others if churn is called multiple times). This
+ * function can only be used to create leave-join churn (peers "never"
+ * leave for good). First "voff" random peers that are currently
+ * online will be taken offline; then "von" random peers that are then
+ * offline will be put back online. No notifications will be
+ * generated for any of these operations except for the callback upon
+ * completion.
+ *
+ * @param pg handle for the peer group
+ * @param voff number of peers that should go offline
+ * @param von number of peers that should come back online;
+ * must be zero on first call (since "testbed_start"
+ * always starts all of the peers)
+ * @param timeout how long to wait for operations to finish before
+ * giving up
+ * @param cb function to call at the end
+ * @param cb_cls closure for cb
+ */
+void
+GNUNET_TESTING_daemons_churn (struct GNUNET_TESTING_PeerGroup *pg,
+ unsigned int voff,
+ unsigned int von,
+ struct GNUNET_TIME_Relative timeout,
+ GNUNET_TESTING_NotifyCompletion cb,
+ void *cb_cls)
+{
+ struct ChurnContext *churn_ctx;
+ unsigned int running;
+ unsigned int stopped;
+ unsigned int i;
+ unsigned int *running_arr;
+ unsigned int *stopped_arr;
+ unsigned int *running_permute;
+ unsigned int *stopped_permute;
+
+ running = 0;
+ stopped = 0;
+
+ if ((von == 0) && (voff == 0)) /* No peers at all? */
+ {
+ cb(cb_cls, NULL);
+ return;
+ }
+
+ for (i = 0; i < pg->total; i++)
+ {
+ if (pg->peers[i].daemon->running == GNUNET_YES)
+ {
+ running++;
+ }
+ else
+ {
+ stopped++;
+ }
+ }
+
+ if (voff > running)
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to stop more peers than are currently running!\n");
+ cb(cb_cls, "Trying to stop more peers than are currently running!");
+ return;
+ }
+
+ if (von > stopped)
+ {
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Trying to start more peers than are currently stopped!\n");
+ cb(cb_cls, "Trying to start more peers than are currently stopped!");
+ return;
+ }
+
+ churn_ctx = GNUNET_malloc(sizeof(struct ChurnContext));
+ running_arr = GNUNET_malloc(running * sizeof(unsigned int));
+ stopped_arr = GNUNET_malloc(stopped * sizeof(unsigned int));
+
+ running_permute = NULL;
+ stopped_permute = NULL;
+
+ if (running > 0)
+ running_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, running);
+ if (stopped > 0)
+ stopped_permute = GNUNET_CRYPTO_random_permute(GNUNET_CRYPTO_QUALITY_WEAK, stopped);
+
+ running = 0;
+ stopped = 0;
+
+ churn_ctx->num_to_start = von;
+ churn_ctx->num_to_stop = voff;
+ churn_ctx->cb = cb;
+ churn_ctx->cb_cls = cb_cls;
+
+ for (i = 0; i < pg->total; i++)
+ {
+ if (pg->peers[i].daemon->running == GNUNET_YES)
+ {
+ running_arr[running] = i;
+ running++;
+ }
+ else
+ {
+ stopped_arr[stopped] = i;
+ stopped++;
+ }
+ }
+
+ for (i = 0; i < voff; i++)
+ {
+#if DEBUG_CHURN
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Stopping peer %d!\n", running_permute[i]);
+#endif
+ GNUNET_TESTING_daemon_stop(pg->peers[running_arr[running_permute[i]]].daemon, timeout, &churn_stop_callback, churn_ctx, GNUNET_NO, GNUNET_YES);
+ }
+
+ for (i = 0; i < von; i++)
+ {
+#if DEBUG_CHURN
+ GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Starting up peer %d!\n", stopped_permute[i]);
+#endif
+ GNUNET_TESTING_daemon_start_stopped(pg->peers[stopped_arr[stopped_permute[i]]].daemon, timeout, &churn_start_callback, churn_ctx);
+ }
+
+ GNUNET_free(running_arr);
+ GNUNET_free(stopped_arr);
+ GNUNET_free_non_null(running_permute);
+ GNUNET_free_non_null(stopped_permute);
+}
+
+
/**
* Restart all peers in the given group.
*
* @param pg the handle to the peer group
- * @param timeout how long to wait on failure
* @param callback function to call on completion (or failure)
* @param callback_cls closure for the callback function
*/
as well... */
if (NULL != pg->peers[off].daemon)
- GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES);
+ GNUNET_TESTING_daemon_stop (pg->peers[off].daemon, timeout, NULL, NULL, GNUNET_YES, GNUNET_NO);
if (NULL != pg->peers[off].cfg)
GNUNET_CONFIGURATION_destroy (pg->peers[off].cfg);