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 * The message queue for sending messages to the controller service
76 * The message to be sent
78 struct GNUNET_MessageHeader *msg;
81 * next pointer for DLL
83 struct MessageQueue *next;
86 * prev pointer for DLL
88 struct MessageQueue *prev;
93 * Structure for a controller link
98 * The next ptr for DLL
100 struct ControllerLink *next;
103 * The prev ptr for DLL
105 struct ControllerLink *prev;
108 * The host which will be referred in the peer start request. This is the
109 * host where the peer should be started
111 struct GNUNET_TESTBED_Host *delegated_host;
114 * The host which will contacted to delegate the peer start request
116 struct GNUNET_TESTBED_Host *slave_host;
119 * The configuration to be used to connect to slave host
121 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
124 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
125 * if we are just allowed to use the slave via TCP/IP
132 * handle for host registration
134 struct GNUNET_TESTBED_HostRegistrationHandle
137 * The host being registered
139 struct GNUNET_TESTBED_Host *host;
142 * The controller at which this host is being registered
144 struct GNUNET_TESTBED_Controller *c;
147 * The Registartion completion callback
149 GNUNET_TESTBED_HostRegistrationCompletion cc;
152 * The closure for above callback
159 * Context data for forwarded Operation
161 struct ForwardedOperationData
165 * The callback to call when reply is available
167 GNUNET_CLIENT_MessageHandler cc;
170 * The closure for the above callback
178 * Returns the operation context with the given id if found in the Operation
179 * context queues of the controller
181 * @param c the controller whose queues are searched
182 * @param id the id which has to be checked
183 * @return the matching operation context; NULL if no match found
185 static struct OperationContext *
186 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
188 struct OperationContext *opc;
190 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
200 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
201 * controller (testbed service)
203 * @param c the controller handler
204 * @param msg message received
205 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
209 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
210 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
212 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
221 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
223 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
224 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
228 msg_size = ntohs (msg->header.size);
229 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
231 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
232 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
233 rh->cc (rh->cc_cls, NULL);
237 /* We have an error message */
238 emsg = (char *) &msg[1];
239 if ('\0' != emsg[msg_size -
240 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
246 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
247 ntohl (msg->host_id), emsg);
248 rh->cc (rh->cc_cls, emsg);
255 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
256 * controller (testbed service)
258 * @param c the controller handler
259 * @param msg message received
260 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
264 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
266 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
268 struct OperationContext *opc;
269 struct GNUNET_TESTBED_EventInformation *event;
272 op_id = GNUNET_ntohll (msg->operation_id);
273 LOG_DEBUG ("Operation %ul successful\n", op_id);
274 if (NULL == (opc = find_opc (c, op_id)))
276 LOG_DEBUG ("Operation not found\n");
280 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
281 event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
283 event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
288 struct ForwardedOperationData *fo_data;
291 if (NULL != fo_data->cc)
292 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
293 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
294 GNUNET_free (fo_data);
299 case OP_PEER_DESTROY:
301 struct GNUNET_TESTBED_Peer *peer;
308 case OP_LINK_CONTROLLERS:
315 event->details.operation_finished.operation = opc->op;
316 event->details.operation_finished.op_cls = NULL;
317 event->details.operation_finished.emsg = NULL;
318 event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
319 event->details.operation_finished.op_result.generic = NULL;
321 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
322 opc->state = OPC_STATE_FINISHED;
326 c->cc (c->cc_cls, event);
334 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
335 * controller (testbed service)
337 * @param c the controller handler
338 * @param msg message received
339 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
343 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
345 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
347 struct OperationContext *opc;
348 struct PeerCreateData *data;
349 struct GNUNET_TESTBED_Peer *peer;
350 GNUNET_TESTBED_PeerCreateCallback cb;
354 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
355 == ntohs (msg->header.size));
356 op_id = GNUNET_ntohll (msg->operation_id);
357 if (NULL == (opc = find_opc (c, op_id)))
359 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
362 if (OP_FORWARDED == opc->type)
364 struct ForwardedOperationData *fo_data;
367 if (NULL != fo_data->cc)
368 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
369 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
370 GNUNET_free (fo_data);
374 GNUNET_assert (OP_PEER_CREATE == opc->type);
375 GNUNET_assert (NULL != opc->data);
377 GNUNET_assert (NULL != data->peer);
379 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
380 peer->state = PS_CREATED;
383 GNUNET_free (opc->data);
384 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
385 opc->state = OPC_STATE_FINISHED;
387 cb (cls, peer, NULL);
393 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
394 * controller (testbed service)
396 * @param c the controller handler
397 * @param msg message received
398 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
402 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
403 const struct GNUNET_TESTBED_PeerEventMessage *msg)
405 struct OperationContext *opc;
406 struct GNUNET_TESTBED_Peer *peer;
407 struct GNUNET_TESTBED_EventInformation event;
410 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
411 == ntohs (msg->header.size));
412 op_id = GNUNET_ntohll (msg->operation_id);
413 if (NULL == (opc = find_opc (c, op_id)))
415 LOG_DEBUG ("Operation not found\n");
418 if (OP_FORWARDED == opc->type)
420 struct ForwardedOperationData *fo_data;
423 if (NULL != fo_data->cc)
424 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
425 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
426 GNUNET_free (fo_data);
430 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
432 GNUNET_assert (NULL != peer);
433 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
436 case GNUNET_TESTBED_ET_PEER_START:
437 peer->state = PS_STARTED;
438 event.details.peer_start.host = peer->host;
439 event.details.peer_start.peer = peer;
441 case GNUNET_TESTBED_ET_PEER_STOP:
442 peer->state = PS_STOPPED;
443 event.details.peer_stop.peer = peer;
446 GNUNET_assert (0); /* We should never reach this state */
448 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
449 opc->state = OPC_STATE_FINISHED;
450 if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
454 c->cc (c->cc_cls, &event);
461 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
462 * controller (testbed service)
464 * @param c the controller handler
465 * @param msg message received
466 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
470 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
471 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
473 struct OperationContext *opc;
474 struct OverlayConnectData *data;
475 struct GNUNET_TESTBED_EventInformation event;
478 op_id = GNUNET_ntohll (msg->operation_id);
479 if (NULL == (opc = find_opc (c, op_id)))
481 LOG_DEBUG ("Operation not found\n");
485 GNUNET_assert (NULL != data);
486 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id)
487 && (ntohl (msg->peer2) == data->p2->unique_id));
488 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
491 case GNUNET_TESTBED_ET_CONNECT:
492 event.details.peer_connect.peer1 = data->p1;
493 event.details.peer_connect.peer2 = data->p2;
495 case GNUNET_TESTBED_ET_DISCONNECT:
496 GNUNET_assert (0); /* FIXME: implement */
499 GNUNET_assert (0); /* Should never reach here */
502 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
503 opc->state = OPC_STATE_FINISHED;
505 if (0 != ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT)
509 c->cc (c->cc_cls, &event);
516 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
517 * controller (testbed service)
519 * @param c the controller handler
520 * @param msg message received
521 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
525 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
526 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
528 struct OperationContext *opc;
529 struct GNUNET_TESTBED_Peer *peer;
530 struct PeerInfoData *data;
531 struct PeerInfoData2 *response_data;
532 struct GNUNET_TESTBED_EventInformation info;
535 op_id = GNUNET_ntohll (msg->operation_id);
536 if (NULL == (opc = find_opc (c, op_id)))
538 LOG_DEBUG ("Operation not found\n");
541 if (OP_FORWARDED == opc->type)
543 struct ForwardedOperationData *fo_data;
546 if (NULL != fo_data->cc)
547 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
548 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
549 GNUNET_free (fo_data);
554 GNUNET_assert (NULL != data);
556 GNUNET_assert (NULL != peer);
557 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
558 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
560 LOG_DEBUG ("Skipping operation callback as flag not set\n");
563 response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
564 response_data->pit = data->pit;
567 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
568 info.details.operation_finished.operation = opc->op;
569 info.details.operation_finished.op_cls = NULL;
570 info.details.operation_finished.emsg = NULL;
571 info.details.operation_finished.pit = response_data->pit;
572 switch (response_data->pit)
574 case GNUNET_TESTBED_PIT_IDENTITY:
576 struct GNUNET_PeerIdentity *peer_identity;
578 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
579 (void) memcpy (peer_identity, &msg->peer_identity,
580 sizeof (struct GNUNET_PeerIdentity));
581 response_data->details.peer_identity = peer_identity;
582 info.details.operation_finished.op_result.pid = peer_identity;
585 case GNUNET_TESTBED_PIT_CONFIGURATION:
586 response_data->details.cfg = /* Freed in oprelease_peer_getinfo */
587 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (msg);
588 info.details.operation_finished.op_result.cfg = response_data->details.cfg;
590 case GNUNET_TESTBED_PIT_GENERIC:
591 GNUNET_assert (0); /* never reach here */
594 opc->data = response_data;
595 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
596 opc->state = OPC_STATE_FINISHED;
597 if (0 != ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT)
601 c->cc (c->cc_cls, &info);
608 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT 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,
619 GNUNET_TESTBED_OperationFailureEventMessage *msg)
621 struct OperationContext *opc;
624 struct GNUNET_TESTBED_EventInformation event;
627 op_id = GNUNET_ntohll (msg->operation_id);
628 if (NULL == (opc = find_opc (c, op_id)))
630 LOG_DEBUG ("Operation not found\n");
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_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
641 GNUNET_free (fo_data);
645 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
646 opc->state = OPC_STATE_FINISHED;
649 /* FIXME: Cleanup the data pointer depending on the type of opc */
653 msize = ntohs (msg->header.size);
655 if (msize > sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage))
657 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
658 emsg = (char *) &msg[1];
659 GNUNET_assert ('\0' == emsg[msize - 1]);
661 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask))
664 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
665 event.details.operation_finished.operation = opc->op;
666 event.details.operation_finished.op_cls = NULL;
667 event.details.operation_finished.emsg = emsg;
668 event.details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
669 event.details.operation_finished.op_result.generic = NULL;
670 c->cc (c->cc_cls, &event);
677 * Handler for messages from controller (testbed service)
679 * @param cls the controller handler
680 * @param msg message received, NULL on timeout or fatal error
683 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
685 struct GNUNET_TESTBED_Controller *c = cls;
689 c->in_receive = GNUNET_NO;
690 /* FIXME: Add checks for message integrity */
693 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
697 msize = ntohs (msg->size);
698 switch (ntohs (msg->type))
700 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
701 GNUNET_assert (msize >= sizeof (struct
702 GNUNET_TESTBED_HostConfirmedMessage));
704 handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
706 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
708 (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
710 handle_opsuccess (c, (const struct
711 GNUNET_TESTBED_GenericOperationSuccessEventMessage
714 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
715 GNUNET_assert (msize ==
716 sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
718 handle_peer_create_success
719 (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
721 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
722 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
724 handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
727 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
728 GNUNET_assert (msize >=
729 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
732 (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
735 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
736 GNUNET_assert (msize ==
737 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
739 handle_peer_conevent (c, (const struct
740 GNUNET_TESTBED_ConnectionEventMessage *) msg);
742 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT:
743 GNUNET_assert (msize >=
744 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
746 handle_op_fail_event (c, (const struct
747 GNUNET_TESTBED_OperationFailureEventMessage *)
753 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
755 c->in_receive = GNUNET_YES;
756 GNUNET_CLIENT_receive (c->client, &message_handler, c,
757 GNUNET_TIME_UNIT_FOREVER_REL);
763 * Function called to notify a client about the connection begin ready to queue
764 * more data. "buf" will be NULL and "size" zero if the connection was closed
765 * for writing in the meantime.
768 * @param size number of bytes available in buf
769 * @param buf where the callee should write the message
770 * @return number of bytes written to buf
773 transmit_ready_notify (void *cls, size_t size, void *buf)
775 struct GNUNET_TESTBED_Controller *c = cls;
776 struct MessageQueue *mq_entry;
779 mq_entry = c->mq_head;
780 GNUNET_assert (NULL != mq_entry);
781 if ((0 == size) && (NULL == buf)) /* Timeout */
783 LOG_DEBUG ("Message sending timed out -- retrying\n");
785 GNUNET_CLIENT_notify_transmit_ready (c->client,
786 ntohs (mq_entry->msg->size),
788 GNUNET_YES, &transmit_ready_notify,
792 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
793 size = ntohs (mq_entry->msg->size);
794 memcpy (buf, mq_entry->msg, size);
795 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
796 ntohs (mq_entry->msg->type), size);
797 GNUNET_free (mq_entry->msg);
798 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
799 GNUNET_free (mq_entry);
800 mq_entry = c->mq_head;
801 if (NULL != mq_entry)
803 GNUNET_CLIENT_notify_transmit_ready (c->client,
804 ntohs (mq_entry->msg->size),
806 GNUNET_YES, &transmit_ready_notify,
808 if (GNUNET_NO == c->in_receive)
810 c->in_receive = GNUNET_YES;
811 GNUNET_CLIENT_receive (c->client, &message_handler, c,
812 GNUNET_TIME_UNIT_FOREVER_REL);
819 * Queues a message in send queue for sending to the service
821 * @param controller the handle to the controller
822 * @param msg the message to queue
825 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
826 struct GNUNET_MessageHeader *msg)
828 struct MessageQueue *mq_entry;
832 type = ntohs (msg->type);
833 size = ntohs (msg->size);
834 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
835 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
836 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
838 LOG (GNUNET_ERROR_TYPE_DEBUG,
839 "Queueing message of type %u, size %u for sending\n", type,
841 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
843 if (NULL == controller->th)
845 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
847 GNUNET_YES, &transmit_ready_notify,
853 * Sends the given message as an operation. The given callback is called when a
854 * reply for the operation is available. Call
855 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
856 * operation context if the cc hasn't been called
858 * @param controller the controller to which the message has to be sent
859 * @param operation_id the operation id of the message
860 * @param msg the message to send
861 * @param cc the callback to call when reply is available
862 * @param cc_cls the closure for the above callback
863 * @return the operation context which can be used to cancel the forwarded
866 struct OperationContext *
867 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
869 uint64_t operation_id,
870 const struct GNUNET_MessageHeader *msg,
871 GNUNET_CLIENT_MessageHandler cc,
874 struct OperationContext *opc;
875 struct ForwardedOperationData *data;
876 struct GNUNET_MessageHeader *dup_msg;
879 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
881 data->cc_cls = cc_cls;
882 opc = GNUNET_malloc (sizeof (struct OperationContext));
884 opc->type = OP_FORWARDED;
886 opc->id = operation_id;
887 msize = ntohs (msg->size);
888 dup_msg = GNUNET_malloc (msize);
889 (void) memcpy (dup_msg, msg, msize);
890 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
891 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head,
892 controller->ocq_tail, opc);
898 * Function to cancel an operation created by simply forwarding an operation
901 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
904 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
906 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
907 GNUNET_free (opc->data);
913 * Handle for controller process
915 struct GNUNET_TESTBED_ControllerProc
920 struct GNUNET_HELPER_Handle *helper;
923 * The host where the helper is run
925 struct GNUNET_TESTBED_Host *host;
928 * The controller error callback
930 GNUNET_TESTBED_ControllerStatusCallback cb;
933 * The closure for the above callback
938 * The send handle for the helper
940 struct GNUNET_HELPER_SendHandle *shandle;
943 * The message corresponding to send handle
945 struct GNUNET_MessageHeader *msg;
948 * The port number for ssh; used for helpers starting ssh
953 * The ssh destination string; used for helpers starting ssh
958 * The configuration of the running testbed service
960 struct GNUNET_CONFIGURATION_Handle *cfg;
966 * Functions with this signature are called whenever a
967 * complete message is received by the tokenizer.
969 * Do not call GNUNET_SERVER_mst_destroy in callback
972 * @param client identification of the client
973 * @param message the actual message
975 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
978 helper_mst (void *cls, void *client,
979 const struct GNUNET_MessageHeader *message)
981 struct GNUNET_TESTBED_ControllerProc *cp = cls;
982 const struct GNUNET_TESTBED_HelperReply *msg;
983 const char *hostname;
988 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
989 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
990 < ntohs (msg->header.size));
991 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
992 == ntohs (msg->header.type));
993 config_size = (uLongf) ntohs (msg->config_size);
994 xconfig_size = (uLongf) (ntohs (msg->header.size)
995 - sizeof (struct GNUNET_TESTBED_HelperReply));
996 config = GNUNET_malloc (config_size);
997 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
998 (const Bytef *) &msg[1], xconfig_size));
999 GNUNET_assert (NULL == cp->cfg);
1000 cp->cfg = GNUNET_CONFIGURATION_create ();
1001 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
1002 config_size, GNUNET_NO));
1003 GNUNET_free (config);
1004 if ((NULL == cp->host) ||
1005 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1006 hostname = "localhost";
1007 /* Change the hostname so that we can connect to it */
1008 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1010 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1016 * Continuation function from GNUNET_HELPER_send()
1018 * @param cls closure
1019 * @param result GNUNET_OK on success,
1020 * GNUNET_NO if helper process died
1021 * GNUNET_SYSERR during GNUNET_HELPER_stop
1024 clear_msg (void *cls, int result)
1026 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1028 GNUNET_assert (NULL != cp->shandle);
1030 GNUNET_free (cp->msg);
1035 * Callback that will be called when the helper process dies. This is not called
1036 * when the helper process is stoped using GNUNET_HELPER_stop()
1038 * @param cls the closure from GNUNET_HELPER_start()
1041 helper_exp_cb (void *cls)
1043 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1044 GNUNET_TESTBED_ControllerStatusCallback cb;
1050 GNUNET_TESTBED_controller_stop (cp);
1052 cb (cb_cls, NULL, GNUNET_SYSERR);
1057 * Function to call to start a link-controllers type operation once all queues
1058 * the operation is part of declare that the operation can be activated.
1060 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1063 opstart_link_controllers (void *cls)
1065 struct OperationContext *opc = cls;
1066 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1068 GNUNET_assert (NULL != opc->data);
1071 opc->state = OPC_STATE_STARTED;
1072 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1073 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1078 * Callback which will be called when link-controllers type operation is released
1080 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1083 oprelease_link_controllers (void *cls)
1085 struct OperationContext *opc = cls;
1087 if (OPC_STATE_INIT == opc->state)
1088 GNUNET_free (opc->data);
1089 if (OPC_STATE_STARTED == opc->state)
1090 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1096 * Starts a controller process at the host. FIXME: add controller start callback
1097 * with the configuration with which the controller is started
1099 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1100 * host when starting testbed controller at host
1101 * @param host the host where the controller has to be started; NULL for
1103 * @param cfg template configuration to use for the remote controller; the
1104 * remote controller will be started with a slightly modified
1105 * configuration (port numbers, unix domain sockets and service home
1106 * values are changed as per TESTING library on the remote host)
1107 * @param cb function called when the controller is successfully started or
1108 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1109 * called if cb is called with GNUNET_SYSERR as status. Will never be
1110 * called in the same task as 'GNUNET_TESTBED_controller_start'
1111 * (synchronous errors will be signalled by returning NULL). This
1112 * parameter cannot be NULL.
1113 * @param cls closure for above callbacks
1114 * @return the controller process handle, NULL on errors
1116 struct GNUNET_TESTBED_ControllerProc *
1117 GNUNET_TESTBED_controller_start (const char *controller_ip,
1118 struct GNUNET_TESTBED_Host *host,
1119 const struct GNUNET_CONFIGURATION_Handle *cfg,
1120 GNUNET_TESTBED_ControllerStatusCallback cb,
1123 struct GNUNET_TESTBED_ControllerProc *cp;
1124 struct GNUNET_TESTBED_HelperInit *msg;
1126 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1127 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1129 char * const binary_argv[] = {
1130 "gnunet-testbed-helper", NULL
1133 cp->helper = GNUNET_HELPER_start (GNUNET_YES,
1134 "gnunet-testbed-helper", binary_argv,
1135 &helper_mst, &helper_exp_cb, cp);
1139 char *remote_args[8];
1141 const char *username;
1142 const char *hostname;
1144 username = GNUNET_TESTBED_host_get_username_ (host);
1145 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1146 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1147 if (NULL == username)
1148 GNUNET_asprintf (&cp->dst, "%s", hostname);
1150 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1151 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1153 remote_args[argp++] = "ssh";
1154 remote_args[argp++] = "-p";
1155 remote_args[argp++] = cp->port;
1156 remote_args[argp++] = "-o";
1157 remote_args[argp++] = "BatchMode=yes";
1158 remote_args[argp++] = cp->dst;
1159 remote_args[argp++] = "gnunet-testbed-helper";
1160 remote_args[argp++] = NULL;
1161 GNUNET_assert (argp == 8);
1162 cp->helper = GNUNET_HELPER_start (GNUNET_NO,
1164 &helper_mst, &helper_exp_cb, cp);
1166 if (NULL == cp->helper)
1168 GNUNET_free_non_null (cp->port);
1169 GNUNET_free_non_null (cp->dst);
1176 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1177 cp->msg = &msg->header;
1178 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
1180 if (NULL == cp->shandle)
1183 GNUNET_TESTBED_controller_stop (cp);
1191 * Stop the controller process (also will terminate all peers and controllers
1192 * dependent on this controller). This function blocks until the testbed has
1193 * been fully terminated (!).
1195 * @param cproc the controller process handle
1198 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1200 if (NULL != cproc->shandle)
1201 GNUNET_HELPER_send_cancel (cproc->shandle);
1202 if (NULL != cproc->helper)
1203 GNUNET_HELPER_stop (cproc->helper);
1204 if (NULL != cproc->cfg)
1205 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1206 GNUNET_free_non_null (cproc->port);
1207 GNUNET_free_non_null (cproc->dst);
1208 GNUNET_free (cproc);
1213 * Start a controller process using the given configuration at the
1216 * @param cfg configuration to use
1217 * @param host host to run the controller on; This should be the same host if
1218 * the controller was previously started with
1219 * GNUNET_TESTBED_controller_start; NULL for localhost
1220 * @param event_mask bit mask with set of events to call 'cc' for;
1221 * or-ed values of "1LL" shifted by the
1222 * respective 'enum GNUNET_TESTBED_EventType'
1223 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1224 * @param cc controller callback to invoke on events
1225 * @param cc_cls closure for cc
1226 * @return handle to the controller
1228 struct GNUNET_TESTBED_Controller *
1229 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1230 struct GNUNET_TESTBED_Host *host,
1231 uint64_t event_mask,
1232 GNUNET_TESTBED_ControllerCallback cc,
1235 struct GNUNET_TESTBED_Controller *controller;
1236 struct GNUNET_TESTBED_InitMessage *msg;
1237 const char *controller_hostname;
1238 unsigned long long max_parallel_operations;
1239 unsigned long long max_parallel_service_connections;
1242 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1243 "MAX_PARALLEL_OPERATIONS",
1244 &max_parallel_operations))
1250 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1251 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1252 &max_parallel_service_connections))
1257 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1258 controller->cc = cc;
1259 controller->cc_cls = cc_cls;
1260 controller->event_mask = event_mask;
1261 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1262 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1263 if (NULL == controller->client)
1265 GNUNET_TESTBED_controller_disconnect (controller);
1270 host = GNUNET_TESTBED_host_create_by_id_ (0);
1271 if (NULL == host) /* If the above host create fails */
1273 LOG (GNUNET_ERROR_TYPE_WARNING,
1274 "Treating NULL host as localhost. Multiple references to localhost "
1275 "may break when localhost freed before calling disconnect \n");
1276 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1280 controller->aux_host = GNUNET_YES;
1283 GNUNET_assert (NULL != host);
1284 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1285 controller->host = host;
1286 controller->opq_parallel_operations =
1287 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1288 max_parallel_operations);
1289 controller->opq_parallel_service_connections =
1290 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1291 max_parallel_service_connections);
1292 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1293 if (NULL == controller_hostname)
1294 controller_hostname = "127.0.0.1";
1295 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage)
1296 + strlen (controller_hostname) + 1);
1297 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1298 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage)
1299 + strlen (controller_hostname) + 1);
1300 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1301 msg->event_mask = GNUNET_htonll (controller->event_mask);
1302 strcpy ((char *) &msg[1], controller_hostname);
1303 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
1310 * Configure shared services at a controller. Using this function,
1311 * you can specify that certain services (such as "resolver")
1312 * should not be run for each peer but instead be shared
1313 * across N peers on the specified host. This function
1314 * must be called before any peers are created at the host.
1316 * @param controller controller to configure
1317 * @param service_name name of the service to share
1318 * @param num_peers number of peers that should share one instance
1319 * of the specified service (1 for no sharing is the default),
1320 * use 0 to disable the service
1323 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
1324 const char *service_name,
1327 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1328 uint16_t service_name_size;
1331 service_name_size = strlen (service_name) + 1;
1332 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1333 + service_name_size;
1334 msg = GNUNET_malloc (msg_size);
1335 msg->header.size = htons (msg_size);
1336 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1337 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1338 msg->num_peers = htonl (num_peers);
1339 memcpy (&msg[1], service_name, service_name_size);
1340 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1345 * disconnects from the controller.
1347 * @param controller handle to controller to stop
1350 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1352 struct MessageQueue *mq_entry;
1354 if (NULL != controller->th)
1355 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1356 /* Clear the message queue */
1357 while (NULL != (mq_entry = controller->mq_head))
1359 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1360 controller->mq_tail,
1362 GNUNET_free (mq_entry->msg);
1363 GNUNET_free (mq_entry);
1365 if (NULL != controller->client)
1366 GNUNET_CLIENT_disconnect (controller->client);
1367 GNUNET_CONFIGURATION_destroy (controller->cfg);
1368 if (GNUNET_YES == controller->aux_host)
1369 GNUNET_TESTBED_host_destroy (controller->host);
1370 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1371 GNUNET_TESTBED_operation_queue_destroy_
1372 (controller->opq_parallel_service_connections);
1373 GNUNET_free (controller);
1378 * Register a host with the controller
1380 * @param controller the controller handle
1381 * @param host the host to register
1382 * @param cc the completion callback to call to inform the status of
1383 * registration. After calling this callback the registration handle
1384 * will be invalid. Cannot be NULL.
1385 * @param cc_cls the closure for the cc
1386 * @return handle to the host registration which can be used to cancel the
1389 struct GNUNET_TESTBED_HostRegistrationHandle *
1390 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1391 struct GNUNET_TESTBED_Host *host,
1392 GNUNET_TESTBED_HostRegistrationCompletion cc,
1395 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1396 struct GNUNET_TESTBED_AddHostMessage *msg;
1397 const char *username;
1398 const char *hostname;
1400 uint16_t user_name_length;
1402 if (NULL != controller->rh)
1404 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1405 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1407 LOG (GNUNET_ERROR_TYPE_WARNING,
1408 "Host hostname: %s already registered\n",
1409 (NULL == hostname) ? "localhost" : hostname);
1412 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1415 GNUNET_assert (NULL != cc);
1417 rh->cc_cls = cc_cls;
1418 controller->rh = rh;
1419 username = GNUNET_TESTBED_host_get_username_ (host);
1420 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1421 user_name_length = 0;
1422 if (NULL != username)
1424 user_name_length = strlen (username) + 1;
1425 msg_size += user_name_length;
1427 /* FIXME: what happens when hostname is NULL? localhost */
1428 GNUNET_assert (NULL != hostname);
1429 msg_size += strlen (hostname) + 1;
1430 msg = GNUNET_malloc (msg_size);
1431 msg->header.size = htons (msg_size);
1432 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1433 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1434 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1435 msg->user_name_length = htons (user_name_length);
1436 if (NULL != username)
1437 memcpy (&msg[1], username, user_name_length);
1438 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1439 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1445 * Cancel the pending registration. Note that if the registration message is
1446 * already sent to the service the cancellation has only the effect that the
1447 * registration completion callback for the registration is never called.
1449 * @param handle the registration handle to cancel
1452 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1455 if (handle != handle->c->rh)
1460 handle->c->rh = NULL;
1461 GNUNET_free (handle);
1466 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1467 * serialized and compressed
1469 * @param master handle to the master controller who creates the association
1470 * @param delegated_host requests to which host should be delegated; cannot be NULL
1471 * @param slave_host which host is used to run the slave controller; use NULL to
1472 * make the master controller connect to the delegated host
1473 * @param sxcfg serialized and compressed configuration
1474 * @param sxcfg_size the size scfg
1475 * @param scfg_size the size of uncompressed serialized configuration
1476 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1477 * be started by the master controller; GNUNET_NO if we are just
1478 * allowed to use the slave via TCP/IP
1480 struct GNUNET_TESTBED_Operation *
1481 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1482 struct GNUNET_TESTBED_Host *delegated_host,
1483 struct GNUNET_TESTBED_Host *slave_host,
1489 struct OperationContext *opc;
1490 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1493 GNUNET_assert (GNUNET_YES ==
1494 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1495 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1496 GNUNET_assert (GNUNET_YES ==
1497 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1498 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1499 msg = GNUNET_malloc (msg_size);
1500 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1501 msg->header.size = htons (msg_size);
1502 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1503 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
1504 ((NULL != slave_host) ? slave_host : master->host));
1505 msg->config_size = htons ((uint16_t) scfg_size);
1506 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1507 memcpy (&msg[1], sxcfg, sxcfg_size);
1508 opc = GNUNET_malloc (sizeof (struct OperationContext));
1511 opc->type = OP_LINK_CONTROLLERS;
1512 opc->id = master->operation_counter++;
1513 opc->state = OPC_STATE_INIT;
1514 msg->operation_id = GNUNET_htonll (opc->id);
1515 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1516 &oprelease_link_controllers);
1517 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1524 * Compresses given configuration using zlib compress
1526 * @param config the serialized configuration
1527 * @param size the size of config
1528 * @param xconfig will be set to the compressed configuration (memory is fresly
1530 * @return the size of the xconfig
1533 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1538 xsize = compressBound ((uLong) size);
1539 *xconfig = GNUNET_malloc (xsize);
1540 GNUNET_assert (Z_OK ==
1541 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1542 (const Bytef *) config, (uLongf) size,
1549 * Create a link from slave controller to delegated controller. Whenever the
1550 * master controller is asked to start a peer at the delegated controller the
1551 * request will be routed towards slave controller (if a route exists). The
1552 * slave controller will then route it to the delegated controller. The
1553 * configuration of the slave controller is given and to be used to either
1554 * create the slave controller or to connect to an existing slave controller
1555 * process. 'is_subordinate' specifies if the given slave controller should be
1556 * started and managed by the master controller, or if the slave already has a
1557 * master and this is just a secondary master that is also allowed to use the
1560 * @param master handle to the master controller who creates the association
1561 * @param delegated_host requests to which host should be delegated
1562 * @param slave_host which host is used to run the slave controller
1563 * @param slave_cfg configuration to use for the slave controller
1564 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1565 * by the master controller; GNUNET_NO if we are just
1566 * allowed to use the slave via TCP/IP
1567 * @return the operation handle
1569 struct GNUNET_TESTBED_Operation *
1570 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1571 struct GNUNET_TESTBED_Host *delegated_host,
1572 struct GNUNET_TESTBED_Host *slave_host,
1573 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1576 struct GNUNET_TESTBED_Operation *op;
1582 GNUNET_assert (GNUNET_YES ==
1583 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1584 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1585 GNUNET_assert (GNUNET_YES ==
1586 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1587 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1588 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1589 GNUNET_free (config);
1590 GNUNET_assert ((UINT16_MAX -
1591 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1592 >= cc_size); /* Configuration doesn't fit in 1 message */
1593 op = GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1594 (const char *) cconfig,
1595 cc_size, config_size, is_subordinate);
1596 GNUNET_free (cconfig);
1602 * Ask the testbed controller to write the current overlay topology to
1603 * a file. Naturally, the file will only contain a snapshot as the
1604 * topology may evolve all the time.
1606 * @param controller overlay controller to inspect
1607 * @param filename name of the file the topology should
1611 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1612 const char *filename)
1619 * Creates a helper initialization message. This function is here because we
1620 * want to use this in testing
1622 * @param cname the ip address of the controlling host
1623 * @param cfg the configuration that has to used to start the testbed service
1625 * @return the initialization message
1627 struct GNUNET_TESTBED_HelperInit *
1628 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1629 const struct GNUNET_CONFIGURATION_Handle *cfg)
1631 struct GNUNET_TESTBED_HelperInit *msg;
1635 size_t xconfig_size;
1639 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1640 GNUNET_assert (NULL != config);
1642 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1643 GNUNET_free (config);
1644 cname_len = strlen (cname);
1645 msg_size = xconfig_size + cname_len + 1 +
1646 sizeof (struct GNUNET_TESTBED_HelperInit);
1647 msg = GNUNET_realloc (xconfig, msg_size);
1648 (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1649 msg->header.size = htons (msg_size);
1650 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1651 msg->cname_size = htons (cname_len);
1652 msg->config_size = htons (config_size);
1653 (void) strcpy ((char *) &msg[1], cname);
1659 * Cancel a pending operation. Releases all resources
1660 * of the operation and will ensure that no event
1661 * is generated for the operation. Does NOT guarantee
1662 * that the operation will be fully undone (or that
1663 * nothing ever happened).
1665 * @param operation operation to cancel
1668 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1670 GNUNET_TESTBED_operation_done (operation);
1675 * Signal that the information from an operation has been fully
1676 * processed. This function MUST be called for each event
1677 * of type 'operation_finished' to fully remove the operation
1678 * from the operation queue. After calling this function, the
1679 * 'op_result' becomes invalid (!).
1681 * @param operation operation to signal completion for
1684 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1686 switch (operation->type)
1688 case OP_PEER_CREATE:
1689 case OP_PEER_DESTROY:
1693 case OP_OVERLAY_CONNECT:
1694 case OP_LINK_CONTROLLERS:
1695 GNUNET_TESTBED_operation_release_ (operation);
1705 * Generates configuration by parsing Peer configuration information reply message
1707 * @param msg the peer configuration information message
1708 * @return handle to the parsed configuration
1710 struct GNUNET_CONFIGURATION_Handle *
1711 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (const struct
1712 GNUNET_TESTBED_PeerConfigurationInformationMessage
1715 struct GNUNET_CONFIGURATION_Handle *cfg;
1721 config_size = (uLong) ntohs (msg->config_size);
1722 config = GNUNET_malloc (config_size);
1723 msize = ntohs (msg->header.size);
1724 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1725 if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
1726 (const Bytef *) &msg[1], (uLong) msize)))
1728 cfg = GNUNET_CONFIGURATION_create ();
1729 GNUNET_assert (GNUNET_OK ==
1730 GNUNET_CONFIGURATION_deserialize (cfg, config,
1731 (size_t) config_size,
1733 GNUNET_free (config);
1737 /* end of testbed_api.c */