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"
45 * Generic logging shorthand
47 #define LOG(kind, ...) \
48 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
53 #define LOG_DEBUG(...) \
54 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
57 * Relative time seconds shorthand
59 #define TIME_REL_SECS(sec) \
60 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
64 * Default server message sending retry timeout
66 #define TIMEOUT_REL TIME_REL_SECS(1)
70 * The message queue for sending messages to the controller service
75 * The message to be sent
77 struct GNUNET_MessageHeader *msg;
80 * next pointer for DLL
82 struct MessageQueue *next;
85 * prev pointer for DLL
87 struct MessageQueue *prev;
92 * Structure for a controller link
97 * The next ptr for DLL
99 struct ControllerLink *next;
102 * The prev ptr for DLL
104 struct ControllerLink *prev;
107 * The host which will be referred in the peer start request. This is the
108 * host where the peer should be started
110 struct GNUNET_TESTBED_Host *delegated_host;
113 * The host which will contacted to delegate the peer start request
115 struct GNUNET_TESTBED_Host *slave_host;
118 * The configuration to be used to connect to slave host
120 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
123 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
124 * if we are just allowed to use the slave via TCP/IP
131 * handle for host registration
133 struct GNUNET_TESTBED_HostRegistrationHandle
136 * The host being registered
138 struct GNUNET_TESTBED_Host *host;
141 * The controller at which this host is being registered
143 struct GNUNET_TESTBED_Controller *c;
146 * The Registartion completion callback
148 GNUNET_TESTBED_HostRegistrationCompletion cc;
151 * The closure for above callback
158 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
159 * controller (testbed service)
161 * @param c the controller handler
162 * @param msg message received
163 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
167 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
168 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
170 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
179 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
181 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
182 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
186 msg_size = ntohs (msg->header.size);
187 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
189 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
190 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
191 rh->cc (rh->cc_cls, NULL);
195 /* We have an error message */
196 emsg = (char *) &msg[1];
197 if ('\0' != emsg[msg_size -
198 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
204 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
205 ntohl (msg->host_id), emsg);
206 rh->cc (rh->cc_cls, emsg);
213 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
214 * controller (testbed service)
216 * @param c the controller handler
217 * @param msg message received
218 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
222 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
224 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
226 struct GNUNET_TESTBED_Operation *op;
227 struct GNUNET_TESTBED_EventInformation *event;
230 op_id = GNUNET_ntohll (msg->operation_id);
231 LOG_DEBUG ("Operation %ul successful\n", op_id);
232 for (op = c->op_head; NULL != op; op = op->next)
234 if (op->operation_id == op_id)
239 LOG_DEBUG ("Operation not found\n");
243 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
244 event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
246 event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
249 case OP_PEER_DESTROY:
251 struct PeerDestroyData *data;
255 event->details.operation_finished.operation = op;
256 event->details.operation_finished.op_cls = NULL;
257 event->details.operation_finished.emsg = NULL;
258 event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
259 event->details.operation_finished.op_result.generic = NULL;
261 data = (struct PeerDestroyData *) op->data;
262 if (NULL != data->peer->details)
264 if (NULL != data->peer->details->cfg)
265 GNUNET_CONFIGURATION_destroy (data->peer->details->cfg);
268 GNUNET_free (data->peer);
276 GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
281 c->cc (c->cc_cls, event);
289 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
290 * controller (testbed service)
292 * @param c the controller handler
293 * @param msg message received
294 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
298 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
300 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
302 struct GNUNET_TESTBED_Operation *op;
303 struct PeerCreateData *data;
304 struct GNUNET_TESTBED_Peer *peer;
305 GNUNET_TESTBED_PeerCreateCallback cb;
309 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
310 == ntohs (msg->header.size));
311 op_id = GNUNET_ntohll (msg->operation_id);
312 for (op = c->op_head; NULL != op; op = op->next)
314 if (op->operation_id == op_id)
319 LOG_DEBUG ("Operation not found\n");
322 GNUNET_assert (OP_PEER_CREATE == op->type);
323 GNUNET_assert (NULL != op->data);
325 GNUNET_assert (NULL != data->peer);
327 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
330 GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
334 cb (cls, peer, NULL);
340 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
341 * controller (testbed service)
343 * @param c the controller handler
344 * @param msg message received
345 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
349 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
350 const struct GNUNET_TESTBED_PeerEventMessage *msg)
352 struct GNUNET_TESTBED_Operation *op;
353 struct GNUNET_TESTBED_Peer *peer;
354 struct GNUNET_TESTBED_EventInformation event;
357 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
358 == ntohs (msg->header.size));
359 op_id = GNUNET_ntohll (msg->operation_id);
360 for (op = c->op_head; NULL != op; op = op->next)
362 if (op->operation_id == op_id)
367 LOG_DEBUG ("Operation not found\n");
370 GNUNET_assert ((OP_PEER_START == op->type) || (OP_PEER_STOP == op->type));
372 GNUNET_assert (NULL != peer);
373 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
376 case GNUNET_TESTBED_ET_PEER_START:
377 event.details.peer_start.host = peer->host;
378 event.details.peer_start.peer = peer;
380 case GNUNET_TESTBED_ET_PEER_STOP:
381 event.details.peer_stop.peer = peer;
384 GNUNET_assert (0); /* We should never reach this state */
386 GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
388 if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
392 c->cc (c->cc_cls, &event);
399 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
400 * controller (testbed service)
402 * @param c the controller handler
403 * @param msg message received
404 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
408 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
409 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
411 struct GNUNET_TESTBED_Operation *op;
412 struct GNUNET_TESTBED_Peer *peer;
413 struct GNUNET_TESTBED_EventInformation info;
416 op_id = GNUNET_ntohll (msg->operation_id);
417 for (op = c->op_head; NULL != op; op = op->next)
419 if (op->operation_id == op_id)
424 LOG_DEBUG ("Operation not found");
428 GNUNET_assert (NULL != peer);
429 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
430 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
432 GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
436 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
437 info.details.operation_finished.operation = op;
438 info.details.operation_finished.op_cls = NULL;
439 info.details.operation_finished.emsg = NULL;
440 info.details.operation_finished.pit = op->operation_id;
441 switch (op->operation_id)
443 case GNUNET_TESTBED_PIT_IDENTITY:
445 struct GNUNET_PeerIdentity *peer_identity;
447 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
448 (void) memcpy (peer_identity, &msg->peer_identity, sizeof (struct GNUNET_PeerIdentity));
449 info.details.operation_finished.op_result.pid = peer_identity;
452 case GNUNET_TESTBED_PIT_CONFIGURATION:
454 struct GNUNET_CONFIGURATION_Handle *cfg;
460 config_size = (uLong) ntohs (msg->config_size);
461 config = GNUNET_malloc (config_size);
462 msize = ntohs (msg->header.size);
463 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
464 if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
465 (const Bytef *) &msg[1], (uLong) msize)))
467 cfg = GNUNET_CONFIGURATION_create ();
468 GNUNET_assert (GNUNET_OK ==
469 GNUNET_CONFIGURATION_deserialize (cfg, config,
470 (size_t) config_size, GNUNET_NO));
471 info.details.operation_finished.op_result.cfg = cfg;
474 case GNUNET_TESTBED_PIT_GENERIC:
475 GNUNET_assert (0); /* never reach here */
478 c->cc (c->cc_cls, &info);
479 GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
486 * Handler for messages from controller (testbed service)
488 * @param cls the controller handler
489 * @param msg message received, NULL on timeout or fatal error
492 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
494 struct GNUNET_TESTBED_Controller *c = cls;
498 c->in_receive = GNUNET_NO;
499 /* FIXME: Add checks for message integrity */
502 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
506 msize = ntohs (msg->size);
507 switch (ntohs (msg->type))
509 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
510 GNUNET_assert (msize >= sizeof (struct
511 GNUNET_TESTBED_HostConfirmedMessage));
513 handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
515 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
517 (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
519 handle_opsuccess (c, (const struct
520 GNUNET_TESTBED_GenericOperationSuccessEventMessage
523 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
524 GNUNET_assert (msize ==
525 sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
527 handle_peer_create_success
528 (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
530 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
531 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
533 handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
536 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
537 GNUNET_assert (msize >=
538 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
541 (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg);
545 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
547 c->in_receive = GNUNET_YES;
548 GNUNET_CLIENT_receive (c->client, &message_handler, c,
549 GNUNET_TIME_UNIT_FOREVER_REL);
555 * Function called to notify a client about the connection begin ready to queue
556 * more data. "buf" will be NULL and "size" zero if the connection was closed
557 * for writing in the meantime.
560 * @param size number of bytes available in buf
561 * @param buf where the callee should write the message
562 * @return number of bytes written to buf
565 transmit_ready_notify (void *cls, size_t size, void *buf)
567 struct GNUNET_TESTBED_Controller *c = cls;
568 struct MessageQueue *mq_entry;
571 mq_entry = c->mq_head;
572 GNUNET_assert (NULL != mq_entry);
573 if ((0 == size) && (NULL == buf)) /* Timeout */
575 LOG_DEBUG ("Message sending timed out -- retrying\n");
577 GNUNET_CLIENT_notify_transmit_ready (c->client,
578 ntohs (mq_entry->msg->size),
580 GNUNET_YES, &transmit_ready_notify,
584 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
585 size = ntohs (mq_entry->msg->size);
586 memcpy (buf, mq_entry->msg, size);
587 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
588 ntohs (mq_entry->msg->type), size);
589 GNUNET_free (mq_entry->msg);
590 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
591 GNUNET_free (mq_entry);
592 mq_entry = c->mq_head;
593 if (NULL != mq_entry)
595 GNUNET_CLIENT_notify_transmit_ready (c->client,
596 ntohs (mq_entry->msg->size),
598 GNUNET_YES, &transmit_ready_notify,
600 if (GNUNET_NO == c->in_receive)
602 c->in_receive = GNUNET_YES;
603 GNUNET_CLIENT_receive (c->client, &message_handler, c,
604 GNUNET_TIME_UNIT_FOREVER_REL);
611 * Queues a message in send queue for sending to the service
613 * @param controller the handle to the controller
614 * @param msg the message to queue
617 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
618 struct GNUNET_MessageHeader *msg)
620 struct MessageQueue *mq_entry;
624 type = ntohs (msg->type);
625 size = ntohs (msg->size);
626 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
627 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
628 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
630 LOG (GNUNET_ERROR_TYPE_DEBUG,
631 "Queueing message of type %u, size %u for sending\n", type,
633 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
635 if (NULL == controller->th)
637 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
639 GNUNET_YES, &transmit_ready_notify,
645 * Handle for controller process
647 struct GNUNET_TESTBED_ControllerProc
652 struct GNUNET_HELPER_Handle *helper;
655 * The host where the helper is run
657 struct GNUNET_TESTBED_Host *host;
660 * The controller error callback
662 GNUNET_TESTBED_ControllerStatusCallback cb;
665 * The closure for the above callback
670 * The send handle for the helper
672 struct GNUNET_HELPER_SendHandle *shandle;
675 * The message corresponding to send handle
677 struct GNUNET_MessageHeader *msg;
680 * The port number for ssh; used for helpers starting ssh
685 * The ssh destination string; used for helpers starting ssh
690 * The configuration of the running testbed service
692 struct GNUNET_CONFIGURATION_Handle *cfg;
698 * Functions with this signature are called whenever a
699 * complete message is received by the tokenizer.
701 * Do not call GNUNET_SERVER_mst_destroy in callback
704 * @param client identification of the client
705 * @param message the actual message
707 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
709 static int helper_mst (void *cls, void *client,
710 const struct GNUNET_MessageHeader *message)
712 struct GNUNET_TESTBED_ControllerProc *cp = cls;
713 const struct GNUNET_TESTBED_HelperReply *msg;
714 const char *hostname;
719 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
720 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
721 < ntohs (msg->header.size));
722 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
723 == ntohs (msg->header.type));
724 config_size = (uLongf) ntohs (msg->config_size);
725 xconfig_size = (uLongf) (ntohs (msg->header.size)
726 - sizeof (struct GNUNET_TESTBED_HelperReply));
727 config = GNUNET_malloc (config_size);
728 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
729 (const Bytef *) &msg[1], xconfig_size));
730 GNUNET_assert (NULL == cp->cfg);
731 cp->cfg = GNUNET_CONFIGURATION_create ();
732 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
733 config_size, GNUNET_NO));
734 GNUNET_free (config);
735 if ((NULL == cp->host) ||
736 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
737 hostname = "localhost";
738 /* Change the hostname so that we can connect to it */
739 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
741 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
747 * Continuation function from GNUNET_HELPER_send()
750 * @param result GNUNET_OK on success,
751 * GNUNET_NO if helper process died
752 * GNUNET_SYSERR during GNUNET_HELPER_stop
755 clear_msg (void *cls, int result)
757 struct GNUNET_TESTBED_ControllerProc *cp = cls;
759 GNUNET_assert (NULL != cp->shandle);
761 GNUNET_free (cp->msg);
766 * Callback that will be called when the helper process dies. This is not called
767 * when the helper process is stoped using GNUNET_HELPER_stop()
769 * @param cls the closure from GNUNET_HELPER_start()
772 helper_exp_cb (void *cls)
774 struct GNUNET_TESTBED_ControllerProc *cp = cls;
775 GNUNET_TESTBED_ControllerStatusCallback cb;
780 GNUNET_TESTBED_controller_stop (cp);
782 cb (cb_cls, NULL, GNUNET_SYSERR);
787 * Starts a controller process at the host. FIXME: add controller start callback
788 * with the configuration with which the controller is started
790 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
791 * host when starting testbed controller at host
792 * @param host the host where the controller has to be started; NULL for
794 * @param cfg template configuration to use for the remote controller; the
795 * remote controller will be started with a slightly modified
796 * configuration (port numbers, unix domain sockets and service home
797 * values are changed as per TESTING library on the remote host)
798 * @param cb function called when the controller is successfully started or
799 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
800 * called if cb is called with GNUNET_SYSERR as status. Will never be
801 * called in the same task as 'GNUNET_TESTBED_controller_start'
802 * (synchronous errors will be signalled by returning NULL). This
803 * parameter cannot be NULL.
804 * @param cls closure for above callbacks
805 * @return the controller process handle, NULL on errors
807 struct GNUNET_TESTBED_ControllerProc *
808 GNUNET_TESTBED_controller_start (const char *controller_ip,
809 struct GNUNET_TESTBED_Host *host,
810 const struct GNUNET_CONFIGURATION_Handle *cfg,
811 GNUNET_TESTBED_ControllerStatusCallback cb,
814 struct GNUNET_TESTBED_ControllerProc *cp;
815 struct GNUNET_TESTBED_HelperInit *msg;
817 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
818 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
820 char * const binary_argv[] = {
821 "gnunet-testbed-helper", NULL
824 cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
825 &helper_mst, &helper_exp_cb, cp);
829 char *remote_args[6 + 1];
831 const char *username;
832 const char *hostname;
834 username = GNUNET_TESTBED_host_get_username_ (host);
835 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
836 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
837 if (NULL == username)
838 GNUNET_asprintf (&cp->dst, "%s", hostname);
840 GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
842 remote_args[argp++] = "ssh";
843 remote_args[argp++] = "-p";
844 remote_args[argp++] = cp->port;
845 remote_args[argp++] = "-q";
846 remote_args[argp++] = cp->dst;
847 remote_args[argp++] = "gnunet-testbed-helper";
848 remote_args[argp++] = NULL;
849 GNUNET_assert (argp == 6 + 1);
850 cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
851 &helper_mst, &helper_exp_cb, cp);
853 if (NULL == cp->helper)
855 GNUNET_free_non_null (cp->port);
856 GNUNET_free_non_null (cp->dst);
863 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
864 cp->msg = &msg->header;
865 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
867 if (NULL == cp->shandle)
870 GNUNET_TESTBED_controller_stop (cp);
878 * Stop the controller process (also will terminate all peers and controllers
879 * dependent on this controller). This function blocks until the testbed has
880 * been fully terminated (!).
882 * @param cproc the controller process handle
885 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
887 if (NULL != cproc->shandle)
888 GNUNET_HELPER_send_cancel (cproc->shandle);
889 GNUNET_HELPER_stop (cproc->helper);
890 if (NULL != cproc->cfg)
891 GNUNET_CONFIGURATION_destroy (cproc->cfg);
892 GNUNET_free_non_null (cproc->port);
893 GNUNET_free_non_null (cproc->dst);
899 * Start a controller process using the given configuration at the
902 * @param cfg configuration to use
903 * @param host host to run the controller on; This should be the same host if
904 * the controller was previously started with
905 * GNUNET_TESTBED_controller_start; NULL for localhost
906 * @param event_mask bit mask with set of events to call 'cc' for;
907 * or-ed values of "1LL" shifted by the
908 * respective 'enum GNUNET_TESTBED_EventType'
909 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
910 * @param cc controller callback to invoke on events
911 * @param cc_cls closure for cc
912 * @return handle to the controller
914 struct GNUNET_TESTBED_Controller *
915 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
916 struct GNUNET_TESTBED_Host *host,
918 GNUNET_TESTBED_ControllerCallback cc,
921 struct GNUNET_TESTBED_Controller *controller;
922 struct GNUNET_TESTBED_InitMessage *msg;
924 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
926 controller->cc_cls = cc_cls;
927 controller->event_mask = event_mask;
928 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
929 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
930 if (NULL == controller->client)
932 GNUNET_TESTBED_controller_disconnect (controller);
937 host = GNUNET_TESTBED_host_create_by_id_ (0);
940 LOG (GNUNET_ERROR_TYPE_WARNING,
941 "Treating NULL host as localhost. Multiple references to localhost. "
942 " May break when localhost freed before calling disconnect \n");
943 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
947 controller->aux_host = GNUNET_YES;
950 GNUNET_assert (NULL != host);
951 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
952 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
953 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
954 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
955 msg->event_mask = GNUNET_htonll (controller->event_mask);
956 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
962 * Configure shared services at a controller. Using this function,
963 * you can specify that certain services (such as "resolver")
964 * should not be run for each peer but instead be shared
965 * across N peers on the specified host. This function
966 * must be called before any peers are created at the host.
968 * @param controller controller to configure
969 * @param service_name name of the service to share
970 * @param num_peers number of peers that should share one instance
971 * of the specified service (1 for no sharing is the default),
972 * use 0 to disable the service
975 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
976 const char *service_name,
979 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
980 uint16_t service_name_size;
983 service_name_size = strlen (service_name) + 1;
984 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
986 msg = GNUNET_malloc (msg_size);
987 msg->header.size = htons (msg_size);
988 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
989 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
990 msg->num_peers = htonl (num_peers);
991 memcpy (&msg[1], service_name, service_name_size);
992 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
997 * disconnects from the controller.
999 * @param controller handle to controller to stop
1002 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1004 struct MessageQueue *mq_entry;
1006 if (NULL != controller->th)
1007 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1008 /* Clear the message queue */
1009 while (NULL != (mq_entry = controller->mq_head))
1011 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1012 controller->mq_tail,
1014 GNUNET_free (mq_entry->msg);
1015 GNUNET_free (mq_entry);
1017 if (NULL != controller->client)
1018 GNUNET_CLIENT_disconnect (controller->client);
1019 GNUNET_CONFIGURATION_destroy (controller->cfg);
1020 if (GNUNET_YES == controller->aux_host)
1021 GNUNET_TESTBED_host_destroy (controller->host);
1022 GNUNET_free (controller);
1027 * Register a host with the controller
1029 * @param controller the controller handle
1030 * @param host the host to register
1031 * @param cc the completion callback to call to inform the status of
1032 * registration. After calling this callback the registration handle
1033 * will be invalid. Cannot be NULL.
1034 * @param cc_cls the closure for the cc
1035 * @return handle to the host registration which can be used to cancel the
1038 struct GNUNET_TESTBED_HostRegistrationHandle *
1039 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1040 struct GNUNET_TESTBED_Host *host,
1041 GNUNET_TESTBED_HostRegistrationCompletion cc,
1044 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1045 struct GNUNET_TESTBED_AddHostMessage *msg;
1046 const char *username;
1047 const char *hostname;
1049 uint16_t user_name_length;
1051 if (NULL != controller->rh)
1053 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1054 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1056 LOG (GNUNET_ERROR_TYPE_WARNING,
1057 "Host hostname: %s already registered\n",
1058 (NULL == hostname) ? "localhost" : hostname);
1061 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1064 GNUNET_assert (NULL != cc);
1066 rh->cc_cls = cc_cls;
1067 controller->rh = rh;
1068 username = GNUNET_TESTBED_host_get_username_ (host);
1069 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1070 user_name_length = 0;
1071 if (NULL != username)
1073 user_name_length = strlen (username) + 1;
1074 msg_size += user_name_length;
1076 /* FIXME: what happens when hostname is NULL? localhost */
1077 GNUNET_assert (NULL != hostname);
1078 msg_size += strlen (hostname) + 1;
1079 msg = GNUNET_malloc (msg_size);
1080 msg->header.size = htons (msg_size);
1081 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1082 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1083 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1084 msg->user_name_length = htons (user_name_length);
1085 if (NULL != username)
1086 memcpy (&msg[1], username, user_name_length);
1087 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1088 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1094 * Cancel the pending registration. Note that if the registration message is
1095 * already sent to the service the cancellation has only the effect that the
1096 * registration completion callback for the registration is never called.
1098 * @param handle the registration handle to cancel
1101 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1104 if (handle != handle->c->rh)
1109 handle->c->rh = NULL;
1110 GNUNET_free (handle);
1115 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1116 * serialized and compressed
1118 * @param master handle to the master controller who creates the association
1119 * @param delegated_host requests to which host should be delegated; cannot be NULL
1120 * @param slave_host which host is used to run the slave controller; use NULL to
1121 * make the master controller connect to the delegated host
1122 * @param sxcfg serialized and compressed configuration
1123 * @param sxcfg_size the size scfg
1124 * @param scfg_size the size of uncompressed serialized configuration
1125 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1126 * be started by the master controller; GNUNET_NO if we are just
1127 * allowed to use the slave via TCP/IP
1130 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1131 struct GNUNET_TESTBED_Host *delegated_host,
1132 struct GNUNET_TESTBED_Host *slave_host,
1138 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1141 GNUNET_assert (GNUNET_YES ==
1142 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1143 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1144 GNUNET_assert (GNUNET_YES ==
1145 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1146 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1147 msg = GNUNET_malloc (msg_size);
1148 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1149 msg->header.size = htons (msg_size);
1150 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1151 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
1152 ((NULL != slave_host) ? slave_host : master->host));
1153 msg->config_size = htons ((uint16_t) scfg_size);
1154 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1155 memcpy (&msg[1], sxcfg, sxcfg_size);
1156 GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1161 * Compresses given configuration using zlib compress
1163 * @param config the serialized configuration
1164 * @param size the size of config
1165 * @param xconfig will be set to the compressed configuration (memory is fresly
1167 * @return the size of the xconfig
1170 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1175 xsize = compressBound ((uLong) size);
1176 *xconfig = GNUNET_malloc (xsize);
1177 GNUNET_assert (Z_OK ==
1178 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1179 (const Bytef *) config, (uLongf) size,
1186 * Create a link from slave controller to delegated controller. Whenever the
1187 * master controller is asked to start a peer at the delegated controller the
1188 * request will be routed towards slave controller (if a route exists). The
1189 * slave controller will then route it to the delegated controller. The
1190 * configuration of the slave controller is given and to be used to either
1191 * create the slave controller or to connect to an existing slave controller
1192 * process. 'is_subordinate' specifies if the given slave controller should be
1193 * started and managed by the master controller, or if the slave already has a
1194 * master and this is just a secondary master that is also allowed to use the
1197 * @param master handle to the master controller who creates the association
1198 * @param delegated_host requests to which host should be delegated
1199 * @param slave_host which host is used to run the slave controller
1200 * @param slave_cfg configuration to use for the slave controller
1201 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1202 * by the master controller; GNUNET_NO if we are just
1203 * allowed to use the slave via TCP/IP
1206 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1207 struct GNUNET_TESTBED_Host *delegated_host,
1208 struct GNUNET_TESTBED_Host *slave_host,
1209 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1217 GNUNET_assert (GNUNET_YES ==
1218 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1219 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1220 GNUNET_assert (GNUNET_YES ==
1221 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1222 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1223 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1224 GNUNET_free (config);
1225 GNUNET_assert ((UINT16_MAX -
1226 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1227 >= cc_size); /* Configuration doesn't fit in 1 message */
1228 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1229 (const char *) cconfig,
1230 cc_size, config_size, is_subordinate);
1231 GNUNET_free (cconfig);
1236 * Ask the testbed controller to write the current overlay topology to
1237 * a file. Naturally, the file will only contain a snapshot as the
1238 * topology may evolve all the time.
1240 * @param controller overlay controller to inspect
1241 * @param filename name of the file the topology should
1245 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1246 const char *filename)
1253 * Creates a helper initialization message. Only for testing.
1255 * @param cname the ip address of the controlling host
1256 * @param cfg the configuration that has to used to start the testbed service
1258 * @return the initialization message
1260 struct GNUNET_TESTBED_HelperInit *
1261 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1262 const struct GNUNET_CONFIGURATION_Handle *cfg)
1264 struct GNUNET_TESTBED_HelperInit *msg;
1268 size_t xconfig_size;
1272 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1273 GNUNET_assert (NULL != config);
1275 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1276 GNUNET_free (config);
1277 cname_len = strlen (cname);
1278 msg_size = xconfig_size + cname_len + 1 +
1279 sizeof (struct GNUNET_TESTBED_HelperInit);
1280 msg = GNUNET_realloc (xconfig, msg_size);
1281 (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1282 msg->header.size = htons (msg_size);
1283 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1284 msg->cname_size = htons (cname_len);
1285 msg->config_size = htons (config_size);
1286 (void) strcpy ((char *) &msg[1], cname);
1291 /* end of testbed_api.c */