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;
195 * This context information will be created for each host that is registered at
196 * slave controllers during overlay connects.
198 struct RegisteredHostContext
201 * The host which is being registered
203 struct GNUNET_TESTBED_Host *reg_host;
206 * The host of the controller which has to connect to the above rhost
208 struct GNUNET_TESTBED_Host *host;
211 * The gateway to which this operation is forwarded to
213 struct Slave *gateway;
216 * The gateway through which peer2's controller can be reached
218 struct Slave *gateway2;
221 * Handle for sub-operations
223 struct GNUNET_TESTBED_Operation *sub_op;
226 * The client which initiated the link controller operation
228 struct GNUNET_SERVER_Client *client;
231 * Head of the ForwardedOverlayConnectContext DLL
233 struct ForwardedOverlayConnectContext *focc_dll_head;
236 * Tail of the ForwardedOverlayConnectContext DLL
238 struct ForwardedOverlayConnectContext *focc_dll_tail;
241 * Enumeration of states for this context
251 * State where we attempt to get peer2's controller configuration
256 * State where we attempt to link the controller of peer 1 to the controller
262 * State where we attempt to do the overlay connection again
272 * Structure representing a connected(directly-linked) controller
277 * The controller process handle if we had started the controller
279 struct GNUNET_TESTBED_ControllerProc *controller_proc;
282 * The controller handle
284 struct GNUNET_TESTBED_Controller *controller;
287 * The configuration of the slave. Cannot be NULL
289 struct GNUNET_CONFIGURATION_Handle *cfg;
292 * handle to lcc which is associated with this slave startup. Should be set to
293 * NULL when the slave has successfully started up
295 struct LinkControllersContext *lcc;
298 * Head of the host registration DLL
300 struct HostRegistration *hr_dll_head;
303 * Tail of the host registration DLL
305 struct HostRegistration *hr_dll_tail;
308 * The current host registration handle
310 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
313 * Hashmap to hold Registered host contexts
315 struct GNUNET_CONTAINER_MultiHashMap *reghost_map;
318 * The id of the host this controller is running on
326 * States of LCFContext
331 * The Context has been initialized; Nothing has been done on it
336 * Delegated host has been registered at the forwarding controller
338 DELEGATED_HOST_REGISTERED,
341 * The slave host has been registred at the forwarding controller
343 SLAVE_HOST_REGISTERED,
346 * The context has been finished (may have error)
353 * Link controllers request forwarding context
358 * The gateway which will pass the link message to delegated host
360 struct Slave *gateway;
363 * The controller link message that has to be forwarded to
365 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
368 * The client which has asked to perform this operation
370 struct GNUNET_SERVER_Client *client;
373 * The id of the operation which created this context
375 uint64_t operation_id;
378 * The state of this context
380 enum LCFContextState state;
385 uint32_t delegated_host_id;
390 uint32_t slave_host_id;
396 * Structure of a queue entry in LCFContext request queue
398 struct LCFContextQueue
403 struct LCFContext *lcf;
408 struct LCFContextQueue *next;
413 struct LCFContextQueue *prev;
428 * The peer handle from testing API
430 struct GNUNET_TESTING_Peer *peer;
433 * The modified (by GNUNET_TESTING_peer_configure) configuration this
434 * peer is configured with
436 struct GNUNET_CONFIGURATION_Handle *cfg;
439 * Is the peer running
448 * The slave this peer is started through
453 * The id of the remote host this peer is running on
455 uint32_t remote_host_id;
462 * Is this peer locally created?
467 * Our local reference id for this peer
472 * References to peers are using in forwarded overlay contexts and remote
473 * overlay connect contexts. A peer can only be destroyed after all such
474 * contexts are destroyed. For this, we maintain a reference counter. When we
475 * use a peer in any such context, we increment this counter. We decrement it
476 * when we are destroying these contexts
478 uint32_t reference_cnt;
481 * While destroying a peer, due to the fact that there could be references to
482 * this peer, we delay the peer destroy to a further time. We do this by using
483 * this flag to destroy the peer while destroying a context in which this peer
484 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
487 uint32_t destroy_flag;
493 * Context information for connecting 2 peers in overlay
495 struct OverlayConnectContext
498 * The next pointer for maintaining a DLL
500 struct OverlayConnectContext *next;
503 * The prev pointer for maintaining a DLL
505 struct OverlayConnectContext *prev;
508 * The client which has requested for overlay connection
510 struct GNUNET_SERVER_Client *client;
513 * the peer which has to connect to the other peer
518 * Transport handle of the first peer to get its HELLO
520 struct GNUNET_TRANSPORT_Handle *p1th;
523 * Transport handle of other peer to offer first peer's HELLO
525 struct GNUNET_TRANSPORT_Handle *p2th;
528 * Core handles of the first peer; used to notify when second peer connects to it
530 struct GNUNET_CORE_Handle *ch;
533 * HELLO of the other peer
535 struct GNUNET_MessageHeader *hello;
538 * Get hello handle to acquire HELLO of first peer
540 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
543 * The error message we send if this overlay connect operation has timed out
548 * Operation context for suboperations
550 struct OperationContext *opc;
553 * Controller of peer 2; NULL if the peer is local
555 struct GNUNET_TESTBED_Controller *peer2_controller;
558 * The peer identity of the first peer
560 struct GNUNET_PeerIdentity peer_identity;
563 * The peer identity of the other peer
565 struct GNUNET_PeerIdentity other_peer_identity;
568 * The id of the operation responsible for creating this context
573 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
576 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
579 * The id of the overlay connect timeout task
581 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
584 * The id of the cleanup task
586 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
596 uint32_t other_peer_id;
599 * Number of times we tried to send hello; used to increase delay in offering
607 * Context information for RequestOverlayConnect
608 * operations. RequestOverlayConnect is used when peers A, B reside on different
609 * hosts and the host controller for peer B is asked by the host controller of
610 * peer A to make peer B connect to peer A
612 struct RequestOverlayConnectContext
615 * the next pointer for DLL
617 struct RequestOverlayConnectContext *next;
620 * the prev pointer for DLL
622 struct RequestOverlayConnectContext *prev;
625 * The transport handle of peer B
627 struct GNUNET_TRANSPORT_Handle *th;
632 struct GNUNET_MessageHeader *hello;
635 * The peer identity of peer A
637 struct GNUNET_PeerIdentity a_id;
640 * Task for offering HELLO of A to B and doing try_connect
642 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
645 * Task to timeout RequestOverlayConnect
647 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
650 * Number of times we tried to send hello; used to increase delay in offering
659 * Context information for operations forwarded to subcontrollers
661 struct ForwardedOperationContext
664 * The generated operation context
666 struct OperationContext *opc;
669 * The client to which we have to reply
671 struct GNUNET_SERVER_Client *client;
679 * Task ID for the timeout task
681 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
684 * The id of the operation that has been forwarded
686 uint64_t operation_id;
692 * Context information used while linking controllers
694 struct LinkControllersContext
697 * The client which initiated the link controller operation
699 struct GNUNET_SERVER_Client *client;
702 * The ID of the operation
704 uint64_t operation_id;
710 * Context information to used during operations which forward the overlay
713 struct ForwardedOverlayConnectContext
716 * next ForwardedOverlayConnectContext in the DLL
718 struct ForwardedOverlayConnectContext *next;
721 * previous ForwardedOverlayConnectContext in the DLL
723 struct ForwardedOverlayConnectContext *prev;
726 * A copy of the original overlay connect message
728 struct GNUNET_MessageHeader *orig_msg;
731 * The id of the operation which created this context information
733 uint64_t operation_id;
746 * Id of the host where peer2 is running
748 uint32_t peer2_host_id;
754 * The master context; generated with the first INIT message
756 static struct Context *master_context;
759 * Our hostname; we give this to all the peers we start
761 static char *hostname;
771 static struct GNUNET_CONFIGURATION_Handle *our_config;
774 * Current Transmit Handle; NULL if no notify transmit exists currently
776 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
783 * The head for the LCF queue
785 static struct LCFContextQueue *lcfq_head;
788 * The tail for the LCF queue
790 static struct LCFContextQueue *lcfq_tail;
793 * The message queue head
795 static struct MessageQueue *mq_head;
798 * The message queue tail
800 static struct MessageQueue *mq_tail;
803 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
805 static struct OverlayConnectContext *occq_head;
808 * DLL tail for OverlayConnectContext DLL
810 static struct OverlayConnectContext *occq_tail;
813 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
816 static struct RequestOverlayConnectContext *roccq_head;
819 * DLL tail for RequectOverlayConnectContext DLL
821 static struct RequestOverlayConnectContext *roccq_tail;
826 static struct GNUNET_TESTBED_Host **host_list;
831 static struct Route **route_list;
834 * A list of directly linked neighbours
836 static struct Slave **slave_list;
839 * A list of peers we know about
841 static struct Peer **peer_list;
844 * The hashmap of shared services
846 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
849 * The event mask for the events we listen from sub-controllers
851 static uint64_t event_mask;
854 * The size of the host list
856 static uint32_t host_list_size;
859 * The size of the route list
861 static uint32_t route_list_size;
864 * The size of directly linked neighbours list
866 static uint32_t slave_list_size;
869 * The size of the peer list
871 static uint32_t peer_list_size;
878 * The lcf_task handle
880 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
883 * The shutdown task handle
885 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
889 * Function called to notify a client about the connection begin ready to queue
890 * more data. "buf" will be NULL and "size" zero if the connection was closed
891 * for writing in the meantime.
894 * @param size number of bytes available in buf
895 * @param buf where the callee should write the message
896 * @return number of bytes written to buf
899 transmit_ready_notify (void *cls, size_t size, void *buf)
901 struct MessageQueue *mq_entry;
903 transmit_handle = NULL;
905 GNUNET_assert (NULL != mq_entry);
908 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
909 size = ntohs (mq_entry->msg->size);
910 memcpy (buf, mq_entry->msg, size);
911 GNUNET_free (mq_entry->msg);
912 GNUNET_SERVER_client_drop (mq_entry->client);
913 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
914 GNUNET_free (mq_entry);
916 if (NULL != mq_entry)
918 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
919 ntohs (mq_entry->msg->size),
920 GNUNET_TIME_UNIT_FOREVER_REL,
921 &transmit_ready_notify, NULL);
927 * Queues a message in send queue for sending to the service
929 * @param client the client to whom the queued message has to be sent
930 * @param msg the message to queue
933 queue_message (struct GNUNET_SERVER_Client *client,
934 struct GNUNET_MessageHeader *msg)
936 struct MessageQueue *mq_entry;
940 type = ntohs (msg->type);
941 size = ntohs (msg->size);
942 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
943 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
944 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
946 mq_entry->client = client;
947 GNUNET_SERVER_client_keep (client);
948 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
950 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
951 if (NULL == transmit_handle)
953 GNUNET_SERVER_notify_transmit_ready (client, size,
954 GNUNET_TIME_UNIT_FOREVER_REL,
955 &transmit_ready_notify, NULL);
960 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
962 * @param ptr the memory block to realloc
963 * @param size the size of ptr
964 * @param new_size the size to which ptr has to be realloc'ed
965 * @return the newly reallocated memory block
968 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
970 ptr = GNUNET_realloc (ptr, new_size);
972 (void) memset (ptr + size, 0, new_size - size);
978 * Function to add a host to the current list of known hosts
980 * @param host the host to add
981 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
985 host_list_add (struct GNUNET_TESTBED_Host *host)
990 host_id = GNUNET_TESTBED_host_get_id_ (host);
991 orig_size = host_list_size;
992 if (host_list_size <= host_id)
994 while (host_list_size <= host_id)
995 host_list_size += LIST_GROW_STEP;
997 TESTBED_realloc (host_list,
998 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
999 sizeof (struct GNUNET_TESTBED_Host *)
1002 if (NULL != host_list[host_id])
1004 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
1005 return GNUNET_SYSERR;
1007 host_list[host_id] = host;
1013 * Adds a route to the route list
1015 * @param route the route to add
1018 route_list_add (struct Route *route)
1022 orig_size = route_list_size;
1023 if (route->dest >= route_list_size)
1025 while (route->dest >= route_list_size)
1026 route_list_size += LIST_GROW_STEP;
1028 TESTBED_realloc (route_list,
1029 sizeof (struct Route *) * orig_size,
1030 sizeof (struct Route *) * route_list_size);
1032 GNUNET_assert (NULL == route_list[route->dest]);
1033 route_list[route->dest] = route;
1038 * Adds a slave to the slave array
1040 * @param slave the slave controller to add
1043 slave_list_add (struct Slave *slave)
1047 orig_size = slave_list_size;
1048 if (slave->host_id >= slave_list_size)
1050 while (slave->host_id >= slave_list_size)
1051 slave_list_size += LIST_GROW_STEP;
1053 TESTBED_realloc (slave_list, sizeof (struct Slave *) * orig_size,
1054 sizeof (struct Slave *) * slave_list_size);
1056 GNUNET_assert (NULL == slave_list[slave->host_id]);
1057 slave_list[slave->host_id] = slave;
1062 * Adds a peer to the peer array
1064 * @param peer the peer to add
1067 peer_list_add (struct Peer *peer)
1071 orig_size = peer_list_size;
1072 if (peer->id >= peer_list_size)
1074 while (peer->id >= peer_list_size)
1075 peer_list_size += LIST_GROW_STEP;
1077 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
1078 sizeof (struct Peer *) * peer_list_size);
1080 GNUNET_assert (NULL == peer_list[peer->id]);
1081 peer_list[peer->id] = peer;
1086 * Removes a the give peer from the peer array
1088 * @param peer the peer to be removed
1091 peer_list_remove (struct Peer *peer)
1096 peer_list[peer->id] = NULL;
1097 orig_size = peer_list_size;
1098 while (peer_list_size >= LIST_GROW_STEP)
1100 for (id = peer_list_size - 1;
1101 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1102 if (NULL != peer_list[id])
1104 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1106 peer_list_size -= LIST_GROW_STEP;
1108 if (orig_size == peer_list_size)
1111 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1116 * Finds the route with directly connected host as destination through which
1117 * the destination host can be reached
1119 * @param host_id the id of the destination host
1120 * @return the route with directly connected destination host; NULL if no route
1123 static struct Route *
1124 find_dest_route (uint32_t host_id)
1126 struct Route *route;
1128 if (route_list_size <= host_id)
1130 while (NULL != (route = route_list[host_id]))
1132 if (route->thru == master_context->host_id)
1134 host_id = route->thru;
1141 * Routes message to a host given its host_id
1143 * @param host_id the id of the destination host
1144 * @param msg the message to be routed
1147 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1154 * Send operation failure message to client
1156 * @param client the client to which the failure message has to be sent to
1157 * @param operation_id the id of the failed operation
1158 * @param emsg the error message; can be NULL
1161 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1162 uint64_t operation_id, const char *emsg)
1164 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1168 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1169 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1171 msg = GNUNET_malloc (msize);
1172 msg->header.size = htons (msize);
1173 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1174 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1175 msg->operation_id = GNUNET_htonll (operation_id);
1177 memcpy (&msg[1], emsg, emsg_len);
1178 queue_message (client, &msg->header);
1183 * Function to send generic operation success message to given client
1185 * @param client the client to send the message to
1186 * @param operation_id the id of the operation which was successful
1189 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1190 uint64_t operation_id)
1192 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1195 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1196 msg = GNUNET_malloc (msize);
1197 msg->header.size = htons (msize);
1198 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1199 msg->operation_id = GNUNET_htonll (operation_id);
1200 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1201 queue_message (client, &msg->header);
1206 * Callback which will be called to after a host registration succeeded or failed
1208 * @param cls the handle to the slave at which the registration is completed
1209 * @param emsg the error message; NULL if host registration is successful
1212 hr_completion (void *cls, const char *emsg);
1216 * Attempts to register the next host in the host registration queue
1218 * @param slave the slave controller whose host registration queue is checked
1219 * for host registrations
1222 register_next_host (struct Slave *slave)
1224 struct HostRegistration *hr;
1226 hr = slave->hr_dll_head;
1227 GNUNET_assert (NULL != hr);
1228 GNUNET_assert (NULL == slave->rhandle);
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "Registering host %u at %u\n",
1231 GNUNET_TESTBED_host_get_id_ (hr->host),
1232 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1233 slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
1241 * Callback which will be called to after a host registration succeeded or failed
1243 * @param cls the handle to the slave at which the registration is completed
1244 * @param emsg the error message; NULL if host registration is successful
1247 hr_completion (void *cls, const char *emsg)
1249 struct Slave *slave = cls;
1250 struct HostRegistration *hr;
1252 slave->rhandle = NULL;
1253 hr = slave->hr_dll_head;
1254 GNUNET_assert (NULL != hr);
1255 LOG (GNUNET_ERROR_TYPE_DEBUG,
1256 "Registering host %u at %u successful\n",
1257 GNUNET_TESTBED_host_get_id_ (hr->host),
1258 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1259 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
1263 hr->cb (hr->cb_cls, emsg);
1265 if (NULL != slave->hr_dll_head)
1266 register_next_host (slave);
1271 * Adds a host registration's request to a slave's registration queue
1273 * @param slave the slave controller at which the given host has to be
1275 * @param cb the host registration completion callback
1276 * @param cb_cls the closure for the host registration completion callback
1277 * @param host the host which has to be registered
1280 queue_host_registration (struct Slave *slave,
1281 GNUNET_TESTBED_HostRegistrationCompletion cb,
1283 struct GNUNET_TESTBED_Host *host)
1285 struct HostRegistration *hr;
1288 LOG (GNUNET_ERROR_TYPE_DEBUG,
1289 "Queueing host registration for host %u at %u\n",
1290 GNUNET_TESTBED_host_get_id_ (host),
1291 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1292 hr = GNUNET_malloc (sizeof (struct HostRegistration));
1294 hr->cb_cls = cb_cls;
1296 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
1297 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
1300 if (GNUNET_YES == call_register)
1301 register_next_host (slave);
1306 * The Link Controller forwarding task
1308 * @param cls the LCFContext
1309 * @param tc the Task context from scheduler
1312 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1316 * Completion callback for host registrations while forwarding Link Controller messages
1318 * @param cls the LCFContext
1319 * @param emsg the error message; NULL if host registration is successful
1322 lcf_proc_cc (void *cls, const char *emsg)
1324 struct LCFContext *lcf = cls;
1326 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1331 goto registration_error;
1332 lcf->state = DELEGATED_HOST_REGISTERED;
1333 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1335 case DELEGATED_HOST_REGISTERED:
1337 goto registration_error;
1338 lcf->state = SLAVE_HOST_REGISTERED;
1339 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1342 GNUNET_assert (0); /* Shouldn't reach here */
1347 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1349 lcf->state = FINISHED;
1350 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1355 * Callback to be called when forwarded link controllers operation is
1356 * successfull. We have to relay the reply msg back to the client
1358 * @param cls ForwardedOperationContext
1359 * @param msg the peer create success message
1362 forwarded_operation_reply_relay (void *cls,
1363 const struct GNUNET_MessageHeader *msg)
1365 struct ForwardedOperationContext *fopc = cls;
1366 struct GNUNET_MessageHeader *dup_msg;
1369 msize = ntohs (msg->size);
1370 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1372 dup_msg = GNUNET_malloc (msize);
1373 (void) memcpy (dup_msg, msg, msize);
1374 queue_message (fopc->client, dup_msg);
1375 GNUNET_SERVER_client_drop (fopc->client);
1376 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1382 * Task to free resources when forwarded link controllers has been timedout
1384 * @param cls the ForwardedOperationContext
1385 * @param tc the task context from scheduler
1388 forwarded_operation_timeout (void *cls,
1389 const struct GNUNET_SCHEDULER_TaskContext *tc)
1391 struct ForwardedOperationContext *fopc = cls;
1393 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1394 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1395 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1396 GNUNET_SERVER_client_drop (fopc->client);
1402 * The Link Controller forwarding task
1404 * @param cls the LCFContext
1405 * @param tc the Task context from scheduler
1408 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1410 struct LCFContext *lcf = cls;
1411 struct LCFContextQueue *lcfq;
1412 struct ForwardedOperationContext *fopc;
1414 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1419 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1420 lcf->gateway->controller))
1422 queue_host_registration (lcf->gateway,
1424 host_list[lcf->delegated_host_id]);
1428 lcf->state = DELEGATED_HOST_REGISTERED;
1429 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1432 case DELEGATED_HOST_REGISTERED:
1434 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1435 lcf->gateway->controller))
1437 queue_host_registration (lcf->gateway,
1439 host_list[lcf->slave_host_id]);
1443 lcf->state = SLAVE_HOST_REGISTERED;
1444 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1447 case SLAVE_HOST_REGISTERED:
1448 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1449 fopc->client = lcf->client;
1450 fopc->operation_id = lcf->operation_id;
1452 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1455 &forwarded_operation_reply_relay,
1457 fopc->timeout_task =
1458 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1460 lcf->state = FINISHED;
1461 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1465 GNUNET_assert (lcfq->lcf == lcf);
1466 GNUNET_free (lcf->msg);
1468 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1470 if (NULL != lcfq_head)
1472 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1478 * Cleans up ForwardedOverlayConnectContext
1480 * @param focc the ForwardedOverlayConnectContext to cleanup
1483 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1485 GNUNET_free_non_null (focc->orig_msg);
1491 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1493 * @param rhc the RegisteredHostContext
1496 process_next_focc (struct RegisteredHostContext *rhc);
1500 * Timeout task for cancelling a forwarded overlay connect connect
1502 * @param cls the ForwardedOverlayConnectContext
1503 * @param tc the task context from the scheduler
1506 forwarded_overlay_connect_timeout (void *cls,
1507 const struct GNUNET_SCHEDULER_TaskContext
1510 struct ForwardedOperationContext *fopc = cls;
1511 struct RegisteredHostContext *rhc;
1512 struct ForwardedOverlayConnectContext *focc;
1515 focc = rhc->focc_dll_head;
1516 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1517 cleanup_focc (focc);
1518 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
1519 focc->peer1, focc->peer2);
1520 forwarded_operation_timeout (cls, tc);
1521 if (NULL != rhc->focc_dll_head)
1522 process_next_focc (rhc);
1527 * Callback to be called when forwarded overlay connection operation has a reply
1528 * from the sub-controller successfull. We have to relay the reply msg back to
1531 * @param cls ForwardedOperationContext
1532 * @param msg the peer create success message
1535 forwarded_overlay_connect_listener (void *cls,
1536 const struct GNUNET_MessageHeader *msg)
1538 struct ForwardedOperationContext *fopc = cls;
1539 struct RegisteredHostContext *rhc;
1540 struct ForwardedOverlayConnectContext *focc;
1543 forwarded_operation_reply_relay (cls, msg);
1544 focc = rhc->focc_dll_head;
1545 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1546 cleanup_focc (focc);
1547 if (NULL != rhc->focc_dll_head)
1548 process_next_focc (rhc);
1553 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1555 * @param rhc the RegisteredHostContext
1558 process_next_focc (struct RegisteredHostContext *rhc)
1560 struct ForwardedOperationContext *fopc;
1561 struct ForwardedOverlayConnectContext *focc;
1563 focc = rhc->focc_dll_head;
1564 GNUNET_assert (NULL != focc);
1565 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1566 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1567 GNUNET_SERVER_client_keep (rhc->client);
1568 fopc->client = rhc->client;
1569 fopc->operation_id = focc->operation_id;
1572 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1573 focc->operation_id, focc->orig_msg,
1574 &forwarded_overlay_connect_listener,
1576 GNUNET_free (focc->orig_msg);
1577 focc->orig_msg = NULL;
1578 fopc->timeout_task =
1579 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1585 * Callback for event from slave controllers
1587 * @param cls struct Slave *
1588 * @param event information about the event
1591 slave_event_callback (void *cls,
1592 const struct GNUNET_TESTBED_EventInformation *event)
1594 struct RegisteredHostContext *rhc;
1595 struct GNUNET_CONFIGURATION_Handle *cfg;
1596 struct GNUNET_TESTBED_Operation *old_op;
1598 /* We currently only get here when working on RegisteredHostContexts */
1599 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1600 rhc = event->details.operation_finished.op_cls;
1601 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1605 cfg = event->details.operation_finished.generic;
1606 old_op = rhc->sub_op;
1607 rhc->state = RHC_LINK;
1609 GNUNET_TESTBED_controller_link (rhc,
1610 rhc->gateway->controller,
1615 GNUNET_TESTBED_operation_done (old_op);
1618 LOG_DEBUG ("OL: Linking controllers successfull\n");
1619 GNUNET_TESTBED_operation_done (rhc->sub_op);
1621 rhc->state = RHC_OL_CONNECT;
1622 process_next_focc (rhc);
1631 * Callback to signal successfull startup of the controller process
1633 * @param cls the handle to the slave whose status is to be found here
1634 * @param cfg the configuration with which the controller has been started;
1635 * NULL if status is not GNUNET_OK
1636 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1637 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1640 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1643 struct Slave *slave = cls;
1644 struct LinkControllersContext *lcc;
1647 if (GNUNET_SYSERR == status)
1649 slave->controller_proc = NULL;
1650 slave_list[slave->host_id] = NULL;
1651 if (NULL != slave->cfg)
1652 GNUNET_CONFIGURATION_destroy (slave->cfg);
1653 GNUNET_free (slave);
1655 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1656 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1660 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1662 &slave_event_callback, slave);
1663 if (NULL != slave->controller)
1665 send_operation_success_msg (lcc->client, lcc->operation_id);
1666 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1670 send_operation_fail_msg (lcc->client, lcc->operation_id,
1671 "Could not connect to delegated controller");
1672 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1673 slave_list[slave->host_id] = NULL;
1674 GNUNET_free (slave);
1681 if (NULL != lcc->client)
1683 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1684 GNUNET_SERVER_client_drop (lcc->client);
1695 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1698 * @param client identification of the client
1699 * @param message the actual message
1702 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1703 const struct GNUNET_MessageHeader *message)
1705 const struct GNUNET_TESTBED_InitMessage *msg;
1706 struct GNUNET_TESTBED_Host *host;
1707 const char *controller_hostname;
1710 if (NULL != master_context)
1712 LOG_DEBUG ("We are being connected to laterally\n");
1713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1716 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1717 msize = ntohs (message->size);
1718 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1724 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1725 controller_hostname = (const char *) &msg[1];
1726 if ('\0' != controller_hostname[msize - 1])
1729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1732 master_context = GNUNET_malloc (sizeof (struct Context));
1733 master_context->client = client;
1734 master_context->host_id = ntohl (msg->host_id);
1735 master_context->master_ip = GNUNET_strdup (controller_hostname);
1736 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1737 master_context->system =
1738 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1740 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1741 master_context->master_ip,
1744 host_list_add (host);
1745 GNUNET_SERVER_client_keep (client);
1746 LOG_DEBUG ("Created master context with host ID: %u\n",
1747 master_context->host_id);
1748 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1753 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1756 * @param client identification of the client
1757 * @param message the actual message
1760 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1761 const struct GNUNET_MessageHeader *message)
1763 struct GNUNET_TESTBED_Host *host;
1764 const struct GNUNET_TESTBED_AddHostMessage *msg;
1765 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1770 uint16_t username_length;
1771 uint16_t hostname_length;
1772 uint16_t reply_size;
1775 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1776 msize = ntohs (msg->header.size);
1777 username = (char *) &msg[1];
1778 username_length = ntohs (msg->user_name_length);
1779 if (0 != username_length)
1781 /* msg must contain hostname */
1782 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1783 username_length + 1));
1784 if (0 != username_length)
1785 GNUNET_assert ('\0' == username[username_length - 1]);
1786 hostname = username + username_length;
1788 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1789 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1790 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1791 host_id = ntohl (msg->host_id);
1792 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1793 LOG_DEBUG ("-------host id: %u\n", host_id);
1794 LOG_DEBUG ("-------hostname: %s\n", hostname);
1795 if (0 != username_length)
1796 LOG_DEBUG ("-------username: %s\n", username);
1799 LOG_DEBUG ("-------username: NULL\n");
1802 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1804 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1805 ntohs (msg->ssh_port));
1806 GNUNET_assert (NULL != host);
1807 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1808 if (GNUNET_OK != host_list_add (host))
1810 /* We are unable to add a host */
1811 emsg = "A host exists with given host-id";
1812 LOG_DEBUG ("%s: %u", emsg, host_id);
1813 GNUNET_TESTBED_host_destroy (host);
1814 reply_size += strlen (emsg) + 1;
1815 reply = GNUNET_malloc (reply_size);
1816 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1820 LOG_DEBUG ("Added host %u at %u\n",
1821 host_id, master_context->host_id);
1822 reply = GNUNET_malloc (reply_size);
1824 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1825 reply->header.size = htons (reply_size);
1826 reply->host_id = htonl (host_id);
1827 queue_message (client, &reply->header);
1828 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1833 * Iterator over hash map entries.
1835 * @param cls closure
1836 * @param key current key code
1837 * @param value value in the hash map
1838 * @return GNUNET_YES if we should continue to
1843 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1845 struct SharedService *queried_ss = cls;
1846 struct SharedService *ss = value;
1848 if (0 == strcmp (ss->name, queried_ss->name))
1856 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1859 * @param client identification of the client
1860 * @param message the actual message
1863 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1864 const struct GNUNET_MessageHeader *message)
1866 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1867 struct SharedService *ss;
1869 struct GNUNET_HashCode hash;
1871 uint16_t service_name_size;
1873 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1874 msg_size = ntohs (message->size);
1875 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1882 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1883 service_name = (char *) &msg[1];
1884 if ('\0' != service_name[service_name_size - 1])
1887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1890 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1891 service_name, ntohl (msg->num_peers));
1892 if (ntohl (msg->host_id) != master_context->host_id)
1894 route_message (ntohl (msg->host_id), message);
1895 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1899 ss = GNUNET_malloc (sizeof (struct SharedService));
1900 ss->name = strdup (service_name);
1901 ss->num_shared = ntohl (msg->num_peers);
1902 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1903 if (GNUNET_SYSERR ==
1904 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1905 &ss_exists_iterator, ss))
1907 LOG (GNUNET_ERROR_TYPE_WARNING,
1908 "Service %s already configured as a shared service. "
1909 "Ignoring service sharing request \n", ss->name);
1910 GNUNET_free (ss->name);
1914 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1920 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1923 * @param client identification of the client
1924 * @param message the actual message
1927 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1928 const struct GNUNET_MessageHeader *message)
1930 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1931 struct GNUNET_CONFIGURATION_Handle *cfg;
1932 struct LCFContextQueue *lcfq;
1933 struct Route *route;
1934 struct Route *new_route;
1938 uint32_t delegated_host_id;
1939 uint32_t slave_host_id;
1942 if (NULL == master_context)
1945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1948 msize = ntohs (message->size);
1949 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1955 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1956 delegated_host_id = ntohl (msg->delegated_host_id);
1957 if (delegated_host_id == master_context->host_id)
1960 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1964 if ((delegated_host_id >= host_list_size) ||
1965 (NULL == host_list[delegated_host_id]))
1967 LOG (GNUNET_ERROR_TYPE_WARNING,
1968 "Delegated host %u not registered with us\n", delegated_host_id);
1969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1972 slave_host_id = ntohl (msg->slave_host_id);
1973 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1975 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1980 if (slave_host_id == delegated_host_id)
1982 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1987 if (slave_host_id == master_context->host_id) /* Link from us */
1989 struct Slave *slave;
1990 struct LinkControllersContext *lcc;
1992 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1993 config_size = ntohs (msg->config_size);
1994 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1996 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1998 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2001 config = GNUNET_malloc (config_size);
2002 dest_size = (uLongf) config_size;
2004 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
2007 GNUNET_break (0); /* Compression error */
2008 GNUNET_free (config);
2009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2012 if (config_size != dest_size)
2014 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
2015 GNUNET_free (config);
2016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2019 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
2021 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2023 GNUNET_break (0); /* Configuration parsing error */
2024 GNUNET_free (config);
2025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2028 GNUNET_free (config);
2029 if ((delegated_host_id < slave_list_size) &&
2030 (NULL != slave_list[delegated_host_id]))
2032 GNUNET_break (0); /* Configuration parsing error */
2033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2036 slave = GNUNET_malloc (sizeof (struct Slave));
2037 slave->host_id = delegated_host_id;
2038 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
2039 slave_list_add (slave);
2040 if (1 != msg->is_subordinate)
2043 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
2045 &slave_event_callback, slave);
2047 if (NULL != slave->controller)
2048 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2050 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2051 "Could not connect to delegated controller");
2052 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2055 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
2056 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
2057 GNUNET_SERVER_client_keep (client);
2058 lcc->client = client;
2060 slave->controller_proc =
2061 GNUNET_TESTBED_controller_start (master_context->master_ip,
2062 host_list[slave->host_id], cfg,
2063 &slave_status_callback, slave);
2064 GNUNET_CONFIGURATION_destroy (cfg);
2065 new_route = GNUNET_malloc (sizeof (struct Route));
2066 new_route->dest = delegated_host_id;
2067 new_route->thru = master_context->host_id;
2068 route_list_add (new_route);
2072 /* Route the request */
2073 if (slave_host_id >= route_list_size)
2075 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
2076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2079 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
2080 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
2081 lcfq->lcf->delegated_host_id = delegated_host_id;
2082 lcfq->lcf->slave_host_id = slave_host_id;
2083 route = find_dest_route (slave_host_id);
2084 GNUNET_assert (NULL != route); /* because we add routes carefully */
2085 GNUNET_assert (route->dest < slave_list_size);
2086 GNUNET_assert (NULL != slave_list[route->dest]);
2087 lcfq->lcf->state = INIT;
2088 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
2089 lcfq->lcf->gateway = slave_list[route->dest];
2090 lcfq->lcf->msg = GNUNET_malloc (msize);
2091 (void) memcpy (lcfq->lcf->msg, msg, msize);
2092 GNUNET_SERVER_client_keep (client);
2093 lcfq->lcf->client = client;
2094 if (NULL == lcfq_head)
2096 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2097 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2098 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
2101 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2102 /* FIXME: Adding a new route should happen after the controllers are linked
2104 if (1 != msg->is_subordinate)
2106 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2109 if ((delegated_host_id < route_list_size)
2110 && (NULL != route_list[delegated_host_id]))
2112 GNUNET_break_op (0); /* Are you trying to link delegated host twice
2113 with is subordinate flag set to GNUNET_YES? */
2114 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2117 new_route = GNUNET_malloc (sizeof (struct Route));
2118 new_route->dest = delegated_host_id;
2119 new_route->thru = route->dest;
2120 route_list_add (new_route);
2121 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2126 * The task to be executed if the forwarded peer create operation has been
2129 * @param cls the FowardedOperationContext
2130 * @param tc the TaskContext from the scheduler
2133 peer_create_forward_timeout (void *cls,
2134 const struct GNUNET_SCHEDULER_TaskContext *tc)
2136 struct ForwardedOperationContext *fopc = cls;
2138 GNUNET_free (fopc->cls);
2139 forwarded_operation_timeout (fopc, tc);
2144 * Callback to be called when forwarded peer create operation is successfull. We
2145 * have to relay the reply msg back to the client
2147 * @param cls ForwardedOperationContext
2148 * @param msg the peer create success message
2151 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2153 struct ForwardedOperationContext *fopc = cls;
2154 struct GNUNET_MessageHeader *dup_msg;
2155 struct Peer *remote_peer;
2157 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2158 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
2160 GNUNET_assert (NULL != fopc->cls);
2161 remote_peer = fopc->cls;
2162 peer_list_add (remote_peer);
2164 dup_msg = GNUNET_copy_message (msg);
2165 queue_message (fopc->client, dup_msg);
2166 GNUNET_SERVER_client_drop (fopc->client);
2172 * Function to destroy a peer
2174 * @param the peer structure to destroy
2177 destroy_peer (struct Peer *peer)
2179 GNUNET_break (0 == peer->reference_cnt);
2180 if (GNUNET_YES == peer->is_remote)
2182 peer_list_remove (peer);
2186 if (GNUNET_YES == peer->details.local.is_running)
2188 GNUNET_TESTING_peer_stop (peer->details.local.peer);
2189 peer->details.local.is_running = GNUNET_NO;
2191 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2192 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2193 peer_list_remove (peer);
2199 * Callback to be called when forwarded peer destroy operation is successfull. We
2200 * have to relay the reply msg back to the client
2202 * @param cls ForwardedOperationContext
2203 * @param msg the peer create success message
2206 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2208 struct ForwardedOperationContext *fopc = cls;
2209 struct GNUNET_MessageHeader *dup_msg;
2210 struct Peer *remote_peer;
2212 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2213 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS == ntohs (msg->type))
2215 remote_peer = fopc->cls;
2216 GNUNET_assert (NULL != remote_peer);
2217 remote_peer->destroy_flag = GNUNET_YES;
2218 if (0 == remote_peer->reference_cnt)
2219 destroy_peer (remote_peer);
2221 dup_msg = GNUNET_copy_message (msg);
2222 queue_message (fopc->client, dup_msg);
2223 GNUNET_SERVER_client_drop (fopc->client);
2230 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
2233 * @param client identification of the client
2234 * @param message the actual message
2237 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
2238 const struct GNUNET_MessageHeader *message)
2240 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
2241 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
2242 struct GNUNET_CONFIGURATION_Handle *cfg;
2243 struct ForwardedOperationContext *fo_ctxt;
2244 struct Route *route;
2249 uint32_t config_size;
2255 msize = ntohs (message->size);
2256 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
2258 GNUNET_break (0); /* We need configuration */
2259 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2262 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
2263 host_id = ntohl (msg->host_id);
2264 peer_id = ntohl (msg->peer_id);
2265 if (UINT32_MAX == peer_id)
2267 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2268 "Cannot create peer with given ID");
2269 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2272 if (host_id == master_context->host_id)
2276 /* We are responsible for this peer */
2277 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
2278 config_size = ntohl (msg->config_size);
2279 config = GNUNET_malloc (config_size);
2280 dest_size = config_size;
2283 uncompress ((Bytef *) config, (uLongf *) & dest_size,
2284 (const Bytef *) &msg[1], (uLong) msize)))
2286 GNUNET_break (0); /* uncompression error */
2287 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2290 if (config_size != dest_size)
2292 GNUNET_break (0); /* Uncompressed config size mismatch */
2293 GNUNET_free (config);
2294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2297 cfg = GNUNET_CONFIGURATION_create ();
2299 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2301 GNUNET_break (0); /* Configuration parsing error */
2302 GNUNET_free (config);
2303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2306 GNUNET_free (config);
2307 peer = GNUNET_malloc (sizeof (struct Peer));
2308 peer->is_remote = GNUNET_NO;
2309 peer->details.local.cfg = cfg;
2311 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
2312 peer->details.local.peer =
2313 GNUNET_TESTING_peer_configure (master_context->system,
2314 peer->details.local.cfg, peer->id,
2315 NULL /* Peer id */ ,
2317 if (NULL == peer->details.local.peer)
2319 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2326 peer->details.local.is_running = GNUNET_NO;
2327 peer_list_add (peer);
2329 GNUNET_malloc (sizeof
2330 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2331 reply->header.size =
2332 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2333 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2334 reply->peer_id = msg->peer_id;
2335 reply->operation_id = msg->operation_id;
2336 queue_message (client, &reply->header);
2337 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2341 /* Forward peer create request */
2342 route = find_dest_route (host_id);
2346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2350 peer = GNUNET_malloc (sizeof (struct Peer));
2351 peer->is_remote = GNUNET_YES;
2353 peer->details.remote.slave = slave_list[route->dest];
2354 peer->details.remote.remote_host_id = host_id;
2355 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2356 GNUNET_SERVER_client_keep (client);
2357 fo_ctxt->client = client;
2358 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2359 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2361 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2362 fo_ctxt->operation_id,
2364 peer_create_success_cb, fo_ctxt);
2365 fo_ctxt->timeout_task =
2366 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2368 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2373 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2376 * @param client identification of the client
2377 * @param message the actual message
2380 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2381 const struct GNUNET_MessageHeader *message)
2383 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2384 struct ForwardedOperationContext *fopc;
2388 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2389 peer_id = ntohl (msg->peer_id);
2390 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2391 peer_id, GNUNET_ntohll (msg->operation_id));
2392 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2394 LOG (GNUNET_ERROR_TYPE_ERROR,
2395 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2396 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2397 "Peer doesn't exist");
2398 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2401 peer = peer_list[peer_id];
2402 if (GNUNET_YES == peer->is_remote)
2404 /* Forward the destory message to sub controller */
2405 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2406 GNUNET_SERVER_client_keep (client);
2407 fopc->client = client;
2409 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2411 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2412 fopc->operation_id, &msg->header,
2413 &peer_destroy_success_cb,
2415 fopc->timeout_task =
2416 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2418 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2421 peer->destroy_flag = GNUNET_YES;
2422 if (0 == peer->reference_cnt)
2423 destroy_peer (peer);
2425 LOG (GNUNET_ERROR_TYPE_WARNING,
2426 "Delaying peer destroy as peer is currently in use\n");
2427 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2428 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2433 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2436 * @param client identification of the client
2437 * @param message the actual message
2440 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2441 const struct GNUNET_MessageHeader *message)
2443 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2444 struct GNUNET_TESTBED_PeerEventMessage *reply;
2445 struct ForwardedOperationContext *fopc;
2449 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2450 peer_id = ntohl (msg->peer_id);
2451 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2454 LOG (GNUNET_ERROR_TYPE_ERROR,
2455 "Asked to start a non existent peer with id: %u\n", peer_id);
2456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2459 peer = peer_list[peer_id];
2460 if (GNUNET_YES == peer->is_remote)
2462 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2463 GNUNET_SERVER_client_keep (client);
2464 fopc->client = client;
2465 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2467 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2468 fopc->operation_id, &msg->header,
2469 &forwarded_operation_reply_relay,
2471 fopc->timeout_task =
2472 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2477 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2479 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2484 peer->details.local.is_running = GNUNET_YES;
2485 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2486 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2487 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2488 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2489 reply->host_id = htonl (master_context->host_id);
2490 reply->peer_id = msg->peer_id;
2491 reply->operation_id = msg->operation_id;
2492 queue_message (client, &reply->header);
2493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2498 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2501 * @param client identification of the client
2502 * @param message the actual message
2505 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2506 const struct GNUNET_MessageHeader *message)
2508 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2509 struct GNUNET_TESTBED_PeerEventMessage *reply;
2510 struct ForwardedOperationContext *fopc;
2514 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2515 peer_id = ntohl (msg->peer_id);
2516 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2518 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2520 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2523 peer = peer_list[peer_id];
2524 if (GNUNET_YES == peer->is_remote)
2526 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2527 GNUNET_SERVER_client_keep (client);
2528 fopc->client = client;
2529 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2531 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2532 fopc->operation_id, &msg->header,
2533 &forwarded_operation_reply_relay,
2535 fopc->timeout_task =
2536 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2541 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2543 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2544 "Peer not running");
2545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2548 peer->details.local.is_running = GNUNET_NO;
2549 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2550 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2551 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2552 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2553 reply->host_id = htonl (master_context->host_id);
2554 reply->peer_id = msg->peer_id;
2555 reply->operation_id = msg->operation_id;
2556 queue_message (client, &reply->header);
2557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2562 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2565 * @param client identification of the client
2566 * @param message the actual message
2569 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2570 const struct GNUNET_MessageHeader *message)
2572 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2573 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2582 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2583 peer_id = ntohl (msg->peer_id);
2584 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2586 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2588 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2591 peer = peer_list[peer_id];
2592 if (GNUNET_YES == peer->is_remote)
2594 struct ForwardedOperationContext *fopc;
2596 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2597 GNUNET_SERVER_client_keep (client);
2598 fopc->client = client;
2599 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2601 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2602 fopc->operation_id, &msg->header,
2603 &forwarded_operation_reply_relay,
2605 fopc->timeout_task =
2606 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2608 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2612 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2614 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2615 GNUNET_free (config);
2618 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2619 reply = GNUNET_realloc (xconfig, msize);
2620 (void) memmove (&reply[1], reply, xc_size);
2621 reply->header.size = htons (msize);
2622 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2623 reply->peer_id = msg->peer_id;
2624 reply->operation_id = msg->operation_id;
2625 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2626 &reply->peer_identity);
2627 reply->config_size = htons ((uint16_t) c_size);
2628 queue_message (client, &reply->header);
2629 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2634 * Cleanup overlay connect context structure
2636 * @param occ the overlay connect context
2639 cleanup_occ (struct OverlayConnectContext *occ)
2641 LOG_DEBUG ("Cleaning up occ\n");
2642 GNUNET_free_non_null (occ->emsg);
2643 GNUNET_free_non_null (occ->hello);
2644 GNUNET_SERVER_client_drop (occ->client);
2645 if (NULL != occ->opc)
2646 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2647 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2648 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2649 if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
2650 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
2651 if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
2652 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2653 if (NULL != occ->ch)
2655 GNUNET_CORE_disconnect (occ->ch);
2656 occ->peer->reference_cnt--;
2658 if (NULL != occ->ghh)
2659 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2660 if (NULL != occ->p1th)
2662 GNUNET_TRANSPORT_disconnect (occ->p1th);
2663 occ->peer->reference_cnt--;
2665 if (NULL != occ->p2th)
2667 GNUNET_TRANSPORT_disconnect (occ->p2th);
2668 peer_list[occ->other_peer_id]->reference_cnt--;
2669 if ((GNUNET_YES == peer_list[occ->other_peer_id]->destroy_flag)
2670 && (0 == peer_list[occ->other_peer_id]->reference_cnt))
2671 destroy_peer (peer_list[occ->other_peer_id]);
2673 if ((GNUNET_YES == occ->peer->destroy_flag)
2674 && (0 == occ->peer->reference_cnt))
2675 destroy_peer (occ->peer);
2676 GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
2682 * Task for cleaing up overlay connect context structure
2684 * @param cls the overlay connect context
2685 * @param tc the task context
2688 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2690 struct OverlayConnectContext *occ = cls;
2692 occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
2698 * Task which will be run when overlay connect request has been timed out
2700 * @param cls the OverlayConnectContext
2701 * @param tc the TaskContext
2704 timeout_overlay_connect (void *cls,
2705 const struct GNUNET_SCHEDULER_TaskContext *tc)
2707 struct OverlayConnectContext *occ = cls;
2709 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2710 LOG (GNUNET_ERROR_TYPE_WARNING,
2711 "Timeout while connecting peers %u and %u\n",
2712 occ->peer_id, occ->other_peer_id);
2713 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2720 * Function called to notify transport users that another
2721 * peer connected to us.
2723 * @param cls closure
2724 * @param new_peer the peer that connected
2725 * @param ats performance data
2726 * @param ats_count number of entries in ats (excluding 0-termination)
2729 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2730 const struct GNUNET_ATS_Information *ats,
2731 unsigned int ats_count)
2733 struct OverlayConnectContext *occ = cls;
2734 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2736 char *other_peer_str;
2738 LOG_DEBUG ("Overlay connect notify\n");
2740 memcmp (new_peer, &occ->peer_identity,
2741 sizeof (struct GNUNET_PeerIdentity)))
2743 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2744 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2746 memcmp (new_peer, &occ->other_peer_identity,
2747 sizeof (struct GNUNET_PeerIdentity)))
2749 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2750 new_peer_str, other_peer_str);
2751 GNUNET_free (new_peer_str);
2752 GNUNET_free (other_peer_str);
2755 GNUNET_free (new_peer_str);
2756 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2757 GNUNET_i2s (&occ->peer_identity));
2758 GNUNET_free (other_peer_str);
2759 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2761 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2762 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2764 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2765 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2766 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2767 GNUNET_free_non_null (occ->emsg);
2769 if (NULL != occ->p2th)
2771 GNUNET_TRANSPORT_disconnect (occ->p2th);
2772 peer_list[occ->other_peer_id]->reference_cnt--;
2775 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2776 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2778 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2779 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2780 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2781 msg->peer1 = htonl (occ->peer_id);
2782 msg->peer2 = htonl (occ->other_peer_id);
2783 msg->operation_id = GNUNET_htonll (occ->op_id);
2784 queue_message (occ->client, &msg->header);
2785 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
2786 //cleanup_occ (occ);
2791 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2794 * @param cls the OverlayConnectContext
2795 * @param tc the TaskContext from scheduler
2798 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2800 struct OverlayConnectContext *occ = cls;
2801 char *other_peer_str;
2803 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2804 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2806 GNUNET_assert (NULL != occ->hello);
2807 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2808 if (NULL != occ->peer2_controller)
2810 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2812 uint16_t hello_size;
2814 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2815 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2816 hello_size = ntohs (occ->hello->size);
2817 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2818 msg = GNUNET_malloc (msize);
2819 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2820 msg->header.size = htons (msize);
2821 msg->peer = htonl (occ->other_peer_id);
2822 msg->operation_id = GNUNET_htonll (occ->op_id);
2823 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2824 sizeof (struct GNUNET_PeerIdentity));
2825 memcpy (msg->hello, occ->hello, hello_size);
2826 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2830 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2831 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2832 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2833 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2834 occ->send_hello_task =
2835 GNUNET_SCHEDULER_add_delayed
2836 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2837 100 * (pow (2, occ->retries++))),
2840 GNUNET_free (other_peer_str);
2844 * Test for checking whether HELLO message is empty
2846 * @param cls empty flag to set
2847 * @param address the HELLO
2848 * @param expiration expiration of the HELLO
2852 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2853 struct GNUNET_TIME_Absolute expiration)
2863 * Function called whenever there is an update to the HELLO of peers in the
2864 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2865 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2867 * @param cls closure
2868 * @param hello our updated HELLO
2871 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2873 struct OverlayConnectContext *occ = cls;
2877 msize = ntohs (hello->size);
2879 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2880 hello, GNUNET_NO, &test_address,
2882 if (GNUNET_YES == empty)
2884 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2887 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2888 occ->hello = GNUNET_malloc (msize);
2889 memcpy (occ->hello, hello, msize);
2890 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2892 GNUNET_TRANSPORT_disconnect (occ->p1th);
2894 occ->peer->reference_cnt--;
2895 GNUNET_free_non_null (occ->emsg);
2896 if (NULL == occ->peer2_controller)
2898 peer_list[occ->other_peer_id]->reference_cnt++;
2900 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2901 &occ->other_peer_identity, NULL, NULL, NULL,
2903 if (NULL == occ->p2th)
2905 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2906 GNUNET_i2s (&occ->other_peer_identity));
2907 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2908 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2912 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2913 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2918 * Function called after GNUNET_CORE_connect has succeeded (or failed
2919 * for good). Note that the private key of the peer is intentionally
2920 * not exposed here; if you need it, your process should try to read
2921 * the private key file directly (which should work if you are
2924 * @param cls closure
2925 * @param server handle to the server, NULL if we failed
2926 * @param my_identity ID of this peer, NULL if we failed
2929 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2930 const struct GNUNET_PeerIdentity *my_identity)
2932 struct OverlayConnectContext *occ = cls;
2934 GNUNET_free_non_null (occ->emsg);
2935 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2936 if ((NULL == server) || (NULL == my_identity))
2938 GNUNET_free (occ->emsg);
2941 memcpy (&occ->peer_identity, my_identity,
2942 sizeof (struct GNUNET_PeerIdentity));
2943 occ->peer->reference_cnt++;
2945 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2946 &occ->peer_identity, NULL, NULL, NULL, NULL);
2947 if (NULL == occ->p1th)
2949 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2950 GNUNET_i2s (&occ->peer_identity));
2953 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2954 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2955 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2959 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2960 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2966 * Callback to be called when forwarded get peer config operation as part of
2967 * overlay connect is successfull. Connection to Peer 1's core is made and is
2968 * checked for new connection from peer 2
2970 * @param cls ForwardedOperationContext
2971 * @param msg the peer create success message
2974 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2976 struct OverlayConnectContext *occ = cls;
2977 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2978 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2983 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2985 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2987 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2988 sizeof (struct GNUNET_PeerIdentity));
2989 GNUNET_free_non_null (occ->emsg);
2990 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2991 occ->peer->reference_cnt++;
2993 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2994 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2995 GNUNET_NO, no_handlers);
2996 if (NULL == occ->ch)
3001 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3003 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3008 * Callback which will be called to after a host registration succeeded or failed
3010 * @param cls the RegisteredHostContext
3011 * @param emsg the error message; NULL if host registration is successful
3014 registeredhost_registration_completion (void *cls, const char *emsg)
3016 struct RegisteredHostContext *rhc = cls;
3017 struct GNUNET_CONFIGURATION_Handle *cfg;
3018 uint32_t peer2_host_id;
3020 /* if (NULL != rhc->focc_dll_head) */
3021 /* process_next_focc (rhc); */
3022 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
3023 GNUNET_assert (RHC_INIT == rhc->state);
3024 GNUNET_assert (NULL == rhc->sub_op);
3025 if ((NULL == rhc->gateway2)
3026 || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
3027 && (NULL != slave_list[peer2_host_id])))
3029 rhc->state = RHC_LINK;
3030 cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
3032 GNUNET_TESTBED_controller_link (rhc,
3033 rhc->gateway->controller,
3040 rhc->state = RHC_GET_CFG;
3041 rhc->sub_op = GNUNET_TESTBED_get_slave_config (rhc,
3042 rhc->gateway2->controller,
3048 * Iterator to match a registered host context
3050 * @param cls pointer 2 pointer of RegisteredHostContext
3051 * @param key current key code
3052 * @param value value in the hash map
3053 * @return GNUNET_YES if we should continue to
3058 reghost_match_iterator (void *cls,
3059 const struct GNUNET_HashCode * key,
3062 struct RegisteredHostContext **rh = cls;
3063 struct RegisteredHostContext *rh_val = value;
3065 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
3076 * Function to generate the hashcode corresponding to a RegisteredHostContext
3078 * @param reg_host the host which is being registered in RegisteredHostContext
3079 * @param host the host of the controller which has to connect to the above rhost
3080 * @return the hashcode
3082 static struct GNUNET_HashCode
3083 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
3084 struct GNUNET_TESTBED_Host *host)
3086 struct GNUNET_HashCode hash;
3087 uint32_t host_ids[2];
3089 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
3090 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
3091 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
3097 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
3100 * @param client identification of the client
3101 * @param message the actual message
3104 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
3105 const struct GNUNET_MessageHeader *message)
3107 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
3108 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3112 struct OverlayConnectContext *occ;
3113 struct GNUNET_TESTBED_Controller *peer2_controller;
3114 uint64_t operation_id;
3117 uint32_t peer2_host_id;
3119 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
3120 p1 = ntohl (msg->peer1);
3121 p2 = ntohl (msg->peer2);
3122 peer2_host_id = ntohl (msg->peer2_host_id);
3123 GNUNET_assert (p1 < peer_list_size);
3124 GNUNET_assert (NULL != peer_list[p1]);
3125 peer = peer_list[p1];
3126 operation_id = GNUNET_ntohll (msg->operation_id);
3127 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
3128 p1, p2, operation_id);
3129 if (GNUNET_YES == peer->is_remote)
3131 struct ForwardedOperationContext *fopc;
3132 struct Route *route_to_peer2_host;
3133 struct Route *route_to_peer1_host;
3135 LOG_DEBUG ("Forwarding overlay connect\n");
3136 route_to_peer2_host = NULL;
3137 route_to_peer1_host = NULL;
3138 route_to_peer2_host = find_dest_route (peer2_host_id);
3139 if ((NULL != route_to_peer2_host)
3140 || (peer2_host_id == master_context->host_id))
3142 /* Peer 2 either below us OR with us */
3143 route_to_peer1_host =
3144 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
3145 /* Because we get this message only if we know where peer 1 is */
3146 GNUNET_assert (NULL != route_to_peer1_host);
3147 if ((peer2_host_id == master_context->host_id)
3148 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
3150 /* Peer2 is either with us OR peer1 and peer2 can be reached through
3151 different gateways */
3152 struct GNUNET_HashCode hash;
3153 struct RegisteredHostContext *rhc;
3156 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
3157 if (NULL != route_to_peer2_host)
3158 rhc->reg_host = host_list[route_to_peer2_host->dest];
3160 rhc->reg_host = host_list[master_context->host_id];
3161 rhc->host = host_list[route_to_peer1_host->dest];
3162 GNUNET_assert (NULL != rhc->reg_host);
3163 GNUNET_assert (NULL != rhc->host);
3164 rhc->gateway = peer->details.remote.slave;
3165 rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
3166 slave_list[route_to_peer2_host->dest];
3167 rhc->state = RHC_INIT;
3168 GNUNET_SERVER_client_keep (client);
3169 rhc->client = client;
3170 hash = hash_hosts (rhc->reg_host, rhc->host);
3171 skip_focc = GNUNET_NO;
3173 GNUNET_CONTAINER_multihashmap_contains
3174 (peer->details.remote.slave->reghost_map, &hash))
3175 || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
3176 (peer->details.remote.slave->reghost_map, &hash,
3177 reghost_match_iterator, &rhc)))
3179 /* create and add a new registerd host context */
3180 /* add the focc to its queue */
3181 GNUNET_CONTAINER_multihashmap_put
3182 (peer->details.remote.slave->reghost_map,
3183 &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3184 GNUNET_assert (NULL != host_list[peer2_host_id]);
3185 queue_host_registration (peer->details.remote.slave,
3186 registeredhost_registration_completion, rhc,
3187 host_list[peer2_host_id]);
3190 /* rhc is now set to the existing one from the hash map by
3191 reghost_match_iterator() */
3192 /* if queue is empty then ignore creating focc and proceed with
3193 normal forwarding */
3194 if (NULL == rhc->focc_dll_head)
3195 skip_focc = GNUNET_YES;
3197 if (GNUNET_NO == skip_focc)
3199 struct ForwardedOverlayConnectContext *focc;
3202 msize = sizeof (struct GNUNET_TESTBED_OverlayConnectMessage);
3203 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
3206 focc->peer2_host_id = peer2_host_id;
3207 focc->orig_msg = GNUNET_malloc (msize);
3208 (void) memcpy (focc->orig_msg, message, msize);
3209 focc->operation_id = operation_id;
3210 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
3213 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3218 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
3219 GNUNET_SERVER_client_keep (client);
3220 fopc->client = client;
3221 fopc->operation_id = operation_id;
3223 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
3224 operation_id, message,
3225 &forwarded_operation_reply_relay,
3227 fopc->timeout_task =
3228 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
3230 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3234 peer2_controller = NULL;
3235 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
3237 if ((peer2_host_id >= slave_list_size)
3238 || (NULL ==slave_list[peer2_host_id]))
3240 struct GNUNET_TESTBED_NeedControllerConfig *reply;
3242 LOG_DEBUG ("Need controller configuration for connecting peers %u and %u\n",
3244 reply = GNUNET_malloc (sizeof (struct
3245 GNUNET_TESTBED_NeedControllerConfig));
3246 reply->header.size = htons (sizeof (struct
3247 GNUNET_TESTBED_NeedControllerConfig));
3248 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG);
3249 reply->controller_host_id = msg->peer2_host_id;
3250 reply->operation_id = msg->operation_id;
3251 queue_message (client, &reply->header);
3252 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3257 //occ->peer2_controller = slave_list[peer2_host_id]->controller;
3258 peer2_controller = slave_list[peer2_host_id]->controller;
3259 if (NULL == peer2_controller)
3261 GNUNET_break (0); /* What's going on? */
3262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3269 if (GNUNET_YES == peer_list[p2]->is_remote)
3270 peer2_controller = peer_list[p2]->details.remote.slave->controller;
3272 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
3273 GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
3274 GNUNET_SERVER_client_keep (client);
3275 occ->client = client;
3277 occ->other_peer_id = p2;
3278 occ->peer = peer_list[p1];
3279 occ->op_id = GNUNET_ntohll (msg->operation_id);
3280 occ->peer2_controller = peer2_controller;
3281 /* Get the identity of the second peer */
3282 if (NULL != occ->peer2_controller)
3284 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
3287 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
3288 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
3289 cmsg.peer_id = msg->peer2;
3290 cmsg.operation_id = msg->operation_id;
3292 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
3293 occ->op_id, &cmsg.header,
3294 &overlay_connect_get_config,
3297 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
3299 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3300 (GNUNET_TIME_UNIT_SECONDS, 30),
3301 &timeout_overlay_connect, occ);
3302 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3305 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
3306 &occ->other_peer_identity);
3307 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
3308 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3309 occ->peer->reference_cnt++;
3311 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3312 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3313 GNUNET_NO, no_handlers);
3314 if (NULL == occ->ch)
3316 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3319 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3320 (GNUNET_TIME_UNIT_SECONDS, 30),
3321 &timeout_overlay_connect, occ);
3322 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3327 * Function to cleanup RequestOverlayConnectContext and any associated tasks
3330 * @param rocc the RequestOverlayConnectContext
3333 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
3335 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
3336 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
3337 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
3338 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
3339 GNUNET_TRANSPORT_disconnect (rocc->th);
3340 GNUNET_free_non_null (rocc->hello);
3341 GNUNET_CONTAINER_DLL_remove (roccq_head, roccq_tail, rocc);
3347 * Task to timeout rocc and cleanit up
3349 * @param cls the RequestOverlayConnectContext
3350 * @param tc the TaskContext from scheduler
3353 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3355 struct RequestOverlayConnectContext *rocc = cls;
3357 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
3358 cleanup_rocc (rocc);
3363 * Function called to notify transport users that another
3364 * peer connected to us.
3366 * @param cls closure
3367 * @param new_peer the peer that connected
3368 * @param ats performance data
3369 * @param ats_count number of entries in ats (excluding 0-termination)
3372 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3373 const struct GNUNET_ATS_Information * ats,
3376 struct RequestOverlayConnectContext *rocc = cls;
3378 LOG_DEBUG ("Request Overlay connect notify\n");
3379 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3381 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3382 cleanup_rocc (rocc);
3387 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3388 * whose identity is in RequestOverlayConnectContext
3390 * @param cls the RequestOverlayConnectContext
3391 * @param tc the TaskContext from scheduler
3394 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3396 struct RequestOverlayConnectContext *rocc = cls;
3398 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3399 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
3400 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
3401 rocc->attempt_connect_task_id =
3402 GNUNET_SCHEDULER_add_delayed
3403 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3404 100 * (pow (2, rocc->retries++))),
3405 &attempt_connect_task, rocc);
3410 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3413 * @param client identification of the client
3414 * @param message the actual message
3417 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3418 const struct GNUNET_MessageHeader *message)
3420 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3421 struct RequestOverlayConnectContext *rocc;
3427 msize = ntohs (message->size);
3428 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3434 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3435 if ((NULL == msg->hello) ||
3436 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3442 hsize = ntohs (msg->hello->size);
3443 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3449 peer_id = ntohl (msg->peer);
3450 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3452 GNUNET_break_op (0);
3453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3456 if (GNUNET_YES == peer->is_remote)
3458 struct GNUNET_MessageHeader *msg2;
3460 msg2 = GNUNET_copy_message (message);
3461 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3465 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3466 GNUNET_CONTAINER_DLL_insert_tail (roccq_head, roccq_tail, rocc);
3467 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
3468 NULL, &transport_connect_notify, NULL);
3469 if (NULL == rocc->th)
3473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3476 memcpy (&rocc->a_id, &msg->peer_identity,
3477 sizeof (struct GNUNET_PeerIdentity));
3478 rocc->hello = GNUNET_malloc (hsize);
3479 memcpy (rocc->hello, msg->hello, hsize);
3480 rocc->attempt_connect_task_id =
3481 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3482 rocc->timeout_rocc_task_id =
3483 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3484 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3489 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3492 * @param client identification of the client
3493 * @param message the actual message
3496 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3497 const struct GNUNET_MessageHeader *message)
3499 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3500 struct Slave *slave;
3501 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3505 size_t xconfig_size;
3510 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3511 slave_id = ntohl (msg->slave_id);
3512 op_id = GNUNET_ntohll (msg->operation_id);
3513 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3515 send_operation_fail_msg (client, op_id, "Slave not found");
3516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3519 slave = slave_list[slave_id];
3520 if (NULL == slave->cfg)
3522 send_operation_fail_msg (client, op_id,
3523 "Configuration not found (slave not started by me)");
3524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3527 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3528 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3530 GNUNET_free (config);
3531 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3532 GNUNET_break (reply_size <= UINT16_MAX);
3533 GNUNET_break (config_size <= UINT16_MAX);
3534 reply = GNUNET_realloc (xconfig, reply_size);
3535 (void) memmove (&reply[1], reply, xconfig_size);
3536 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3537 reply->header.size = htons ((uint16_t) reply_size);
3538 reply->slave_id = msg->slave_id;
3539 reply->operation_id = msg->operation_id;
3540 reply->config_size = htons ((uint16_t) config_size);
3541 queue_message (client, &reply->header);
3542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3547 * Iterator over hash map entries.
3549 * @param cls closure
3550 * @param key current key code
3551 * @param value value in the hash map
3552 * @return GNUNET_YES if we should continue to
3557 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3559 struct SharedService *ss = value;
3561 GNUNET_assert (GNUNET_YES ==
3562 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3563 GNUNET_free (ss->name);
3570 * Iterator for freeing hash map entries in a slave's reghost_map
3572 * @param cls handle to the slave
3573 * @param key current key code
3574 * @param value value in the hash map
3575 * @return GNUNET_YES if we should continue to
3580 reghost_free_iterator (void *cls,
3581 const struct GNUNET_HashCode * key,
3584 struct Slave *slave = cls;
3585 struct RegisteredHostContext *rhc = value;
3586 struct ForwardedOverlayConnectContext *focc;
3588 GNUNET_assert (GNUNET_YES ==
3589 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3591 while (NULL != (focc = rhc->focc_dll_head))
3593 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3596 cleanup_focc (focc);
3598 if (NULL != rhc->sub_op)
3599 GNUNET_TESTBED_operation_cancel (rhc->sub_op);
3600 if (NULL != rhc->client)
3601 GNUNET_SERVER_client_drop (rhc->client);
3602 GNUNET_free (value);
3608 * Task to clean up and shutdown nicely
3611 * @param tc the TaskContext from scheduler
3614 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3616 struct LCFContextQueue *lcfq;
3617 struct OverlayConnectContext *occ;
3618 struct RequestOverlayConnectContext *rocc;
3621 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3622 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3623 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3625 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3626 if (NULL != lcfq_head)
3628 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3630 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3631 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3634 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3635 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3637 GNUNET_free (lcfq->lcf->msg);
3638 GNUNET_free (lcfq->lcf);
3639 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3642 while (NULL != (occ = occq_head))
3644 while (NULL != (rocc = roccq_head))
3645 cleanup_rocc (rocc);
3646 /* Clear peer list */
3647 for (id = 0; id < peer_list_size; id++)
3648 if (NULL != peer_list[id])
3650 /* If destroy flag is set it means that this peer should have been
3651 destroyed by a context which we destroy before */
3652 GNUNET_break (GNUNET_NO == peer_list[id]->destroy_flag);
3653 /* counter should be zero as we free all contexts before */
3654 GNUNET_break (0 == peer_list[id]->reference_cnt);
3655 if (GNUNET_NO == peer_list[id]->is_remote)
3657 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3658 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
3659 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3660 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3662 GNUNET_free (peer_list[id]);
3664 GNUNET_free_non_null (peer_list);
3665 /* Clear host list */
3666 for (id = 0; id < host_list_size; id++)
3667 if (NULL != host_list[id])
3668 GNUNET_TESTBED_host_destroy (host_list[id]);
3669 GNUNET_free_non_null (host_list);
3670 /* Clear route list */
3671 for (id = 0; id < route_list_size; id++)
3672 if (NULL != route_list[id])
3673 GNUNET_free (route_list[id]);
3674 GNUNET_free_non_null (route_list);
3675 /* Clear slave_list */
3676 for (id = 0; id < slave_list_size; id++)
3677 if (NULL != slave_list[id])
3679 struct HostRegistration *hr_entry;
3681 while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
3683 GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
3684 slave_list[id]->hr_dll_tail,
3686 GNUNET_free (hr_entry);
3688 if (NULL != slave_list[id]->rhandle)
3689 GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
3690 (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
3691 reghost_free_iterator,
3693 GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
3694 if (NULL != slave_list[id]->cfg)
3695 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
3696 if (NULL != slave_list[id]->controller)
3697 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
3698 if (NULL != slave_list[id]->controller_proc)
3699 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
3700 GNUNET_free (slave_list[id]);
3702 GNUNET_free_non_null (slave_list);
3703 if (NULL != master_context)
3705 GNUNET_free_non_null (master_context->master_ip);
3706 if (NULL != master_context->system)
3707 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
3708 GNUNET_free (master_context);
3709 master_context = NULL;
3711 GNUNET_free_non_null (hostname);
3712 GNUNET_CONFIGURATION_destroy (our_config);
3717 * Callback for client disconnect
3720 * @param client the client which has disconnected
3723 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
3725 if (NULL == master_context)
3727 if (client == master_context->client)
3729 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
3730 GNUNET_SERVER_client_drop (client);
3731 /* should not be needed as we're terminated by failure to read
3732 * from stdin, but if stdin fails for some reason, this shouldn't
3733 * hurt for now --- might need to revise this later if we ever
3734 * decide that master connections might be temporarily down
3735 * for some reason */
3736 //GNUNET_SCHEDULER_shutdown ();
3744 * @param cls closure
3745 * @param server the initialized server
3746 * @param cfg configuration to use
3749 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
3750 const struct GNUNET_CONFIGURATION_Handle *cfg)
3752 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
3753 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
3754 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
3755 {&handle_configure_shared_service, NULL,
3756 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
3757 {&handle_link_controllers, NULL,
3758 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
3759 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
3760 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
3761 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
3762 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
3763 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
3764 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
3765 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
3766 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
3767 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
3768 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
3769 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
3770 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
3772 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
3773 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
3777 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
3778 (cfg, "testbed", "HOSTNAME", &hostname));
3779 our_config = GNUNET_CONFIGURATION_dup (cfg);
3780 GNUNET_SERVER_add_handlers (server, message_handlers);
3781 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
3782 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
3784 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
3785 GNUNET_SCHEDULER_PRIORITY_IDLE,
3786 &shutdown_task, NULL);
3787 LOG_DEBUG ("Testbed startup complete\n");
3788 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
3793 * The starting point of execution
3796 main (int argc, char *const *argv)
3798 //sleep (15); /* Debugging */
3799 return (GNUNET_OK ==
3800 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
3801 &testbed_run, NULL)) ? 0 : 1;
3804 /* end of gnunet-service-testbed.c */