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;
291 c->cc (c->cc_cls, event);
299 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
300 * controller (testbed service)
302 * @param c the controller handler
303 * @param msg message received
304 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
308 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
310 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
312 struct OperationContext *opc;
313 struct PeerCreateData *data;
314 struct GNUNET_TESTBED_Peer *peer;
315 GNUNET_TESTBED_PeerCreateCallback cb;
319 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
320 == ntohs (msg->header.size));
321 op_id = GNUNET_ntohll (msg->operation_id);
322 if (NULL == (opc = find_opc (c, op_id)))
324 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
327 GNUNET_assert (OP_PEER_CREATE == opc->type);
328 GNUNET_assert (NULL != opc->data);
330 GNUNET_assert (NULL != data->peer);
332 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
333 peer->state = PS_CREATED;
337 cb (cls, peer, NULL);
343 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
344 * controller (testbed service)
346 * @param c the controller handler
347 * @param msg message received
348 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
352 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
353 const struct GNUNET_TESTBED_PeerEventMessage *msg)
355 struct OperationContext *opc;
356 struct GNUNET_TESTBED_Peer *peer;
357 struct GNUNET_TESTBED_EventInformation event;
360 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
361 == ntohs (msg->header.size));
362 op_id = GNUNET_ntohll (msg->operation_id);
363 if (NULL == (opc = find_opc (c, op_id)))
365 LOG_DEBUG ("Operation not found\n");
368 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
370 GNUNET_assert (NULL != peer);
371 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
374 case GNUNET_TESTBED_ET_PEER_START:
375 peer->state = PS_STARTED;
376 event.details.peer_start.host = peer->host;
377 event.details.peer_start.peer = peer;
379 case GNUNET_TESTBED_ET_PEER_STOP:
380 peer->state = PS_STOPPED;
381 event.details.peer_stop.peer = peer;
384 GNUNET_assert (0); /* We should never reach this state */
386 if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
390 c->cc (c->cc_cls, &event);
397 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
398 * controller (testbed service)
400 * @param c the controller handler
401 * @param msg message received
402 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
406 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
407 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
409 struct OperationContext *opc;
410 struct GNUNET_TESTBED_Peer *peer;
411 struct PeerInfoData *data;
412 struct PeerInfoData2 *response_data;
413 struct GNUNET_TESTBED_EventInformation info;
416 op_id = GNUNET_ntohll (msg->operation_id);
417 if (NULL == (opc = find_opc (c, op_id)))
419 LOG_DEBUG ("Operation not found\n");
423 GNUNET_assert (NULL != data);
425 GNUNET_assert (NULL != peer);
426 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
427 opc->completed = GNUNET_YES;
428 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
430 LOG_DEBUG ("Skipping operation callback as flag not set\n");
433 response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
434 response_data->pit = data->pit;
437 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
438 info.details.operation_finished.operation = opc->op;
439 info.details.operation_finished.op_cls = NULL;
440 info.details.operation_finished.emsg = NULL;
441 info.details.operation_finished.pit = response_data->pit;
442 switch (response_data->pit)
444 case GNUNET_TESTBED_PIT_IDENTITY:
446 struct GNUNET_PeerIdentity *peer_identity;
448 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
449 (void) memcpy (peer_identity, &msg->peer_identity,
450 sizeof (struct GNUNET_PeerIdentity));
451 response_data->details.peer_identity = peer_identity;
452 info.details.operation_finished.op_result.pid = peer_identity;
455 case GNUNET_TESTBED_PIT_CONFIGURATION:
457 struct GNUNET_CONFIGURATION_Handle *cfg;
463 config_size = (uLong) ntohs (msg->config_size);
464 config = GNUNET_malloc (config_size);
465 msize = ntohs (msg->header.size);
466 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
467 if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
468 (const Bytef *) &msg[1], (uLong) msize)))
470 cfg = GNUNET_CONFIGURATION_create (); /* Freed in oprelease_peer_getinfo */
471 GNUNET_assert (GNUNET_OK ==
472 GNUNET_CONFIGURATION_deserialize (cfg, config,
473 (size_t) config_size,
475 GNUNET_free (config);
476 response_data->details.cfg = cfg;
477 info.details.operation_finished.op_result.cfg = cfg;
480 case GNUNET_TESTBED_PIT_GENERIC:
481 GNUNET_assert (0); /* never reach here */
484 opc->data = response_data;
485 c->cc (c->cc_cls, &info);
491 * Handler for messages from controller (testbed service)
493 * @param cls the controller handler
494 * @param msg message received, NULL on timeout or fatal error
497 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
499 struct GNUNET_TESTBED_Controller *c = cls;
503 c->in_receive = GNUNET_NO;
504 /* FIXME: Add checks for message integrity */
507 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
511 msize = ntohs (msg->size);
512 switch (ntohs (msg->type))
514 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
515 GNUNET_assert (msize >= sizeof (struct
516 GNUNET_TESTBED_HostConfirmedMessage));
518 handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
520 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
522 (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
524 handle_opsuccess (c, (const struct
525 GNUNET_TESTBED_GenericOperationSuccessEventMessage
528 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
529 GNUNET_assert (msize ==
530 sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
532 handle_peer_create_success
533 (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
535 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
536 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
538 handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
541 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
542 GNUNET_assert (msize >=
543 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
546 (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
552 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
554 c->in_receive = GNUNET_YES;
555 GNUNET_CLIENT_receive (c->client, &message_handler, c,
556 GNUNET_TIME_UNIT_FOREVER_REL);
562 * Function called to notify a client about the connection begin ready to queue
563 * more data. "buf" will be NULL and "size" zero if the connection was closed
564 * for writing in the meantime.
567 * @param size number of bytes available in buf
568 * @param buf where the callee should write the message
569 * @return number of bytes written to buf
572 transmit_ready_notify (void *cls, size_t size, void *buf)
574 struct GNUNET_TESTBED_Controller *c = cls;
575 struct MessageQueue *mq_entry;
578 mq_entry = c->mq_head;
579 GNUNET_assert (NULL != mq_entry);
580 if ((0 == size) && (NULL == buf)) /* Timeout */
582 LOG_DEBUG ("Message sending timed out -- retrying\n");
584 GNUNET_CLIENT_notify_transmit_ready (c->client,
585 ntohs (mq_entry->msg->size),
587 GNUNET_YES, &transmit_ready_notify,
591 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
592 size = ntohs (mq_entry->msg->size);
593 memcpy (buf, mq_entry->msg, size);
594 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
595 ntohs (mq_entry->msg->type), size);
596 GNUNET_free (mq_entry->msg);
597 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
598 GNUNET_free (mq_entry);
599 mq_entry = c->mq_head;
600 if (NULL != mq_entry)
602 GNUNET_CLIENT_notify_transmit_ready (c->client,
603 ntohs (mq_entry->msg->size),
605 GNUNET_YES, &transmit_ready_notify,
607 if (GNUNET_NO == c->in_receive)
609 c->in_receive = GNUNET_YES;
610 GNUNET_CLIENT_receive (c->client, &message_handler, c,
611 GNUNET_TIME_UNIT_FOREVER_REL);
618 * Queues a message in send queue for sending to the service
620 * @param controller the handle to the controller
621 * @param msg the message to queue
624 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
625 struct GNUNET_MessageHeader *msg)
627 struct MessageQueue *mq_entry;
631 type = ntohs (msg->type);
632 size = ntohs (msg->size);
633 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
634 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
635 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
637 LOG (GNUNET_ERROR_TYPE_DEBUG,
638 "Queueing message of type %u, size %u for sending\n", type,
640 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
642 if (NULL == controller->th)
644 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
646 GNUNET_YES, &transmit_ready_notify,
652 * Handle for controller process
654 struct GNUNET_TESTBED_ControllerProc
659 struct GNUNET_HELPER_Handle *helper;
662 * The host where the helper is run
664 struct GNUNET_TESTBED_Host *host;
667 * The controller error callback
669 GNUNET_TESTBED_ControllerStatusCallback cb;
672 * The closure for the above callback
677 * The send handle for the helper
679 struct GNUNET_HELPER_SendHandle *shandle;
682 * The message corresponding to send handle
684 struct GNUNET_MessageHeader *msg;
687 * The port number for ssh; used for helpers starting ssh
692 * The ssh destination string; used for helpers starting ssh
697 * The configuration of the running testbed service
699 struct GNUNET_CONFIGURATION_Handle *cfg;
705 * Functions with this signature are called whenever a
706 * complete message is received by the tokenizer.
708 * Do not call GNUNET_SERVER_mst_destroy in callback
711 * @param client identification of the client
712 * @param message the actual message
714 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
716 static int helper_mst (void *cls, void *client,
717 const struct GNUNET_MessageHeader *message)
719 struct GNUNET_TESTBED_ControllerProc *cp = cls;
720 const struct GNUNET_TESTBED_HelperReply *msg;
721 const char *hostname;
726 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
727 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
728 < ntohs (msg->header.size));
729 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
730 == ntohs (msg->header.type));
731 config_size = (uLongf) ntohs (msg->config_size);
732 xconfig_size = (uLongf) (ntohs (msg->header.size)
733 - sizeof (struct GNUNET_TESTBED_HelperReply));
734 config = GNUNET_malloc (config_size);
735 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
736 (const Bytef *) &msg[1], xconfig_size));
737 GNUNET_assert (NULL == cp->cfg);
738 cp->cfg = GNUNET_CONFIGURATION_create ();
739 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
740 config_size, GNUNET_NO));
741 GNUNET_free (config);
742 if ((NULL == cp->host) ||
743 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
744 hostname = "localhost";
745 /* Change the hostname so that we can connect to it */
746 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
748 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
754 * Continuation function from GNUNET_HELPER_send()
757 * @param result GNUNET_OK on success,
758 * GNUNET_NO if helper process died
759 * GNUNET_SYSERR during GNUNET_HELPER_stop
762 clear_msg (void *cls, int result)
764 struct GNUNET_TESTBED_ControllerProc *cp = cls;
766 GNUNET_assert (NULL != cp->shandle);
768 GNUNET_free (cp->msg);
773 * Callback that will be called when the helper process dies. This is not called
774 * when the helper process is stoped using GNUNET_HELPER_stop()
776 * @param cls the closure from GNUNET_HELPER_start()
779 helper_exp_cb (void *cls)
781 struct GNUNET_TESTBED_ControllerProc *cp = cls;
782 GNUNET_TESTBED_ControllerStatusCallback cb;
787 GNUNET_TESTBED_controller_stop (cp);
789 cb (cb_cls, NULL, GNUNET_SYSERR);
794 * Starts a controller process at the host. FIXME: add controller start callback
795 * with the configuration with which the controller is started
797 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
798 * host when starting testbed controller at host
799 * @param host the host where the controller has to be started; NULL for
801 * @param cfg template configuration to use for the remote controller; the
802 * remote controller will be started with a slightly modified
803 * configuration (port numbers, unix domain sockets and service home
804 * values are changed as per TESTING library on the remote host)
805 * @param cb function called when the controller is successfully started or
806 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
807 * called if cb is called with GNUNET_SYSERR as status. Will never be
808 * called in the same task as 'GNUNET_TESTBED_controller_start'
809 * (synchronous errors will be signalled by returning NULL). This
810 * parameter cannot be NULL.
811 * @param cls closure for above callbacks
812 * @return the controller process handle, NULL on errors
814 struct GNUNET_TESTBED_ControllerProc *
815 GNUNET_TESTBED_controller_start (const char *controller_ip,
816 struct GNUNET_TESTBED_Host *host,
817 const struct GNUNET_CONFIGURATION_Handle *cfg,
818 GNUNET_TESTBED_ControllerStatusCallback cb,
821 struct GNUNET_TESTBED_ControllerProc *cp;
822 struct GNUNET_TESTBED_HelperInit *msg;
824 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
825 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
827 char * const binary_argv[] = {
828 "gnunet-testbed-helper", NULL
831 cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
832 &helper_mst, &helper_exp_cb, cp);
836 char *remote_args[6 + 1];
838 const char *username;
839 const char *hostname;
841 username = GNUNET_TESTBED_host_get_username_ (host);
842 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
843 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
844 if (NULL == username)
845 GNUNET_asprintf (&cp->dst, "%s", hostname);
847 GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
849 remote_args[argp++] = "ssh";
850 remote_args[argp++] = "-p";
851 remote_args[argp++] = cp->port;
852 remote_args[argp++] = "-q";
853 remote_args[argp++] = cp->dst;
854 remote_args[argp++] = "gnunet-testbed-helper";
855 remote_args[argp++] = NULL;
856 GNUNET_assert (argp == 6 + 1);
857 cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
858 &helper_mst, &helper_exp_cb, cp);
860 if (NULL == cp->helper)
862 GNUNET_free_non_null (cp->port);
863 GNUNET_free_non_null (cp->dst);
870 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
871 cp->msg = &msg->header;
872 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
874 if (NULL == cp->shandle)
877 GNUNET_TESTBED_controller_stop (cp);
885 * Stop the controller process (also will terminate all peers and controllers
886 * dependent on this controller). This function blocks until the testbed has
887 * been fully terminated (!).
889 * @param cproc the controller process handle
892 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
894 if (NULL != cproc->shandle)
895 GNUNET_HELPER_send_cancel (cproc->shandle);
896 GNUNET_HELPER_stop (cproc->helper);
897 if (NULL != cproc->cfg)
898 GNUNET_CONFIGURATION_destroy (cproc->cfg);
899 GNUNET_free_non_null (cproc->port);
900 GNUNET_free_non_null (cproc->dst);
906 * Start a controller process using the given configuration at the
909 * @param cfg configuration to use
910 * @param host host to run the controller on; This should be the same host if
911 * the controller was previously started with
912 * GNUNET_TESTBED_controller_start; NULL for localhost
913 * @param event_mask bit mask with set of events to call 'cc' for;
914 * or-ed values of "1LL" shifted by the
915 * respective 'enum GNUNET_TESTBED_EventType'
916 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
917 * @param cc controller callback to invoke on events
918 * @param cc_cls closure for cc
919 * @return handle to the controller
921 struct GNUNET_TESTBED_Controller *
922 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
923 struct GNUNET_TESTBED_Host *host,
925 GNUNET_TESTBED_ControllerCallback cc,
928 struct GNUNET_TESTBED_Controller *controller;
929 struct GNUNET_TESTBED_InitMessage *msg;
930 unsigned long long max_parallel_peer_create;
933 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
934 "MAX_PARALLEL_PEER_CREATE",
935 &max_parallel_peer_create))
940 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
942 controller->cc_cls = cc_cls;
943 controller->event_mask = event_mask;
944 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
945 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
946 if (NULL == controller->client)
948 GNUNET_TESTBED_controller_disconnect (controller);
953 host = GNUNET_TESTBED_host_create_by_id_ (0);
956 LOG (GNUNET_ERROR_TYPE_WARNING,
957 "Treating NULL host as localhost. Multiple references to localhost "
958 "may break when localhost freed before calling disconnect \n");
959 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
963 controller->aux_host = GNUNET_YES;
966 GNUNET_assert (NULL != host);
967 controller->opq_peer_create =
968 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
969 max_parallel_peer_create);
970 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
971 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
972 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
973 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
974 msg->event_mask = GNUNET_htonll (controller->event_mask);
975 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
983 * Configure shared services at a controller. Using this function,
984 * you can specify that certain services (such as "resolver")
985 * should not be run for each peer but instead be shared
986 * across N peers on the specified host. This function
987 * must be called before any peers are created at the host.
989 * @param controller controller to configure
990 * @param service_name name of the service to share
991 * @param num_peers number of peers that should share one instance
992 * of the specified service (1 for no sharing is the default),
993 * use 0 to disable the service
996 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
997 const char *service_name,
1000 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1001 uint16_t service_name_size;
1004 service_name_size = strlen (service_name) + 1;
1005 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1006 + service_name_size;
1007 msg = GNUNET_malloc (msg_size);
1008 msg->header.size = htons (msg_size);
1009 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1010 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1011 msg->num_peers = htonl (num_peers);
1012 memcpy (&msg[1], service_name, service_name_size);
1013 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1018 * disconnects from the controller.
1020 * @param controller handle to controller to stop
1023 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1025 struct MessageQueue *mq_entry;
1027 if (NULL != controller->th)
1028 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1029 /* Clear the message queue */
1030 while (NULL != (mq_entry = controller->mq_head))
1032 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1033 controller->mq_tail,
1035 GNUNET_free (mq_entry->msg);
1036 GNUNET_free (mq_entry);
1038 if (NULL != controller->client)
1039 GNUNET_CLIENT_disconnect (controller->client);
1040 GNUNET_CONFIGURATION_destroy (controller->cfg);
1041 if (GNUNET_YES == controller->aux_host)
1042 GNUNET_TESTBED_host_destroy (controller->host);
1043 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_peer_create);
1044 GNUNET_free (controller);
1049 * Register a host with the controller
1051 * @param controller the controller handle
1052 * @param host the host to register
1053 * @param cc the completion callback to call to inform the status of
1054 * registration. After calling this callback the registration handle
1055 * will be invalid. Cannot be NULL.
1056 * @param cc_cls the closure for the cc
1057 * @return handle to the host registration which can be used to cancel the
1060 struct GNUNET_TESTBED_HostRegistrationHandle *
1061 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1062 struct GNUNET_TESTBED_Host *host,
1063 GNUNET_TESTBED_HostRegistrationCompletion cc,
1066 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1067 struct GNUNET_TESTBED_AddHostMessage *msg;
1068 const char *username;
1069 const char *hostname;
1071 uint16_t user_name_length;
1073 if (NULL != controller->rh)
1075 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1076 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1078 LOG (GNUNET_ERROR_TYPE_WARNING,
1079 "Host hostname: %s already registered\n",
1080 (NULL == hostname) ? "localhost" : hostname);
1083 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1086 GNUNET_assert (NULL != cc);
1088 rh->cc_cls = cc_cls;
1089 controller->rh = rh;
1090 username = GNUNET_TESTBED_host_get_username_ (host);
1091 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1092 user_name_length = 0;
1093 if (NULL != username)
1095 user_name_length = strlen (username) + 1;
1096 msg_size += user_name_length;
1098 /* FIXME: what happens when hostname is NULL? localhost */
1099 GNUNET_assert (NULL != hostname);
1100 msg_size += strlen (hostname) + 1;
1101 msg = GNUNET_malloc (msg_size);
1102 msg->header.size = htons (msg_size);
1103 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1104 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1105 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1106 msg->user_name_length = htons (user_name_length);
1107 if (NULL != username)
1108 memcpy (&msg[1], username, user_name_length);
1109 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1110 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1116 * Cancel the pending registration. Note that if the registration message is
1117 * already sent to the service the cancellation has only the effect that the
1118 * registration completion callback for the registration is never called.
1120 * @param handle the registration handle to cancel
1123 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1126 if (handle != handle->c->rh)
1131 handle->c->rh = NULL;
1132 GNUNET_free (handle);
1137 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1138 * serialized and compressed
1140 * @param master handle to the master controller who creates the association
1141 * @param delegated_host requests to which host should be delegated; cannot be NULL
1142 * @param slave_host which host is used to run the slave controller; use NULL to
1143 * make the master controller connect to the delegated host
1144 * @param sxcfg serialized and compressed configuration
1145 * @param sxcfg_size the size scfg
1146 * @param scfg_size the size of uncompressed serialized configuration
1147 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1148 * be started by the master controller; GNUNET_NO if we are just
1149 * allowed to use the slave via TCP/IP
1152 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1153 struct GNUNET_TESTBED_Host *delegated_host,
1154 struct GNUNET_TESTBED_Host *slave_host,
1160 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1163 GNUNET_assert (GNUNET_YES ==
1164 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1165 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1166 GNUNET_assert (GNUNET_YES ==
1167 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1168 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1169 msg = GNUNET_malloc (msg_size);
1170 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1171 msg->header.size = htons (msg_size);
1172 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1173 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
1174 ((NULL != slave_host) ? slave_host : master->host));
1175 msg->config_size = htons ((uint16_t) scfg_size);
1176 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1177 memcpy (&msg[1], sxcfg, sxcfg_size);
1178 GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1183 * Compresses given configuration using zlib compress
1185 * @param config the serialized configuration
1186 * @param size the size of config
1187 * @param xconfig will be set to the compressed configuration (memory is fresly
1189 * @return the size of the xconfig
1192 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1197 xsize = compressBound ((uLong) size);
1198 *xconfig = GNUNET_malloc (xsize);
1199 GNUNET_assert (Z_OK ==
1200 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1201 (const Bytef *) config, (uLongf) size,
1208 * Create a link from slave controller to delegated controller. Whenever the
1209 * master controller is asked to start a peer at the delegated controller the
1210 * request will be routed towards slave controller (if a route exists). The
1211 * slave controller will then route it to the delegated controller. The
1212 * configuration of the slave controller is given and to be used to either
1213 * create the slave controller or to connect to an existing slave controller
1214 * process. 'is_subordinate' specifies if the given slave controller should be
1215 * started and managed by the master controller, or if the slave already has a
1216 * master and this is just a secondary master that is also allowed to use the
1219 * @param master handle to the master controller who creates the association
1220 * @param delegated_host requests to which host should be delegated
1221 * @param slave_host which host is used to run the slave controller
1222 * @param slave_cfg configuration to use for the slave controller
1223 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1224 * by the master controller; GNUNET_NO if we are just
1225 * allowed to use the slave via TCP/IP
1228 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1229 struct GNUNET_TESTBED_Host *delegated_host,
1230 struct GNUNET_TESTBED_Host *slave_host,
1231 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1239 GNUNET_assert (GNUNET_YES ==
1240 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1241 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1242 GNUNET_assert (GNUNET_YES ==
1243 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1244 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1245 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1246 GNUNET_free (config);
1247 GNUNET_assert ((UINT16_MAX -
1248 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1249 >= cc_size); /* Configuration doesn't fit in 1 message */
1250 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1251 (const char *) cconfig,
1252 cc_size, config_size, is_subordinate);
1253 GNUNET_free (cconfig);
1258 * Ask the testbed controller to write the current overlay topology to
1259 * a file. Naturally, the file will only contain a snapshot as the
1260 * topology may evolve all the time.
1262 * @param controller overlay controller to inspect
1263 * @param filename name of the file the topology should
1267 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1268 const char *filename)
1275 * Creates a helper initialization message. Only for testing.
1277 * @param cname the ip address of the controlling host
1278 * @param cfg the configuration that has to used to start the testbed service
1280 * @return the initialization message
1282 struct GNUNET_TESTBED_HelperInit *
1283 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1284 const struct GNUNET_CONFIGURATION_Handle *cfg)
1286 struct GNUNET_TESTBED_HelperInit *msg;
1290 size_t xconfig_size;
1294 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1295 GNUNET_assert (NULL != config);
1297 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1298 GNUNET_free (config);
1299 cname_len = strlen (cname);
1300 msg_size = xconfig_size + cname_len + 1 +
1301 sizeof (struct GNUNET_TESTBED_HelperInit);
1302 msg = GNUNET_realloc (xconfig, msg_size);
1303 (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1304 msg->header.size = htons (msg_size);
1305 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1306 msg->cname_size = htons (cname_len);
1307 msg->config_size = htons (config_size);
1308 (void) strcpy ((char *) &msg[1], cname);
1314 * Cancel a pending operation. Releases all resources
1315 * of the operation and will ensure that no event
1316 * is generated for the operation. Does NOT guarantee
1317 * that the operation will be fully undone (or that
1318 * nothing ever happened).
1320 * @param operation operation to cancel
1323 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1325 GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1326 operation->controller->op_tail,
1328 GNUNET_TESTBED_operation_done (operation);
1333 * Signal that the information from an operation has been fully
1334 * processed. This function MUST be called for each event
1335 * of type 'operation_finished' to fully remove the operation
1336 * from the operation queue. After calling this function, the
1337 * 'op_result' becomes invalid (!).
1339 * @param operation operation to signal completion for
1342 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1344 switch (operation->type)
1346 case OP_PEER_CREATE:
1347 case OP_PEER_DESTROY:
1351 GNUNET_TESTBED_operation_release_ (operation);
1353 case OP_OVERLAY_CONNECT:
1354 GNUNET_free_non_null (operation->data);
1357 GNUNET_free (operation);
1361 /* end of testbed_api.c */