From 66e9353163686f84163816a7fee09209ce522fcb Mon Sep 17 00:00:00 2001 From: "Nathan S. Evans" Date: Tue, 26 Jul 2011 12:39:23 +0000 Subject: [PATCH] load hosts from file, implementation in testing --- src/include/gnunet_testing_lib.h | 55 +++++++++++------- src/nse/nse-profiler.c | 5 +- src/testing/testing_group.c | 99 ++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 23 deletions(-) diff --git a/src/include/gnunet_testing_lib.h b/src/include/gnunet_testing_lib.h index 34920bbab..0b998cf95 100644 --- a/src/include/gnunet_testing_lib.h +++ b/src/include/gnunet_testing_lib.h @@ -602,31 +602,17 @@ GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d, struct GNUNET_TIME_Relative timeout, GNUNET_TESTING_NotifyCompletion cb, void *cb_cls); - /** - * Establish a connection between two GNUnet daemons. + * Read a testing hosts file based on a configuration. + * Returns a DLL of hosts (caller must free!) on success + * or NULL on failure. * - * @param d1 handle for the first daemon - * @param d2 handle for the second daemon - * @param timeout how long is the connection attempt - * allowed to take? - * @param max_connect_attempts how many times should we try to reconnect - * (within timeout) - * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume - * the HELLO has already been exchanged - * @param cb function to call at the end - * @param cb_cls closure for cb + * @param cfg a configuration with a testing section + * + * @return DLL of hosts on success, NULL on failure */ -void -GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1, - struct GNUNET_TESTING_Daemon *d2, - struct GNUNET_TIME_Relative timeout, - unsigned int max_connect_attempts, - int send_hello, - GNUNET_TESTING_NotifyConnection cb, - void *cb_cls); - - +struct GNUNET_TESTING_Host * +GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg); /** @@ -679,6 +665,31 @@ GNUNET_TESTING_daemons_start (const struct GNUNET_CONFIGURATION_Handle *cfg, void GNUNET_TESTING_daemons_continue_startup(struct GNUNET_TESTING_PeerGroup *pg); + +/** + * Establish a connection between two GNUnet daemons. + * + * @param d1 handle for the first daemon + * @param d2 handle for the second daemon + * @param timeout how long is the connection attempt + * allowed to take? + * @param max_connect_attempts how many times should we try to reconnect + * (within timeout) + * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume + * the HELLO has already been exchanged + * @param cb function to call at the end + * @param cb_cls closure for cb + */ +void +GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1, + struct GNUNET_TESTING_Daemon *d2, + struct GNUNET_TIME_Relative timeout, + unsigned int max_connect_attempts, + int send_hello, + GNUNET_TESTING_NotifyConnection cb, + void *cb_cls); + + /** * Restart all peers in the given group. * diff --git a/src/nse/nse-profiler.c b/src/nse/nse-profiler.c index 8d90ca260..3194979a5 100644 --- a/src/nse/nse-profiler.c +++ b/src/nse/nse-profiler.c @@ -580,6 +580,7 @@ run (void *cls, { char *temp_str; unsigned long long temp_wait; + struct GNUNET_TESTING_Host *hosts; ok = 1; testing_cfg = GNUNET_CONFIGURATION_create(); @@ -673,12 +674,14 @@ run (void *cls, } GNUNET_free_non_null(temp_str); + hosts = GNUNET_TESTING_hosts_load (testing_cfg); + pg = GNUNET_TESTING_peergroup_start(testing_cfg, num_peers, TIMEOUT, &connect_cb, &my_cb, NULL, - NULL); + hosts); GNUNET_assert (pg != NULL); shutdown_handle = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_get_forever(), &shutdown_task, diff --git a/src/testing/testing_group.c b/src/testing/testing_group.c index 5b5981707..89e866d10 100644 --- a/src/testing/testing_group.c +++ b/src/testing/testing_group.c @@ -6811,6 +6811,105 @@ schedule_shutdown_task(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) } +/** + * Read a testing hosts file based on a configuration. + * Returns a DLL of hosts (caller must free!) on success + * or NULL on failure. + * + * @param cfg a configuration with a testing section + * + * @return DLL of hosts on success, NULL on failure + */ +struct GNUNET_TESTING_Host * +GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + struct GNUNET_TESTING_Host *hosts; + struct GNUNET_TESTING_Host *temphost; + char *data; + char *buf; + char *hostfile; + struct stat frstat; + int count; + int ret; + + /* Check for a hostfile containing user@host:port triples */ + if (GNUNET_OK + != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile", + &hostfile)) + return NULL; + + hosts = NULL; + temphost = NULL; + data = NULL; + if (hostfile != NULL) + { + if (GNUNET_OK != GNUNET_DISK_file_test (hostfile)) + GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ + | GNUNET_DISK_PERM_USER_WRITE); + if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0)) + { + GNUNET_log (GNUNET_ERROR_TYPE_ERROR, + "Could not open file specified for host list, ending test!"); + GNUNET_free(hostfile); + return NULL; + } + + data = GNUNET_malloc_large (frstat.st_size); + GNUNET_assert(data != NULL); + if (frstat.st_size + != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size)) + { + GNUNET_log ( + GNUNET_ERROR_TYPE_ERROR, + "Could not read file %s specified for host list, ending test!", + hostfile); + GNUNET_free (hostfile); + GNUNET_free (data); + return NULL; + } + + GNUNET_free_non_null(hostfile); + + buf = data; + count = 0; + while (count < frstat.st_size - 1) + { + count++; + if (((data[count] == '\n')) && (buf != &data[count])) + { + data[count] = '\0'; + temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host)); + ret = sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd", + &temphost->username, &temphost->hostname, + &temphost->port); + if (3 == ret) + { + GNUNET_log ( + GNUNET_ERROR_TYPE_DEBUG, + "Successfully read host %s, port %d and user %s from file\n", + temphost->hostname, temphost->port, + temphost->username); + } + else + { + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Error reading line `%s' in hostfile\n", buf); + GNUNET_free(temphost); + buf = &data[count + 1]; + continue; + } + temphost->next = hosts; + hosts = temphost; + buf = &data[count + 1]; + } + else if ((data[count] == '\n') || (data[count] == '\0')) + buf = &data[count + 1]; + } + } + GNUNET_free_non_null(data); + + return hosts; +} /** * Shutdown all peers started in the given group. -- 2.25.1