/** * Create a host to run peers and controllers on. * * @param hostname name of the host, use "NULL" for localhost * @param username username to use for the login; may be NULL * @param cfg the configuration to use as a template while starting a controller * on this host. Operation queue sizes specific to a host are also * read from this configuration handle * @param port port number to use for ssh; use 0 to let ssh decide * @return handle to the host, NULL on error */ struct GNUNET_TESTBED_Host * GNUNET_TESTBED_host_create (const char *hostname, const char *username, const struct GNUNET_CONFIGURATION_Handle *cfg, uint16_t port) { static uint32_t uid_generator; if (NULL == hostname) return GNUNET_TESTBED_host_create_with_id (0, hostname, username, cfg, port); return GNUNET_TESTBED_host_create_with_id (++uid_generator, hostname, username, cfg, port); }
/** * Create a host by ID; given this host handle, we could not * run peers at the host, but we can talk about the host * internally. * * @param id global host ID assigned to the host; 0 is * reserved to always mean 'localhost' * @param cfg the configuration to use as a template while starting a controller * on this host. Operation queue sizes specific to a host are also * read from this configuration handle * @return handle to the host, NULL on error */ struct GNUNET_TESTBED_Host * GNUNET_TESTBED_host_create_by_id_ (uint32_t id, const struct GNUNET_CONFIGURATION_Handle *cfg) { return GNUNET_TESTBED_host_create_with_id (id, NULL, NULL, cfg, 0); }
/** * Task for inserting delay between tests * * @param * @return */ static void delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { delay_task_id = NULL; switch (result) { case SLAVE2_PEER_CREATE_SUCCESS: op = GNUNET_TESTBED_peer_stop (NULL, slave1_peer, NULL, NULL); FAIL_TEST (NULL != op); break; case MASTER_SLAVE2_PEERS_CONNECTED: slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, cfg, 0); rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL); break; case SLAVE2_SLAVE3_PEERS_CONNECTED: op = GNUNET_TESTBED_peer_stop (NULL, slave2_peer, NULL, NULL); FAIL_TEST (NULL != op); break; default: FAIL_TEST (0); } }
/** * 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_cont (void *cls, const char *emsg) { rh = NULL; switch (result) { case MASTER_PEER_START_SUCCESS: GNUNET_assert (NULL == emsg); GNUNET_assert (NULL != mc); result = SLAVE1_REGISTERED; slave2 = GNUNET_TESTBED_host_create_with_id (2, "127.0.0.1", NULL, 0); GNUNET_assert (NULL != slave2); rh = GNUNET_TESTBED_register_host (mc, slave2, ®istration_cont, NULL); GNUNET_assert (NULL != rh); break; case SLAVE1_REGISTERED: GNUNET_assert (NULL == emsg); GNUNET_assert (NULL != mc); result = SLAVE2_REGISTERED; GNUNET_assert (NULL != cfg); op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, cfg, GNUNET_YES); GNUNET_assert (NULL != op); break; case SLAVE2_PEER_DESTROY_SUCCESS: GNUNET_assert (NULL == emsg); GNUNET_assert (NULL != mc); GNUNET_assert (NULL == op); result = SLAVE3_REGISTERED; op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, cfg, GNUNET_YES); GNUNET_assert (NULL != op); break; default: GNUNET_break (0); do_abort_now (NULL); } }
/** * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages * * @param cls NULL * @param client identification of the client * @param message the actual message */ static void handle_add_host (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { struct GNUNET_TESTBED_Host *host; const struct GNUNET_TESTBED_AddHostMessage *msg; struct GNUNET_TESTBED_HostConfirmedMessage *reply; struct GNUNET_CONFIGURATION_Handle *host_cfg; char *username; char *hostname; char *emsg; const void *ptr; uint32_t host_id; uint16_t username_length; uint16_t hostname_length; uint16_t reply_size; uint16_t msize; msg = (const struct GNUNET_TESTBED_AddHostMessage *) message; msize = ntohs (msg->header.size); if (msize <= sizeof (struct GNUNET_TESTBED_AddHostMessage)) { GNUNET_break_op (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } username_length = ntohs (msg->username_length); hostname_length = ntohs (msg->hostname_length); /* msg must contain hostname */ if ((msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length)) || (0 == hostname_length)) { GNUNET_break_op (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } /* msg must contain configuration */ if (msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + hostname_length)) { GNUNET_break_op (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } username = NULL; hostname = NULL; ptr = &msg[1]; if (0 != username_length) { username = GNUNET_malloc (username_length + 1); strncpy (username, ptr, username_length); ptr += username_length; } hostname = GNUNET_malloc (hostname_length + 1); strncpy (hostname, ptr, hostname_length); if (NULL == (host_cfg = GNUNET_TESTBED_extract_config_ (message))) { GNUNET_free_non_null (username); GNUNET_free_non_null (hostname); GNUNET_break_op (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } host_id = ntohl (msg->host_id); LOG_DEBUG ("Received ADDHOST %u message\n", host_id); LOG_DEBUG ("-------host id: %u\n", host_id); LOG_DEBUG ("-------hostname: %s\n", hostname); if (NULL != username) LOG_DEBUG ("-------username: %s\n", username); else LOG_DEBUG ("-------username: <not given>\n"); LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port)); host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username, host_cfg, ntohs (msg->ssh_port)); GNUNET_free_non_null (username); GNUNET_free (hostname); GNUNET_CONFIGURATION_destroy (host_cfg); if (NULL == host) { GNUNET_break_op (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage); if (GNUNET_OK != host_list_add (host)) { /* We are unable to add a host */ emsg = "A host exists with given host-id"; LOG_DEBUG ("%s: %u", emsg, host_id); GNUNET_TESTBED_host_destroy (host); reply_size += strlen (emsg) + 1; reply = GNUNET_malloc (reply_size); memcpy (&reply[1], emsg, strlen (emsg) + 1); } else { LOG_DEBUG ("Added host %u at %u\n", host_id, GST_context->host_id); reply = GNUNET_malloc (reply_size); } reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST_SUCCESS); reply->header.size = htons (reply_size); reply->host_id = htonl (host_id); GST_queue_message (client, &reply->header); GNUNET_SERVER_receive_done (client, GNUNET_OK); }
/** * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages * * @param cls NULL * @param client identification of the client * @param message the actual message */ static void handle_init (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { const struct GNUNET_TESTBED_InitMessage *msg; struct GNUNET_TESTBED_Host *host; const char *controller_hostname; char *ss_str; struct GNUNET_TESTING_SharedService *ss; unsigned int cnt; uint16_t msize; if (NULL != GST_context) { LOG_DEBUG ("We are being connected to laterally\n"); GNUNET_SERVER_receive_done (client, GNUNET_OK); return; } msg = (const struct GNUNET_TESTBED_InitMessage *) message; msize = ntohs (message->size); if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } msize -= sizeof (struct GNUNET_TESTBED_InitMessage); controller_hostname = (const char *) &msg[1]; if ('\0' != controller_hostname[msize - 1]) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } ss_str = NULL; ss = NULL; if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (GST_config, "TESTBED", "SHARED_SERVICES", &ss_str)) { ss = parse_shared_services (ss_str, GST_config); GNUNET_free (ss_str); ss_str = NULL; } GST_context = GNUNET_new (struct Context); GNUNET_SERVER_client_keep (client); GST_context->client = client; GST_context->host_id = ntohl (msg->host_id); GST_context->master_ip = GNUNET_strdup (controller_hostname); LOG_DEBUG ("Our IP: %s\n", GST_context->master_ip); GST_context->system = GNUNET_TESTING_system_create ("testbed", GST_context->master_ip, hostname, ss); if (NULL != ss) { for (cnt = 0; NULL != ss[cnt].service; cnt++) { ss_str = (char *) ss[cnt].service; GNUNET_free (ss_str); } GNUNET_free (ss); ss = NULL; } host = GNUNET_TESTBED_host_create_with_id (GST_context->host_id, GST_context->master_ip, NULL, GST_config, 0); host_list_add (host); LOG_DEBUG ("Created master context with host ID: %u\n", GST_context->host_id); GNUNET_SERVER_receive_done (client, GNUNET_OK); }
/** * Signature of the event handler function called by the * respective event controller. * * @param cls closure * @param event information about the event */ static void controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event) { switch (result) { case SLAVE2_REGISTERED: check_operation_success (event); GNUNET_TESTBED_operation_done (op); op = NULL; result = SLAVE1_LINK_SUCCESS; GNUNET_assert (NULL != slave2); GNUNET_assert (NULL != slave); op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL); GNUNET_assert (NULL != op); break; case SLAVE1_PEER_START_SUCCESS: check_operation_success (event); GNUNET_TESTBED_operation_done (op); result = SLAVE2_LINK_SUCCESS; op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL); GNUNET_assert (NULL != op); break; case MASTER_PEER_CREATE_SUCCESS: GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type); GNUNET_assert (event->details.peer_start.host == host); GNUNET_assert (event->details.peer_start.peer == master_peer); GNUNET_TESTBED_operation_done (op); result = MASTER_PEER_START_SUCCESS; slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0); GNUNET_assert (NULL != slave); rh = GNUNET_TESTBED_register_host (mc, slave, ®istration_cont, NULL); GNUNET_assert (NULL != rh); break; case SLAVE1_PEER_CREATE_SUCCESS: GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type); GNUNET_assert (event->details.peer_start.host == slave); GNUNET_assert (event->details.peer_start.peer == slave1_peer); GNUNET_TESTBED_operation_done (op); result = SLAVE1_PEER_START_SUCCESS; op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, cfg, GNUNET_YES); break; case SLAVE2_PEER_CREATE_SUCCESS: GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type); GNUNET_assert (event->details.peer_stop.peer == slave1_peer); GNUNET_TESTBED_operation_done (op); result = SLAVE1_PEER_STOP_SUCCESS; op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL); GNUNET_assert (NULL != op); break; case SLAVE1_PEER_STOP_SUCCESS: GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type); GNUNET_assert (event->details.peer_start.host == slave2); GNUNET_assert (event->details.peer_start.peer == slave2_peer); GNUNET_TESTBED_operation_done (op); result = SLAVE2_PEER_START_SUCCESS; op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer, slave2_peer); break; case SLAVE2_PEER_START_SUCCESS: GNUNET_assert (NULL != event); GNUNET_assert (GNUNET_TESTBED_ET_CONNECT == event->type); GNUNET_assert (event->details.peer_connect.peer1 == master_peer); GNUNET_assert (event->details.peer_connect.peer2 == slave2_peer); result = MASTER_SLAVE2_PEERS_CONNECTED; GNUNET_TESTBED_operation_done (op); op = NULL; GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task, NULL); break; case MASTER_SLAVE2_PEERS_CONNECTED: GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type); GNUNET_assert (event->details.peer_stop.peer == slave2_peer); GNUNET_TESTBED_operation_done (op); result = SLAVE2_PEER_STOP_SUCCESS; op = GNUNET_TESTBED_peer_destroy (slave1_peer); GNUNET_assert (NULL != op); break; case SLAVE2_PEER_STOP_SUCCESS: check_operation_success (event); GNUNET_TESTBED_operation_done (op); result = SLAVE1_PEER_DESTROY_SUCCESS; op = GNUNET_TESTBED_peer_destroy (slave2_peer); GNUNET_assert (NULL != op); break; case SLAVE1_PEER_DESTROY_SUCCESS: check_operation_success (event); GNUNET_TESTBED_operation_done (op); op = NULL; result = SLAVE2_PEER_DESTROY_SUCCESS; slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, 0); rh = GNUNET_TESTBED_register_host (mc, slave3, ®istration_cont, NULL); break; case SLAVE3_REGISTERED: check_operation_success (event); GNUNET_TESTBED_operation_done (op); op = NULL; result = SLAVE3_STARTED; op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3); GNUNET_assert (NULL != op); break; case SLAVE3_STARTED: GNUNET_assert (NULL != event); GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type); GNUNET_assert (event->details.operation_finished.operation == op); GNUNET_assert (NULL == event->details.operation_finished.op_cls); GNUNET_assert (NULL == event->details.operation_finished.emsg); cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic); GNUNET_TESTBED_operation_done (op); result = SLAVE3_GET_CONFIG_SUCCESS; op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, cfg3, GNUNET_NO); break; case SLAVE3_GET_CONFIG_SUCCESS: result = SLAVE3_LINK_SUCCESS; GNUNET_TESTBED_operation_done (op); op = GNUNET_TESTBED_peer_destroy (master_peer); break; case SLAVE3_LINK_SUCCESS: check_operation_success (event); result = SUCCESS; GNUNET_TESTBED_operation_done (op); op = NULL; GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3), &do_shutdown, NULL); break; default: GNUNET_assert (0); } }