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 * Our host id according to this context
91 * The message queue for sending messages to clients
96 * The message to be sent
98 struct GNUNET_MessageHeader *msg;
101 * The client to send the message to
103 struct GNUNET_SERVER_Client *client;
106 * next pointer for DLL
108 struct MessageQueue *next;
111 * prev pointer for DLL
113 struct MessageQueue *prev;
118 * The structure for identifying a shared service
123 * The name of the shared service
128 * Number of shared peers per instance of the shared service
133 * Number of peers currently sharing the service
135 uint32_t num_sharing;
150 * The destination host is reachable thru
157 * Context information used while linking controllers
159 struct LinkControllersContext;
163 * A DLL of host registrations to be made
165 struct HostRegistration
168 * next registration in the DLL
170 struct HostRegistration *next;
173 * previous registration in the DLL
175 struct HostRegistration *prev;
178 * The callback to call after this registration's status is available
180 GNUNET_TESTBED_HostRegistrationCompletion cb;
183 * The closure for the above callback
188 * The host that has to be registered
190 struct GNUNET_TESTBED_Host *host;
196 * Structure representing a connected(directly-linked) controller
201 * The controller process handle if we had started the controller
203 struct GNUNET_TESTBED_ControllerProc *controller_proc;
206 * The controller handle
208 struct GNUNET_TESTBED_Controller *controller;
211 * The configuration of the slave. Cannot be NULL
213 struct GNUNET_CONFIGURATION_Handle *cfg;
216 * handle to lcc which is associated with this slave startup. Should be set to
217 * NULL when the slave has successfully started up
219 struct LinkControllersContext *lcc;
222 * Head of the host registration DLL
224 struct HostRegistration *hr_dll_head;
227 * Tail of the host registration DLL
229 struct HostRegistration *hr_dll_tail;
232 * The id of the host this controller is running on
240 * States of LCFContext
245 * The Context has been initialized; Nothing has been done on it
250 * Delegated host has been registered at the forwarding controller
252 DELEGATED_HOST_REGISTERED,
255 * The slave host has been registred at the forwarding controller
257 SLAVE_HOST_REGISTERED,
260 * The context has been finished (may have error)
267 * Link controllers request forwarding context
272 * The gateway which will pass the link message to delegated host
274 struct Slave *gateway;
277 * The controller link message that has to be forwarded to
279 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
282 * The client which has asked to perform this operation
284 struct GNUNET_SERVER_Client *client;
287 * The host registration handle while registered hosts in this context
289 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
292 * The id of the operation which created this context
294 uint64_t operation_id;
297 * The state of this context
299 enum LCFContextState state;
304 uint32_t delegated_host_id;
309 uint32_t slave_host_id;
315 * Structure of a queue entry in LCFContext request queue
317 struct LCFContextQueue
322 struct LCFContext *lcf;
327 struct LCFContextQueue *next;
332 struct LCFContextQueue *prev;
346 * The peer handle from testing API
348 struct GNUNET_TESTING_Peer *peer;
351 * The modified (by GNUNET_TESTING_peer_configure) configuration this
352 * peer is configured with
354 struct GNUNET_CONFIGURATION_Handle *cfg;
357 * Is the peer running
366 * The slave this peer is started through
371 * The id of the remote host this peer is running on
373 uint32_t remote_host_id;
380 * Is this peer locally created?
385 * Our local reference id for this peer
393 * Context information for connecting 2 peers in overlay
395 struct OverlayConnectContext
398 * The client which has requested for overlay connection
400 struct GNUNET_SERVER_Client *client;
403 * the peer which has to connect to the other peer
408 * Transport handle of the first peer to get its HELLO
410 struct GNUNET_TRANSPORT_Handle *p1th;
413 * Transport handle of other peer to offer first peer's HELLO
415 struct GNUNET_TRANSPORT_Handle *p2th;
418 * Core handles of the first peer; used to notify when second peer connects to it
420 struct GNUNET_CORE_Handle *ch;
423 * HELLO of the other peer
425 struct GNUNET_MessageHeader *hello;
428 * Get hello handle to acquire HELLO of first peer
430 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
433 * The error message we send if this overlay connect operation has timed out
438 * Operation context for suboperations
440 struct OperationContext *opc;
443 * Controller of peer 2; NULL if the peer is local
445 struct GNUNET_TESTBED_Controller *peer2_controller;
448 * The peer identity of the first peer
450 struct GNUNET_PeerIdentity peer_identity;
453 * The peer identity of the other peer
455 struct GNUNET_PeerIdentity other_peer_identity;
458 * The id of the operation responsible for creating this context
463 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
466 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
469 * The id of the overlay connect timeout task
471 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
481 uint32_t other_peer_id;
484 * Number of times we tried to send hello; used to increase delay in offering
492 * Context information for RequestOverlayConnect
493 * operations. RequestOverlayConnect is used when peers A, B reside on different
494 * hosts and the host controller for peer B is asked by the host controller of
495 * peer A to make peer B connect to peer A
497 struct RequestOverlayConnectContext
500 * The transport handle of peer B
502 struct GNUNET_TRANSPORT_Handle *th;
507 struct GNUNET_MessageHeader *hello;
510 * The peer identity of peer A
512 struct GNUNET_PeerIdentity a_id;
515 * Task for offering HELLO of A to B and doing try_connect
517 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
520 * Task to timeout RequestOverlayConnect
522 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
525 * Number of times we tried to send hello; used to increase delay in offering
534 * Context information for operations forwarded to subcontrollers
536 struct ForwardedOperationContext
539 * The generated operation context
541 struct OperationContext *opc;
544 * The client to which we have to reply
546 struct GNUNET_SERVER_Client *client;
554 * Task ID for the timeout task
556 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
559 * The id of the operation that has been forwarded
561 uint64_t operation_id;
567 * Context information used while linking controllers
569 struct LinkControllersContext
572 * The client which initiated the link controller operation
574 struct GNUNET_SERVER_Client *client;
577 * The ID of the operation
579 uint64_t operation_id;
585 * Context information to used during operations which forward the overlay
588 struct ForwardedOverlayConnectContext
591 * The gateway controller to which this operation is forwarded to
593 struct GNUNET_TESTBED_Controller *gateway;
596 * The gateway controller through which peer2's controller can be reached
598 struct GNUNET_TESTBED_Controller *gateway2;
601 * Handle for sub-operations
603 struct GNUNET_TESTBED_Operation *sub_op;
606 * The client which initiated the link controller operation
608 struct GNUNET_SERVER_Client *client;
611 * A copy of the original overlay connect message
613 struct GNUNET_MessageHeader *orig_msg;
616 * The host registration handle while registered hosts in this context
618 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
621 * The id of the operation which created this context information
623 uint64_t operation_id;
626 * Id of the timeout task;
628 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
631 * Enumeration of states for this context
641 * State where we attempt to register peer2's controller with peer1's controller
646 * State where we attempt to get peer2's controller configuration
651 * State where we attempt to link the controller of peer 1 to the controller
657 * State where we attempt to do the overlay connection again
674 * Id of the host where peer2 is running
676 uint32_t peer2_host_id;
682 * The master context; generated with the first INIT message
684 static struct Context *master_context;
687 * Our hostname; we give this to all the peers we start
689 static char *hostname;
699 static struct GNUNET_CONFIGURATION_Handle *our_config;
702 * Current Transmit Handle; NULL if no notify transmit exists currently
704 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
711 * The head for the LCF queue
713 static struct LCFContextQueue *lcfq_head;
716 * The tail for the LCF queue
718 static struct LCFContextQueue *lcfq_tail;
721 * The message queue head
723 static struct MessageQueue *mq_head;
726 * The message queue tail
728 static struct MessageQueue *mq_tail;
733 static struct GNUNET_TESTBED_Host **host_list;
738 static struct Route **route_list;
741 * A list of directly linked neighbours
743 static struct Slave **slave_list;
746 * A list of peers we know about
748 static struct Peer **peer_list;
751 * The hashmap of shared services
753 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
756 * The event mask for the events we listen from sub-controllers
758 static uint64_t event_mask;
761 * The size of the host list
763 static uint32_t host_list_size;
766 * The size of the route list
768 static uint32_t route_list_size;
771 * The size of directly linked neighbours list
773 static uint32_t slave_list_size;
776 * The size of the peer list
778 static uint32_t peer_list_size;
785 * The lcf_task handle
787 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
790 * The shutdown task handle
792 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
796 * Function called to notify a client about the connection begin ready to queue
797 * more data. "buf" will be NULL and "size" zero if the connection was closed
798 * for writing in the meantime.
801 * @param size number of bytes available in buf
802 * @param buf where the callee should write the message
803 * @return number of bytes written to buf
806 transmit_ready_notify (void *cls, size_t size, void *buf)
808 struct MessageQueue *mq_entry;
810 transmit_handle = NULL;
812 GNUNET_assert (NULL != mq_entry);
815 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
816 size = ntohs (mq_entry->msg->size);
817 memcpy (buf, mq_entry->msg, size);
818 GNUNET_free (mq_entry->msg);
819 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
820 GNUNET_free (mq_entry);
822 if (NULL != mq_entry)
824 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
825 ntohs (mq_entry->msg->size),
826 GNUNET_TIME_UNIT_FOREVER_REL,
827 &transmit_ready_notify, NULL);
833 * Queues a message in send queue for sending to the service
835 * @param client the client to whom the queued message has to be sent
836 * @param msg the message to queue
839 queue_message (struct GNUNET_SERVER_Client *client,
840 struct GNUNET_MessageHeader *msg)
842 struct MessageQueue *mq_entry;
846 type = ntohs (msg->type);
847 size = ntohs (msg->size);
848 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
849 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
850 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
852 mq_entry->client = client;
853 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
855 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
856 if (NULL == transmit_handle)
858 GNUNET_SERVER_notify_transmit_ready (client, size,
859 GNUNET_TIME_UNIT_FOREVER_REL,
860 &transmit_ready_notify, NULL);
865 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
867 * @param ptr the memory block to realloc
868 * @param size the size of ptr
869 * @param new_size the size to which ptr has to be realloc'ed
870 * @return the newly reallocated memory block
873 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
875 ptr = GNUNET_realloc (ptr, new_size);
877 (void) memset (ptr + size, 0, new_size - size);
883 * Function to add a host to the current list of known hosts
885 * @param host the host to add
886 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
890 host_list_add (struct GNUNET_TESTBED_Host *host)
895 host_id = GNUNET_TESTBED_host_get_id_ (host);
896 orig_size = host_list_size;
897 if (host_list_size <= host_id)
899 while (host_list_size <= host_id)
900 host_list_size += LIST_GROW_STEP;
902 TESTBED_realloc (host_list,
903 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
904 sizeof (struct GNUNET_TESTBED_Host *)
907 if (NULL != host_list[host_id])
909 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
910 return GNUNET_SYSERR;
912 host_list[host_id] = host;
918 * Adds a route to the route list
920 * @param route the route to add
923 route_list_add (struct Route *route)
927 orig_size = route_list_size;
928 if (route->dest >= route_list_size)
930 while (route->dest >= route_list_size)
931 route_list_size += LIST_GROW_STEP;
933 TESTBED_realloc (route_list,
934 sizeof (struct Route *) * orig_size,
935 sizeof (struct Route *) * route_list_size);
937 GNUNET_assert (NULL == route_list[route->dest]);
938 route_list[route->dest] = route;
943 * Adds a slave to the slave array
945 * @param slave the slave controller to add
948 slave_list_add (struct Slave *slave)
950 if (slave->host_id >= slave_list_size)
953 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
954 sizeof (struct Slave *) * (slave_list_size +
956 slave_list_size += LIST_GROW_STEP;
958 GNUNET_assert (NULL == slave_list[slave->host_id]);
959 slave_list[slave->host_id] = slave;
964 * Adds a peer to the peer array
966 * @param peer the peer to add
969 peer_list_add (struct Peer *peer)
973 orig_size = peer_list_size;
974 if (peer->id >= peer_list_size)
976 while (peer->id >= peer_list_size)
977 peer_list_size += LIST_GROW_STEP;
979 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
980 sizeof (struct Peer *) * peer_list_size);
982 GNUNET_assert (NULL == peer_list[peer->id]);
983 peer_list[peer->id] = peer;
988 * Removes a the give peer from the peer array
990 * @param peer the peer to be removed
993 peer_list_remove (struct Peer *peer)
998 peer_list[peer->id] = NULL;
999 orig_size = peer_list_size;
1000 while (peer_list_size >= LIST_GROW_STEP)
1002 for (id = peer_list_size - 1;
1003 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1004 if (NULL != peer_list[id])
1006 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1008 peer_list_size -= LIST_GROW_STEP;
1010 if (orig_size == peer_list_size)
1013 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1018 * Finds the route with directly connected host as destination through which
1019 * the destination host can be reached
1021 * @param host_id the id of the destination host
1022 * @return the route with directly connected destination host; NULL if no route
1025 static struct Route *
1026 find_dest_route (uint32_t host_id)
1028 struct Route *route;
1030 if (route_list_size <= host_id)
1032 while (NULL != (route = route_list[host_id]))
1034 if (route->thru == master_context->host_id)
1036 host_id = route->thru;
1043 * Routes message to a host given its host_id
1045 * @param host_id the id of the destination host
1046 * @param msg the message to be routed
1049 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1056 * Send operation failure message to client
1058 * @param client the client to which the failure message has to be sent to
1059 * @param operation_id the id of the failed operation
1060 * @param emsg the error message; can be NULL
1063 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1064 uint64_t operation_id, const char *emsg)
1066 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1070 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1071 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1073 msg = GNUNET_malloc (msize);
1074 msg->header.size = htons (msize);
1075 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1076 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1077 msg->operation_id = GNUNET_htonll (operation_id);
1079 memcpy (&msg[1], emsg, emsg_len);
1080 queue_message (client, &msg->header);
1085 * Function to send generic operation success message to given client
1087 * @param client the client to send the message to
1088 * @param operation_id the id of the operation which was successful
1091 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1092 uint64_t operation_id)
1094 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1097 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1098 msg = GNUNET_malloc (msize);
1099 msg->header.size = htons (msize);
1100 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1101 msg->operation_id = GNUNET_htonll (operation_id);
1102 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1103 queue_message (client, &msg->header);
1108 * The Link Controller forwarding task
1110 * @param cls the LCFContext
1111 * @param tc the Task context from scheduler
1114 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1118 * Completion callback for host registrations while forwarding Link Controller messages
1120 * @param cls the LCFContext
1121 * @param emsg the error message; NULL if host registration is successful
1124 lcf_proc_cc (void *cls, const char *emsg)
1126 struct LCFContext *lcf = cls;
1128 lcf->rhandle = NULL;
1129 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1134 goto registration_error;
1135 lcf->state = DELEGATED_HOST_REGISTERED;
1136 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1138 case DELEGATED_HOST_REGISTERED:
1140 goto registration_error;
1141 lcf->state = SLAVE_HOST_REGISTERED;
1142 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1145 GNUNET_assert (0); /* Shouldn't reach here */
1150 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1152 lcf->state = FINISHED;
1153 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1158 * Callback to be called when forwarded link controllers operation is
1159 * successfull. We have to relay the reply msg back to the client
1161 * @param cls ForwardedOperationContext
1162 * @param msg the peer create success message
1165 forwarded_operation_reply_relay (void *cls,
1166 const struct GNUNET_MessageHeader *msg)
1168 struct ForwardedOperationContext *fopc = cls;
1169 struct GNUNET_MessageHeader *dup_msg;
1172 msize = ntohs (msg->size);
1173 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1175 dup_msg = GNUNET_malloc (msize);
1176 (void) memcpy (dup_msg, msg, msize);
1177 queue_message (fopc->client, dup_msg);
1178 GNUNET_SERVER_client_drop (fopc->client);
1179 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1185 * Task to free resources when forwarded link controllers has been timedout
1187 * @param cls the ForwardedOperationContext
1188 * @param tc the task context from scheduler
1191 forwarded_operation_timeout (void *cls,
1192 const struct GNUNET_SCHEDULER_TaskContext *tc)
1194 struct ForwardedOperationContext *fopc = cls;
1196 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1197 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1198 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1199 GNUNET_SERVER_client_drop (fopc->client);
1205 * The Link Controller forwarding task
1207 * @param cls the LCFContext
1208 * @param tc the Task context from scheduler
1211 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1213 struct LCFContext *lcf = cls;
1214 struct LCFContextQueue *lcfq;
1215 struct ForwardedOperationContext *fopc;
1217 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1222 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1223 lcf->gateway->controller))
1226 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1227 host_list[lcf->delegated_host_id],
1232 lcf->state = DELEGATED_HOST_REGISTERED;
1233 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1236 case DELEGATED_HOST_REGISTERED:
1238 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1239 lcf->gateway->controller))
1242 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1243 host_list[lcf->slave_host_id],
1248 lcf->state = SLAVE_HOST_REGISTERED;
1249 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1252 case SLAVE_HOST_REGISTERED:
1253 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1254 fopc->client = lcf->client;
1255 fopc->operation_id = lcf->operation_id;
1257 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1260 &forwarded_operation_reply_relay,
1262 fopc->timeout_task =
1263 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1265 lcf->state = FINISHED;
1266 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1270 GNUNET_assert (lcfq->lcf == lcf);
1271 GNUNET_free (lcf->msg);
1273 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1275 if (NULL != lcfq_head)
1277 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1283 * Callback to be called when forwarded overlay connection operation has a reply
1284 * from the sub-controller successfull. We have to relay the reply msg back to
1287 * @param cls ForwardedOperationContext
1288 * @param msg the peer create success message
1291 forwarded_overlay_connect_listener (void *cls,
1292 const struct GNUNET_MessageHeader *msg);
1296 * Cleans up ForwardedOverlayConnectContext
1298 * @param focc the ForwardedOverlayConnectContext to cleanup
1301 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1303 if (GNUNET_SCHEDULER_NO_TASK != focc->timeout_task)
1304 GNUNET_SCHEDULER_cancel (focc->timeout_task);
1305 if (NULL != focc->sub_op)
1306 GNUNET_TESTBED_operation_done (focc->sub_op);
1307 if (NULL != focc->client)
1308 GNUNET_SERVER_client_drop (focc->client);
1309 if (NULL != focc->rhandle)
1310 GNUNET_TESTBED_cancel_registration (focc->rhandle);
1311 GNUNET_free_non_null (focc->orig_msg);
1316 * Callback for event from slave controllers
1318 * @param cls struct Slave *
1319 * @param event information about the event
1322 slave_event_callback (void *cls,
1323 const struct GNUNET_TESTBED_EventInformation *event)
1325 struct ForwardedOverlayConnectContext *focc;
1326 struct ForwardedOperationContext *fopc;
1327 struct GNUNET_CONFIGURATION_Handle *slave_cfg;
1328 struct GNUNET_TESTBED_Operation *old_op;
1331 /* We currently only get here when doing overlay connect operations and that
1332 too while trying out sub operations */
1333 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1334 focc = event->details.operation_finished.op_cls;
1335 LOG_DEBUG ("Operation successful\n");
1336 if (NULL != event->details.operation_finished.emsg)
1338 GNUNET_asprintf (&emsg, "Failure executing suboperation: %s",
1339 event->details.operation_finished.emsg);
1340 send_operation_fail_msg (focc->client, focc->operation_id,
1343 cleanup_focc (focc);
1346 switch (focc->state)
1349 slave_cfg = event->details.operation_finished.generic;
1350 old_op = focc->sub_op;
1351 focc->state = FOCC_LINK;
1352 focc->sub_op = GNUNET_TESTBED_controller_link_ (focc,
1354 focc->peer2_host_id,
1355 peer_list[focc->peer1]->details.remote.remote_host_id,
1358 GNUNET_TESTBED_operation_done (old_op);
1361 LOG_DEBUG ("OL: Linking controllers successfull\n");
1362 GNUNET_TESTBED_operation_done (focc->sub_op);
1363 focc->sub_op = NULL;
1364 focc->state = FOCC_OL_CONNECT;
1365 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1366 fopc->client = focc->client;
1367 focc->client = NULL;
1368 fopc->operation_id = focc->operation_id;
1371 GNUNET_TESTBED_forward_operation_msg_ (focc->gateway,
1372 focc->operation_id, focc->orig_msg,
1373 &forwarded_operation_reply_relay,
1375 GNUNET_free (focc->orig_msg);
1376 focc->orig_msg = NULL;
1377 fopc->timeout_task =
1378 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1380 cleanup_focc (focc);
1390 * Callback to signal successfull startup of the controller process
1392 * @param cls the handle to the slave whose status is to be found here
1393 * @param cfg the configuration with which the controller has been started;
1394 * NULL if status is not GNUNET_OK
1395 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1396 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1399 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1402 struct Slave *slave = cls;
1403 struct LinkControllersContext *lcc;
1406 if (GNUNET_SYSERR == status)
1408 slave->controller_proc = NULL;
1409 slave_list[slave->host_id] = NULL;
1410 if (NULL != slave->cfg)
1411 GNUNET_CONFIGURATION_destroy (slave->cfg);
1412 GNUNET_free (slave);
1414 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1415 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1419 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1421 &slave_event_callback, slave);
1422 if (NULL != slave->controller)
1424 send_operation_success_msg (lcc->client, lcc->operation_id);
1425 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1429 send_operation_fail_msg (lcc->client, lcc->operation_id,
1430 "Could not connect to delegated controller");
1431 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1432 slave_list[slave->host_id] = NULL;
1433 GNUNET_free (slave);
1440 if (NULL != lcc->client)
1442 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1443 GNUNET_SERVER_client_drop (lcc->client);
1454 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1457 * @param client identification of the client
1458 * @param message the actual message
1461 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1462 const struct GNUNET_MessageHeader *message)
1464 const struct GNUNET_TESTBED_InitMessage *msg;
1465 struct GNUNET_TESTBED_Host *host;
1466 const char *controller_hostname;
1469 if (NULL != master_context)
1471 LOG_DEBUG ("We are being connected to laterally\n");
1472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1475 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1476 msize = ntohs (message->size);
1477 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1483 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1484 controller_hostname = (const char *) &msg[1];
1485 if ('\0' != controller_hostname[msize - 1])
1488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1491 master_context = GNUNET_malloc (sizeof (struct Context));
1492 master_context->client = client;
1493 master_context->host_id = ntohl (msg->host_id);
1494 master_context->master_ip = GNUNET_strdup (controller_hostname);
1495 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1496 master_context->system =
1497 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1499 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1500 master_context->master_ip,
1503 host_list_add (host);
1504 GNUNET_SERVER_client_keep (client);
1505 LOG_DEBUG ("Created master context with host ID: %u\n",
1506 master_context->host_id);
1507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1512 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1515 * @param client identification of the client
1516 * @param message the actual message
1519 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1520 const struct GNUNET_MessageHeader *message)
1522 struct GNUNET_TESTBED_Host *host;
1523 const struct GNUNET_TESTBED_AddHostMessage *msg;
1524 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1529 uint16_t username_length;
1530 uint16_t hostname_length;
1531 uint16_t reply_size;
1534 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1535 msize = ntohs (msg->header.size);
1536 username = (char *) &msg[1];
1537 username_length = ntohs (msg->user_name_length);
1538 if (0 != username_length)
1540 /* msg must contain hostname */
1541 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1542 username_length + 1));
1543 if (0 != username_length)
1544 GNUNET_assert ('\0' == username[username_length - 1]);
1545 hostname = username + username_length;
1547 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1548 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1549 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1550 host_id = ntohl (msg->host_id);
1551 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1552 LOG_DEBUG ("-------host id: %u\n", host_id);
1553 LOG_DEBUG ("-------hostname: %s\n", hostname);
1554 if (0 != username_length)
1555 LOG_DEBUG ("-------username: %s\n", username);
1558 LOG_DEBUG ("-------username: NULL\n");
1561 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1563 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1564 ntohs (msg->ssh_port));
1565 GNUNET_assert (NULL != host);
1566 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1567 if (GNUNET_OK != host_list_add (host))
1569 /* We are unable to add a host */
1570 emsg = "A host exists with given host-id";
1571 LOG_DEBUG ("%s: %u", emsg, host_id);
1572 GNUNET_TESTBED_host_destroy (host);
1573 reply_size += strlen (emsg) + 1;
1574 reply = GNUNET_malloc (reply_size);
1575 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1579 LOG_DEBUG ("Added host %u at %u\n",
1580 host_id, master_context->host_id);
1581 reply = GNUNET_malloc (reply_size);
1583 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1584 reply->header.size = htons (reply_size);
1585 reply->host_id = htonl (host_id);
1586 queue_message (client, &reply->header);
1587 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1592 * Iterator over hash map entries.
1594 * @param cls closure
1595 * @param key current key code
1596 * @param value value in the hash map
1597 * @return GNUNET_YES if we should continue to
1602 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1604 struct SharedService *queried_ss = cls;
1605 struct SharedService *ss = value;
1607 if (0 == strcmp (ss->name, queried_ss->name))
1615 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1618 * @param client identification of the client
1619 * @param message the actual message
1622 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1623 const struct GNUNET_MessageHeader *message)
1625 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1626 struct SharedService *ss;
1628 struct GNUNET_HashCode hash;
1630 uint16_t service_name_size;
1632 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1633 msg_size = ntohs (message->size);
1634 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1641 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1642 service_name = (char *) &msg[1];
1643 if ('\0' != service_name[service_name_size - 1])
1646 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1649 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1650 service_name, ntohl (msg->num_peers));
1651 if (ntohl (msg->host_id) != master_context->host_id)
1653 route_message (ntohl (msg->host_id), message);
1654 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1658 ss = GNUNET_malloc (sizeof (struct SharedService));
1659 ss->name = strdup (service_name);
1660 ss->num_shared = ntohl (msg->num_peers);
1661 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1662 if (GNUNET_SYSERR ==
1663 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1664 &ss_exists_iterator, ss))
1666 LOG (GNUNET_ERROR_TYPE_WARNING,
1667 "Service %s already configured as a shared service. "
1668 "Ignoring service sharing request \n", ss->name);
1669 GNUNET_free (ss->name);
1673 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1674 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1679 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1682 * @param client identification of the client
1683 * @param message the actual message
1686 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1687 const struct GNUNET_MessageHeader *message)
1689 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1690 struct GNUNET_CONFIGURATION_Handle *cfg;
1691 struct LCFContextQueue *lcfq;
1692 struct Route *route;
1693 struct Route *new_route;
1697 uint32_t delegated_host_id;
1698 uint32_t slave_host_id;
1701 if (NULL == master_context)
1704 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1707 msize = ntohs (message->size);
1708 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1714 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1715 delegated_host_id = ntohl (msg->delegated_host_id);
1716 if (delegated_host_id == master_context->host_id)
1719 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1723 if ((delegated_host_id >= host_list_size) ||
1724 (NULL == host_list[delegated_host_id]))
1726 LOG (GNUNET_ERROR_TYPE_WARNING,
1727 "Delegated host %u not registered with us\n", delegated_host_id);
1728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1731 slave_host_id = ntohl (msg->slave_host_id);
1732 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1734 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1739 if (slave_host_id == delegated_host_id)
1741 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1746 if (slave_host_id == master_context->host_id) /* Link from us */
1748 struct Slave *slave;
1749 struct LinkControllersContext *lcc;
1751 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1752 config_size = ntohs (msg->config_size);
1753 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1755 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1757 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1760 config = GNUNET_malloc (config_size);
1761 dest_size = (uLongf) config_size;
1763 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1766 GNUNET_break (0); /* Compression error */
1767 GNUNET_free (config);
1768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1771 if (config_size != dest_size)
1773 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1774 GNUNET_free (config);
1775 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1778 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1780 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1782 GNUNET_break (0); /* Configuration parsing error */
1783 GNUNET_free (config);
1784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1787 GNUNET_free (config);
1788 if ((delegated_host_id < slave_list_size) &&
1789 (NULL != slave_list[delegated_host_id]))
1791 GNUNET_break (0); /* Configuration parsing error */
1792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1795 slave = GNUNET_malloc (sizeof (struct Slave));
1796 slave->host_id = delegated_host_id;
1797 slave_list_add (slave);
1798 if (1 != msg->is_subordinate)
1801 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1803 &slave_event_callback, slave);
1805 if (NULL != slave->controller)
1806 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1808 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1809 "Could not connect to delegated controller");
1810 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1813 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1814 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1815 GNUNET_SERVER_client_keep (client);
1816 lcc->client = client;
1818 slave->controller_proc =
1819 GNUNET_TESTBED_controller_start (master_context->master_ip,
1820 host_list[slave->host_id], cfg,
1821 &slave_status_callback, slave);
1822 GNUNET_CONFIGURATION_destroy (cfg);
1823 new_route = GNUNET_malloc (sizeof (struct Route));
1824 new_route->dest = delegated_host_id;
1825 new_route->thru = master_context->host_id;
1826 route_list_add (new_route);
1830 /* Route the request */
1831 if (slave_host_id >= route_list_size)
1833 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1837 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1838 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1839 lcfq->lcf->delegated_host_id = delegated_host_id;
1840 lcfq->lcf->slave_host_id = slave_host_id;
1841 route = find_dest_route (slave_host_id);
1842 GNUNET_assert (NULL != route); /* because we add routes carefully */
1843 GNUNET_assert (route->dest < slave_list_size);
1844 GNUNET_assert (NULL != slave_list[route->dest]);
1845 lcfq->lcf->state = INIT;
1846 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1847 lcfq->lcf->gateway = slave_list[route->dest];
1848 lcfq->lcf->msg = GNUNET_malloc (msize);
1849 (void) memcpy (lcfq->lcf->msg, msg, msize);
1850 GNUNET_SERVER_client_keep (client);
1851 lcfq->lcf->client = client;
1852 if (NULL == lcfq_head)
1854 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1855 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1856 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1859 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1860 /* FIXME: Adding a new route should happen after the controllers are linked
1862 if (1 != msg->is_subordinate)
1864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1867 if ((delegated_host_id < route_list_size)
1868 && (NULL != route_list[delegated_host_id]))
1870 GNUNET_break_op (0); /* Are you trying to link delegated host twice
1871 with is subordinate flag set to GNUNET_YES? */
1872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1875 new_route = GNUNET_malloc (sizeof (struct Route));
1876 new_route->dest = delegated_host_id;
1877 new_route->thru = route->dest;
1878 route_list_add (new_route);
1879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1884 * The task to be executed if the forwarded peer create operation has been
1887 * @param cls the FowardedOperationContext
1888 * @param tc the TaskContext from the scheduler
1891 peer_create_forward_timeout (void *cls,
1892 const struct GNUNET_SCHEDULER_TaskContext *tc)
1894 struct ForwardedOperationContext *fo_ctxt = cls;
1896 /* send error msg to client */
1897 GNUNET_free (fo_ctxt->cls);
1898 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1899 GNUNET_SERVER_client_drop (fo_ctxt->client);
1900 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1901 GNUNET_free (fo_ctxt);
1906 * Callback to be called when forwarded peer create operation is
1907 * successfull. We have to relay the reply msg back to the client
1909 * @param cls ForwardedOperationContext
1910 * @param msg the peer create success message
1913 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1915 struct ForwardedOperationContext *fo_ctxt = cls;
1916 struct GNUNET_MessageHeader *dup_msg;
1917 struct Peer *remote_peer;
1920 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1921 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1923 GNUNET_assert (NULL != fo_ctxt->cls);
1924 remote_peer = fo_ctxt->cls;
1925 peer_list_add (remote_peer);
1927 msize = ntohs (msg->size);
1928 dup_msg = GNUNET_malloc (msize);
1929 (void) memcpy (dup_msg, msg, msize);
1930 queue_message (fo_ctxt->client, dup_msg);
1931 GNUNET_SERVER_client_drop (fo_ctxt->client);
1932 GNUNET_free (fo_ctxt);
1938 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1941 * @param client identification of the client
1942 * @param message the actual message
1945 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1946 const struct GNUNET_MessageHeader *message)
1948 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1949 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1950 struct GNUNET_CONFIGURATION_Handle *cfg;
1951 struct ForwardedOperationContext *fo_ctxt;
1952 struct Route *route;
1957 uint32_t config_size;
1963 msize = ntohs (message->size);
1964 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1966 GNUNET_break (0); /* We need configuration */
1967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1970 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1971 host_id = ntohl (msg->host_id);
1972 peer_id = ntohl (msg->peer_id);
1973 if (UINT32_MAX == peer_id)
1975 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1976 "Cannot create peer with given ID");
1977 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1980 if (host_id == master_context->host_id)
1984 /* We are responsible for this peer */
1985 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1986 config_size = ntohl (msg->config_size);
1987 config = GNUNET_malloc (config_size);
1988 dest_size = config_size;
1991 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1992 (const Bytef *) &msg[1], (uLong) msize)))
1994 GNUNET_break (0); /* uncompression error */
1995 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1998 if (config_size != dest_size)
2000 GNUNET_break (0); /* Uncompressed config size mismatch */
2001 GNUNET_free (config);
2002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2005 cfg = GNUNET_CONFIGURATION_create ();
2007 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2009 GNUNET_break (0); /* Configuration parsing error */
2010 GNUNET_free (config);
2011 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2014 GNUNET_free (config);
2015 peer = GNUNET_malloc (sizeof (struct Peer));
2016 peer->is_remote = GNUNET_NO;
2017 peer->details.local.cfg = cfg;
2019 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
2020 peer->details.local.peer =
2021 GNUNET_TESTING_peer_configure (master_context->system,
2022 peer->details.local.cfg, peer->id,
2023 NULL /* Peer id */ ,
2025 if (NULL == peer->details.local.peer)
2027 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2034 peer->details.local.is_running = GNUNET_NO;
2035 peer_list_add (peer);
2037 GNUNET_malloc (sizeof
2038 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2039 reply->header.size =
2040 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2041 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2042 reply->peer_id = msg->peer_id;
2043 reply->operation_id = msg->operation_id;
2044 queue_message (client, &reply->header);
2045 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2049 /* Forward peer create request */
2050 route = find_dest_route (host_id);
2054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2058 peer = GNUNET_malloc (sizeof (struct Peer));
2059 peer->is_remote = GNUNET_YES;
2061 peer->details.remote.slave = slave_list[route->dest];
2062 peer->details.remote.remote_host_id = host_id;
2063 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2064 GNUNET_SERVER_client_keep (client);
2065 fo_ctxt->client = client;
2066 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2067 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2069 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2070 fo_ctxt->operation_id,
2072 peer_create_success_cb, fo_ctxt);
2073 fo_ctxt->timeout_task =
2074 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2076 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2081 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2084 * @param client identification of the client
2085 * @param message the actual message
2088 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2089 const struct GNUNET_MessageHeader *message)
2091 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2092 struct ForwardedOperationContext *fopc;
2096 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2097 peer_id = ntohl (msg->peer_id);
2098 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2099 peer_id, GNUNET_ntohll (msg->operation_id));
2100 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2102 LOG (GNUNET_ERROR_TYPE_ERROR,
2103 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2104 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2105 "Peer doesn't exist");
2106 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2109 peer = peer_list[peer_id];
2110 if (GNUNET_YES == peer->is_remote)
2112 /* Forward the destory message to sub controller */
2113 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2114 GNUNET_SERVER_client_keep (client);
2115 fopc->client = client;
2116 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2118 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2119 fopc->operation_id, &msg->header,
2120 &forwarded_operation_reply_relay,
2122 fopc->timeout_task =
2123 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2128 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2129 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2130 peer_list_remove (peer);
2132 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2133 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2138 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2141 * @param client identification of the client
2142 * @param message the actual message
2145 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2146 const struct GNUNET_MessageHeader *message)
2148 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2149 struct GNUNET_TESTBED_PeerEventMessage *reply;
2150 struct ForwardedOperationContext *fopc;
2154 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2155 peer_id = ntohl (msg->peer_id);
2156 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2159 LOG (GNUNET_ERROR_TYPE_ERROR,
2160 "Asked to start a non existent peer with id: %u\n", peer_id);
2161 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2164 peer = peer_list[peer_id];
2165 if (GNUNET_YES == peer->is_remote)
2167 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2168 GNUNET_SERVER_client_keep (client);
2169 fopc->client = client;
2170 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2172 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2173 fopc->operation_id, &msg->header,
2174 &forwarded_operation_reply_relay,
2176 fopc->timeout_task =
2177 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2182 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2184 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2186 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2189 peer->details.local.is_running = GNUNET_YES;
2190 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2191 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2192 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2193 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2194 reply->host_id = htonl (master_context->host_id);
2195 reply->peer_id = msg->peer_id;
2196 reply->operation_id = msg->operation_id;
2197 queue_message (client, &reply->header);
2198 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2203 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2206 * @param client identification of the client
2207 * @param message the actual message
2210 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2211 const struct GNUNET_MessageHeader *message)
2213 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2214 struct GNUNET_TESTBED_PeerEventMessage *reply;
2215 struct ForwardedOperationContext *fopc;
2219 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2220 peer_id = ntohl (msg->peer_id);
2221 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2223 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2225 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2228 peer = peer_list[peer_id];
2229 if (GNUNET_YES == peer->is_remote)
2231 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2232 GNUNET_SERVER_client_keep (client);
2233 fopc->client = client;
2234 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2236 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2237 fopc->operation_id, &msg->header,
2238 &forwarded_operation_reply_relay,
2240 fopc->timeout_task =
2241 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2243 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2246 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2248 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2249 "Peer not running");
2250 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2253 peer->details.local.is_running = GNUNET_NO;
2254 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2255 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2256 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2257 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2258 reply->host_id = htonl (master_context->host_id);
2259 reply->peer_id = msg->peer_id;
2260 reply->operation_id = msg->operation_id;
2261 queue_message (client, &reply->header);
2262 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2267 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2270 * @param client identification of the client
2271 * @param message the actual message
2274 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2275 const struct GNUNET_MessageHeader *message)
2277 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2278 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2287 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2288 peer_id = ntohl (msg->peer_id);
2289 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2291 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2293 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2296 peer = peer_list[peer_id];
2297 if (GNUNET_YES == peer->is_remote)
2299 struct ForwardedOperationContext *fopc;
2301 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2302 GNUNET_SERVER_client_keep (client);
2303 fopc->client = client;
2304 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2306 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2307 fopc->operation_id, &msg->header,
2308 &forwarded_operation_reply_relay,
2310 fopc->timeout_task =
2311 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2313 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2317 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2319 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2320 GNUNET_free (config);
2323 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2324 reply = GNUNET_realloc (xconfig, msize);
2325 (void) memmove (&reply[1], reply, xc_size);
2326 reply->header.size = htons (msize);
2327 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2328 reply->peer_id = msg->peer_id;
2329 reply->operation_id = msg->operation_id;
2330 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2331 &reply->peer_identity);
2332 reply->config_size = htons ((uint16_t) c_size);
2333 queue_message (client, &reply->header);
2334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2339 * Task for cleaing up overlay connect context structure
2341 * @param cls the overlay connect context
2342 * @param tc the task context
2345 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2347 struct OverlayConnectContext *occ = cls;
2349 LOG_DEBUG ("Cleaning up occ\n");
2350 GNUNET_free_non_null (occ->emsg);
2351 GNUNET_free_non_null (occ->hello);
2352 GNUNET_SERVER_client_drop (occ->client);
2353 if (NULL != occ->opc)
2354 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2355 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2356 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2357 if (NULL != occ->ch)
2358 GNUNET_CORE_disconnect (occ->ch);
2359 if (NULL != occ->ghh)
2360 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2361 if (NULL != occ->p1th)
2362 GNUNET_TRANSPORT_disconnect (occ->p1th);
2363 if (NULL != occ->p2th)
2364 GNUNET_TRANSPORT_disconnect (occ->p2th);
2370 * Task which will be run when overlay connect request has been timed out
2372 * @param cls the OverlayConnectContext
2373 * @param tc the TaskContext
2376 timeout_overlay_connect (void *cls,
2377 const struct GNUNET_SCHEDULER_TaskContext *tc)
2379 struct OverlayConnectContext *occ = cls;
2381 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2382 LOG (GNUNET_ERROR_TYPE_WARNING,
2383 "Timeout while connecting peers %u and %u\n",
2384 occ->peer_id, occ->other_peer_id);
2385 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2386 occ_cleanup (occ, tc);
2392 * Function called to notify transport users that another
2393 * peer connected to us.
2395 * @param cls closure
2396 * @param new_peer the peer that connected
2397 * @param ats performance data
2398 * @param ats_count number of entries in ats (excluding 0-termination)
2401 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2402 const struct GNUNET_ATS_Information *ats,
2403 unsigned int ats_count)
2405 struct OverlayConnectContext *occ = cls;
2406 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2408 char *other_peer_str;
2410 LOG_DEBUG ("Overlay connect notify\n");
2412 memcmp (new_peer, &occ->peer_identity,
2413 sizeof (struct GNUNET_PeerIdentity)))
2415 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2416 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2418 memcmp (new_peer, &occ->other_peer_identity,
2419 sizeof (struct GNUNET_PeerIdentity)))
2421 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2422 new_peer_str, other_peer_str);
2423 GNUNET_free (new_peer_str);
2424 GNUNET_free (other_peer_str);
2427 GNUNET_free (new_peer_str);
2428 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2429 GNUNET_i2s (&occ->peer_identity));
2430 GNUNET_free (other_peer_str);
2431 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2433 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2434 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2436 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2437 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2438 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2439 GNUNET_free_non_null (occ->emsg);
2441 if (NULL != occ->p2th)
2442 GNUNET_TRANSPORT_disconnect (occ->p2th);
2444 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2445 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2447 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2448 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2449 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2450 msg->peer1 = htonl (occ->peer_id);
2451 msg->peer2 = htonl (occ->other_peer_id);
2452 msg->operation_id = GNUNET_htonll (occ->op_id);
2453 queue_message (occ->client, &msg->header);
2454 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2459 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2462 * @param cls the OverlayConnectContext
2463 * @param tc the TaskContext from scheduler
2466 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2468 struct OverlayConnectContext *occ = cls;
2469 char *other_peer_str;
2471 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2472 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2474 GNUNET_assert (NULL != occ->hello);
2475 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2476 if (NULL != occ->peer2_controller)
2478 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2480 uint16_t hello_size;
2482 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2483 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2484 hello_size = ntohs (occ->hello->size);
2485 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2486 msg = GNUNET_malloc (msize);
2487 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2488 msg->header.size = htons (msize);
2489 msg->peer = htonl (occ->other_peer_id);
2490 msg->operation_id = GNUNET_htonll (occ->op_id);
2491 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2492 sizeof (struct GNUNET_PeerIdentity));
2493 memcpy (msg->hello, occ->hello, hello_size);
2494 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2498 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2499 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2500 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2501 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2502 occ->send_hello_task =
2503 GNUNET_SCHEDULER_add_delayed
2504 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2505 100 * (pow (2, occ->retries++))),
2508 GNUNET_free (other_peer_str);
2512 * Test for checking whether HELLO message is empty
2514 * @param cls empty flag to set
2515 * @param address the HELLO
2516 * @param expiration expiration of the HELLO
2520 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2521 struct GNUNET_TIME_Absolute expiration)
2531 * Function called whenever there is an update to the HELLO of peers in the
2532 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2533 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2535 * @param cls closure
2536 * @param hello our updated HELLO
2539 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2541 struct OverlayConnectContext *occ = cls;
2545 msize = ntohs (hello->size);
2547 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2548 hello, GNUNET_NO, &test_address,
2550 if (GNUNET_YES == empty)
2552 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2555 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2556 occ->hello = GNUNET_malloc (msize);
2557 memcpy (occ->hello, hello, msize);
2558 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2560 GNUNET_TRANSPORT_disconnect (occ->p1th);
2562 GNUNET_free_non_null (occ->emsg);
2563 if (NULL == occ->peer2_controller)
2566 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2567 &occ->other_peer_identity, NULL, NULL, NULL,
2569 if (NULL == occ->p2th)
2571 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2572 GNUNET_i2s (&occ->other_peer_identity));
2573 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2574 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2578 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2579 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2584 * Function called after GNUNET_CORE_connect has succeeded (or failed
2585 * for good). Note that the private key of the peer is intentionally
2586 * not exposed here; if you need it, your process should try to read
2587 * the private key file directly (which should work if you are
2590 * @param cls closure
2591 * @param server handle to the server, NULL if we failed
2592 * @param my_identity ID of this peer, NULL if we failed
2595 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2596 const struct GNUNET_PeerIdentity *my_identity)
2598 struct OverlayConnectContext *occ = cls;
2600 GNUNET_free_non_null (occ->emsg);
2601 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2602 if ((NULL == server) || (NULL == my_identity))
2604 GNUNET_free (occ->emsg);
2607 memcpy (&occ->peer_identity, my_identity,
2608 sizeof (struct GNUNET_PeerIdentity));
2610 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2611 &occ->peer_identity, NULL, NULL, NULL, NULL);
2612 if (NULL == occ->p1th)
2614 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2615 GNUNET_i2s (&occ->peer_identity));
2618 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2619 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2620 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2624 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2625 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2631 * Callback to be called when forwarded get peer config operation as part of
2632 * overlay connect is successfull. Connection to Peer 1's core is made and is
2633 * checked for new connection from peer 2
2635 * @param cls ForwardedOperationContext
2636 * @param msg the peer create success message
2639 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2641 struct OverlayConnectContext *occ = cls;
2642 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2643 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2648 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2650 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2652 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2653 sizeof (struct GNUNET_PeerIdentity));
2654 GNUNET_free_non_null (occ->emsg);
2655 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2657 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2658 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2659 GNUNET_NO, no_handlers);
2660 if (NULL == occ->ch)
2665 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2667 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2672 * This callback is a part of overlay connect operation. This will be run when
2673 * the registration operation of peer2's controller is completed at peer1's
2676 * @param cls the ForwardedOverlayConnectContext
2677 * @param emsg the error message in case of any failure; NULL if host
2678 * registration is successfull.
2681 focc_reg_completion_cc (void *cls, const char *emsg)
2683 struct ForwardedOverlayConnectContext *focc = cls;
2684 struct GNUNET_CONFIGURATION_Handle *cfg;
2686 GNUNET_assert (FOCC_REGISTER == focc->state);
2687 focc->rhandle = NULL;
2688 GNUNET_assert (NULL == focc->sub_op);
2689 LOG_DEBUG ("[%u -> %u] Registering peer2's host successful\n",
2690 focc->peer1, focc->peer2);
2691 if ((NULL == focc->gateway2)
2692 || ((focc->peer2_host_id < slave_list_size) /* Check if we have the needed config */
2693 && (NULL != slave_list[focc->peer2_host_id])))
2695 focc->state = FOCC_LINK;
2696 cfg = (NULL == focc->gateway2) ?
2697 our_config : slave_list[focc->peer2_host_id]->cfg;
2699 GNUNET_TESTBED_controller_link_ (focc,
2701 focc->peer2_host_id,
2702 peer_list[focc->peer1]->details.remote.remote_host_id,
2707 focc->state = FOCC_GET_CFG;
2708 focc->sub_op = GNUNET_TESTBED_get_slave_config_ (focc, focc->gateway2,
2709 focc->peer2_host_id);
2714 * Callback to be called when forwarded overlay connection operation has a reply
2715 * from the sub-controller successfull. We have to relay the reply msg back to
2718 * @param cls ForwardedOperationContext
2719 * @param msg the peer create success message
2722 forwarded_overlay_connect_listener (void *cls,
2723 const struct GNUNET_MessageHeader *msg)
2725 struct ForwardedOperationContext *fopc = cls;
2726 struct ForwardedOverlayConnectContext *focc;
2731 forwarded_operation_reply_relay (cls, msg);
2734 switch (focc->state)
2737 if (GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG != ntohs (msg->type))
2739 GNUNET_break (0); /* Something failed; you may check output of
2741 cleanup_focc (focc);
2742 forwarded_operation_reply_relay (cls, msg);
2745 LOG_DEBUG ("[%u -> %u] Registering peer2's host %u at %u\n",
2746 focc->peer1, focc->peer2, focc->peer2_host_id,
2747 peer_list[focc->peer1]->details.remote.remote_host_id);
2748 focc->state = FOCC_REGISTER;
2750 GNUNET_TESTBED_register_host (focc->gateway,
2751 host_list[focc->peer2_host_id],
2752 focc_reg_completion_cc, focc);
2753 GNUNET_assert (NULL != focc->rhandle);
2758 GNUNET_SERVER_client_drop (fopc->client);
2759 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2760 fopc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2766 * Timeout task for cancelling a forwarded overlay connect connect
2768 * @param cls the ForwardedOverlayConnectContext
2769 * @param tc the task context from the scheduler
2772 forwarded_overlay_connect_timeout (void *cls,
2773 const struct GNUNET_SCHEDULER_TaskContext
2776 struct ForwardedOverlayConnectContext *focc = cls;
2778 focc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2779 LOG_DEBUG ("Overlay linking between peers %u and %u failed during state %u\n",
2780 focc->peer1, focc->peer2, focc->state);
2781 send_operation_fail_msg (focc->client, focc->operation_id, "Timeout");
2782 cleanup_focc (focc);
2787 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2790 * @param client identification of the client
2791 * @param message the actual message
2794 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2795 const struct GNUNET_MessageHeader *message)
2797 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2798 struct OverlayConnectContext *occ;
2799 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2803 uint64_t operation_id;
2806 uint32_t peer2_host_id;
2808 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2809 p1 = ntohl (msg->peer1);
2810 p2 = ntohl (msg->peer2);
2811 peer2_host_id = ntohl (msg->peer2_host_id);
2812 GNUNET_assert (p1 < peer_list_size);
2813 GNUNET_assert (NULL != peer_list[p1]);
2814 peer = peer_list[p1];
2815 operation_id = GNUNET_ntohll (msg->operation_id);
2816 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
2817 p1, p2, operation_id);
2818 if (GNUNET_YES == peer->is_remote)
2820 struct ForwardedOperationContext *fopc;
2821 struct Route *route_to_peer2_host;
2822 struct Route *route_to_peer1_host;
2824 LOG_DEBUG ("Forwarding overlay connect\n");
2825 GNUNET_SERVER_client_keep (client);
2826 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2827 fopc->client = client;
2828 fopc->operation_id = operation_id;
2829 route_to_peer2_host = NULL;
2830 route_to_peer1_host = NULL;
2831 route_to_peer2_host = find_dest_route (peer2_host_id);
2832 if ((NULL != route_to_peer2_host)
2833 || (peer2_host_id == master_context->host_id))
2835 route_to_peer1_host =
2836 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
2837 GNUNET_assert (NULL != route_to_peer1_host);
2838 if ((peer2_host_id == master_context->host_id)
2839 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
2841 struct ForwardedOverlayConnectContext *focc;
2844 msize = sizeof (struct GNUNET_TESTBED_OverlayConnectMessage);
2845 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
2846 focc->gateway = peer->details.remote.slave->controller;
2847 focc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
2848 slave_list[route_to_peer2_host->dest]->controller;
2851 focc->peer2_host_id = peer2_host_id;
2852 focc->state = FOCC_INIT;
2853 focc->orig_msg = GNUNET_malloc (msize);
2854 (void) memcpy (focc->orig_msg, message, msize);
2855 GNUNET_SERVER_client_keep (client);
2856 focc->client = client;
2857 focc->operation_id = operation_id;
2859 focc->timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
2860 &forwarded_overlay_connect_timeout,
2865 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2866 operation_id, message,
2867 &forwarded_overlay_connect_listener,
2869 fopc->timeout_task =
2870 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2872 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2875 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2876 GNUNET_SERVER_client_keep (client);
2877 occ->client = client;
2879 occ->other_peer_id = p2;
2880 occ->peer = peer_list[p1];
2881 occ->op_id = GNUNET_ntohll (msg->operation_id);
2882 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
2884 if ((peer2_host_id >= slave_list_size)
2885 || (NULL ==slave_list[peer2_host_id]))
2887 struct GNUNET_TESTBED_NeedControllerConfig *reply;
2889 LOG_DEBUG ("Need controller configuration for connecting peers %u and %u\n",
2890 occ->peer_id, occ->other_peer_id);
2892 reply = GNUNET_malloc (sizeof (struct
2893 GNUNET_TESTBED_NeedControllerConfig));
2894 reply->header.size = htons (sizeof (struct
2895 GNUNET_TESTBED_NeedControllerConfig));
2896 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG);
2897 reply->controller_host_id = msg->peer2_host_id;
2898 reply->operation_id = msg->operation_id;
2899 queue_message (client, &reply->header);
2900 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2905 occ->peer2_controller = slave_list[peer2_host_id]->controller;
2906 if (NULL == occ->peer2_controller)
2908 GNUNET_break (0); /* What's going on? */
2909 GNUNET_SERVER_client_drop (client);
2911 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2918 if (GNUNET_YES == peer_list[occ->other_peer_id]->is_remote)
2919 occ->peer2_controller = peer_list[occ->other_peer_id]->details.remote.slave->controller;
2921 /* Get the identity of the second peer */
2922 if (NULL != occ->peer2_controller)
2924 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2927 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2928 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2929 cmsg.peer_id = msg->peer2;
2930 cmsg.operation_id = msg->operation_id;
2932 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
2933 occ->op_id, &cmsg.header,
2934 &overlay_connect_get_config,
2937 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2939 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2940 (GNUNET_TIME_UNIT_SECONDS, 30),
2941 &timeout_overlay_connect, occ);
2942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2945 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
2946 &occ->other_peer_identity);
2947 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2948 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2950 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2951 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2952 GNUNET_NO, no_handlers);
2953 if (NULL == occ->ch)
2955 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2958 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2959 (GNUNET_TIME_UNIT_SECONDS, 30),
2960 &timeout_overlay_connect, occ);
2961 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2966 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2969 * @param rocc the RequestOverlayConnectContext
2972 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2974 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2975 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2976 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2977 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2978 GNUNET_TRANSPORT_disconnect (rocc->th);
2979 GNUNET_free_non_null (rocc->hello);
2985 * Task to timeout rocc and cleanit up
2987 * @param cls the RequestOverlayConnectContext
2988 * @param tc the TaskContext from scheduler
2991 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2993 struct RequestOverlayConnectContext *rocc = cls;
2995 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2996 cleanup_rocc (rocc);
3001 * Function called to notify transport users that another
3002 * peer connected to us.
3004 * @param cls closure
3005 * @param new_peer the peer that connected
3006 * @param ats performance data
3007 * @param ats_count number of entries in ats (excluding 0-termination)
3010 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3011 const struct GNUNET_ATS_Information * ats,
3014 struct RequestOverlayConnectContext *rocc = cls;
3016 LOG_DEBUG ("Request Overlay connect notify\n");
3017 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3019 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3020 cleanup_rocc (rocc);
3025 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3026 * whose identity is in RequestOverlayConnectContext
3028 * @param cls the RequestOverlayConnectContext
3029 * @param tc the TaskContext from scheduler
3032 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3034 struct RequestOverlayConnectContext *rocc = cls;
3036 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3037 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
3038 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
3039 rocc->attempt_connect_task_id =
3040 GNUNET_SCHEDULER_add_delayed
3041 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3042 100 * (pow (2, rocc->retries++))),
3043 &attempt_connect_task, rocc);
3048 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3051 * @param client identification of the client
3052 * @param message the actual message
3055 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3056 const struct GNUNET_MessageHeader *message)
3058 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3059 struct RequestOverlayConnectContext *rocc;
3065 msize = ntohs (message->size);
3066 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3072 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3073 if ((NULL == msg->hello) ||
3074 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3077 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3080 hsize = ntohs (msg->hello->size);
3081 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3087 peer_id = ntohl (msg->peer);
3088 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3090 GNUNET_break_op (0);
3091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3094 if (GNUNET_YES == peer->is_remote)
3096 struct GNUNET_MessageHeader *msg2;
3098 msg2 = GNUNET_malloc (msize);
3099 (void) memcpy (msg2, message, msize);
3100 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3104 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3105 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
3106 NULL, &transport_connect_notify, NULL);
3107 if (NULL == rocc->th)
3111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3114 memcpy (&rocc->a_id, &msg->peer_identity,
3115 sizeof (struct GNUNET_PeerIdentity));
3116 rocc->hello = GNUNET_malloc (hsize);
3117 memcpy (rocc->hello, msg->hello, hsize);
3118 rocc->attempt_connect_task_id =
3119 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3120 rocc->timeout_rocc_task_id =
3121 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3122 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3127 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3130 * @param client identification of the client
3131 * @param message the actual message
3134 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3135 const struct GNUNET_MessageHeader *message)
3137 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3138 struct Slave *slave;
3139 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3143 size_t xconfig_size;
3148 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3149 slave_id = ntohl (msg->slave_id);
3150 op_id = GNUNET_ntohll (msg->operation_id);
3151 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3153 send_operation_fail_msg (client, op_id, "Slave not found");
3154 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3157 slave = slave_list[slave_id];
3158 if (NULL == slave->cfg)
3160 send_operation_fail_msg (client, op_id,
3161 "Configuration not found (slave not started by me)");
3162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3165 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3166 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3168 GNUNET_free (config);
3169 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3170 GNUNET_break (reply_size <= UINT16_MAX);
3171 GNUNET_break (config_size <= UINT16_MAX);
3172 reply = GNUNET_realloc (xconfig, reply_size);
3173 (void) memmove (&reply[1], reply, xconfig_size);
3174 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3175 reply->header.size = htons ((uint16_t) reply_size);
3176 reply->slave_id = msg->slave_id;
3177 reply->operation_id = msg->operation_id;
3178 reply->config_size = htons ((uint16_t) config_size);
3179 queue_message (client, &reply->header);
3180 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3185 * Iterator over hash map entries.
3187 * @param cls closure
3188 * @param key current key code
3189 * @param value value in the hash map
3190 * @return GNUNET_YES if we should continue to
3195 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3197 struct SharedService *ss = value;
3199 GNUNET_assert (GNUNET_YES ==
3200 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3201 GNUNET_free (ss->name);
3208 * Task to clean up and shutdown nicely
3211 * @param tc the TaskContext from scheduler
3214 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3216 struct LCFContextQueue *lcfq;
3219 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3220 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3221 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3223 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3224 if (NULL != lcfq_head)
3226 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3228 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3229 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3231 if (NULL != lcfq_head->lcf->rhandle)
3232 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
3234 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3235 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3237 GNUNET_free (lcfq->lcf->msg);
3238 GNUNET_free (lcfq->lcf);
3239 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3242 /* Clear peer list */
3243 for (id = 0; id < peer_list_size; id++)
3244 if (NULL != peer_list[id])
3246 if (GNUNET_NO == peer_list[id]->is_remote)
3248 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3249 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
3250 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3251 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3253 GNUNET_free (peer_list[id]);
3255 GNUNET_free_non_null (peer_list);
3256 /* Clear host list */
3257 for (id = 0; id < host_list_size; id++)
3258 if (NULL != host_list[id])
3259 GNUNET_TESTBED_host_destroy (host_list[id]);
3260 GNUNET_free_non_null (host_list);
3261 /* Clear route list */
3262 for (id = 0; id < route_list_size; id++)
3263 if (NULL != route_list[id])
3264 GNUNET_free (route_list[id]);
3265 GNUNET_free_non_null (route_list);
3266 /* Clear slave_list */
3267 for (id = 0; id < slave_list_size; id++)
3268 if (NULL != slave_list[id])
3270 if (NULL != slave_list[id]->cfg)
3271 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
3272 if (NULL != slave_list[id]->controller)
3273 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
3274 if (NULL != slave_list[id]->controller_proc)
3275 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
3276 GNUNET_free (slave_list[id]);
3278 GNUNET_free_non_null (slave_list);
3279 if (NULL != master_context)
3281 GNUNET_free_non_null (master_context->master_ip);
3282 if (NULL != master_context->system)
3283 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
3284 GNUNET_free (master_context);
3285 master_context = NULL;
3287 GNUNET_free_non_null (hostname);
3288 GNUNET_CONFIGURATION_destroy (our_config);
3293 * Callback for client disconnect
3296 * @param client the client which has disconnected
3299 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
3301 if (NULL == master_context)
3303 if (client == master_context->client)
3305 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
3306 GNUNET_SERVER_client_drop (client);
3307 /* should not be needed as we're terminated by failure to read
3308 * from stdin, but if stdin fails for some reason, this shouldn't
3309 * hurt for now --- might need to revise this later if we ever
3310 * decide that master connections might be temporarily down
3311 * for some reason */
3312 //GNUNET_SCHEDULER_shutdown ();
3320 * @param cls closure
3321 * @param server the initialized server
3322 * @param cfg configuration to use
3325 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
3326 const struct GNUNET_CONFIGURATION_Handle *cfg)
3328 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
3329 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
3330 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
3331 {&handle_configure_shared_service, NULL,
3332 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
3333 {&handle_link_controllers, NULL,
3334 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
3335 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
3336 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
3337 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
3338 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
3339 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
3340 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
3341 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
3342 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
3343 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
3344 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
3345 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
3346 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
3348 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
3349 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
3353 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
3354 (cfg, "testbed", "HOSTNAME", &hostname));
3355 our_config = GNUNET_CONFIGURATION_dup (cfg);
3356 GNUNET_SERVER_add_handlers (server, message_handlers);
3357 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
3358 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
3360 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
3361 &shutdown_task, NULL);
3362 LOG_DEBUG ("Testbed startup complete\n");
3363 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
3368 * The starting point of execution
3371 main (int argc, char *const *argv)
3373 //sleep (15); /* Debugging */
3374 return (GNUNET_OK ==
3375 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
3376 &testbed_run, NULL)) ? 0 : 1;
3379 /* end of gnunet-service-testbed.c */