/** * Type of a function to call when we receive a message * from the service. * * @param cls ServiceConnectData * @param msg message received, NULL on timeout or fatal error */ static void configuration_receiver (void *cls, const struct GNUNET_MessageHeader *msg) { struct ServiceConnectData *data = cls; struct GNUNET_TESTBED_Controller *c; const char *emsg; struct GNUNET_TESTBED_EventInformation info; uint16_t mtype; c = data->peer->controller; mtype = ntohs (msg->type); emsg = NULL; info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED; info.op = data->operation; info.op_cls = data->op_cls; if (GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT == mtype) { emsg = GNUNET_TESTBED_parse_error_string_ ((const struct GNUNET_TESTBED_OperationFailureEventMessage *) msg); if (NULL == emsg) emsg = "Unknown error"; info.details.operation_finished.emsg = emsg; info.details.operation_finished.generic = NULL; goto call_cb; } data->cfg = GNUNET_TESTBED_extract_config_ (msg); GNUNET_assert (NULL == data->op_result); data->op_result = data->ca (data->cada_cls, data->cfg); info.details.operation_finished.emsg = NULL; info.details.operation_finished.generic = data->op_result; data->state = SERVICE_CONNECTED; call_cb: if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) && (NULL != c->cc)) c->cc (c->cc_cls, &info); if (NULL != data->cb) data->cb (data->cb_cls, data->operation, data->op_result, emsg); }
/** * Handler for #GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages * * @param cls identification of the client * @param msg the actual message */ void handle_peer_create (void *cls, const struct GNUNET_TESTBED_PeerCreateMessage *msg) { struct GNUNET_SERVICE_Client *client = cls; struct GNUNET_MQ_Envelope *env; struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply; struct GNUNET_CONFIGURATION_Handle *cfg; struct ForwardedOperationContext *fo_ctxt; struct Route *route; struct Peer *peer; char *emsg; uint32_t host_id; uint32_t peer_id; host_id = ntohl (msg->host_id); peer_id = ntohl (msg->peer_id); if (VALID_PEER_ID (peer_id)) { (void) GNUNET_asprintf (&emsg, "Peer with ID %u already exists", peer_id); GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id), emsg); GNUNET_free (emsg); GNUNET_SERVICE_client_continue (client); return; } if (UINT32_MAX == peer_id) { GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id), "Cannot create peer with given ID"); GNUNET_SERVICE_client_continue (client); return; } if (host_id == GST_context->host_id) { /* We are responsible for this peer */ cfg = GNUNET_TESTBED_extract_config_ (&msg->header); if (NULL == cfg) { GNUNET_break (0); GNUNET_SERVICE_client_drop (client); return; } GNUNET_CONFIGURATION_set_value_number (cfg, "TESTBED", "PEERID", (unsigned long long) peer_id); GNUNET_CONFIGURATION_set_value_number (cfg, "PATHS", "PEERID", (unsigned long long) peer_id); peer = GNUNET_new (struct Peer); peer->is_remote = GNUNET_NO; peer->details.local.cfg = cfg; peer->id = peer_id; LOG_DEBUG ("Creating peer with id: %u\n", (unsigned int) peer->id); peer->details.local.peer = GNUNET_TESTING_peer_configure (GST_context->system, peer->details.local.cfg, peer->id, NULL /* Peer id */ , &emsg); if (NULL == peer->details.local.peer) { LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg); GNUNET_free (emsg); GNUNET_free (peer); GNUNET_break (0); GNUNET_SERVICE_client_drop (client); return; } peer->details.local.is_running = GNUNET_NO; peer_list_add (peer); env = GNUNET_MQ_msg (reply, GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS); reply->peer_id = msg->peer_id; reply->operation_id = msg->operation_id; GNUNET_MQ_send (GNUNET_SERVICE_client_get_mq (client), env); GNUNET_SERVICE_client_continue (client); return; }
/** * 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); }