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_PEERCONEVENT 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_conevent (struct GNUNET_TESTBED_Controller *c,
414 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
416 struct OperationContext *opc;
417 struct OverlayConnectData *data;
418 struct GNUNET_TESTBED_EventInformation event;
421 op_id = GNUNET_ntohll (msg->operation_id);
422 if (NULL == (opc = find_opc (c, op_id)))
424 LOG_DEBUG ("Operation not found\n");
428 GNUNET_assert (NULL != data);
429 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id)
430 && (ntohl (msg->peer2) == data->p2->unique_id));
431 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
434 case GNUNET_TESTBED_ET_CONNECT:
435 event.details.peer_connect.peer1 = data->p1;
436 event.details.peer_connect.peer2 = data->p2;
438 case GNUNET_TESTBED_ET_DISCONNECT:
439 GNUNET_assert (0); /* FIXME: implement */
442 GNUNET_assert (0); /* Should never reach here */
445 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
446 opc->state = OPC_STATE_FINISHED;
448 if (0 != ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT)
452 c->cc (c->cc_cls, &event);
459 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
460 * controller (testbed service)
462 * @param c the controller handler
463 * @param msg message received
464 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
468 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
469 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
471 struct OperationContext *opc;
472 struct GNUNET_TESTBED_Peer *peer;
473 struct PeerInfoData *data;
474 struct PeerInfoData2 *response_data;
475 struct GNUNET_TESTBED_EventInformation info;
478 op_id = GNUNET_ntohll (msg->operation_id);
479 if (NULL == (opc = find_opc (c, op_id)))
481 LOG_DEBUG ("Operation not found\n");
485 GNUNET_assert (NULL != data);
487 GNUNET_assert (NULL != peer);
488 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
489 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
491 LOG_DEBUG ("Skipping operation callback as flag not set\n");
494 response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
495 response_data->pit = data->pit;
498 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
499 info.details.operation_finished.operation = opc->op;
500 info.details.operation_finished.op_cls = NULL;
501 info.details.operation_finished.emsg = NULL;
502 info.details.operation_finished.pit = response_data->pit;
503 switch (response_data->pit)
505 case GNUNET_TESTBED_PIT_IDENTITY:
507 struct GNUNET_PeerIdentity *peer_identity;
509 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
510 (void) memcpy (peer_identity, &msg->peer_identity,
511 sizeof (struct GNUNET_PeerIdentity));
512 response_data->details.peer_identity = peer_identity;
513 info.details.operation_finished.op_result.pid = peer_identity;
516 case GNUNET_TESTBED_PIT_CONFIGURATION:
518 struct GNUNET_CONFIGURATION_Handle *cfg;
524 config_size = (uLong) ntohs (msg->config_size);
525 config = GNUNET_malloc (config_size);
526 msize = ntohs (msg->header.size);
527 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
528 if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
529 (const Bytef *) &msg[1], (uLong) msize)))
531 cfg = GNUNET_CONFIGURATION_create (); /* Freed in oprelease_peer_getinfo */
532 GNUNET_assert (GNUNET_OK ==
533 GNUNET_CONFIGURATION_deserialize (cfg, config,
534 (size_t) config_size,
536 GNUNET_free (config);
537 response_data->details.cfg = cfg;
538 info.details.operation_finished.op_result.cfg = cfg;
541 case GNUNET_TESTBED_PIT_GENERIC:
542 GNUNET_assert (0); /* never reach here */
545 opc->data = response_data;
546 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
547 opc->state = OPC_STATE_FINISHED;
548 c->cc (c->cc_cls, &info);
554 * Handler for messages from controller (testbed service)
556 * @param cls the controller handler
557 * @param msg message received, NULL on timeout or fatal error
560 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
562 struct GNUNET_TESTBED_Controller *c = cls;
566 c->in_receive = GNUNET_NO;
567 /* FIXME: Add checks for message integrity */
570 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
574 msize = ntohs (msg->size);
575 switch (ntohs (msg->type))
577 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
578 GNUNET_assert (msize >= sizeof (struct
579 GNUNET_TESTBED_HostConfirmedMessage));
581 handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
583 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
585 (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
587 handle_opsuccess (c, (const struct
588 GNUNET_TESTBED_GenericOperationSuccessEventMessage
591 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
592 GNUNET_assert (msize ==
593 sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
595 handle_peer_create_success
596 (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
598 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
599 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
601 handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
604 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
605 GNUNET_assert (msize >=
606 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
609 (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
612 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
613 GNUNET_assert (msize ==
614 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
616 handle_peer_conevent (c, (const struct
617 GNUNET_TESTBED_ConnectionEventMessage *) msg);
622 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
624 c->in_receive = GNUNET_YES;
625 GNUNET_CLIENT_receive (c->client, &message_handler, c,
626 GNUNET_TIME_UNIT_FOREVER_REL);
632 * Function called to notify a client about the connection begin ready to queue
633 * more data. "buf" will be NULL and "size" zero if the connection was closed
634 * for writing in the meantime.
637 * @param size number of bytes available in buf
638 * @param buf where the callee should write the message
639 * @return number of bytes written to buf
642 transmit_ready_notify (void *cls, size_t size, void *buf)
644 struct GNUNET_TESTBED_Controller *c = cls;
645 struct MessageQueue *mq_entry;
648 mq_entry = c->mq_head;
649 GNUNET_assert (NULL != mq_entry);
650 if ((0 == size) && (NULL == buf)) /* Timeout */
652 LOG_DEBUG ("Message sending timed out -- retrying\n");
654 GNUNET_CLIENT_notify_transmit_ready (c->client,
655 ntohs (mq_entry->msg->size),
657 GNUNET_YES, &transmit_ready_notify,
661 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
662 size = ntohs (mq_entry->msg->size);
663 memcpy (buf, mq_entry->msg, size);
664 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
665 ntohs (mq_entry->msg->type), size);
666 GNUNET_free (mq_entry->msg);
667 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
668 GNUNET_free (mq_entry);
669 mq_entry = c->mq_head;
670 if (NULL != mq_entry)
672 GNUNET_CLIENT_notify_transmit_ready (c->client,
673 ntohs (mq_entry->msg->size),
675 GNUNET_YES, &transmit_ready_notify,
677 if (GNUNET_NO == c->in_receive)
679 c->in_receive = GNUNET_YES;
680 GNUNET_CLIENT_receive (c->client, &message_handler, c,
681 GNUNET_TIME_UNIT_FOREVER_REL);
688 * Queues a message in send queue for sending to the service
690 * @param controller the handle to the controller
691 * @param msg the message to queue
694 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
695 struct GNUNET_MessageHeader *msg)
697 struct MessageQueue *mq_entry;
701 type = ntohs (msg->type);
702 size = ntohs (msg->size);
703 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
704 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
705 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
707 LOG (GNUNET_ERROR_TYPE_DEBUG,
708 "Queueing message of type %u, size %u for sending\n", type,
710 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
712 if (NULL == controller->th)
714 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
716 GNUNET_YES, &transmit_ready_notify,
722 * Handle for controller process
724 struct GNUNET_TESTBED_ControllerProc
729 struct GNUNET_HELPER_Handle *helper;
732 * The host where the helper is run
734 struct GNUNET_TESTBED_Host *host;
737 * The controller error callback
739 GNUNET_TESTBED_ControllerStatusCallback cb;
742 * The closure for the above callback
747 * The send handle for the helper
749 struct GNUNET_HELPER_SendHandle *shandle;
752 * The message corresponding to send handle
754 struct GNUNET_MessageHeader *msg;
757 * The port number for ssh; used for helpers starting ssh
762 * The ssh destination string; used for helpers starting ssh
767 * The configuration of the running testbed service
769 struct GNUNET_CONFIGURATION_Handle *cfg;
775 * Functions with this signature are called whenever a
776 * complete message is received by the tokenizer.
778 * Do not call GNUNET_SERVER_mst_destroy in callback
781 * @param client identification of the client
782 * @param message the actual message
784 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
786 static int helper_mst (void *cls, void *client,
787 const struct GNUNET_MessageHeader *message)
789 struct GNUNET_TESTBED_ControllerProc *cp = cls;
790 const struct GNUNET_TESTBED_HelperReply *msg;
791 const char *hostname;
796 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
797 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
798 < ntohs (msg->header.size));
799 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
800 == ntohs (msg->header.type));
801 config_size = (uLongf) ntohs (msg->config_size);
802 xconfig_size = (uLongf) (ntohs (msg->header.size)
803 - sizeof (struct GNUNET_TESTBED_HelperReply));
804 config = GNUNET_malloc (config_size);
805 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
806 (const Bytef *) &msg[1], xconfig_size));
807 GNUNET_assert (NULL == cp->cfg);
808 cp->cfg = GNUNET_CONFIGURATION_create ();
809 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
810 config_size, GNUNET_NO));
811 GNUNET_free (config);
812 if ((NULL == cp->host) ||
813 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
814 hostname = "localhost";
815 /* Change the hostname so that we can connect to it */
816 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
818 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
824 * Continuation function from GNUNET_HELPER_send()
827 * @param result GNUNET_OK on success,
828 * GNUNET_NO if helper process died
829 * GNUNET_SYSERR during GNUNET_HELPER_stop
832 clear_msg (void *cls, int result)
834 struct GNUNET_TESTBED_ControllerProc *cp = cls;
836 GNUNET_assert (NULL != cp->shandle);
838 GNUNET_free (cp->msg);
843 * Callback that will be called when the helper process dies. This is not called
844 * when the helper process is stoped using GNUNET_HELPER_stop()
846 * @param cls the closure from GNUNET_HELPER_start()
849 helper_exp_cb (void *cls)
851 struct GNUNET_TESTBED_ControllerProc *cp = cls;
852 GNUNET_TESTBED_ControllerStatusCallback cb;
857 GNUNET_TESTBED_controller_stop (cp);
859 cb (cb_cls, NULL, GNUNET_SYSERR);
864 * Starts a controller process at the host. FIXME: add controller start callback
865 * with the configuration with which the controller is started
867 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
868 * host when starting testbed controller at host
869 * @param host the host where the controller has to be started; NULL for
871 * @param cfg template configuration to use for the remote controller; the
872 * remote controller will be started with a slightly modified
873 * configuration (port numbers, unix domain sockets and service home
874 * values are changed as per TESTING library on the remote host)
875 * @param cb function called when the controller is successfully started or
876 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
877 * called if cb is called with GNUNET_SYSERR as status. Will never be
878 * called in the same task as 'GNUNET_TESTBED_controller_start'
879 * (synchronous errors will be signalled by returning NULL). This
880 * parameter cannot be NULL.
881 * @param cls closure for above callbacks
882 * @return the controller process handle, NULL on errors
884 struct GNUNET_TESTBED_ControllerProc *
885 GNUNET_TESTBED_controller_start (const char *controller_ip,
886 struct GNUNET_TESTBED_Host *host,
887 const struct GNUNET_CONFIGURATION_Handle *cfg,
888 GNUNET_TESTBED_ControllerStatusCallback cb,
891 struct GNUNET_TESTBED_ControllerProc *cp;
892 struct GNUNET_TESTBED_HelperInit *msg;
894 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
895 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
897 char * const binary_argv[] = {
898 "gnunet-testbed-helper", NULL
901 cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
902 &helper_mst, &helper_exp_cb, cp);
906 char *remote_args[6 + 1];
908 const char *username;
909 const char *hostname;
911 username = GNUNET_TESTBED_host_get_username_ (host);
912 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
913 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
914 if (NULL == username)
915 GNUNET_asprintf (&cp->dst, "%s", hostname);
917 GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
919 remote_args[argp++] = "ssh";
920 remote_args[argp++] = "-p";
921 remote_args[argp++] = cp->port;
922 remote_args[argp++] = "-q";
923 remote_args[argp++] = cp->dst;
924 remote_args[argp++] = "gnunet-testbed-helper";
925 remote_args[argp++] = NULL;
926 GNUNET_assert (argp == 6 + 1);
927 cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
928 &helper_mst, &helper_exp_cb, cp);
930 if (NULL == cp->helper)
932 GNUNET_free_non_null (cp->port);
933 GNUNET_free_non_null (cp->dst);
940 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
941 cp->msg = &msg->header;
942 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
944 if (NULL == cp->shandle)
947 GNUNET_TESTBED_controller_stop (cp);
955 * Stop the controller process (also will terminate all peers and controllers
956 * dependent on this controller). This function blocks until the testbed has
957 * been fully terminated (!).
959 * @param cproc the controller process handle
962 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
964 if (NULL != cproc->shandle)
965 GNUNET_HELPER_send_cancel (cproc->shandle);
966 GNUNET_HELPER_stop (cproc->helper);
967 if (NULL != cproc->cfg)
968 GNUNET_CONFIGURATION_destroy (cproc->cfg);
969 GNUNET_free_non_null (cproc->port);
970 GNUNET_free_non_null (cproc->dst);
976 * Start a controller process using the given configuration at the
979 * @param cfg configuration to use
980 * @param host host to run the controller on; This should be the same host if
981 * the controller was previously started with
982 * GNUNET_TESTBED_controller_start; NULL for localhost
983 * @param event_mask bit mask with set of events to call 'cc' for;
984 * or-ed values of "1LL" shifted by the
985 * respective 'enum GNUNET_TESTBED_EventType'
986 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
987 * @param cc controller callback to invoke on events
988 * @param cc_cls closure for cc
989 * @return handle to the controller
991 struct GNUNET_TESTBED_Controller *
992 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
993 struct GNUNET_TESTBED_Host *host,
995 GNUNET_TESTBED_ControllerCallback cc,
998 struct GNUNET_TESTBED_Controller *controller;
999 struct GNUNET_TESTBED_InitMessage *msg;
1000 const char *controller_hostname;
1001 unsigned long long max_parallel_peer_create;
1004 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1005 "MAX_PARALLEL_PEER_CREATE",
1006 &max_parallel_peer_create))
1011 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1012 controller->cc = cc;
1013 controller->cc_cls = cc_cls;
1014 controller->event_mask = event_mask;
1015 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1016 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1017 if (NULL == controller->client)
1019 GNUNET_TESTBED_controller_disconnect (controller);
1024 host = GNUNET_TESTBED_host_create_by_id_ (0);
1025 if (NULL == host) /* If the above host create fails */
1027 LOG (GNUNET_ERROR_TYPE_WARNING,
1028 "Treating NULL host as localhost. Multiple references to localhost "
1029 "may break when localhost freed before calling disconnect \n");
1030 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1034 controller->aux_host = GNUNET_YES;
1037 GNUNET_assert (NULL != host);
1038 controller->opq_peer_create =
1039 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1040 max_parallel_peer_create);
1041 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1042 if (NULL == controller_hostname)
1043 controller_hostname = "127.0.0.1";
1044 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage)
1045 + strlen (controller_hostname) + 1);
1046 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1047 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage)
1048 + strlen (controller_hostname) + 1);
1049 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1050 msg->event_mask = GNUNET_htonll (controller->event_mask);
1051 strcpy ((char *) &msg[1], controller_hostname);
1052 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
1059 * Configure shared services at a controller. Using this function,
1060 * you can specify that certain services (such as "resolver")
1061 * should not be run for each peer but instead be shared
1062 * across N peers on the specified host. This function
1063 * must be called before any peers are created at the host.
1065 * @param controller controller to configure
1066 * @param service_name name of the service to share
1067 * @param num_peers number of peers that should share one instance
1068 * of the specified service (1 for no sharing is the default),
1069 * use 0 to disable the service
1072 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
1073 const char *service_name,
1076 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1077 uint16_t service_name_size;
1080 service_name_size = strlen (service_name) + 1;
1081 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1082 + service_name_size;
1083 msg = GNUNET_malloc (msg_size);
1084 msg->header.size = htons (msg_size);
1085 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1086 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1087 msg->num_peers = htonl (num_peers);
1088 memcpy (&msg[1], service_name, service_name_size);
1089 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1094 * disconnects from the controller.
1096 * @param controller handle to controller to stop
1099 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1101 struct MessageQueue *mq_entry;
1103 if (NULL != controller->th)
1104 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1105 /* Clear the message queue */
1106 while (NULL != (mq_entry = controller->mq_head))
1108 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1109 controller->mq_tail,
1111 GNUNET_free (mq_entry->msg);
1112 GNUNET_free (mq_entry);
1114 if (NULL != controller->client)
1115 GNUNET_CLIENT_disconnect (controller->client);
1116 GNUNET_CONFIGURATION_destroy (controller->cfg);
1117 if (GNUNET_YES == controller->aux_host)
1118 GNUNET_TESTBED_host_destroy (controller->host);
1119 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_peer_create);
1120 GNUNET_free (controller);
1125 * Register a host with the controller
1127 * @param controller the controller handle
1128 * @param host the host to register
1129 * @param cc the completion callback to call to inform the status of
1130 * registration. After calling this callback the registration handle
1131 * will be invalid. Cannot be NULL.
1132 * @param cc_cls the closure for the cc
1133 * @return handle to the host registration which can be used to cancel the
1136 struct GNUNET_TESTBED_HostRegistrationHandle *
1137 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1138 struct GNUNET_TESTBED_Host *host,
1139 GNUNET_TESTBED_HostRegistrationCompletion cc,
1142 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1143 struct GNUNET_TESTBED_AddHostMessage *msg;
1144 const char *username;
1145 const char *hostname;
1147 uint16_t user_name_length;
1149 if (NULL != controller->rh)
1151 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1152 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1154 LOG (GNUNET_ERROR_TYPE_WARNING,
1155 "Host hostname: %s already registered\n",
1156 (NULL == hostname) ? "localhost" : hostname);
1159 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1162 GNUNET_assert (NULL != cc);
1164 rh->cc_cls = cc_cls;
1165 controller->rh = rh;
1166 username = GNUNET_TESTBED_host_get_username_ (host);
1167 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1168 user_name_length = 0;
1169 if (NULL != username)
1171 user_name_length = strlen (username) + 1;
1172 msg_size += user_name_length;
1174 /* FIXME: what happens when hostname is NULL? localhost */
1175 GNUNET_assert (NULL != hostname);
1176 msg_size += strlen (hostname) + 1;
1177 msg = GNUNET_malloc (msg_size);
1178 msg->header.size = htons (msg_size);
1179 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1180 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1181 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1182 msg->user_name_length = htons (user_name_length);
1183 if (NULL != username)
1184 memcpy (&msg[1], username, user_name_length);
1185 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1186 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1192 * Cancel the pending registration. Note that if the registration message is
1193 * already sent to the service the cancellation has only the effect that the
1194 * registration completion callback for the registration is never called.
1196 * @param handle the registration handle to cancel
1199 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1202 if (handle != handle->c->rh)
1207 handle->c->rh = NULL;
1208 GNUNET_free (handle);
1213 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1214 * serialized and compressed
1216 * @param master handle to the master controller who creates the association
1217 * @param delegated_host requests to which host should be delegated; cannot be NULL
1218 * @param slave_host which host is used to run the slave controller; use NULL to
1219 * make the master controller connect to the delegated host
1220 * @param sxcfg serialized and compressed configuration
1221 * @param sxcfg_size the size scfg
1222 * @param scfg_size the size of uncompressed serialized configuration
1223 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1224 * be started by the master controller; GNUNET_NO if we are just
1225 * allowed to use the slave via TCP/IP
1228 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1229 struct GNUNET_TESTBED_Host *delegated_host,
1230 struct GNUNET_TESTBED_Host *slave_host,
1236 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
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 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1245 msg = GNUNET_malloc (msg_size);
1246 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1247 msg->header.size = htons (msg_size);
1248 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1249 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
1250 ((NULL != slave_host) ? slave_host : master->host));
1251 msg->config_size = htons ((uint16_t) scfg_size);
1252 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1253 memcpy (&msg[1], sxcfg, sxcfg_size);
1254 GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1259 * Compresses given configuration using zlib compress
1261 * @param config the serialized configuration
1262 * @param size the size of config
1263 * @param xconfig will be set to the compressed configuration (memory is fresly
1265 * @return the size of the xconfig
1268 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1273 xsize = compressBound ((uLong) size);
1274 *xconfig = GNUNET_malloc (xsize);
1275 GNUNET_assert (Z_OK ==
1276 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1277 (const Bytef *) config, (uLongf) size,
1284 * Create a link from slave controller to delegated controller. Whenever the
1285 * master controller is asked to start a peer at the delegated controller the
1286 * request will be routed towards slave controller (if a route exists). The
1287 * slave controller will then route it to the delegated controller. The
1288 * configuration of the slave controller is given and to be used to either
1289 * create the slave controller or to connect to an existing slave controller
1290 * process. 'is_subordinate' specifies if the given slave controller should be
1291 * started and managed by the master controller, or if the slave already has a
1292 * master and this is just a secondary master that is also allowed to use the
1295 * @param master handle to the master controller who creates the association
1296 * @param delegated_host requests to which host should be delegated
1297 * @param slave_host which host is used to run the slave controller
1298 * @param slave_cfg configuration to use for the slave controller
1299 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1300 * by the master controller; GNUNET_NO if we are just
1301 * allowed to use the slave via TCP/IP
1304 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1305 struct GNUNET_TESTBED_Host *delegated_host,
1306 struct GNUNET_TESTBED_Host *slave_host,
1307 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1315 GNUNET_assert (GNUNET_YES ==
1316 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1317 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1318 GNUNET_assert (GNUNET_YES ==
1319 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1320 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1321 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1322 GNUNET_free (config);
1323 GNUNET_assert ((UINT16_MAX -
1324 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1325 >= cc_size); /* Configuration doesn't fit in 1 message */
1326 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1327 (const char *) cconfig,
1328 cc_size, config_size, is_subordinate);
1329 GNUNET_free (cconfig);
1334 * Ask the testbed controller to write the current overlay topology to
1335 * a file. Naturally, the file will only contain a snapshot as the
1336 * topology may evolve all the time.
1338 * @param controller overlay controller to inspect
1339 * @param filename name of the file the topology should
1343 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1344 const char *filename)
1351 * Creates a helper initialization message. This function is here because we
1352 * want to use this in testing
1354 * @param cname the ip address of the controlling host
1355 * @param cfg the configuration that has to used to start the testbed service
1357 * @return the initialization message
1359 struct GNUNET_TESTBED_HelperInit *
1360 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1361 const struct GNUNET_CONFIGURATION_Handle *cfg)
1363 struct GNUNET_TESTBED_HelperInit *msg;
1367 size_t xconfig_size;
1371 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1372 GNUNET_assert (NULL != config);
1374 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1375 GNUNET_free (config);
1376 cname_len = strlen (cname);
1377 msg_size = xconfig_size + cname_len + 1 +
1378 sizeof (struct GNUNET_TESTBED_HelperInit);
1379 msg = GNUNET_realloc (xconfig, msg_size);
1380 (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1381 msg->header.size = htons (msg_size);
1382 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1383 msg->cname_size = htons (cname_len);
1384 msg->config_size = htons (config_size);
1385 (void) strcpy ((char *) &msg[1], cname);
1391 * Cancel a pending operation. Releases all resources
1392 * of the operation and will ensure that no event
1393 * is generated for the operation. Does NOT guarantee
1394 * that the operation will be fully undone (or that
1395 * nothing ever happened).
1397 * @param operation operation to cancel
1400 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1402 GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1403 operation->controller->op_tail,
1405 GNUNET_TESTBED_operation_done (operation);
1410 * Signal that the information from an operation has been fully
1411 * processed. This function MUST be called for each event
1412 * of type 'operation_finished' to fully remove the operation
1413 * from the operation queue. After calling this function, the
1414 * 'op_result' becomes invalid (!).
1416 * @param operation operation to signal completion for
1419 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1421 switch (operation->type)
1423 case OP_PEER_CREATE:
1424 case OP_PEER_DESTROY:
1428 case OP_OVERLAY_CONNECT:
1429 GNUNET_TESTBED_operation_release_ (operation);
1438 /* end of testbed_api.c */