aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_group.c
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2011-07-26 12:39:23 +0000
committerNathan S. Evans <evans@in.tum.de>2011-07-26 12:39:23 +0000
commit66e9353163686f84163816a7fee09209ce522fcb (patch)
tree2776423670bd69f4f21ef0e6e39af340585ed99b /src/testing/testing_group.c
parente8bc36209dd09c9720c6ea8657e4f49c35c08956 (diff)
downloadgnunet-66e9353163686f84163816a7fee09209ce522fcb.tar.gz
gnunet-66e9353163686f84163816a7fee09209ce522fcb.zip
load hosts from file, implementation in testing
Diffstat (limited to 'src/testing/testing_group.c')
-rw-r--r--src/testing/testing_group.c99
1 files changed, 99 insertions, 0 deletions
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)
6811 6811
6812} 6812}
6813 6813
6814/**
6815 * Read a testing hosts file based on a configuration.
6816 * Returns a DLL of hosts (caller must free!) on success
6817 * or NULL on failure.
6818 *
6819 * @param cfg a configuration with a testing section
6820 *
6821 * @return DLL of hosts on success, NULL on failure
6822 */
6823struct GNUNET_TESTING_Host *
6824GNUNET_TESTING_hosts_load (const struct GNUNET_CONFIGURATION_Handle *cfg)
6825{
6826 struct GNUNET_TESTING_Host *hosts;
6827 struct GNUNET_TESTING_Host *temphost;
6828 char *data;
6829 char *buf;
6830 char *hostfile;
6831 struct stat frstat;
6832 int count;
6833 int ret;
6834
6835 /* Check for a hostfile containing user@host:port triples */
6836 if (GNUNET_OK
6837 != GNUNET_CONFIGURATION_get_value_string (cfg, "testing", "hostfile",
6838 &hostfile))
6839 return NULL;
6840
6841 hosts = NULL;
6842 temphost = NULL;
6843 data = NULL;
6844 if (hostfile != NULL)
6845 {
6846 if (GNUNET_OK != GNUNET_DISK_file_test (hostfile))
6847 GNUNET_DISK_fn_write (hostfile, NULL, 0, GNUNET_DISK_PERM_USER_READ
6848 | GNUNET_DISK_PERM_USER_WRITE);
6849 if ((0 != STAT (hostfile, &frstat)) || (frstat.st_size == 0))
6850 {
6851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6852 "Could not open file specified for host list, ending test!");
6853 GNUNET_free(hostfile);
6854 return NULL;
6855 }
6856
6857 data = GNUNET_malloc_large (frstat.st_size);
6858 GNUNET_assert(data != NULL);
6859 if (frstat.st_size
6860 != GNUNET_DISK_fn_read (hostfile, data, frstat.st_size))
6861 {
6862 GNUNET_log (
6863 GNUNET_ERROR_TYPE_ERROR,
6864 "Could not read file %s specified for host list, ending test!",
6865 hostfile);
6866 GNUNET_free (hostfile);
6867 GNUNET_free (data);
6868 return NULL;
6869 }
6870
6871 GNUNET_free_non_null(hostfile);
6872
6873 buf = data;
6874 count = 0;
6875 while (count < frstat.st_size - 1)
6876 {
6877 count++;
6878 if (((data[count] == '\n')) && (buf != &data[count]))
6879 {
6880 data[count] = '\0';
6881 temphost = GNUNET_malloc(sizeof(struct GNUNET_TESTING_Host));
6882 ret = sscanf (buf, "%a[a-zA-Z0-9_]@%a[a-zA-Z0-9.]:%hd",
6883 &temphost->username, &temphost->hostname,
6884 &temphost->port);
6885 if (3 == ret)
6886 {
6887 GNUNET_log (
6888 GNUNET_ERROR_TYPE_DEBUG,
6889 "Successfully read host %s, port %d and user %s from file\n",
6890 temphost->hostname, temphost->port,
6891 temphost->username);
6892 }
6893 else
6894 {
6895 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6896 "Error reading line `%s' in hostfile\n", buf);
6897 GNUNET_free(temphost);
6898 buf = &data[count + 1];
6899 continue;
6900 }
6901 temphost->next = hosts;
6902 hosts = temphost;
6903 buf = &data[count + 1];
6904 }
6905 else if ((data[count] == '\n') || (data[count] == '\0'))
6906 buf = &data[count + 1];
6907 }
6908 }
6909 GNUNET_free_non_null(data);
6910
6911 return hosts;
6912}
6814 6913
6815/** 6914/**
6816 * Shutdown all peers started in the given group. 6915 * Shutdown all peers started in the given group.