/** * Attempts to register the next host in the host registration queue * * @param slave the slave controller whose host registration queue is checked * for host registrations */ static void register_next_host (struct Slave *slave) { struct HostRegistration *hr; hr = slave->hr_dll_head; GNUNET_assert (NULL != hr); GNUNET_assert (NULL == slave->rhandle); LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u\n", GNUNET_TESTBED_host_get_id_ (hr->host), GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id])); slave->rhandle = GNUNET_TESTBED_register_host (slave->controller, hr->host, hr_completion, slave); }
/** * Function to call to start a peer_create type operation once all * queues the operation is part of declare that the * operation can be activated. * * @param cls the closure from GNUNET_TESTBED_operation_create_() */ static void opstart_peer_create (void *cls) { struct OperationContext *opc = cls; struct PeerCreateData *data; struct GNUNET_TESTBED_PeerCreateMessage *msg; char *config; char *xconfig; size_t c_size; size_t xc_size; uint16_t msize; GNUNET_assert (OP_PEER_CREATE == opc->type); data = opc->data; GNUNET_assert (NULL != data); GNUNET_assert (NULL != data->peer); opc->state = OPC_STATE_STARTED; config = GNUNET_CONFIGURATION_serialize (data->cfg, &c_size); xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig); GNUNET_free (config); msize = xc_size + sizeof (struct GNUNET_TESTBED_PeerCreateMessage); msg = GNUNET_realloc (xconfig, msize); memmove (&msg[1], msg, xc_size); msg->header.size = htons (msize); msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER); msg->operation_id = GNUNET_htonll (opc->id); msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->peer->host)); msg->peer_id = htonl (data->peer->unique_id); msg->config_size = htonl (c_size); GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc); GNUNET_TESTBED_queue_message_ (opc->c, &msg->header); }
/** * Function to create a neigbour and add it into the neighbour list * * @param host the host of the neighbour */ struct Neighbour * GST_create_neighbour (struct GNUNET_TESTBED_Host *host) { struct Neighbour *n; n = GNUNET_new (struct Neighbour); n->host_id = GNUNET_TESTBED_host_get_id_ (host); neighbour_list_add (n); /* just add; connect on-demand */ return n; }
/** * Callback which will be called to after a host registration succeeded or failed * * @param cls the handle to the slave at which the registration is completed * @param emsg the error message; NULL if host registration is successful */ static void hr_completion (void *cls, const char *emsg) { struct Slave *slave = cls; struct HostRegistration *hr; slave->rhandle = NULL; hr = slave->hr_dll_head; GNUNET_assert (NULL != hr); LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u successful\n", GNUNET_TESTBED_host_get_id_ (hr->host), GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id])); GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head, slave->hr_dll_tail, hr); if (NULL != hr->cb) hr->cb (hr->cb_cls, emsg); GNUNET_free (hr); if (NULL != slave->hr_dll_head) register_next_host (slave); }
/** * Adds a host registration's request to a slave's registration queue * * @param slave the slave controller at which the given host has to be * registered * @param cb the host registration completion callback * @param cb_cls the closure for the host registration completion callback * @param host the host which has to be registered */ void GST_queue_host_registration (struct Slave *slave, GNUNET_TESTBED_HostRegistrationCompletion cb, void *cb_cls, struct GNUNET_TESTBED_Host *host) { struct HostRegistration *hr; int call_register; LOG (GNUNET_ERROR_TYPE_DEBUG, "Queueing host registration for host %u at %u\n", GNUNET_TESTBED_host_get_id_ (host), GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id])); hr = GNUNET_new (struct HostRegistration); hr->cb = cb; hr->cb_cls = cb_cls; hr->host = host; call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO; GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head, slave->hr_dll_tail, hr); if (GNUNET_YES == call_register) register_next_host (slave); }
/** * Function to add a host to the current list of known hosts * * @param host the host to add * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id * already in use */ static int host_list_add (struct GNUNET_TESTBED_Host *host) { uint32_t host_id; host_id = GNUNET_TESTBED_host_get_id_ (host); if (GST_host_list_size <= host_id) GST_array_grow_large_enough (GST_host_list, GST_host_list_size, host_id); if (NULL != GST_host_list[host_id]) { LOG_DEBUG ("A host with id: %u already exists\n", host_id); return GNUNET_SYSERR; } GST_host_list[host_id] = host; return GNUNET_OK; }
/** * Function called when a overlay connect operation is ready * * @param cls the closure from GNUNET_TESTBED_operation_create_() */ static void opstart_overlay_connect (void *cls) { struct OperationContext *opc = cls; struct GNUNET_TESTBED_OverlayConnectMessage *msg; struct OverlayConnectData *data; opc->state = OPC_STATE_STARTED; data = opc->data; GNUNET_assert (NULL != data); msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)); msg->header.size = htons (sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)); msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT); msg->peer1 = htonl (data->p1->unique_id); msg->peer2 = htonl (data->p2->unique_id); msg->operation_id = GNUNET_htonll (opc->id); msg->peer2_host_id = htonl (GNUNET_TESTBED_host_get_id_ (data->p2->host)); GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc); GNUNET_TESTBED_queue_message_ (opc->c, &msg->header); }
/** * 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; }
/** * Starts a controller process at the given host. The given host's configration * is used as a Template configuration to use for the remote controller; the * remote controller will be started with a slightly modified configuration * (port numbers, unix domain sockets and service home values are changed as per * TESTING library on the remote host). The modified configuration replaces the * host's existing configuration before signalling success through the * GNUNET_TESTBED_ControllerStatusCallback() * * @param trusted_ip the ip address of the controller which will be set as TRUSTED * HOST(all connections form this ip are permitted by the testbed) when * starting testbed controller at host. This can either be a single ip * address or a network address in CIDR notation. * @param host the host where the controller has to be started. CANNOT be NULL. * @param cb function called when the controller is successfully started or * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be * called if cb is called with GNUNET_SYSERR as status. Will never be * called in the same task as 'GNUNET_TESTBED_controller_start' * (synchronous errors will be signalled by returning NULL). This * parameter cannot be NULL. * @param cls closure for above callbacks * @return the controller process handle, NULL on errors */ struct GNUNET_TESTBED_ControllerProc * GNUNET_TESTBED_controller_start (const char *trusted_ip, struct GNUNET_TESTBED_Host *host, GNUNET_TESTBED_ControllerStatusCallback cb, void *cls) { struct GNUNET_TESTBED_ControllerProc *cp; struct GNUNET_TESTBED_HelperInit *msg; const struct GNUNET_CONFIGURATION_Handle *cfg; const char *hostname; static char *const binary_argv[] = { HELPER_TESTBED_BINARY, NULL }; GNUNET_assert (NULL != host); GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host))); hostname = NULL; API_VIOLATION (GNUNET_NO == host->locked, "Host is already locked by a previous call to GNUNET_TESTBED_controller_start()"); host->locked = GNUNET_YES; API_VIOLATION (GNUNET_NO == host->controller_started, "Attempting to start a controller on a host which is already started a controller"); cp = GNUNET_new (struct GNUNET_TESTBED_ControllerProc); if (0 == GNUNET_TESTBED_host_get_id_ (host)) { cp->helper = GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv, &helper_mst, &helper_exp_cb, cp); } else { char *helper_binary_path_args[2]; char **rsh_args; char **rsh_suffix_args; const char *username; char *port; char *argstr; char *aux; unsigned int cnt; username = host->username; hostname = host->hostname; GNUNET_asprintf (&port, "%u", host->port); LOG_DEBUG ("Starting remote connection to destination %s\n", hostname); if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "testbed", "HELPER_BINARY_PATH", &helper_binary_path_args[0])) helper_binary_path_args[0] = GNUNET_OS_get_libexec_binary_path (HELPER_TESTBED_BINARY); helper_binary_path_args[1] = NULL; rsh_args = gen_rsh_args (port, hostname, username); rsh_suffix_args = gen_rsh_suffix_args ((const char **) helper_binary_path_args); cp->helper_argv = join_argv ((const char **) rsh_args, (const char **) rsh_suffix_args); free_argv (rsh_args); free_argv (rsh_suffix_args); GNUNET_free (port); argstr = GNUNET_strdup (""); for (cnt = 0; NULL != cp->helper_argv[cnt]; cnt++) { aux = argstr; GNUNET_assert (0 < GNUNET_asprintf (&argstr, "%s %s", aux, cp->helper_argv[cnt])); GNUNET_free (aux); } LOG_DEBUG ("Helper cmd str: %s\n", argstr); GNUNET_free (argstr); cp->helper = GNUNET_HELPER_start (GNUNET_NO, cp->helper_argv[0], cp->helper_argv, &helper_mst, &helper_exp_cb, cp); GNUNET_free (helper_binary_path_args[0]); } if (NULL == cp->helper) { if (NULL != cp->helper_argv) free_argv (cp->helper_argv); GNUNET_free (cp); return NULL; } cp->host = host; cp->cb = cb; cp->cls = cls; msg = GNUNET_TESTBED_create_helper_init_msg_ (trusted_ip, hostname, cfg); cp->msg = &msg->header; cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp); if (NULL == cp->shandle) { GNUNET_free (msg); GNUNET_TESTBED_controller_stop (cp); return NULL; } return cp; }
/** * 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); }
/** * Register a host with the controller * * @param controller the controller handle * @param host the host to register * @param cc the completion callback to call to inform the status of * registration. After calling this callback the registration handle * will be invalid. Cannot be NULL. * @param cc_cls the closure for the cc * @return handle to the host registration which can be used to cancel the * registration */ struct GNUNET_TESTBED_HostRegistrationHandle * GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller, struct GNUNET_TESTBED_Host *host, GNUNET_TESTBED_HostRegistrationCompletion cc, void *cc_cls) { struct GNUNET_TESTBED_HostRegistrationHandle *rh; struct GNUNET_TESTBED_AddHostMessage *msg; const char *username; const char *hostname; char *config; char *cconfig; void *ptr; size_t cc_size; size_t config_size; uint16_t msg_size; uint16_t username_length; uint16_t hostname_length; if (NULL != controller->rh) return NULL; hostname = GNUNET_TESTBED_host_get_hostname (host); if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller)) { LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n", (NULL == hostname) ? "localhost" : hostname); return NULL; } rh = GNUNET_new (struct GNUNET_TESTBED_HostRegistrationHandle); rh->host = host; rh->c = controller; GNUNET_assert (NULL != cc); rh->cc = cc; rh->cc_cls = cc_cls; controller->rh = rh; username = GNUNET_TESTBED_host_get_username_ (host); username_length = 0; if (NULL != username) username_length = strlen (username); GNUNET_assert (NULL != hostname); /* Hostname must be present */ hostname_length = strlen (hostname); GNUNET_assert (NULL != host->cfg); config = GNUNET_CONFIGURATION_serialize (host->cfg, &config_size); cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig); GNUNET_free (config); msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage)); msg_size += username_length; msg_size += hostname_length; msg_size += cc_size; msg = GNUNET_malloc (msg_size); msg->header.size = htons (msg_size); msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST); msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host)); msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host)); ptr = &msg[1]; if (NULL != username) { msg->username_length = htons (username_length); GNUNET_memcpy (ptr, username, username_length); ptr += username_length; } msg->hostname_length = htons (hostname_length); GNUNET_memcpy (ptr, hostname, hostname_length); ptr += hostname_length; msg->config_size = htons (config_size); GNUNET_memcpy (ptr, cconfig, cc_size); ptr += cc_size; GNUNET_assert ((ptr - (void *) msg) == msg_size); GNUNET_free (cconfig); GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg); return rh; }