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"
77 * The message queue for sending messages to the controller service
82 * The message to be sent
84 struct GNUNET_MessageHeader *msg;
87 * next pointer for DLL
89 struct MessageQueue *next;
92 * prev pointer for DLL
94 struct MessageQueue *prev;
99 * Structure for a controller link
101 struct ControllerLink
104 * The next ptr for DLL
106 struct ControllerLink *next;
109 * The prev ptr for DLL
111 struct ControllerLink *prev;
114 * The host which will be referred in the peer start request. This is the
115 * host where the peer should be started
117 struct GNUNET_TESTBED_Host *delegated_host;
120 * The host which will contacted to delegate the peer start request
122 struct GNUNET_TESTBED_Host *slave_host;
125 * The configuration to be used to connect to slave host
127 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
130 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
131 * if we are just allowed to use the slave via TCP/IP
138 * handle for host registration
140 struct GNUNET_TESTBED_HostRegistrationHandle
143 * The host being registered
145 struct GNUNET_TESTBED_Host *host;
148 * The controller at which this host is being registered
150 struct GNUNET_TESTBED_Controller *c;
153 * The Registartion completion callback
155 GNUNET_TESTBED_HostRegistrationCompletion cc;
158 * The closure for above callback
165 * Context data for forwarded Operation
167 struct ForwardedOperationData
171 * The callback to call when reply is available
173 GNUNET_CLIENT_MessageHandler cc;
176 * The closure for the above callback
184 * Returns the operation context with the given id if found in the Operation
185 * context queues of the controller
187 * @param c the controller whose queues are searched
188 * @param id the id which has to be checked
189 * @return the matching operation context; NULL if no match found
191 static struct OperationContext *
192 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
194 struct OperationContext *opc;
196 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
206 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
207 * controller (testbed service)
209 * @param c the controller handler
210 * @param msg message received
211 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
215 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
216 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
218 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
227 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
229 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
230 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
234 msg_size = ntohs (msg->header.size);
235 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
237 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
238 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
239 rh->cc (rh->cc_cls, NULL);
243 /* We have an error message */
244 emsg = (char *) &msg[1];
246 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
252 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
253 ntohl (msg->host_id), emsg);
254 rh->cc (rh->cc_cls, emsg);
261 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
262 * controller (testbed service)
264 * @param c the controller handler
265 * @param msg message received
266 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
270 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
272 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
274 struct OperationContext *opc;
275 struct GNUNET_TESTBED_EventInformation event;
278 op_id = GNUNET_ntohll (msg->operation_id);
279 LOG_DEBUG ("Operation %ul successful\n", op_id);
280 if (NULL == (opc = find_opc (c, op_id)))
282 LOG_DEBUG ("Operation not found\n");
289 struct ForwardedOperationData *fo_data;
292 if (NULL != fo_data->cc)
293 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
294 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
295 GNUNET_free (fo_data);
300 case OP_PEER_DESTROY:
302 struct GNUNET_TESTBED_Peer *peer;
310 case OP_LINK_CONTROLLERS:
315 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
316 event.details.operation_finished.operation = opc->op;
317 event.details.operation_finished.op_cls = NULL;
318 event.details.operation_finished.emsg = NULL;
319 event.details.operation_finished.generic = NULL;
320 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
321 opc->state = OPC_STATE_FINISHED;
322 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
325 c->cc (c->cc_cls, &event);
332 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
333 * controller (testbed service)
335 * @param c the controller handler
336 * @param msg message received
337 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
341 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
343 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
345 struct OperationContext *opc;
346 struct PeerCreateData *data;
347 struct GNUNET_TESTBED_Peer *peer;
348 GNUNET_TESTBED_PeerCreateCallback cb;
352 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
353 ntohs (msg->header.size));
354 op_id = GNUNET_ntohll (msg->operation_id);
355 if (NULL == (opc = find_opc (c, op_id)))
357 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
360 if (OP_FORWARDED == opc->type)
362 struct ForwardedOperationData *fo_data;
365 if (NULL != fo_data->cc)
366 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
367 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
368 GNUNET_free (fo_data);
372 GNUNET_assert (OP_PEER_CREATE == opc->type);
373 GNUNET_assert (NULL != opc->data);
375 GNUNET_assert (NULL != data->peer);
377 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
378 peer->state = PS_CREATED;
381 GNUNET_free (opc->data);
382 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
383 opc->state = OPC_STATE_FINISHED;
385 cb (cls, peer, NULL);
391 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
392 * controller (testbed service)
394 * @param c the controller handler
395 * @param msg message received
396 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
400 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
401 const struct GNUNET_TESTBED_PeerEventMessage *msg)
403 struct OperationContext *opc;
404 struct GNUNET_TESTBED_Peer *peer;
405 struct PeerEventData *data;
406 GNUNET_TESTBED_PeerChurnCallback pcc;
408 struct GNUNET_TESTBED_EventInformation event;
411 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
412 ntohs (msg->header.size));
413 op_id = GNUNET_ntohll (msg->operation_id);
414 if (NULL == (opc = find_opc (c, op_id)))
416 LOG_DEBUG ("Operation not found\n");
419 if (OP_FORWARDED == opc->type)
421 struct ForwardedOperationData *fo_data;
424 if (NULL != fo_data->cc)
425 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
426 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
427 GNUNET_free (fo_data);
431 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
433 GNUNET_assert (NULL != data);
435 GNUNET_assert (NULL != peer);
436 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
439 case GNUNET_TESTBED_ET_PEER_START:
440 peer->state = PS_STARTED;
441 event.details.peer_start.host = peer->host;
442 event.details.peer_start.peer = peer;
444 case GNUNET_TESTBED_ET_PEER_STOP:
445 peer->state = PS_STOPPED;
446 event.details.peer_stop.peer = peer;
449 GNUNET_assert (0); /* We should never reach this state */
452 pcc_cls = data->pcc_cls;
454 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
455 opc->state = OPC_STATE_FINISHED;
457 ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP) &
461 c->cc (c->cc_cls, &event);
470 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
471 * controller (testbed service)
473 * @param c the controller handler
474 * @param msg message received
475 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
479 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
480 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
482 struct OperationContext *opc;
483 struct OverlayConnectData *data;
484 GNUNET_TESTBED_OperationCompletionCallback cb;
486 struct GNUNET_TESTBED_EventInformation event;
489 op_id = GNUNET_ntohll (msg->operation_id);
490 if (NULL == (opc = find_opc (c, op_id)))
492 LOG_DEBUG ("Operation not found\n");
496 GNUNET_assert (NULL != data);
497 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
498 (ntohl (msg->peer2) == data->p2->unique_id));
499 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
502 case GNUNET_TESTBED_ET_CONNECT:
503 event.details.peer_connect.peer1 = data->p1;
504 event.details.peer_connect.peer2 = data->p2;
506 case GNUNET_TESTBED_ET_DISCONNECT:
507 GNUNET_assert (0); /* FIXME: implement */
510 GNUNET_assert (0); /* Should never reach here */
514 cb_cls = data->cb_cls;
515 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
516 opc->state = OPC_STATE_FINISHED;
519 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
523 c->cc (c->cc_cls, &event);
526 cb (cb_cls, opc->op, NULL);
532 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
533 * controller (testbed service)
535 * @param c the controller handler
536 * @param msg message received
537 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
541 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
543 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
545 struct OperationContext *opc;
546 struct GNUNET_TESTBED_Peer *peer;
547 struct PeerInfoData *data;
548 struct GNUNET_TESTBED_PeerInformation *pinfo;
549 GNUNET_TESTBED_PeerInfoCallback cb;
553 op_id = GNUNET_ntohll (msg->operation_id);
554 if (NULL == (opc = find_opc (c, op_id)))
556 LOG_DEBUG ("Operation not found\n");
559 if (OP_FORWARDED == opc->type)
561 struct ForwardedOperationData *fo_data;
564 if (NULL != fo_data->cc)
565 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
566 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
567 GNUNET_free (fo_data);
572 GNUNET_assert (NULL != data);
574 GNUNET_assert (NULL != peer);
575 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
576 pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
577 pinfo->pit = data->pit;
579 cb_cls = data->cb_cls;
584 case GNUNET_TESTBED_PIT_IDENTITY:
585 pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
586 (void) memcpy (pinfo->result.id, &msg->peer_identity,
587 sizeof (struct GNUNET_PeerIdentity));
589 case GNUNET_TESTBED_PIT_CONFIGURATION:
590 pinfo->result.cfg = /* Freed in oprelease_peer_getinfo */
591 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (msg);
593 case GNUNET_TESTBED_PIT_GENERIC:
594 GNUNET_assert (0); /* never reach here */
598 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
599 opc->state = OPC_STATE_FINISHED;
601 cb (cb_cls, opc->op, pinfo, NULL);
607 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
608 * controller (testbed service)
610 * @param c the controller handler
611 * @param msg message received
612 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
616 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
617 const struct GNUNET_TESTBED_OperationFailureEventMessage
620 struct OperationContext *opc;
623 struct GNUNET_TESTBED_EventInformation event;
625 op_id = GNUNET_ntohll (msg->operation_id);
626 if (NULL == (opc = find_opc (c, op_id)))
628 LOG_DEBUG ("Operation not found\n");
631 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
632 if (OP_FORWARDED == opc->type)
634 struct ForwardedOperationData *fo_data;
637 if (NULL != fo_data->cc)
638 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
639 GNUNET_free (fo_data);
643 opc->state = OPC_STATE_FINISHED;
644 emsg = GNUNET_TESTBED_parse_error_string_ (msg);
646 emsg = "Unknown error";
647 if (OP_PEER_INFO == opc->type)
649 struct PeerInfoData *data;
651 if (NULL != data->cb)
652 data->cb (data->cb_cls, opc->op, NULL, emsg);
654 return GNUNET_YES; /* We do not call controller callback for peer info */
656 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
659 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
660 event.details.operation_finished.operation = opc->op;
661 event.details.operation_finished.op_cls = NULL;
662 event.details.operation_finished.emsg = emsg;
663 event.details.operation_finished.generic = NULL;
664 c->cc (c->cc_cls, &event);
670 struct PeerCreateData *data;
672 GNUNET_free (data->peer);
673 if (NULL != data->cb)
674 data->cb (data->cls, NULL, emsg);
681 struct PeerEventData *data;
683 if (NULL != data->pcc)
684 data->pcc (data->pcc_cls, emsg);
688 case OP_PEER_DESTROY:
692 case OP_OVERLAY_CONNECT:
694 struct OverlayConnectData *data;
696 if (NULL != data->cb)
697 data->cb (data->cb_cls, opc->op, emsg);
703 case OP_LINK_CONTROLLERS: /* No secondary callback */
713 * Handler for messages from controller (testbed service)
715 * @param cls the controller handler
716 * @param msg message received, NULL on timeout or fatal error
719 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
721 struct GNUNET_TESTBED_Controller *c = cls;
725 c->in_receive = GNUNET_NO;
726 /* FIXME: Add checks for message integrity */
729 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
733 msize = ntohs (msg->size);
734 switch (ntohs (msg->type))
736 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
737 GNUNET_assert (msize >=
738 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
740 handle_addhostconfirm (c,
741 (const struct GNUNET_TESTBED_HostConfirmedMessage
744 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
745 GNUNET_assert (msize ==
747 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
751 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
754 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
755 GNUNET_assert (msize ==
757 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
759 handle_peer_create_success (c,
761 GNUNET_TESTBED_PeerCreateSuccessEventMessage
764 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
765 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
767 handle_peer_event (c,
768 (const struct GNUNET_TESTBED_PeerEventMessage *)
772 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
773 GNUNET_assert (msize >=
775 GNUNET_TESTBED_PeerConfigurationInformationMessage));
777 handle_peer_config (c,
779 GNUNET_TESTBED_PeerConfigurationInformationMessage
782 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
783 GNUNET_assert (msize ==
784 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
786 handle_peer_conevent (c,
788 GNUNET_TESTBED_ConnectionEventMessage *) msg);
790 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT:
791 GNUNET_assert (msize >=
792 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
794 handle_op_fail_event (c,
796 GNUNET_TESTBED_OperationFailureEventMessage *)
802 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
804 c->in_receive = GNUNET_YES;
805 GNUNET_CLIENT_receive (c->client, &message_handler, c,
806 GNUNET_TIME_UNIT_FOREVER_REL);
812 * Function called to notify a client about the connection begin ready to queue
813 * more data. "buf" will be NULL and "size" zero if the connection was closed
814 * for writing in the meantime.
817 * @param size number of bytes available in buf
818 * @param buf where the callee should write the message
819 * @return number of bytes written to buf
822 transmit_ready_notify (void *cls, size_t size, void *buf)
824 struct GNUNET_TESTBED_Controller *c = cls;
825 struct MessageQueue *mq_entry;
828 mq_entry = c->mq_head;
829 GNUNET_assert (NULL != mq_entry);
830 if ((0 == size) && (NULL == buf)) /* Timeout */
832 LOG_DEBUG ("Message sending timed out -- retrying\n");
834 GNUNET_CLIENT_notify_transmit_ready (c->client,
835 ntohs (mq_entry->msg->size),
836 TIMEOUT_REL, GNUNET_YES,
837 &transmit_ready_notify, c);
840 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
841 size = ntohs (mq_entry->msg->size);
842 memcpy (buf, mq_entry->msg, size);
843 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
844 ntohs (mq_entry->msg->type), size);
845 GNUNET_free (mq_entry->msg);
846 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
847 GNUNET_free (mq_entry);
848 mq_entry = c->mq_head;
849 if (NULL != mq_entry)
851 GNUNET_CLIENT_notify_transmit_ready (c->client,
852 ntohs (mq_entry->msg->size),
853 TIMEOUT_REL, GNUNET_YES,
854 &transmit_ready_notify, c);
855 if (GNUNET_NO == c->in_receive)
857 c->in_receive = GNUNET_YES;
858 GNUNET_CLIENT_receive (c->client, &message_handler, c,
859 GNUNET_TIME_UNIT_FOREVER_REL);
866 * Queues a message in send queue for sending to the service
868 * @param controller the handle to the controller
869 * @param msg the message to queue
872 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
873 struct GNUNET_MessageHeader *msg)
875 struct MessageQueue *mq_entry;
879 type = ntohs (msg->type);
880 size = ntohs (msg->size);
881 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
882 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
883 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
885 LOG (GNUNET_ERROR_TYPE_DEBUG,
886 "Queueing message of type %u, size %u for sending\n", type,
888 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
890 if (NULL == controller->th)
892 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
893 TIMEOUT_REL, GNUNET_YES,
894 &transmit_ready_notify,
900 * Sends the given message as an operation. The given callback is called when a
901 * reply for the operation is available. Call
902 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
903 * operation context if the cc hasn't been called
905 * @param controller the controller to which the message has to be sent
906 * @param operation_id the operation id of the message
907 * @param msg the message to send
908 * @param cc the callback to call when reply is available
909 * @param cc_cls the closure for the above callback
910 * @return the operation context which can be used to cancel the forwarded
913 struct OperationContext *
914 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
915 *controller, uint64_t operation_id,
916 const struct GNUNET_MessageHeader *msg,
917 GNUNET_CLIENT_MessageHandler cc,
920 struct OperationContext *opc;
921 struct ForwardedOperationData *data;
922 struct GNUNET_MessageHeader *dup_msg;
925 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
927 data->cc_cls = cc_cls;
928 opc = GNUNET_malloc (sizeof (struct OperationContext));
930 opc->type = OP_FORWARDED;
932 opc->id = operation_id;
933 msize = ntohs (msg->size);
934 dup_msg = GNUNET_malloc (msize);
935 (void) memcpy (dup_msg, msg, msize);
936 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
937 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
944 * Function to cancel an operation created by simply forwarding an operation
947 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
950 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
952 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
953 GNUNET_free (opc->data);
959 * Handle for controller process
961 struct GNUNET_TESTBED_ControllerProc
966 struct GNUNET_HELPER_Handle *helper;
969 * The host where the helper is run
971 struct GNUNET_TESTBED_Host *host;
974 * The controller error callback
976 GNUNET_TESTBED_ControllerStatusCallback cb;
979 * The closure for the above callback
984 * The send handle for the helper
986 struct GNUNET_HELPER_SendHandle *shandle;
989 * The message corresponding to send handle
991 struct GNUNET_MessageHeader *msg;
994 * The port number for ssh; used for helpers starting ssh
999 * The ssh destination string; used for helpers starting ssh
1004 * The configuration of the running testbed service
1006 struct GNUNET_CONFIGURATION_Handle *cfg;
1012 * Functions with this signature are called whenever a
1013 * complete message is received by the tokenizer.
1015 * Do not call GNUNET_SERVER_mst_destroy in callback
1017 * @param cls closure
1018 * @param client identification of the client
1019 * @param message the actual message
1021 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1024 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1026 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1027 const struct GNUNET_TESTBED_HelperReply *msg;
1028 const char *hostname;
1031 uLongf xconfig_size;
1033 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1034 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1035 ntohs (msg->header.size));
1036 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1037 ntohs (msg->header.type));
1038 config_size = (uLongf) ntohs (msg->config_size);
1040 (uLongf) (ntohs (msg->header.size) -
1041 sizeof (struct GNUNET_TESTBED_HelperReply));
1042 config = GNUNET_malloc (config_size);
1043 GNUNET_assert (Z_OK ==
1044 uncompress ((Bytef *) config, &config_size,
1045 (const Bytef *) &msg[1], xconfig_size));
1046 GNUNET_assert (NULL == cp->cfg);
1047 cp->cfg = GNUNET_CONFIGURATION_create ();
1048 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1049 (cp->cfg, config, config_size, GNUNET_NO));
1050 GNUNET_free (config);
1051 if ((NULL == cp->host) ||
1052 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1053 hostname = "localhost";
1054 /* Change the hostname so that we can connect to it */
1055 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1057 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1063 * Continuation function from GNUNET_HELPER_send()
1065 * @param cls closure
1066 * @param result GNUNET_OK on success,
1067 * GNUNET_NO if helper process died
1068 * GNUNET_SYSERR during GNUNET_HELPER_stop
1071 clear_msg (void *cls, int result)
1073 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1075 GNUNET_assert (NULL != cp->shandle);
1077 GNUNET_free (cp->msg);
1082 * Callback that will be called when the helper process dies. This is not called
1083 * when the helper process is stoped using GNUNET_HELPER_stop()
1085 * @param cls the closure from GNUNET_HELPER_start()
1088 helper_exp_cb (void *cls)
1090 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1091 GNUNET_TESTBED_ControllerStatusCallback cb;
1097 GNUNET_TESTBED_controller_stop (cp);
1099 cb (cb_cls, NULL, GNUNET_SYSERR);
1104 * Function to call to start a link-controllers type operation once all queues
1105 * the operation is part of declare that the operation can be activated.
1107 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1110 opstart_link_controllers (void *cls)
1112 struct OperationContext *opc = cls;
1113 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1115 GNUNET_assert (NULL != opc->data);
1118 opc->state = OPC_STATE_STARTED;
1119 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1120 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1125 * Callback which will be called when link-controllers type operation is released
1127 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1130 oprelease_link_controllers (void *cls)
1132 struct OperationContext *opc = cls;
1134 if (OPC_STATE_INIT == opc->state)
1135 GNUNET_free (opc->data);
1136 if (OPC_STATE_STARTED == opc->state)
1137 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1143 * Starts a controller process at the host. FIXME: add controller start callback
1144 * with the configuration with which the controller is started
1146 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1147 * host when starting testbed controller at host
1148 * @param host the host where the controller has to be started; NULL for
1150 * @param cfg template configuration to use for the remote controller; the
1151 * remote controller will be started with a slightly modified
1152 * configuration (port numbers, unix domain sockets and service home
1153 * values are changed as per TESTING library on the remote host)
1154 * @param cb function called when the controller is successfully started or
1155 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1156 * called if cb is called with GNUNET_SYSERR as status. Will never be
1157 * called in the same task as 'GNUNET_TESTBED_controller_start'
1158 * (synchronous errors will be signalled by returning NULL). This
1159 * parameter cannot be NULL.
1160 * @param cls closure for above callbacks
1161 * @return the controller process handle, NULL on errors
1163 struct GNUNET_TESTBED_ControllerProc *
1164 GNUNET_TESTBED_controller_start (const char *controller_ip,
1165 struct GNUNET_TESTBED_Host *host,
1166 const struct GNUNET_CONFIGURATION_Handle *cfg,
1167 GNUNET_TESTBED_ControllerStatusCallback cb,
1170 struct GNUNET_TESTBED_ControllerProc *cp;
1171 struct GNUNET_TESTBED_HelperInit *msg;
1172 static char *const binary_argv[] = {
1173 HELPER_TESTBED_BINARY, NULL
1176 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1177 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1179 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1180 &helper_mst, &helper_exp_cb, cp);
1183 char *remote_args[8];
1185 const char *username;
1186 const char *hostname;
1188 username = GNUNET_TESTBED_host_get_username_ (host);
1189 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1190 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1191 if (NULL == username)
1192 GNUNET_asprintf (&cp->dst, "%s", hostname);
1194 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1195 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1197 remote_args[argp++] = "ssh";
1198 remote_args[argp++] = "-p";
1199 remote_args[argp++] = cp->port;
1200 remote_args[argp++] = "-o";
1201 remote_args[argp++] = "BatchMode=yes";
1202 remote_args[argp++] = cp->dst;
1203 remote_args[argp++] = HELPER_TESTBED_BINARY;
1204 remote_args[argp++] = NULL;
1205 GNUNET_assert (argp == 8);
1207 GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1208 &helper_exp_cb, cp);
1210 if (NULL == cp->helper)
1212 GNUNET_free_non_null (cp->port);
1213 GNUNET_free_non_null (cp->dst);
1220 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1221 cp->msg = &msg->header;
1223 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1224 if (NULL == cp->shandle)
1227 GNUNET_TESTBED_controller_stop (cp);
1235 * Stop the controller process (also will terminate all peers and controllers
1236 * dependent on this controller). This function blocks until the testbed has
1237 * been fully terminated (!).
1239 * @param cproc the controller process handle
1242 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1244 if (NULL != cproc->shandle)
1245 GNUNET_HELPER_send_cancel (cproc->shandle);
1246 if (NULL != cproc->helper)
1247 GNUNET_HELPER_stop (cproc->helper);
1248 if (NULL != cproc->cfg)
1249 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1250 GNUNET_free_non_null (cproc->port);
1251 GNUNET_free_non_null (cproc->dst);
1252 GNUNET_free (cproc);
1257 * Start a controller process using the given configuration at the
1260 * @param cfg configuration to use
1261 * @param host host to run the controller on; This should be the same host if
1262 * the controller was previously started with
1263 * GNUNET_TESTBED_controller_start; NULL for localhost
1264 * @param event_mask bit mask with set of events to call 'cc' for;
1265 * or-ed values of "1LL" shifted by the
1266 * respective 'enum GNUNET_TESTBED_EventType'
1267 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1268 * @param cc controller callback to invoke on events
1269 * @param cc_cls closure for cc
1270 * @return handle to the controller
1272 struct GNUNET_TESTBED_Controller *
1273 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1274 *cfg, struct GNUNET_TESTBED_Host *host,
1275 uint64_t event_mask,
1276 GNUNET_TESTBED_ControllerCallback cc,
1279 struct GNUNET_TESTBED_Controller *controller;
1280 struct GNUNET_TESTBED_InitMessage *msg;
1281 const char *controller_hostname;
1282 unsigned long long max_parallel_operations;
1283 unsigned long long max_parallel_service_connections;
1284 unsigned long long max_parallel_topology_config_operations;
1287 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1288 "MAX_PARALLEL_OPERATIONS",
1289 &max_parallel_operations))
1295 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1296 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1297 &max_parallel_service_connections))
1303 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1304 "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1305 &max_parallel_topology_config_operations))
1310 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1311 controller->cc = cc;
1312 controller->cc_cls = cc_cls;
1313 controller->event_mask = event_mask;
1314 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1315 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1316 if (NULL == controller->client)
1318 GNUNET_TESTBED_controller_disconnect (controller);
1323 host = GNUNET_TESTBED_host_create_by_id_ (0);
1324 if (NULL == host) /* If the above host create fails */
1326 LOG (GNUNET_ERROR_TYPE_WARNING,
1327 "Treating NULL host as localhost. Multiple references to localhost "
1328 "may break when localhost freed before calling disconnect \n");
1329 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1333 controller->aux_host = GNUNET_YES;
1336 GNUNET_assert (NULL != host);
1337 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1338 controller->host = host;
1339 controller->opq_parallel_operations =
1340 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1341 max_parallel_operations);
1342 controller->opq_parallel_service_connections =
1343 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1344 max_parallel_service_connections);
1345 controller->opq_parallel_topology_config_operations=
1346 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1347 max_parallel_service_connections);
1348 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1349 if (NULL == controller_hostname)
1350 controller_hostname = "127.0.0.1";
1352 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1353 strlen (controller_hostname) + 1);
1354 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1356 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1357 strlen (controller_hostname) + 1);
1358 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1359 msg->event_mask = GNUNET_htonll (controller->event_mask);
1360 strcpy ((char *) &msg[1], controller_hostname);
1361 GNUNET_TESTBED_queue_message_ (controller,
1362 (struct GNUNET_MessageHeader *) msg);
1368 * Configure shared services at a controller. Using this function,
1369 * you can specify that certain services (such as "resolver")
1370 * should not be run for each peer but instead be shared
1371 * across N peers on the specified host. This function
1372 * must be called before any peers are created at the host.
1374 * @param controller controller to configure
1375 * @param service_name name of the service to share
1376 * @param num_peers number of peers that should share one instance
1377 * of the specified service (1 for no sharing is the default),
1378 * use 0 to disable the service
1381 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1383 const char *service_name,
1386 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1387 uint16_t service_name_size;
1390 service_name_size = strlen (service_name) + 1;
1392 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1394 msg = GNUNET_malloc (msg_size);
1395 msg->header.size = htons (msg_size);
1396 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1397 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1398 msg->num_peers = htonl (num_peers);
1399 memcpy (&msg[1], service_name, service_name_size);
1400 GNUNET_TESTBED_queue_message_ (controller,
1401 (struct GNUNET_MessageHeader *) msg);
1406 * disconnects from the controller.
1408 * @param controller handle to controller to stop
1411 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1414 struct MessageQueue *mq_entry;
1416 if (NULL != controller->th)
1417 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1418 /* Clear the message queue */
1419 while (NULL != (mq_entry = controller->mq_head))
1421 GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1423 GNUNET_free (mq_entry->msg);
1424 GNUNET_free (mq_entry);
1426 if (NULL != controller->client)
1427 GNUNET_CLIENT_disconnect (controller->client);
1428 GNUNET_CONFIGURATION_destroy (controller->cfg);
1429 if (GNUNET_YES == controller->aux_host)
1430 GNUNET_TESTBED_host_destroy (controller->host);
1431 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1432 GNUNET_TESTBED_operation_queue_destroy_
1433 (controller->opq_parallel_service_connections);
1434 GNUNET_free (controller);
1439 * Register a host with the controller
1441 * @param controller the controller handle
1442 * @param host the host to register
1443 * @param cc the completion callback to call to inform the status of
1444 * registration. After calling this callback the registration handle
1445 * will be invalid. Cannot be NULL.
1446 * @param cc_cls the closure for the cc
1447 * @return handle to the host registration which can be used to cancel the
1450 struct GNUNET_TESTBED_HostRegistrationHandle *
1451 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1452 struct GNUNET_TESTBED_Host *host,
1453 GNUNET_TESTBED_HostRegistrationCompletion cc,
1456 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1457 struct GNUNET_TESTBED_AddHostMessage *msg;
1458 const char *username;
1459 const char *hostname;
1461 uint16_t user_name_length;
1463 if (NULL != controller->rh)
1465 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1466 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1468 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1469 (NULL == hostname) ? "localhost" : hostname);
1472 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1475 GNUNET_assert (NULL != cc);
1477 rh->cc_cls = cc_cls;
1478 controller->rh = rh;
1479 username = GNUNET_TESTBED_host_get_username_ (host);
1480 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1481 user_name_length = 0;
1482 if (NULL != username)
1484 user_name_length = strlen (username) + 1;
1485 msg_size += user_name_length;
1487 /* FIXME: what happens when hostname is NULL? localhost */
1488 GNUNET_assert (NULL != hostname);
1489 msg_size += strlen (hostname) + 1;
1490 msg = GNUNET_malloc (msg_size);
1491 msg->header.size = htons (msg_size);
1492 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1493 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1494 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1495 msg->user_name_length = htons (user_name_length);
1496 if (NULL != username)
1497 memcpy (&msg[1], username, user_name_length);
1498 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1499 GNUNET_TESTBED_queue_message_ (controller,
1500 (struct GNUNET_MessageHeader *) msg);
1506 * Cancel the pending registration. Note that if the registration message is
1507 * already sent to the service the cancellation has only the effect that the
1508 * registration completion callback for the registration is never called.
1510 * @param handle the registration handle to cancel
1513 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1516 if (handle != handle->c->rh)
1521 handle->c->rh = NULL;
1522 GNUNET_free (handle);
1527 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1528 * serialized and compressed
1530 * @param master handle to the master controller who creates the association
1531 * @param delegated_host requests to which host should be delegated; cannot be NULL
1532 * @param slave_host which host is used to run the slave controller; use NULL to
1533 * make the master controller connect to the delegated host
1534 * @param sxcfg serialized and compressed configuration
1535 * @param sxcfg_size the size scfg
1536 * @param scfg_size the size of uncompressed serialized configuration
1537 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1538 * be started by the master controller; GNUNET_NO if we are just
1539 * allowed to use the slave via TCP/IP
1541 struct GNUNET_TESTBED_Operation *
1542 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1543 struct GNUNET_TESTBED_Host *delegated_host,
1544 struct GNUNET_TESTBED_Host *slave_host,
1545 const char *sxcfg, size_t sxcfg_size,
1546 size_t scfg_size, int is_subordinate)
1548 struct OperationContext *opc;
1549 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1552 GNUNET_assert (GNUNET_YES ==
1553 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1554 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1555 GNUNET_assert (GNUNET_YES ==
1556 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1557 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1558 msg = GNUNET_malloc (msg_size);
1559 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1560 msg->header.size = htons (msg_size);
1561 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1562 msg->slave_host_id =
1563 htonl (GNUNET_TESTBED_host_get_id_
1564 ((NULL != slave_host) ? slave_host : master->host));
1565 msg->config_size = htons ((uint16_t) scfg_size);
1566 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1567 memcpy (&msg[1], sxcfg, sxcfg_size);
1568 opc = GNUNET_malloc (sizeof (struct OperationContext));
1571 opc->type = OP_LINK_CONTROLLERS;
1572 opc->id = master->operation_counter++;
1573 opc->state = OPC_STATE_INIT;
1574 msg->operation_id = GNUNET_htonll (opc->id);
1576 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1577 &oprelease_link_controllers);
1578 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1585 * Compresses given configuration using zlib compress
1587 * @param config the serialized configuration
1588 * @param size the size of config
1589 * @param xconfig will be set to the compressed configuration (memory is fresly
1591 * @return the size of the xconfig
1594 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1599 xsize = compressBound ((uLong) size);
1600 *xconfig = GNUNET_malloc (xsize);
1601 GNUNET_assert (Z_OK ==
1602 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1603 (const Bytef *) config, (uLongf) size,
1610 * Create a link from slave controller to delegated controller. Whenever the
1611 * master controller is asked to start a peer at the delegated controller the
1612 * request will be routed towards slave controller (if a route exists). The
1613 * slave controller will then route it to the delegated controller. The
1614 * configuration of the slave controller is given and to be used to either
1615 * create the slave controller or to connect to an existing slave controller
1616 * process. 'is_subordinate' specifies if the given slave controller should be
1617 * started and managed by the master controller, or if the slave already has a
1618 * master and this is just a secondary master that is also allowed to use the
1621 * @param master handle to the master controller who creates the association
1622 * @param delegated_host requests to which host should be delegated
1623 * @param slave_host which host is used to run the slave controller
1624 * @param slave_cfg configuration to use for the slave controller
1625 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1626 * by the master controller; GNUNET_NO if we are just
1627 * allowed to use the slave via TCP/IP
1628 * @return the operation handle
1630 struct GNUNET_TESTBED_Operation *
1631 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1632 struct GNUNET_TESTBED_Host *delegated_host,
1633 struct GNUNET_TESTBED_Host *slave_host,
1634 const struct GNUNET_CONFIGURATION_Handle
1635 *slave_cfg, int is_subordinate)
1637 struct GNUNET_TESTBED_Operation *op;
1643 GNUNET_assert (GNUNET_YES ==
1644 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1645 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1646 GNUNET_assert (GNUNET_YES ==
1647 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1648 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1649 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1650 GNUNET_free (config);
1651 GNUNET_assert ((UINT16_MAX - sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >= cc_size); /* Configuration doesn't fit in 1 message */
1652 op = GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1653 (const char *) cconfig, cc_size,
1654 config_size, is_subordinate);
1655 GNUNET_free (cconfig);
1661 * Ask the testbed controller to write the current overlay topology to
1662 * a file. Naturally, the file will only contain a snapshot as the
1663 * topology may evolve all the time.
1665 * @param controller overlay controller to inspect
1666 * @param filename name of the file the topology should
1670 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1672 const char *filename)
1679 * Creates a helper initialization message. This function is here because we
1680 * want to use this in testing
1682 * @param cname the ip address of the controlling host
1683 * @param cfg the configuration that has to used to start the testbed service
1685 * @return the initialization message
1687 struct GNUNET_TESTBED_HelperInit *
1688 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1689 const struct GNUNET_CONFIGURATION_Handle
1692 struct GNUNET_TESTBED_HelperInit *msg;
1696 size_t xconfig_size;
1700 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1701 GNUNET_assert (NULL != config);
1703 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1704 GNUNET_free (config);
1705 cname_len = strlen (cname);
1707 xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
1708 msg = GNUNET_realloc (xconfig, msg_size);
1709 (void) memmove (((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1710 msg->header.size = htons (msg_size);
1711 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1712 msg->cname_size = htons (cname_len);
1713 msg->config_size = htons (config_size);
1714 (void) strcpy ((char *) &msg[1], cname);
1720 * Cancel a pending operation. Releases all resources
1721 * of the operation and will ensure that no event
1722 * is generated for the operation. Does NOT guarantee
1723 * that the operation will be fully undone (or that
1724 * nothing ever happened).
1726 * @param operation operation to cancel
1729 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1731 GNUNET_TESTBED_operation_done (operation);
1736 * Signal that the information from an operation has been fully
1737 * processed. This function MUST be called for each event
1738 * of type 'operation_finished' to fully remove the operation
1739 * from the operation queue. After calling this function, the
1740 * 'op_result' becomes invalid (!).
1742 * @param operation operation to signal completion for
1745 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1747 switch (operation->type)
1749 case OP_PEER_CREATE:
1750 case OP_PEER_DESTROY:
1754 case OP_OVERLAY_CONNECT:
1755 case OP_LINK_CONTROLLERS:
1756 GNUNET_TESTBED_operation_release_ (operation);
1766 * Generates configuration by parsing Peer configuration information reply message
1768 * @param msg the peer configuration information message
1769 * @return handle to the parsed configuration
1771 struct GNUNET_CONFIGURATION_Handle *
1772 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (const struct
1773 GNUNET_TESTBED_PeerConfigurationInformationMessage
1776 struct GNUNET_CONFIGURATION_Handle *cfg;
1782 config_size = (uLong) ntohs (msg->config_size);
1783 config = GNUNET_malloc (config_size);
1784 msize = ntohs (msg->header.size);
1785 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1788 uncompress ((Bytef *) config, &config_size, (const Bytef *) &msg[1],
1791 cfg = GNUNET_CONFIGURATION_create ();
1792 GNUNET_assert (GNUNET_OK ==
1793 GNUNET_CONFIGURATION_deserialize (cfg, config,
1794 (size_t) config_size,
1796 GNUNET_free (config);
1802 * Checks the integrity of the OperationFailureEventMessage and if good returns
1803 * the error message it contains.
1805 * @param msg the OperationFailureEventMessage
1806 * @return the error message
1809 GNUNET_TESTBED_parse_error_string_ (const struct
1810 GNUNET_TESTBED_OperationFailureEventMessage
1816 msize = ntohs (msg->header.size);
1817 if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
1819 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1820 emsg = (const char *) &msg[1];
1821 if ('\0' != emsg[msize])
1829 /* end of testbed_api.c */