2 This file is part of GNUnet
3 (C) 2008--2013 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"
44 #include "testbed_api_sd.h"
47 * Generic logging shorthand
49 #define LOG(kind, ...) \
50 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__)
55 #define LOG_DEBUG(...) \
56 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
59 * Relative time seconds shorthand
61 #define TIME_REL_SECS(sec) \
62 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
66 * Default server message sending retry timeout
68 #define TIMEOUT_REL TIME_REL_SECS(1)
72 * The message queue for sending messages to the controller service
77 * The message to be sent
79 struct GNUNET_MessageHeader *msg;
82 * next pointer for DLL
84 struct MessageQueue *next;
87 * prev pointer for DLL
89 struct MessageQueue *prev;
94 * Context data for forwarded Operation
96 struct ForwardedOperationData
100 * The callback to call when reply is available
102 GNUNET_CLIENT_MessageHandler cc;
105 * The closure for the above callback
113 * Context data for get slave config operations
115 struct GetSlaveConfigData
118 * The id of the slave controller
126 * Context data for controller link operations
128 struct ControllerLinkData
131 * The controller link message
133 struct GNUNET_TESTBED_ControllerLinkRequest *msg;
136 * The id of the host which is hosting the controller to be linked
144 * Date context for OP_SHUTDOWN_PEERS operations
146 struct ShutdownPeersData
149 * The operation completion callback to call
151 GNUNET_TESTBED_OperationCompletionCallback cb;
154 * The closure for the above callback
161 * An entry in the stack for keeping operations which are about to expire
163 struct ExpireOperationEntry
166 * DLL head; new entries are to be inserted here
168 struct ExpireOperationEntry *next;
171 * DLL tail; entries are deleted from here
173 struct ExpireOperationEntry *prev;
176 * The operation. This will be a dangling pointer when the operation is freed
178 const struct GNUNET_TESTBED_Operation *op;
183 * DLL head for list of operations marked for expiry
185 static struct ExpireOperationEntry *exop_head;
188 * DLL tail for list of operation marked for expiry
190 static struct ExpireOperationEntry *exop_tail;
194 * Inserts an operation into the list of operations marked for expiry
196 * @param op the operation to insert
199 exop_insert (struct GNUNET_TESTBED_Operation *op)
201 struct ExpireOperationEntry *entry;
203 entry = GNUNET_malloc (sizeof (struct ExpireOperationEntry));
205 GNUNET_CONTAINER_DLL_insert_tail (exop_head, exop_tail, entry);
210 * Checks if an operation is present in the list of operations marked for
211 * expiry. If the operation is found, it and the tail of operations after it
212 * are removed from the list.
214 * @param op the operation to check
215 * @return GNUNET_NO if the operation is not present in the list; GNUNET_YES if
216 * the operation is found in the list (the operation is then removed
217 * from the list -- calling this function again with the same
218 * paramenter will return GNUNET_NO)
221 exop_check (const struct GNUNET_TESTBED_Operation *const op)
223 struct ExpireOperationEntry *entry;
224 struct ExpireOperationEntry *entry2;
229 while (NULL != entry)
238 if (GNUNET_NO == found)
240 /* Truncate the tail */
241 while (NULL != entry)
243 entry2 = entry->next;
244 GNUNET_CONTAINER_DLL_remove (exop_head, exop_tail, entry);
253 * Context information to be used while searching for operation contexts
258 * The result of the search
260 struct OperationContext *opc;
263 * The id of the operation context we are searching for
270 * Search iterator for searching an operation context
272 * @param cls the serach context
273 * @param key current key code
274 * @param value value in the hash map
275 * @return GNUNET_YES if we should continue to
280 opc_search_iterator (void *cls, uint32_t key, void *value)
282 struct SearchContext *sc = cls;
283 struct OperationContext *opc = value;
285 GNUNET_assert (NULL != opc);
286 GNUNET_assert (NULL == sc->opc);
287 if (opc->id != sc->id)
295 * Returns the operation context with the given id if found in the Operation
296 * context queues of the controller
298 * @param c the controller whose operation context map is searched
299 * @param id the id which has to be checked
300 * @return the matching operation context; NULL if no match found
302 static struct OperationContext *
303 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
305 struct SearchContext sc;
309 GNUNET_assert (NULL != c->opc_map);
311 GNUNET_CONTAINER_multihashmap32_get_multiple (c->opc_map, (uint32_t) id,
312 &opc_search_iterator, &sc))
319 * Inserts the given operation context into the operation context map of the
320 * given controller. Creates the operation context map if one does not exist
323 * @param c the controller
324 * @param opc the operation context to be inserted
327 GNUNET_TESTBED_insert_opc_ (struct GNUNET_TESTBED_Controller *c,
328 struct OperationContext *opc)
330 if (NULL == c->opc_map)
331 c->opc_map = GNUNET_CONTAINER_multihashmap32_create (256);
332 GNUNET_assert (GNUNET_OK ==
333 GNUNET_CONTAINER_multihashmap32_put (c->opc_map,
334 (uint32_t) opc->id, opc,
335 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
340 * Removes the given operation context from the operation context map of the
343 * @param c the controller
344 * @param opc the operation context to remove
347 GNUNET_TESTBED_remove_opc_ (const struct GNUNET_TESTBED_Controller *c,
348 struct OperationContext *opc)
350 GNUNET_assert (NULL != c->opc_map);
351 GNUNET_assert (GNUNET_YES ==
352 GNUNET_CONTAINER_multihashmap32_remove (c->opc_map,
359 * Handler for forwarded operations
361 * @param c the controller handle
362 * @param opc the opearation context
363 * @param msg the message
366 handle_forwarded_operation_msg (struct GNUNET_TESTBED_Controller *c,
367 struct OperationContext *opc,
368 const struct GNUNET_MessageHeader *msg)
370 struct ForwardedOperationData *fo_data;
373 if (NULL != fo_data->cc)
374 fo_data->cc (fo_data->cc_cls, msg);
375 GNUNET_TESTBED_remove_opc_ (c, opc);
376 GNUNET_free (fo_data);
382 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
383 * controller (testbed service)
385 * @param c the controller handler
386 * @param msg message received
387 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
391 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
393 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
395 struct OperationContext *opc;
396 GNUNET_TESTBED_OperationCompletionCallback op_comp_cb;
397 void *op_comp_cb_cls;
398 struct GNUNET_TESTBED_EventInformation event;
401 op_id = GNUNET_ntohll (msg->operation_id);
402 LOG_DEBUG ("Operation %lu successful\n", op_id);
403 if (NULL == (opc = find_opc (c, op_id)))
405 LOG_DEBUG ("Operation not found\n");
408 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
410 event.op_cls = opc->op_cls;
411 event.details.operation_finished.emsg = NULL;
412 event.details.operation_finished.generic = NULL;
414 op_comp_cb_cls = NULL;
419 handle_forwarded_operation_msg (c, opc,
420 (const struct GNUNET_MessageHeader *) msg);
424 case OP_PEER_DESTROY:
426 struct GNUNET_TESTBED_Peer *peer;
429 GNUNET_TESTBED_peer_deregister_ (peer);
435 case OP_SHUTDOWN_PEERS:
437 struct ShutdownPeersData *data;
440 op_comp_cb = data->cb;
441 op_comp_cb_cls = data->cb_cls;
444 GNUNET_TESTBED_cleanup_peers_ ();
447 case OP_MANAGE_SERVICE:
449 struct ManageServiceData *data;
451 GNUNET_assert (NULL != (data = opc->data));
452 op_comp_cb = data->cb;
453 op_comp_cb_cls = data->cb_cls;
458 case OP_PEER_RECONFIGURE:
463 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
464 opc->state = OPC_STATE_FINISHED;
465 exop_insert (event.op);
466 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
469 c->cc (c->cc_cls, &event);
470 if (GNUNET_NO == exop_check (event.op))
474 LOG_DEBUG ("Not calling callback\n");
475 if (NULL != op_comp_cb)
476 op_comp_cb (op_comp_cb_cls, event.op, NULL);
477 /* You could have marked the operation as done by now */
478 GNUNET_break (GNUNET_NO == exop_check (event.op));
484 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
485 * controller (testbed service)
487 * @param c the controller handle
488 * @param msg message received
489 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
493 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
495 GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
497 struct OperationContext *opc;
498 struct PeerCreateData *data;
499 struct GNUNET_TESTBED_Peer *peer;
500 struct GNUNET_TESTBED_Operation *op;
501 GNUNET_TESTBED_PeerCreateCallback cb;
505 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
506 ntohs (msg->header.size));
507 op_id = GNUNET_ntohll (msg->operation_id);
508 if (NULL == (opc = find_opc (c, op_id)))
510 LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
513 if (OP_FORWARDED == opc->type)
515 handle_forwarded_operation_msg (c, opc,
516 (const struct GNUNET_MessageHeader *) msg);
519 GNUNET_assert (OP_PEER_CREATE == opc->type);
520 GNUNET_assert (NULL != opc->data);
522 GNUNET_assert (NULL != data->peer);
524 GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
525 peer->state = PS_CREATED;
526 GNUNET_TESTBED_peer_register_ (peer);
530 GNUNET_free (opc->data);
531 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
532 opc->state = OPC_STATE_FINISHED;
535 cb (cls, peer, NULL);
536 /* You could have marked the operation as done by now */
537 GNUNET_break (GNUNET_NO == exop_check (op));
543 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
544 * controller (testbed service)
546 * @param c the controller handler
547 * @param msg message received
548 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
552 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
553 const struct GNUNET_TESTBED_PeerEventMessage *msg)
555 struct OperationContext *opc;
556 struct GNUNET_TESTBED_Peer *peer;
557 struct PeerEventData *data;
558 GNUNET_TESTBED_PeerChurnCallback pcc;
560 struct GNUNET_TESTBED_EventInformation event;
564 GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
565 ntohs (msg->header.size));
566 op_id = GNUNET_ntohll (msg->operation_id);
567 if (NULL == (opc = find_opc (c, op_id)))
569 LOG_DEBUG ("Operation not found\n");
572 if (OP_FORWARDED == opc->type)
574 handle_forwarded_operation_msg (c, opc,
575 (const struct GNUNET_MessageHeader *) msg);
578 GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
580 GNUNET_assert (NULL != data);
582 GNUNET_assert (NULL != peer);
583 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
585 event.op_cls = opc->op_cls;
588 case GNUNET_TESTBED_ET_PEER_START:
589 peer->state = PS_STARTED;
590 event.details.peer_start.host = peer->host;
591 event.details.peer_start.peer = peer;
593 case GNUNET_TESTBED_ET_PEER_STOP:
594 peer->state = PS_STOPPED;
595 event.details.peer_stop.peer = peer;
598 GNUNET_assert (0); /* We should never reach this state */
601 pcc_cls = data->pcc_cls;
603 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
604 opc->state = OPC_STATE_FINISHED;
605 exop_insert (event.op);
606 mask = 1LL << GNUNET_TESTBED_ET_PEER_START;
607 mask |= 1LL << GNUNET_TESTBED_ET_PEER_STOP;
608 if (0 != (mask & c->event_mask))
611 c->cc (c->cc_cls, &event);
612 if (GNUNET_NO == exop_check (event.op))
617 /* You could have marked the operation as done by now */
618 GNUNET_break (GNUNET_NO == exop_check (event.op));
624 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
625 * controller (testbed service)
627 * @param c the controller handler
628 * @param msg message received
629 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
633 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
634 const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
636 struct OperationContext *opc;
637 struct OverlayConnectData *data;
638 GNUNET_TESTBED_OperationCompletionCallback cb;
640 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 if (OP_FORWARDED == opc->type)
652 handle_forwarded_operation_msg (c, opc,
653 (const struct GNUNET_MessageHeader *) msg);
656 GNUNET_assert (OP_OVERLAY_CONNECT == opc->type);
657 GNUNET_assert (NULL != (data = opc->data));
658 GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
659 (ntohl (msg->peer2) == data->p2->unique_id));
660 event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
662 event.op_cls = opc->op_cls;
665 case GNUNET_TESTBED_ET_CONNECT:
666 event.details.peer_connect.peer1 = data->p1;
667 event.details.peer_connect.peer2 = data->p2;
669 case GNUNET_TESTBED_ET_DISCONNECT:
670 GNUNET_assert (0); /* FIXME: implement */
673 GNUNET_assert (0); /* Should never reach here */
677 cb_cls = data->cb_cls;
678 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
679 opc->state = OPC_STATE_FINISHED;
680 exop_insert (event.op);
681 mask = 1LL << GNUNET_TESTBED_ET_CONNECT;
682 mask |= 1LL << GNUNET_TESTBED_ET_DISCONNECT;
683 if (0 != (mask & c->event_mask))
686 c->cc (c->cc_cls, &event);
687 if (GNUNET_NO == exop_check (event.op))
691 cb (cb_cls, opc->op, NULL);
692 /* You could have marked the operation as done by now */
693 GNUNET_break (GNUNET_NO == exop_check (event.op));
699 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
700 * controller (testbed service)
702 * @param c the controller handler
703 * @param msg message received
704 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
708 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
710 GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
712 struct OperationContext *opc;
713 struct GNUNET_TESTBED_Peer *peer;
714 struct PeerInfoData *data;
715 struct GNUNET_TESTBED_PeerInformation *pinfo;
716 GNUNET_TESTBED_PeerInfoCallback cb;
720 op_id = GNUNET_ntohll (msg->operation_id);
721 if (NULL == (opc = find_opc (c, op_id)))
723 LOG_DEBUG ("Operation not found\n");
726 if (OP_FORWARDED == opc->type)
728 handle_forwarded_operation_msg (c, opc,
729 (const struct GNUNET_MessageHeader *) msg);
733 GNUNET_assert (NULL != data);
735 GNUNET_assert (NULL != peer);
736 GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
737 pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
738 pinfo->pit = data->pit;
740 cb_cls = data->cb_cls;
741 GNUNET_assert (NULL != cb);
746 case GNUNET_TESTBED_PIT_IDENTITY:
747 pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
748 (void) memcpy (pinfo->result.id, &msg->peer_identity,
749 sizeof (struct GNUNET_PeerIdentity));
751 case GNUNET_TESTBED_PIT_CONFIGURATION:
752 pinfo->result.cfg = /* Freed in oprelease_peer_getinfo */
753 GNUNET_TESTBED_extract_config_ (&msg->header);
755 case GNUNET_TESTBED_PIT_GENERIC:
756 GNUNET_assert (0); /* never reach here */
760 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
761 opc->state = OPC_STATE_FINISHED;
762 cb (cb_cls, opc->op, pinfo, NULL);
763 /* We dont check whether the operation is marked as done here as the
764 operation contains data (cfg/identify) which will be freed at a later point
771 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
772 * controller (testbed service)
774 * @param c the controller handler
775 * @param msg message received
776 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
780 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
781 const struct GNUNET_TESTBED_OperationFailureEventMessage
784 struct OperationContext *opc;
788 struct GNUNET_TESTBED_EventInformation event;
790 op_id = GNUNET_ntohll (msg->operation_id);
791 if (NULL == (opc = find_opc (c, op_id)))
793 LOG_DEBUG ("Operation not found\n");
796 if (OP_FORWARDED == opc->type)
798 handle_forwarded_operation_msg (c, opc,
799 (const struct GNUNET_MessageHeader *) msg);
802 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
803 opc->state = OPC_STATE_FINISHED;
804 emsg = GNUNET_TESTBED_parse_error_string_ (msg);
806 emsg = "Unknown error";
807 if (OP_PEER_INFO == opc->type)
809 struct PeerInfoData *data;
812 if (NULL != data->cb)
813 data->cb (data->cb_cls, opc->op, NULL, emsg);
815 return GNUNET_YES; /* We do not call controller callback for peer info */
817 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
819 event.op_cls = opc->op_cls;
820 event.details.operation_finished.emsg = emsg;
821 event.details.operation_finished.generic = NULL;
822 mask = (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
823 if ((0 != (mask & c->event_mask)) && (NULL != c->cc))
825 exop_insert (event.op);
826 c->cc (c->cc_cls, &event);
827 if (GNUNET_NO == exop_check (event.op))
834 struct PeerCreateData *data;
837 GNUNET_free (data->peer);
838 if (NULL != data->cb)
839 data->cb (data->cls, NULL, emsg);
846 struct PeerEventData *data;
849 if (NULL != data->pcc)
850 data->pcc (data->pcc_cls, emsg);
854 case OP_PEER_DESTROY:
858 case OP_OVERLAY_CONNECT:
860 struct OverlayConnectData *data;
863 data->failed = GNUNET_YES;
864 if (NULL != data->cb)
865 data->cb (data->cb_cls, opc->op, emsg);
870 case OP_LINK_CONTROLLERS: /* No secondary callback */
872 case OP_SHUTDOWN_PEERS:
874 struct ShutdownPeersData *data;
877 GNUNET_free (data); /* FIXME: Decide whether we call data->op_cb */
881 case OP_MANAGE_SERVICE:
883 struct ManageServiceData *data = opc->data;
884 GNUNET_TESTBED_OperationCompletionCallback cb;
887 GNUNET_assert (NULL != data);
889 cb_cls = data->cb_cls;
892 exop_insert (event.op);
894 cb (cb_cls, opc->op, emsg);
895 /* You could have marked the operation as done by now */
896 GNUNET_break (GNUNET_NO == exop_check (event.op));
907 * Function to build GET_SLAVE_CONFIG message
909 * @param op_id the id this message should contain in its operation id field
910 * @param slave_id the id this message should contain in its slave id field
911 * @return newly allocated SlaveGetConfigurationMessage
913 static struct GNUNET_TESTBED_SlaveGetConfigurationMessage *
914 GNUNET_TESTBED_generate_slavegetconfig_msg_ (uint64_t op_id, uint32_t slave_id)
916 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
919 msize = sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage);
920 msg = GNUNET_malloc (msize);
921 msg->header.size = htons (msize);
923 htons (GNUNET_MESSAGE_TYPE_TESTBED_GET_SLAVE_CONFIGURATION);
924 msg->operation_id = GNUNET_htonll (op_id);
925 msg->slave_id = htonl (slave_id);
931 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG message from controller
934 * @param c the controller handler
935 * @param msg message received
936 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
940 handle_slave_config (struct GNUNET_TESTBED_Controller *c,
941 const struct GNUNET_TESTBED_SlaveConfiguration *msg)
943 struct OperationContext *opc;
946 struct GNUNET_TESTBED_EventInformation event;
948 op_id = GNUNET_ntohll (msg->operation_id);
949 if (NULL == (opc = find_opc (c, op_id)))
951 LOG_DEBUG ("Operation not found\n");
954 if (OP_GET_SLAVE_CONFIG != opc->type)
959 opc->state = OPC_STATE_FINISHED;
960 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
961 mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
962 if ((0 != (mask & c->event_mask)) &&
965 opc->data = GNUNET_TESTBED_extract_config_ (&msg->header);
966 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
968 event.op_cls = opc->op_cls;
969 event.details.operation_finished.generic = opc->data;
970 event.details.operation_finished.emsg = NULL;
971 c->cc (c->cc_cls, &event);
978 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG message from controller
981 * @param c the controller handler
982 * @param msg message received
983 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
987 handle_link_controllers_result (struct GNUNET_TESTBED_Controller *c,
989 GNUNET_TESTBED_ControllerLinkResponse *msg)
991 struct OperationContext *opc;
992 struct ControllerLinkData *data;
993 struct GNUNET_CONFIGURATION_Handle *cfg;
994 struct GNUNET_TESTBED_Host *host;
997 struct GNUNET_TESTBED_EventInformation event;
999 op_id = GNUNET_ntohll (msg->operation_id);
1000 if (NULL == (opc = find_opc (c, op_id)))
1002 LOG_DEBUG ("Operation not found\n");
1005 if (OP_FORWARDED == opc->type)
1007 handle_forwarded_operation_msg (c, opc,
1008 (const struct GNUNET_MessageHeader *) msg);
1011 if (OP_LINK_CONTROLLERS != opc->type)
1016 GNUNET_assert (NULL != (data = opc->data));
1017 host = GNUNET_TESTBED_host_lookup_by_id_ (data->host_id);
1018 GNUNET_assert (NULL != host);
1021 opc->state = OPC_STATE_FINISHED;
1022 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
1023 event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
1025 event.op_cls = opc->op_cls;
1026 event.details.operation_finished.emsg = NULL;
1027 event.details.operation_finished.generic = NULL;
1030 if (GNUNET_NO == ntohs (msg->success))
1032 emsg = GNUNET_malloc (ntohs (msg->header.size)
1034 GNUNET_TESTBED_ControllerLinkResponse) + 1);
1035 memcpy (emsg, &msg[1], ntohs (msg->header.size)
1037 GNUNET_TESTBED_ControllerLinkResponse));
1038 event.details.operation_finished.emsg = emsg;
1042 if (0 != ntohs (msg->config_size))
1044 cfg = GNUNET_TESTBED_extract_config_ ((const struct GNUNET_MessageHeader *) msg);
1045 GNUNET_assert (NULL != cfg);
1046 GNUNET_TESTBED_host_replace_cfg_ (host, cfg);
1049 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
1052 c->cc (c->cc_cls, &event);
1055 LOG_DEBUG ("Not calling callback\n");
1057 GNUNET_CONFIGURATION_destroy (cfg);
1058 GNUNET_free_non_null (emsg);
1064 * Handler for messages from controller (testbed service)
1066 * @param cls the controller handler
1067 * @param msg message received, NULL on timeout or fatal error
1070 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
1072 struct GNUNET_TESTBED_Controller *c = cls;
1076 c->in_receive = GNUNET_NO;
1077 /* FIXME: Add checks for message integrity */
1080 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
1083 msize = ntohs (msg->size);
1084 switch (ntohs (msg->type))
1086 case GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST_SUCCESS:
1087 GNUNET_assert (msize >=
1088 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
1090 GNUNET_TESTBED_host_handle_addhostconfirm_
1091 (c, (const struct GNUNET_TESTBED_HostConfirmedMessage*) msg);
1093 case GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS:
1094 GNUNET_assert (msize ==
1096 GNUNET_TESTBED_GenericOperationSuccessEventMessage));
1098 handle_opsuccess (c,
1100 GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
1103 case GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT:
1104 GNUNET_assert (msize >=
1105 sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
1107 handle_op_fail_event (c,
1109 GNUNET_TESTBED_OperationFailureEventMessage *)
1112 case GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS:
1113 GNUNET_assert (msize ==
1115 GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1117 handle_peer_create_success (c,
1119 GNUNET_TESTBED_PeerCreateSuccessEventMessage
1122 case GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT:
1123 GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1125 handle_peer_event (c,
1126 (const struct GNUNET_TESTBED_PeerEventMessage *)
1130 case GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONFIGURATION:
1131 GNUNET_assert (msize >=
1133 GNUNET_TESTBED_PeerConfigurationInformationMessage));
1135 handle_peer_config (c,
1137 GNUNET_TESTBED_PeerConfigurationInformationMessage
1140 case GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONNECT_EVENT:
1141 GNUNET_assert (msize ==
1142 sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1144 handle_peer_conevent (c,
1146 GNUNET_TESTBED_ConnectionEventMessage *) msg);
1148 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION:
1149 GNUNET_assert (msize > sizeof (struct GNUNET_TESTBED_SlaveConfiguration));
1151 handle_slave_config (c,
1152 (const struct GNUNET_TESTBED_SlaveConfiguration *)
1155 case GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT:
1157 handle_link_controllers_result (c,
1159 GNUNET_TESTBED_ControllerLinkResponse
1165 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
1167 c->in_receive = GNUNET_YES;
1168 GNUNET_CLIENT_receive (c->client, &message_handler, c,
1169 GNUNET_TIME_UNIT_FOREVER_REL);
1175 * Function called to notify a client about the connection begin ready to queue
1176 * more data. "buf" will be NULL and "size" zero if the connection was closed
1177 * for writing in the meantime.
1179 * @param cls closure
1180 * @param size number of bytes available in buf
1181 * @param buf where the callee should write the message
1182 * @return number of bytes written to buf
1185 transmit_ready_notify (void *cls, size_t size, void *buf)
1187 struct GNUNET_TESTBED_Controller *c = cls;
1188 struct MessageQueue *mq_entry;
1191 mq_entry = c->mq_head;
1192 GNUNET_assert (NULL != mq_entry);
1193 if ((0 == size) && (NULL == buf)) /* Timeout */
1195 LOG_DEBUG ("Message sending timed out -- retrying\n");
1197 GNUNET_CLIENT_notify_transmit_ready (c->client,
1198 ntohs (mq_entry->msg->size),
1199 TIMEOUT_REL, GNUNET_YES,
1200 &transmit_ready_notify, c);
1203 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
1204 size = ntohs (mq_entry->msg->size);
1205 memcpy (buf, mq_entry->msg, size);
1206 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
1207 ntohs (mq_entry->msg->type), size);
1208 GNUNET_free (mq_entry->msg);
1209 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
1210 GNUNET_free (mq_entry);
1211 mq_entry = c->mq_head;
1212 if (NULL != mq_entry)
1214 GNUNET_CLIENT_notify_transmit_ready (c->client,
1215 ntohs (mq_entry->msg->size),
1216 TIMEOUT_REL, GNUNET_YES,
1217 &transmit_ready_notify, c);
1218 if (GNUNET_NO == c->in_receive)
1220 c->in_receive = GNUNET_YES;
1221 GNUNET_CLIENT_receive (c->client, &message_handler, c,
1222 GNUNET_TIME_UNIT_FOREVER_REL);
1229 * Queues a message in send queue for sending to the service
1231 * @param controller the handle to the controller
1232 * @param msg the message to queue
1235 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
1236 struct GNUNET_MessageHeader *msg)
1238 struct MessageQueue *mq_entry;
1242 type = ntohs (msg->type);
1243 size = ntohs (msg->size);
1244 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
1245 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
1246 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
1247 mq_entry->msg = msg;
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "Queueing message of type %u, size %u for sending\n", type,
1251 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
1253 if (NULL == controller->th)
1255 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
1256 TIMEOUT_REL, GNUNET_YES,
1257 &transmit_ready_notify,
1263 * Sends the given message as an operation. The given callback is called when a
1264 * reply for the operation is available. Call
1265 * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
1266 * operation context if the cc hasn't been called
1268 * @param controller the controller to which the message has to be sent
1269 * @param operation_id the operation id of the message
1270 * @param msg the message to send
1271 * @param cc the callback to call when reply is available
1272 * @param cc_cls the closure for the above callback
1273 * @return the operation context which can be used to cancel the forwarded
1276 struct OperationContext *
1277 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
1278 *controller, uint64_t operation_id,
1279 const struct GNUNET_MessageHeader *msg,
1280 GNUNET_CLIENT_MessageHandler cc,
1283 struct OperationContext *opc;
1284 struct ForwardedOperationData *data;
1285 struct GNUNET_MessageHeader *dup_msg;
1288 data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
1290 data->cc_cls = cc_cls;
1291 opc = GNUNET_malloc (sizeof (struct OperationContext));
1292 opc->c = controller;
1293 opc->type = OP_FORWARDED;
1295 opc->id = operation_id;
1296 msize = ntohs (msg->size);
1297 dup_msg = GNUNET_malloc (msize);
1298 (void) memcpy (dup_msg, msg, msize);
1299 GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
1300 GNUNET_TESTBED_insert_opc_ (controller, opc);
1306 * Function to cancel an operation created by simply forwarding an operation
1309 * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
1312 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
1314 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
1315 GNUNET_free (opc->data);
1321 * Function to call to start a link-controllers type operation once all queues
1322 * the operation is part of declare that the operation can be activated.
1324 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1327 opstart_link_controllers (void *cls)
1329 struct OperationContext *opc = cls;
1330 struct ControllerLinkData *data;
1331 struct GNUNET_TESTBED_ControllerLinkRequest *msg;
1333 GNUNET_assert (NULL != opc->data);
1337 opc->state = OPC_STATE_STARTED;
1338 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
1339 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1344 * Callback which will be called when link-controllers type operation is released
1346 * @param cls the closure from GNUNET_TESTBED_operation_create_()
1349 oprelease_link_controllers (void *cls)
1351 struct OperationContext *opc = cls;
1352 struct ControllerLinkData *data;
1357 case OPC_STATE_INIT:
1358 GNUNET_free (data->msg);
1360 case OPC_STATE_STARTED:
1361 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
1363 case OPC_STATE_FINISHED:
1366 GNUNET_free_non_null (data);
1372 * Function to be called when get slave config operation is ready
1374 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1377 opstart_get_slave_config (void *cls)
1379 struct OperationContext *opc = cls;
1380 struct GetSlaveConfigData *data = opc->data;
1381 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1383 GNUNET_assert (NULL != data);
1384 msg = GNUNET_TESTBED_generate_slavegetconfig_msg_ (opc->id, data->slave_id);
1385 GNUNET_free (opc->data);
1388 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
1389 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1390 opc->state = OPC_STATE_STARTED;
1395 * Function to be called when get slave config operation is cancelled or finished
1397 * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1400 oprelease_get_slave_config (void *cls)
1402 struct OperationContext *opc = cls;
1406 case OPC_STATE_INIT:
1407 GNUNET_free (opc->data);
1409 case OPC_STATE_STARTED:
1410 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
1412 case OPC_STATE_FINISHED:
1413 if (NULL != opc->data)
1414 GNUNET_CONFIGURATION_destroy (opc->data);
1422 * Start a controller process using the given configuration at the
1425 * @param host host to run the controller on; This should be the same host if
1426 * the controller was previously started with
1427 * GNUNET_TESTBED_controller_start()
1428 * @param event_mask bit mask with set of events to call 'cc' for;
1429 * or-ed values of "1LL" shifted by the
1430 * respective 'enum GNUNET_TESTBED_EventType'
1431 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1432 * @param cc controller callback to invoke on events
1433 * @param cc_cls closure for cc
1434 * @return handle to the controller
1436 struct GNUNET_TESTBED_Controller *
1437 GNUNET_TESTBED_controller_connect (struct GNUNET_TESTBED_Host *host,
1438 uint64_t event_mask,
1439 GNUNET_TESTBED_ControllerCallback cc,
1442 struct GNUNET_TESTBED_Controller *controller;
1443 struct GNUNET_TESTBED_InitMessage *msg;
1444 const struct GNUNET_CONFIGURATION_Handle *cfg;
1445 const char *controller_hostname;
1446 unsigned long long max_parallel_operations;
1447 unsigned long long max_parallel_service_connections;
1448 unsigned long long max_parallel_topology_config_operations;
1450 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (host)));
1452 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1453 "MAX_PARALLEL_OPERATIONS",
1454 &max_parallel_operations))
1460 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1461 "MAX_PARALLEL_SERVICE_CONNECTIONS",
1462 &max_parallel_service_connections))
1468 GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1469 "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1470 &max_parallel_topology_config_operations))
1475 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1476 controller->cc = cc;
1477 controller->cc_cls = cc_cls;
1478 controller->event_mask = event_mask;
1479 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1480 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1481 if (NULL == controller->client)
1483 GNUNET_TESTBED_controller_disconnect (controller);
1486 GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1487 controller->host = host;
1488 controller->opq_parallel_operations =
1489 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1490 max_parallel_operations);
1491 controller->opq_parallel_service_connections =
1492 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1493 max_parallel_service_connections);
1494 controller->opq_parallel_topology_config_operations =
1495 GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1496 max_parallel_topology_config_operations);
1497 controller_hostname = GNUNET_TESTBED_host_get_hostname (host);
1498 if (NULL == controller_hostname)
1499 controller_hostname = "127.0.0.1";
1501 GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1502 strlen (controller_hostname) + 1);
1503 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1505 htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1506 strlen (controller_hostname) + 1);
1507 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1508 msg->event_mask = GNUNET_htonll (controller->event_mask);
1509 strcpy ((char *) &msg[1], controller_hostname);
1510 GNUNET_TESTBED_queue_message_ (controller,
1511 (struct GNUNET_MessageHeader *) msg);
1517 * Iterator to free opc map entries
1519 * @param cls closure
1520 * @param key current key code
1521 * @param value value in the hash map
1522 * @return GNUNET_YES if we should continue to
1527 opc_free_iterator (void *cls, uint32_t key, void *value)
1529 struct GNUNET_CONTAINER_MultiHashMap32 *map = cls;
1530 struct OperationContext *opc = value;
1532 GNUNET_assert (NULL != opc);
1534 GNUNET_assert (GNUNET_YES ==
1535 GNUNET_CONTAINER_multihashmap32_remove (map, key, value));
1542 * Stop the given controller (also will terminate all peers and
1543 * controllers dependent on this controller). This function
1544 * blocks until the testbed has been fully terminated (!).
1546 * @param c handle to controller to stop
1549 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1552 struct MessageQueue *mq_entry;
1555 GNUNET_CLIENT_notify_transmit_ready_cancel (c->th);
1556 /* Clear the message queue */
1557 while (NULL != (mq_entry = c->mq_head))
1559 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail,
1561 GNUNET_free (mq_entry->msg);
1562 GNUNET_free (mq_entry);
1564 if (NULL != c->client)
1565 GNUNET_CLIENT_disconnect (c->client);
1566 if (NULL != c->host)
1567 GNUNET_TESTBED_deregister_host_at_ (c->host, c);
1568 GNUNET_CONFIGURATION_destroy (c->cfg);
1569 GNUNET_TESTBED_operation_queue_destroy_ (c->opq_parallel_operations);
1570 GNUNET_TESTBED_operation_queue_destroy_
1571 (c->opq_parallel_service_connections);
1572 GNUNET_TESTBED_operation_queue_destroy_
1573 (c->opq_parallel_topology_config_operations);
1574 if (NULL != c->opc_map)
1576 GNUNET_assert (GNUNET_SYSERR !=
1577 GNUNET_CONTAINER_multihashmap32_iterate (c->opc_map,
1580 GNUNET_assert (0 == GNUNET_CONTAINER_multihashmap32_size (c->opc_map));
1581 GNUNET_CONTAINER_multihashmap32_destroy (c->opc_map);
1588 * Compresses given configuration using zlib compress
1590 * @param config the serialized configuration
1591 * @param size the size of config
1592 * @param xconfig will be set to the compressed configuration (memory is fresly
1594 * @return the size of the xconfig
1597 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1602 xsize = compressBound ((uLong) size);
1603 *xconfig = GNUNET_malloc (xsize);
1604 GNUNET_assert (Z_OK ==
1605 compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1606 (const Bytef *) config, (uLongf) size,
1613 * Function to serialize and compress using zlib a configuration through a
1614 * configuration handle
1616 * @param cfg the configuration
1617 * @param size the size of configuration when serialize. Will be set on success.
1618 * @param xsize the sizeo of the compressed configuration. Will be set on success.
1619 * @return the serialized and compressed configuration
1622 GNUNET_TESTBED_compress_cfg_ (const struct GNUNET_CONFIGURATION_Handle *cfg,
1623 size_t *size, size_t *xsize)
1630 config = GNUNET_CONFIGURATION_serialize (cfg, &size_);
1631 xsize_ = GNUNET_TESTBED_compress_config_ (config, size_, &xconfig);
1632 GNUNET_free (config);
1640 * Create a link from slave controller to delegated controller. Whenever the
1641 * master controller is asked to start a peer at the delegated controller the
1642 * request will be routed towards slave controller (if a route exists). The
1643 * slave controller will then route it to the delegated controller. The
1644 * configuration of the delegated controller is given and is used to either
1645 * create the delegated controller or to connect to an existing controller. Note
1646 * that while starting the delegated controller the configuration will be
1647 * modified to accommodate available free ports. the 'is_subordinate' specifies
1648 * if the given delegated controller should be started and managed by the slave
1649 * controller, or if the delegated controller already has a master and the slave
1650 * controller connects to it as a non master controller. The success or failure
1651 * of this operation will be signalled through the
1652 * GNUNET_TESTBED_ControllerCallback() with an event of type
1653 * GNUNET_TESTBED_ET_OPERATION_FINISHED
1655 * @param op_cls the operation closure for the event which is generated to
1656 * signal success or failure of this operation
1657 * @param master handle to the master controller who creates the association
1658 * @param delegated_host requests to which host should be delegated; cannot be NULL
1659 * @param slave_host which host is used to run the slave controller; use NULL to
1660 * make the master controller connect to the delegated host
1661 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1662 * be started by the slave controller; GNUNET_NO if the slave
1663 * controller has to connect to the already started delegated
1664 * controller via TCP/IP
1665 * @return the operation handle
1667 struct GNUNET_TESTBED_Operation *
1668 GNUNET_TESTBED_controller_link (void *op_cls,
1669 struct GNUNET_TESTBED_Controller *master,
1670 struct GNUNET_TESTBED_Host *delegated_host,
1671 struct GNUNET_TESTBED_Host *slave_host,
1674 struct OperationContext *opc;
1675 struct GNUNET_TESTBED_ControllerLinkRequest *msg;
1676 struct ControllerLinkData *data;
1677 uint32_t slave_host_id;
1678 uint32_t delegated_host_id;
1681 GNUNET_assert (GNUNET_YES ==
1682 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1684 GNUNET_TESTBED_host_get_id_ ((NULL !=
1685 slave_host) ? slave_host : master->host);
1686 delegated_host_id = GNUNET_TESTBED_host_get_id_ (delegated_host);
1687 if ((NULL != slave_host) && (0 != slave_host_id))
1688 GNUNET_assert (GNUNET_YES ==
1689 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1690 msg_size = sizeof (struct GNUNET_TESTBED_ControllerLinkRequest);
1691 msg = GNUNET_malloc (msg_size);
1692 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS);
1693 msg->header.size = htons (msg_size);
1694 msg->delegated_host_id = htonl (delegated_host_id);
1695 msg->slave_host_id = htonl (slave_host_id);
1696 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1697 data = GNUNET_malloc (sizeof (struct ControllerLinkData));
1699 data->host_id = delegated_host_id;
1700 opc = GNUNET_malloc (sizeof (struct OperationContext));
1703 opc->type = OP_LINK_CONTROLLERS;
1704 opc->id = GNUNET_TESTBED_get_next_op_id (opc->c);
1705 opc->state = OPC_STATE_INIT;
1706 opc->op_cls = op_cls;
1707 msg->operation_id = GNUNET_htonll (opc->id);
1709 GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1710 &oprelease_link_controllers);
1711 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1713 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
1719 * Like GNUNET_TESTBED_get_slave_config(), however without the host registration
1720 * check. Another difference is that this function takes the id of the slave
1723 * @param op_cls the closure for the operation
1724 * @param master the handle to master controller
1725 * @param slave_host_id id of the host where the slave controller is running to
1726 * the slave_host should remain valid until this operation is cancelled
1727 * or marked as finished
1728 * @return the operation handle;
1730 struct GNUNET_TESTBED_Operation *
1731 GNUNET_TESTBED_get_slave_config_ (void *op_cls,
1732 struct GNUNET_TESTBED_Controller *master,
1733 uint32_t slave_host_id)
1735 struct OperationContext *opc;
1736 struct GetSlaveConfigData *data;
1738 data = GNUNET_malloc (sizeof (struct GetSlaveConfigData));
1739 data->slave_id = slave_host_id;
1740 opc = GNUNET_malloc (sizeof (struct OperationContext));
1741 opc->state = OPC_STATE_INIT;
1743 opc->id = GNUNET_TESTBED_get_next_op_id (master);
1744 opc->type = OP_GET_SLAVE_CONFIG;
1746 opc->op_cls = op_cls;
1748 GNUNET_TESTBED_operation_create_ (opc, &opstart_get_slave_config,
1749 &oprelease_get_slave_config);
1750 GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1752 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
1758 * Function to acquire the configuration of a running slave controller. The
1759 * completion of the operation is signalled through the controller_cb from
1760 * GNUNET_TESTBED_controller_connect(). If the operation is successful the
1761 * handle to the configuration is available in the generic pointer of
1762 * operation_finished field of struct GNUNET_TESTBED_EventInformation.
1764 * @param op_cls the closure for the operation
1765 * @param master the handle to master controller
1766 * @param slave_host the host where the slave controller is running; the handle
1767 * to the slave_host should remain valid until this operation is
1768 * cancelled or marked as finished
1769 * @return the operation handle; NULL if the slave_host is not registered at
1772 struct GNUNET_TESTBED_Operation *
1773 GNUNET_TESTBED_get_slave_config (void *op_cls,
1774 struct GNUNET_TESTBED_Controller *master,
1775 struct GNUNET_TESTBED_Host *slave_host)
1777 if (GNUNET_NO == GNUNET_TESTBED_is_host_registered_ (slave_host, master))
1779 return GNUNET_TESTBED_get_slave_config_ (op_cls, master,
1780 GNUNET_TESTBED_host_get_id_
1786 * Ask the testbed controller to write the current overlay topology to
1787 * a file. Naturally, the file will only contain a snapshot as the
1788 * topology may evolve all the time.
1790 * @param controller overlay controller to inspect
1791 * @param filename name of the file the topology should
1795 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1797 const char *filename)
1804 * Creates a helper initialization message. This function is here because we
1805 * want to use this in testing
1807 * @param trusted_ip the ip address of the controller which will be set as TRUSTED
1808 * HOST(all connections form this ip are permitted by the testbed) when
1809 * starting testbed controller at host. This can either be a single ip
1810 * address or a network address in CIDR notation.
1811 * @param hostname the hostname of the destination this message is intended for
1812 * @param cfg the configuration that has to used to start the testbed service
1814 * @return the initialization message
1816 struct GNUNET_TESTBED_HelperInit *
1817 GNUNET_TESTBED_create_helper_init_msg_ (const char *trusted_ip,
1818 const char *hostname,
1819 const struct GNUNET_CONFIGURATION_Handle
1822 struct GNUNET_TESTBED_HelperInit *msg;
1826 size_t xconfig_size;
1827 uint16_t trusted_ip_len;
1828 uint16_t hostname_len;
1831 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1832 GNUNET_assert (NULL != config);
1834 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1835 GNUNET_free (config);
1836 trusted_ip_len = strlen (trusted_ip);
1837 hostname_len = (NULL == hostname) ? 0 : strlen (hostname);
1839 xconfig_size + trusted_ip_len + 1 +
1840 sizeof (struct GNUNET_TESTBED_HelperInit);
1841 msg_size += hostname_len;
1842 msg = GNUNET_realloc (xconfig, msg_size);
1843 (void) memmove (((void *) &msg[1]) + trusted_ip_len + 1 + hostname_len, msg,
1845 msg->header.size = htons (msg_size);
1846 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1847 msg->trusted_ip_size = htons (trusted_ip_len);
1848 msg->hostname_size = htons (hostname_len);
1849 msg->config_size = htons (config_size);
1850 (void) strcpy ((char *) &msg[1], trusted_ip);
1851 if (0 != hostname_len)
1852 (void) strncpy (((char *) &msg[1]) + trusted_ip_len + 1, hostname,
1859 * Signal that the information from an operation has been fully
1860 * processed. This function MUST be called for each event
1861 * of type 'operation_finished' to fully remove the operation
1862 * from the operation queue. After calling this function, the
1863 * 'op_result' becomes invalid (!).
1865 * @param operation operation to signal completion for
1868 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1870 (void) exop_check (operation);
1871 GNUNET_TESTBED_operation_release_ (operation);
1876 * Generates configuration by uncompressing configuration in given message. The
1877 * given message should be of the following types:
1878 * GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONFIGURATION,
1879 * GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION,
1880 * GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST,
1881 * GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS,
1882 * GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT,
1884 * @param msg the message containing compressed configuration
1885 * @return handle to the parsed configuration; NULL upon error while parsing the message
1887 struct GNUNET_CONFIGURATION_Handle *
1888 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg)
1890 struct GNUNET_CONFIGURATION_Handle *cfg;
1897 switch (ntohs (msg->type))
1899 case GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONFIGURATION:
1901 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
1904 (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *) msg;
1905 data_len = (uLong) ntohs (imsg->config_size);
1907 ntohs (imsg->header.size) -
1908 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1909 xdata = (const Bytef *) &imsg[1];
1912 case GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION:
1914 const struct GNUNET_TESTBED_SlaveConfiguration *imsg;
1916 imsg = (const struct GNUNET_TESTBED_SlaveConfiguration *) msg;
1917 data_len = (uLong) ntohs (imsg->config_size);
1919 ntohs (imsg->header.size) -
1920 sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
1921 xdata = (const Bytef *) &imsg[1];
1924 case GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST:
1926 const struct GNUNET_TESTBED_AddHostMessage *imsg;
1929 imsg = (const struct GNUNET_TESTBED_AddHostMessage *) msg;
1930 data_len = (uLong) ntohs (imsg->config_size);
1931 osize = sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1932 ntohs (imsg->username_length) + ntohs (imsg->hostname_length);
1933 xdata_len = ntohs (imsg->header.size) - osize;
1934 xdata = (const Bytef *) ((const void *) imsg + osize);
1937 case GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT:
1939 const struct GNUNET_TESTBED_ControllerLinkResponse *imsg;
1941 imsg = (const struct GNUNET_TESTBED_ControllerLinkResponse *) msg;
1942 data_len = ntohs (imsg->config_size);
1943 xdata_len = ntohs (imsg->header.size) -
1944 sizeof (const struct GNUNET_TESTBED_ControllerLinkResponse);
1945 xdata = (const Bytef *) &imsg[1];
1948 case GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER:
1950 const struct GNUNET_TESTBED_PeerCreateMessage *imsg;
1952 imsg = (const struct GNUNET_TESTBED_PeerCreateMessage *) msg;
1953 data_len = ntohs (imsg->config_size);
1954 xdata_len = ntohs (imsg->header.size) -
1955 sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1956 xdata = (const Bytef *) &imsg[1];
1959 case GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER:
1961 const struct GNUNET_TESTBED_PeerReconfigureMessage *imsg;
1963 imsg = (const struct GNUNET_TESTBED_PeerReconfigureMessage *) msg;
1964 data_len = ntohs (imsg->config_size);
1965 xdata_len = ntohs (imsg->header.size) -
1966 sizeof (struct GNUNET_TESTBED_PeerReconfigureMessage);
1967 xdata = (const Bytef *) &imsg[1];
1973 data = GNUNET_malloc (data_len);
1974 if (Z_OK != (ret = uncompress (data, &data_len, xdata, xdata_len)))
1977 GNUNET_break_op (0); /* Un-compression failure */
1980 cfg = GNUNET_CONFIGURATION_create ();
1982 GNUNET_CONFIGURATION_deserialize (cfg, (const char *) data,
1987 GNUNET_break_op (0); /* De-serialization failure */
1996 * Checks the integrity of the OperationFailureEventMessage and if good returns
1997 * the error message it contains.
1999 * @param msg the OperationFailureEventMessage
2000 * @return the error message
2003 GNUNET_TESTBED_parse_error_string_ (const struct
2004 GNUNET_TESTBED_OperationFailureEventMessage
2010 msize = ntohs (msg->header.size);
2011 if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
2013 msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
2014 emsg = (const char *) &msg[1];
2015 if ('\0' != emsg[msize - 1])
2025 * Function to return the operation id for a controller. The operation id is
2026 * created from the controllers host id and its internal operation counter.
2028 * @param controller the handle to the controller whose operation id has to be incremented
2029 * @return the incremented operation id.
2032 GNUNET_TESTBED_get_next_op_id (struct GNUNET_TESTBED_Controller * controller)
2036 op_id = (uint64_t) GNUNET_TESTBED_host_get_id_ (controller->host);
2037 op_id = op_id << 32;
2038 op_id |= (uint64_t) controller->operation_counter++;
2044 * Function called when a shutdown peers operation is ready
2046 * @param cls the closure from GNUNET_TESTBED_operation_create_()
2049 opstart_shutdown_peers (void *cls)
2051 struct OperationContext *opc = cls;
2052 struct GNUNET_TESTBED_ShutdownPeersMessage *msg;
2054 opc->state = OPC_STATE_STARTED;
2055 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ShutdownPeersMessage));
2057 htons (sizeof (struct GNUNET_TESTBED_ShutdownPeersMessage));
2058 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS);
2059 msg->operation_id = GNUNET_htonll (opc->id);
2060 GNUNET_TESTBED_insert_opc_ (opc->c, opc);
2061 GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
2066 * Callback which will be called when shutdown peers operation is released
2068 * @param cls the closure from GNUNET_TESTBED_operation_create_()
2071 oprelease_shutdown_peers (void *cls)
2073 struct OperationContext *opc = cls;
2077 case OPC_STATE_STARTED:
2078 GNUNET_TESTBED_remove_opc_ (opc->c, opc);
2079 /* no break; continue */
2080 case OPC_STATE_INIT:
2081 GNUNET_free (opc->data);
2083 case OPC_STATE_FINISHED:
2091 * Stops and destroys all peers. Is equivalent of calling
2092 * GNUNET_TESTBED_peer_stop() and GNUNET_TESTBED_peer_destroy() on all peers,
2093 * except that the peer stop event and operation finished event corresponding to
2094 * the respective functions are not generated. This function should be called
2095 * when there are no other pending operations. If there are pending operations,
2096 * it will return NULL
2098 * @param c the controller to send this message to
2099 * @param op_cls closure for the operation
2100 * @param cb the callback to call when all peers are stopped and destroyed
2101 * @param cb_cls the closure for the callback
2102 * @return operation handle on success; NULL if any pending operations are
2105 struct GNUNET_TESTBED_Operation *
2106 GNUNET_TESTBED_shutdown_peers (struct GNUNET_TESTBED_Controller *c,
2108 GNUNET_TESTBED_OperationCompletionCallback cb,
2111 struct OperationContext *opc;
2112 struct ShutdownPeersData *data;
2114 if (0 != GNUNET_CONTAINER_multihashmap32_size (c->opc_map))
2116 data = GNUNET_malloc (sizeof (struct ShutdownPeersData));
2118 data->cb_cls = cb_cls;
2119 opc = GNUNET_malloc (sizeof (struct OperationContext));
2121 opc->op_cls = op_cls;
2123 opc->id = GNUNET_TESTBED_get_next_op_id (c);
2124 opc->type = OP_SHUTDOWN_PEERS;
2125 opc->state = OPC_STATE_INIT;
2126 opc->op = GNUNET_TESTBED_operation_create_ (opc, &opstart_shutdown_peers,
2127 &oprelease_shutdown_peers);
2128 GNUNET_TESTBED_operation_queue_insert_ (opc->c->opq_parallel_operations,
2130 GNUNET_TESTBED_operation_begin_wait_ (opc->op);
2135 /* end of testbed_api.c */