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 * The message queue for sending messages to the controller service
83 * The message to be sent
85 struct GNUNET_MessageHeader *msg;
88 * next pointer for DLL
90 struct MessageQueue *next;
93 * prev pointer for DLL
95 struct MessageQueue *prev;
100 * Structure for a controller link
102 struct ControllerLink
105 * The next ptr for DLL
107 struct ControllerLink *next;
110 * The prev ptr for DLL
112 struct ControllerLink *prev;
115 * The host which will be referred in the peer start request. This is the
116 * host where the peer should be started
118 struct GNUNET_TESTBED_Host *delegated_host;
121 * The host which will contacted to delegate the peer start request
123 struct GNUNET_TESTBED_Host *slave_host;
126 * The configuration to be used to connect to slave host
128 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
131 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
132 * if we are just allowed to use the slave via TCP/IP
139 * handle for host registration
141 struct GNUNET_TESTBED_HostRegistrationHandle
144 * The host being registered
146 struct GNUNET_TESTBED_Host *host;
149 * The controller at which this host is being registered
151 struct GNUNET_TESTBED_Controller *c;
154 * The Registartion completion callback
156 GNUNET_TESTBED_HostRegistrationCompletion cc;
159 * The closure for above callback
166 * Context data for forwarded Operation
168 struct ForwardedOperationData
172 * The callback to call when reply is available
174 GNUNET_CLIENT_MessageHandler cc;
177 * The closure for the above callback
185 * Returns the operation context with the given id if found in the Operation
186 * context queues of the controller
188 * @param c the controller whose queues are searched
189 * @param id the id which has to be checked
190 * @return the matching operation context; NULL if no match found
192 static struct OperationContext *
193 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
195 struct OperationContext *opc;
197 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
207 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
208 * controller (testbed service)
210 * @param c the controller handler
211 * @param msg message received
212 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
216 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
217 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
219 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
228 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
230 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
231 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
235 msg_size = ntohs (msg->header.size);
236 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
238 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
239 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
240 rh->cc (rh->cc_cls, NULL);
244 /* We have an error message */
245 emsg = (char *) &msg[1];
247 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
253 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
254 ntohl (msg->host_id), emsg);
255 rh->cc (rh->cc_cls, emsg);
262 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
263 * controller (testbed service)
265 * @param c the controller handler
266 * @param msg message received
267 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
271 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
273 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
275 struct OperationContext *opc;
276 struct GNUNET_TESTBED_EventInformation event;
279 op_id = GNUNET_ntohll (msg->operation_id);
280 LOG_DEBUG ("Operation %ul successful\n", op_id);
281 if (NULL == (opc = find_opc (c, op_id)))
283 LOG_DEBUG ("Operation not found\n");
290 struct ForwardedOperationData *fo_data;
293 if (NULL != fo_data->cc)
294 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
295 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
296 GNUNET_free (fo_data);
301 case OP_PEER_DESTROY:
303 struct GNUNET_TESTBED_Peer *peer;
311 case OP_LINK_CONTROLLERS:
316 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
317 event.details.operation_finished.operation = opc->op;
318 event.details.operation_finished.op_cls = NULL;
319 event.details.operation_finished.emsg = NULL;
320 event.details.operation_finished.generic = NULL;
321 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
322 opc->state = OPC_STATE_FINISHED;
323 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
326 c->cc (c->cc_cls, &event);
333 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
334 * controller (testbed service)
336 * @param c the controller handler
337 * @param msg message received
338 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
342 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
344 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
346 struct OperationContext *opc;
347 struct PeerCreateData *data;
348 struct GNUNET_TESTBED_Peer *peer;
349 GNUNET_TESTBED_PeerCreateCallback cb;
353 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
354 ntohs (msg->header.size));
355 op_id = GNUNET_ntohll (msg->operation_id);
356 if (NULL == (opc = find_opc (c, op_id)))
358 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
361 if (OP_FORWARDED == opc->type)
363 struct ForwardedOperationData *fo_data;
366 if (NULL != fo_data->cc)
367 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
368 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
369 GNUNET_free (fo_data);
373 GNUNET_assert (OP_PEER_CREATE == opc->type);
374 GNUNET_assert (NULL != opc->data);
376 GNUNET_assert (NULL != data->peer);
378 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
379 peer->state = PS_CREATED;
382 GNUNET_free (opc->data);
383 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
384 opc->state = OPC_STATE_FINISHED;
386 cb (cls, peer, NULL);
392 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
393 * controller (testbed service)
395 * @param c the controller handler
396 * @param msg message received
397 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
401 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
402 const struct GNUNET_TESTBED_PeerEventMessage *msg)
404 struct OperationContext *opc;
405 struct GNUNET_TESTBED_Peer *peer;
406 struct PeerEventData *data;
407 GNUNET_TESTBED_PeerChurnCallback pcc;
409 struct GNUNET_TESTBED_EventInformation event;
412 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
413 ntohs (msg->header.size));
414 op_id = GNUNET_ntohll (msg->operation_id);
415 if (NULL == (opc = find_opc (c, op_id)))
417 LOG_DEBUG ("Operation not found\n");
420 if (OP_FORWARDED == opc->type)
422 struct ForwardedOperationData *fo_data;
425 if (NULL != fo_data->cc)
426 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
427 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
428 GNUNET_free (fo_data);
432 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
434 GNUNET_assert (NULL != data);
436 GNUNET_assert (NULL != peer);
437 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
440 case GNUNET_TESTBED_ET_PEER_START:
441 peer->state = PS_STARTED;
442 event.details.peer_start.host = peer->host;
443 event.details.peer_start.peer = peer;
445 case GNUNET_TESTBED_ET_PEER_STOP:
446 peer->state = PS_STOPPED;
447 event.details.peer_stop.peer = peer;
450 GNUNET_assert (0); /* We should never reach this state */
453 pcc_cls = data->pcc_cls;
455 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
456 opc->state = OPC_STATE_FINISHED;
458 ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP) &
462 c->cc (c->cc_cls, &event);
471 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
472 * controller (testbed service)
474 * @param c the controller handler
475 * @param msg message received
476 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
480 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
481 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
483 struct OperationContext *opc;
484 struct OverlayConnectData *data;
485 GNUNET_TESTBED_OperationCompletionCallback cb;
487 struct GNUNET_TESTBED_EventInformation event;
490 op_id = GNUNET_ntohll (msg->operation_id);
491 if (NULL == (opc = find_opc (c, op_id)))
493 LOG_DEBUG ("Operation not found\n");
497 GNUNET_assert (NULL != data);
498 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
499 (ntohl (msg->peer2) == data->p2->unique_id));
500 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
503 case GNUNET_TESTBED_ET_CONNECT:
504 event.details.peer_connect.peer1 = data->p1;
505 event.details.peer_connect.peer2 = data->p2;
507 case GNUNET_TESTBED_ET_DISCONNECT:
508 GNUNET_assert (0); /* FIXME: implement */
511 GNUNET_assert (0); /* Should never reach here */
515 cb_cls = data->cb_cls;
516 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
517 opc->state = OPC_STATE_FINISHED;
520 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
524 c->cc (c->cc_cls, &event);
527 cb (cb_cls, opc->op, NULL);
533 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
534 * controller (testbed service)
536 * @param c the controller handler
537 * @param msg message received
538 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
542 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
544 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
546 struct OperationContext *opc;
547 struct GNUNET_TESTBED_Peer *peer;
548 struct PeerInfoData *data;
549 struct GNUNET_TESTBED_PeerInformation *pinfo;
550 GNUNET_TESTBED_PeerInfoCallback cb;
554 op_id = GNUNET_ntohll (msg->operation_id);
555 if (NULL == (opc = find_opc (c, op_id)))
557 LOG_DEBUG ("Operation not found\n");
560 if (OP_FORWARDED == opc->type)
562 struct ForwardedOperationData *fo_data;
565 if (NULL != fo_data->cc)
566 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
567 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
568 GNUNET_free (fo_data);
573 GNUNET_assert (NULL != data);
575 GNUNET_assert (NULL != peer);
576 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
577 pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
578 pinfo->pit = data->pit;
580 cb_cls = data->cb_cls;
585 case GNUNET_TESTBED_PIT_IDENTITY:
586 pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
587 (void) memcpy (pinfo->result.id, &msg->peer_identity,
588 sizeof (struct GNUNET_PeerIdentity));
590 case GNUNET_TESTBED_PIT_CONFIGURATION:
591 pinfo->result.cfg = /* Freed in oprelease_peer_getinfo */
592 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (msg);
594 case GNUNET_TESTBED_PIT_GENERIC:
595 GNUNET_assert (0); /* never reach here */
599 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
600 opc->state = OPC_STATE_FINISHED;
602 cb (cb_cls, opc->op, pinfo, NULL);
608 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
609 * controller (testbed service)
611 * @param c the controller handler
612 * @param msg message received
613 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
617 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
618 const struct GNUNET_TESTBED_OperationFailureEventMessage
621 struct OperationContext *opc;
624 struct GNUNET_TESTBED_EventInformation event;
626 op_id = GNUNET_ntohll (msg->operation_id);
627 if (NULL == (opc = find_opc (c, op_id)))
629 LOG_DEBUG ("Operation not found\n");
632 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
633 if (OP_FORWARDED == opc->type)
635 struct ForwardedOperationData *fo_data;
638 if (NULL != fo_data->cc)
639 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
640 GNUNET_free (fo_data);
644 opc->state = OPC_STATE_FINISHED;
645 emsg = GNUNET_TESTBED_parse_error_string_ (msg);
647 emsg = "Unknown error";
648 if (OP_PEER_INFO == opc->type)
650 struct PeerInfoData *data;
652 if (NULL != data->cb)
653 data->cb (data->cb_cls, opc->op, NULL, emsg);
655 return GNUNET_YES; /* We do not call controller callback for peer info */
657 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
660 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
661 event.details.operation_finished.operation = opc->op;
662 event.details.operation_finished.op_cls = NULL;
663 event.details.operation_finished.emsg = emsg;
664 event.details.operation_finished.generic = NULL;
665 c->cc (c->cc_cls, &event);
671 struct PeerCreateData *data;
673 GNUNET_free (data->peer);
674 if (NULL != data->cb)
675 data->cb (data->cls, NULL, emsg);
682 struct PeerEventData *data;
684 if (NULL != data->pcc)
685 data->pcc (data->pcc_cls, emsg);
689 case OP_PEER_DESTROY:
693 case OP_OVERLAY_CONNECT:
695 struct OverlayConnectData *data;
697 if (NULL != data->cb)
698 data->cb (data->cb_cls, opc->op, emsg);
704 case OP_LINK_CONTROLLERS: /* No secondary callback */
714 * Handler for messages from controller (testbed service)
716 * @param cls the controller handler
717 * @param msg message received, NULL on timeout or fatal error
720 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
722 struct GNUNET_TESTBED_Controller *c = cls;
726 c->in_receive = GNUNET_NO;
727 /* FIXME: Add checks for message integrity */
730 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
734 msize = ntohs (msg->size);
735 switch (ntohs (msg->type))
737 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
738 GNUNET_assert (msize >=
739 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
741 handle_addhostconfirm (c,
742 (const struct GNUNET_TESTBED_HostConfirmedMessage
745 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
746 GNUNET_assert (msize ==
748 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
752 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
755 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
756 GNUNET_assert (msize ==
758 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
760 handle_peer_create_success (c,
762 GNUNET_TESTBED_PeerCreateSuccessEventMessage
765 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
766 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
768 handle_peer_event (c,
769 (const struct GNUNET_TESTBED_PeerEventMessage *)
773 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
774 GNUNET_assert (msize >=
776 GNUNET_TESTBED_PeerConfigurationInformationMessage));
778 handle_peer_config (c,
780 GNUNET_TESTBED_PeerConfigurationInformationMessage
783 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
784 GNUNET_assert (msize ==
785 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
787 handle_peer_conevent (c,
789 GNUNET_TESTBED_ConnectionEventMessage *) msg);
791 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT:
792 GNUNET_assert (msize >=
793 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
795 handle_op_fail_event (c,
797 GNUNET_TESTBED_OperationFailureEventMessage *)
803 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
805 c->in_receive = GNUNET_YES;
806 GNUNET_CLIENT_receive (c->client, &message_handler, c,
807 GNUNET_TIME_UNIT_FOREVER_REL);
813 * Function called to notify a client about the connection begin ready to queue
814 * more data. "buf" will be NULL and "size" zero if the connection was closed
815 * for writing in the meantime.
818 * @param size number of bytes available in buf
819 * @param buf where the callee should write the message
820 * @return number of bytes written to buf
823 transmit_ready_notify (void *cls, size_t size, void *buf)
825 struct GNUNET_TESTBED_Controller *c = cls;
826 struct MessageQueue *mq_entry;
829 mq_entry = c->mq_head;
830 GNUNET_assert (NULL != mq_entry);
831 if ((0 == size) && (NULL == buf)) /* Timeout */
833 LOG_DEBUG ("Message sending timed out -- retrying\n");
835 GNUNET_CLIENT_notify_transmit_ready (c->client,
836 ntohs (mq_entry->msg->size),
837 TIMEOUT_REL, GNUNET_YES,
838 &transmit_ready_notify, c);
841 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
842 size = ntohs (mq_entry->msg->size);
843 memcpy (buf, mq_entry->msg, size);
844 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
845 ntohs (mq_entry->msg->type), size);
846 GNUNET_free (mq_entry->msg);
847 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
848 GNUNET_free (mq_entry);
849 mq_entry = c->mq_head;
850 if (NULL != mq_entry)
852 GNUNET_CLIENT_notify_transmit_ready (c->client,
853 ntohs (mq_entry->msg->size),
854 TIMEOUT_REL, GNUNET_YES,
855 &transmit_ready_notify, c);
856 if (GNUNET_NO == c->in_receive)
858 c->in_receive = GNUNET_YES;
859 GNUNET_CLIENT_receive (c->client, &message_handler, c,
860 GNUNET_TIME_UNIT_FOREVER_REL);
867 * Queues a message in send queue for sending to the service
869 * @param controller the handle to the controller
870 * @param msg the message to queue
873 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
874 struct GNUNET_MessageHeader *msg)
876 struct MessageQueue *mq_entry;
880 type = ntohs (msg->type);
881 size = ntohs (msg->size);
882 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
883 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
884 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
886 LOG (GNUNET_ERROR_TYPE_DEBUG,
887 "Queueing message of type %u, size %u for sending\n", type,
889 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
891 if (NULL == controller->th)
893 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
894 TIMEOUT_REL, GNUNET_YES,
895 &transmit_ready_notify,
901 * Sends the given message as an operation. The given callback is called when a
902 * reply for the operation is available. Call
903 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
904 * operation context if the cc hasn't been called
906 * @param controller the controller to which the message has to be sent
907 * @param operation_id the operation id of the message
908 * @param msg the message to send
909 * @param cc the callback to call when reply is available
910 * @param cc_cls the closure for the above callback
911 * @return the operation context which can be used to cancel the forwarded
914 struct OperationContext *
915 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
916 *controller, uint64_t operation_id,
917 const struct GNUNET_MessageHeader *msg,
918 GNUNET_CLIENT_MessageHandler cc,
921 struct OperationContext *opc;
922 struct ForwardedOperationData *data;
923 struct GNUNET_MessageHeader *dup_msg;
926 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
928 data->cc_cls = cc_cls;
929 opc = GNUNET_malloc (sizeof (struct OperationContext));
931 opc->type = OP_FORWARDED;
933 opc->id = operation_id;
934 msize = ntohs (msg->size);
935 dup_msg = GNUNET_malloc (msize);
936 (void) memcpy (dup_msg, msg, msize);
937 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
938 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
945 * Function to cancel an operation created by simply forwarding an operation
948 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
951 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
953 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
954 GNUNET_free (opc->data);
960 * Handle for controller process
962 struct GNUNET_TESTBED_ControllerProc
967 struct GNUNET_HELPER_Handle *helper;
970 * The host where the helper is run
972 struct GNUNET_TESTBED_Host *host;
975 * The controller error callback
977 GNUNET_TESTBED_ControllerStatusCallback cb;
980 * The closure for the above callback
985 * The send handle for the helper
987 struct GNUNET_HELPER_SendHandle *shandle;
990 * The message corresponding to send handle
992 struct GNUNET_MessageHeader *msg;
995 * The port number for ssh; used for helpers starting ssh
1000 * The ssh destination string; used for helpers starting ssh
1005 * The configuration of the running testbed service
1007 struct GNUNET_CONFIGURATION_Handle *cfg;
1013 * Functions with this signature are called whenever a
1014 * complete message is received by the tokenizer.
1016 * Do not call GNUNET_SERVER_mst_destroy in callback
1018 * @param cls closure
1019 * @param client identification of the client
1020 * @param message the actual message
1022 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1025 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1027 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1028 const struct GNUNET_TESTBED_HelperReply *msg;
1029 const char *hostname;
1032 uLongf xconfig_size;
1034 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1035 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1036 ntohs (msg->header.size));
1037 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1038 ntohs (msg->header.type));
1039 config_size = (uLongf) ntohs (msg->config_size);
1041 (uLongf) (ntohs (msg->header.size) -
1042 sizeof (struct GNUNET_TESTBED_HelperReply));
1043 config = GNUNET_malloc (config_size);
1044 GNUNET_assert (Z_OK ==
1045 uncompress ((Bytef *) config, &config_size,
1046 (const Bytef *) &msg[1], xconfig_size));
1047 GNUNET_assert (NULL == cp->cfg);
1048 cp->cfg = GNUNET_CONFIGURATION_create ();
1049 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1050 (cp->cfg, config, config_size, GNUNET_NO));
1051 GNUNET_free (config);
1052 if ((NULL == cp->host) ||
1053 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1054 hostname = "localhost";
1055 /* Change the hostname so that we can connect to it */
1056 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1058 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1064 * Continuation function from GNUNET_HELPER_send()
1066 * @param cls closure
1067 * @param result GNUNET_OK on success,
1068 * GNUNET_NO if helper process died
1069 * GNUNET_SYSERR during GNUNET_HELPER_stop
1072 clear_msg (void *cls, int result)
1074 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1076 GNUNET_assert (NULL != cp->shandle);
1078 GNUNET_free (cp->msg);
1083 * Callback that will be called when the helper process dies. This is not called
1084 * when the helper process is stoped using GNUNET_HELPER_stop()
1086 * @param cls the closure from GNUNET_HELPER_start()
1089 helper_exp_cb (void *cls)
1091 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1092 GNUNET_TESTBED_ControllerStatusCallback cb;
1098 GNUNET_TESTBED_controller_stop (cp);
1100 cb (cb_cls, NULL, GNUNET_SYSERR);
1105 * Function to call to start a link-controllers type operation once all queues
1106 * the operation is part of declare that the operation can be activated.
1108 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1111 opstart_link_controllers (void *cls)
1113 struct OperationContext *opc = cls;
1114 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1116 GNUNET_assert (NULL != opc->data);
1119 opc->state = OPC_STATE_STARTED;
1120 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1121 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1126 * Callback which will be called when link-controllers type operation is released
1128 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1131 oprelease_link_controllers (void *cls)
1133 struct OperationContext *opc = cls;
1135 if (OPC_STATE_INIT == opc->state)
1136 GNUNET_free (opc->data);
1137 if (OPC_STATE_STARTED == opc->state)
1138 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1144 * Starts a controller process at the host. FIXME: add controller start callback
1145 * with the configuration with which the controller is started
1147 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1148 * host when starting testbed controller at host
1149 * @param host the host where the controller has to be started; NULL for
1151 * @param cfg template configuration to use for the remote controller; the
1152 * remote controller will be started with a slightly modified
1153 * configuration (port numbers, unix domain sockets and service home
1154 * values are changed as per TESTING library on the remote host)
1155 * @param cb function called when the controller is successfully started or
1156 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1157 * called if cb is called with GNUNET_SYSERR as status. Will never be
1158 * called in the same task as 'GNUNET_TESTBED_controller_start'
1159 * (synchronous errors will be signalled by returning NULL). This
1160 * parameter cannot be NULL.
1161 * @param cls closure for above callbacks
1162 * @return the controller process handle, NULL on errors
1164 struct GNUNET_TESTBED_ControllerProc *
1165 GNUNET_TESTBED_controller_start (const char *controller_ip,
1166 struct GNUNET_TESTBED_Host *host,
1167 const struct GNUNET_CONFIGURATION_Handle *cfg,
1168 GNUNET_TESTBED_ControllerStatusCallback cb,
1171 struct GNUNET_TESTBED_ControllerProc *cp;
1172 struct GNUNET_TESTBED_HelperInit *msg;
1173 static char *const binary_argv[] = {
1174 HELPER_TESTBED_BINARY, NULL
1177 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1178 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1180 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1181 &helper_mst, &helper_exp_cb, cp);
1184 char *remote_args[8];
1186 const char *username;
1187 const char *hostname;
1189 username = GNUNET_TESTBED_host_get_username_ (host);
1190 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1191 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1192 if (NULL == username)
1193 GNUNET_asprintf (&cp->dst, "%s", hostname);
1195 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1196 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1198 remote_args[argp++] = "ssh";
1199 remote_args[argp++] = "-p";
1200 remote_args[argp++] = cp->port;
1201 remote_args[argp++] = "-o";
1202 remote_args[argp++] = "BatchMode=yes";
1203 remote_args[argp++] = cp->dst;
1204 remote_args[argp++] = HELPER_TESTBED_BINARY_SSH;
1205 remote_args[argp++] = NULL;
1206 GNUNET_assert (argp == 8);
1208 GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1209 &helper_exp_cb, cp);
1211 if (NULL == cp->helper)
1213 GNUNET_free_non_null (cp->port);
1214 GNUNET_free_non_null (cp->dst);
1221 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1222 cp->msg = &msg->header;
1224 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1225 if (NULL == cp->shandle)
1228 GNUNET_TESTBED_controller_stop (cp);
1236 * Stop the controller process (also will terminate all peers and controllers
1237 * dependent on this controller). This function blocks until the testbed has
1238 * been fully terminated (!).
1240 * @param cproc the controller process handle
1243 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1245 if (NULL != cproc->shandle)
1246 GNUNET_HELPER_send_cancel (cproc->shandle);
1247 if (NULL != cproc->helper)
1248 GNUNET_HELPER_stop (cproc->helper);
1249 if (NULL != cproc->cfg)
1250 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1251 GNUNET_free_non_null (cproc->port);
1252 GNUNET_free_non_null (cproc->dst);
1253 GNUNET_free (cproc);
1258 * Start a controller process using the given configuration at the
1261 * @param cfg configuration to use
1262 * @param host host to run the controller on; This should be the same host if
1263 * the controller was previously started with
1264 * GNUNET_TESTBED_controller_start; NULL for localhost
1265 * @param event_mask bit mask with set of events to call 'cc' for;
1266 * or-ed values of "1LL" shifted by the
1267 * respective 'enum GNUNET_TESTBED_EventType'
1268 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1269 * @param cc controller callback to invoke on events
1270 * @param cc_cls closure for cc
1271 * @return handle to the controller
1273 struct GNUNET_TESTBED_Controller *
1274 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1275 *cfg, struct GNUNET_TESTBED_Host *host,
1276 uint64_t event_mask,
1277 GNUNET_TESTBED_ControllerCallback cc,
1280 struct GNUNET_TESTBED_Controller *controller;
1281 struct GNUNET_TESTBED_InitMessage *msg;
1282 const char *controller_hostname;
1283 unsigned long long max_parallel_operations;
1284 unsigned long long max_parallel_service_connections;
1285 unsigned long long max_parallel_topology_config_operations;
1288 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1289 "MAX_PARALLEL_OPERATIONS",
1290 &max_parallel_operations))
1296 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1297 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1298 &max_parallel_service_connections))
1304 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1305 "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1306 &max_parallel_topology_config_operations))
1311 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1312 controller->cc = cc;
1313 controller->cc_cls = cc_cls;
1314 controller->event_mask = event_mask;
1315 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1316 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1317 if (NULL == controller->client)
1319 GNUNET_TESTBED_controller_disconnect (controller);
1324 host = GNUNET_TESTBED_host_create_by_id_ (0);
1325 if (NULL == host) /* If the above host create fails */
1327 LOG (GNUNET_ERROR_TYPE_WARNING,
1328 "Treating NULL host as localhost. Multiple references to localhost "
1329 "may break when localhost freed before calling disconnect \n");
1330 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1334 controller->aux_host = GNUNET_YES;
1337 GNUNET_assert (NULL != host);
1338 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1339 controller->host = host;
1340 controller->opq_parallel_operations =
1341 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1342 max_parallel_operations);
1343 controller->opq_parallel_service_connections =
1344 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1345 max_parallel_service_connections);
1346 controller->opq_parallel_topology_config_operations=
1347 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1348 max_parallel_service_connections);
1349 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1350 if (NULL == controller_hostname)
1351 controller_hostname = "127.0.0.1";
1353 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1354 strlen (controller_hostname) + 1);
1355 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1357 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1358 strlen (controller_hostname) + 1);
1359 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1360 msg->event_mask = GNUNET_htonll (controller->event_mask);
1361 strcpy ((char *) &msg[1], controller_hostname);
1362 GNUNET_TESTBED_queue_message_ (controller,
1363 (struct GNUNET_MessageHeader *) msg);
1369 * Configure shared services at a controller. Using this function,
1370 * you can specify that certain services (such as "resolver")
1371 * should not be run for each peer but instead be shared
1372 * across N peers on the specified host. This function
1373 * must be called before any peers are created at the host.
1375 * @param controller controller to configure
1376 * @param service_name name of the service to share
1377 * @param num_peers number of peers that should share one instance
1378 * of the specified service (1 for no sharing is the default),
1379 * use 0 to disable the service
1382 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1384 const char *service_name,
1387 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1388 uint16_t service_name_size;
1391 service_name_size = strlen (service_name) + 1;
1393 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1395 msg = GNUNET_malloc (msg_size);
1396 msg->header.size = htons (msg_size);
1397 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1398 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1399 msg->num_peers = htonl (num_peers);
1400 memcpy (&msg[1], service_name, service_name_size);
1401 GNUNET_TESTBED_queue_message_ (controller,
1402 (struct GNUNET_MessageHeader *) msg);
1407 * disconnects from the controller.
1409 * @param controller handle to controller to stop
1412 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1415 struct MessageQueue *mq_entry;
1417 if (NULL != controller->th)
1418 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1419 /* Clear the message queue */
1420 while (NULL != (mq_entry = controller->mq_head))
1422 GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1424 GNUNET_free (mq_entry->msg);
1425 GNUNET_free (mq_entry);
1427 if (NULL != controller->client)
1428 GNUNET_CLIENT_disconnect (controller->client);
1429 GNUNET_CONFIGURATION_destroy (controller->cfg);
1430 if (GNUNET_YES == controller->aux_host)
1431 GNUNET_TESTBED_host_destroy (controller->host);
1432 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1433 GNUNET_TESTBED_operation_queue_destroy_
1434 (controller->opq_parallel_service_connections);
1435 GNUNET_TESTBED_operation_queue_destroy_
1436 (controller->opq_parallel_topology_config_operations);
1437 GNUNET_free (controller);
1442 * Register a host with the controller
1444 * @param controller the controller handle
1445 * @param host the host to register
1446 * @param cc the completion callback to call to inform the status of
1447 * registration. After calling this callback the registration handle
1448 * will be invalid. Cannot be NULL.
1449 * @param cc_cls the closure for the cc
1450 * @return handle to the host registration which can be used to cancel the
1453 struct GNUNET_TESTBED_HostRegistrationHandle *
1454 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1455 struct GNUNET_TESTBED_Host *host,
1456 GNUNET_TESTBED_HostRegistrationCompletion cc,
1459 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1460 struct GNUNET_TESTBED_AddHostMessage *msg;
1461 const char *username;
1462 const char *hostname;
1464 uint16_t user_name_length;
1466 if (NULL != controller->rh)
1468 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1469 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1471 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1472 (NULL == hostname) ? "localhost" : hostname);
1475 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1478 GNUNET_assert (NULL != cc);
1480 rh->cc_cls = cc_cls;
1481 controller->rh = rh;
1482 username = GNUNET_TESTBED_host_get_username_ (host);
1483 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1484 user_name_length = 0;
1485 if (NULL != username)
1487 user_name_length = strlen (username) + 1;
1488 msg_size += user_name_length;
1490 /* FIXME: what happens when hostname is NULL? localhost */
1491 GNUNET_assert (NULL != hostname);
1492 msg_size += strlen (hostname) + 1;
1493 msg = GNUNET_malloc (msg_size);
1494 msg->header.size = htons (msg_size);
1495 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1496 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1497 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1498 msg->user_name_length = htons (user_name_length);
1499 if (NULL != username)
1500 memcpy (&msg[1], username, user_name_length);
1501 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1502 GNUNET_TESTBED_queue_message_ (controller,
1503 (struct GNUNET_MessageHeader *) msg);
1509 * Cancel the pending registration. Note that if the registration message is
1510 * already sent to the service the cancellation has only the effect that the
1511 * registration completion callback for the registration is never called.
1513 * @param handle the registration handle to cancel
1516 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1519 if (handle != handle->c->rh)
1524 handle->c->rh = NULL;
1525 GNUNET_free (handle);
1530 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1531 * serialized and compressed
1533 * @param master handle to the master controller who creates the association
1534 * @param delegated_host requests to which host should be delegated; cannot be NULL
1535 * @param slave_host which host is used to run the slave controller; use NULL to
1536 * make the master controller connect to the delegated host
1537 * @param sxcfg serialized and compressed configuration
1538 * @param sxcfg_size the size scfg
1539 * @param scfg_size the size of uncompressed serialized configuration
1540 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1541 * be started by the master controller; GNUNET_NO if we are just
1542 * allowed to use the slave via TCP/IP
1544 struct GNUNET_TESTBED_Operation *
1545 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1546 struct GNUNET_TESTBED_Host *delegated_host,
1547 struct GNUNET_TESTBED_Host *slave_host,
1548 const char *sxcfg, size_t sxcfg_size,
1549 size_t scfg_size, int is_subordinate)
1551 struct OperationContext *opc;
1552 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1555 GNUNET_assert (GNUNET_YES ==
1556 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1557 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1558 GNUNET_assert (GNUNET_YES ==
1559 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1560 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1561 msg = GNUNET_malloc (msg_size);
1562 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1563 msg->header.size = htons (msg_size);
1564 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1565 msg->slave_host_id =
1566 htonl (GNUNET_TESTBED_host_get_id_
1567 ((NULL != slave_host) ? slave_host : master->host));
1568 msg->config_size = htons ((uint16_t) scfg_size);
1569 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1570 memcpy (&msg[1], sxcfg, sxcfg_size);
1571 opc = GNUNET_malloc (sizeof (struct OperationContext));
1574 opc->type = OP_LINK_CONTROLLERS;
1575 opc->id = master->operation_counter++;
1576 opc->state = OPC_STATE_INIT;
1577 msg->operation_id = GNUNET_htonll (opc->id);
1579 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1580 &oprelease_link_controllers);
1581 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1588 * Compresses given configuration using zlib compress
1590 * @param config the serialized configuration
1591 * @param size the size of config
1592 * @param xconfig will be set to the compressed configuration (memory is fresly
1594 * @return the size of the xconfig
1597 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1602 xsize = compressBound ((uLong) size);
1603 *xconfig = GNUNET_malloc (xsize);
1604 GNUNET_assert (Z_OK ==
1605 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1606 (const Bytef *) config, (uLongf) size,
1613 * Create a link from slave controller to delegated controller. Whenever the
1614 * master controller is asked to start a peer at the delegated controller the
1615 * request will be routed towards slave controller (if a route exists). The
1616 * slave controller will then route it to the delegated controller. The
1617 * configuration of the slave controller is given and to be used to either
1618 * create the slave controller or to connect to an existing slave controller
1619 * process. 'is_subordinate' specifies if the given slave controller should be
1620 * started and managed by the master controller, or if the slave already has a
1621 * master and this is just a secondary master that is also allowed to use the
1624 * @param master handle to the master controller who creates the association
1625 * @param delegated_host requests to which host should be delegated
1626 * @param slave_host which host is used to run the slave controller
1627 * @param slave_cfg configuration to use for the slave controller
1628 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1629 * by the master controller; GNUNET_NO if we are just
1630 * allowed to use the slave via TCP/IP
1631 * @return the operation handle
1633 struct GNUNET_TESTBED_Operation *
1634 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1635 struct GNUNET_TESTBED_Host *delegated_host,
1636 struct GNUNET_TESTBED_Host *slave_host,
1637 const struct GNUNET_CONFIGURATION_Handle
1638 *slave_cfg, int is_subordinate)
1640 struct GNUNET_TESTBED_Operation *op;
1646 GNUNET_assert (GNUNET_YES ==
1647 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1648 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1649 GNUNET_assert (GNUNET_YES ==
1650 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1651 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1652 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1653 GNUNET_free (config);
1654 GNUNET_assert ((UINT16_MAX - sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >= cc_size); /* Configuration doesn't fit in 1 message */
1655 op = GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1656 (const char *) cconfig, cc_size,
1657 config_size, is_subordinate);
1658 GNUNET_free (cconfig);
1664 * Function to acquire the configuration of a running slave controller. The
1665 * completion of the operation is signalled through the controller_cb from
1666 * GNUNET_TESTBED_controller_connect(). If the operation is successful the
1667 * handle to the configuration is available in the generic pointer of
1668 * operation_finished field of struct GNUNET_TESTBED_EventInformation.
1670 * @param op_cls the closure for the operation
1671 * @param master the handle to master controller
1672 * @param slave_host the host where the slave controller is running
1673 * @return the operation handle
1675 struct GNUNET_TESTBED_Operation *
1676 GNUNET_TESTBED_get_slave_config (void *op_cls,
1677 struct GNUNET_TESTBED_Controller *master,
1678 struct GNUNET_TESTBED_Host *slave_host)
1686 * Ask the testbed controller to write the current overlay topology to
1687 * a file. Naturally, the file will only contain a snapshot as the
1688 * topology may evolve all the time.
1690 * @param controller overlay controller to inspect
1691 * @param filename name of the file the topology should
1695 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1697 const char *filename)
1704 * Creates a helper initialization message. This function is here because we
1705 * want to use this in testing
1707 * @param cname the ip address of the controlling host
1708 * @param cfg the configuration that has to used to start the testbed service
1710 * @return the initialization message
1712 struct GNUNET_TESTBED_HelperInit *
1713 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1714 const struct GNUNET_CONFIGURATION_Handle
1717 struct GNUNET_TESTBED_HelperInit *msg;
1721 size_t xconfig_size;
1725 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1726 GNUNET_assert (NULL != config);
1728 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1729 GNUNET_free (config);
1730 cname_len = strlen (cname);
1732 xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
1733 msg = GNUNET_realloc (xconfig, msg_size);
1734 (void) memmove (((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1735 msg->header.size = htons (msg_size);
1736 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1737 msg->cname_size = htons (cname_len);
1738 msg->config_size = htons (config_size);
1739 (void) strcpy ((char *) &msg[1], cname);
1745 * Cancel a pending operation. Releases all resources
1746 * of the operation and will ensure that no event
1747 * is generated for the operation. Does NOT guarantee
1748 * that the operation will be fully undone (or that
1749 * nothing ever happened).
1751 * @param operation operation to cancel
1754 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1756 GNUNET_TESTBED_operation_done (operation);
1761 * Signal that the information from an operation has been fully
1762 * processed. This function MUST be called for each event
1763 * of type 'operation_finished' to fully remove the operation
1764 * from the operation queue. After calling this function, the
1765 * 'op_result' becomes invalid (!).
1767 * @param operation operation to signal completion for
1770 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1772 switch (operation->type)
1774 case OP_PEER_CREATE:
1775 case OP_PEER_DESTROY:
1779 case OP_OVERLAY_CONNECT:
1780 case OP_LINK_CONTROLLERS:
1781 GNUNET_TESTBED_operation_release_ (operation);
1791 * Generates configuration by parsing Peer configuration information reply message
1793 * @param msg the peer configuration information message
1794 * @return handle to the parsed configuration
1796 struct GNUNET_CONFIGURATION_Handle *
1797 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (const struct
1798 GNUNET_TESTBED_PeerConfigurationInformationMessage
1801 struct GNUNET_CONFIGURATION_Handle *cfg;
1807 config_size = (uLong) ntohs (msg->config_size);
1808 config = GNUNET_malloc (config_size);
1809 msize = ntohs (msg->header.size);
1810 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1813 uncompress ((Bytef *) config, &config_size, (const Bytef *) &msg[1],
1816 cfg = GNUNET_CONFIGURATION_create ();
1817 GNUNET_assert (GNUNET_OK ==
1818 GNUNET_CONFIGURATION_deserialize (cfg, config,
1819 (size_t) config_size,
1821 GNUNET_free (config);
1827 * Checks the integrity of the OperationFailureEventMessage and if good returns
1828 * the error message it contains.
1830 * @param msg the OperationFailureEventMessage
1831 * @return the error message
1834 GNUNET_TESTBED_parse_error_string_ (const struct
1835 GNUNET_TESTBED_OperationFailureEventMessage
1841 msize = ntohs (msg->header.size);
1842 if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
1844 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1845 emsg = (const char *) &msg[1];
1846 if ('\0' != emsg[msize])
1854 /* end of testbed_api.c */