2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api.c
23 * @brief API for accessing the GNUnet testing service.
24 * This library is supposed to make it easier to write
25 * testcases and script large-scale benchmarks.
26 * @author Christian Grothoff
27 * @author Sree Harsha Totakura
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_hello_lib.h"
40 #include "testbed_api.h"
41 #include "testbed_api_hosts.h"
42 #include "testbed_api_peers.h"
43 #include "testbed_api_operations.h"
46 * Generic logging shorthand
48 #define LOG(kind, ...) \
49 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
54 #define LOG_DEBUG(...) \
55 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
58 * Relative time seconds shorthand
60 #define TIME_REL_SECS(sec) \
61 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
65 * Default server message sending retry timeout
67 #define TIMEOUT_REL TIME_REL_SECS(1)
71 * The message queue for sending messages to the controller service
76 * The message to be sent
78 struct GNUNET_MessageHeader *msg;
81 * next pointer for DLL
83 struct MessageQueue *next;
86 * prev pointer for DLL
88 struct MessageQueue *prev;
93 * Structure for a controller link
98 * The next ptr for DLL
100 struct ControllerLink *next;
103 * The prev ptr for DLL
105 struct ControllerLink *prev;
108 * The host which will be referred in the peer start request. This is the
109 * host where the peer should be started
111 struct GNUNET_TESTBED_Host *delegated_host;
114 * The host which will contacted to delegate the peer start request
116 struct GNUNET_TESTBED_Host *slave_host;
119 * The configuration to be used to connect to slave host
121 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
124 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
125 * if we are just allowed to use the slave via TCP/IP
132 * handle for host registration
134 struct GNUNET_TESTBED_HostRegistrationHandle
137 * The host being registered
139 struct GNUNET_TESTBED_Host *host;
142 * The controller at which this host is being registered
144 struct GNUNET_TESTBED_Controller *c;
147 * The Registartion completion callback
149 GNUNET_TESTBED_HostRegistrationCompletion cc;
152 * The closure for above callback
159 * Returns the operation context with the given id if found in the Operation
160 * context queues of the controller
162 * @param c the controller whose queues are searched
163 * @param id the id which has to be checked
164 * @return the matching operation context; NULL if no match found
166 static struct OperationContext *
167 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
169 struct OperationContext *opc;
171 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
181 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
182 * controller (testbed service)
184 * @param c the controller handler
185 * @param msg message received
186 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
190 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
191 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
193 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
202 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
204 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
205 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
209 msg_size = ntohs (msg->header.size);
210 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
212 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
213 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
214 rh->cc (rh->cc_cls, NULL);
218 /* We have an error message */
219 emsg = (char *) &msg[1];
220 if ('\0' != emsg[msg_size -
221 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
227 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
228 ntohl (msg->host_id), emsg);
229 rh->cc (rh->cc_cls, emsg);
236 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
237 * controller (testbed service)
239 * @param c the controller handler
240 * @param msg message received
241 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
245 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
247 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
249 struct OperationContext *opc;
250 struct GNUNET_TESTBED_EventInformation *event;
253 op_id = GNUNET_ntohll (msg->operation_id);
254 LOG_DEBUG ("Operation %ul successful\n", op_id);
255 if (NULL == (opc = find_opc (c, op_id)))
257 LOG_DEBUG ("Operation not found\n");
261 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
262 event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
264 event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
267 case OP_PEER_DESTROY:
269 struct GNUNET_TESTBED_Peer *peer;
273 event->details.operation_finished.operation = opc->op;
274 event->details.operation_finished.op_cls = NULL;
275 event->details.operation_finished.emsg = NULL;
276 event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
277 event->details.operation_finished.op_result.generic = NULL;
288 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
289 opc->state = OPC_STATE_FINISHED;
293 c->cc (c->cc_cls, event);
301 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
302 * controller (testbed service)
304 * @param c the controller handler
305 * @param msg message received
306 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
310 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
312 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
314 struct OperationContext *opc;
315 struct PeerCreateData *data;
316 struct GNUNET_TESTBED_Peer *peer;
317 GNUNET_TESTBED_PeerCreateCallback cb;
321 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
322 == ntohs (msg->header.size));
323 op_id = GNUNET_ntohll (msg->operation_id);
324 if (NULL == (opc = find_opc (c, op_id)))
326 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
329 GNUNET_assert (OP_PEER_CREATE == opc->type);
330 GNUNET_assert (NULL != opc->data);
332 GNUNET_assert (NULL != data->peer);
334 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
335 peer->state = PS_CREATED;
338 GNUNET_free (opc->data);
339 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
340 opc->state = OPC_STATE_FINISHED;
342 cb (cls, peer, NULL);
348 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
349 * controller (testbed service)
351 * @param c the controller handler
352 * @param msg message received
353 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
357 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
358 const struct GNUNET_TESTBED_PeerEventMessage *msg)
360 struct OperationContext *opc;
361 struct GNUNET_TESTBED_Peer *peer;
362 struct GNUNET_TESTBED_EventInformation event;
365 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
366 == ntohs (msg->header.size));
367 op_id = GNUNET_ntohll (msg->operation_id);
368 if (NULL == (opc = find_opc (c, op_id)))
370 LOG_DEBUG ("Operation not found\n");
373 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
375 GNUNET_assert (NULL != peer);
376 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
379 case GNUNET_TESTBED_ET_PEER_START:
380 peer->state = PS_STARTED;
381 event.details.peer_start.host = peer->host;
382 event.details.peer_start.peer = peer;
384 case GNUNET_TESTBED_ET_PEER_STOP:
385 peer->state = PS_STOPPED;
386 event.details.peer_stop.peer = peer;
389 GNUNET_assert (0); /* We should never reach this state */
391 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
392 opc->state = OPC_STATE_FINISHED;
393 if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
397 c->cc (c->cc_cls, &event);
404 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
405 * controller (testbed service)
407 * @param c the controller handler
408 * @param msg message received
409 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
413 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
414 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
416 struct OperationContext *opc;
417 struct GNUNET_TESTBED_Peer *peer;
418 struct PeerInfoData *data;
419 struct PeerInfoData2 *response_data;
420 struct GNUNET_TESTBED_EventInformation info;
423 op_id = GNUNET_ntohll (msg->operation_id);
424 if (NULL == (opc = find_opc (c, op_id)))
426 LOG_DEBUG ("Operation not found\n");
430 GNUNET_assert (NULL != data);
432 GNUNET_assert (NULL != peer);
433 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
434 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
436 LOG_DEBUG ("Skipping operation callback as flag not set\n");
439 response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
440 response_data->pit = data->pit;
443 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
444 info.details.operation_finished.operation = opc->op;
445 info.details.operation_finished.op_cls = NULL;
446 info.details.operation_finished.emsg = NULL;
447 info.details.operation_finished.pit = response_data->pit;
448 switch (response_data->pit)
450 case GNUNET_TESTBED_PIT_IDENTITY:
452 struct GNUNET_PeerIdentity *peer_identity;
454 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
455 (void) memcpy (peer_identity, &msg->peer_identity,
456 sizeof (struct GNUNET_PeerIdentity));
457 response_data->details.peer_identity = peer_identity;
458 info.details.operation_finished.op_result.pid = peer_identity;
461 case GNUNET_TESTBED_PIT_CONFIGURATION:
463 struct GNUNET_CONFIGURATION_Handle *cfg;
469 config_size = (uLong) ntohs (msg->config_size);
470 config = GNUNET_malloc (config_size);
471 msize = ntohs (msg->header.size);
472 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
473 if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
474 (const Bytef *) &msg[1], (uLong) msize)))
476 cfg = GNUNET_CONFIGURATION_create (); /* Freed in oprelease_peer_getinfo */
477 GNUNET_assert (GNUNET_OK ==
478 GNUNET_CONFIGURATION_deserialize (cfg, config,
479 (size_t) config_size,
481 GNUNET_free (config);
482 response_data->details.cfg = cfg;
483 info.details.operation_finished.op_result.cfg = cfg;
486 case GNUNET_TESTBED_PIT_GENERIC:
487 GNUNET_assert (0); /* never reach here */
490 opc->data = response_data;
491 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
492 opc->state = OPC_STATE_FINISHED;
493 c->cc (c->cc_cls, &info);
499 * Handler for messages from controller (testbed service)
501 * @param cls the controller handler
502 * @param msg message received, NULL on timeout or fatal error
505 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
507 struct GNUNET_TESTBED_Controller *c = cls;
511 c->in_receive = GNUNET_NO;
512 /* FIXME: Add checks for message integrity */
515 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
519 msize = ntohs (msg->size);
520 switch (ntohs (msg->type))
522 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
523 GNUNET_assert (msize >= sizeof (struct
524 GNUNET_TESTBED_HostConfirmedMessage));
526 handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
528 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
530 (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
532 handle_opsuccess (c, (const struct
533 GNUNET_TESTBED_GenericOperationSuccessEventMessage
536 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
537 GNUNET_assert (msize ==
538 sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
540 handle_peer_create_success
541 (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
543 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
544 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
546 handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
549 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
550 GNUNET_assert (msize >=
551 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
554 (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
560 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
562 c->in_receive = GNUNET_YES;
563 GNUNET_CLIENT_receive (c->client, &message_handler, c,
564 GNUNET_TIME_UNIT_FOREVER_REL);
570 * Function called to notify a client about the connection begin ready to queue
571 * more data. "buf" will be NULL and "size" zero if the connection was closed
572 * for writing in the meantime.
575 * @param size number of bytes available in buf
576 * @param buf where the callee should write the message
577 * @return number of bytes written to buf
580 transmit_ready_notify (void *cls, size_t size, void *buf)
582 struct GNUNET_TESTBED_Controller *c = cls;
583 struct MessageQueue *mq_entry;
586 mq_entry = c->mq_head;
587 GNUNET_assert (NULL != mq_entry);
588 if ((0 == size) && (NULL == buf)) /* Timeout */
590 LOG_DEBUG ("Message sending timed out -- retrying\n");
592 GNUNET_CLIENT_notify_transmit_ready (c->client,
593 ntohs (mq_entry->msg->size),
595 GNUNET_YES, &transmit_ready_notify,
599 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
600 size = ntohs (mq_entry->msg->size);
601 memcpy (buf, mq_entry->msg, size);
602 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
603 ntohs (mq_entry->msg->type), size);
604 GNUNET_free (mq_entry->msg);
605 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
606 GNUNET_free (mq_entry);
607 mq_entry = c->mq_head;
608 if (NULL != mq_entry)
610 GNUNET_CLIENT_notify_transmit_ready (c->client,
611 ntohs (mq_entry->msg->size),
613 GNUNET_YES, &transmit_ready_notify,
615 if (GNUNET_NO == c->in_receive)
617 c->in_receive = GNUNET_YES;
618 GNUNET_CLIENT_receive (c->client, &message_handler, c,
619 GNUNET_TIME_UNIT_FOREVER_REL);
626 * Queues a message in send queue for sending to the service
628 * @param controller the handle to the controller
629 * @param msg the message to queue
632 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
633 struct GNUNET_MessageHeader *msg)
635 struct MessageQueue *mq_entry;
639 type = ntohs (msg->type);
640 size = ntohs (msg->size);
641 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
642 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
643 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Queueing message of type %u, size %u for sending\n", type,
648 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
650 if (NULL == controller->th)
652 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
654 GNUNET_YES, &transmit_ready_notify,
660 * Handle for controller process
662 struct GNUNET_TESTBED_ControllerProc
667 struct GNUNET_HELPER_Handle *helper;
670 * The host where the helper is run
672 struct GNUNET_TESTBED_Host *host;
675 * The controller error callback
677 GNUNET_TESTBED_ControllerStatusCallback cb;
680 * The closure for the above callback
685 * The send handle for the helper
687 struct GNUNET_HELPER_SendHandle *shandle;
690 * The message corresponding to send handle
692 struct GNUNET_MessageHeader *msg;
695 * The port number for ssh; used for helpers starting ssh
700 * The ssh destination string; used for helpers starting ssh
705 * The configuration of the running testbed service
707 struct GNUNET_CONFIGURATION_Handle *cfg;
713 * Functions with this signature are called whenever a
714 * complete message is received by the tokenizer.
716 * Do not call GNUNET_SERVER_mst_destroy in callback
719 * @param client identification of the client
720 * @param message the actual message
722 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
724 static int helper_mst (void *cls, void *client,
725 const struct GNUNET_MessageHeader *message)
727 struct GNUNET_TESTBED_ControllerProc *cp = cls;
728 const struct GNUNET_TESTBED_HelperReply *msg;
729 const char *hostname;
734 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
735 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
736 < ntohs (msg->header.size));
737 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
738 == ntohs (msg->header.type));
739 config_size = (uLongf) ntohs (msg->config_size);
740 xconfig_size = (uLongf) (ntohs (msg->header.size)
741 - sizeof (struct GNUNET_TESTBED_HelperReply));
742 config = GNUNET_malloc (config_size);
743 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
744 (const Bytef *) &msg[1], xconfig_size));
745 GNUNET_assert (NULL == cp->cfg);
746 cp->cfg = GNUNET_CONFIGURATION_create ();
747 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
748 config_size, GNUNET_NO));
749 GNUNET_free (config);
750 if ((NULL == cp->host) ||
751 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
752 hostname = "localhost";
753 /* Change the hostname so that we can connect to it */
754 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
756 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
762 * Continuation function from GNUNET_HELPER_send()
765 * @param result GNUNET_OK on success,
766 * GNUNET_NO if helper process died
767 * GNUNET_SYSERR during GNUNET_HELPER_stop
770 clear_msg (void *cls, int result)
772 struct GNUNET_TESTBED_ControllerProc *cp = cls;
774 GNUNET_assert (NULL != cp->shandle);
776 GNUNET_free (cp->msg);
781 * Callback that will be called when the helper process dies. This is not called
782 * when the helper process is stoped using GNUNET_HELPER_stop()
784 * @param cls the closure from GNUNET_HELPER_start()
787 helper_exp_cb (void *cls)
789 struct GNUNET_TESTBED_ControllerProc *cp = cls;
790 GNUNET_TESTBED_ControllerStatusCallback cb;
795 GNUNET_TESTBED_controller_stop (cp);
797 cb (cb_cls, NULL, GNUNET_SYSERR);
802 * Starts a controller process at the host. FIXME: add controller start callback
803 * with the configuration with which the controller is started
805 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
806 * host when starting testbed controller at host
807 * @param host the host where the controller has to be started; NULL for
809 * @param cfg template configuration to use for the remote controller; the
810 * remote controller will be started with a slightly modified
811 * configuration (port numbers, unix domain sockets and service home
812 * values are changed as per TESTING library on the remote host)
813 * @param cb function called when the controller is successfully started or
814 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
815 * called if cb is called with GNUNET_SYSERR as status. Will never be
816 * called in the same task as 'GNUNET_TESTBED_controller_start'
817 * (synchronous errors will be signalled by returning NULL). This
818 * parameter cannot be NULL.
819 * @param cls closure for above callbacks
820 * @return the controller process handle, NULL on errors
822 struct GNUNET_TESTBED_ControllerProc *
823 GNUNET_TESTBED_controller_start (const char *controller_ip,
824 struct GNUNET_TESTBED_Host *host,
825 const struct GNUNET_CONFIGURATION_Handle *cfg,
826 GNUNET_TESTBED_ControllerStatusCallback cb,
829 struct GNUNET_TESTBED_ControllerProc *cp;
830 struct GNUNET_TESTBED_HelperInit *msg;
832 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
833 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
835 char * const binary_argv[] = {
836 "gnunet-testbed-helper", NULL
839 cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
840 &helper_mst, &helper_exp_cb, cp);
844 char *remote_args[6 + 1];
846 const char *username;
847 const char *hostname;
849 username = GNUNET_TESTBED_host_get_username_ (host);
850 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
851 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
852 if (NULL == username)
853 GNUNET_asprintf (&cp->dst, "%s", hostname);
855 GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
857 remote_args[argp++] = "ssh";
858 remote_args[argp++] = "-p";
859 remote_args[argp++] = cp->port;
860 remote_args[argp++] = "-q";
861 remote_args[argp++] = cp->dst;
862 remote_args[argp++] = "gnunet-testbed-helper";
863 remote_args[argp++] = NULL;
864 GNUNET_assert (argp == 6 + 1);
865 cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
866 &helper_mst, &helper_exp_cb, cp);
868 if (NULL == cp->helper)
870 GNUNET_free_non_null (cp->port);
871 GNUNET_free_non_null (cp->dst);
878 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
879 cp->msg = &msg->header;
880 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
882 if (NULL == cp->shandle)
885 GNUNET_TESTBED_controller_stop (cp);
893 * Stop the controller process (also will terminate all peers and controllers
894 * dependent on this controller). This function blocks until the testbed has
895 * been fully terminated (!).
897 * @param cproc the controller process handle
900 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
902 if (NULL != cproc->shandle)
903 GNUNET_HELPER_send_cancel (cproc->shandle);
904 GNUNET_HELPER_stop (cproc->helper);
905 if (NULL != cproc->cfg)
906 GNUNET_CONFIGURATION_destroy (cproc->cfg);
907 GNUNET_free_non_null (cproc->port);
908 GNUNET_free_non_null (cproc->dst);
914 * Start a controller process using the given configuration at the
917 * @param cfg configuration to use
918 * @param host host to run the controller on; This should be the same host if
919 * the controller was previously started with
920 * GNUNET_TESTBED_controller_start; NULL for localhost
921 * @param event_mask bit mask with set of events to call 'cc' for;
922 * or-ed values of "1LL" shifted by the
923 * respective 'enum GNUNET_TESTBED_EventType'
924 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
925 * @param cc controller callback to invoke on events
926 * @param cc_cls closure for cc
927 * @return handle to the controller
929 struct GNUNET_TESTBED_Controller *
930 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
931 struct GNUNET_TESTBED_Host *host,
933 GNUNET_TESTBED_ControllerCallback cc,
936 struct GNUNET_TESTBED_Controller *controller;
937 struct GNUNET_TESTBED_InitMessage *msg;
938 unsigned long long max_parallel_peer_create;
941 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
942 "MAX_PARALLEL_PEER_CREATE",
943 &max_parallel_peer_create))
948 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
950 controller->cc_cls = cc_cls;
951 controller->event_mask = event_mask;
952 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
953 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
954 if (NULL == controller->client)
956 GNUNET_TESTBED_controller_disconnect (controller);
961 host = GNUNET_TESTBED_host_create_by_id_ (0);
964 LOG (GNUNET_ERROR_TYPE_WARNING,
965 "Treating NULL host as localhost. Multiple references to localhost "
966 "may break when localhost freed before calling disconnect \n");
967 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
971 controller->aux_host = GNUNET_YES;
974 GNUNET_assert (NULL != host);
975 controller->opq_peer_create =
976 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
977 max_parallel_peer_create);
978 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
979 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
980 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
981 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
982 msg->event_mask = GNUNET_htonll (controller->event_mask);
983 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
991 * Configure shared services at a controller. Using this function,
992 * you can specify that certain services (such as "resolver")
993 * should not be run for each peer but instead be shared
994 * across N peers on the specified host. This function
995 * must be called before any peers are created at the host.
997 * @param controller controller to configure
998 * @param service_name name of the service to share
999 * @param num_peers number of peers that should share one instance
1000 * of the specified service (1 for no sharing is the default),
1001 * use 0 to disable the service
1004 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
1005 const char *service_name,
1008 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1009 uint16_t service_name_size;
1012 service_name_size = strlen (service_name) + 1;
1013 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1014 + service_name_size;
1015 msg = GNUNET_malloc (msg_size);
1016 msg->header.size = htons (msg_size);
1017 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1018 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1019 msg->num_peers = htonl (num_peers);
1020 memcpy (&msg[1], service_name, service_name_size);
1021 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1026 * disconnects from the controller.
1028 * @param controller handle to controller to stop
1031 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1033 struct MessageQueue *mq_entry;
1035 if (NULL != controller->th)
1036 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1037 /* Clear the message queue */
1038 while (NULL != (mq_entry = controller->mq_head))
1040 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1041 controller->mq_tail,
1043 GNUNET_free (mq_entry->msg);
1044 GNUNET_free (mq_entry);
1046 if (NULL != controller->client)
1047 GNUNET_CLIENT_disconnect (controller->client);
1048 GNUNET_CONFIGURATION_destroy (controller->cfg);
1049 if (GNUNET_YES == controller->aux_host)
1050 GNUNET_TESTBED_host_destroy (controller->host);
1051 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_peer_create);
1052 GNUNET_free (controller);
1057 * Register a host with the controller
1059 * @param controller the controller handle
1060 * @param host the host to register
1061 * @param cc the completion callback to call to inform the status of
1062 * registration. After calling this callback the registration handle
1063 * will be invalid. Cannot be NULL.
1064 * @param cc_cls the closure for the cc
1065 * @return handle to the host registration which can be used to cancel the
1068 struct GNUNET_TESTBED_HostRegistrationHandle *
1069 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1070 struct GNUNET_TESTBED_Host *host,
1071 GNUNET_TESTBED_HostRegistrationCompletion cc,
1074 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1075 struct GNUNET_TESTBED_AddHostMessage *msg;
1076 const char *username;
1077 const char *hostname;
1079 uint16_t user_name_length;
1081 if (NULL != controller->rh)
1083 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1084 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1086 LOG (GNUNET_ERROR_TYPE_WARNING,
1087 "Host hostname: %s already registered\n",
1088 (NULL == hostname) ? "localhost" : hostname);
1091 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1094 GNUNET_assert (NULL != cc);
1096 rh->cc_cls = cc_cls;
1097 controller->rh = rh;
1098 username = GNUNET_TESTBED_host_get_username_ (host);
1099 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1100 user_name_length = 0;
1101 if (NULL != username)
1103 user_name_length = strlen (username) + 1;
1104 msg_size += user_name_length;
1106 /* FIXME: what happens when hostname is NULL? localhost */
1107 GNUNET_assert (NULL != hostname);
1108 msg_size += strlen (hostname) + 1;
1109 msg = GNUNET_malloc (msg_size);
1110 msg->header.size = htons (msg_size);
1111 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1112 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1113 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1114 msg->user_name_length = htons (user_name_length);
1115 if (NULL != username)
1116 memcpy (&msg[1], username, user_name_length);
1117 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1118 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1124 * Cancel the pending registration. Note that if the registration message is
1125 * already sent to the service the cancellation has only the effect that the
1126 * registration completion callback for the registration is never called.
1128 * @param handle the registration handle to cancel
1131 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1134 if (handle != handle->c->rh)
1139 handle->c->rh = NULL;
1140 GNUNET_free (handle);
1145 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1146 * serialized and compressed
1148 * @param master handle to the master controller who creates the association
1149 * @param delegated_host requests to which host should be delegated; cannot be NULL
1150 * @param slave_host which host is used to run the slave controller; use NULL to
1151 * make the master controller connect to the delegated host
1152 * @param sxcfg serialized and compressed configuration
1153 * @param sxcfg_size the size scfg
1154 * @param scfg_size the size of uncompressed serialized configuration
1155 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1156 * be started by the master controller; GNUNET_NO if we are just
1157 * allowed to use the slave via TCP/IP
1160 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1161 struct GNUNET_TESTBED_Host *delegated_host,
1162 struct GNUNET_TESTBED_Host *slave_host,
1168 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1171 GNUNET_assert (GNUNET_YES ==
1172 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1173 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1174 GNUNET_assert (GNUNET_YES ==
1175 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1176 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1177 msg = GNUNET_malloc (msg_size);
1178 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1179 msg->header.size = htons (msg_size);
1180 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1181 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
1182 ((NULL != slave_host) ? slave_host : master->host));
1183 msg->config_size = htons ((uint16_t) scfg_size);
1184 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1185 memcpy (&msg[1], sxcfg, sxcfg_size);
1186 GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1191 * Compresses given configuration using zlib compress
1193 * @param config the serialized configuration
1194 * @param size the size of config
1195 * @param xconfig will be set to the compressed configuration (memory is fresly
1197 * @return the size of the xconfig
1200 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1205 xsize = compressBound ((uLong) size);
1206 *xconfig = GNUNET_malloc (xsize);
1207 GNUNET_assert (Z_OK ==
1208 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1209 (const Bytef *) config, (uLongf) size,
1216 * Create a link from slave controller to delegated controller. Whenever the
1217 * master controller is asked to start a peer at the delegated controller the
1218 * request will be routed towards slave controller (if a route exists). The
1219 * slave controller will then route it to the delegated controller. The
1220 * configuration of the slave controller is given and to be used to either
1221 * create the slave controller or to connect to an existing slave controller
1222 * process. 'is_subordinate' specifies if the given slave controller should be
1223 * started and managed by the master controller, or if the slave already has a
1224 * master and this is just a secondary master that is also allowed to use the
1227 * @param master handle to the master controller who creates the association
1228 * @param delegated_host requests to which host should be delegated
1229 * @param slave_host which host is used to run the slave controller
1230 * @param slave_cfg configuration to use for the slave controller
1231 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1232 * by the master controller; GNUNET_NO if we are just
1233 * allowed to use the slave via TCP/IP
1236 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1237 struct GNUNET_TESTBED_Host *delegated_host,
1238 struct GNUNET_TESTBED_Host *slave_host,
1239 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1247 GNUNET_assert (GNUNET_YES ==
1248 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1249 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1250 GNUNET_assert (GNUNET_YES ==
1251 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1252 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1253 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1254 GNUNET_free (config);
1255 GNUNET_assert ((UINT16_MAX -
1256 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1257 >= cc_size); /* Configuration doesn't fit in 1 message */
1258 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1259 (const char *) cconfig,
1260 cc_size, config_size, is_subordinate);
1261 GNUNET_free (cconfig);
1266 * Ask the testbed controller to write the current overlay topology to
1267 * a file. Naturally, the file will only contain a snapshot as the
1268 * topology may evolve all the time.
1270 * @param controller overlay controller to inspect
1271 * @param filename name of the file the topology should
1275 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1276 const char *filename)
1283 * Creates a helper initialization message. Only for testing.
1285 * @param cname the ip address of the controlling host
1286 * @param cfg the configuration that has to used to start the testbed service
1288 * @return the initialization message
1290 struct GNUNET_TESTBED_HelperInit *
1291 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1292 const struct GNUNET_CONFIGURATION_Handle *cfg)
1294 struct GNUNET_TESTBED_HelperInit *msg;
1298 size_t xconfig_size;
1302 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1303 GNUNET_assert (NULL != config);
1305 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1306 GNUNET_free (config);
1307 cname_len = strlen (cname);
1308 msg_size = xconfig_size + cname_len + 1 +
1309 sizeof (struct GNUNET_TESTBED_HelperInit);
1310 msg = GNUNET_realloc (xconfig, msg_size);
1311 (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1312 msg->header.size = htons (msg_size);
1313 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1314 msg->cname_size = htons (cname_len);
1315 msg->config_size = htons (config_size);
1316 (void) strcpy ((char *) &msg[1], cname);
1322 * Cancel a pending operation. Releases all resources
1323 * of the operation and will ensure that no event
1324 * is generated for the operation. Does NOT guarantee
1325 * that the operation will be fully undone (or that
1326 * nothing ever happened).
1328 * @param operation operation to cancel
1331 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1333 GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1334 operation->controller->op_tail,
1336 GNUNET_TESTBED_operation_done (operation);
1341 * Signal that the information from an operation has been fully
1342 * processed. This function MUST be called for each event
1343 * of type 'operation_finished' to fully remove the operation
1344 * from the operation queue. After calling this function, the
1345 * 'op_result' becomes invalid (!).
1347 * @param operation operation to signal completion for
1350 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1352 switch (operation->type)
1354 case OP_PEER_CREATE:
1355 case OP_PEER_DESTROY:
1359 GNUNET_TESTBED_operation_release_ (operation);
1361 case OP_OVERLAY_CONNECT:
1362 GNUNET_free_non_null (operation->data);
1365 GNUNET_free (operation);
1369 /* end of testbed_api.c */