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;
286 case OP_PEER_DESTROY:
288 struct GNUNET_TESTBED_Peer *peer;
292 event->details.operation_finished.operation = opc->op;
293 event->details.operation_finished.op_cls = NULL;
294 event->details.operation_finished.emsg = NULL;
295 event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
296 event->details.operation_finished.op_result.generic = NULL;
307 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
308 opc->state = OPC_STATE_FINISHED;
312 c->cc (c->cc_cls, event);
320 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
321 * controller (testbed service)
323 * @param c the controller handler
324 * @param msg message received
325 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
329 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
331 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
333 struct OperationContext *opc;
334 struct PeerCreateData *data;
335 struct GNUNET_TESTBED_Peer *peer;
336 GNUNET_TESTBED_PeerCreateCallback cb;
340 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
341 == ntohs (msg->header.size));
342 op_id = GNUNET_ntohll (msg->operation_id);
343 if (NULL == (opc = find_opc (c, op_id)))
345 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
348 if (OP_FORWARDED == opc->type)
350 struct ForwardedOperationData *fo_data;
353 if (NULL != fo_data->cc)
354 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
355 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
356 GNUNET_free (fo_data);
360 GNUNET_assert (OP_PEER_CREATE == opc->type);
361 GNUNET_assert (NULL != opc->data);
363 GNUNET_assert (NULL != data->peer);
365 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
366 peer->state = PS_CREATED;
369 GNUNET_free (opc->data);
370 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
371 opc->state = OPC_STATE_FINISHED;
373 cb (cls, peer, NULL);
379 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
380 * controller (testbed service)
382 * @param c the controller handler
383 * @param msg message received
384 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
388 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
389 const struct GNUNET_TESTBED_PeerEventMessage *msg)
391 struct OperationContext *opc;
392 struct GNUNET_TESTBED_Peer *peer;
393 struct GNUNET_TESTBED_EventInformation event;
396 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
397 == ntohs (msg->header.size));
398 op_id = GNUNET_ntohll (msg->operation_id);
399 if (NULL == (opc = find_opc (c, op_id)))
401 LOG_DEBUG ("Operation not found\n");
404 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
406 GNUNET_assert (NULL != peer);
407 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
410 case GNUNET_TESTBED_ET_PEER_START:
411 peer->state = PS_STARTED;
412 event.details.peer_start.host = peer->host;
413 event.details.peer_start.peer = peer;
415 case GNUNET_TESTBED_ET_PEER_STOP:
416 peer->state = PS_STOPPED;
417 event.details.peer_stop.peer = peer;
420 GNUNET_assert (0); /* We should never reach this state */
422 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
423 opc->state = OPC_STATE_FINISHED;
424 if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
428 c->cc (c->cc_cls, &event);
435 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
436 * controller (testbed service)
438 * @param c the controller handler
439 * @param msg message received
440 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
444 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
445 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
447 struct OperationContext *opc;
448 struct OverlayConnectData *data;
449 struct GNUNET_TESTBED_EventInformation event;
452 op_id = GNUNET_ntohll (msg->operation_id);
453 if (NULL == (opc = find_opc (c, op_id)))
455 LOG_DEBUG ("Operation not found\n");
459 GNUNET_assert (NULL != data);
460 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id)
461 && (ntohl (msg->peer2) == data->p2->unique_id));
462 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
465 case GNUNET_TESTBED_ET_CONNECT:
466 event.details.peer_connect.peer1 = data->p1;
467 event.details.peer_connect.peer2 = data->p2;
469 case GNUNET_TESTBED_ET_DISCONNECT:
470 GNUNET_assert (0); /* FIXME: implement */
473 GNUNET_assert (0); /* Should never reach here */
476 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
477 opc->state = OPC_STATE_FINISHED;
479 if (0 != ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT)
483 c->cc (c->cc_cls, &event);
490 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
491 * controller (testbed service)
493 * @param c the controller handler
494 * @param msg message received
495 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
499 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
500 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
502 struct OperationContext *opc;
503 struct GNUNET_TESTBED_Peer *peer;
504 struct PeerInfoData *data;
505 struct PeerInfoData2 *response_data;
506 struct GNUNET_TESTBED_EventInformation info;
509 op_id = GNUNET_ntohll (msg->operation_id);
510 if (NULL == (opc = find_opc (c, op_id)))
512 LOG_DEBUG ("Operation not found\n");
516 GNUNET_assert (NULL != data);
518 GNUNET_assert (NULL != peer);
519 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
520 if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
522 LOG_DEBUG ("Skipping operation callback as flag not set\n");
525 response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
526 response_data->pit = data->pit;
529 info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
530 info.details.operation_finished.operation = opc->op;
531 info.details.operation_finished.op_cls = NULL;
532 info.details.operation_finished.emsg = NULL;
533 info.details.operation_finished.pit = response_data->pit;
534 switch (response_data->pit)
536 case GNUNET_TESTBED_PIT_IDENTITY:
538 struct GNUNET_PeerIdentity *peer_identity;
540 peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
541 (void) memcpy (peer_identity, &msg->peer_identity,
542 sizeof (struct GNUNET_PeerIdentity));
543 response_data->details.peer_identity = peer_identity;
544 info.details.operation_finished.op_result.pid = peer_identity;
547 case GNUNET_TESTBED_PIT_CONFIGURATION:
549 struct GNUNET_CONFIGURATION_Handle *cfg;
555 config_size = (uLong) ntohs (msg->config_size);
556 config = GNUNET_malloc (config_size);
557 msize = ntohs (msg->header.size);
558 msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
559 if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
560 (const Bytef *) &msg[1], (uLong) msize)))
562 cfg = GNUNET_CONFIGURATION_create (); /* Freed in oprelease_peer_getinfo */
563 GNUNET_assert (GNUNET_OK ==
564 GNUNET_CONFIGURATION_deserialize (cfg, config,
565 (size_t) config_size,
567 GNUNET_free (config);
568 response_data->details.cfg = cfg;
569 info.details.operation_finished.op_result.cfg = cfg;
572 case GNUNET_TESTBED_PIT_GENERIC:
573 GNUNET_assert (0); /* never reach here */
576 opc->data = response_data;
577 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
578 opc->state = OPC_STATE_FINISHED;
579 c->cc (c->cc_cls, &info);
585 * Handler for messages from controller (testbed service)
587 * @param cls the controller handler
588 * @param msg message received, NULL on timeout or fatal error
591 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
593 struct GNUNET_TESTBED_Controller *c = cls;
597 c->in_receive = GNUNET_NO;
598 /* FIXME: Add checks for message integrity */
601 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
605 msize = ntohs (msg->size);
606 switch (ntohs (msg->type))
608 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
609 GNUNET_assert (msize >= sizeof (struct
610 GNUNET_TESTBED_HostConfirmedMessage));
612 handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
614 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
616 (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
618 handle_opsuccess (c, (const struct
619 GNUNET_TESTBED_GenericOperationSuccessEventMessage
622 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
623 GNUNET_assert (msize ==
624 sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
626 handle_peer_create_success
627 (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
629 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
630 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
632 handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
635 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
636 GNUNET_assert (msize >=
637 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
640 (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
643 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
644 GNUNET_assert (msize ==
645 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
647 handle_peer_conevent (c, (const struct
648 GNUNET_TESTBED_ConnectionEventMessage *) msg);
653 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
655 c->in_receive = GNUNET_YES;
656 GNUNET_CLIENT_receive (c->client, &message_handler, c,
657 GNUNET_TIME_UNIT_FOREVER_REL);
663 * Function called to notify a client about the connection begin ready to queue
664 * more data. "buf" will be NULL and "size" zero if the connection was closed
665 * for writing in the meantime.
668 * @param size number of bytes available in buf
669 * @param buf where the callee should write the message
670 * @return number of bytes written to buf
673 transmit_ready_notify (void *cls, size_t size, void *buf)
675 struct GNUNET_TESTBED_Controller *c = cls;
676 struct MessageQueue *mq_entry;
679 mq_entry = c->mq_head;
680 GNUNET_assert (NULL != mq_entry);
681 if ((0 == size) && (NULL == buf)) /* Timeout */
683 LOG_DEBUG ("Message sending timed out -- retrying\n");
685 GNUNET_CLIENT_notify_transmit_ready (c->client,
686 ntohs (mq_entry->msg->size),
688 GNUNET_YES, &transmit_ready_notify,
692 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
693 size = ntohs (mq_entry->msg->size);
694 memcpy (buf, mq_entry->msg, size);
695 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
696 ntohs (mq_entry->msg->type), size);
697 GNUNET_free (mq_entry->msg);
698 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
699 GNUNET_free (mq_entry);
700 mq_entry = c->mq_head;
701 if (NULL != mq_entry)
703 GNUNET_CLIENT_notify_transmit_ready (c->client,
704 ntohs (mq_entry->msg->size),
706 GNUNET_YES, &transmit_ready_notify,
708 if (GNUNET_NO == c->in_receive)
710 c->in_receive = GNUNET_YES;
711 GNUNET_CLIENT_receive (c->client, &message_handler, c,
712 GNUNET_TIME_UNIT_FOREVER_REL);
719 * Queues a message in send queue for sending to the service
721 * @param controller the handle to the controller
722 * @param msg the message to queue
725 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
726 struct GNUNET_MessageHeader *msg)
728 struct MessageQueue *mq_entry;
732 type = ntohs (msg->type);
733 size = ntohs (msg->size);
734 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
735 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
736 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
738 LOG (GNUNET_ERROR_TYPE_DEBUG,
739 "Queueing message of type %u, size %u for sending\n", type,
741 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
743 if (NULL == controller->th)
745 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
747 GNUNET_YES, &transmit_ready_notify,
753 * Sends the given message as an operation. The given callback is called when a
754 * reply for the operation is available. Call
755 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
756 * operation context if the cc hasn't been called
758 * @param controller the controller to which the message has to be sent
759 * @param operation_id the operation id of the message
760 * @param msg the message to send
761 * @param cc the callback to call when reply is available
762 * @param cc_cls the closure for the above callback
763 * @return the operation context which can be used to cancel the forwarded
766 struct OperationContext *
767 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
769 uint64_t operation_id,
770 const struct GNUNET_MessageHeader *msg,
771 GNUNET_CLIENT_MessageHandler cc,
774 struct OperationContext *opc;
775 struct ForwardedOperationData *data;
776 struct GNUNET_MessageHeader *dup_msg;
779 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
781 data->cc_cls = cc_cls;
782 opc = GNUNET_malloc (sizeof (struct OperationContext));
784 opc->type = OP_FORWARDED;
786 opc->id = operation_id;
787 msize = ntohs (msg->size);
788 dup_msg = GNUNET_malloc (msize);
789 (void) memcpy (dup_msg, msg, msize);
790 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
791 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head,
792 controller->ocq_tail, opc);
798 * Function to cancel an operation created by simply forwarding an operation
801 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
804 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
806 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
807 GNUNET_free (opc->data);
813 * Handle for controller process
815 struct GNUNET_TESTBED_ControllerProc
820 struct GNUNET_HELPER_Handle *helper;
823 * The host where the helper is run
825 struct GNUNET_TESTBED_Host *host;
828 * The controller error callback
830 GNUNET_TESTBED_ControllerStatusCallback cb;
833 * The closure for the above callback
838 * The send handle for the helper
840 struct GNUNET_HELPER_SendHandle *shandle;
843 * The message corresponding to send handle
845 struct GNUNET_MessageHeader *msg;
848 * The port number for ssh; used for helpers starting ssh
853 * The ssh destination string; used for helpers starting ssh
858 * The configuration of the running testbed service
860 struct GNUNET_CONFIGURATION_Handle *cfg;
866 * Functions with this signature are called whenever a
867 * complete message is received by the tokenizer.
869 * Do not call GNUNET_SERVER_mst_destroy in callback
872 * @param client identification of the client
873 * @param message the actual message
875 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
877 static int helper_mst (void *cls, void *client,
878 const struct GNUNET_MessageHeader *message)
880 struct GNUNET_TESTBED_ControllerProc *cp = cls;
881 const struct GNUNET_TESTBED_HelperReply *msg;
882 const char *hostname;
887 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
888 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
889 < ntohs (msg->header.size));
890 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
891 == ntohs (msg->header.type));
892 config_size = (uLongf) ntohs (msg->config_size);
893 xconfig_size = (uLongf) (ntohs (msg->header.size)
894 - sizeof (struct GNUNET_TESTBED_HelperReply));
895 config = GNUNET_malloc (config_size);
896 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
897 (const Bytef *) &msg[1], xconfig_size));
898 GNUNET_assert (NULL == cp->cfg);
899 cp->cfg = GNUNET_CONFIGURATION_create ();
900 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
901 config_size, GNUNET_NO));
902 GNUNET_free (config);
903 if ((NULL == cp->host) ||
904 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
905 hostname = "localhost";
906 /* Change the hostname so that we can connect to it */
907 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
909 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
915 * Continuation function from GNUNET_HELPER_send()
918 * @param result GNUNET_OK on success,
919 * GNUNET_NO if helper process died
920 * GNUNET_SYSERR during GNUNET_HELPER_stop
923 clear_msg (void *cls, int result)
925 struct GNUNET_TESTBED_ControllerProc *cp = cls;
927 GNUNET_assert (NULL != cp->shandle);
929 GNUNET_free (cp->msg);
934 * Callback that will be called when the helper process dies. This is not called
935 * when the helper process is stoped using GNUNET_HELPER_stop()
937 * @param cls the closure from GNUNET_HELPER_start()
940 helper_exp_cb (void *cls)
942 struct GNUNET_TESTBED_ControllerProc *cp = cls;
943 GNUNET_TESTBED_ControllerStatusCallback cb;
949 GNUNET_TESTBED_controller_stop (cp);
951 cb (cb_cls, NULL, GNUNET_SYSERR);
956 * Starts a controller process at the host. FIXME: add controller start callback
957 * with the configuration with which the controller is started
959 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
960 * host when starting testbed controller at host
961 * @param host the host where the controller has to be started; NULL for
963 * @param cfg template configuration to use for the remote controller; the
964 * remote controller will be started with a slightly modified
965 * configuration (port numbers, unix domain sockets and service home
966 * values are changed as per TESTING library on the remote host)
967 * @param cb function called when the controller is successfully started or
968 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
969 * called if cb is called with GNUNET_SYSERR as status. Will never be
970 * called in the same task as 'GNUNET_TESTBED_controller_start'
971 * (synchronous errors will be signalled by returning NULL). This
972 * parameter cannot be NULL.
973 * @param cls closure for above callbacks
974 * @return the controller process handle, NULL on errors
976 struct GNUNET_TESTBED_ControllerProc *
977 GNUNET_TESTBED_controller_start (const char *controller_ip,
978 struct GNUNET_TESTBED_Host *host,
979 const struct GNUNET_CONFIGURATION_Handle *cfg,
980 GNUNET_TESTBED_ControllerStatusCallback cb,
983 struct GNUNET_TESTBED_ControllerProc *cp;
984 struct GNUNET_TESTBED_HelperInit *msg;
986 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
987 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
989 char * const binary_argv[] = {
990 "gnunet-testbed-helper", NULL
993 cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
994 &helper_mst, &helper_exp_cb, cp);
998 char *remote_args[6 + 1];
1000 const char *username;
1001 const char *hostname;
1003 username = GNUNET_TESTBED_host_get_username_ (host);
1004 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1005 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1006 if (NULL == username)
1007 GNUNET_asprintf (&cp->dst, "%s", hostname);
1009 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1010 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1012 remote_args[argp++] = "ssh";
1013 remote_args[argp++] = "-p";
1014 remote_args[argp++] = cp->port;
1015 remote_args[argp++] = "-q";
1016 remote_args[argp++] = cp->dst;
1017 remote_args[argp++] = "gnunet-testbed-helper";
1018 remote_args[argp++] = NULL;
1019 GNUNET_assert (argp == 6 + 1);
1020 cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
1021 &helper_mst, &helper_exp_cb, cp);
1023 if (NULL == cp->helper)
1025 GNUNET_free_non_null (cp->port);
1026 GNUNET_free_non_null (cp->dst);
1033 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1034 cp->msg = &msg->header;
1035 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
1037 if (NULL == cp->shandle)
1040 GNUNET_TESTBED_controller_stop (cp);
1048 * Stop the controller process (also will terminate all peers and controllers
1049 * dependent on this controller). This function blocks until the testbed has
1050 * been fully terminated (!).
1052 * @param cproc the controller process handle
1055 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1057 if (NULL != cproc->shandle)
1058 GNUNET_HELPER_send_cancel (cproc->shandle);
1059 if (NULL != cproc->helper)
1060 GNUNET_HELPER_stop (cproc->helper);
1061 if (NULL != cproc->cfg)
1062 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1063 GNUNET_free_non_null (cproc->port);
1064 GNUNET_free_non_null (cproc->dst);
1065 GNUNET_free (cproc);
1070 * Start a controller process using the given configuration at the
1073 * @param cfg configuration to use
1074 * @param host host to run the controller on; This should be the same host if
1075 * the controller was previously started with
1076 * GNUNET_TESTBED_controller_start; NULL for localhost
1077 * @param event_mask bit mask with set of events to call 'cc' for;
1078 * or-ed values of "1LL" shifted by the
1079 * respective 'enum GNUNET_TESTBED_EventType'
1080 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1081 * @param cc controller callback to invoke on events
1082 * @param cc_cls closure for cc
1083 * @return handle to the controller
1085 struct GNUNET_TESTBED_Controller *
1086 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
1087 struct GNUNET_TESTBED_Host *host,
1088 uint64_t event_mask,
1089 GNUNET_TESTBED_ControllerCallback cc,
1092 struct GNUNET_TESTBED_Controller *controller;
1093 struct GNUNET_TESTBED_InitMessage *msg;
1094 const char *controller_hostname;
1095 unsigned long long max_parallel_peer_create;
1098 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1099 "MAX_PARALLEL_PEER_CREATE",
1100 &max_parallel_peer_create))
1105 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1106 controller->cc = cc;
1107 controller->cc_cls = cc_cls;
1108 controller->event_mask = event_mask;
1109 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1110 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1111 if (NULL == controller->client)
1113 GNUNET_TESTBED_controller_disconnect (controller);
1118 host = GNUNET_TESTBED_host_create_by_id_ (0);
1119 if (NULL == host) /* If the above host create fails */
1121 LOG (GNUNET_ERROR_TYPE_WARNING,
1122 "Treating NULL host as localhost. Multiple references to localhost "
1123 "may break when localhost freed before calling disconnect \n");
1124 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1128 controller->aux_host = GNUNET_YES;
1131 GNUNET_assert (NULL != host);
1132 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1133 controller->host = host;
1134 controller->opq_peer_create =
1135 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1136 max_parallel_peer_create);
1137 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1138 if (NULL == controller_hostname)
1139 controller_hostname = "127.0.0.1";
1140 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage)
1141 + strlen (controller_hostname) + 1);
1142 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1143 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage)
1144 + strlen (controller_hostname) + 1);
1145 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1146 msg->event_mask = GNUNET_htonll (controller->event_mask);
1147 strcpy ((char *) &msg[1], controller_hostname);
1148 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *)
1155 * Configure shared services at a controller. Using this function,
1156 * you can specify that certain services (such as "resolver")
1157 * should not be run for each peer but instead be shared
1158 * across N peers on the specified host. This function
1159 * must be called before any peers are created at the host.
1161 * @param controller controller to configure
1162 * @param service_name name of the service to share
1163 * @param num_peers number of peers that should share one instance
1164 * of the specified service (1 for no sharing is the default),
1165 * use 0 to disable the service
1168 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
1169 const char *service_name,
1172 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1173 uint16_t service_name_size;
1176 service_name_size = strlen (service_name) + 1;
1177 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1178 + service_name_size;
1179 msg = GNUNET_malloc (msg_size);
1180 msg->header.size = htons (msg_size);
1181 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1182 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1183 msg->num_peers = htonl (num_peers);
1184 memcpy (&msg[1], service_name, service_name_size);
1185 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1190 * disconnects from the controller.
1192 * @param controller handle to controller to stop
1195 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1197 struct MessageQueue *mq_entry;
1199 if (NULL != controller->th)
1200 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1201 /* Clear the message queue */
1202 while (NULL != (mq_entry = controller->mq_head))
1204 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1205 controller->mq_tail,
1207 GNUNET_free (mq_entry->msg);
1208 GNUNET_free (mq_entry);
1210 if (NULL != controller->client)
1211 GNUNET_CLIENT_disconnect (controller->client);
1212 GNUNET_CONFIGURATION_destroy (controller->cfg);
1213 if (GNUNET_YES == controller->aux_host)
1214 GNUNET_TESTBED_host_destroy (controller->host);
1215 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_peer_create);
1216 GNUNET_free (controller);
1221 * Register a host with the controller
1223 * @param controller the controller handle
1224 * @param host the host to register
1225 * @param cc the completion callback to call to inform the status of
1226 * registration. After calling this callback the registration handle
1227 * will be invalid. Cannot be NULL.
1228 * @param cc_cls the closure for the cc
1229 * @return handle to the host registration which can be used to cancel the
1232 struct GNUNET_TESTBED_HostRegistrationHandle *
1233 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1234 struct GNUNET_TESTBED_Host *host,
1235 GNUNET_TESTBED_HostRegistrationCompletion cc,
1238 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1239 struct GNUNET_TESTBED_AddHostMessage *msg;
1240 const char *username;
1241 const char *hostname;
1243 uint16_t user_name_length;
1245 if (NULL != controller->rh)
1247 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1248 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1250 LOG (GNUNET_ERROR_TYPE_WARNING,
1251 "Host hostname: %s already registered\n",
1252 (NULL == hostname) ? "localhost" : hostname);
1255 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1258 GNUNET_assert (NULL != cc);
1260 rh->cc_cls = cc_cls;
1261 controller->rh = rh;
1262 username = GNUNET_TESTBED_host_get_username_ (host);
1263 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1264 user_name_length = 0;
1265 if (NULL != username)
1267 user_name_length = strlen (username) + 1;
1268 msg_size += user_name_length;
1270 /* FIXME: what happens when hostname is NULL? localhost */
1271 GNUNET_assert (NULL != hostname);
1272 msg_size += strlen (hostname) + 1;
1273 msg = GNUNET_malloc (msg_size);
1274 msg->header.size = htons (msg_size);
1275 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1276 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1277 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1278 msg->user_name_length = htons (user_name_length);
1279 if (NULL != username)
1280 memcpy (&msg[1], username, user_name_length);
1281 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1282 GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1288 * Cancel the pending registration. Note that if the registration message is
1289 * already sent to the service the cancellation has only the effect that the
1290 * registration completion callback for the registration is never called.
1292 * @param handle the registration handle to cancel
1295 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1298 if (handle != handle->c->rh)
1303 handle->c->rh = NULL;
1304 GNUNET_free (handle);
1309 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1310 * serialized and compressed
1312 * @param master handle to the master controller who creates the association
1313 * @param delegated_host requests to which host should be delegated; cannot be NULL
1314 * @param slave_host which host is used to run the slave controller; use NULL to
1315 * make the master controller connect to the delegated host
1316 * @param sxcfg serialized and compressed configuration
1317 * @param sxcfg_size the size scfg
1318 * @param scfg_size the size of uncompressed serialized configuration
1319 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1320 * be started by the master controller; GNUNET_NO if we are just
1321 * allowed to use the slave via TCP/IP
1324 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1325 struct GNUNET_TESTBED_Host *delegated_host,
1326 struct GNUNET_TESTBED_Host *slave_host,
1332 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1335 GNUNET_assert (GNUNET_YES ==
1336 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1337 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1338 GNUNET_assert (GNUNET_YES ==
1339 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1340 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1341 msg = GNUNET_malloc (msg_size);
1342 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1343 msg->header.size = htons (msg_size);
1344 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1345 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
1346 ((NULL != slave_host) ? slave_host : master->host));
1347 msg->config_size = htons ((uint16_t) scfg_size);
1348 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1349 memcpy (&msg[1], sxcfg, sxcfg_size);
1350 GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1355 * Compresses given configuration using zlib compress
1357 * @param config the serialized configuration
1358 * @param size the size of config
1359 * @param xconfig will be set to the compressed configuration (memory is fresly
1361 * @return the size of the xconfig
1364 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1369 xsize = compressBound ((uLong) size);
1370 *xconfig = GNUNET_malloc (xsize);
1371 GNUNET_assert (Z_OK ==
1372 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1373 (const Bytef *) config, (uLongf) size,
1380 * Create a link from slave controller to delegated controller. Whenever the
1381 * master controller is asked to start a peer at the delegated controller the
1382 * request will be routed towards slave controller (if a route exists). The
1383 * slave controller will then route it to the delegated controller. The
1384 * configuration of the slave controller is given and to be used to either
1385 * create the slave controller or to connect to an existing slave controller
1386 * process. 'is_subordinate' specifies if the given slave controller should be
1387 * started and managed by the master controller, or if the slave already has a
1388 * master and this is just a secondary master that is also allowed to use the
1391 * @param master handle to the master controller who creates the association
1392 * @param delegated_host requests to which host should be delegated
1393 * @param slave_host which host is used to run the slave controller
1394 * @param slave_cfg configuration to use for the slave controller
1395 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1396 * by the master controller; GNUNET_NO if we are just
1397 * allowed to use the slave via TCP/IP
1400 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1401 struct GNUNET_TESTBED_Host *delegated_host,
1402 struct GNUNET_TESTBED_Host *slave_host,
1403 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1411 GNUNET_assert (GNUNET_YES ==
1412 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1413 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1414 GNUNET_assert (GNUNET_YES ==
1415 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1416 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1417 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1418 GNUNET_free (config);
1419 GNUNET_assert ((UINT16_MAX -
1420 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1421 >= cc_size); /* Configuration doesn't fit in 1 message */
1422 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1423 (const char *) cconfig,
1424 cc_size, config_size, is_subordinate);
1425 GNUNET_free (cconfig);
1430 * Ask the testbed controller to write the current overlay topology to
1431 * a file. Naturally, the file will only contain a snapshot as the
1432 * topology may evolve all the time.
1434 * @param controller overlay controller to inspect
1435 * @param filename name of the file the topology should
1439 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1440 const char *filename)
1447 * Creates a helper initialization message. This function is here because we
1448 * want to use this in testing
1450 * @param cname the ip address of the controlling host
1451 * @param cfg the configuration that has to used to start the testbed service
1453 * @return the initialization message
1455 struct GNUNET_TESTBED_HelperInit *
1456 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1457 const struct GNUNET_CONFIGURATION_Handle *cfg)
1459 struct GNUNET_TESTBED_HelperInit *msg;
1463 size_t xconfig_size;
1467 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1468 GNUNET_assert (NULL != config);
1470 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1471 GNUNET_free (config);
1472 cname_len = strlen (cname);
1473 msg_size = xconfig_size + cname_len + 1 +
1474 sizeof (struct GNUNET_TESTBED_HelperInit);
1475 msg = GNUNET_realloc (xconfig, msg_size);
1476 (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1477 msg->header.size = htons (msg_size);
1478 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1479 msg->cname_size = htons (cname_len);
1480 msg->config_size = htons (config_size);
1481 (void) strcpy ((char *) &msg[1], cname);
1487 * Cancel a pending operation. Releases all resources
1488 * of the operation and will ensure that no event
1489 * is generated for the operation. Does NOT guarantee
1490 * that the operation will be fully undone (or that
1491 * nothing ever happened).
1493 * @param operation operation to cancel
1496 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1498 GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1499 operation->controller->op_tail,
1501 GNUNET_TESTBED_operation_done (operation);
1506 * Signal that the information from an operation has been fully
1507 * processed. This function MUST be called for each event
1508 * of type 'operation_finished' to fully remove the operation
1509 * from the operation queue. After calling this function, the
1510 * 'op_result' becomes invalid (!).
1512 * @param operation operation to signal completion for
1515 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1517 switch (operation->type)
1519 case OP_PEER_CREATE:
1520 case OP_PEER_DESTROY:
1524 case OP_OVERLAY_CONNECT:
1525 GNUNET_TESTBED_operation_release_ (operation);
1533 /* end of testbed_api.c */