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");
286 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
287 event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
289 event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
294 struct ForwardedOperationData *fo_data;
297 if (NULL != fo_data->cc)
298 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
299 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
300 GNUNET_free (fo_data);
305 case OP_PEER_DESTROY:
307 struct GNUNET_TESTBED_Peer *peer;
315 case OP_LINK_CONTROLLERS:
322 event->details.operation_finished.operation = opc->op;
323 event->details.operation_finished.op_cls = NULL;
324 event->details.operation_finished.emsg = NULL;
325 event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
326 event->details.operation_finished.op_result.generic = NULL;
328 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
329 opc->state = OPC_STATE_FINISHED;
333 c->cc (c->cc_cls, event);
341 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
342 * controller (testbed service)
344 * @param c the controller handler
345 * @param msg message received
346 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
350 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
352 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
354 struct OperationContext *opc;
355 struct PeerCreateData *data;
356 struct GNUNET_TESTBED_Peer *peer;
357 GNUNET_TESTBED_PeerCreateCallback cb;
361 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
362 ntohs (msg->header.size));
363 op_id = GNUNET_ntohll (msg->operation_id);
364 if (NULL == (opc = find_opc (c, op_id)))
366 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
369 if (OP_FORWARDED == opc->type)
371 struct ForwardedOperationData *fo_data;
374 if (NULL != fo_data->cc)
375 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
376 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
377 GNUNET_free (fo_data);
381 GNUNET_assert (OP_PEER_CREATE == opc->type);
382 GNUNET_assert (NULL != opc->data);
384 GNUNET_assert (NULL != data->peer);
386 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
387 peer->state = PS_CREATED;
390 GNUNET_free (opc->data);
391 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
392 opc->state = OPC_STATE_FINISHED;
394 cb (cls, peer, NULL);
400 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
401 * controller (testbed service)
403 * @param c the controller handler
404 * @param msg message received
405 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
409 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
410 const struct GNUNET_TESTBED_PeerEventMessage *msg)
412 struct OperationContext *opc;
413 struct GNUNET_TESTBED_Peer *peer;
414 struct GNUNET_TESTBED_EventInformation event;
417 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
418 ntohs (msg->header.size));
419 op_id = GNUNET_ntohll (msg->operation_id);
420 if (NULL == (opc = find_opc (c, op_id)))
422 LOG_DEBUG ("Operation not found\n");
425 if (OP_FORWARDED == opc->type)
427 struct ForwardedOperationData *fo_data;
430 if (NULL != fo_data->cc)
431 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
432 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
433 GNUNET_free (fo_data);
437 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
439 GNUNET_assert (NULL != peer);
440 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
443 case GNUNET_TESTBED_ET_PEER_START:
444 peer->state = PS_STARTED;
445 event.details.peer_start.host = peer->host;
446 event.details.peer_start.peer = peer;
448 case GNUNET_TESTBED_ET_PEER_STOP:
449 peer->state = PS_STOPPED;
450 event.details.peer_stop.peer = peer;
453 GNUNET_assert (0); /* We should never reach this state */
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);
469 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
470 * controller (testbed service)
472 * @param c the controller handler
473 * @param msg message received
474 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
478 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
479 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
481 struct OperationContext *opc;
482 struct OverlayConnectData *data;
483 struct GNUNET_TESTBED_EventInformation event;
486 op_id = GNUNET_ntohll (msg->operation_id);
487 if (NULL == (opc = find_opc (c, op_id)))
489 LOG_DEBUG ("Operation not found\n");
493 GNUNET_assert (NULL != data);
494 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
495 (ntohl (msg->peer2) == data->p2->unique_id));
496 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
499 case GNUNET_TESTBED_ET_CONNECT:
500 event.details.peer_connect.peer1 = data->p1;
501 event.details.peer_connect.peer2 = data->p2;
503 case GNUNET_TESTBED_ET_DISCONNECT:
504 GNUNET_assert (0); /* FIXME: implement */
507 GNUNET_assert (0); /* Should never reach here */
510 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
511 opc->state = OPC_STATE_FINISHED;
514 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
518 c->cc (c->cc_cls, &event);
525 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
526 * controller (testbed service)
528 * @param c the controller handler
529 * @param msg message received
530 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
534 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
536 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
538 struct OperationContext *opc;
539 struct GNUNET_TESTBED_Peer *peer;
540 struct PeerInfoData *data;
541 struct PeerInfoData2 *response_data;
542 struct GNUNET_TESTBED_EventInformation info;
545 op_id = GNUNET_ntohll (msg->operation_id);
546 if (NULL == (opc = find_opc (c, op_id)))
548 LOG_DEBUG ("Operation not found\n");
551 if (OP_FORWARDED == opc->type)
553 struct ForwardedOperationData *fo_data;
556 if (NULL != fo_data->cc)
557 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
558 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
559 GNUNET_free (fo_data);
564 GNUNET_assert (NULL != data);
566 GNUNET_assert (NULL != peer);
567 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
568 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
570 LOG_DEBUG ("Skipping operation callback as flag not set\n");
573 response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
574 response_data->pit = data->pit;
577 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
578 info.details.operation_finished.operation = opc->op;
579 info.details.operation_finished.op_cls = NULL;
580 info.details.operation_finished.emsg = NULL;
581 info.details.operation_finished.pit = response_data->pit;
582 switch (response_data->pit)
584 case GNUNET_TESTBED_PIT_IDENTITY:
586 struct GNUNET_PeerIdentity *peer_identity;
588 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
589 (void) memcpy (peer_identity, &msg->peer_identity,
590 sizeof (struct GNUNET_PeerIdentity));
591 response_data->details.peer_identity = peer_identity;
592 info.details.operation_finished.op_result.pid = peer_identity;
595 case GNUNET_TESTBED_PIT_CONFIGURATION:
596 response_data->details.cfg = /* Freed in oprelease_peer_getinfo */
597 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (msg);
598 info.details.operation_finished.op_result.cfg = response_data->details.cfg;
600 case GNUNET_TESTBED_PIT_GENERIC:
601 GNUNET_assert (0); /* never reach here */
604 opc->data = response_data;
605 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
606 opc->state = OPC_STATE_FINISHED;
608 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
612 c->cc (c->cc_cls, &info);
619 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT message from
620 * controller (testbed service)
622 * @param c the controller handler
623 * @param msg message received
624 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
628 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
629 const struct GNUNET_TESTBED_OperationFailureEventMessage
632 struct OperationContext *opc;
635 struct GNUNET_TESTBED_EventInformation event;
638 op_id = GNUNET_ntohll (msg->operation_id);
639 if (NULL == (opc = find_opc (c, op_id)))
641 LOG_DEBUG ("Operation not found\n");
644 if (OP_FORWARDED == opc->type)
646 struct ForwardedOperationData *fo_data;
649 if (NULL != fo_data->cc)
650 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
651 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
652 GNUNET_free (fo_data);
656 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
657 opc->state = OPC_STATE_FINISHED;
660 /* FIXME: Cleanup the data pointer depending on the type of opc */
664 msize = ntohs (msg->header.size);
666 if (msize > sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage))
668 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
669 emsg = (char *) &msg[1];
670 GNUNET_assert ('\0' == emsg[msize - 1]);
672 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
675 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
676 event.details.operation_finished.operation = opc->op;
677 event.details.operation_finished.op_cls = NULL;
678 event.details.operation_finished.emsg = emsg;
679 event.details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
680 event.details.operation_finished.op_result.generic = NULL;
681 c->cc (c->cc_cls, &event);
688 * Handler for messages from controller (testbed service)
690 * @param cls the controller handler
691 * @param msg message received, NULL on timeout or fatal error
694 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
696 struct GNUNET_TESTBED_Controller *c = cls;
700 c->in_receive = GNUNET_NO;
701 /* FIXME: Add checks for message integrity */
704 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
708 msize = ntohs (msg->size);
709 switch (ntohs (msg->type))
711 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
712 GNUNET_assert (msize >=
713 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
715 handle_addhostconfirm (c,
716 (const struct GNUNET_TESTBED_HostConfirmedMessage
719 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
720 GNUNET_assert (msize ==
722 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
726 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
729 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
730 GNUNET_assert (msize ==
732 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
734 handle_peer_create_success (c,
736 GNUNET_TESTBED_PeerCreateSuccessEventMessage
739 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
740 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
742 handle_peer_event (c,
743 (const struct GNUNET_TESTBED_PeerEventMessage *)
747 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
748 GNUNET_assert (msize >=
750 GNUNET_TESTBED_PeerConfigurationInformationMessage));
752 handle_peer_config (c,
754 GNUNET_TESTBED_PeerConfigurationInformationMessage
757 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
758 GNUNET_assert (msize ==
759 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
761 handle_peer_conevent (c,
763 GNUNET_TESTBED_ConnectionEventMessage *) msg);
765 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT:
766 GNUNET_assert (msize >=
767 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
769 handle_op_fail_event (c,
771 GNUNET_TESTBED_OperationFailureEventMessage *)
777 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
779 c->in_receive = GNUNET_YES;
780 GNUNET_CLIENT_receive (c->client, &message_handler, c,
781 GNUNET_TIME_UNIT_FOREVER_REL);
787 * Function called to notify a client about the connection begin ready to queue
788 * more data. "buf" will be NULL and "size" zero if the connection was closed
789 * for writing in the meantime.
792 * @param size number of bytes available in buf
793 * @param buf where the callee should write the message
794 * @return number of bytes written to buf
797 transmit_ready_notify (void *cls, size_t size, void *buf)
799 struct GNUNET_TESTBED_Controller *c = cls;
800 struct MessageQueue *mq_entry;
803 mq_entry = c->mq_head;
804 GNUNET_assert (NULL != mq_entry);
805 if ((0 == size) && (NULL == buf)) /* Timeout */
807 LOG_DEBUG ("Message sending timed out -- retrying\n");
809 GNUNET_CLIENT_notify_transmit_ready (c->client,
810 ntohs (mq_entry->msg->size),
811 TIMEOUT_REL, GNUNET_YES,
812 &transmit_ready_notify, c);
815 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
816 size = ntohs (mq_entry->msg->size);
817 memcpy (buf, mq_entry->msg, size);
818 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
819 ntohs (mq_entry->msg->type), size);
820 GNUNET_free (mq_entry->msg);
821 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
822 GNUNET_free (mq_entry);
823 mq_entry = c->mq_head;
824 if (NULL != mq_entry)
826 GNUNET_CLIENT_notify_transmit_ready (c->client,
827 ntohs (mq_entry->msg->size),
828 TIMEOUT_REL, GNUNET_YES,
829 &transmit_ready_notify, c);
830 if (GNUNET_NO == c->in_receive)
832 c->in_receive = GNUNET_YES;
833 GNUNET_CLIENT_receive (c->client, &message_handler, c,
834 GNUNET_TIME_UNIT_FOREVER_REL);
841 * Queues a message in send queue for sending to the service
843 * @param controller the handle to the controller
844 * @param msg the message to queue
847 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
848 struct GNUNET_MessageHeader *msg)
850 struct MessageQueue *mq_entry;
854 type = ntohs (msg->type);
855 size = ntohs (msg->size);
856 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
857 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
858 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
860 LOG (GNUNET_ERROR_TYPE_DEBUG,
861 "Queueing message of type %u, size %u for sending\n", type,
863 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
865 if (NULL == controller->th)
867 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
868 TIMEOUT_REL, GNUNET_YES,
869 &transmit_ready_notify,
875 * Sends the given message as an operation. The given callback is called when a
876 * reply for the operation is available. Call
877 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
878 * operation context if the cc hasn't been called
880 * @param controller the controller to which the message has to be sent
881 * @param operation_id the operation id of the message
882 * @param msg the message to send
883 * @param cc the callback to call when reply is available
884 * @param cc_cls the closure for the above callback
885 * @return the operation context which can be used to cancel the forwarded
888 struct OperationContext *
889 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
890 *controller, uint64_t operation_id,
891 const struct GNUNET_MessageHeader *msg,
892 GNUNET_CLIENT_MessageHandler cc,
895 struct OperationContext *opc;
896 struct ForwardedOperationData *data;
897 struct GNUNET_MessageHeader *dup_msg;
900 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
902 data->cc_cls = cc_cls;
903 opc = GNUNET_malloc (sizeof (struct OperationContext));
905 opc->type = OP_FORWARDED;
907 opc->id = operation_id;
908 msize = ntohs (msg->size);
909 dup_msg = GNUNET_malloc (msize);
910 (void) memcpy (dup_msg, msg, msize);
911 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
912 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
919 * Function to cancel an operation created by simply forwarding an operation
922 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
925 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
927 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
928 GNUNET_free (opc->data);
934 * Handle for controller process
936 struct GNUNET_TESTBED_ControllerProc
941 struct GNUNET_HELPER_Handle *helper;
944 * The host where the helper is run
946 struct GNUNET_TESTBED_Host *host;
949 * The controller error callback
951 GNUNET_TESTBED_ControllerStatusCallback cb;
954 * The closure for the above callback
959 * The send handle for the helper
961 struct GNUNET_HELPER_SendHandle *shandle;
964 * The message corresponding to send handle
966 struct GNUNET_MessageHeader *msg;
969 * The port number for ssh; used for helpers starting ssh
974 * The ssh destination string; used for helpers starting ssh
979 * The configuration of the running testbed service
981 struct GNUNET_CONFIGURATION_Handle *cfg;
987 * Functions with this signature are called whenever a
988 * complete message is received by the tokenizer.
990 * Do not call GNUNET_SERVER_mst_destroy in callback
993 * @param client identification of the client
994 * @param message the actual message
996 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
999 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1001 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1002 const struct GNUNET_TESTBED_HelperReply *msg;
1003 const char *hostname;
1006 uLongf xconfig_size;
1008 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1009 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1010 ntohs (msg->header.size));
1011 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1012 ntohs (msg->header.type));
1013 config_size = (uLongf) ntohs (msg->config_size);
1015 (uLongf) (ntohs (msg->header.size) -
1016 sizeof (struct GNUNET_TESTBED_HelperReply));
1017 config = GNUNET_malloc (config_size);
1018 GNUNET_assert (Z_OK ==
1019 uncompress ((Bytef *) config, &config_size,
1020 (const Bytef *) &msg[1], xconfig_size));
1021 GNUNET_assert (NULL == cp->cfg);
1022 cp->cfg = GNUNET_CONFIGURATION_create ();
1023 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1024 (cp->cfg, config, config_size, GNUNET_NO));
1025 GNUNET_free (config);
1026 if ((NULL == cp->host) ||
1027 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1028 hostname = "localhost";
1029 /* Change the hostname so that we can connect to it */
1030 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1032 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1038 * Continuation function from GNUNET_HELPER_send()
1040 * @param cls closure
1041 * @param result GNUNET_OK on success,
1042 * GNUNET_NO if helper process died
1043 * GNUNET_SYSERR during GNUNET_HELPER_stop
1046 clear_msg (void *cls, int result)
1048 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1050 GNUNET_assert (NULL != cp->shandle);
1052 GNUNET_free (cp->msg);
1057 * Callback that will be called when the helper process dies. This is not called
1058 * when the helper process is stoped using GNUNET_HELPER_stop()
1060 * @param cls the closure from GNUNET_HELPER_start()
1063 helper_exp_cb (void *cls)
1065 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1066 GNUNET_TESTBED_ControllerStatusCallback cb;
1072 GNUNET_TESTBED_controller_stop (cp);
1074 cb (cb_cls, NULL, GNUNET_SYSERR);
1079 * Function to call to start a link-controllers type operation once all queues
1080 * the operation is part of declare that the operation can be activated.
1082 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1085 opstart_link_controllers (void *cls)
1087 struct OperationContext *opc = cls;
1088 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1090 GNUNET_assert (NULL != opc->data);
1093 opc->state = OPC_STATE_STARTED;
1094 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1095 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1100 * Callback which will be called when link-controllers type operation is released
1102 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1105 oprelease_link_controllers (void *cls)
1107 struct OperationContext *opc = cls;
1109 if (OPC_STATE_INIT == opc->state)
1110 GNUNET_free (opc->data);
1111 if (OPC_STATE_STARTED == opc->state)
1112 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1118 * Starts a controller process at the host. FIXME: add controller start callback
1119 * with the configuration with which the controller is started
1121 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1122 * host when starting testbed controller at host
1123 * @param host the host where the controller has to be started; NULL for
1125 * @param cfg template configuration to use for the remote controller; the
1126 * remote controller will be started with a slightly modified
1127 * configuration (port numbers, unix domain sockets and service home
1128 * values are changed as per TESTING library on the remote host)
1129 * @param cb function called when the controller is successfully started or
1130 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1131 * called if cb is called with GNUNET_SYSERR as status. Will never be
1132 * called in the same task as 'GNUNET_TESTBED_controller_start'
1133 * (synchronous errors will be signalled by returning NULL). This
1134 * parameter cannot be NULL.
1135 * @param cls closure for above callbacks
1136 * @return the controller process handle, NULL on errors
1138 struct GNUNET_TESTBED_ControllerProc *
1139 GNUNET_TESTBED_controller_start (const char *controller_ip,
1140 struct GNUNET_TESTBED_Host *host,
1141 const struct GNUNET_CONFIGURATION_Handle *cfg,
1142 GNUNET_TESTBED_ControllerStatusCallback cb,
1145 struct GNUNET_TESTBED_ControllerProc *cp;
1146 struct GNUNET_TESTBED_HelperInit *msg;
1148 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1149 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1151 char *const binary_argv[] = {
1152 HELPER_TESTBED_BINARY, NULL
1156 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1157 &helper_mst, &helper_exp_cb, cp);
1161 char *remote_args[8];
1163 const char *username;
1164 const char *hostname;
1166 username = GNUNET_TESTBED_host_get_username_ (host);
1167 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1168 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1169 if (NULL == username)
1170 GNUNET_asprintf (&cp->dst, "%s", hostname);
1172 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1173 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1175 remote_args[argp++] = "ssh";
1176 remote_args[argp++] = "-p";
1177 remote_args[argp++] = cp->port;
1178 remote_args[argp++] = "-o";
1179 remote_args[argp++] = "BatchMode=yes";
1180 remote_args[argp++] = cp->dst;
1181 remote_args[argp++] = HELPER_TESTBED_BINARY;
1182 remote_args[argp++] = NULL;
1183 GNUNET_assert (argp == 8);
1185 GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1186 &helper_exp_cb, cp);
1188 if (NULL == cp->helper)
1190 GNUNET_free_non_null (cp->port);
1191 GNUNET_free_non_null (cp->dst);
1198 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1199 cp->msg = &msg->header;
1201 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1202 if (NULL == cp->shandle)
1205 GNUNET_TESTBED_controller_stop (cp);
1213 * Stop the controller process (also will terminate all peers and controllers
1214 * dependent on this controller). This function blocks until the testbed has
1215 * been fully terminated (!).
1217 * @param cproc the controller process handle
1220 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1222 if (NULL != cproc->shandle)
1223 GNUNET_HELPER_send_cancel (cproc->shandle);
1224 if (NULL != cproc->helper)
1225 GNUNET_HELPER_stop (cproc->helper);
1226 if (NULL != cproc->cfg)
1227 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1228 GNUNET_free_non_null (cproc->port);
1229 GNUNET_free_non_null (cproc->dst);
1230 GNUNET_free (cproc);
1235 * Start a controller process using the given configuration at the
1238 * @param cfg configuration to use
1239 * @param host host to run the controller on; This should be the same host if
1240 * the controller was previously started with
1241 * GNUNET_TESTBED_controller_start; NULL for localhost
1242 * @param event_mask bit mask with set of events to call 'cc' for;
1243 * or-ed values of "1LL" shifted by the
1244 * respective 'enum GNUNET_TESTBED_EventType'
1245 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1246 * @param cc controller callback to invoke on events
1247 * @param cc_cls closure for cc
1248 * @return handle to the controller
1250 struct GNUNET_TESTBED_Controller *
1251 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1252 *cfg, struct GNUNET_TESTBED_Host *host,
1253 uint64_t event_mask,
1254 GNUNET_TESTBED_ControllerCallback cc,
1257 struct GNUNET_TESTBED_Controller *controller;
1258 struct GNUNET_TESTBED_InitMessage *msg;
1259 const char *controller_hostname;
1260 unsigned long long max_parallel_operations;
1261 unsigned long long max_parallel_service_connections;
1264 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1265 "MAX_PARALLEL_OPERATIONS",
1266 &max_parallel_operations))
1272 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1273 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1274 &max_parallel_service_connections))
1279 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1280 controller->cc = cc;
1281 controller->cc_cls = cc_cls;
1282 controller->event_mask = event_mask;
1283 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1284 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1285 if (NULL == controller->client)
1287 GNUNET_TESTBED_controller_disconnect (controller);
1292 host = GNUNET_TESTBED_host_create_by_id_ (0);
1293 if (NULL == host) /* If the above host create fails */
1295 LOG (GNUNET_ERROR_TYPE_WARNING,
1296 "Treating NULL host as localhost. Multiple references to localhost "
1297 "may break when localhost freed before calling disconnect \n");
1298 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1302 controller->aux_host = GNUNET_YES;
1305 GNUNET_assert (NULL != host);
1306 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1307 controller->host = host;
1308 controller->opq_parallel_operations =
1309 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1310 max_parallel_operations);
1311 controller->opq_parallel_service_connections =
1312 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1313 max_parallel_service_connections);
1314 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1315 if (NULL == controller_hostname)
1316 controller_hostname = "127.0.0.1";
1318 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1319 strlen (controller_hostname) + 1);
1320 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1322 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1323 strlen (controller_hostname) + 1);
1324 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1325 msg->event_mask = GNUNET_htonll (controller->event_mask);
1326 strcpy ((char *) &msg[1], controller_hostname);
1327 GNUNET_TESTBED_queue_message_ (controller,
1328 (struct GNUNET_MessageHeader *) msg);
1334 * Configure shared services at a controller. Using this function,
1335 * you can specify that certain services (such as "resolver")
1336 * should not be run for each peer but instead be shared
1337 * across N peers on the specified host. This function
1338 * must be called before any peers are created at the host.
1340 * @param controller controller to configure
1341 * @param service_name name of the service to share
1342 * @param num_peers number of peers that should share one instance
1343 * of the specified service (1 for no sharing is the default),
1344 * use 0 to disable the service
1347 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1349 const char *service_name,
1352 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1353 uint16_t service_name_size;
1356 service_name_size = strlen (service_name) + 1;
1358 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1360 msg = GNUNET_malloc (msg_size);
1361 msg->header.size = htons (msg_size);
1362 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1363 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1364 msg->num_peers = htonl (num_peers);
1365 memcpy (&msg[1], service_name, service_name_size);
1366 GNUNET_TESTBED_queue_message_ (controller,
1367 (struct GNUNET_MessageHeader *) msg);
1372 * disconnects from the controller.
1374 * @param controller handle to controller to stop
1377 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1380 struct MessageQueue *mq_entry;
1382 if (NULL != controller->th)
1383 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1384 /* Clear the message queue */
1385 while (NULL != (mq_entry = controller->mq_head))
1387 GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1389 GNUNET_free (mq_entry->msg);
1390 GNUNET_free (mq_entry);
1392 if (NULL != controller->client)
1393 GNUNET_CLIENT_disconnect (controller->client);
1394 GNUNET_CONFIGURATION_destroy (controller->cfg);
1395 if (GNUNET_YES == controller->aux_host)
1396 GNUNET_TESTBED_host_destroy (controller->host);
1397 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1398 GNUNET_TESTBED_operation_queue_destroy_
1399 (controller->opq_parallel_service_connections);
1400 GNUNET_free (controller);
1405 * Register a host with the controller
1407 * @param controller the controller handle
1408 * @param host the host to register
1409 * @param cc the completion callback to call to inform the status of
1410 * registration. After calling this callback the registration handle
1411 * will be invalid. Cannot be NULL.
1412 * @param cc_cls the closure for the cc
1413 * @return handle to the host registration which can be used to cancel the
1416 struct GNUNET_TESTBED_HostRegistrationHandle *
1417 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1418 struct GNUNET_TESTBED_Host *host,
1419 GNUNET_TESTBED_HostRegistrationCompletion cc,
1422 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1423 struct GNUNET_TESTBED_AddHostMessage *msg;
1424 const char *username;
1425 const char *hostname;
1427 uint16_t user_name_length;
1429 if (NULL != controller->rh)
1431 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1432 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1434 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1435 (NULL == hostname) ? "localhost" : hostname);
1438 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1441 GNUNET_assert (NULL != cc);
1443 rh->cc_cls = cc_cls;
1444 controller->rh = rh;
1445 username = GNUNET_TESTBED_host_get_username_ (host);
1446 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1447 user_name_length = 0;
1448 if (NULL != username)
1450 user_name_length = strlen (username) + 1;
1451 msg_size += user_name_length;
1453 /* FIXME: what happens when hostname is NULL? localhost */
1454 GNUNET_assert (NULL != hostname);
1455 msg_size += strlen (hostname) + 1;
1456 msg = GNUNET_malloc (msg_size);
1457 msg->header.size = htons (msg_size);
1458 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1459 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1460 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1461 msg->user_name_length = htons (user_name_length);
1462 if (NULL != username)
1463 memcpy (&msg[1], username, user_name_length);
1464 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1465 GNUNET_TESTBED_queue_message_ (controller,
1466 (struct GNUNET_MessageHeader *) msg);
1472 * Cancel the pending registration. Note that if the registration message is
1473 * already sent to the service the cancellation has only the effect that the
1474 * registration completion callback for the registration is never called.
1476 * @param handle the registration handle to cancel
1479 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1482 if (handle != handle->c->rh)
1487 handle->c->rh = NULL;
1488 GNUNET_free (handle);
1493 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1494 * serialized and compressed
1496 * @param master handle to the master controller who creates the association
1497 * @param delegated_host requests to which host should be delegated; cannot be NULL
1498 * @param slave_host which host is used to run the slave controller; use NULL to
1499 * make the master controller connect to the delegated host
1500 * @param sxcfg serialized and compressed configuration
1501 * @param sxcfg_size the size scfg
1502 * @param scfg_size the size of uncompressed serialized configuration
1503 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1504 * be started by the master controller; GNUNET_NO if we are just
1505 * allowed to use the slave via TCP/IP
1507 struct GNUNET_TESTBED_Operation *
1508 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1509 struct GNUNET_TESTBED_Host *delegated_host,
1510 struct GNUNET_TESTBED_Host *slave_host,
1511 const char *sxcfg, size_t sxcfg_size,
1512 size_t scfg_size, int is_subordinate)
1514 struct OperationContext *opc;
1515 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1518 GNUNET_assert (GNUNET_YES ==
1519 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1520 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1521 GNUNET_assert (GNUNET_YES ==
1522 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1523 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1524 msg = GNUNET_malloc (msg_size);
1525 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1526 msg->header.size = htons (msg_size);
1527 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1528 msg->slave_host_id =
1529 htonl (GNUNET_TESTBED_host_get_id_
1530 ((NULL != slave_host) ? slave_host : master->host));
1531 msg->config_size = htons ((uint16_t) scfg_size);
1532 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1533 memcpy (&msg[1], sxcfg, sxcfg_size);
1534 opc = GNUNET_malloc (sizeof (struct OperationContext));
1537 opc->type = OP_LINK_CONTROLLERS;
1538 opc->id = master->operation_counter++;
1539 opc->state = OPC_STATE_INIT;
1540 msg->operation_id = GNUNET_htonll (opc->id);
1542 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1543 &oprelease_link_controllers);
1544 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1551 * Compresses given configuration using zlib compress
1553 * @param config the serialized configuration
1554 * @param size the size of config
1555 * @param xconfig will be set to the compressed configuration (memory is fresly
1557 * @return the size of the xconfig
1560 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1565 xsize = compressBound ((uLong) size);
1566 *xconfig = GNUNET_malloc (xsize);
1567 GNUNET_assert (Z_OK ==
1568 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1569 (const Bytef *) config, (uLongf) size,
1576 * Create a link from slave controller to delegated controller. Whenever the
1577 * master controller is asked to start a peer at the delegated controller the
1578 * request will be routed towards slave controller (if a route exists). The
1579 * slave controller will then route it to the delegated controller. The
1580 * configuration of the slave controller is given and to be used to either
1581 * create the slave controller or to connect to an existing slave controller
1582 * process. 'is_subordinate' specifies if the given slave controller should be
1583 * started and managed by the master controller, or if the slave already has a
1584 * master and this is just a secondary master that is also allowed to use the
1587 * @param master handle to the master controller who creates the association
1588 * @param delegated_host requests to which host should be delegated
1589 * @param slave_host which host is used to run the slave controller
1590 * @param slave_cfg configuration to use for the slave controller
1591 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1592 * by the master controller; GNUNET_NO if we are just
1593 * allowed to use the slave via TCP/IP
1594 * @return the operation handle
1596 struct GNUNET_TESTBED_Operation *
1597 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1598 struct GNUNET_TESTBED_Host *delegated_host,
1599 struct GNUNET_TESTBED_Host *slave_host,
1600 const struct GNUNET_CONFIGURATION_Handle
1601 *slave_cfg, int is_subordinate)
1603 struct GNUNET_TESTBED_Operation *op;
1609 GNUNET_assert (GNUNET_YES ==
1610 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1611 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1612 GNUNET_assert (GNUNET_YES ==
1613 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1614 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1615 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1616 GNUNET_free (config);
1617 GNUNET_assert ((UINT16_MAX - sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >= cc_size); /* Configuration doesn't fit in 1 message */
1618 op = GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1619 (const char *) cconfig, cc_size,
1620 config_size, is_subordinate);
1621 GNUNET_free (cconfig);
1627 * Ask the testbed controller to write the current overlay topology to
1628 * a file. Naturally, the file will only contain a snapshot as the
1629 * topology may evolve all the time.
1631 * @param controller overlay controller to inspect
1632 * @param filename name of the file the topology should
1636 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1638 const char *filename)
1645 * Creates a helper initialization message. This function is here because we
1646 * want to use this in testing
1648 * @param cname the ip address of the controlling host
1649 * @param cfg the configuration that has to used to start the testbed service
1651 * @return the initialization message
1653 struct GNUNET_TESTBED_HelperInit *
1654 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1655 const struct GNUNET_CONFIGURATION_Handle
1658 struct GNUNET_TESTBED_HelperInit *msg;
1662 size_t xconfig_size;
1666 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1667 GNUNET_assert (NULL != config);
1669 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1670 GNUNET_free (config);
1671 cname_len = strlen (cname);
1673 xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
1674 msg = GNUNET_realloc (xconfig, msg_size);
1675 (void) memmove (((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1676 msg->header.size = htons (msg_size);
1677 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1678 msg->cname_size = htons (cname_len);
1679 msg->config_size = htons (config_size);
1680 (void) strcpy ((char *) &msg[1], cname);
1686 * Cancel a pending operation. Releases all resources
1687 * of the operation and will ensure that no event
1688 * is generated for the operation. Does NOT guarantee
1689 * that the operation will be fully undone (or that
1690 * nothing ever happened).
1692 * @param operation operation to cancel
1695 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1697 GNUNET_TESTBED_operation_done (operation);
1702 * Signal that the information from an operation has been fully
1703 * processed. This function MUST be called for each event
1704 * of type 'operation_finished' to fully remove the operation
1705 * from the operation queue. After calling this function, the
1706 * 'op_result' becomes invalid (!).
1708 * @param operation operation to signal completion for
1711 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1713 switch (operation->type)
1715 case OP_PEER_CREATE:
1716 case OP_PEER_DESTROY:
1720 case OP_OVERLAY_CONNECT:
1721 case OP_LINK_CONTROLLERS:
1722 GNUNET_TESTBED_operation_release_ (operation);
1732 * Generates configuration by parsing Peer configuration information reply message
1734 * @param msg the peer configuration information message
1735 * @return handle to the parsed configuration
1737 struct GNUNET_CONFIGURATION_Handle *
1738 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (const struct
1739 GNUNET_TESTBED_PeerConfigurationInformationMessage
1742 struct GNUNET_CONFIGURATION_Handle *cfg;
1748 config_size = (uLong) ntohs (msg->config_size);
1749 config = GNUNET_malloc (config_size);
1750 msize = ntohs (msg->header.size);
1751 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1754 uncompress ((Bytef *) config, &config_size, (const Bytef *) &msg[1],
1757 cfg = GNUNET_CONFIGURATION_create ();
1758 GNUNET_assert (GNUNET_OK ==
1759 GNUNET_CONFIGURATION_deserialize (cfg, config,
1760 (size_t) config_size,
1762 GNUNET_free (config);
1766 /* end of testbed_api.c */