2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api.c
23 * @brief API for accessing the GNUnet testing service.
24 * This library is supposed to make it easier to write
25 * testcases and script large-scale benchmarks.
26 * @author Christian Grothoff
27 * @author Sree Harsha Totakura
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_hello_lib.h"
40 #include "testbed_api.h"
41 #include "testbed_api_hosts.h"
42 #include "testbed_api_peers.h"
43 #include "testbed_api_operations.h"
46 * Generic logging shorthand
48 #define LOG(kind, ...) \
49 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
54 #define LOG_DEBUG(...) \
55 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
58 * Relative time seconds shorthand
60 #define TIME_REL_SECS(sec) \
61 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
65 * Default server message sending retry timeout
67 #define TIMEOUT_REL TIME_REL_SECS(1)
71 * Testbed Helper binary name
73 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
74 #define HELPER_TESTBED_BINARY_SSH ". ~/.bashrc; gnunet-helper-testbed"
78 * The message queue for sending messages to the controller service
83 * The message to be sent
85 struct GNUNET_MessageHeader *msg;
88 * next pointer for DLL
90 struct MessageQueue *next;
93 * prev pointer for DLL
95 struct MessageQueue *prev;
100 * Structure for a controller link
102 struct ControllerLink
105 * The next ptr for DLL
107 struct ControllerLink *next;
110 * The prev ptr for DLL
112 struct ControllerLink *prev;
115 * The host which will be referred in the peer start request. This is the
116 * host where the peer should be started
118 struct GNUNET_TESTBED_Host *delegated_host;
121 * The host which will contacted to delegate the peer start request
123 struct GNUNET_TESTBED_Host *slave_host;
126 * The configuration to be used to connect to slave host
128 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
131 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
132 * if we are just allowed to use the slave via TCP/IP
139 * handle for host registration
141 struct GNUNET_TESTBED_HostRegistrationHandle
144 * The host being registered
146 struct GNUNET_TESTBED_Host *host;
149 * The controller at which this host is being registered
151 struct GNUNET_TESTBED_Controller *c;
154 * The Registartion completion callback
156 GNUNET_TESTBED_HostRegistrationCompletion cc;
159 * The closure for above callback
166 * Context data for forwarded Operation
168 struct ForwardedOperationData
172 * The callback to call when reply is available
174 GNUNET_CLIENT_MessageHandler cc;
177 * The closure for the above callback
185 * Context data for get slave config operations
187 struct GetSlaveConfigData
190 * The operation closure
195 * The id of the slave controller
203 * Returns the operation context with the given id if found in the Operation
204 * context queues of the controller
206 * @param c the controller whose queues are searched
207 * @param id the id which has to be checked
208 * @return the matching operation context; NULL if no match found
210 static struct OperationContext *
211 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
213 struct OperationContext *opc;
215 for (opc = c->ocq_head; NULL != opc; opc = opc->next)
225 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
226 * controller (testbed service)
228 * @param c the controller handler
229 * @param msg message received
230 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
234 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
235 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
237 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
246 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
248 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
249 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
253 msg_size = ntohs (msg->header.size);
254 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
256 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
257 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
258 rh->cc (rh->cc_cls, NULL);
262 /* We have an error message */
263 emsg = (char *) &msg[1];
265 emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
271 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
272 ntohl (msg->host_id), emsg);
273 rh->cc (rh->cc_cls, emsg);
280 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
281 * controller (testbed service)
283 * @param c the controller handler
284 * @param msg message received
285 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
289 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
291 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
293 struct OperationContext *opc;
294 struct GNUNET_TESTBED_EventInformation event;
297 op_id = GNUNET_ntohll (msg->operation_id);
298 LOG_DEBUG ("Operation %ul successful\n", op_id);
299 if (NULL == (opc = find_opc (c, op_id)))
301 LOG_DEBUG ("Operation not found\n");
308 struct ForwardedOperationData *fo_data;
311 if (NULL != fo_data->cc)
312 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
313 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
314 GNUNET_free (fo_data);
319 case OP_PEER_DESTROY:
321 struct GNUNET_TESTBED_Peer *peer;
329 case OP_LINK_CONTROLLERS:
334 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
335 event.details.operation_finished.operation = opc->op;
336 event.details.operation_finished.op_cls = NULL;
337 event.details.operation_finished.emsg = NULL;
338 event.details.operation_finished.generic = NULL;
339 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
340 opc->state = OPC_STATE_FINISHED;
341 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
344 c->cc (c->cc_cls, &event);
351 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
352 * controller (testbed service)
354 * @param c the controller handler
355 * @param msg message received
356 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
360 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
362 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
364 struct OperationContext *opc;
365 struct PeerCreateData *data;
366 struct GNUNET_TESTBED_Peer *peer;
367 GNUNET_TESTBED_PeerCreateCallback cb;
371 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
372 ntohs (msg->header.size));
373 op_id = GNUNET_ntohll (msg->operation_id);
374 if (NULL == (opc = find_opc (c, op_id)))
376 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
379 if (OP_FORWARDED == opc->type)
381 struct ForwardedOperationData *fo_data;
384 if (NULL != fo_data->cc)
385 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
386 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
387 GNUNET_free (fo_data);
391 GNUNET_assert (OP_PEER_CREATE == opc->type);
392 GNUNET_assert (NULL != opc->data);
394 GNUNET_assert (NULL != data->peer);
396 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
397 peer->state = PS_CREATED;
400 GNUNET_free (opc->data);
401 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
402 opc->state = OPC_STATE_FINISHED;
404 cb (cls, peer, NULL);
410 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
411 * controller (testbed service)
413 * @param c the controller handler
414 * @param msg message received
415 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
419 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
420 const struct GNUNET_TESTBED_PeerEventMessage *msg)
422 struct OperationContext *opc;
423 struct GNUNET_TESTBED_Peer *peer;
424 struct PeerEventData *data;
425 GNUNET_TESTBED_PeerChurnCallback pcc;
427 struct GNUNET_TESTBED_EventInformation event;
430 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
431 ntohs (msg->header.size));
432 op_id = GNUNET_ntohll (msg->operation_id);
433 if (NULL == (opc = find_opc (c, op_id)))
435 LOG_DEBUG ("Operation not found\n");
438 if (OP_FORWARDED == opc->type)
440 struct ForwardedOperationData *fo_data;
443 if (NULL != fo_data->cc)
444 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
445 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
446 GNUNET_free (fo_data);
450 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
452 GNUNET_assert (NULL != data);
454 GNUNET_assert (NULL != peer);
455 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
458 case GNUNET_TESTBED_ET_PEER_START:
459 peer->state = PS_STARTED;
460 event.details.peer_start.host = peer->host;
461 event.details.peer_start.peer = peer;
463 case GNUNET_TESTBED_ET_PEER_STOP:
464 peer->state = PS_STOPPED;
465 event.details.peer_stop.peer = peer;
468 GNUNET_assert (0); /* We should never reach this state */
471 pcc_cls = data->pcc_cls;
473 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
474 opc->state = OPC_STATE_FINISHED;
476 ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP) &
480 c->cc (c->cc_cls, &event);
489 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
490 * controller (testbed service)
492 * @param c the controller handler
493 * @param msg message received
494 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
498 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
499 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
501 struct OperationContext *opc;
502 struct OverlayConnectData *data;
503 GNUNET_TESTBED_OperationCompletionCallback cb;
505 struct GNUNET_TESTBED_EventInformation event;
508 op_id = GNUNET_ntohll (msg->operation_id);
509 if (NULL == (opc = find_opc (c, op_id)))
511 LOG_DEBUG ("Operation not found\n");
515 GNUNET_assert (NULL != data);
516 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
517 (ntohl (msg->peer2) == data->p2->unique_id));
518 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
521 case GNUNET_TESTBED_ET_CONNECT:
522 event.details.peer_connect.peer1 = data->p1;
523 event.details.peer_connect.peer2 = data->p2;
525 case GNUNET_TESTBED_ET_DISCONNECT:
526 GNUNET_assert (0); /* FIXME: implement */
529 GNUNET_assert (0); /* Should never reach here */
533 cb_cls = data->cb_cls;
534 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
535 opc->state = OPC_STATE_FINISHED;
538 ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
542 c->cc (c->cc_cls, &event);
545 cb (cb_cls, opc->op, NULL);
551 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
552 * controller (testbed service)
554 * @param c the controller handler
555 * @param msg message received
556 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
560 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
562 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
564 struct OperationContext *opc;
565 struct GNUNET_TESTBED_Peer *peer;
566 struct PeerInfoData *data;
567 struct GNUNET_TESTBED_PeerInformation *pinfo;
568 GNUNET_TESTBED_PeerInfoCallback cb;
572 op_id = GNUNET_ntohll (msg->operation_id);
573 if (NULL == (opc = find_opc (c, op_id)))
575 LOG_DEBUG ("Operation not found\n");
578 if (OP_FORWARDED == opc->type)
580 struct ForwardedOperationData *fo_data;
583 if (NULL != fo_data->cc)
584 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
585 GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
586 GNUNET_free (fo_data);
591 GNUNET_assert (NULL != data);
593 GNUNET_assert (NULL != peer);
594 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
595 pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
596 pinfo->pit = data->pit;
598 cb_cls = data->cb_cls;
603 case GNUNET_TESTBED_PIT_IDENTITY:
604 pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
605 (void) memcpy (pinfo->result.id, &msg->peer_identity,
606 sizeof (struct GNUNET_PeerIdentity));
608 case GNUNET_TESTBED_PIT_CONFIGURATION:
609 pinfo->result.cfg = /* Freed in oprelease_peer_getinfo */
610 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (&msg->header);
612 case GNUNET_TESTBED_PIT_GENERIC:
613 GNUNET_assert (0); /* never reach here */
617 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
618 opc->state = OPC_STATE_FINISHED;
620 cb (cb_cls, opc->op, pinfo, NULL);
626 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
627 * controller (testbed service)
629 * @param c the controller handler
630 * @param msg message received
631 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
635 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
636 const struct GNUNET_TESTBED_OperationFailureEventMessage
639 struct OperationContext *opc;
642 struct GNUNET_TESTBED_EventInformation event;
644 op_id = GNUNET_ntohll (msg->operation_id);
645 if (NULL == (opc = find_opc (c, op_id)))
647 LOG_DEBUG ("Operation not found\n");
650 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
651 if (OP_FORWARDED == opc->type)
653 struct ForwardedOperationData *fo_data;
656 if (NULL != fo_data->cc)
657 fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
658 GNUNET_free (fo_data);
662 opc->state = OPC_STATE_FINISHED;
663 emsg = GNUNET_TESTBED_parse_error_string_ (msg);
665 emsg = "Unknown error";
666 if (OP_PEER_INFO == opc->type)
668 struct PeerInfoData *data;
670 if (NULL != data->cb)
671 data->cb (data->cb_cls, opc->op, NULL, emsg);
673 return GNUNET_YES; /* We do not call controller callback for peer info */
675 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
678 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
679 event.details.operation_finished.operation = opc->op;
680 event.details.operation_finished.op_cls = NULL;
681 event.details.operation_finished.emsg = emsg;
682 event.details.operation_finished.generic = NULL;
683 c->cc (c->cc_cls, &event);
689 struct PeerCreateData *data;
691 GNUNET_free (data->peer);
692 if (NULL != data->cb)
693 data->cb (data->cls, NULL, emsg);
700 struct PeerEventData *data;
702 if (NULL != data->pcc)
703 data->pcc (data->pcc_cls, emsg);
707 case OP_PEER_DESTROY:
711 case OP_OVERLAY_CONNECT:
713 struct OverlayConnectData *data;
715 if (NULL != data->cb)
716 data->cb (data->cb_cls, opc->op, emsg);
722 case OP_LINK_CONTROLLERS: /* No secondary callback */
732 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG message from controller
735 * @param c the controller handler
736 * @param msg message received
737 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
741 handle_slave_config (struct GNUNET_TESTBED_Controller *c,
742 const struct GNUNET_TESTBED_SlaveConfiguration * msg)
744 struct OperationContext *opc;
747 struct GNUNET_TESTBED_EventInformation event;
749 op_id = GNUNET_ntohll (msg->operation_id);
750 if (NULL == (opc = find_opc (c, op_id)))
752 LOG_DEBUG ("Operation not found\n");
755 if (OP_GET_SLAVE_CONFIG != opc->type)
760 op_cls = ((struct GetSlaveConfigData *) opc->data)->op_cls;
761 GNUNET_free (opc->data);
763 opc->state = OPC_STATE_FINISHED;
764 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
765 if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
769 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (&msg->header);
770 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
771 event.details.operation_finished.generic = opc->data;
772 event.details.operation_finished.operation = opc->op;
773 event.details.operation_finished.op_cls = op_cls;
774 event.details.operation_finished.emsg = NULL;
775 c->cc (c->cc_cls, &event);
782 * Handler for messages from controller (testbed service)
784 * @param cls the controller handler
785 * @param msg message received, NULL on timeout or fatal error
788 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
790 struct GNUNET_TESTBED_Controller *c = cls;
794 c->in_receive = GNUNET_NO;
795 /* FIXME: Add checks for message integrity */
798 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
802 msize = ntohs (msg->size);
803 switch (ntohs (msg->type))
805 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
806 GNUNET_assert (msize >=
807 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
809 handle_addhostconfirm (c,
810 (const struct GNUNET_TESTBED_HostConfirmedMessage
813 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
814 GNUNET_assert (msize ==
816 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
820 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
823 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
824 GNUNET_assert (msize ==
826 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
828 handle_peer_create_success (c,
830 GNUNET_TESTBED_PeerCreateSuccessEventMessage
833 case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
834 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
836 handle_peer_event (c,
837 (const struct GNUNET_TESTBED_PeerEventMessage *)
841 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
842 GNUNET_assert (msize >=
844 GNUNET_TESTBED_PeerConfigurationInformationMessage));
846 handle_peer_config (c,
848 GNUNET_TESTBED_PeerConfigurationInformationMessage
851 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
852 GNUNET_assert (msize ==
853 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
855 handle_peer_conevent (c,
857 GNUNET_TESTBED_ConnectionEventMessage *) msg);
859 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT:
860 GNUNET_assert (msize >=
861 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
863 handle_op_fail_event (c,
865 GNUNET_TESTBED_OperationFailureEventMessage *)
868 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
869 GNUNET_assert (msize >
870 sizeof (struct GNUNET_TESTBED_SlaveConfiguration));
872 handle_slave_config (c, (const struct
873 GNUNET_TESTBED_SlaveConfiguration *) msg);
878 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
880 c->in_receive = GNUNET_YES;
881 GNUNET_CLIENT_receive (c->client, &message_handler, c,
882 GNUNET_TIME_UNIT_FOREVER_REL);
888 * Function called to notify a client about the connection begin ready to queue
889 * more data. "buf" will be NULL and "size" zero if the connection was closed
890 * for writing in the meantime.
893 * @param size number of bytes available in buf
894 * @param buf where the callee should write the message
895 * @return number of bytes written to buf
898 transmit_ready_notify (void *cls, size_t size, void *buf)
900 struct GNUNET_TESTBED_Controller *c = cls;
901 struct MessageQueue *mq_entry;
904 mq_entry = c->mq_head;
905 GNUNET_assert (NULL != mq_entry);
906 if ((0 == size) && (NULL == buf)) /* Timeout */
908 LOG_DEBUG ("Message sending timed out -- retrying\n");
910 GNUNET_CLIENT_notify_transmit_ready (c->client,
911 ntohs (mq_entry->msg->size),
912 TIMEOUT_REL, GNUNET_YES,
913 &transmit_ready_notify, c);
916 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
917 size = ntohs (mq_entry->msg->size);
918 memcpy (buf, mq_entry->msg, size);
919 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
920 ntohs (mq_entry->msg->type), size);
921 GNUNET_free (mq_entry->msg);
922 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
923 GNUNET_free (mq_entry);
924 mq_entry = c->mq_head;
925 if (NULL != mq_entry)
927 GNUNET_CLIENT_notify_transmit_ready (c->client,
928 ntohs (mq_entry->msg->size),
929 TIMEOUT_REL, GNUNET_YES,
930 &transmit_ready_notify, c);
931 if (GNUNET_NO == c->in_receive)
933 c->in_receive = GNUNET_YES;
934 GNUNET_CLIENT_receive (c->client, &message_handler, c,
935 GNUNET_TIME_UNIT_FOREVER_REL);
942 * Queues a message in send queue for sending to the service
944 * @param controller the handle to the controller
945 * @param msg the message to queue
948 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
949 struct GNUNET_MessageHeader *msg)
951 struct MessageQueue *mq_entry;
955 type = ntohs (msg->type);
956 size = ntohs (msg->size);
957 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
958 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
959 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Queueing message of type %u, size %u for sending\n", type,
964 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
966 if (NULL == controller->th)
968 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
969 TIMEOUT_REL, GNUNET_YES,
970 &transmit_ready_notify,
976 * Sends the given message as an operation. The given callback is called when a
977 * reply for the operation is available. Call
978 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
979 * operation context if the cc hasn't been called
981 * @param controller the controller to which the message has to be sent
982 * @param operation_id the operation id of the message
983 * @param msg the message to send
984 * @param cc the callback to call when reply is available
985 * @param cc_cls the closure for the above callback
986 * @return the operation context which can be used to cancel the forwarded
989 struct OperationContext *
990 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
991 *controller, uint64_t operation_id,
992 const struct GNUNET_MessageHeader *msg,
993 GNUNET_CLIENT_MessageHandler cc,
996 struct OperationContext *opc;
997 struct ForwardedOperationData *data;
998 struct GNUNET_MessageHeader *dup_msg;
1001 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
1003 data->cc_cls = cc_cls;
1004 opc = GNUNET_malloc (sizeof (struct OperationContext));
1005 opc->c = controller;
1006 opc->type = OP_FORWARDED;
1008 opc->id = operation_id;
1009 msize = ntohs (msg->size);
1010 dup_msg = GNUNET_malloc (msize);
1011 (void) memcpy (dup_msg, msg, msize);
1012 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
1013 GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
1020 * Function to cancel an operation created by simply forwarding an operation
1023 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
1026 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
1028 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1029 GNUNET_free (opc->data);
1035 * Handle for controller process
1037 struct GNUNET_TESTBED_ControllerProc
1040 * The process handle
1042 struct GNUNET_HELPER_Handle *helper;
1045 * The host where the helper is run
1047 struct GNUNET_TESTBED_Host *host;
1050 * The controller error callback
1052 GNUNET_TESTBED_ControllerStatusCallback cb;
1055 * The closure for the above callback
1060 * The send handle for the helper
1062 struct GNUNET_HELPER_SendHandle *shandle;
1065 * The message corresponding to send handle
1067 struct GNUNET_MessageHeader *msg;
1070 * The port number for ssh; used for helpers starting ssh
1075 * The ssh destination string; used for helpers starting ssh
1080 * The configuration of the running testbed service
1082 struct GNUNET_CONFIGURATION_Handle *cfg;
1088 * Functions with this signature are called whenever a
1089 * complete message is received by the tokenizer.
1091 * Do not call GNUNET_SERVER_mst_destroy in callback
1093 * @param cls closure
1094 * @param client identification of the client
1095 * @param message the actual message
1097 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1100 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1102 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1103 const struct GNUNET_TESTBED_HelperReply *msg;
1104 const char *hostname;
1107 uLongf xconfig_size;
1109 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1110 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1111 ntohs (msg->header.size));
1112 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1113 ntohs (msg->header.type));
1114 config_size = (uLongf) ntohs (msg->config_size);
1116 (uLongf) (ntohs (msg->header.size) -
1117 sizeof (struct GNUNET_TESTBED_HelperReply));
1118 config = GNUNET_malloc (config_size);
1119 GNUNET_assert (Z_OK ==
1120 uncompress ((Bytef *) config, &config_size,
1121 (const Bytef *) &msg[1], xconfig_size));
1122 GNUNET_assert (NULL == cp->cfg);
1123 cp->cfg = GNUNET_CONFIGURATION_create ();
1124 GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1125 (cp->cfg, config, config_size, GNUNET_NO));
1126 GNUNET_free (config);
1127 if ((NULL == cp->host) ||
1128 (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1129 hostname = "localhost";
1130 /* Change the hostname so that we can connect to it */
1131 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1133 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1139 * Continuation function from GNUNET_HELPER_send()
1141 * @param cls closure
1142 * @param result GNUNET_OK on success,
1143 * GNUNET_NO if helper process died
1144 * GNUNET_SYSERR during GNUNET_HELPER_stop
1147 clear_msg (void *cls, int result)
1149 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1151 GNUNET_assert (NULL != cp->shandle);
1153 GNUNET_free (cp->msg);
1158 * Callback that will be called when the helper process dies. This is not called
1159 * when the helper process is stoped using GNUNET_HELPER_stop()
1161 * @param cls the closure from GNUNET_HELPER_start()
1164 helper_exp_cb (void *cls)
1166 struct GNUNET_TESTBED_ControllerProc *cp = cls;
1167 GNUNET_TESTBED_ControllerStatusCallback cb;
1173 GNUNET_TESTBED_controller_stop (cp);
1175 cb (cb_cls, NULL, GNUNET_SYSERR);
1180 * Function to call to start a link-controllers type operation once all queues
1181 * the operation is part of declare that the operation can be activated.
1183 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1186 opstart_link_controllers (void *cls)
1188 struct OperationContext *opc = cls;
1189 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1191 GNUNET_assert (NULL != opc->data);
1194 opc->state = OPC_STATE_STARTED;
1195 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1196 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1201 * Callback which will be called when link-controllers type operation is released
1203 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1206 oprelease_link_controllers (void *cls)
1208 struct OperationContext *opc = cls;
1210 if (OPC_STATE_INIT == opc->state)
1211 GNUNET_free (opc->data);
1212 if (OPC_STATE_STARTED == opc->state)
1213 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1219 * Function to be called when get slave config operation is ready
1221 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1224 opstart_get_slave_config (void *cls)
1226 struct OperationContext *opc = cls;
1227 struct GetSlaveConfigData *data;
1228 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1232 msize = sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage);
1233 msg = GNUNET_malloc (msize);
1234 msg->header.size = htons (msize);
1235 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG);
1236 msg->operation_id = GNUNET_htonll (opc->id);
1237 msg->slave_id = htonl (data->slave_id);
1238 GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1239 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1240 opc->state = OPC_STATE_STARTED;
1245 * Function to be called when get slave config operation is cancelled or finished
1247 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1250 oprelease_get_slave_config (void *cls)
1252 struct OperationContext *opc = cls;
1256 case OPC_STATE_INIT:
1257 GNUNET_free (opc->data);
1259 case OPC_STATE_STARTED:
1260 GNUNET_free (opc->data);
1261 GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1263 case OPC_STATE_FINISHED:
1264 if (NULL != opc->data)
1265 GNUNET_CONFIGURATION_destroy (opc->data);
1273 * Starts a controller process at the host. FIXME: add controller start callback
1274 * with the configuration with which the controller is started
1276 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1277 * host when starting testbed controller at host
1278 * @param host the host where the controller has to be started; NULL for
1280 * @param cfg template configuration to use for the remote controller; the
1281 * remote controller will be started with a slightly modified
1282 * configuration (port numbers, unix domain sockets and service home
1283 * values are changed as per TESTING library on the remote host)
1284 * @param cb function called when the controller is successfully started or
1285 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1286 * called if cb is called with GNUNET_SYSERR as status. Will never be
1287 * called in the same task as 'GNUNET_TESTBED_controller_start'
1288 * (synchronous errors will be signalled by returning NULL). This
1289 * parameter cannot be NULL.
1290 * @param cls closure for above callbacks
1291 * @return the controller process handle, NULL on errors
1293 struct GNUNET_TESTBED_ControllerProc *
1294 GNUNET_TESTBED_controller_start (const char *controller_ip,
1295 struct GNUNET_TESTBED_Host *host,
1296 const struct GNUNET_CONFIGURATION_Handle *cfg,
1297 GNUNET_TESTBED_ControllerStatusCallback cb,
1300 struct GNUNET_TESTBED_ControllerProc *cp;
1301 struct GNUNET_TESTBED_HelperInit *msg;
1302 static char *const binary_argv[] = {
1303 HELPER_TESTBED_BINARY, NULL
1306 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1307 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1309 GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1310 &helper_mst, &helper_exp_cb, cp);
1313 char *remote_args[8];
1315 const char *username;
1316 const char *hostname;
1318 username = GNUNET_TESTBED_host_get_username_ (host);
1319 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1320 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1321 if (NULL == username)
1322 GNUNET_asprintf (&cp->dst, "%s", hostname);
1324 GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1325 LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1327 remote_args[argp++] = "ssh";
1328 remote_args[argp++] = "-p";
1329 remote_args[argp++] = cp->port;
1330 remote_args[argp++] = "-o";
1331 remote_args[argp++] = "BatchMode=yes";
1332 remote_args[argp++] = cp->dst;
1333 remote_args[argp++] = HELPER_TESTBED_BINARY_SSH;
1334 remote_args[argp++] = NULL;
1335 GNUNET_assert (argp == 8);
1337 GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1338 &helper_exp_cb, cp);
1340 if (NULL == cp->helper)
1342 GNUNET_free_non_null (cp->port);
1343 GNUNET_free_non_null (cp->dst);
1350 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1351 cp->msg = &msg->header;
1353 GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1354 if (NULL == cp->shandle)
1357 GNUNET_TESTBED_controller_stop (cp);
1365 * Stop the controller process (also will terminate all peers and controllers
1366 * dependent on this controller). This function blocks until the testbed has
1367 * been fully terminated (!).
1369 * @param cproc the controller process handle
1372 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1374 if (NULL != cproc->shandle)
1375 GNUNET_HELPER_send_cancel (cproc->shandle);
1376 if (NULL != cproc->helper)
1377 GNUNET_HELPER_stop (cproc->helper);
1378 if (NULL != cproc->cfg)
1379 GNUNET_CONFIGURATION_destroy (cproc->cfg);
1380 GNUNET_free_non_null (cproc->port);
1381 GNUNET_free_non_null (cproc->dst);
1382 GNUNET_free (cproc);
1387 * Start a controller process using the given configuration at the
1390 * @param cfg configuration to use
1391 * @param host host to run the controller on; This should be the same host if
1392 * the controller was previously started with
1393 * GNUNET_TESTBED_controller_start; NULL for localhost
1394 * @param event_mask bit mask with set of events to call 'cc' for;
1395 * or-ed values of "1LL" shifted by the
1396 * respective 'enum GNUNET_TESTBED_EventType'
1397 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1398 * @param cc controller callback to invoke on events
1399 * @param cc_cls closure for cc
1400 * @return handle to the controller
1402 struct GNUNET_TESTBED_Controller *
1403 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1404 *cfg, struct GNUNET_TESTBED_Host *host,
1405 uint64_t event_mask,
1406 GNUNET_TESTBED_ControllerCallback cc,
1409 struct GNUNET_TESTBED_Controller *controller;
1410 struct GNUNET_TESTBED_InitMessage *msg;
1411 const char *controller_hostname;
1412 unsigned long long max_parallel_operations;
1413 unsigned long long max_parallel_service_connections;
1414 unsigned long long max_parallel_topology_config_operations;
1417 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1418 "MAX_PARALLEL_OPERATIONS",
1419 &max_parallel_operations))
1425 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1426 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1427 &max_parallel_service_connections))
1433 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1434 "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1435 &max_parallel_topology_config_operations))
1440 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1441 controller->cc = cc;
1442 controller->cc_cls = cc_cls;
1443 controller->event_mask = event_mask;
1444 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1445 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1446 if (NULL == controller->client)
1448 GNUNET_TESTBED_controller_disconnect (controller);
1453 host = GNUNET_TESTBED_host_create_by_id_ (0);
1454 if (NULL == host) /* If the above host create fails */
1456 LOG (GNUNET_ERROR_TYPE_WARNING,
1457 "Treating NULL host as localhost. Multiple references to localhost "
1458 "may break when localhost freed before calling disconnect \n");
1459 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1463 controller->aux_host = GNUNET_YES;
1466 GNUNET_assert (NULL != host);
1467 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1468 controller->host = host;
1469 controller->opq_parallel_operations =
1470 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1471 max_parallel_operations);
1472 controller->opq_parallel_service_connections =
1473 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1474 max_parallel_service_connections);
1475 controller->opq_parallel_topology_config_operations=
1476 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1477 max_parallel_service_connections);
1478 controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1479 if (NULL == controller_hostname)
1480 controller_hostname = "127.0.0.1";
1482 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1483 strlen (controller_hostname) + 1);
1484 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1486 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1487 strlen (controller_hostname) + 1);
1488 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1489 msg->event_mask = GNUNET_htonll (controller->event_mask);
1490 strcpy ((char *) &msg[1], controller_hostname);
1491 GNUNET_TESTBED_queue_message_ (controller,
1492 (struct GNUNET_MessageHeader *) msg);
1498 * Configure shared services at a controller. Using this function,
1499 * you can specify that certain services (such as "resolver")
1500 * should not be run for each peer but instead be shared
1501 * across N peers on the specified host. This function
1502 * must be called before any peers are created at the host.
1504 * @param controller controller to configure
1505 * @param service_name name of the service to share
1506 * @param num_peers number of peers that should share one instance
1507 * of the specified service (1 for no sharing is the default),
1508 * use 0 to disable the service
1511 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1513 const char *service_name,
1516 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1517 uint16_t service_name_size;
1520 service_name_size = strlen (service_name) + 1;
1522 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1524 msg = GNUNET_malloc (msg_size);
1525 msg->header.size = htons (msg_size);
1526 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1527 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1528 msg->num_peers = htonl (num_peers);
1529 memcpy (&msg[1], service_name, service_name_size);
1530 GNUNET_TESTBED_queue_message_ (controller,
1531 (struct GNUNET_MessageHeader *) msg);
1536 * disconnects from the controller.
1538 * @param controller handle to controller to stop
1541 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1544 struct MessageQueue *mq_entry;
1546 if (NULL != controller->th)
1547 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1548 /* Clear the message queue */
1549 while (NULL != (mq_entry = controller->mq_head))
1551 GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1553 GNUNET_free (mq_entry->msg);
1554 GNUNET_free (mq_entry);
1556 if (NULL != controller->client)
1557 GNUNET_CLIENT_disconnect (controller->client);
1558 GNUNET_CONFIGURATION_destroy (controller->cfg);
1559 if (GNUNET_YES == controller->aux_host)
1560 GNUNET_TESTBED_host_destroy (controller->host);
1561 GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1562 GNUNET_TESTBED_operation_queue_destroy_
1563 (controller->opq_parallel_service_connections);
1564 GNUNET_TESTBED_operation_queue_destroy_
1565 (controller->opq_parallel_topology_config_operations);
1566 GNUNET_free (controller);
1571 * Register a host with the controller
1573 * @param controller the controller handle
1574 * @param host the host to register
1575 * @param cc the completion callback to call to inform the status of
1576 * registration. After calling this callback the registration handle
1577 * will be invalid. Cannot be NULL.
1578 * @param cc_cls the closure for the cc
1579 * @return handle to the host registration which can be used to cancel the
1582 struct GNUNET_TESTBED_HostRegistrationHandle *
1583 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1584 struct GNUNET_TESTBED_Host *host,
1585 GNUNET_TESTBED_HostRegistrationCompletion cc,
1588 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1589 struct GNUNET_TESTBED_AddHostMessage *msg;
1590 const char *username;
1591 const char *hostname;
1593 uint16_t user_name_length;
1595 if (NULL != controller->rh)
1597 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1598 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1600 LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1601 (NULL == hostname) ? "localhost" : hostname);
1604 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1607 GNUNET_assert (NULL != cc);
1609 rh->cc_cls = cc_cls;
1610 controller->rh = rh;
1611 username = GNUNET_TESTBED_host_get_username_ (host);
1612 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1613 user_name_length = 0;
1614 if (NULL != username)
1616 user_name_length = strlen (username) + 1;
1617 msg_size += user_name_length;
1619 /* FIXME: what happens when hostname is NULL? localhost */
1620 GNUNET_assert (NULL != hostname);
1621 msg_size += strlen (hostname) + 1;
1622 msg = GNUNET_malloc (msg_size);
1623 msg->header.size = htons (msg_size);
1624 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1625 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1626 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1627 msg->user_name_length = htons (user_name_length);
1628 if (NULL != username)
1629 memcpy (&msg[1], username, user_name_length);
1630 strcpy (((void *) &msg[1]) + user_name_length, hostname);
1631 GNUNET_TESTBED_queue_message_ (controller,
1632 (struct GNUNET_MessageHeader *) msg);
1638 * Cancel the pending registration. Note that if the registration message is
1639 * already sent to the service the cancellation has only the effect that the
1640 * registration completion callback for the registration is never called.
1642 * @param handle the registration handle to cancel
1645 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1648 if (handle != handle->c->rh)
1653 handle->c->rh = NULL;
1654 GNUNET_free (handle);
1659 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1660 * serialized and compressed
1662 * @param master handle to the master controller who creates the association
1663 * @param delegated_host requests to which host should be delegated; cannot be NULL
1664 * @param slave_host which host is used to run the slave controller; use NULL to
1665 * make the master controller connect to the delegated host
1666 * @param sxcfg serialized and compressed configuration
1667 * @param sxcfg_size the size scfg
1668 * @param scfg_size the size of uncompressed serialized configuration
1669 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1670 * be started by the master controller; GNUNET_NO if we are just
1671 * allowed to use the slave via TCP/IP
1673 struct GNUNET_TESTBED_Operation *
1674 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1675 struct GNUNET_TESTBED_Host *delegated_host,
1676 struct GNUNET_TESTBED_Host *slave_host,
1677 const char *sxcfg, size_t sxcfg_size,
1678 size_t scfg_size, int is_subordinate)
1680 struct OperationContext *opc;
1681 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1684 GNUNET_assert (GNUNET_YES ==
1685 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1686 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1687 GNUNET_assert (GNUNET_YES ==
1688 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1689 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1690 msg = GNUNET_malloc (msg_size);
1691 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1692 msg->header.size = htons (msg_size);
1693 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1694 msg->slave_host_id =
1695 htonl (GNUNET_TESTBED_host_get_id_
1696 ((NULL != slave_host) ? slave_host : master->host));
1697 msg->config_size = htons ((uint16_t) scfg_size);
1698 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1699 memcpy (&msg[1], sxcfg, sxcfg_size);
1700 opc = GNUNET_malloc (sizeof (struct OperationContext));
1703 opc->type = OP_LINK_CONTROLLERS;
1704 opc->id = master->operation_counter++;
1705 opc->state = OPC_STATE_INIT;
1706 msg->operation_id = GNUNET_htonll (opc->id);
1708 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1709 &oprelease_link_controllers);
1710 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1717 * Compresses given configuration using zlib compress
1719 * @param config the serialized configuration
1720 * @param size the size of config
1721 * @param xconfig will be set to the compressed configuration (memory is fresly
1723 * @return the size of the xconfig
1726 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1731 xsize = compressBound ((uLong) size);
1732 *xconfig = GNUNET_malloc (xsize);
1733 GNUNET_assert (Z_OK ==
1734 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1735 (const Bytef *) config, (uLongf) size,
1742 * Create a link from slave controller to delegated controller. Whenever the
1743 * master controller is asked to start a peer at the delegated controller the
1744 * request will be routed towards slave controller (if a route exists). The
1745 * slave controller will then route it to the delegated controller. The
1746 * configuration of the slave controller is given and to be used to either
1747 * create the slave controller or to connect to an existing slave controller
1748 * process. 'is_subordinate' specifies if the given slave controller should be
1749 * started and managed by the master controller, or if the slave already has a
1750 * master and this is just a secondary master that is also allowed to use the
1753 * @param master handle to the master controller who creates the association
1754 * @param delegated_host requests to which host should be delegated
1755 * @param slave_host which host is used to run the slave controller
1756 * @param slave_cfg configuration to use for the slave controller
1757 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1758 * by the master controller; GNUNET_NO if we are just
1759 * allowed to use the slave via TCP/IP
1760 * @return the operation handle
1762 struct GNUNET_TESTBED_Operation *
1763 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1764 struct GNUNET_TESTBED_Host *delegated_host,
1765 struct GNUNET_TESTBED_Host *slave_host,
1766 const struct GNUNET_CONFIGURATION_Handle
1767 *slave_cfg, int is_subordinate)
1769 struct GNUNET_TESTBED_Operation *op;
1775 GNUNET_assert (GNUNET_YES ==
1776 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1777 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1778 GNUNET_assert (GNUNET_YES ==
1779 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1780 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1781 cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1782 GNUNET_free (config);
1783 /* Configuration doesn't fit in 1 message */
1784 GNUNET_assert ((UINT16_MAX -
1785 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >= cc_size);
1786 op = GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1787 (const char *) cconfig, cc_size,
1788 config_size, is_subordinate);
1789 GNUNET_free (cconfig);
1795 * Function to acquire the configuration of a running slave controller. The
1796 * completion of the operation is signalled through the controller_cb from
1797 * GNUNET_TESTBED_controller_connect(). If the operation is successful the
1798 * handle to the configuration is available in the generic pointer of
1799 * operation_finished field of struct GNUNET_TESTBED_EventInformation.
1801 * @param op_cls the closure for the operation
1802 * @param master the handle to master controller
1803 * @param slave_host the host where the slave controller is running; the handle
1804 * to the slave_host should remain valid until this operation is
1805 * cancelled or marked as finished
1806 * @return the operation handle; NULL if the slave_host is not registered at
1809 struct GNUNET_TESTBED_Operation *
1810 GNUNET_TESTBED_get_slave_config (void *op_cls,
1811 struct GNUNET_TESTBED_Controller *master,
1812 struct GNUNET_TESTBED_Host *slave_host)
1814 struct OperationContext *opc;
1815 struct GetSlaveConfigData *data;
1817 if (GNUNET_NO == GNUNET_TESTBED_is_host_registered_ (slave_host, master))
1819 data = GNUNET_malloc (sizeof (struct GetSlaveConfigData));
1820 data->slave_id = GNUNET_TESTBED_host_get_id_ (slave_host);
1821 data->op_cls = op_cls;
1822 opc = GNUNET_malloc (sizeof (struct OperationContext));
1823 opc->state = OPC_STATE_INIT;
1825 opc->id = master->operation_counter++;
1826 opc->type = OP_GET_SLAVE_CONFIG;
1829 GNUNET_TESTBED_operation_create_ (opc, &opstart_get_slave_config,
1830 &oprelease_get_slave_config);
1831 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1838 * Ask the testbed controller to write the current overlay topology to
1839 * a file. Naturally, the file will only contain a snapshot as the
1840 * topology may evolve all the time.
1842 * @param controller overlay controller to inspect
1843 * @param filename name of the file the topology should
1847 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1849 const char *filename)
1856 * Creates a helper initialization message. This function is here because we
1857 * want to use this in testing
1859 * @param cname the ip address of the controlling host
1860 * @param cfg the configuration that has to used to start the testbed service
1862 * @return the initialization message
1864 struct GNUNET_TESTBED_HelperInit *
1865 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1866 const struct GNUNET_CONFIGURATION_Handle
1869 struct GNUNET_TESTBED_HelperInit *msg;
1873 size_t xconfig_size;
1877 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1878 GNUNET_assert (NULL != config);
1880 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1881 GNUNET_free (config);
1882 cname_len = strlen (cname);
1884 xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
1885 msg = GNUNET_realloc (xconfig, msg_size);
1886 (void) memmove (((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1887 msg->header.size = htons (msg_size);
1888 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1889 msg->cname_size = htons (cname_len);
1890 msg->config_size = htons (config_size);
1891 (void) strcpy ((char *) &msg[1], cname);
1897 * Cancel a pending operation. Releases all resources
1898 * of the operation and will ensure that no event
1899 * is generated for the operation. Does NOT guarantee
1900 * that the operation will be fully undone (or that
1901 * nothing ever happened).
1903 * @param operation operation to cancel
1906 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1908 GNUNET_TESTBED_operation_done (operation);
1913 * Signal that the information from an operation has been fully
1914 * processed. This function MUST be called for each event
1915 * of type 'operation_finished' to fully remove the operation
1916 * from the operation queue. After calling this function, the
1917 * 'op_result' becomes invalid (!).
1919 * @param operation operation to signal completion for
1922 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1924 switch (operation->type)
1926 case OP_PEER_CREATE:
1927 case OP_PEER_DESTROY:
1931 case OP_OVERLAY_CONNECT:
1932 case OP_LINK_CONTROLLERS:
1933 GNUNET_TESTBED_operation_release_ (operation);
1943 * Generates configuration by parsing Peer configuration information reply message
1945 * @param msg the message containing compressed configuration. This message
1946 * should be of the following types: GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
1947 * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
1948 * @return handle to the parsed configuration
1950 struct GNUNET_CONFIGURATION_Handle *
1951 GNUNET_TESTBED_get_config_from_peerinfo_msg_ (const struct GNUNET_MessageHeader *msg)
1953 struct GNUNET_CONFIGURATION_Handle *cfg;
1960 switch (ntohs (msg->type))
1962 case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
1964 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
1966 imsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
1968 data_len = (uLong) ntohs (imsg->config_size);
1969 xdata_len = ntohs (imsg->header.size)
1970 - sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1971 xdata = (const Bytef *) &imsg[1];
1974 case GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG:
1976 const struct GNUNET_TESTBED_SlaveConfiguration *imsg;
1978 imsg = (const struct GNUNET_TESTBED_SlaveConfiguration *) msg;
1979 data_len = (uLong) ntohs (imsg->config_size);
1980 xdata_len = ntohs (imsg->header.size)
1981 - sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
1982 xdata = (const Bytef *) &imsg[1];
1988 data = GNUNET_malloc (data_len);
1991 uncompress (data, &data_len, xdata, xdata_len)))
1993 cfg = GNUNET_CONFIGURATION_create ();
1994 GNUNET_assert (GNUNET_OK ==
1995 GNUNET_CONFIGURATION_deserialize (cfg, (const char *) data,
2004 * Checks the integrity of the OperationFailureEventMessage and if good returns
2005 * the error message it contains.
2007 * @param msg the OperationFailureEventMessage
2008 * @return the error message
2011 GNUNET_TESTBED_parse_error_string_ (const struct
2012 GNUNET_TESTBED_OperationFailureEventMessage
2018 msize = ntohs (msg->header.size);
2019 if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
2021 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
2022 emsg = (const char *) &msg[1];
2023 if ('\0' != emsg[msize])
2031 /* end of testbed_api.c */