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 id of the slave controller
251 * Context data for controller link operations
253 struct ControllerLinkData
256 * The controller link message
258 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
264 * Returns the operation context with the given id if found in the Operation
265 * context queues of the controller
267 * @param c the controller whose queues are searched
268 * @param id the id which has to be checked
269 * @return the matching operation context; NULL if no match found
271 static struct OperationContext *
272 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
274 struct OperationContext *opc;
276 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
286 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
287 * controller (testbed service)
289 * @param c the controller handler
290 * @param msg message received
291 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
295 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
296 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
298 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
307 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
309 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
310 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
314 msg_size = ntohs (msg->header.size);
315 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
317 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
318 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
319 rh->cc (rh->cc_cls, NULL);
323 /* We have an error message */
324 emsg = (char *) &msg[1];
326 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
332 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
333 ntohl (msg->host_id), emsg);
334 rh->cc (rh->cc_cls, emsg);
341 * Handler for forwarded operations
343 * @param c the controller handle
344 * @param opc the opearation context
345 * @param msg the message
348 handle_forwarded_operation_msg (struct GNUNET_TESTBED_Controller *c,
349 struct OperationContext *opc,
350 const struct GNUNET_MessageHeader *msg)
352 struct ForwardedOperationData *fo_data;
355 if (NULL != fo_data->cc)
356 fo_data->cc (fo_data->cc_cls, msg);
357 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
358 GNUNET_free (fo_data);
364 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
365 * controller (testbed service)
367 * @param c the controller handler
368 * @param msg message received
369 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
373 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
375 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
377 struct OperationContext *opc;
378 struct GNUNET_TESTBED_EventInformation event;
381 op_id = GNUNET_ntohll (msg->operation_id);
382 LOG_DEBUG ("Operation %ul successful\n", op_id);
383 if (NULL == (opc = find_opc (c, op_id)))
385 LOG_DEBUG ("Operation not found\n");
388 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
389 event.details.operation_finished.operation = opc->op;
390 event.details.operation_finished.op_cls = opc->op_cls;
391 event.details.operation_finished.emsg = NULL;
392 event.details.operation_finished.generic = NULL;
397 handle_forwarded_operation_msg
398 (c, opc, (const struct GNUNET_MessageHeader *) msg);
402 case OP_PEER_DESTROY:
404 struct GNUNET_TESTBED_Peer *peer;
412 case OP_LINK_CONTROLLERS:
414 struct ControllerLinkData *data;
417 GNUNET_assert (NULL != data);
425 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
426 opc->state = OPC_STATE_FINISHED;
427 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
430 c->cc (c->cc_cls, &event);
433 LOG_DEBUG ("Not calling callback\n");
439 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
440 * controller (testbed service)
442 * @param c the controller handle
443 * @param msg message received
444 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
448 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
450 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
452 struct OperationContext *opc;
453 struct PeerCreateData *data;
454 struct GNUNET_TESTBED_Peer *peer;
455 GNUNET_TESTBED_PeerCreateCallback cb;
459 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
460 ntohs (msg->header.size));
461 op_id = GNUNET_ntohll (msg->operation_id);
462 if (NULL == (opc = find_opc (c, op_id)))
464 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
467 if (OP_FORWARDED == opc->type)
469 handle_forwarded_operation_msg (c, opc,
470 (const struct GNUNET_MessageHeader *) msg);
473 GNUNET_assert (OP_PEER_CREATE == opc->type);
474 GNUNET_assert (NULL != opc->data);
476 GNUNET_assert (NULL != data->peer);
478 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
479 peer->state = PS_CREATED;
482 GNUNET_free (opc->data);
483 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
484 opc->state = OPC_STATE_FINISHED;
486 cb (cls, peer, NULL);
492 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
493 * controller (testbed service)
495 * @param c the controller handler
496 * @param msg message received
497 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
501 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
502 const struct GNUNET_TESTBED_PeerEventMessage *msg)
504 struct OperationContext *opc;
505 struct GNUNET_TESTBED_Peer *peer;
506 struct PeerEventData *data;
507 GNUNET_TESTBED_PeerChurnCallback pcc;
509 struct GNUNET_TESTBED_EventInformation event;
512 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
513 ntohs (msg->header.size));
514 op_id = GNUNET_ntohll (msg->operation_id);
515 if (NULL == (opc = find_opc (c, op_id)))
517 LOG_DEBUG ("Operation not found\n");
520 if (OP_FORWARDED == opc->type)
522 handle_forwarded_operation_msg (c, opc,
523 (const struct GNUNET_MessageHeader *) msg);
526 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
528 GNUNET_assert (NULL != data);
530 GNUNET_assert (NULL != peer);
531 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
534 case GNUNET_TESTBED_ET_PEER_START:
535 peer->state = PS_STARTED;
536 event.details.peer_start.host = peer->host;
537 event.details.peer_start.peer = peer;
539 case GNUNET_TESTBED_ET_PEER_STOP:
540 peer->state = PS_STOPPED;
541 event.details.peer_stop.peer = peer;
544 GNUNET_assert (0); /* We should never reach this state */
547 pcc_cls = data->pcc_cls;
549 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
550 opc->state = OPC_STATE_FINISHED;
552 ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP) &
556 c->cc (c->cc_cls, &event);
565 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
566 * controller (testbed service)
568 * @param c the controller handler
569 * @param msg message received
570 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
574 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
575 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
577 struct OperationContext *opc;
578 struct OverlayConnectData *data;
579 GNUNET_TESTBED_OperationCompletionCallback cb;
581 struct GNUNET_TESTBED_EventInformation event;
584 op_id = GNUNET_ntohll (msg->operation_id);
585 if (NULL == (opc = find_opc (c, op_id)))
587 LOG_DEBUG ("Operation not found\n");
590 if (OP_FORWARDED == opc->type)
592 handle_forwarded_operation_msg (c, opc,
593 (const struct GNUNET_MessageHeader *) msg);
596 GNUNET_assert (OP_OVERLAY_CONNECT == opc->type);
598 GNUNET_assert (NULL != data);
599 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
600 (ntohl (msg->peer2) == data->p2->unique_id));
601 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
604 case GNUNET_TESTBED_ET_CONNECT:
605 event.details.peer_connect.peer1 = data->p1;
606 event.details.peer_connect.peer2 = data->p2;
608 case GNUNET_TESTBED_ET_DISCONNECT:
609 GNUNET_assert (0); /* FIXME: implement */
612 GNUNET_assert (0); /* Should never reach here */
616 cb_cls = data->cb_cls;
617 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
618 opc->state = OPC_STATE_FINISHED;
621 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
625 c->cc (c->cc_cls, &event);
628 cb (cb_cls, opc->op, NULL);
634 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
635 * controller (testbed service)
637 * @param c the controller handler
638 * @param msg message received
639 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
643 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
645 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
647 struct OperationContext *opc;
648 struct GNUNET_TESTBED_Peer *peer;
649 struct PeerInfoData *data;
650 struct GNUNET_TESTBED_PeerInformation *pinfo;
651 GNUNET_TESTBED_PeerInfoCallback cb;
655 op_id = GNUNET_ntohll (msg->operation_id);
656 if (NULL == (opc = find_opc (c, op_id)))
658 LOG_DEBUG ("Operation not found\n");
661 if (OP_FORWARDED == opc->type)
663 handle_forwarded_operation_msg (c, opc,
664 (const struct GNUNET_MessageHeader *) msg);
668 GNUNET_assert (NULL != data);
670 GNUNET_assert (NULL != peer);
671 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
672 pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
673 pinfo->pit = data->pit;
675 cb_cls = data->cb_cls;
680 case GNUNET_TESTBED_PIT_IDENTITY:
681 pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
682 (void) memcpy (pinfo->result.id, &msg->peer_identity,
683 sizeof (struct GNUNET_PeerIdentity));
685 case GNUNET_TESTBED_PIT_CONFIGURATION:
686 pinfo->result.cfg = /* Freed in oprelease_peer_getinfo */
687 GNUNET_TESTBED_extract_config_ (&msg->header);
689 case GNUNET_TESTBED_PIT_GENERIC:
690 GNUNET_assert (0); /* never reach here */
694 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
695 opc->state = OPC_STATE_FINISHED;
697 cb (cb_cls, opc->op, pinfo, NULL);
703 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
704 * controller (testbed service)
706 * @param c the controller handler
707 * @param msg message received
708 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
712 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
713 const struct GNUNET_TESTBED_OperationFailureEventMessage
716 struct OperationContext *opc;
719 struct GNUNET_TESTBED_EventInformation event;
721 op_id = GNUNET_ntohll (msg->operation_id);
722 if (NULL == (opc = find_opc (c, op_id)))
724 LOG_DEBUG ("Operation not found\n");
727 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
728 if (OP_FORWARDED == opc->type)
730 handle_forwarded_operation_msg (c, opc,
731 (const struct GNUNET_MessageHeader *) msg);
734 opc->state = OPC_STATE_FINISHED;
735 emsg = GNUNET_TESTBED_parse_error_string_ (msg);
737 emsg = "Unknown error";
738 if (OP_PEER_INFO == opc->type)
740 struct PeerInfoData *data;
742 if (NULL != data->cb)
743 data->cb (data->cb_cls, opc->op, NULL, emsg);
745 return GNUNET_YES; /* We do not call controller callback for peer info */
747 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
750 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
751 event.details.operation_finished.operation = opc->op;
752 event.details.operation_finished.op_cls = opc->op_cls;
753 event.details.operation_finished.emsg = emsg;
754 event.details.operation_finished.generic = NULL;
755 c->cc (c->cc_cls, &event);
761 struct PeerCreateData *data;
763 GNUNET_free (data->peer);
764 if (NULL != data->cb)
765 data->cb (data->cls, NULL, emsg);
772 struct PeerEventData *data;
774 if (NULL != data->pcc)
775 data->pcc (data->pcc_cls, emsg);
779 case OP_PEER_DESTROY:
783 case OP_OVERLAY_CONNECT:
785 struct OverlayConnectData *data;
787 if (NULL != data->cb)
788 data->cb (data->cb_cls, opc->op, emsg);
794 case OP_LINK_CONTROLLERS: /* No secondary callback */
804 * Function to build GET_SLAVE_CONFIG message
806 * @param op_id the id this message should contain in its operation id field
807 * @param slave_id the id this message should contain in its slave id field
808 * @return newly allocated SlaveGetConfigurationMessage
810 static struct GNUNET_TESTBED_SlaveGetConfigurationMessage *
811 GNUNET_TESTBED_generate_slavegetconfig_msg_ (uint64_t op_id, uint32_t slave_id)
813 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
816 msize = sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage);
817 msg = GNUNET_malloc (msize);
818 msg->header.size = htons (msize);
819 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG);
820 msg->operation_id = GNUNET_htonll (op_id);
821 msg->slave_id = htonl (slave_id);
827 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG message from controller
830 * @param c the controller handler
831 * @param msg message received
832 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
836 handle_slave_config (struct GNUNET_TESTBED_Controller *c,
837 const struct GNUNET_TESTBED_SlaveConfiguration * msg)
839 struct OperationContext *opc;
841 struct GNUNET_TESTBED_EventInformation event;
843 op_id = GNUNET_ntohll (msg->operation_id);
844 if (NULL == (opc = find_opc (c, op_id)))
846 LOG_DEBUG ("Operation not found\n");
849 if (OP_GET_SLAVE_CONFIG != opc->type)
854 GNUNET_free (opc->data);
856 opc->state = OPC_STATE_FINISHED;
857 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
858 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
861 opc->data = GNUNET_TESTBED_extract_config_ (&msg->header);
862 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
863 event.details.operation_finished.generic = opc->data;
864 event.details.operation_finished.operation = opc->op;
865 event.details.operation_finished.op_cls = opc->op_cls;
866 event.details.operation_finished.emsg = NULL;
867 c->cc (c->cc_cls, &event);
874 * Callback to check status for suboperations generated during overlay connect.
876 * @param cls the OverlayConnectData
877 * @param message the reply message to the suboperation
880 overlay_connect_ondemand_handler (void *cls,
881 const struct GNUNET_MessageHeader *message)
883 struct OverlayConnectData *oc_data = cls;
885 switch (oc_data->state)
887 case OCD_CFG_ACQUIRE:
889 struct GNUNET_CONFIGURATION_Handle *cfg;
891 if (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG != ntohs (message->type))
893 GNUNET_break (0); /* treat operation as failed */
895 cfg = GNUNET_TESTBED_extract_config_ (message);
898 GNUNET_break (0); /* failed operation */
900 oc_data->state = OCD_LINK_CONTROLLERS;
909 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG message from
910 * controller (testbed service)
912 * @param c the controller handler
913 * @param msg message received
914 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
918 handle_need_controller_config (struct GNUNET_TESTBED_Controller *c,
919 const struct GNUNET_TESTBED_NeedControllerConfig * msg)
921 struct OperationContext *opc;
922 struct OverlayConnectData *oc_data;
925 op_id = GNUNET_ntohll (msg->operation_id);
926 if (NULL == (opc = find_opc (c, op_id)))
928 LOG_DEBUG ("Operation not found\n");
931 if (OP_FORWARDED == opc->type)
933 handle_forwarded_operation_msg (c, opc,
934 (const struct GNUNET_MessageHeader *) msg);
937 GNUNET_assert (OP_OVERLAY_CONNECT == opc->type);
939 /* FIXME: Should spawn operations to:
940 1. Acquire configuration of peer2's controller,
941 2. link peer1's controller to peer2's controller
942 3. ask them to attempt overlay connect on peer1 and peer2 again */
943 switch (oc_data->state)
947 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *get_cfg_msg;
950 GNUNET_assert (NULL == oc_data->sub_opc);
951 sub_op_id = GNUNET_TESTBED_get_next_op_id (oc_data->p1->controller);
953 GNUNET_TESTBED_generate_slavegetconfig_msg_
954 (sub_op_id, GNUNET_TESTBED_host_get_id_ (oc_data->p2->host));
955 oc_data->state = OCD_CFG_ACQUIRE;
957 GNUNET_TESTBED_forward_operation_msg_ (oc_data->p1->controller,
958 sub_op_id, &get_cfg_msg->header,
959 overlay_connect_ondemand_handler,
971 * Handler for messages from controller (testbed service)
973 * @param cls the controller handler
974 * @param msg message received, NULL on timeout or fatal error
977 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
979 struct GNUNET_TESTBED_Controller *c = cls;
983 c->in_receive = GNUNET_NO;
984 /* FIXME: Add checks for message integrity */
987 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
991 msize = ntohs (msg->size);
992 switch (ntohs (msg->type))
994 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
995 GNUNET_assert (msize >=
996 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
998 handle_addhostconfirm (c,
999 (const struct GNUNET_TESTBED_HostConfirmedMessage
1002 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
1003 GNUNET_assert (msize ==
1005 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
1007 handle_opsuccess (c,
1009 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
1012 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
1013 GNUNET_assert (msize ==
1015 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1017 handle_peer_create_success (c,
1019 GNUNET_TESTBED_PeerCreateSuccessEventMessage
1022 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
1023 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1025 handle_peer_event (c,
1026 (const struct GNUNET_TESTBED_PeerEventMessage *)
1030 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
1031 GNUNET_assert (msize >=
1033 GNUNET_TESTBED_PeerConfigurationInformationMessage));
1035 handle_peer_config (c,
1037 GNUNET_TESTBED_PeerConfigurationInformationMessage
1040 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
1041 GNUNET_assert (msize ==
1042 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1044 handle_peer_conevent (c,
1046 GNUNET_TESTBED_ConnectionEventMessage *) msg);
1048 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT:
1049 GNUNET_assert (msize >=
1050 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
1052 handle_op_fail_event (c,
1054 GNUNET_TESTBED_OperationFailureEventMessage *)
1057 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
1058 GNUNET_assert (msize >
1059 sizeof (struct GNUNET_TESTBED_SlaveConfiguration));
1061 handle_slave_config (c, (const struct
1062 GNUNET_TESTBED_SlaveConfiguration *) msg);
1064 case GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG:
1065 GNUNET_assert (msize == sizeof (struct
1066 GNUNET_TESTBED_NeedControllerConfig));
1068 handle_need_controller_config (c, (const struct
1069 GNUNET_TESTBED_NeedControllerConfig
1075 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
1077 c->in_receive = GNUNET_YES;
1078 GNUNET_CLIENT_receive (c->client, &message_handler, c,
1079 GNUNET_TIME_UNIT_FOREVER_REL);
1085 * Function called to notify a client about the connection begin ready to queue
1086 * more data. "buf" will be NULL and "size" zero if the connection was closed
1087 * for writing in the meantime.
1089 * @param cls closure
1090 * @param size number of bytes available in buf
1091 * @param buf where the callee should write the message
1092 * @return number of bytes written to buf
1095 transmit_ready_notify (void *cls, size_t size, void *buf)
1097 struct GNUNET_TESTBED_Controller *c = cls;
1098 struct MessageQueue *mq_entry;
1101 mq_entry = c->mq_head;
1102 GNUNET_assert (NULL != mq_entry);
1103 if ((0 == size) && (NULL == buf)) /* Timeout */
1105 LOG_DEBUG ("Message sending timed out -- retrying\n");
1107 GNUNET_CLIENT_notify_transmit_ready (c->client,
1108 ntohs (mq_entry->msg->size),
1109 TIMEOUT_REL, GNUNET_YES,
1110 &transmit_ready_notify, c);
1113 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
1114 size = ntohs (mq_entry->msg->size);
1115 memcpy (buf, mq_entry->msg, size);
1116 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
1117 ntohs (mq_entry->msg->type), size);
1118 GNUNET_free (mq_entry->msg);
1119 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
1120 GNUNET_free (mq_entry);
1121 mq_entry = c->mq_head;
1122 if (NULL != mq_entry)
1124 GNUNET_CLIENT_notify_transmit_ready (c->client,
1125 ntohs (mq_entry->msg->size),
1126 TIMEOUT_REL, GNUNET_YES,
1127 &transmit_ready_notify, c);
1128 if (GNUNET_NO == c->in_receive)
1130 c->in_receive = GNUNET_YES;
1131 GNUNET_CLIENT_receive (c->client, &message_handler, c,
1132 GNUNET_TIME_UNIT_FOREVER_REL);
1139 * Queues a message in send queue for sending to the service
1141 * @param controller the handle to the controller
1142 * @param msg the message to queue
1145 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
1146 struct GNUNET_MessageHeader *msg)
1148 struct MessageQueue *mq_entry;
1152 type = ntohs (msg->type);
1153 size = ntohs (msg->size);
1154 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
1155 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
1156 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
1157 mq_entry->msg = msg;
1158 LOG (GNUNET_ERROR_TYPE_DEBUG,
1159 "Queueing message of type %u, size %u for sending\n", type,
1161 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
1163 if (NULL == controller->th)
1165 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
1166 TIMEOUT_REL, GNUNET_YES,
1167 &transmit_ready_notify,
1173 * Sends the given message as an operation. The given callback is called when a
1174 * reply for the operation is available. Call
1175 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
1176 * operation context if the cc hasn't been called
1178 * @param controller the controller to which the message has to be sent
1179 * @param operation_id the operation id of the message
1180 * @param msg the message to send
1181 * @param cc the callback to call when reply is available
1182 * @param cc_cls the closure for the above callback
1183 * @return the operation context which can be used to cancel the forwarded
1186 struct OperationContext *
1187 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
1188 *controller, uint64_t operation_id,
1189 const struct GNUNET_MessageHeader *msg,
1190 GNUNET_CLIENT_MessageHandler cc,
1193 struct OperationContext *opc;
1194 struct ForwardedOperationData *data;
1195 struct GNUNET_MessageHeader *dup_msg;
1198 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
1200 data->cc_cls = cc_cls;
1201 opc = GNUNET_malloc (sizeof (struct OperationContext));
1202 opc->c = controller;
1203 opc->type = OP_FORWARDED;
1205 opc->id = operation_id;
1206 msize = ntohs (msg->size);
1207 dup_msg = GNUNET_malloc (msize);
1208 (void) memcpy (dup_msg, msg, msize);
1209 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
1210 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
1217 * Function to cancel an operation created by simply forwarding an operation
1220 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
1223 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
1225 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1226 GNUNET_free (opc->data);
1232 * Functions with this signature are called whenever a
1233 * complete message is received by the tokenizer.
1235 * Do not call GNUNET_SERVER_mst_destroy in callback
1237 * @param cls closure
1238 * @param client identification of the client
1239 * @param message the actual message
1241 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1244 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1246 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1247 const struct GNUNET_TESTBED_HelperReply *msg;
1248 const char *hostname;
1251 uLongf xconfig_size;
1253 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1254 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1255 ntohs (msg->header.size));
1256 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1257 ntohs (msg->header.type));
1258 config_size = (uLongf) ntohs (msg->config_size);
1260 (uLongf) (ntohs (msg->header.size) -
1261 sizeof (struct GNUNET_TESTBED_HelperReply));
1262 config = GNUNET_malloc (config_size);
1263 GNUNET_assert (Z_OK ==
1264 uncompress ((Bytef *) config, &config_size,
1265 (const Bytef *) &msg[1], xconfig_size));
1266 GNUNET_assert (NULL == cp->cfg);
1267 cp->cfg = GNUNET_CONFIGURATION_create ();
1268 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1269 (cp->cfg, config, config_size, GNUNET_NO));
1270 GNUNET_free (config);
1271 if ((NULL == cp->host) ||
1272 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1273 hostname = "localhost";
1274 /* Change the hostname so that we can connect to it */
1275 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1277 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1283 * Continuation function from GNUNET_HELPER_send()
1285 * @param cls closure
1286 * @param result GNUNET_OK on success,
1287 * GNUNET_NO if helper process died
1288 * GNUNET_SYSERR during GNUNET_HELPER_stop
1291 clear_msg (void *cls, int result)
1293 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1295 GNUNET_assert (NULL != cp->shandle);
1297 GNUNET_free (cp->msg);
1302 * Callback that will be called when the helper process dies. This is not called
1303 * when the helper process is stoped using GNUNET_HELPER_stop()
1305 * @param cls the closure from GNUNET_HELPER_start()
1308 helper_exp_cb (void *cls)
1310 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1311 GNUNET_TESTBED_ControllerStatusCallback cb;
1317 GNUNET_TESTBED_controller_stop (cp);
1319 cb (cb_cls, NULL, GNUNET_SYSERR);
1324 * Function to call to start a link-controllers type operation once all queues
1325 * the operation is part of declare that the operation can be activated.
1327 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1330 opstart_link_controllers (void *cls)
1332 struct OperationContext *opc = cls;
1333 struct ControllerLinkData *data;
1334 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1336 GNUNET_assert (NULL != opc->data);
1340 opc->state = OPC_STATE_STARTED;
1341 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1342 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1347 * Callback which will be called when link-controllers type operation is released
1349 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1352 oprelease_link_controllers (void *cls)
1354 struct OperationContext *opc = cls;
1355 struct ControllerLinkData *data;
1360 case OPC_STATE_INIT:
1361 GNUNET_free (data->msg);
1363 case OPC_STATE_STARTED:
1364 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1366 case OPC_STATE_FINISHED:
1369 GNUNET_free_non_null (data);
1375 * Function to be called when get slave config operation is ready
1377 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1380 opstart_get_slave_config (void *cls)
1382 struct OperationContext *opc = cls;
1383 struct GetSlaveConfigData *data;
1384 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1387 msg = GNUNET_TESTBED_generate_slavegetconfig_msg_ (opc->id, data->slave_id);
1388 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1389 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1390 opc->state = OPC_STATE_STARTED;
1395 * Function to be called when get slave config operation is cancelled or finished
1397 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1400 oprelease_get_slave_config (void *cls)
1402 struct OperationContext *opc = cls;
1406 case OPC_STATE_INIT:
1407 GNUNET_free (opc->data);
1409 case OPC_STATE_STARTED:
1410 GNUNET_free (opc->data);
1411 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1413 case OPC_STATE_FINISHED:
1414 if (NULL != opc->data)
1415 GNUNET_CONFIGURATION_destroy (opc->data);
1423 * Starts a controller process at the host. FIXME: add controller start callback
1424 * with the configuration with which the controller is started
1426 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1427 * host when starting testbed controller at host
1428 * @param host the host where the controller has to be started; NULL for
1430 * @param cfg template configuration to use for the remote controller; the
1431 * remote controller will be started with a slightly modified
1432 * configuration (port numbers, unix domain sockets and service home
1433 * values are changed as per TESTING library on the remote host)
1434 * @param cb function called when the controller is successfully started or
1435 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1436 * called if cb is called with GNUNET_SYSERR as status. Will never be
1437 * called in the same task as 'GNUNET_TESTBED_controller_start'
1438 * (synchronous errors will be signalled by returning NULL). This
1439 * parameter cannot be NULL.
1440 * @param cls closure for above callbacks
1441 * @return the controller process handle, NULL on errors
1443 struct GNUNET_TESTBED_ControllerProc *
1444 GNUNET_TESTBED_controller_start (const char *controller_ip,
1445 struct GNUNET_TESTBED_Host *host,
1446 const struct GNUNET_CONFIGURATION_Handle *cfg,
1447 GNUNET_TESTBED_ControllerStatusCallback cb,
1450 struct GNUNET_TESTBED_ControllerProc *cp;
1451 struct GNUNET_TESTBED_HelperInit *msg;
1452 const char *hostname;
1453 static char *const binary_argv[] = {
1454 HELPER_TESTBED_BINARY, NULL
1458 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1459 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1461 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1462 &helper_mst, &helper_exp_cb, cp);
1465 char *remote_args[10];
1467 const char *username;
1469 username = GNUNET_TESTBED_host_get_username_ (host);
1470 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1471 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1472 if (NULL == username)
1473 GNUNET_asprintf (&cp->dst, "%s", hostname);
1475 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1476 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1478 remote_args[argp++] = "ssh";
1479 remote_args[argp++] = "-p";
1480 remote_args[argp++] = cp->port;
1481 remote_args[argp++] = "-o";
1482 remote_args[argp++] = "BatchMode=yes";
1483 remote_args[argp++] = "-o";
1484 remote_args[argp++] = "NoHostAuthenticationForLocalhost=yes";
1485 remote_args[argp++] = cp->dst;
1486 remote_args[argp++] = HELPER_TESTBED_BINARY_SSH;
1487 remote_args[argp++] = NULL;
1488 GNUNET_assert (argp == 10);
1490 GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1491 &helper_exp_cb, cp);
1493 if (NULL == cp->helper)
1495 GNUNET_free_non_null (cp->port);
1496 GNUNET_free_non_null (cp->dst);
1503 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, hostname, cfg);
1504 cp->msg = &msg->header;
1506 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1507 if (NULL == cp->shandle)
1510 GNUNET_TESTBED_controller_stop (cp);
1518 * Stop the controller process (also will terminate all peers and controllers
1519 * dependent on this controller). This function blocks until the testbed has
1520 * been fully terminated (!). The controller status cb from
1521 * GNUNET_TESTBED_controller_start() will not be called.
1523 * @param cproc the controller process handle
1526 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1528 if (NULL != cproc->shandle)
1529 GNUNET_HELPER_send_cancel (cproc->shandle);
1530 if (NULL != cproc->helper)
1531 GNUNET_HELPER_stop (cproc->helper);
1532 if (NULL != cproc->cfg)
1533 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1534 GNUNET_free_non_null (cproc->port);
1535 GNUNET_free_non_null (cproc->dst);
1536 GNUNET_free (cproc);
1541 * Start a controller process using the given configuration at the
1544 * @param cfg configuration to use
1545 * @param host host to run the controller on; This should be the same host if
1546 * the controller was previously started with
1547 * GNUNET_TESTBED_controller_start; NULL for localhost
1548 * @param event_mask bit mask with set of events to call 'cc' for;
1549 * or-ed values of "1LL" shifted by the
1550 * respective 'enum GNUNET_TESTBED_EventType'
1551 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1552 * @param cc controller callback to invoke on events
1553 * @param cc_cls closure for cc
1554 * @return handle to the controller
1556 struct GNUNET_TESTBED_Controller *
1557 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1558 *cfg, struct GNUNET_TESTBED_Host *host,
1559 uint64_t event_mask,
1560 GNUNET_TESTBED_ControllerCallback cc,
1563 struct GNUNET_TESTBED_Controller *controller;
1564 struct GNUNET_TESTBED_InitMessage *msg;
1565 const char *controller_hostname;
1566 unsigned long long max_parallel_operations;
1567 unsigned long long max_parallel_service_connections;
1568 unsigned long long max_parallel_topology_config_operations;
1571 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1572 "MAX_PARALLEL_OPERATIONS",
1573 &max_parallel_operations))
1579 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1580 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1581 &max_parallel_service_connections))
1587 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1588 "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1589 &max_parallel_topology_config_operations))
1594 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1595 controller->cc = cc;
1596 controller->cc_cls = cc_cls;
1597 controller->event_mask = event_mask;
1598 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1599 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1600 if (NULL == controller->client)
1602 GNUNET_TESTBED_controller_disconnect (controller);
1607 host = GNUNET_TESTBED_host_create_by_id_ (0);
1608 if (NULL == host) /* If the above host create fails */
1610 LOG (GNUNET_ERROR_TYPE_WARNING,
1611 "Treating NULL host as localhost. Multiple references to localhost "
1612 "may break when localhost freed before calling disconnect \n");
1613 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1617 controller->aux_host = GNUNET_YES;
1620 GNUNET_assert (NULL != host);
1621 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1622 controller->host = host;
1623 controller->opq_parallel_operations =
1624 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1625 max_parallel_operations);
1626 controller->opq_parallel_service_connections =
1627 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1628 max_parallel_service_connections);
1629 controller->opq_parallel_topology_config_operations=
1630 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1631 max_parallel_service_connections);
1632 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1633 if (NULL == controller_hostname)
1634 controller_hostname = "127.0.0.1";
1636 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1637 strlen (controller_hostname) + 1);
1638 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1640 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1641 strlen (controller_hostname) + 1);
1642 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1643 msg->event_mask = GNUNET_htonll (controller->event_mask);
1644 strcpy ((char *) &msg[1], controller_hostname);
1645 GNUNET_TESTBED_queue_message_ (controller,
1646 (struct GNUNET_MessageHeader *) msg);
1652 * Configure shared services at a controller. Using this function,
1653 * you can specify that certain services (such as "resolver")
1654 * should not be run for each peer but instead be shared
1655 * across N peers on the specified host. This function
1656 * must be called before any peers are created at the host.
1658 * @param controller controller to configure
1659 * @param service_name name of the service to share
1660 * @param num_peers number of peers that should share one instance
1661 * of the specified service (1 for no sharing is the default),
1662 * use 0 to disable the service
1665 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1667 const char *service_name,
1670 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1671 uint16_t service_name_size;
1674 service_name_size = strlen (service_name) + 1;
1676 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1678 msg = GNUNET_malloc (msg_size);
1679 msg->header.size = htons (msg_size);
1680 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1681 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1682 msg->num_peers = htonl (num_peers);
1683 memcpy (&msg[1], service_name, service_name_size);
1684 GNUNET_TESTBED_queue_message_ (controller,
1685 (struct GNUNET_MessageHeader *) msg);
1686 GNUNET_break (0); /* This function is not yet implemented on the
1692 * disconnects from the controller.
1694 * @param controller handle to controller to stop
1697 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1700 struct MessageQueue *mq_entry;
1702 if (NULL != controller->th)
1703 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1704 /* Clear the message queue */
1705 while (NULL != (mq_entry = controller->mq_head))
1707 GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1709 GNUNET_free (mq_entry->msg);
1710 GNUNET_free (mq_entry);
1712 if (NULL != controller->client)
1713 GNUNET_CLIENT_disconnect (controller->client);
1714 GNUNET_CONFIGURATION_destroy (controller->cfg);
1715 if (GNUNET_YES == controller->aux_host)
1716 GNUNET_TESTBED_host_destroy (controller->host);
1717 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1718 GNUNET_TESTBED_operation_queue_destroy_
1719 (controller->opq_parallel_service_connections);
1720 GNUNET_TESTBED_operation_queue_destroy_
1721 (controller->opq_parallel_topology_config_operations);
1722 GNUNET_free (controller);
1727 * Register a host with the controller
1729 * @param controller the controller handle
1730 * @param host the host to register
1731 * @param cc the completion callback to call to inform the status of
1732 * registration. After calling this callback the registration handle
1733 * will be invalid. Cannot be NULL.
1734 * @param cc_cls the closure for the cc
1735 * @return handle to the host registration which can be used to cancel the
1738 struct GNUNET_TESTBED_HostRegistrationHandle *
1739 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1740 struct GNUNET_TESTBED_Host *host,
1741 GNUNET_TESTBED_HostRegistrationCompletion cc,
1744 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1745 struct GNUNET_TESTBED_AddHostMessage *msg;
1746 const char *username;
1747 const char *hostname;
1749 uint16_t user_name_length;
1751 if (NULL != controller->rh)
1753 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1754 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1756 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1757 (NULL == hostname) ? "localhost" : hostname);
1760 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1763 GNUNET_assert (NULL != cc);
1765 rh->cc_cls = cc_cls;
1766 controller->rh = rh;
1767 username = GNUNET_TESTBED_host_get_username_ (host);
1768 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1769 user_name_length = 0;
1770 if (NULL != username)
1772 user_name_length = strlen (username) + 1;
1773 msg_size += user_name_length;
1775 /* FIXME: what happens when hostname is NULL? localhost */
1776 GNUNET_assert (NULL != hostname);
1777 msg_size += strlen (hostname) + 1;
1778 msg = GNUNET_malloc (msg_size);
1779 msg->header.size = htons (msg_size);
1780 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1781 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1782 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1783 if (NULL != username)
1785 msg->user_name_length = htons (user_name_length - 1);
1786 memcpy (&msg[1], username, user_name_length);
1789 msg->user_name_length = htons (user_name_length);
1790 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1791 GNUNET_TESTBED_queue_message_ (controller,
1792 (struct GNUNET_MessageHeader *) msg);
1798 * Cancel the pending registration. Note that if the registration message is
1799 * already sent to the service the cancellation has only the effect that the
1800 * registration completion callback for the registration is never called.
1802 * @param handle the registration handle to cancel
1805 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1808 if (handle != handle->c->rh)
1813 handle->c->rh = NULL;
1814 GNUNET_free (handle);
1819 * Same as the GNUNET_TESTBED_controller_link_2, but with ids for delegated host
1822 * @param op_cls the operation closure for the event which is generated to
1823 * signal success or failure of this operation
1824 * @param master handle to the master controller who creates the association
1825 * @param delegated_host_id id of the host to which requests should be delegated
1826 * @param slave_host_id id of the host which is used to run the slave controller
1827 * @param sxcfg serialized and compressed configuration
1828 * @param sxcfg_size the size sxcfg
1829 * @param scfg_size the size of uncompressed serialized configuration
1830 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1831 * be started by the slave controller; GNUNET_NO if the slave
1832 * controller has to connect to the already started delegated
1833 * controller via TCP/IP
1834 * @return the operation handle
1836 struct GNUNET_TESTBED_Operation *
1837 GNUNET_TESTBED_controller_link_2_ (void *op_cls,
1838 struct GNUNET_TESTBED_Controller *master,
1839 uint32_t delegated_host_id,
1840 uint32_t slave_host_id,
1841 const char *sxcfg, size_t sxcfg_size,
1842 size_t scfg_size, int is_subordinate)
1844 struct OperationContext *opc;
1845 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1846 struct ControllerLinkData *data;
1849 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1850 msg = GNUNET_malloc (msg_size);
1851 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1852 msg->header.size = htons (msg_size);
1853 msg->delegated_host_id = htonl (delegated_host_id);
1854 msg->slave_host_id = htonl (slave_host_id);
1855 msg->config_size = htons ((uint16_t) scfg_size);
1856 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1857 memcpy (&msg[1], sxcfg, sxcfg_size);
1858 data = GNUNET_malloc (sizeof (struct ControllerLinkData));
1860 opc = GNUNET_malloc (sizeof (struct OperationContext));
1863 opc->type = OP_LINK_CONTROLLERS;
1864 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
1865 opc->state = OPC_STATE_INIT;
1866 opc->op_cls = op_cls;
1867 msg->operation_id = GNUNET_htonll (opc->id);
1869 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1870 &oprelease_link_controllers);
1871 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1878 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1879 * serialized and compressed
1881 * @param op_cls the operation closure for the event which is generated to
1882 * signal success or failure of this operation
1883 * @param master handle to the master controller who creates the association
1884 * @param delegated_host requests to which host should be delegated; cannot be NULL
1885 * @param slave_host which host is used to run the slave controller; use NULL to
1886 * make the master controller connect to the delegated host
1887 * @param sxcfg serialized and compressed configuration
1888 * @param sxcfg_size the size sxcfg
1889 * @param scfg_size the size of uncompressed serialized configuration
1890 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1891 * be started by the slave controller; GNUNET_NO if the slave
1892 * controller has to connect to the already started delegated
1893 * controller via TCP/IP
1894 * @return the operation handle
1896 struct GNUNET_TESTBED_Operation *
1897 GNUNET_TESTBED_controller_link_2 (void *op_cls,
1898 struct GNUNET_TESTBED_Controller *master,
1899 struct GNUNET_TESTBED_Host *delegated_host,
1900 struct GNUNET_TESTBED_Host *slave_host,
1901 const char *sxcfg, size_t sxcfg_size,
1902 size_t scfg_size, int is_subordinate)
1904 uint32_t delegated_host_id;
1905 uint32_t slave_host_id;
1907 GNUNET_assert (GNUNET_YES ==
1908 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1909 delegated_host_id = GNUNET_TESTBED_host_get_id_ (delegated_host);
1911 GNUNET_TESTBED_host_get_id_ ((NULL != slave_host)
1912 ? slave_host : master->host);
1913 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1914 GNUNET_assert (GNUNET_YES ==
1915 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1917 return GNUNET_TESTBED_controller_link_2_ (op_cls,
1922 scfg_size, is_subordinate);
1928 * Compresses given configuration using zlib compress
1930 * @param config the serialized configuration
1931 * @param size the size of config
1932 * @param xconfig will be set to the compressed configuration (memory is fresly
1934 * @return the size of the xconfig
1937 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1942 xsize = compressBound ((uLong) size);
1943 *xconfig = GNUNET_malloc (xsize);
1944 GNUNET_assert (Z_OK ==
1945 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1946 (const Bytef *) config, (uLongf) size,
1953 * Same as the GNUNET_TESTBED_controller_link, but with ids for delegated host
1956 * @param op_cls the operation closure for the event which is generated to
1957 * signal success or failure of this operation
1958 * @param master handle to the master controller who creates the association
1959 * @param delegated_host_id id of the host to which requests should be
1960 * delegated; cannot be NULL
1961 * @param slave_host_id id of the host which should connect to controller
1962 * running on delegated host ; use NULL to make the master controller
1963 * connect to the delegated host
1964 * @param slave_cfg configuration to use for the slave controller
1965 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1966 * be started by the slave controller; GNUNET_NO if the slave
1967 * controller has to connect to the already started delegated
1968 * controller via TCP/IP
1969 * @return the operation handle
1971 struct GNUNET_TESTBED_Operation *
1972 GNUNET_TESTBED_controller_link_ (void *op_cls,
1973 struct GNUNET_TESTBED_Controller *master,
1974 uint32_t delegated_host_id,
1975 uint32_t slave_host_id,
1976 const struct GNUNET_CONFIGURATION_Handle
1980 struct GNUNET_TESTBED_Operation *op;
1986 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1987 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1988 GNUNET_free (config);
1989 /* Configuration doesn't fit in 1 message */
1990 GNUNET_assert ((UINT16_MAX -
1991 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >=
1993 op = GNUNET_TESTBED_controller_link_2_ (op_cls, master, delegated_host_id,
1994 slave_host_id, (const char *) cconfig,
1995 cc_size, config_size, is_subordinate);
1996 GNUNET_free (cconfig);
2002 * Create a link from slave controller to delegated controller. Whenever the
2003 * master controller is asked to start a peer at the delegated controller the
2004 * request will be routed towards slave controller (if a route exists). The
2005 * slave controller will then route it to the delegated controller. The
2006 * configuration of the delegated controller is given and is used to either
2007 * create the delegated controller or to connect to an existing controller. Note
2008 * that while starting the delegated controller the configuration will be
2009 * modified to accommodate available free ports. the 'is_subordinate' specifies
2010 * if the given delegated controller should be started and managed by the slave
2011 * controller, or if the delegated controller already has a master and the slave
2012 * controller connects to it as a non master controller. The success or failure
2013 * of this operation will be signalled through the
2014 * GNUNET_TESTBED_ControllerCallback() with an event of type
2015 * GNUNET_TESTBED_ET_OPERATION_FINISHED
2017 * @param op_cls the operation closure for the event which is generated to
2018 * signal success or failure of this operation
2019 * @param master handle to the master controller who creates the association
2020 * @param delegated_host requests to which host should be delegated; cannot be NULL
2021 * @param slave_host which host is used to run the slave controller; use NULL to
2022 * make the master controller connect to the delegated host
2023 * @param slave_cfg configuration to use for the slave controller
2024 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
2025 * be started by the slave controller; GNUNET_NO if the slave
2026 * controller has to connect to the already started delegated
2027 * controller via TCP/IP
2028 * @return the operation handle
2030 struct GNUNET_TESTBED_Operation *
2031 GNUNET_TESTBED_controller_link (void *op_cls,
2032 struct GNUNET_TESTBED_Controller *master,
2033 struct GNUNET_TESTBED_Host *delegated_host,
2034 struct GNUNET_TESTBED_Host *slave_host,
2035 const struct GNUNET_CONFIGURATION_Handle
2036 *slave_cfg, int is_subordinate)
2038 uint32_t slave_host_id;
2039 uint32_t delegated_host_id;
2041 GNUNET_assert (GNUNET_YES ==
2042 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
2043 slave_host_id = (NULL == slave_host) ?
2044 0 : GNUNET_TESTBED_host_get_id_ (slave_host);
2045 delegated_host_id = GNUNET_TESTBED_host_get_id_ (delegated_host);
2046 if ((NULL != slave_host) && (0 != slave_host_id))
2047 GNUNET_assert (GNUNET_YES ==
2048 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
2049 return GNUNET_TESTBED_controller_link_ (op_cls, master,
2059 * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
2060 * check. Another difference is that this function takes the id of the slave
2063 * @param op_cls the closure for the operation
2064 * @param master the handle to master controller
2065 * @param slave_host_id id of the host where the slave controller is running to
2066 * the slave_host should remain valid until this operation is cancelled
2067 * or marked as finished
2068 * @return the operation handle;
2070 struct GNUNET_TESTBED_Operation *
2071 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
2072 struct GNUNET_TESTBED_Controller *master,
2073 uint32_t slave_host_id)
2075 struct OperationContext *opc;
2076 struct GetSlaveConfigData *data;
2078 data = GNUNET_malloc (sizeof (struct GetSlaveConfigData));
2079 data->slave_id = slave_host_id;
2080 opc = GNUNET_malloc (sizeof (struct OperationContext));
2081 opc->state = OPC_STATE_INIT;
2083 opc->id = GNUNET_TESTBED_get_next_op_id (master);
2084 opc->type = OP_GET_SLAVE_CONFIG;
2086 opc->op_cls = op_cls;
2088 GNUNET_TESTBED_operation_create_ (opc, &opstart_get_slave_config,
2089 &oprelease_get_slave_config);
2090 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
2097 * Function to acquire the configuration of a running slave controller. The
2098 * completion of the operation is signalled through the controller_cb from
2099 * GNUNET_TESTBED_controller_connect(). If the operation is successful the
2100 * handle to the configuration is available in the generic pointer of
2101 * operation_finished field of struct GNUNET_TESTBED_EventInformation.
2103 * @param op_cls the closure for the operation
2104 * @param master the handle to master controller
2105 * @param slave_host the host where the slave controller is running; the handle
2106 * to the slave_host should remain valid until this operation is
2107 * cancelled or marked as finished
2108 * @return the operation handle; NULL if the slave_host is not registered at
2111 struct GNUNET_TESTBED_Operation *
2112 GNUNET_TESTBED_get_slave_config (void *op_cls,
2113 struct GNUNET_TESTBED_Controller *master,
2114 struct GNUNET_TESTBED_Host *slave_host)
2116 if (GNUNET_NO == GNUNET_TESTBED_is_host_registered_ (slave_host, master))
2118 return GNUNET_TESTBED_get_slave_config_ (op_cls, master,
2119 GNUNET_TESTBED_host_get_id_ (slave_host));
2124 * Ask the testbed controller to write the current overlay topology to
2125 * a file. Naturally, the file will only contain a snapshot as the
2126 * topology may evolve all the time.
2128 * @param controller overlay controller to inspect
2129 * @param filename name of the file the topology should
2133 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
2135 const char *filename)
2142 * Creates a helper initialization message. This function is here because we
2143 * want to use this in testing
2145 * @param cname the ip address of the controlling host
2146 * @param hostname the hostname of the destination this message is intended for
2147 * @param cfg the configuration that has to used to start the testbed service
2149 * @return the initialization message
2151 struct GNUNET_TESTBED_HelperInit *
2152 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
2153 const char *hostname,
2154 const struct GNUNET_CONFIGURATION_Handle
2157 struct GNUNET_TESTBED_HelperInit *msg;
2161 size_t xconfig_size;
2163 uint16_t hostname_len;
2166 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
2167 GNUNET_assert (NULL != config);
2169 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
2170 GNUNET_free (config);
2171 cname_len = strlen (cname);
2172 hostname_len = (NULL == hostname) ? 0 : strlen (hostname);
2174 xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
2175 msg_size += hostname_len;
2176 msg = GNUNET_realloc (xconfig, msg_size);
2177 (void) memmove (((void *) &msg[1]) + cname_len + 1 + hostname_len,
2180 msg->header.size = htons (msg_size);
2181 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
2182 msg->cname_size = htons (cname_len);
2183 msg->hostname_size = htons (hostname_len);
2184 msg->config_size = htons (config_size);
2185 (void) strcpy ((char *) &msg[1], cname);
2186 if (0 != hostname_len)
2187 (void) strncpy (((char *) &msg[1]) + cname_len + 1, hostname, hostname_len);
2193 * Cancel a pending operation. Releases all resources
2194 * of the operation and will ensure that no event
2195 * is generated for the operation. Does NOT guarantee
2196 * that the operation will be fully undone (or that
2197 * nothing ever happened).
2199 * @param operation operation to cancel
2202 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
2204 GNUNET_TESTBED_operation_done (operation);
2209 * Signal that the information from an operation has been fully
2210 * processed. This function MUST be called for each event
2211 * of type 'operation_finished' to fully remove the operation
2212 * from the operation queue. After calling this function, the
2213 * 'op_result' becomes invalid (!).
2215 * @param operation operation to signal completion for
2218 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
2220 switch (operation->type)
2222 case OP_PEER_CREATE:
2223 case OP_PEER_DESTROY:
2227 case OP_OVERLAY_CONNECT:
2228 case OP_LINK_CONTROLLERS:
2229 GNUNET_TESTBED_operation_release_ (operation);
2239 * Generates configuration by uncompressing configuration in given message. The
2240 * given message should be of the following types:
2241 * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
2242 * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
2244 * @param msg the message containing compressed configuration
2245 * @return handle to the parsed configuration
2247 struct GNUNET_CONFIGURATION_Handle *
2248 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg)
2250 struct GNUNET_CONFIGURATION_Handle *cfg;
2257 switch (ntohs (msg->type))
2259 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
2261 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
2263 imsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2265 data_len = (uLong) ntohs (imsg->config_size);
2266 xdata_len = ntohs (imsg->header.size)
2267 - sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2268 xdata = (const Bytef *) &imsg[1];
2271 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
2273 const struct GNUNET_TESTBED_SlaveConfiguration *imsg;
2275 imsg = (const struct GNUNET_TESTBED_SlaveConfiguration *) msg;
2276 data_len = (uLong) ntohs (imsg->config_size);
2277 xdata_len = ntohs (imsg->header.size)
2278 - sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
2279 xdata = (const Bytef *) &imsg[1];
2285 data = GNUNET_malloc (data_len);
2288 uncompress (data, &data_len, xdata, xdata_len)))
2290 cfg = GNUNET_CONFIGURATION_create ();
2291 GNUNET_assert (GNUNET_OK ==
2292 GNUNET_CONFIGURATION_deserialize (cfg, (const char *) data,
2301 * Checks the integrity of the OperationFailureEventMessage and if good returns
2302 * the error message it contains.
2304 * @param msg the OperationFailureEventMessage
2305 * @return the error message
2308 GNUNET_TESTBED_parse_error_string_ (const struct
2309 GNUNET_TESTBED_OperationFailureEventMessage
2315 msize = ntohs (msg->header.size);
2316 if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
2318 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
2319 emsg = (const char *) &msg[1];
2320 if ('\0' != emsg[msize - 1])
2330 * Function to return the operation id for a controller. The operation id is
2331 * created from the controllers host id and its internal operation counter.
2333 * @param controller the handle to the controller whose operation id has to be incremented
2334 * @return the incremented operation id.
2337 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller *controller)
2341 op_id = (uint64_t) GNUNET_TESTBED_host_get_id_ (controller->host);
2342 op_id = op_id << 32;
2343 op_id |= (uint64_t) controller->operation_counter++;
2347 /* end of testbed_api.c */