2 This file is part of GNUnet.
3 (C) 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 2, 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/gnunet-service-testbed.c
23 * @brief implementation of the TESTBED service
24 * @author Sree Harsha Totakura
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_hello_lib.h"
35 #include "gnunet_testbed_service.h"
37 #include "testbed_api.h"
38 #include "testbed_api_hosts.h"
39 #include "gnunet_testing_lib-new.h"
44 #define LOG(kind,...) \
45 GNUNET_log (kind, __VA_ARGS__)
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
54 * By how much should the arrays lists grow
56 #define LIST_GROW_STEP 10
59 * Default timeout for operations which may take some time
61 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
64 * The main context information associated with the client which started us
69 * The client handle associated with this context
71 struct GNUNET_SERVER_Client *client;
74 * The network address of the master controller
79 * The TESTING system handle for starting peers locally
81 struct GNUNET_TESTING_System *system;
84 * Event mask of event to be responded in this context
89 * Our host id according to this context
96 * The message queue for sending messages to clients
101 * The message to be sent
103 struct GNUNET_MessageHeader *msg;
106 * The client to send the message to
108 struct GNUNET_SERVER_Client *client;
111 * next pointer for DLL
113 struct MessageQueue *next;
116 * prev pointer for DLL
118 struct MessageQueue *prev;
123 * The structure for identifying a shared service
128 * The name of the shared service
133 * Number of shared peers per instance of the shared service
138 * Number of peers currently sharing the service
140 uint32_t num_sharing;
155 * The host destination is reachable thru
162 * Structure representing a connected(directly-linked) controller
167 * The controller process handle if we had started the controller
169 struct GNUNET_TESTBED_ControllerProc *controller_proc;
172 * The controller handle
174 struct GNUNET_TESTBED_Controller *controller;
177 * The id of the host this controller is running on
184 * States of LCFContext
189 * The Context has been initialized; Nothing has been done on it
194 * Delegated host has been registered at the forwarding controller
196 DELEGATED_HOST_REGISTERED,
199 * The slave host has been registred at the forwarding controller
201 SLAVE_HOST_REGISTERED,
204 * The context has been finished (may have error)
211 * Link controllers request forwarding context
216 * The gateway which will pass the link message to delegated host
218 struct Slave *gateway;
221 * The controller link message that has to be forwarded to
223 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
226 * The client which has asked to perform this operation
228 struct GNUNET_SERVER_Client *client;
231 * The host registration handle while registered hosts in this context
233 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
236 * The id of the operation which created this context
238 uint64_t operation_id;
241 * The state of this context
243 enum LCFContextState state;
248 uint32_t delegated_host_id;
253 uint32_t slave_host_id;
259 * Structure of a queue entry in LCFContext request queue
261 struct LCFContextQueue
266 struct LCFContext *lcf;
271 struct LCFContextQueue *next;
276 struct LCFContextQueue *prev;
281 * A locally started peer
290 * The peer handle from testing API
292 struct GNUNET_TESTING_Peer *peer;
295 * The modified (by GNUNET_TESTING_peer_configure) configuration this
296 * peer is configured with
298 struct GNUNET_CONFIGURATION_Handle *cfg;
301 * Is the peer running
310 * The controller this peer is started through
312 struct GNUNET_TESTBED_Controller *controller;
319 * Is this peer locally created?
324 * Our local reference id for this peer
332 * Context information for connecting 2 peers in overlay
334 struct OverlayConnectContext
337 * The client which has requested for overlay connection
339 struct GNUNET_SERVER_Client *client;
342 * the peer which has to connect to the other peer
349 struct Peer *other_peer;
352 * Transport handle of the first peer to get its HELLO
354 struct GNUNET_TRANSPORT_Handle *p1th;
357 * Transport handle of other peer to offer first peer's HELLO
359 struct GNUNET_TRANSPORT_Handle *p2th;
362 * Core handles of the first peer; used to notify when second peer connects to it
364 struct GNUNET_CORE_Handle *ch;
367 * HELLO of the other peer
369 struct GNUNET_MessageHeader *hello;
372 * Get hello handle to acquire HELLO of first peer
374 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
377 * The error message we send if this overlay connect operation has timed out
382 * Operation context for suboperations
384 struct OperationContext *opc;
387 * The peer identity of the first peer
389 struct GNUNET_PeerIdentity peer_identity;
392 * The peer identity of the other peer
394 struct GNUNET_PeerIdentity other_peer_identity;
397 * The id of the operation responsible for creating this context
402 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
405 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
408 * The id of the overlay connect timeout task
410 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
416 * Context information for operations forwarded to subcontrollers
418 struct ForwardedOperationContext
421 * The generated operation context
423 struct OperationContext *opc;
426 * The client to which we have to reply
428 struct GNUNET_SERVER_Client *client;
436 * Task ID for the timeout task
438 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
441 * The id of the operation that has been forwarded
443 uint64_t operation_id;
449 * Context information used while linking controllers
451 struct LinkControllersContext
454 * The client which initiated the link controller operation
456 struct GNUNET_SERVER_Client *client;
459 * The ID of the operation
461 uint64_t operation_id;
464 * Pointer to the slave handle if we are directly starting/connecting to the controller
472 * The master context; generated with the first INIT message
474 static struct Context *master_context;
481 * Current Transmit Handle; NULL if no notify transmit exists currently
483 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
490 * The head for the LCF queue
492 static struct LCFContextQueue *lcfq_head;
495 * The tail for the LCF queue
497 static struct LCFContextQueue *lcfq_tail;
500 * The message queue head
502 static struct MessageQueue *mq_head;
505 * The message queue tail
507 static struct MessageQueue *mq_tail;
512 static struct GNUNET_TESTBED_Host **host_list;
517 static struct Route **route_list;
520 * A list of directly linked neighbours
522 static struct Slave **slave_list;
525 * A list of peers we own locally
527 static struct Peer **peer_list;
530 * The hashmap of shared services
532 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
535 * The size of the host list
537 static uint32_t host_list_size;
540 * The size of the route list
542 static uint32_t route_list_size;
545 * The size of directly linked neighbours list
547 static uint32_t slave_list_size;
550 * The size of the peer list
552 static uint32_t peer_list_size;
559 * The lcf_task handle
561 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
564 * The shutdown task handle
566 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
570 * Function called to notify a client about the connection begin ready to queue
571 * more data. "buf" will be NULL and "size" zero if the connection was closed
572 * for writing in the meantime.
575 * @param size number of bytes available in buf
576 * @param buf where the callee should write the message
577 * @return number of bytes written to buf
580 transmit_ready_notify (void *cls, size_t size, void *buf)
582 struct MessageQueue *mq_entry;
584 transmit_handle = NULL;
586 GNUNET_assert (NULL != mq_entry);
589 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
590 size = ntohs (mq_entry->msg->size);
591 memcpy (buf, mq_entry->msg, size);
592 GNUNET_free (mq_entry->msg);
593 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
594 GNUNET_free (mq_entry);
596 if (NULL != mq_entry)
598 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
599 ntohs (mq_entry->msg->size),
600 GNUNET_TIME_UNIT_FOREVER_REL,
601 &transmit_ready_notify, NULL);
607 * Queues a message in send queue for sending to the service
609 * @param client the client to whom the queued message has to be sent
610 * @param msg the message to queue
613 queue_message (struct GNUNET_SERVER_Client *client,
614 struct GNUNET_MessageHeader *msg)
616 struct MessageQueue *mq_entry;
620 type = ntohs (msg->type);
621 size = ntohs (msg->size);
622 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
623 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
624 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
626 mq_entry->client = client;
627 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
629 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
630 if (NULL == transmit_handle)
632 GNUNET_SERVER_notify_transmit_ready (client, size,
633 GNUNET_TIME_UNIT_FOREVER_REL,
634 &transmit_ready_notify, NULL);
639 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
641 * @param ptr the memory block to realloc
642 * @param size the size of ptr
643 * @param new_size the size to which ptr has to be realloc'ed
644 * @return the newly reallocated memory block
647 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
649 ptr = GNUNET_realloc (ptr, new_size);
651 ptr = memset (ptr + size, 0, new_size - size);
657 * Function to add a host to the current list of known hosts
659 * @param host the host to add
660 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
664 host_list_add (struct GNUNET_TESTBED_Host *host)
668 host_id = GNUNET_TESTBED_host_get_id_ (host);
669 if (host_list_size <= host_id)
672 TESTBED_realloc (host_list,
673 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
674 sizeof (struct GNUNET_TESTBED_Host *) *
675 (host_list_size + LIST_GROW_STEP));
676 host_list_size += LIST_GROW_STEP;
678 if (NULL != host_list[host_id])
680 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
681 return GNUNET_SYSERR;
683 host_list[host_id] = host;
689 * Adds a route to the route list
691 * @param route the route to add
694 route_list_add (struct Route *route)
696 if (route->dest >= route_list_size)
699 TESTBED_realloc (route_list, sizeof (struct Route *) * route_list_size,
700 sizeof (struct Route *) * (route_list_size +
702 route_list_size += LIST_GROW_STEP;
704 GNUNET_assert (NULL == route_list[route->dest]);
705 route_list[route->dest] = route;
710 * Adds a slave to the slave array
712 * @param slave the slave controller to add
715 slave_list_add (struct Slave *slave)
717 if (slave->host_id >= slave_list_size)
720 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
721 sizeof (struct Slave *) * (slave_list_size +
723 slave_list_size += LIST_GROW_STEP;
725 GNUNET_assert (NULL == slave_list[slave->host_id]);
726 slave_list[slave->host_id] = slave;
731 * Adds a peer to the peer array
733 * @param peer the peer to add
736 peer_list_add (struct Peer *peer)
740 orig_size = peer_list_size;
741 if (peer->id >= peer_list_size)
743 while (peer->id >= peer_list_size)
744 peer_list_size += LIST_GROW_STEP;
746 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
747 sizeof (struct Peer *) * peer_list_size);
749 GNUNET_assert (NULL == peer_list[peer->id]);
750 peer_list[peer->id] = peer;
755 * Removes a the give peer from the peer array
757 * @param peer the peer to be removed
760 peer_list_remove (struct Peer *peer)
765 peer_list[peer->id] = NULL;
766 orig_size = peer_list_size;
767 while (peer_list_size >= LIST_GROW_STEP)
769 for (id = peer_list_size - 1;
770 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
771 if (NULL != peer_list[id])
773 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
775 peer_list_size -= LIST_GROW_STEP;
777 if (orig_size == peer_list_size)
780 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
785 * Finds the route with directly connected host as destination through which
786 * the destination host can be reached
788 * @param host_id the id of the destination host
789 * @return the route with directly connected destination host; NULL if no route
792 static struct Route *
793 find_dest_route (uint32_t host_id)
797 while (NULL != (route = route_list[host_id]))
799 if (route->thru == master_context->host_id)
801 host_id = route->thru;
808 * Routes message to a host given its host_id
810 * @param host_id the id of the destination host
811 * @param msg the message to be routed
814 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
821 * Send operation failure message to client
823 * @param client the client to which the failure message has to be sent to
824 * @param operation_id the id of the failed operation
825 * @param emsg the error message; can be NULL
828 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
829 uint64_t operation_id, const char *emsg)
831 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
835 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
836 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
838 msg = GNUNET_malloc (msize);
839 msg->header.size = htons (msize);
840 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
841 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
842 msg->operation_id = GNUNET_htonll (operation_id);
844 memcpy (&msg[1], emsg, emsg_len);
845 queue_message (client, &msg->header);
850 * Function to send generic operation success message to given client
852 * @param client the client to send the message to
853 * @param operation_id the id of the operation which was successful
856 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
857 uint64_t operation_id)
859 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
862 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
863 msg = GNUNET_malloc (msize);
864 msg->header.size = htons (msize);
865 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
866 msg->operation_id = GNUNET_htonll (operation_id);
867 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
868 queue_message (client, &msg->header);
873 * The Link Controller forwarding task
875 * @param cls the LCFContext
876 * @param tc the Task context from scheduler
879 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
883 * Completion callback for host registrations while forwarding Link Controller messages
885 * @param cls the LCFContext
886 * @param emsg the error message; NULL if host registration is successful
889 lcf_proc_cc (void *cls, const char *emsg)
891 struct LCFContext *lcf = cls;
894 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
899 goto registration_error;
900 lcf->state = DELEGATED_HOST_REGISTERED;
901 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
903 case DELEGATED_HOST_REGISTERED:
905 goto registration_error;
906 lcf->state = SLAVE_HOST_REGISTERED;
907 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
910 GNUNET_assert (0); /* Shouldn't reach here */
915 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
917 lcf->state = FINISHED;
918 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
923 * Callback to be called when forwarded link controllers operation is
924 * successfull. We have to relay the reply msg back to the client
926 * @param cls ForwardedOperationContext
927 * @param msg the peer create success message
930 forwarded_operation_reply_relay (void *cls,
931 const struct GNUNET_MessageHeader *msg)
933 struct ForwardedOperationContext *fopc = cls;
934 struct GNUNET_MessageHeader *dup_msg;
937 msize = ntohs (msg->size);
938 dup_msg = GNUNET_malloc (msize);
939 (void) memcpy (dup_msg, msg, msize);
940 queue_message (fopc->client, dup_msg);
941 GNUNET_SERVER_client_drop (fopc->client);
942 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
948 * Task to free resources when forwarded link controllers has been timedout
950 * @param cls the ForwardedOperationContext
951 * @param tc the task context from scheduler
954 forwarded_operation_timeout (void *cls,
955 const struct GNUNET_SCHEDULER_TaskContext *tc)
957 struct ForwardedOperationContext *fopc = cls;
959 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
960 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
961 GNUNET_SERVER_client_drop (fopc->client);
967 * The Link Controller forwarding task
969 * @param cls the LCFContext
970 * @param tc the Task context from scheduler
973 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
975 struct LCFContext *lcf = cls;
976 struct LCFContextQueue *lcfq;
977 struct ForwardedOperationContext *fopc;
979 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
984 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
985 lcf->gateway->controller))
988 GNUNET_TESTBED_register_host (lcf->gateway->controller,
989 host_list[lcf->delegated_host_id],
994 lcf->state = DELEGATED_HOST_REGISTERED;
995 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
998 case DELEGATED_HOST_REGISTERED:
1000 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1001 lcf->gateway->controller))
1004 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1005 host_list[lcf->slave_host_id],
1010 lcf->state = SLAVE_HOST_REGISTERED;
1011 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1014 case SLAVE_HOST_REGISTERED:
1015 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1016 fopc->client = lcf->client;
1017 fopc->operation_id = lcf->operation_id;
1019 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1022 &forwarded_operation_reply_relay,
1024 fopc->timeout_task =
1025 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1027 lcf->state = FINISHED;
1028 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1032 GNUNET_assert (lcfq->lcf == lcf);
1033 GNUNET_free (lcf->msg);
1035 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1037 if (NULL != lcfq_head)
1039 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1045 * Callback for event from slave controllers
1047 * @param cls struct Slave *
1048 * @param event information about the event
1051 slave_event_callback (void *cls,
1052 const struct GNUNET_TESTBED_EventInformation *event)
1059 * Callback to signal successfull startup of the controller process
1061 * @param cls the closure from GNUNET_TESTBED_controller_start()
1062 * @param cfg the configuration with which the controller has been started;
1063 * NULL if status is not GNUNET_OK
1064 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1065 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1068 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1071 struct LinkControllersContext *lcc = cls;
1073 if (GNUNET_SYSERR == status)
1075 lcc->slave->controller_proc = NULL;
1076 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1077 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1080 lcc->slave->controller =
1081 GNUNET_TESTBED_controller_connect (cfg, host_list[lcc->slave->host_id],
1082 master_context->event_mask,
1083 &slave_event_callback, lcc->slave);
1084 if (NULL != lcc->slave->controller)
1085 send_operation_success_msg (lcc->client, lcc->operation_id);
1087 send_operation_fail_msg (lcc->client, lcc->operation_id,
1088 "Could not connect to delegated controller");
1089 GNUNET_SERVER_client_drop (lcc->client);
1095 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1098 * @param client identification of the client
1099 * @param message the actual message
1102 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1103 const struct GNUNET_MessageHeader *message)
1105 const struct GNUNET_TESTBED_InitMessage *msg;
1106 struct GNUNET_TESTBED_Host *host;
1107 const char *controller_hostname;
1110 if (NULL != master_context)
1113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1116 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1117 msize = ntohs (message->size);
1118 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1124 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1125 controller_hostname = (const char *) &msg[1];
1126 if ('\0' != controller_hostname[msize - 1])
1129 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1132 master_context = GNUNET_malloc (sizeof (struct Context));
1133 master_context->client = client;
1134 master_context->host_id = ntohl (msg->host_id);
1135 master_context->master_ip = GNUNET_strdup (controller_hostname);
1136 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1137 master_context->system =
1138 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1140 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1142 host_list_add (host);
1143 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1144 GNUNET_SERVER_client_keep (client);
1145 LOG_DEBUG ("Created master context with host ID: %u\n",
1146 master_context->host_id);
1147 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1152 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1155 * @param client identification of the client
1156 * @param message the actual message
1159 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1160 const struct GNUNET_MessageHeader *message)
1162 struct GNUNET_TESTBED_Host *host;
1163 const struct GNUNET_TESTBED_AddHostMessage *msg;
1164 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1169 uint16_t username_length;
1170 uint16_t hostname_length;
1171 uint16_t reply_size;
1174 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1175 msize = ntohs (msg->header.size);
1176 username = (char *) &(msg[1]);
1177 username_length = ntohs (msg->user_name_length);
1178 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1179 if (0 != username_length)
1180 GNUNET_assert ('\0' == username[username_length]);
1181 username_length = (0 == username_length) ? 0 : username_length + 1;
1182 hostname = username + username_length;
1184 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1185 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1186 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1187 host_id = ntohl (msg->host_id);
1188 LOG_DEBUG ("Received ADDHOST message\n");
1189 LOG_DEBUG ("-------host id: %u\n", host_id);
1190 if (NULL != hostname)
1191 LOG_DEBUG ("-------hostname: %s\n", hostname);
1192 if (0 != username_length)
1193 LOG_DEBUG ("-------username: %s\n", username);
1196 LOG_DEBUG ("-------username: NULL\n");
1199 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1201 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1202 ntohs (msg->ssh_port));
1203 GNUNET_assert (NULL != host);
1204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1205 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1206 if (GNUNET_OK != host_list_add (host))
1208 /* We are unable to add a host */
1209 emsg = "A host exists with given host-id";
1210 LOG_DEBUG ("%s: %u", emsg, host_id);
1211 GNUNET_TESTBED_host_destroy (host);
1212 reply_size += strlen (emsg) + 1;
1213 reply = GNUNET_malloc (reply_size);
1214 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1217 reply = GNUNET_malloc (reply_size);
1218 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1219 reply->header.size = htons (reply_size);
1220 reply->host_id = htonl (host_id);
1221 queue_message (client, &reply->header);
1226 * Iterator over hash map entries.
1228 * @param cls closure
1229 * @param key current key code
1230 * @param value value in the hash map
1231 * @return GNUNET_YES if we should continue to
1236 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1238 struct SharedService *queried_ss = cls;
1239 struct SharedService *ss = value;
1241 if (0 == strcmp (ss->name, queried_ss->name))
1249 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1252 * @param client identification of the client
1253 * @param message the actual message
1256 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1257 const struct GNUNET_MessageHeader *message)
1259 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1260 struct SharedService *ss;
1262 struct GNUNET_HashCode hash;
1264 uint16_t service_name_size;
1266 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1267 msg_size = ntohs (message->size);
1268 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1271 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1275 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1276 service_name = (char *) &msg[1];
1277 if ('\0' != service_name[service_name_size - 1])
1280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1283 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1284 service_name, ntohl (msg->num_peers));
1285 if (ntohl (msg->host_id) != master_context->host_id)
1287 route_message (ntohl (msg->host_id), message);
1288 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1291 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1292 ss = GNUNET_malloc (sizeof (struct SharedService));
1293 ss->name = strdup (service_name);
1294 ss->num_shared = ntohl (msg->num_peers);
1295 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1296 if (GNUNET_SYSERR ==
1297 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1298 &ss_exists_iterator, ss))
1300 LOG (GNUNET_ERROR_TYPE_WARNING,
1301 "Service %s already configured as a shared service. "
1302 "Ignoring service sharing request \n", ss->name);
1303 GNUNET_free (ss->name);
1307 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1313 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1316 * @param client identification of the client
1317 * @param message the actual message
1320 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1321 const struct GNUNET_MessageHeader *message)
1323 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1324 struct GNUNET_CONFIGURATION_Handle *cfg;
1325 struct LCFContextQueue *lcfq;
1326 struct Route *route;
1327 struct Route *new_route;
1331 uint32_t delegated_host_id;
1332 uint32_t slave_host_id;
1335 if (NULL == master_context)
1338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1341 msize = ntohs (message->size);
1342 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1348 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1349 delegated_host_id = ntohl (msg->delegated_host_id);
1350 if (delegated_host_id == master_context->host_id)
1353 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1357 if ((delegated_host_id >= host_list_size) ||
1358 (NULL == host_list[delegated_host_id]))
1360 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1364 slave_host_id = ntohl (msg->slave_host_id);
1365 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1367 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1371 if (slave_host_id == delegated_host_id)
1373 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1378 if (slave_host_id == master_context->host_id) /* Link from us */
1380 struct Slave *slave;
1381 struct LinkControllersContext *lcc;
1383 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1384 config_size = ntohs (msg->config_size);
1385 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1387 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1392 config = GNUNET_malloc (config_size);
1393 dest_size = (uLongf) config_size;
1395 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1398 GNUNET_break (0); /* Compression error */
1399 GNUNET_free (config);
1400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1403 if (config_size != dest_size)
1405 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1406 GNUNET_free (config);
1407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1410 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1412 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1414 GNUNET_break (0); /* Configuration parsing error */
1415 GNUNET_free (config);
1416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1419 GNUNET_free (config);
1420 if ((delegated_host_id < slave_list_size) &&
1421 (NULL != slave_list[delegated_host_id]))
1423 GNUNET_break (0); /* Configuration parsing error */
1424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1427 slave = GNUNET_malloc (sizeof (struct Slave));
1428 slave->host_id = delegated_host_id;
1429 slave_list_add (slave);
1430 if (1 != msg->is_subordinate)
1433 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1434 master_context->event_mask,
1435 &slave_event_callback, slave);
1436 GNUNET_CONFIGURATION_destroy (cfg);
1437 if (NULL != slave->controller)
1438 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1440 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1441 "Could not connect to delegated controller");
1442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1445 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1446 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1447 GNUNET_SERVER_client_keep (client);
1448 lcc->client = client;
1450 slave->controller_proc =
1451 GNUNET_TESTBED_controller_start (master_context->master_ip,
1452 host_list[slave->host_id], cfg,
1453 &slave_status_callback, lcc);
1454 GNUNET_CONFIGURATION_destroy (cfg);
1455 new_route = GNUNET_malloc (sizeof (struct Route));
1456 new_route->dest = delegated_host_id;
1457 new_route->thru = master_context->host_id;
1458 route_list_add (new_route);
1459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1463 /* Route the request */
1464 if (slave_host_id >= route_list_size)
1466 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1470 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1471 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1472 lcfq->lcf->delegated_host_id = delegated_host_id;
1473 lcfq->lcf->slave_host_id = slave_host_id;
1474 route = find_dest_route (slave_host_id);
1475 GNUNET_assert (NULL != route); /* because we add routes carefully */
1476 GNUNET_assert (route->dest < slave_list_size);
1477 GNUNET_assert (NULL != slave_list[route->dest]);
1478 lcfq->lcf->state = INIT;
1479 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1480 lcfq->lcf->gateway = slave_list[route->dest];
1481 lcfq->lcf->msg = GNUNET_malloc (msize);
1482 (void) memcpy (lcfq->lcf->msg, msg, msize);
1483 GNUNET_SERVER_client_keep (client);
1484 lcfq->lcf->client = client;
1485 if (NULL == lcfq_head)
1487 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1488 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1489 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1492 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1493 /* FIXME: Adding a new route should happen after the controllers are linked
1495 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1496 new_route = GNUNET_malloc (sizeof (struct Route));
1497 new_route->dest = delegated_host_id;
1498 new_route->thru = route->dest;
1499 route_list_add (new_route);
1504 * The task to be executed if the forwarded peer create operation has been
1507 * @param cls the FowardedOperationContext
1508 * @param tc the TaskContext from the scheduler
1511 peer_create_forward_timeout (void *cls,
1512 const struct GNUNET_SCHEDULER_TaskContext *tc)
1514 struct ForwardedOperationContext *fo_ctxt = cls;
1516 /* send error msg to client */
1517 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1518 GNUNET_SERVER_client_drop (fo_ctxt->client);
1519 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1520 GNUNET_free (fo_ctxt);
1525 * Callback to be called when forwarded peer create operation is
1526 * successfull. We have to relay the reply msg back to the client
1528 * @param cls ForwardedOperationContext
1529 * @param msg the peer create success message
1532 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1534 struct ForwardedOperationContext *fo_ctxt = cls;
1535 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1536 struct GNUNET_MessageHeader *dup_msg;
1540 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1541 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1544 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1545 peer = GNUNET_malloc (sizeof (struct Peer));
1546 peer->is_remote = GNUNET_YES;
1547 peer->id = ntohl (success_msg->peer_id);
1548 GNUNET_assert (NULL != fo_ctxt->cls);
1549 peer->details.remote.controller = fo_ctxt->cls;
1550 peer_list_add (peer);
1552 msize = ntohs (msg->size);
1553 dup_msg = GNUNET_malloc (msize);
1554 (void) memcpy (dup_msg, msg, msize);
1555 queue_message (fo_ctxt->client, dup_msg);
1556 GNUNET_SERVER_client_drop (fo_ctxt->client);
1557 GNUNET_free (fo_ctxt);
1563 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1566 * @param client identification of the client
1567 * @param message the actual message
1570 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1571 const struct GNUNET_MessageHeader *message)
1573 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1574 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1575 struct GNUNET_CONFIGURATION_Handle *cfg;
1576 struct ForwardedOperationContext *fo_ctxt;
1577 struct Route *route;
1582 uint32_t config_size;
1588 msize = ntohs (message->size);
1589 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1591 GNUNET_break (0); /* We need configuration */
1592 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1595 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1596 host_id = ntohl (msg->host_id);
1597 peer_id = ntohl (msg->peer_id);
1598 if (UINT32_MAX == peer_id)
1600 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1601 "Cannot create peer with given ID");
1602 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1605 if (host_id == master_context->host_id)
1609 /* We are responsible for this peer */
1610 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1611 config_size = ntohl (msg->config_size);
1612 config = GNUNET_malloc (config_size);
1613 dest_size = config_size;
1616 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1617 (const Bytef *) &msg[1], (uLong) msize)))
1619 GNUNET_break (0); /* uncompression error */
1620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1623 if (config_size != dest_size)
1625 GNUNET_break (0); /* Uncompressed config size mismatch */
1626 GNUNET_free (config);
1627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1630 cfg = GNUNET_CONFIGURATION_create ();
1632 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1634 GNUNET_break (0); /* Configuration parsing error */
1635 GNUNET_free (config);
1636 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1639 GNUNET_free (config);
1640 peer = GNUNET_malloc (sizeof (struct Peer));
1641 peer->is_remote = GNUNET_NO;
1642 peer->details.local.cfg = cfg;
1644 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1645 peer->details.local.peer =
1646 GNUNET_TESTING_peer_configure (master_context->system,
1647 peer->details.local.cfg, peer->id,
1648 NULL /* Peer id */ ,
1650 if (NULL == peer->details.local.peer)
1652 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1659 peer->details.local.is_running = GNUNET_NO;
1660 peer_list_add (peer);
1662 GNUNET_malloc (sizeof
1663 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1664 reply->header.size =
1665 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1666 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1667 reply->peer_id = msg->peer_id;
1668 reply->operation_id = msg->operation_id;
1669 queue_message (client, &reply->header);
1670 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1674 /* Forward peer create request */
1675 route = find_dest_route (host_id);
1679 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1682 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1683 GNUNET_SERVER_client_keep (client);
1684 fo_ctxt->client = client;
1685 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1686 fo_ctxt->cls = slave_list[route->dest]->controller;
1688 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1689 [route->dest]->controller,
1690 fo_ctxt->operation_id,
1692 peer_create_success_cb, fo_ctxt);
1693 fo_ctxt->timeout_task =
1694 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1702 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1705 * @param client identification of the client
1706 * @param message the actual message
1709 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1710 const struct GNUNET_MessageHeader *message)
1712 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1713 struct ForwardedOperationContext *fopc;
1717 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1718 peer_id = ntohl (msg->peer_id);
1719 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1720 peer_id, GNUNET_ntohll (msg->operation_id));
1721 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1723 LOG (GNUNET_ERROR_TYPE_ERROR,
1724 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1725 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1726 "Peer doesn't exist");
1727 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1730 peer = peer_list[peer_id];
1731 if (GNUNET_YES == peer->is_remote)
1733 /* Forward the destory message to sub controller */
1734 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1735 GNUNET_SERVER_client_keep (client);
1736 fopc->client = client;
1737 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1739 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1740 fopc->operation_id, &msg->header,
1741 &forwarded_operation_reply_relay,
1743 fopc->timeout_task =
1744 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1746 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1749 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1750 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1751 peer_list_remove (peer);
1753 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1754 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1759 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1762 * @param client identification of the client
1763 * @param message the actual message
1766 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1767 const struct GNUNET_MessageHeader *message)
1769 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1770 struct GNUNET_TESTBED_PeerEventMessage *reply;
1771 struct ForwardedOperationContext *fopc;
1775 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1776 peer_id = ntohl (msg->peer_id);
1777 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1780 LOG (GNUNET_ERROR_TYPE_ERROR,
1781 "Asked to start a non existent peer with id: %u\n", peer_id);
1782 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1785 peer = peer_list[peer_id];
1786 if (GNUNET_YES == peer->is_remote)
1788 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1789 GNUNET_SERVER_client_keep (client);
1790 fopc->client = client;
1791 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1793 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1794 fopc->operation_id, &msg->header,
1795 &forwarded_operation_reply_relay,
1797 fopc->timeout_task =
1798 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1800 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1803 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1805 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1810 peer->details.local.is_running = GNUNET_YES;
1811 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1812 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1813 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1814 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1815 reply->host_id = htonl (master_context->host_id);
1816 reply->peer_id = msg->peer_id;
1817 reply->operation_id = msg->operation_id;
1818 queue_message (client, &reply->header);
1819 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1824 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1827 * @param client identification of the client
1828 * @param message the actual message
1831 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1832 const struct GNUNET_MessageHeader *message)
1834 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1835 struct GNUNET_TESTBED_PeerEventMessage *reply;
1836 struct ForwardedOperationContext *fopc;
1840 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1841 peer_id = ntohl (msg->peer_id);
1842 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1844 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1846 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1849 peer = peer_list[peer_id];
1850 if (GNUNET_YES == peer->is_remote)
1852 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1853 GNUNET_SERVER_client_keep (client);
1854 fopc->client = client;
1855 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1857 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1858 fopc->operation_id, &msg->header,
1859 &forwarded_operation_reply_relay,
1861 fopc->timeout_task =
1862 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1867 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1869 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1870 "Peer not running");
1871 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1874 peer->details.local.is_running = GNUNET_NO;
1875 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1876 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1877 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1878 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1879 reply->host_id = htonl (master_context->host_id);
1880 reply->peer_id = msg->peer_id;
1881 reply->operation_id = msg->operation_id;
1882 queue_message (client, &reply->header);
1883 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1888 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1891 * @param client identification of the client
1892 * @param message the actual message
1895 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1896 const struct GNUNET_MessageHeader *message)
1898 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1899 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1908 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1909 peer_id = ntohl (msg->peer_id);
1910 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1912 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1917 peer = peer_list[peer_id];
1918 if (GNUNET_YES == peer->is_remote)
1920 /* FIXME: forward to sub controller */
1922 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1926 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
1928 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1929 GNUNET_free (config);
1932 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1933 reply = GNUNET_realloc (xconfig, msize);
1934 (void) memmove (&reply[1], reply, xc_size);
1935 reply->header.size = htons (msize);
1936 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1937 reply->peer_id = msg->peer_id;
1938 reply->operation_id = msg->operation_id;
1939 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
1940 &reply->peer_identity);
1941 reply->config_size = htons ((uint16_t) c_size);
1942 queue_message (client, &reply->header);
1943 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1948 * Task for cleaing up overlay connect context structure
1950 * @param cls the overlay connect context
1951 * @param tc the task context
1954 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1956 struct OverlayConnectContext *occ = cls;
1958 LOG_DEBUG ("Cleaning up occ\n");
1959 GNUNET_free_non_null (occ->emsg);
1960 GNUNET_free_non_null (occ->hello);
1961 if (NULL != occ->opc)
1962 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
1963 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1964 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1965 if (NULL != occ->ch)
1966 GNUNET_CORE_disconnect (occ->ch);
1967 if (NULL != occ->ghh)
1968 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1969 if (NULL != occ->p1th)
1970 GNUNET_TRANSPORT_disconnect (occ->p1th);
1971 if (NULL != occ->p2th)
1972 GNUNET_TRANSPORT_disconnect (occ->p2th);
1978 * Task which will be run when overlay connect request has been timed out
1980 * @param cls the OverlayConnectContext
1981 * @param tc the TaskContext
1984 timeout_overlay_connect (void *cls,
1985 const struct GNUNET_SCHEDULER_TaskContext *tc)
1987 struct OverlayConnectContext *occ = cls;
1989 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1990 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1991 GNUNET_SERVER_client_drop (occ->client);
1992 occ_cleanup (occ, tc);
1998 * Function called to notify transport users that another
1999 * peer connected to us.
2001 * @param cls closure
2002 * @param new_peer the peer that connected
2003 * @param ats performance data
2004 * @param ats_count number of entries in ats (excluding 0-termination)
2007 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2008 const struct GNUNET_ATS_Information *ats,
2009 unsigned int ats_count)
2011 struct OverlayConnectContext *occ = cls;
2012 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2014 char *other_peer_str;
2016 LOG_DEBUG ("Overlay connect notify\n");
2018 memcmp (new_peer, &occ->peer_identity,
2019 sizeof (struct GNUNET_PeerIdentity)))
2021 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2022 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2024 memcmp (new_peer, &occ->other_peer_identity,
2025 sizeof (struct GNUNET_PeerIdentity)))
2027 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2028 new_peer_str, other_peer_str);
2029 GNUNET_free (new_peer_str);
2030 GNUNET_free (other_peer_str);
2033 GNUNET_free (new_peer_str);
2034 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2035 GNUNET_i2s (&occ->peer_identity));
2036 GNUNET_free (other_peer_str);
2037 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2039 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2040 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2042 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2043 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2044 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2045 GNUNET_free_non_null (occ->emsg);
2047 if (NULL != occ->p2th)
2048 GNUNET_TRANSPORT_disconnect (occ->p2th);
2050 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2051 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2053 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2054 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2055 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2056 msg->peer1 = htonl (occ->peer->id);
2057 msg->peer2 = htonl (occ->other_peer->id);
2058 msg->operation_id = GNUNET_htonll (occ->op_id);
2059 queue_message (occ->client, &msg->header);
2060 GNUNET_SERVER_client_drop (occ->client);
2061 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2066 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2069 * @param cls the OverlayConnectContext
2070 * @param tc the TaskContext from scheduler
2073 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2075 struct OverlayConnectContext *occ = cls;
2076 char *other_peer_str;
2078 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2079 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2081 GNUNET_assert (NULL != occ->hello);
2082 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2083 if (GNUNET_YES == occ->other_peer->is_remote)
2085 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2087 uint16_t hello_size;
2089 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2090 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2091 hello_size = ntohs (occ->hello->size);
2092 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2093 msg = GNUNET_malloc (msize);
2094 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2095 msg->header.size = htons (msize);
2096 msg->peer = htonl (occ->peer->id);
2097 msg->operation_id = GNUNET_htonll (occ->op_id);
2098 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2099 sizeof (struct GNUNET_PeerIdentity));
2100 memcpy (msg->hello, occ->hello, hello_size);
2101 GNUNET_TESTBED_queue_message_ (occ->other_peer->details.remote.controller,
2106 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2107 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2108 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2109 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2111 GNUNET_free (other_peer_str);
2112 occ->send_hello_task =
2113 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &send_hello, occ);
2117 * Test for checking whether HELLO message is empty
2119 * @param cls empty flag to set
2120 * @param address the HELLO
2121 * @param expiration expiration of the HELLO
2125 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2126 struct GNUNET_TIME_Absolute expiration)
2136 * Function called whenever there is an update to the HELLO of peers in the
2137 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2138 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2140 * @param cls closure
2141 * @param hello our updated HELLO
2144 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2146 struct OverlayConnectContext *occ = cls;
2150 msize = ntohs (hello->size);
2152 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2153 hello, GNUNET_NO, &test_address,
2155 if (GNUNET_YES == empty)
2157 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2160 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2161 occ->hello = GNUNET_malloc (msize);
2162 memcpy (occ->hello, hello, msize);
2163 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2165 GNUNET_TRANSPORT_disconnect (occ->p1th);
2167 GNUNET_free_non_null (occ->emsg);
2168 if (GNUNET_NO == occ->other_peer->is_remote)
2171 GNUNET_TRANSPORT_connect (occ->other_peer->details.local.cfg,
2172 &occ->other_peer_identity, NULL, NULL, NULL,
2174 if (NULL == occ->p2th)
2176 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2177 GNUNET_i2s (&occ->other_peer_identity));
2178 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2179 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2183 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2184 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2189 * Function called after GNUNET_CORE_connect has succeeded (or failed
2190 * for good). Note that the private key of the peer is intentionally
2191 * not exposed here; if you need it, your process should try to read
2192 * the private key file directly (which should work if you are
2195 * @param cls closure
2196 * @param server handle to the server, NULL if we failed
2197 * @param my_identity ID of this peer, NULL if we failed
2200 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2201 const struct GNUNET_PeerIdentity *my_identity)
2203 struct OverlayConnectContext *occ = cls;
2205 GNUNET_free_non_null (occ->emsg);
2206 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2207 if ((NULL == server) || (NULL == my_identity))
2209 GNUNET_free (occ->emsg);
2212 memcpy (&occ->peer_identity, my_identity,
2213 sizeof (struct GNUNET_PeerIdentity));
2215 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2216 &occ->peer_identity, NULL, NULL, NULL, NULL);
2217 if (NULL == occ->p1th)
2219 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2220 GNUNET_i2s (&occ->peer_identity));
2223 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2224 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2225 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2229 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2230 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2236 * Callback to be called when forwarded get peer config operation as part of
2237 * overlay connect is successfull. Connection to Peer 1's core is made and is
2238 * checked for new connection from peer 2
2240 * @param cls ForwardedOperationContext
2241 * @param msg the peer create success message
2244 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2246 struct OverlayConnectContext *occ = cls;
2247 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2248 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2253 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2255 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2257 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2258 sizeof (struct GNUNET_PeerIdentity));
2259 GNUNET_free_non_null (occ->emsg);
2260 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2262 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2263 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2264 GNUNET_NO, no_handlers);
2265 if (NULL == occ->ch)
2270 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2272 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2277 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2280 * @param client identification of the client
2281 * @param message the actual message
2284 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2285 const struct GNUNET_MessageHeader *message)
2287 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2288 struct OverlayConnectContext *occ;
2289 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2295 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2296 p1 = ntohl (msg->peer1);
2297 p2 = ntohl (msg->peer2);
2298 GNUNET_assert (p1 < peer_list_size);
2299 GNUNET_assert (NULL != peer_list[p1]);
2300 GNUNET_assert (p2 < peer_list_size);
2301 GNUNET_assert (NULL != peer_list[p2]);
2302 /* FIXME: Add cases where we have to forward overlay connect message to sub
2304 GNUNET_assert (GNUNET_NO == peer_list[p1]->is_remote);
2305 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2306 GNUNET_SERVER_client_keep (client);
2307 occ->client = client;
2308 occ->peer = peer_list[p1];
2309 occ->other_peer = peer_list[p2];
2310 occ->op_id = GNUNET_ntohll (msg->operation_id);
2311 /* Get the identity of the second peer */
2312 if (GNUNET_YES == occ->other_peer->is_remote)
2314 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2317 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2318 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2319 cmsg.peer_id = msg->peer2;
2320 cmsg.operation_id = msg->operation_id;
2322 GNUNET_TESTBED_forward_operation_msg_ (occ->other_peer->details.remote.controller,
2323 occ->op_id, &cmsg.header,
2324 &overlay_connect_get_config,
2327 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2329 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2330 (GNUNET_TIME_UNIT_SECONDS, 30),
2331 &timeout_overlay_connect, occ);
2334 GNUNET_TESTING_peer_get_identity (occ->other_peer->details.local.peer,
2335 &occ->other_peer_identity);
2336 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2337 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2339 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2340 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2341 GNUNET_NO, no_handlers);
2342 if (NULL == occ->ch)
2344 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2347 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2348 (GNUNET_TIME_UNIT_SECONDS, 30),
2349 &timeout_overlay_connect, occ);
2350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2355 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2358 * @param client identification of the client
2359 * @param message the actual message
2362 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2363 const struct GNUNET_MessageHeader *message)
2365 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2366 struct GNUNET_TRANSPORT_Handle *th;
2370 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= ntohs
2374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2377 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2378 if ((NULL == msg->hello) ||
2379 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2385 peer_id = ntohl (msg->peer);
2386 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2392 if (GNUNET_NO != peer->is_remote)
2395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2398 th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, NULL, NULL,
2403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2406 GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL);
2407 GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity);
2408 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2413 * Iterator over hash map entries.
2415 * @param cls closure
2416 * @param key current key code
2417 * @param value value in the hash map
2418 * @return GNUNET_YES if we should continue to
2423 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2425 struct SharedService *ss = value;
2427 GNUNET_assert (GNUNET_YES ==
2428 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2429 GNUNET_free (ss->name);
2436 * Task to clean up and shutdown nicely
2439 * @param tc the TaskContext from scheduler
2442 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2444 struct LCFContextQueue *lcfq;
2447 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2448 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2449 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2451 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2452 if (NULL != lcfq_head)
2454 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2456 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2457 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2459 if (NULL != lcfq_head->lcf->rhandle)
2460 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2462 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2463 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2465 GNUNET_free (lcfq->lcf->msg);
2466 GNUNET_free (lcfq->lcf);
2467 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2470 /* Clear peer list */
2471 for (id = 0; id < peer_list_size; id++)
2472 if (NULL != peer_list[id])
2474 if (GNUNET_NO == peer_list[id]->is_remote)
2476 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2477 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2478 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2479 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2481 GNUNET_free (peer_list[id]);
2483 GNUNET_free_non_null (peer_list);
2484 /* Clear host list */
2485 for (id = 0; id < host_list_size; id++)
2486 if (NULL != host_list[id])
2487 GNUNET_TESTBED_host_destroy (host_list[id]);
2488 GNUNET_free_non_null (host_list);
2489 /* Clear route list */
2490 for (id = 0; id < route_list_size; id++)
2491 if (NULL != route_list[id])
2492 GNUNET_free (route_list[id]);
2493 GNUNET_free_non_null (route_list);
2494 /* Clear slave_list */
2495 for (id = 0; id < slave_list_size; id++)
2496 if (NULL != slave_list[id])
2498 if (NULL != slave_list[id]->controller)
2499 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2500 if (NULL != slave_list[id]->controller_proc)
2501 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2503 if (NULL != master_context)
2505 GNUNET_free_non_null (master_context->master_ip);
2506 if (NULL != master_context->system)
2507 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2508 GNUNET_free (master_context);
2509 master_context = NULL;
2515 * Callback for client disconnect
2518 * @param client the client which has disconnected
2521 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2523 if (NULL == master_context)
2525 if (client == master_context->client)
2527 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2528 GNUNET_SERVER_client_drop (client);
2529 /* should not be needed as we're terminated by failure to read
2530 * from stdin, but if stdin fails for some reason, this shouldn't
2531 * hurt for now --- might need to revise this later if we ever
2532 * decide that master connections might be temporarily down
2533 * for some reason */
2534 //GNUNET_SCHEDULER_shutdown ();
2542 * @param cls closure
2543 * @param server the initialized server
2544 * @param cfg configuration to use
2547 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2548 const struct GNUNET_CONFIGURATION_Handle *cfg)
2550 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2551 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2552 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2553 {&handle_configure_shared_service, NULL,
2554 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2555 {&handle_link_controllers, NULL,
2556 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2557 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2558 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2559 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2560 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2561 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2562 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2563 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2564 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2565 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2566 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2567 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2568 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2569 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2573 GNUNET_SERVER_add_handlers (server, message_handlers);
2574 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2575 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2577 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2578 &shutdown_task, NULL);
2579 LOG_DEBUG ("Testbed startup complete\n");
2584 * The starting point of execution
2587 main (int argc, char *const *argv)
2589 //sleep (15); /* Debugging */
2590 return (GNUNET_OK ==
2591 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2592 &testbed_run, NULL)) ? 0 : 1;
2595 /* end of gnunet-service-testbed.c */