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 * Testbed Helper binary name
73 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
74 #define HELPER_TESTBED_BINARY_SSH ". ~/.bashrc; gnunet-helper-testbed"
78 * Handle for controller process
80 struct GNUNET_TESTBED_ControllerProc
85 struct GNUNET_HELPER_Handle *helper;
88 * The host where the helper is run
90 struct GNUNET_TESTBED_Host *host;
93 * The controller error callback
95 GNUNET_TESTBED_ControllerStatusCallback cb;
98 * The closure for the above callback
103 * The send handle for the helper
105 struct GNUNET_HELPER_SendHandle *shandle;
108 * The message corresponding to send handle
110 struct GNUNET_MessageHeader *msg;
113 * The port number for ssh; used for helpers starting ssh
118 * The ssh destination string; used for helpers starting ssh
123 * The configuration of the running testbed service
125 struct GNUNET_CONFIGURATION_Handle *cfg;
131 * The message queue for sending messages to the controller service
136 * The message to be sent
138 struct GNUNET_MessageHeader *msg;
141 * next pointer for DLL
143 struct MessageQueue *next;
146 * prev pointer for DLL
148 struct MessageQueue *prev;
153 * Structure for a controller link
155 struct ControllerLink
158 * The next ptr for DLL
160 struct ControllerLink *next;
163 * The prev ptr for DLL
165 struct ControllerLink *prev;
168 * The host which will be referred in the peer start request. This is the
169 * host where the peer should be started
171 struct GNUNET_TESTBED_Host *delegated_host;
174 * The host which will contacted to delegate the peer start request
176 struct GNUNET_TESTBED_Host *slave_host;
179 * The configuration to be used to connect to slave host
181 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
184 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
185 * if we are just allowed to use the slave via TCP/IP
192 * handle for host registration
194 struct GNUNET_TESTBED_HostRegistrationHandle
197 * The host being registered
199 struct GNUNET_TESTBED_Host *host;
202 * The controller at which this host is being registered
204 struct GNUNET_TESTBED_Controller *c;
207 * The Registartion completion callback
209 GNUNET_TESTBED_HostRegistrationCompletion cc;
212 * The closure for above callback
219 * Context data for forwarded Operation
221 struct ForwardedOperationData
225 * The callback to call when reply is available
227 GNUNET_CLIENT_MessageHandler cc;
230 * The closure for the above callback
238 * Context data for get slave config operations
240 struct GetSlaveConfigData
243 * The operation closure
248 * The id of the slave controller
256 * Context data for controller link operations
258 struct ControllerLinkData
261 * The controller link message
263 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
266 * The operation closure
274 * Returns the operation context with the given id if found in the Operation
275 * context queues of the controller
277 * @param c the controller whose queues are searched
278 * @param id the id which has to be checked
279 * @return the matching operation context; NULL if no match found
281 static struct OperationContext *
282 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
284 struct OperationContext *opc;
286 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
296 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
297 * controller (testbed service)
299 * @param c the controller handler
300 * @param msg message received
301 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
305 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
306 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
308 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
317 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
319 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
320 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
324 msg_size = ntohs (msg->header.size);
325 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
327 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
328 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
329 rh->cc (rh->cc_cls, NULL);
333 /* We have an error message */
334 emsg = (char *) &msg[1];
336 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
342 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
343 ntohl (msg->host_id), emsg);
344 rh->cc (rh->cc_cls, emsg);
351 * Handler for forwarded operations
353 * @param c the controller handle
354 * @param opc the opearation context
355 * @param msg the message
358 handle_forwarded_operation_msg (struct GNUNET_TESTBED_Controller *c,
359 struct OperationContext *opc,
360 const struct GNUNET_MessageHeader *msg)
362 struct ForwardedOperationData *fo_data;
365 if (NULL != fo_data->cc)
366 fo_data->cc (fo_data->cc_cls, msg);
367 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
368 GNUNET_free (fo_data);
374 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
375 * controller (testbed service)
377 * @param c the controller handler
378 * @param msg message received
379 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
383 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
385 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
387 struct OperationContext *opc;
388 struct GNUNET_TESTBED_EventInformation event;
391 op_id = GNUNET_ntohll (msg->operation_id);
392 LOG_DEBUG ("Operation %ul successful\n", op_id);
393 if (NULL == (opc = find_opc (c, op_id)))
395 LOG_DEBUG ("Operation not found\n");
398 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
399 event.details.operation_finished.operation = opc->op;
400 event.details.operation_finished.op_cls = NULL;
401 event.details.operation_finished.emsg = NULL;
402 event.details.operation_finished.generic = NULL;
407 handle_forwarded_operation_msg
408 (c, opc, (const struct GNUNET_MessageHeader *) msg);
412 case OP_PEER_DESTROY:
414 struct GNUNET_TESTBED_Peer *peer;
422 case OP_LINK_CONTROLLERS:
424 struct ControllerLinkData *data;
427 GNUNET_assert (NULL != data);
428 event.details.operation_finished.op_cls = data->op_cls;
436 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
437 opc->state = OPC_STATE_FINISHED;
438 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
441 c->cc (c->cc_cls, &event);
448 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
449 * controller (testbed service)
451 * @param c the controller handle
452 * @param msg message received
453 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
457 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
459 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
461 struct OperationContext *opc;
462 struct PeerCreateData *data;
463 struct GNUNET_TESTBED_Peer *peer;
464 GNUNET_TESTBED_PeerCreateCallback cb;
468 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
469 ntohs (msg->header.size));
470 op_id = GNUNET_ntohll (msg->operation_id);
471 if (NULL == (opc = find_opc (c, op_id)))
473 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
476 if (OP_FORWARDED == opc->type)
478 handle_forwarded_operation_msg (c, opc,
479 (const struct GNUNET_MessageHeader *) msg);
482 GNUNET_assert (OP_PEER_CREATE == opc->type);
483 GNUNET_assert (NULL != opc->data);
485 GNUNET_assert (NULL != data->peer);
487 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
488 peer->state = PS_CREATED;
491 GNUNET_free (opc->data);
492 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
493 opc->state = OPC_STATE_FINISHED;
495 cb (cls, peer, NULL);
501 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
502 * controller (testbed service)
504 * @param c the controller handler
505 * @param msg message received
506 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
510 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
511 const struct GNUNET_TESTBED_PeerEventMessage *msg)
513 struct OperationContext *opc;
514 struct GNUNET_TESTBED_Peer *peer;
515 struct PeerEventData *data;
516 GNUNET_TESTBED_PeerChurnCallback pcc;
518 struct GNUNET_TESTBED_EventInformation event;
521 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
522 ntohs (msg->header.size));
523 op_id = GNUNET_ntohll (msg->operation_id);
524 if (NULL == (opc = find_opc (c, op_id)))
526 LOG_DEBUG ("Operation not found\n");
529 if (OP_FORWARDED == opc->type)
531 handle_forwarded_operation_msg (c, opc,
532 (const struct GNUNET_MessageHeader *) msg);
535 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
537 GNUNET_assert (NULL != data);
539 GNUNET_assert (NULL != peer);
540 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
543 case GNUNET_TESTBED_ET_PEER_START:
544 peer->state = PS_STARTED;
545 event.details.peer_start.host = peer->host;
546 event.details.peer_start.peer = peer;
548 case GNUNET_TESTBED_ET_PEER_STOP:
549 peer->state = PS_STOPPED;
550 event.details.peer_stop.peer = peer;
553 GNUNET_assert (0); /* We should never reach this state */
556 pcc_cls = data->pcc_cls;
558 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
559 opc->state = OPC_STATE_FINISHED;
561 ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP) &
565 c->cc (c->cc_cls, &event);
574 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
575 * controller (testbed service)
577 * @param c the controller handler
578 * @param msg message received
579 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
583 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
584 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
586 struct OperationContext *opc;
587 struct OverlayConnectData *data;
588 GNUNET_TESTBED_OperationCompletionCallback cb;
590 struct GNUNET_TESTBED_EventInformation event;
593 op_id = GNUNET_ntohll (msg->operation_id);
594 if (NULL == (opc = find_opc (c, op_id)))
596 LOG_DEBUG ("Operation not found\n");
599 if (OP_FORWARDED == opc->type)
601 handle_forwarded_operation_msg (c, opc,
602 (const struct GNUNET_MessageHeader *) msg);
605 GNUNET_assert (OP_OVERLAY_CONNECT == opc->type);
607 GNUNET_assert (NULL != data);
608 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
609 (ntohl (msg->peer2) == data->p2->unique_id));
610 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
613 case GNUNET_TESTBED_ET_CONNECT:
614 event.details.peer_connect.peer1 = data->p1;
615 event.details.peer_connect.peer2 = data->p2;
617 case GNUNET_TESTBED_ET_DISCONNECT:
618 GNUNET_assert (0); /* FIXME: implement */
621 GNUNET_assert (0); /* Should never reach here */
625 cb_cls = data->cb_cls;
626 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
627 opc->state = OPC_STATE_FINISHED;
630 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
634 c->cc (c->cc_cls, &event);
637 cb (cb_cls, opc->op, NULL);
643 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
644 * controller (testbed service)
646 * @param c the controller handler
647 * @param msg message received
648 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
652 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
654 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
656 struct OperationContext *opc;
657 struct GNUNET_TESTBED_Peer *peer;
658 struct PeerInfoData *data;
659 struct GNUNET_TESTBED_PeerInformation *pinfo;
660 GNUNET_TESTBED_PeerInfoCallback cb;
664 op_id = GNUNET_ntohll (msg->operation_id);
665 if (NULL == (opc = find_opc (c, op_id)))
667 LOG_DEBUG ("Operation not found\n");
670 if (OP_FORWARDED == opc->type)
672 handle_forwarded_operation_msg (c, opc,
673 (const struct GNUNET_MessageHeader *) msg);
677 GNUNET_assert (NULL != data);
679 GNUNET_assert (NULL != peer);
680 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
681 pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
682 pinfo->pit = data->pit;
684 cb_cls = data->cb_cls;
689 case GNUNET_TESTBED_PIT_IDENTITY:
690 pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
691 (void) memcpy (pinfo->result.id, &msg->peer_identity,
692 sizeof (struct GNUNET_PeerIdentity));
694 case GNUNET_TESTBED_PIT_CONFIGURATION:
695 pinfo->result.cfg = /* Freed in oprelease_peer_getinfo */
696 GNUNET_TESTBED_extract_config_ (&msg->header);
698 case GNUNET_TESTBED_PIT_GENERIC:
699 GNUNET_assert (0); /* never reach here */
703 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
704 opc->state = OPC_STATE_FINISHED;
706 cb (cb_cls, opc->op, pinfo, NULL);
712 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
713 * controller (testbed service)
715 * @param c the controller handler
716 * @param msg message received
717 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
721 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
722 const struct GNUNET_TESTBED_OperationFailureEventMessage
725 struct OperationContext *opc;
728 struct GNUNET_TESTBED_EventInformation event;
730 op_id = GNUNET_ntohll (msg->operation_id);
731 if (NULL == (opc = find_opc (c, op_id)))
733 LOG_DEBUG ("Operation not found\n");
736 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
737 if (OP_FORWARDED == opc->type)
739 handle_forwarded_operation_msg (c, opc,
740 (const struct GNUNET_MessageHeader *) msg);
743 opc->state = OPC_STATE_FINISHED;
744 emsg = GNUNET_TESTBED_parse_error_string_ (msg);
746 emsg = "Unknown error";
747 if (OP_PEER_INFO == opc->type)
749 struct PeerInfoData *data;
751 if (NULL != data->cb)
752 data->cb (data->cb_cls, opc->op, NULL, emsg);
754 return GNUNET_YES; /* We do not call controller callback for peer info */
756 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
759 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
760 event.details.operation_finished.operation = opc->op;
761 event.details.operation_finished.op_cls = NULL;
762 event.details.operation_finished.emsg = emsg;
763 event.details.operation_finished.generic = NULL;
764 c->cc (c->cc_cls, &event);
770 struct PeerCreateData *data;
772 GNUNET_free (data->peer);
773 if (NULL != data->cb)
774 data->cb (data->cls, NULL, emsg);
781 struct PeerEventData *data;
783 if (NULL != data->pcc)
784 data->pcc (data->pcc_cls, emsg);
788 case OP_PEER_DESTROY:
792 case OP_OVERLAY_CONNECT:
794 struct OverlayConnectData *data;
796 if (NULL != data->cb)
797 data->cb (data->cb_cls, opc->op, emsg);
803 case OP_LINK_CONTROLLERS: /* No secondary callback */
813 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG message from controller
816 * @param c the controller handler
817 * @param msg message received
818 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
822 handle_slave_config (struct GNUNET_TESTBED_Controller *c,
823 const struct GNUNET_TESTBED_SlaveConfiguration * msg)
825 struct OperationContext *opc;
828 struct GNUNET_TESTBED_EventInformation event;
830 op_id = GNUNET_ntohll (msg->operation_id);
831 if (NULL == (opc = find_opc (c, op_id)))
833 LOG_DEBUG ("Operation not found\n");
836 if (OP_GET_SLAVE_CONFIG != opc->type)
841 op_cls = ((struct GetSlaveConfigData *) opc->data)->op_cls;
842 GNUNET_free (opc->data);
844 opc->state = OPC_STATE_FINISHED;
845 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
846 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
849 opc->data = GNUNET_TESTBED_extract_config_ (&msg->header);
850 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
851 event.details.operation_finished.generic = opc->data;
852 event.details.operation_finished.operation = opc->op;
853 event.details.operation_finished.op_cls = op_cls;
854 event.details.operation_finished.emsg = NULL;
855 c->cc (c->cc_cls, &event);
862 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG message from
863 * controller (testbed service)
865 * @param c the controller handler
866 * @param msg message received
867 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
871 handle_need_controller_config (struct GNUNET_TESTBED_Controller *c,
872 const struct GNUNET_TESTBED_NeedControllerConfig * msg)
874 struct OperationContext *opc;
875 struct OverlayConnectData *oc_data;
878 op_id = GNUNET_ntohll (msg->operation_id);
879 if (NULL == (opc = find_opc (c, op_id)))
881 LOG_DEBUG ("Operation not found\n");
884 if (OP_FORWARDED == opc->type)
886 handle_forwarded_operation_msg (c, opc,
887 (const struct GNUNET_MessageHeader *) msg);
891 /* FIXME: Should spawn operations to:
892 1. Acquire configuration of peer2's controller,
893 2. link peer1's controller to peer2's controller
894 3. ask them to attempt overlay connect on peer1 and peer2 again */
895 GNUNET_break (NULL == oc_data);
902 * Handler for messages from controller (testbed service)
904 * @param cls the controller handler
905 * @param msg message received, NULL on timeout or fatal error
908 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
910 struct GNUNET_TESTBED_Controller *c = cls;
914 c->in_receive = GNUNET_NO;
915 /* FIXME: Add checks for message integrity */
918 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
922 msize = ntohs (msg->size);
923 switch (ntohs (msg->type))
925 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
926 GNUNET_assert (msize >=
927 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
929 handle_addhostconfirm (c,
930 (const struct GNUNET_TESTBED_HostConfirmedMessage
933 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
934 GNUNET_assert (msize ==
936 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
940 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
943 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
944 GNUNET_assert (msize ==
946 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
948 handle_peer_create_success (c,
950 GNUNET_TESTBED_PeerCreateSuccessEventMessage
953 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
954 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
956 handle_peer_event (c,
957 (const struct GNUNET_TESTBED_PeerEventMessage *)
961 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
962 GNUNET_assert (msize >=
964 GNUNET_TESTBED_PeerConfigurationInformationMessage));
966 handle_peer_config (c,
968 GNUNET_TESTBED_PeerConfigurationInformationMessage
971 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
972 GNUNET_assert (msize ==
973 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
975 handle_peer_conevent (c,
977 GNUNET_TESTBED_ConnectionEventMessage *) msg);
979 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT:
980 GNUNET_assert (msize >=
981 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
983 handle_op_fail_event (c,
985 GNUNET_TESTBED_OperationFailureEventMessage *)
988 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
989 GNUNET_assert (msize >
990 sizeof (struct GNUNET_TESTBED_SlaveConfiguration));
992 handle_slave_config (c, (const struct
993 GNUNET_TESTBED_SlaveConfiguration *) msg);
995 case GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG:
996 GNUNET_assert (msize == sizeof (struct
997 GNUNET_TESTBED_NeedControllerConfig));
999 handle_need_controller_config (c, (const struct
1000 GNUNET_TESTBED_NeedControllerConfig
1006 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
1008 c->in_receive = GNUNET_YES;
1009 GNUNET_CLIENT_receive (c->client, &message_handler, c,
1010 GNUNET_TIME_UNIT_FOREVER_REL);
1016 * Function called to notify a client about the connection begin ready to queue
1017 * more data. "buf" will be NULL and "size" zero if the connection was closed
1018 * for writing in the meantime.
1020 * @param cls closure
1021 * @param size number of bytes available in buf
1022 * @param buf where the callee should write the message
1023 * @return number of bytes written to buf
1026 transmit_ready_notify (void *cls, size_t size, void *buf)
1028 struct GNUNET_TESTBED_Controller *c = cls;
1029 struct MessageQueue *mq_entry;
1032 mq_entry = c->mq_head;
1033 GNUNET_assert (NULL != mq_entry);
1034 if ((0 == size) && (NULL == buf)) /* Timeout */
1036 LOG_DEBUG ("Message sending timed out -- retrying\n");
1038 GNUNET_CLIENT_notify_transmit_ready (c->client,
1039 ntohs (mq_entry->msg->size),
1040 TIMEOUT_REL, GNUNET_YES,
1041 &transmit_ready_notify, c);
1044 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
1045 size = ntohs (mq_entry->msg->size);
1046 memcpy (buf, mq_entry->msg, size);
1047 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
1048 ntohs (mq_entry->msg->type), size);
1049 GNUNET_free (mq_entry->msg);
1050 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
1051 GNUNET_free (mq_entry);
1052 mq_entry = c->mq_head;
1053 if (NULL != mq_entry)
1055 GNUNET_CLIENT_notify_transmit_ready (c->client,
1056 ntohs (mq_entry->msg->size),
1057 TIMEOUT_REL, GNUNET_YES,
1058 &transmit_ready_notify, c);
1059 if (GNUNET_NO == c->in_receive)
1061 c->in_receive = GNUNET_YES;
1062 GNUNET_CLIENT_receive (c->client, &message_handler, c,
1063 GNUNET_TIME_UNIT_FOREVER_REL);
1070 * Queues a message in send queue for sending to the service
1072 * @param controller the handle to the controller
1073 * @param msg the message to queue
1076 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
1077 struct GNUNET_MessageHeader *msg)
1079 struct MessageQueue *mq_entry;
1083 type = ntohs (msg->type);
1084 size = ntohs (msg->size);
1085 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
1086 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
1087 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
1088 mq_entry->msg = msg;
1089 LOG (GNUNET_ERROR_TYPE_DEBUG,
1090 "Queueing message of type %u, size %u for sending\n", type,
1092 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
1094 if (NULL == controller->th)
1096 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
1097 TIMEOUT_REL, GNUNET_YES,
1098 &transmit_ready_notify,
1104 * Sends the given message as an operation. The given callback is called when a
1105 * reply for the operation is available. Call
1106 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
1107 * operation context if the cc hasn't been called
1109 * @param controller the controller to which the message has to be sent
1110 * @param operation_id the operation id of the message
1111 * @param msg the message to send
1112 * @param cc the callback to call when reply is available
1113 * @param cc_cls the closure for the above callback
1114 * @return the operation context which can be used to cancel the forwarded
1117 struct OperationContext *
1118 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
1119 *controller, uint64_t operation_id,
1120 const struct GNUNET_MessageHeader *msg,
1121 GNUNET_CLIENT_MessageHandler cc,
1124 struct OperationContext *opc;
1125 struct ForwardedOperationData *data;
1126 struct GNUNET_MessageHeader *dup_msg;
1129 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
1131 data->cc_cls = cc_cls;
1132 opc = GNUNET_malloc (sizeof (struct OperationContext));
1133 opc->c = controller;
1134 opc->type = OP_FORWARDED;
1136 opc->id = operation_id;
1137 msize = ntohs (msg->size);
1138 dup_msg = GNUNET_malloc (msize);
1139 (void) memcpy (dup_msg, msg, msize);
1140 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
1141 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
1148 * Function to cancel an operation created by simply forwarding an operation
1151 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
1154 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
1156 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1157 GNUNET_free (opc->data);
1163 * Functions with this signature are called whenever a
1164 * complete message is received by the tokenizer.
1166 * Do not call GNUNET_SERVER_mst_destroy in callback
1168 * @param cls closure
1169 * @param client identification of the client
1170 * @param message the actual message
1172 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1175 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1177 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1178 const struct GNUNET_TESTBED_HelperReply *msg;
1179 const char *hostname;
1182 uLongf xconfig_size;
1184 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1185 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1186 ntohs (msg->header.size));
1187 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1188 ntohs (msg->header.type));
1189 config_size = (uLongf) ntohs (msg->config_size);
1191 (uLongf) (ntohs (msg->header.size) -
1192 sizeof (struct GNUNET_TESTBED_HelperReply));
1193 config = GNUNET_malloc (config_size);
1194 GNUNET_assert (Z_OK ==
1195 uncompress ((Bytef *) config, &config_size,
1196 (const Bytef *) &msg[1], xconfig_size));
1197 GNUNET_assert (NULL == cp->cfg);
1198 cp->cfg = GNUNET_CONFIGURATION_create ();
1199 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1200 (cp->cfg, config, config_size, GNUNET_NO));
1201 GNUNET_free (config);
1202 if ((NULL == cp->host) ||
1203 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1204 hostname = "localhost";
1205 /* Change the hostname so that we can connect to it */
1206 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1208 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1214 * Continuation function from GNUNET_HELPER_send()
1216 * @param cls closure
1217 * @param result GNUNET_OK on success,
1218 * GNUNET_NO if helper process died
1219 * GNUNET_SYSERR during GNUNET_HELPER_stop
1222 clear_msg (void *cls, int result)
1224 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1226 GNUNET_assert (NULL != cp->shandle);
1228 GNUNET_free (cp->msg);
1233 * Callback that will be called when the helper process dies. This is not called
1234 * when the helper process is stoped using GNUNET_HELPER_stop()
1236 * @param cls the closure from GNUNET_HELPER_start()
1239 helper_exp_cb (void *cls)
1241 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1242 GNUNET_TESTBED_ControllerStatusCallback cb;
1248 GNUNET_TESTBED_controller_stop (cp);
1250 cb (cb_cls, NULL, GNUNET_SYSERR);
1255 * Function to call to start a link-controllers type operation once all queues
1256 * the operation is part of declare that the operation can be activated.
1258 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1261 opstart_link_controllers (void *cls)
1263 struct OperationContext *opc = cls;
1264 struct ControllerLinkData *data;
1265 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1267 GNUNET_assert (NULL != opc->data);
1271 opc->state = OPC_STATE_STARTED;
1272 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1273 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1278 * Callback which will be called when link-controllers type operation is released
1280 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1283 oprelease_link_controllers (void *cls)
1285 struct OperationContext *opc = cls;
1286 struct ControllerLinkData *data;
1291 case OPC_STATE_INIT:
1292 GNUNET_free (data->msg);
1294 case OPC_STATE_STARTED:
1295 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1297 case OPC_STATE_FINISHED:
1300 GNUNET_free_non_null (data);
1306 * Function to be called when get slave config operation is ready
1308 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1311 opstart_get_slave_config (void *cls)
1313 struct OperationContext *opc = cls;
1314 struct GetSlaveConfigData *data;
1315 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1319 msize = sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage);
1320 msg = GNUNET_malloc (msize);
1321 msg->header.size = htons (msize);
1322 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG);
1323 msg->operation_id = GNUNET_htonll (opc->id);
1324 msg->slave_id = htonl (data->slave_id);
1325 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1326 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1327 opc->state = OPC_STATE_STARTED;
1332 * Function to be called when get slave config operation is cancelled or finished
1334 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1337 oprelease_get_slave_config (void *cls)
1339 struct OperationContext *opc = cls;
1343 case OPC_STATE_INIT:
1344 GNUNET_free (opc->data);
1346 case OPC_STATE_STARTED:
1347 GNUNET_free (opc->data);
1348 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1350 case OPC_STATE_FINISHED:
1351 if (NULL != opc->data)
1352 GNUNET_CONFIGURATION_destroy (opc->data);
1360 * Starts a controller process at the host. FIXME: add controller start callback
1361 * with the configuration with which the controller is started
1363 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1364 * host when starting testbed controller at host
1365 * @param host the host where the controller has to be started; NULL for
1367 * @param cfg template configuration to use for the remote controller; the
1368 * remote controller will be started with a slightly modified
1369 * configuration (port numbers, unix domain sockets and service home
1370 * values are changed as per TESTING library on the remote host)
1371 * @param cb function called when the controller is successfully started or
1372 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1373 * called if cb is called with GNUNET_SYSERR as status. Will never be
1374 * called in the same task as 'GNUNET_TESTBED_controller_start'
1375 * (synchronous errors will be signalled by returning NULL). This
1376 * parameter cannot be NULL.
1377 * @param cls closure for above callbacks
1378 * @return the controller process handle, NULL on errors
1380 struct GNUNET_TESTBED_ControllerProc *
1381 GNUNET_TESTBED_controller_start (const char *controller_ip,
1382 struct GNUNET_TESTBED_Host *host,
1383 const struct GNUNET_CONFIGURATION_Handle *cfg,
1384 GNUNET_TESTBED_ControllerStatusCallback cb,
1387 struct GNUNET_TESTBED_ControllerProc *cp;
1388 struct GNUNET_TESTBED_HelperInit *msg;
1389 const char *hostname;
1390 static char *const binary_argv[] = {
1391 HELPER_TESTBED_BINARY, NULL
1395 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1396 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1398 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1399 &helper_mst, &helper_exp_cb, cp);
1402 char *remote_args[8];
1404 const char *username;
1406 username = GNUNET_TESTBED_host_get_username_ (host);
1407 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1408 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1409 if (NULL == username)
1410 GNUNET_asprintf (&cp->dst, "%s", hostname);
1412 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1413 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1415 remote_args[argp++] = "ssh";
1416 remote_args[argp++] = "-p";
1417 remote_args[argp++] = cp->port;
1418 remote_args[argp++] = "-o";
1419 remote_args[argp++] = "BatchMode=yes";
1420 remote_args[argp++] = cp->dst;
1421 remote_args[argp++] = HELPER_TESTBED_BINARY_SSH;
1422 remote_args[argp++] = NULL;
1423 GNUNET_assert (argp == 8);
1425 GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1426 &helper_exp_cb, cp);
1428 if (NULL == cp->helper)
1430 GNUNET_free_non_null (cp->port);
1431 GNUNET_free_non_null (cp->dst);
1438 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, hostname, cfg);
1439 cp->msg = &msg->header;
1441 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1442 if (NULL == cp->shandle)
1445 GNUNET_TESTBED_controller_stop (cp);
1453 * Stop the controller process (also will terminate all peers and controllers
1454 * dependent on this controller). This function blocks until the testbed has
1455 * been fully terminated (!). The controller status cb from
1456 * GNUNET_TESTBED_controller_start() will not be called.
1458 * @param cproc the controller process handle
1461 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1463 if (NULL != cproc->shandle)
1464 GNUNET_HELPER_send_cancel (cproc->shandle);
1465 if (NULL != cproc->helper)
1466 GNUNET_HELPER_stop (cproc->helper);
1467 if (NULL != cproc->cfg)
1468 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1469 GNUNET_free_non_null (cproc->port);
1470 GNUNET_free_non_null (cproc->dst);
1471 GNUNET_free (cproc);
1476 * Start a controller process using the given configuration at the
1479 * @param cfg configuration to use
1480 * @param host host to run the controller on; This should be the same host if
1481 * the controller was previously started with
1482 * GNUNET_TESTBED_controller_start; NULL for localhost
1483 * @param event_mask bit mask with set of events to call 'cc' for;
1484 * or-ed values of "1LL" shifted by the
1485 * respective 'enum GNUNET_TESTBED_EventType'
1486 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1487 * @param cc controller callback to invoke on events
1488 * @param cc_cls closure for cc
1489 * @return handle to the controller
1491 struct GNUNET_TESTBED_Controller *
1492 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1493 *cfg, struct GNUNET_TESTBED_Host *host,
1494 uint64_t event_mask,
1495 GNUNET_TESTBED_ControllerCallback cc,
1498 struct GNUNET_TESTBED_Controller *controller;
1499 struct GNUNET_TESTBED_InitMessage *msg;
1500 const char *controller_hostname;
1501 unsigned long long max_parallel_operations;
1502 unsigned long long max_parallel_service_connections;
1503 unsigned long long max_parallel_topology_config_operations;
1506 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1507 "MAX_PARALLEL_OPERATIONS",
1508 &max_parallel_operations))
1514 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1515 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1516 &max_parallel_service_connections))
1522 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1523 "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1524 &max_parallel_topology_config_operations))
1529 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1530 controller->cc = cc;
1531 controller->cc_cls = cc_cls;
1532 controller->event_mask = event_mask;
1533 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1534 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1535 if (NULL == controller->client)
1537 GNUNET_TESTBED_controller_disconnect (controller);
1542 host = GNUNET_TESTBED_host_create_by_id_ (0);
1543 if (NULL == host) /* If the above host create fails */
1545 LOG (GNUNET_ERROR_TYPE_WARNING,
1546 "Treating NULL host as localhost. Multiple references to localhost "
1547 "may break when localhost freed before calling disconnect \n");
1548 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1552 controller->aux_host = GNUNET_YES;
1555 GNUNET_assert (NULL != host);
1556 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1557 controller->host = host;
1558 controller->opq_parallel_operations =
1559 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1560 max_parallel_operations);
1561 controller->opq_parallel_service_connections =
1562 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1563 max_parallel_service_connections);
1564 controller->opq_parallel_topology_config_operations=
1565 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1566 max_parallel_service_connections);
1567 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1568 if (NULL == controller_hostname)
1569 controller_hostname = "127.0.0.1";
1571 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1572 strlen (controller_hostname) + 1);
1573 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1575 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1576 strlen (controller_hostname) + 1);
1577 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1578 msg->event_mask = GNUNET_htonll (controller->event_mask);
1579 strcpy ((char *) &msg[1], controller_hostname);
1580 GNUNET_TESTBED_queue_message_ (controller,
1581 (struct GNUNET_MessageHeader *) msg);
1587 * Configure shared services at a controller. Using this function,
1588 * you can specify that certain services (such as "resolver")
1589 * should not be run for each peer but instead be shared
1590 * across N peers on the specified host. This function
1591 * must be called before any peers are created at the host.
1593 * @param controller controller to configure
1594 * @param service_name name of the service to share
1595 * @param num_peers number of peers that should share one instance
1596 * of the specified service (1 for no sharing is the default),
1597 * use 0 to disable the service
1600 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1602 const char *service_name,
1605 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1606 uint16_t service_name_size;
1609 service_name_size = strlen (service_name) + 1;
1611 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1613 msg = GNUNET_malloc (msg_size);
1614 msg->header.size = htons (msg_size);
1615 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1616 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1617 msg->num_peers = htonl (num_peers);
1618 memcpy (&msg[1], service_name, service_name_size);
1619 GNUNET_TESTBED_queue_message_ (controller,
1620 (struct GNUNET_MessageHeader *) msg);
1625 * disconnects from the controller.
1627 * @param controller handle to controller to stop
1630 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1633 struct MessageQueue *mq_entry;
1635 if (NULL != controller->th)
1636 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1637 /* Clear the message queue */
1638 while (NULL != (mq_entry = controller->mq_head))
1640 GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1642 GNUNET_free (mq_entry->msg);
1643 GNUNET_free (mq_entry);
1645 if (NULL != controller->client)
1646 GNUNET_CLIENT_disconnect (controller->client);
1647 GNUNET_CONFIGURATION_destroy (controller->cfg);
1648 if (GNUNET_YES == controller->aux_host)
1649 GNUNET_TESTBED_host_destroy (controller->host);
1650 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1651 GNUNET_TESTBED_operation_queue_destroy_
1652 (controller->opq_parallel_service_connections);
1653 GNUNET_TESTBED_operation_queue_destroy_
1654 (controller->opq_parallel_topology_config_operations);
1655 GNUNET_free (controller);
1660 * Register a host with the controller
1662 * @param controller the controller handle
1663 * @param host the host to register
1664 * @param cc the completion callback to call to inform the status of
1665 * registration. After calling this callback the registration handle
1666 * will be invalid. Cannot be NULL.
1667 * @param cc_cls the closure for the cc
1668 * @return handle to the host registration which can be used to cancel the
1671 struct GNUNET_TESTBED_HostRegistrationHandle *
1672 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1673 struct GNUNET_TESTBED_Host *host,
1674 GNUNET_TESTBED_HostRegistrationCompletion cc,
1677 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1678 struct GNUNET_TESTBED_AddHostMessage *msg;
1679 const char *username;
1680 const char *hostname;
1682 uint16_t user_name_length;
1684 if (NULL != controller->rh)
1686 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1687 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1689 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1690 (NULL == hostname) ? "localhost" : hostname);
1693 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1696 GNUNET_assert (NULL != cc);
1698 rh->cc_cls = cc_cls;
1699 controller->rh = rh;
1700 username = GNUNET_TESTBED_host_get_username_ (host);
1701 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1702 user_name_length = 0;
1703 if (NULL != username)
1705 user_name_length = strlen (username) + 1;
1706 msg_size += user_name_length;
1708 /* FIXME: what happens when hostname is NULL? localhost */
1709 GNUNET_assert (NULL != hostname);
1710 msg_size += strlen (hostname) + 1;
1711 msg = GNUNET_malloc (msg_size);
1712 msg->header.size = htons (msg_size);
1713 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1714 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1715 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1716 msg->user_name_length = htons (user_name_length);
1717 if (NULL != username)
1718 memcpy (&msg[1], username, user_name_length);
1719 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1720 GNUNET_TESTBED_queue_message_ (controller,
1721 (struct GNUNET_MessageHeader *) msg);
1727 * Cancel the pending registration. Note that if the registration message is
1728 * already sent to the service the cancellation has only the effect that the
1729 * registration completion callback for the registration is never called.
1731 * @param handle the registration handle to cancel
1734 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1737 if (handle != handle->c->rh)
1742 handle->c->rh = NULL;
1743 GNUNET_free (handle);
1748 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1749 * serialized and compressed
1751 * @param op_cls the operation closure for the event which is generated to
1752 * signal success or failure of this operation
1753 * @param master handle to the master controller who creates the association
1754 * @param delegated_host requests to which host should be delegated; cannot be NULL
1755 * @param slave_host which host is used to run the slave controller; use NULL to
1756 * make the master controller connect to the delegated host
1757 * @param sxcfg serialized and compressed configuration
1758 * @param sxcfg_size the size sxcfg
1759 * @param scfg_size the size of uncompressed serialized configuration
1760 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1761 * be started by the slave controller; GNUNET_NO if the slave
1762 * controller has to connect to the already started delegated
1763 * controller via TCP/IP
1764 * @return the operation handle
1766 struct GNUNET_TESTBED_Operation *
1767 GNUNET_TESTBED_controller_link_2 (void *op_cls,
1768 struct GNUNET_TESTBED_Controller *master,
1769 struct GNUNET_TESTBED_Host *delegated_host,
1770 struct GNUNET_TESTBED_Host *slave_host,
1771 const char *sxcfg, size_t sxcfg_size,
1772 size_t scfg_size, int is_subordinate)
1774 struct OperationContext *opc;
1775 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1776 struct ControllerLinkData *data;
1779 GNUNET_assert (GNUNET_YES ==
1780 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1781 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1782 GNUNET_assert (GNUNET_YES ==
1783 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1784 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1785 msg = GNUNET_malloc (msg_size);
1786 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1787 msg->header.size = htons (msg_size);
1788 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1789 msg->slave_host_id =
1790 htonl (GNUNET_TESTBED_host_get_id_
1791 ((NULL != slave_host) ? slave_host : master->host));
1792 msg->config_size = htons ((uint16_t) scfg_size);
1793 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1794 memcpy (&msg[1], sxcfg, sxcfg_size);
1795 data = GNUNET_malloc (sizeof (struct ControllerLinkData));
1797 data->op_cls = op_cls;
1798 opc = GNUNET_malloc (sizeof (struct OperationContext));
1801 opc->type = OP_LINK_CONTROLLERS;
1802 opc->id = master->operation_counter++;
1803 opc->state = OPC_STATE_INIT;
1804 msg->operation_id = GNUNET_htonll (opc->id);
1806 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1807 &oprelease_link_controllers);
1808 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1815 * Compresses given configuration using zlib compress
1817 * @param config the serialized configuration
1818 * @param size the size of config
1819 * @param xconfig will be set to the compressed configuration (memory is fresly
1821 * @return the size of the xconfig
1824 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1829 xsize = compressBound ((uLong) size);
1830 *xconfig = GNUNET_malloc (xsize);
1831 GNUNET_assert (Z_OK ==
1832 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1833 (const Bytef *) config, (uLongf) size,
1840 * Create a link from slave controller to delegated controller. Whenever the
1841 * master controller is asked to start a peer at the delegated controller the
1842 * request will be routed towards slave controller (if a route exists). The
1843 * slave controller will then route it to the delegated controller. The
1844 * configuration of the delegated controller is given and is used to either
1845 * create the delegated controller or to connect to an existing controller. Note
1846 * that while starting the delegated controller the configuration will be
1847 * modified to accommodate available free ports. the 'is_subordinate' specifies
1848 * if the given delegated controller should be started and managed by the slave
1849 * controller, or if the delegated controller already has a master and the slave
1850 * controller connects to it as a non master controller. The success or failure
1851 * of this operation will be signalled through the
1852 * GNUNET_TESTBED_ControllerCallback() with an event of type
1853 * GNUNET_TESTBED_ET_OPERATION_FINISHED
1855 * @param op_cls the operation closure for the event which is generated to
1856 * signal success or failure of this operation
1857 * @param master handle to the master controller who creates the association
1858 * @param delegated_host requests to which host should be delegated; cannot be NULL
1859 * @param slave_host which host is used to run the slave controller; use NULL to
1860 * make the master controller connect to the delegated host
1861 * @param slave_cfg configuration to use for the slave controller
1862 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1863 * be started by the slave controller; GNUNET_NO if the slave
1864 * controller has to connect to the already started delegated
1865 * controller via TCP/IP
1866 * @return the operation handle
1868 struct GNUNET_TESTBED_Operation *
1869 GNUNET_TESTBED_controller_link (void *op_cls,
1870 struct GNUNET_TESTBED_Controller *master,
1871 struct GNUNET_TESTBED_Host *delegated_host,
1872 struct GNUNET_TESTBED_Host *slave_host,
1873 const struct GNUNET_CONFIGURATION_Handle
1874 *slave_cfg, int is_subordinate)
1876 struct GNUNET_TESTBED_Operation *op;
1882 GNUNET_assert (GNUNET_YES ==
1883 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1884 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1885 GNUNET_assert (GNUNET_YES ==
1886 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1887 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1888 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1889 GNUNET_free (config);
1890 /* Configuration doesn't fit in 1 message */
1891 GNUNET_assert ((UINT16_MAX -
1892 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >= cc_size);
1893 op = GNUNET_TESTBED_controller_link_2 (op_cls, master, delegated_host,
1894 slave_host, (const char *) cconfig,
1895 cc_size, config_size, is_subordinate);
1896 GNUNET_free (cconfig);
1902 * Function to acquire the configuration of a running slave controller. The
1903 * completion of the operation is signalled through the controller_cb from
1904 * GNUNET_TESTBED_controller_connect(). If the operation is successful the
1905 * handle to the configuration is available in the generic pointer of
1906 * operation_finished field of struct GNUNET_TESTBED_EventInformation.
1908 * @param op_cls the closure for the operation
1909 * @param master the handle to master controller
1910 * @param slave_host the host where the slave controller is running; the handle
1911 * to the slave_host should remain valid until this operation is
1912 * cancelled or marked as finished
1913 * @return the operation handle; NULL if the slave_host is not registered at
1916 struct GNUNET_TESTBED_Operation *
1917 GNUNET_TESTBED_get_slave_config (void *op_cls,
1918 struct GNUNET_TESTBED_Controller *master,
1919 struct GNUNET_TESTBED_Host *slave_host)
1921 struct OperationContext *opc;
1922 struct GetSlaveConfigData *data;
1924 if (GNUNET_NO == GNUNET_TESTBED_is_host_registered_ (slave_host, master))
1926 data = GNUNET_malloc (sizeof (struct GetSlaveConfigData));
1927 data->slave_id = GNUNET_TESTBED_host_get_id_ (slave_host);
1928 data->op_cls = op_cls;
1929 opc = GNUNET_malloc (sizeof (struct OperationContext));
1930 opc->state = OPC_STATE_INIT;
1932 opc->id = master->operation_counter++;
1933 opc->type = OP_GET_SLAVE_CONFIG;
1936 GNUNET_TESTBED_operation_create_ (opc, &opstart_get_slave_config,
1937 &oprelease_get_slave_config);
1938 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1945 * Ask the testbed controller to write the current overlay topology to
1946 * a file. Naturally, the file will only contain a snapshot as the
1947 * topology may evolve all the time.
1949 * @param controller overlay controller to inspect
1950 * @param filename name of the file the topology should
1954 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1956 const char *filename)
1963 * Creates a helper initialization message. This function is here because we
1964 * want to use this in testing
1966 * @param cname the ip address of the controlling host
1967 * @param hostname the hostname of the destination this message is intended for
1968 * @param cfg the configuration that has to used to start the testbed service
1970 * @return the initialization message
1972 struct GNUNET_TESTBED_HelperInit *
1973 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1974 const char *hostname,
1975 const struct GNUNET_CONFIGURATION_Handle
1978 struct GNUNET_TESTBED_HelperInit *msg;
1982 size_t xconfig_size;
1984 uint16_t hostname_len;
1987 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1988 GNUNET_assert (NULL != config);
1990 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1991 GNUNET_free (config);
1992 cname_len = strlen (cname);
1993 hostname_len = (NULL == hostname) ? 0 : strlen (hostname);
1995 xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
1996 msg_size += hostname_len;
1997 msg = GNUNET_realloc (xconfig, msg_size);
1998 (void) memmove (((void *) &msg[1]) + cname_len + 1 + hostname_len,
2001 msg->header.size = htons (msg_size);
2002 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
2003 msg->cname_size = htons (cname_len);
2004 msg->hostname_size = htons (hostname_len);
2005 msg->config_size = htons (config_size);
2006 (void) strcpy ((char *) &msg[1], cname);
2007 if (0 != hostname_len)
2008 (void) strncpy (((char *) &msg[1]) + cname_len + 1, hostname, hostname_len);
2014 * Cancel a pending operation. Releases all resources
2015 * of the operation and will ensure that no event
2016 * is generated for the operation. Does NOT guarantee
2017 * that the operation will be fully undone (or that
2018 * nothing ever happened).
2020 * @param operation operation to cancel
2023 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
2025 GNUNET_TESTBED_operation_done (operation);
2030 * Signal that the information from an operation has been fully
2031 * processed. This function MUST be called for each event
2032 * of type 'operation_finished' to fully remove the operation
2033 * from the operation queue. After calling this function, the
2034 * 'op_result' becomes invalid (!).
2036 * @param operation operation to signal completion for
2039 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
2041 switch (operation->type)
2043 case OP_PEER_CREATE:
2044 case OP_PEER_DESTROY:
2048 case OP_OVERLAY_CONNECT:
2049 case OP_LINK_CONTROLLERS:
2050 GNUNET_TESTBED_operation_release_ (operation);
2060 * Generates configuration by uncompressing configuration in given message. The
2061 * given message should be of the following types:
2062 * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
2063 * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
2065 * @param msg the message containing compressed configuration
2066 * @return handle to the parsed configuration
2068 struct GNUNET_CONFIGURATION_Handle *
2069 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg)
2071 struct GNUNET_CONFIGURATION_Handle *cfg;
2078 switch (ntohs (msg->type))
2080 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
2082 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
2084 imsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2086 data_len = (uLong) ntohs (imsg->config_size);
2087 xdata_len = ntohs (imsg->header.size)
2088 - sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2089 xdata = (const Bytef *) &imsg[1];
2092 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
2094 const struct GNUNET_TESTBED_SlaveConfiguration *imsg;
2096 imsg = (const struct GNUNET_TESTBED_SlaveConfiguration *) msg;
2097 data_len = (uLong) ntohs (imsg->config_size);
2098 xdata_len = ntohs (imsg->header.size)
2099 - sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
2100 xdata = (const Bytef *) &imsg[1];
2106 data = GNUNET_malloc (data_len);
2109 uncompress (data, &data_len, xdata, xdata_len)))
2111 cfg = GNUNET_CONFIGURATION_create ();
2112 GNUNET_assert (GNUNET_OK ==
2113 GNUNET_CONFIGURATION_deserialize (cfg, (const char *) data,
2122 * Checks the integrity of the OperationFailureEventMessage and if good returns
2123 * the error message it contains.
2125 * @param msg the OperationFailureEventMessage
2126 * @return the error message
2129 GNUNET_TESTBED_parse_error_string_ (const struct
2130 GNUNET_TESTBED_OperationFailureEventMessage
2136 msize = ntohs (msg->header.size);
2137 if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
2139 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
2140 emsg = (const char *) &msg[1];
2141 if ('\0' != emsg[msize - 1])
2149 /* end of testbed_api.c */