/** * Main run function. * * @param cls NULL * @param args arguments passed to GNUNET_PROGRAM_run * @param cfgfile the path to configuration file * @param cfg the configuration file handle */ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { host = GNUNET_TESTBED_host_create (NULL, NULL, 0); GNUNET_assert (NULL != host); if (GNUNET_YES != GNUNET_TESTBED_is_host_habitable (host, config)) { GNUNET_TESTBED_host_destroy (host); host = NULL; (void) PRINTF ("%s", "Unable to run the test as this system is not configured " "to use password less SSH logins to localhost.\n" "Marking test as successful\n"); result = SUCCESS; return; } cfg = GNUNET_CONFIGURATION_dup (config); cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL); abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL); }
/** * Main run function. * * @param cls NULL * @param args arguments passed to GNUNET_PROGRAM_run * @param cfgfile the path to configuration file * @param cfg the configuration file handle */ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { cfg = GNUNET_CONFIGURATION_dup (config); host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0); FAIL_TEST (NULL != host); if (NULL == (hc_handle = GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb, NULL))) { GNUNET_TESTBED_host_destroy (host); GNUNET_CONFIGURATION_destroy (cfg); cfg = NULL; host = NULL; (void) PRINTF ("%s", "Unable to run the test as this system is not configured " "to use password less SSH logins to localhost.\n" "Marking test as successful\n"); result = SKIP; return; } abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL); }
/** * Callback to signal successfull startup of the controller process * * @param cls the closure from GNUNET_TESTBED_controller_start() * @param cfg the configuration with which the controller has been started; * NULL if status is not GNUNET_OK * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not, * GNUNET_TESTBED_controller_stop() shouldn't be called in this case */ static void status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config, int status) { uint64_t event_mask; if (GNUNET_OK != status) { GNUNET_break (0); cp1 = NULL; abort_test (); return; } event_mask = 0; event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START); event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP); event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT); event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED); switch (result) { case INIT: controller1 = GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb, NULL); if (NULL == controller1) { GNUNET_break (0); abort_test (); return; } result = CONTROLLER1_UP; neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0); if (NULL == neighbour1) { GNUNET_break (0); abort_test (); return; } reg_handle = GNUNET_TESTBED_register_host (controller1, neighbour1, ®istration_comp, neighbour1); if (NULL == reg_handle) { GNUNET_break (0); abort_test (); return; } break; default: GNUNET_break (0); abort_test (); return; } }
/** * Callback which will be called to after a host registration succeeded or failed * * @param cls the host which has been registered * @param emsg the error message; NULL if host registration is successful */ static void registration_comp (void *cls, const char *emsg) { reg_handle = NULL; if (cls == neighbour1) { neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0); if (NULL == neighbour2) { GNUNET_break (0); abort_test (); return; } reg_handle = GNUNET_TESTBED_register_host (controller1, neighbour2, ®istration_comp, neighbour2); if (NULL == reg_handle) { GNUNET_break (0); abort_test (); return; } return; } if (cls != neighbour2) { GNUNET_break (0); abort_test (); return; } peer1.operation = GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb, &peer1); if (NULL == peer1.operation) { GNUNET_break (0); abort_test (); return; } }
/** * Load a set of hosts from a configuration file. * * @param filename file with the host specification * @param cfg the configuration to use as a template while starting a controller * on any of the loaded hosts. Operation queue sizes specific to a host * are also read from this configuration handle * @param hosts set to the hosts found in the file; caller must free this if * number of hosts returned is greater than 0 * @return number of hosts returned in 'hosts', 0 on error */ unsigned int GNUNET_TESTBED_hosts_load_from_file (const char *filename, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_TESTBED_Host ***hosts) { //struct GNUNET_TESTBED_Host **host_array; struct GNUNET_TESTBED_Host *starting_host; char *data; char *buf; char *username; char *hostname; regex_t rex; regmatch_t pmatch[6]; uint64_t fs; short int port; unsigned int offset; unsigned int count; GNUNET_assert (NULL != filename); if (GNUNET_YES != GNUNET_DISK_file_test (filename)) { LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s not found\n"), filename); return 0; } if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) fs = 0; if (0 == fs) { LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s has no data\n"), filename); return 0; } data = GNUNET_malloc (fs); if (fs != GNUNET_DISK_fn_read (filename, data, fs)) { GNUNET_free (data); LOG (GNUNET_ERROR_TYPE_WARNING, _("Hosts file %s cannot be read\n"), filename); return 0; } buf = data; offset = 0; starting_host = NULL; count = 0; /* refer RFC 952 and RFC 1123 for valid hostnames */ GNUNET_assert (0 == regcomp (&rex, "^(([[:alnum:]]+)@)?" /* username */ "([[:alnum:]]+[-[:alnum:]_\\.]+)" /* hostname */ "(:([[:digit:]]{1,5}))?", /* port */ REG_EXTENDED | REG_ICASE)); while (offset < (fs - 1)) { offset++; if (((data[offset] == '\n')) && (buf != &data[offset])) { unsigned int size; data[offset] = '\0'; username = NULL; hostname = NULL; port = 0; if ((REG_NOMATCH == regexec (&rex, buf, 6, pmatch, 0)) || (-1 == pmatch[3].rm_so)) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Error reading line `%s' in hostfile\n", buf); buf = &data[offset + 1]; continue; } if (-1 != pmatch[2].rm_so) { size = pmatch[2].rm_eo - pmatch[2].rm_so; username = GNUNET_malloc (size + 1); username[size] = '\0'; GNUNET_assert (NULL != strncpy (username, buf + pmatch[2].rm_so, size)); } if (-1 != pmatch[5].rm_so) { (void) SSCANF (buf + pmatch[5].rm_so, "%5hd", &port); } size = pmatch[3].rm_eo - pmatch[3].rm_so; hostname = GNUNET_malloc (size + 1); hostname[size] = '\0'; GNUNET_assert (NULL != strncpy (hostname, buf + pmatch[3].rm_so, size)); LOG (GNUNET_ERROR_TYPE_DEBUG, "Successfully read host %s, port %d and user %s from file\n", (NULL == hostname) ? "NULL" : hostname, port, (NULL == username) ? "NULL" : username); /* We store hosts in a static list; hence we only require the starting * host pointer in that list to access the newly created list of hosts */ if (NULL == starting_host) starting_host = GNUNET_TESTBED_host_create (hostname, username, cfg, port); else (void) GNUNET_TESTBED_host_create (hostname, username, cfg, port); count++; GNUNET_free_non_null (username); GNUNET_free (hostname); buf = &data[offset + 1]; } else if ((data[offset] == '\n') || (data[offset] == '\0')) buf = &data[offset + 1]; } regfree (&rex); GNUNET_free (data); if (NULL == starting_host) return 0; *hosts = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * count); memcpy (*hosts, &host_list[GNUNET_TESTBED_host_get_id_ (starting_host)], sizeof (struct GNUNET_TESTBED_Host *) * count); return count; }
/** * Main run function. * * @param cls NULL * @param args arguments passed to GNUNET_PROGRAM_run * @param cfgfile the path to configuration file * @param cfg the configuration file handle */ static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *config) { unsigned int cnt; cfg = GNUNET_CONFIGURATION_dup (config); host = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0); GNUNET_assert (NULL != host); GNUNET_assert (0 != GNUNET_TESTBED_host_get_id_ (host)); GNUNET_TESTBED_host_destroy (host); host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0); GNUNET_assert (NULL != host); GNUNET_assert (0 == GNUNET_TESTBED_host_get_id_ (host)); GNUNET_assert (host == GNUNET_TESTBED_host_lookup_by_id_ (0)); hosts = NULL; num_hosts = GNUNET_TESTBED_hosts_load_from_file ("sample_hosts.txt", cfg, &hosts); GNUNET_assert (7 == num_hosts); GNUNET_assert (NULL != hosts); for (cnt = 0; cnt < num_hosts; cnt++) { if (cnt < 3) { GNUNET_assert (0 == strcmp ("totakura", GNUNET_TESTBED_host_get_username_ (hosts[cnt]))); GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt])); GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt])); } if (3 == cnt) { GNUNET_assert (0 == strcmp ("totakura", GNUNET_TESTBED_host_get_username_ (hosts[cnt]))); GNUNET_assert (NULL != GNUNET_TESTBED_host_get_hostname (hosts[cnt])); GNUNET_assert (2022 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt])); } if (4 == cnt) { GNUNET_assert (0 == strcmp ("totakura", GNUNET_TESTBED_host_get_username_ (hosts[cnt]))); GNUNET_assert (0 == strcmp ("asgard.realm", GNUNET_TESTBED_host_get_hostname (hosts[cnt]))); GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt])); } if (5 == cnt) { GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt])); GNUNET_assert (0 == strcmp ("rivendal", GNUNET_TESTBED_host_get_hostname (hosts[cnt]))); GNUNET_assert (22 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt])); } if (6 == cnt) { GNUNET_assert (NULL == GNUNET_TESTBED_host_get_username_ (hosts[cnt])); GNUNET_assert (0 == strcmp ("rohan", GNUNET_TESTBED_host_get_hostname (hosts[cnt]))); GNUNET_assert (561 == GNUNET_TESTBED_host_get_ssh_port_ (hosts[cnt])); } } status = GNUNET_YES; shutdown_id = GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (0), &do_shutdown, NULL); }