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 * Handle for operations which are forwarded while linking controllers
375 struct ForwardedOperationContext *fopc;
378 * The id of the operation which created this context
380 uint64_t operation_id;
383 * The state of this context
385 enum LCFContextState state;
390 uint32_t delegated_host_id;
395 uint32_t slave_host_id;
401 * Structure of a queue entry in LCFContext request queue
403 struct LCFContextQueue
408 struct LCFContext *lcf;
413 struct LCFContextQueue *next;
418 struct LCFContextQueue *prev;
433 * The peer handle from testing API
435 struct GNUNET_TESTING_Peer *peer;
438 * The modified (by GNUNET_TESTING_peer_configure) configuration this
439 * peer is configured with
441 struct GNUNET_CONFIGURATION_Handle *cfg;
444 * Is the peer running
453 * The slave this peer is started through
458 * The id of the remote host this peer is running on
460 uint32_t remote_host_id;
467 * Is this peer locally created?
472 * Our local reference id for this peer
477 * References to peers are using in forwarded overlay contexts and remote
478 * overlay connect contexts. A peer can only be destroyed after all such
479 * contexts are destroyed. For this, we maintain a reference counter. When we
480 * use a peer in any such context, we increment this counter. We decrement it
481 * when we are destroying these contexts
483 uint32_t reference_cnt;
486 * While destroying a peer, due to the fact that there could be references to
487 * this peer, we delay the peer destroy to a further time. We do this by using
488 * this flag to destroy the peer while destroying a context in which this peer
489 * has been used. When the flag is set to 1 and reference_cnt = 0 we destroy
492 uint32_t destroy_flag;
498 * Context information for transport try connect
500 struct TryConnectContext
503 * The identity of the peer to which the transport has to attempt a connection
505 struct GNUNET_PeerIdentity *pid;
508 * The transport handle
510 struct GNUNET_TRANSPORT_Handle *th;
513 * the try connect handle
515 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
520 GNUNET_SCHEDULER_TaskIdentifier task;
523 * The number of times we attempted to connect
525 unsigned int retries;
531 * Context information for connecting 2 peers in overlay
533 struct OverlayConnectContext
536 * The next pointer for maintaining a DLL
538 struct OverlayConnectContext *next;
541 * The prev pointer for maintaining a DLL
543 struct OverlayConnectContext *prev;
546 * The client which has requested for overlay connection
548 struct GNUNET_SERVER_Client *client;
551 * the peer which has to connect to the other peer
556 * Transport handle of the first peer to get its HELLO
558 struct GNUNET_TRANSPORT_Handle *p1th;
561 * Core handles of the first peer; used to notify when second peer connects to it
563 struct GNUNET_CORE_Handle *ch;
566 * HELLO of the other peer
568 struct GNUNET_MessageHeader *hello;
571 * Get hello handle to acquire HELLO of first peer
573 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
576 * The handle for offering HELLO
578 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
581 * The error message we send if this overlay connect operation has timed out
586 * Operation context for suboperations
588 struct OperationContext *opc;
591 * Controller of peer 2; NULL if the peer is local
593 struct GNUNET_TESTBED_Controller *peer2_controller;
596 * The transport try connect context
598 struct TryConnectContext tcc;
601 * The peer identity of the first peer
603 struct GNUNET_PeerIdentity peer_identity;
606 * The peer identity of the other peer
608 struct GNUNET_PeerIdentity other_peer_identity;
611 * The id of the operation responsible for creating this context
616 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
619 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
622 * The id of the overlay connect timeout task
624 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
627 * The id of the cleanup task
629 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
639 uint32_t other_peer_id;
645 * Context information for RequestOverlayConnect
646 * operations. RequestOverlayConnect is used when peers A, B reside on different
647 * hosts and the host controller for peer B is asked by the host controller of
648 * peer A to make peer B connect to peer A
650 struct RequestOverlayConnectContext
653 * the next pointer for DLL
655 struct RequestOverlayConnectContext *next;
658 * the prev pointer for DLL
660 struct RequestOverlayConnectContext *prev;
663 * The peer handle of peer B
670 struct GNUNET_MessageHeader *hello;
673 * The handle for offering HELLO
675 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
678 * The transport try connect context
680 struct TryConnectContext tcc;
683 * The peer identity of peer A
685 struct GNUNET_PeerIdentity a_id;
688 * Task for offering HELLO of A to B and doing try_connect
690 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
693 * Task to timeout RequestOverlayConnect
695 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
701 * Context information for operations forwarded to subcontrollers
703 struct ForwardedOperationContext
706 * The next pointer for DLL
708 struct ForwardedOperationContext *next;
711 * The prev pointer for DLL
713 struct ForwardedOperationContext *prev;
716 * The generated operation context
718 struct OperationContext *opc;
721 * The client to which we have to reply
723 struct GNUNET_SERVER_Client *client;
731 * Task ID for the timeout task
733 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
736 * The id of the operation that has been forwarded
738 uint64_t operation_id;
741 * The type of the operation which is forwarded
743 enum OperationType type;
749 * Context information used while linking controllers
751 struct LinkControllersContext
754 * The client which initiated the link controller operation
756 struct GNUNET_SERVER_Client *client;
759 * The ID of the operation
761 uint64_t operation_id;
767 * Context information to used during operations which forward the overlay
770 struct ForwardedOverlayConnectContext
773 * next ForwardedOverlayConnectContext in the DLL
775 struct ForwardedOverlayConnectContext *next;
778 * previous ForwardedOverlayConnectContext in the DLL
780 struct ForwardedOverlayConnectContext *prev;
783 * A copy of the original overlay connect message
785 struct GNUNET_MessageHeader *orig_msg;
788 * The id of the operation which created this context information
790 uint64_t operation_id;
803 * Id of the host where peer2 is running
805 uint32_t peer2_host_id;
811 * The master context; generated with the first INIT message
813 static struct Context *master_context;
816 * Our hostname; we give this to all the peers we start
818 static char *hostname;
828 static struct GNUNET_CONFIGURATION_Handle *our_config;
831 * Current Transmit Handle; NULL if no notify transmit exists currently
833 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
840 * The head for the LCF queue
842 static struct LCFContextQueue *lcfq_head;
845 * The tail for the LCF queue
847 static struct LCFContextQueue *lcfq_tail;
850 * The message queue head
852 static struct MessageQueue *mq_head;
855 * The message queue tail
857 static struct MessageQueue *mq_tail;
860 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
862 static struct OverlayConnectContext *occq_head;
865 * DLL tail for OverlayConnectContext DLL
867 static struct OverlayConnectContext *occq_tail;
870 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
873 static struct RequestOverlayConnectContext *roccq_head;
876 * DLL tail for RequectOverlayConnectContext DLL
878 static struct RequestOverlayConnectContext *roccq_tail;
881 * DLL head for forwarded operation contexts
883 static struct ForwardedOperationContext *fopcq_head;
886 * DLL tail for forwarded operation contexts
888 static struct ForwardedOperationContext *fopcq_tail;
893 static struct GNUNET_TESTBED_Host **host_list;
898 static struct Route **route_list;
901 * A list of directly linked neighbours
903 static struct Slave **slave_list;
906 * A list of peers we know about
908 static struct Peer **peer_list;
911 * The hashmap of shared services
913 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
916 * The event mask for the events we listen from sub-controllers
918 static uint64_t event_mask;
921 * The size of the host list
923 static uint32_t host_list_size;
926 * The size of the route list
928 static uint32_t route_list_size;
931 * The size of directly linked neighbours list
933 static uint32_t slave_list_size;
936 * The size of the peer list
938 static uint32_t peer_list_size;
945 * The lcf_task handle
947 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
950 * The shutdown task handle
952 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
956 * Function called to notify a client about the connection begin ready to queue
957 * more data. "buf" will be NULL and "size" zero if the connection was closed
958 * for writing in the meantime.
961 * @param size number of bytes available in buf
962 * @param buf where the callee should write the message
963 * @return number of bytes written to buf
966 transmit_ready_notify (void *cls, size_t size, void *buf)
968 struct MessageQueue *mq_entry;
970 transmit_handle = NULL;
972 GNUNET_assert (NULL != mq_entry);
975 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
976 size = ntohs (mq_entry->msg->size);
977 memcpy (buf, mq_entry->msg, size);
978 GNUNET_free (mq_entry->msg);
979 GNUNET_SERVER_client_drop (mq_entry->client);
980 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
981 GNUNET_free (mq_entry);
983 if (NULL != mq_entry)
985 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
986 ntohs (mq_entry->msg->size),
987 GNUNET_TIME_UNIT_FOREVER_REL,
988 &transmit_ready_notify, NULL);
994 * Queues a message in send queue for sending to the service
996 * @param client the client to whom the queued message has to be sent
997 * @param msg the message to queue
1000 queue_message (struct GNUNET_SERVER_Client *client,
1001 struct GNUNET_MessageHeader *msg)
1003 struct MessageQueue *mq_entry;
1007 type = ntohs (msg->type);
1008 size = ntohs (msg->size);
1009 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
1010 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
1011 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
1012 mq_entry->msg = msg;
1013 mq_entry->client = client;
1014 GNUNET_SERVER_client_keep (client);
1015 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
1017 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
1018 if (NULL == transmit_handle)
1020 GNUNET_SERVER_notify_transmit_ready (client, size,
1021 GNUNET_TIME_UNIT_FOREVER_REL,
1022 &transmit_ready_notify, NULL);
1027 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
1029 * @param ptr the memory block to realloc
1030 * @param size the size of ptr
1031 * @param new_size the size to which ptr has to be realloc'ed
1032 * @return the newly reallocated memory block
1035 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
1037 ptr = GNUNET_realloc (ptr, new_size);
1038 if (new_size > size)
1039 (void) memset (ptr + size, 0, new_size - size);
1045 * Function to add a host to the current list of known hosts
1047 * @param host the host to add
1048 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
1052 host_list_add (struct GNUNET_TESTBED_Host *host)
1057 host_id = GNUNET_TESTBED_host_get_id_ (host);
1058 orig_size = host_list_size;
1059 if (host_list_size <= host_id)
1061 while (host_list_size <= host_id)
1062 host_list_size += LIST_GROW_STEP;
1064 TESTBED_realloc (host_list,
1065 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
1066 sizeof (struct GNUNET_TESTBED_Host *)
1069 if (NULL != host_list[host_id])
1071 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
1072 return GNUNET_SYSERR;
1074 host_list[host_id] = host;
1080 * Adds a route to the route list
1082 * @param route the route to add
1085 route_list_add (struct Route *route)
1089 orig_size = route_list_size;
1090 if (route->dest >= route_list_size)
1092 while (route->dest >= route_list_size)
1093 route_list_size += LIST_GROW_STEP;
1095 TESTBED_realloc (route_list,
1096 sizeof (struct Route *) * orig_size,
1097 sizeof (struct Route *) * route_list_size);
1099 GNUNET_assert (NULL == route_list[route->dest]);
1100 route_list[route->dest] = route;
1105 * Adds a slave to the slave array
1107 * @param slave the slave controller to add
1110 slave_list_add (struct Slave *slave)
1114 orig_size = slave_list_size;
1115 if (slave->host_id >= slave_list_size)
1117 while (slave->host_id >= slave_list_size)
1118 slave_list_size += LIST_GROW_STEP;
1120 TESTBED_realloc (slave_list, sizeof (struct Slave *) * orig_size,
1121 sizeof (struct Slave *) * slave_list_size);
1123 GNUNET_assert (NULL == slave_list[slave->host_id]);
1124 slave_list[slave->host_id] = slave;
1129 * Adds a peer to the peer array
1131 * @param peer the peer to add
1134 peer_list_add (struct Peer *peer)
1138 orig_size = peer_list_size;
1139 if (peer->id >= peer_list_size)
1141 while (peer->id >= peer_list_size)
1142 peer_list_size += LIST_GROW_STEP;
1144 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
1145 sizeof (struct Peer *) * peer_list_size);
1147 GNUNET_assert (NULL == peer_list[peer->id]);
1148 peer_list[peer->id] = peer;
1153 * Removes a the give peer from the peer array
1155 * @param peer the peer to be removed
1158 peer_list_remove (struct Peer *peer)
1163 peer_list[peer->id] = NULL;
1164 orig_size = peer_list_size;
1165 while (peer_list_size >= LIST_GROW_STEP)
1167 for (id = peer_list_size - 1;
1168 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1169 if (NULL != peer_list[id])
1171 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1173 peer_list_size -= LIST_GROW_STEP;
1175 if (orig_size == peer_list_size)
1178 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1183 * Finds the route with directly connected host as destination through which
1184 * the destination host can be reached
1186 * @param host_id the id of the destination host
1187 * @return the route with directly connected destination host; NULL if no route
1190 static struct Route *
1191 find_dest_route (uint32_t host_id)
1193 struct Route *route;
1195 if (route_list_size <= host_id)
1197 while (NULL != (route = route_list[host_id]))
1199 if (route->thru == master_context->host_id)
1201 host_id = route->thru;
1208 * Routes message to a host given its host_id
1210 * @param host_id the id of the destination host
1211 * @param msg the message to be routed
1214 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1221 * Send operation failure message to client
1223 * @param client the client to which the failure message has to be sent to
1224 * @param operation_id the id of the failed operation
1225 * @param emsg the error message; can be NULL
1228 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1229 uint64_t operation_id, const char *emsg)
1231 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1235 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1236 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1238 msg = GNUNET_malloc (msize);
1239 msg->header.size = htons (msize);
1240 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1241 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1242 msg->operation_id = GNUNET_htonll (operation_id);
1244 memcpy (&msg[1], emsg, emsg_len);
1245 queue_message (client, &msg->header);
1250 * Function to send generic operation success message to given client
1252 * @param client the client to send the message to
1253 * @param operation_id the id of the operation which was successful
1256 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1257 uint64_t operation_id)
1259 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1262 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1263 msg = GNUNET_malloc (msize);
1264 msg->header.size = htons (msize);
1265 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1266 msg->operation_id = GNUNET_htonll (operation_id);
1267 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1268 queue_message (client, &msg->header);
1273 * Callback which will be called to after a host registration succeeded or failed
1275 * @param cls the handle to the slave at which the registration is completed
1276 * @param emsg the error message; NULL if host registration is successful
1279 hr_completion (void *cls, const char *emsg);
1283 * Attempts to register the next host in the host registration queue
1285 * @param slave the slave controller whose host registration queue is checked
1286 * for host registrations
1289 register_next_host (struct Slave *slave)
1291 struct HostRegistration *hr;
1293 hr = slave->hr_dll_head;
1294 GNUNET_assert (NULL != hr);
1295 GNUNET_assert (NULL == slave->rhandle);
1296 LOG (GNUNET_ERROR_TYPE_DEBUG,
1297 "Registering host %u at %u\n",
1298 GNUNET_TESTBED_host_get_id_ (hr->host),
1299 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1300 slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
1308 * Callback which will be called to after a host registration succeeded or failed
1310 * @param cls the handle to the slave at which the registration is completed
1311 * @param emsg the error message; NULL if host registration is successful
1314 hr_completion (void *cls, const char *emsg)
1316 struct Slave *slave = cls;
1317 struct HostRegistration *hr;
1319 slave->rhandle = NULL;
1320 hr = slave->hr_dll_head;
1321 GNUNET_assert (NULL != hr);
1322 LOG (GNUNET_ERROR_TYPE_DEBUG,
1323 "Registering host %u at %u successful\n",
1324 GNUNET_TESTBED_host_get_id_ (hr->host),
1325 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1326 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
1330 hr->cb (hr->cb_cls, emsg);
1332 if (NULL != slave->hr_dll_head)
1333 register_next_host (slave);
1338 * Adds a host registration's request to a slave's registration queue
1340 * @param slave the slave controller at which the given host has to be
1342 * @param cb the host registration completion callback
1343 * @param cb_cls the closure for the host registration completion callback
1344 * @param host the host which has to be registered
1347 queue_host_registration (struct Slave *slave,
1348 GNUNET_TESTBED_HostRegistrationCompletion cb,
1350 struct GNUNET_TESTBED_Host *host)
1352 struct HostRegistration *hr;
1355 LOG (GNUNET_ERROR_TYPE_DEBUG,
1356 "Queueing host registration for host %u at %u\n",
1357 GNUNET_TESTBED_host_get_id_ (host),
1358 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1359 hr = GNUNET_malloc (sizeof (struct HostRegistration));
1361 hr->cb_cls = cb_cls;
1363 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
1364 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
1367 if (GNUNET_YES == call_register)
1368 register_next_host (slave);
1373 * The Link Controller forwarding task
1375 * @param cls the LCFContext
1376 * @param tc the Task context from scheduler
1379 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1383 * Completion callback for host registrations while forwarding Link Controller messages
1385 * @param cls the LCFContext
1386 * @param emsg the error message; NULL if host registration is successful
1389 lcf_proc_cc (void *cls, const char *emsg)
1391 struct LCFContext *lcf = cls;
1393 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1398 goto registration_error;
1399 lcf->state = DELEGATED_HOST_REGISTERED;
1400 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1402 case DELEGATED_HOST_REGISTERED:
1404 goto registration_error;
1405 lcf->state = SLAVE_HOST_REGISTERED;
1406 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1409 GNUNET_assert (0); /* Shouldn't reach here */
1414 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1416 lcf->state = FINISHED;
1417 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1422 * Callback to relay the reply msg of a forwarded operation back to the client
1424 * @param cls ForwardedOperationContext
1425 * @param msg the message to relay
1428 forwarded_operation_reply_relay (void *cls,
1429 const struct GNUNET_MessageHeader *msg)
1431 struct ForwardedOperationContext *fopc = cls;
1432 struct GNUNET_MessageHeader *dup_msg;
1435 msize = ntohs (msg->size);
1436 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1438 dup_msg = GNUNET_copy_message (msg);
1439 queue_message (fopc->client, dup_msg);
1440 GNUNET_SERVER_client_drop (fopc->client);
1441 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1442 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1448 * Task to free resources when forwarded operation has been timedout
1450 * @param cls the ForwardedOperationContext
1451 * @param tc the task context from scheduler
1454 forwarded_operation_timeout (void *cls,
1455 const struct GNUNET_SCHEDULER_TaskContext *tc)
1457 struct ForwardedOperationContext *fopc = cls;
1459 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1460 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1461 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1462 GNUNET_SERVER_client_drop (fopc->client);
1463 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1469 * The Link Controller forwarding task
1471 * @param cls the LCFContext
1472 * @param tc the Task context from scheduler
1475 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1479 * Callback to be called when forwarded link controllers operation is
1480 * successfull. We have to relay the reply msg back to the client
1482 * @param cls the LCFContext
1483 * @param msg the message to relay
1486 lcf_forwarded_operation_reply_relay (void *cls,
1487 const struct GNUNET_MessageHeader *msg)
1489 struct LCFContext *lcf = cls;
1491 GNUNET_assert (NULL != lcf->fopc);
1492 forwarded_operation_reply_relay (lcf->fopc, msg);
1494 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1495 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1500 * Task to free resources when forwarded link controllers has been timedout
1502 * @param cls the LCFContext
1503 * @param tc the task context from scheduler
1506 lcf_forwarded_operation_timeout (void *cls,
1507 const struct GNUNET_SCHEDULER_TaskContext *tc)
1509 struct LCFContext *lcf = cls;
1511 GNUNET_assert (NULL != lcf->fopc);
1512 lcf->fopc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1513 forwarded_operation_timeout (lcf->fopc, tc);
1515 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1516 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1521 * The Link Controller forwarding task
1523 * @param cls the LCFContext
1524 * @param tc the Task context from scheduler
1527 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1529 struct LCFContext *lcf = cls;
1530 struct LCFContextQueue *lcfq;
1532 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1537 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1538 lcf->gateway->controller))
1540 queue_host_registration (lcf->gateway,
1542 host_list[lcf->delegated_host_id]);
1546 lcf->state = DELEGATED_HOST_REGISTERED;
1547 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1550 case DELEGATED_HOST_REGISTERED:
1552 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1553 lcf->gateway->controller))
1555 queue_host_registration (lcf->gateway,
1557 host_list[lcf->slave_host_id]);
1561 lcf->state = SLAVE_HOST_REGISTERED;
1562 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1565 case SLAVE_HOST_REGISTERED:
1566 lcf->fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1567 lcf->fopc->client = lcf->client;
1568 lcf->fopc->operation_id = lcf->operation_id;
1569 lcf->fopc->type = OP_LINK_CONTROLLERS;
1571 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1574 &lcf_forwarded_operation_reply_relay,
1576 lcf->fopc->timeout_task =
1577 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &lcf_forwarded_operation_timeout,
1579 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, lcf->fopc);
1580 lcf->state = FINISHED;
1584 GNUNET_assert (lcfq->lcf == lcf);
1585 GNUNET_free (lcf->msg);
1587 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1589 if (NULL != lcfq_head)
1591 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1597 * Cleans up ForwardedOverlayConnectContext
1599 * @param focc the ForwardedOverlayConnectContext to cleanup
1602 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1604 GNUNET_free_non_null (focc->orig_msg);
1610 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1612 * @param rhc the RegisteredHostContext
1615 process_next_focc (struct RegisteredHostContext *rhc);
1619 * Timeout task for cancelling a forwarded overlay connect connect
1621 * @param cls the ForwardedOverlayConnectContext
1622 * @param tc the task context from the scheduler
1625 forwarded_overlay_connect_timeout (void *cls,
1626 const struct GNUNET_SCHEDULER_TaskContext
1629 struct ForwardedOperationContext *fopc = cls;
1630 struct RegisteredHostContext *rhc;
1631 struct ForwardedOverlayConnectContext *focc;
1634 focc = rhc->focc_dll_head;
1635 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1636 cleanup_focc (focc);
1637 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
1638 focc->peer1, focc->peer2);
1639 forwarded_operation_timeout (cls, tc);
1640 if (NULL != rhc->focc_dll_head)
1641 process_next_focc (rhc);
1646 * Callback to be called when forwarded overlay connection operation has a reply
1647 * from the sub-controller successfull. We have to relay the reply msg back to
1650 * @param cls ForwardedOperationContext
1651 * @param msg the peer create success message
1654 forwarded_overlay_connect_listener (void *cls,
1655 const struct GNUNET_MessageHeader *msg)
1657 struct ForwardedOperationContext *fopc = cls;
1658 struct RegisteredHostContext *rhc;
1659 struct ForwardedOverlayConnectContext *focc;
1662 forwarded_operation_reply_relay (cls, msg);
1663 focc = rhc->focc_dll_head;
1664 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1665 cleanup_focc (focc);
1666 if (NULL != rhc->focc_dll_head)
1667 process_next_focc (rhc);
1672 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1674 * @param rhc the RegisteredHostContext
1677 process_next_focc (struct RegisteredHostContext *rhc)
1679 struct ForwardedOperationContext *fopc;
1680 struct ForwardedOverlayConnectContext *focc;
1682 focc = rhc->focc_dll_head;
1683 GNUNET_assert (NULL != focc);
1684 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1685 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1686 GNUNET_SERVER_client_keep (rhc->client);
1687 fopc->client = rhc->client;
1688 fopc->operation_id = focc->operation_id;
1690 fopc->type = OP_OVERLAY_CONNECT;
1692 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1693 focc->operation_id, focc->orig_msg,
1694 &forwarded_overlay_connect_listener,
1696 GNUNET_free (focc->orig_msg);
1697 focc->orig_msg = NULL;
1698 fopc->timeout_task =
1699 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1701 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1706 * Callback for event from slave controllers
1708 * @param cls struct Slave *
1709 * @param event information about the event
1712 slave_event_callback (void *cls,
1713 const struct GNUNET_TESTBED_EventInformation *event)
1715 struct RegisteredHostContext *rhc;
1716 struct GNUNET_CONFIGURATION_Handle *cfg;
1717 struct GNUNET_TESTBED_Operation *old_op;
1719 /* We currently only get here when working on RegisteredHostContexts */
1720 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1721 rhc = event->details.operation_finished.op_cls;
1722 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1726 cfg = event->details.operation_finished.generic;
1727 old_op = rhc->sub_op;
1728 rhc->state = RHC_LINK;
1730 GNUNET_TESTBED_controller_link (rhc,
1731 rhc->gateway->controller,
1736 GNUNET_TESTBED_operation_done (old_op);
1739 LOG_DEBUG ("OL: Linking controllers successfull\n");
1740 GNUNET_TESTBED_operation_done (rhc->sub_op);
1742 rhc->state = RHC_OL_CONNECT;
1743 process_next_focc (rhc);
1752 * Callback to signal successfull startup of the controller process
1754 * @param cls the handle to the slave whose status is to be found here
1755 * @param cfg the configuration with which the controller has been started;
1756 * NULL if status is not GNUNET_OK
1757 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1758 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1761 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1764 struct Slave *slave = cls;
1765 struct LinkControllersContext *lcc;
1768 if (GNUNET_SYSERR == status)
1770 slave->controller_proc = NULL;
1771 slave_list[slave->host_id] = NULL;
1772 if (NULL != slave->cfg)
1773 GNUNET_CONFIGURATION_destroy (slave->cfg);
1774 GNUNET_free (slave);
1776 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1777 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1781 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1783 &slave_event_callback, slave);
1784 if (NULL != slave->controller)
1786 send_operation_success_msg (lcc->client, lcc->operation_id);
1787 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1791 send_operation_fail_msg (lcc->client, lcc->operation_id,
1792 "Could not connect to delegated controller");
1793 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1794 slave_list[slave->host_id] = NULL;
1795 GNUNET_free (slave);
1802 if (NULL != lcc->client)
1804 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1805 GNUNET_SERVER_client_drop (lcc->client);
1816 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1819 * @param client identification of the client
1820 * @param message the actual message
1823 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1824 const struct GNUNET_MessageHeader *message)
1826 const struct GNUNET_TESTBED_InitMessage *msg;
1827 struct GNUNET_TESTBED_Host *host;
1828 const char *controller_hostname;
1831 if (NULL != master_context)
1833 LOG_DEBUG ("We are being connected to laterally\n");
1834 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1837 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1838 msize = ntohs (message->size);
1839 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1842 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1845 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1846 controller_hostname = (const char *) &msg[1];
1847 if ('\0' != controller_hostname[msize - 1])
1850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1853 master_context = GNUNET_malloc (sizeof (struct Context));
1854 GNUNET_SERVER_client_keep (client);
1855 master_context->client = client;
1856 master_context->host_id = ntohl (msg->host_id);
1857 master_context->master_ip = GNUNET_strdup (controller_hostname);
1858 LOG_DEBUG ("Our IP: %s\n", master_context->master_ip);
1859 master_context->system =
1860 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1862 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1863 master_context->master_ip,
1866 host_list_add (host);
1867 LOG_DEBUG ("Created master context with host ID: %u\n",
1868 master_context->host_id);
1869 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1874 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1877 * @param client identification of the client
1878 * @param message the actual message
1881 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1882 const struct GNUNET_MessageHeader *message)
1884 struct GNUNET_TESTBED_Host *host;
1885 const struct GNUNET_TESTBED_AddHostMessage *msg;
1886 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1891 uint16_t username_length;
1892 uint16_t hostname_length;
1893 uint16_t reply_size;
1896 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1897 msize = ntohs (msg->header.size);
1898 username = (char *) &msg[1];
1899 username_length = ntohs (msg->user_name_length);
1900 if (0 != username_length)
1902 /* msg must contain hostname */
1903 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1904 username_length + 1));
1905 if (0 != username_length)
1906 GNUNET_assert ('\0' == username[username_length - 1]);
1907 hostname = username + username_length;
1909 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1910 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1911 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1912 host_id = ntohl (msg->host_id);
1913 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1914 LOG_DEBUG ("-------host id: %u\n", host_id);
1915 LOG_DEBUG ("-------hostname: %s\n", hostname);
1916 if (0 != username_length)
1917 LOG_DEBUG ("-------username: %s\n", username);
1920 LOG_DEBUG ("-------username: NULL\n");
1923 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1925 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1926 ntohs (msg->ssh_port));
1927 GNUNET_assert (NULL != host);
1928 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1929 if (GNUNET_OK != host_list_add (host))
1931 /* We are unable to add a host */
1932 emsg = "A host exists with given host-id";
1933 LOG_DEBUG ("%s: %u", emsg, host_id);
1934 GNUNET_TESTBED_host_destroy (host);
1935 reply_size += strlen (emsg) + 1;
1936 reply = GNUNET_malloc (reply_size);
1937 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1941 LOG_DEBUG ("Added host %u at %u\n",
1942 host_id, master_context->host_id);
1943 reply = GNUNET_malloc (reply_size);
1945 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1946 reply->header.size = htons (reply_size);
1947 reply->host_id = htonl (host_id);
1948 queue_message (client, &reply->header);
1949 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1954 * Iterator over hash map entries.
1956 * @param cls closure
1957 * @param key current key code
1958 * @param value value in the hash map
1959 * @return GNUNET_YES if we should continue to
1964 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1966 struct SharedService *queried_ss = cls;
1967 struct SharedService *ss = value;
1969 if (0 == strcmp (ss->name, queried_ss->name))
1977 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1980 * @param client identification of the client
1981 * @param message the actual message
1984 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1985 const struct GNUNET_MessageHeader *message)
1987 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1988 struct SharedService *ss;
1990 struct GNUNET_HashCode hash;
1992 uint16_t service_name_size;
1994 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1995 msg_size = ntohs (message->size);
1996 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2003 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
2004 service_name = (char *) &msg[1];
2005 if ('\0' != service_name[service_name_size - 1])
2008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2011 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
2012 service_name, ntohl (msg->num_peers));
2013 if (ntohl (msg->host_id) != master_context->host_id)
2015 route_message (ntohl (msg->host_id), message);
2016 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2019 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2020 ss = GNUNET_malloc (sizeof (struct SharedService));
2021 ss->name = strdup (service_name);
2022 ss->num_shared = ntohl (msg->num_peers);
2023 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
2024 if (GNUNET_SYSERR ==
2025 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
2026 &ss_exists_iterator, ss))
2028 LOG (GNUNET_ERROR_TYPE_WARNING,
2029 "Service %s already configured as a shared service. "
2030 "Ignoring service sharing request \n", ss->name);
2031 GNUNET_free (ss->name);
2035 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
2036 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2041 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
2044 * @param client identification of the client
2045 * @param message the actual message
2048 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
2049 const struct GNUNET_MessageHeader *message)
2051 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
2052 struct GNUNET_CONFIGURATION_Handle *cfg;
2053 struct LCFContextQueue *lcfq;
2054 struct Route *route;
2055 struct Route *new_route;
2059 uint32_t delegated_host_id;
2060 uint32_t slave_host_id;
2063 if (NULL == master_context)
2066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2069 msize = ntohs (message->size);
2070 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
2073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2076 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
2077 delegated_host_id = ntohl (msg->delegated_host_id);
2078 if (delegated_host_id == master_context->host_id)
2081 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
2082 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2085 if ((delegated_host_id >= host_list_size) ||
2086 (NULL == host_list[delegated_host_id]))
2088 LOG (GNUNET_ERROR_TYPE_WARNING,
2089 "Delegated host %u not registered with us\n", delegated_host_id);
2090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2093 slave_host_id = ntohl (msg->slave_host_id);
2094 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
2096 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
2098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2101 if (slave_host_id == delegated_host_id)
2103 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
2104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2108 if (slave_host_id == master_context->host_id) /* Link from us */
2110 struct Slave *slave;
2111 struct LinkControllersContext *lcc;
2113 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
2114 config_size = ntohs (msg->config_size);
2115 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
2117 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
2119 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2122 config = GNUNET_malloc (config_size);
2123 dest_size = (uLongf) config_size;
2125 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
2128 GNUNET_break (0); /* Compression error */
2129 GNUNET_free (config);
2130 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2133 if (config_size != dest_size)
2135 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
2136 GNUNET_free (config);
2137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2140 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
2142 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2144 GNUNET_break (0); /* Configuration parsing error */
2145 GNUNET_free (config);
2146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2149 GNUNET_free (config);
2150 if ((delegated_host_id < slave_list_size) &&
2151 (NULL != slave_list[delegated_host_id]))
2153 GNUNET_break (0); /* Configuration parsing error */
2154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2157 slave = GNUNET_malloc (sizeof (struct Slave));
2158 slave->host_id = delegated_host_id;
2159 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
2160 slave_list_add (slave);
2161 if (1 != msg->is_subordinate)
2164 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
2166 &slave_event_callback, slave);
2168 if (NULL != slave->controller)
2169 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2171 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2172 "Could not connect to delegated controller");
2173 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2176 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
2177 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
2178 GNUNET_SERVER_client_keep (client);
2179 lcc->client = client;
2181 slave->controller_proc =
2182 GNUNET_TESTBED_controller_start (master_context->master_ip,
2183 host_list[slave->host_id], cfg,
2184 &slave_status_callback, slave);
2185 GNUNET_CONFIGURATION_destroy (cfg);
2186 new_route = GNUNET_malloc (sizeof (struct Route));
2187 new_route->dest = delegated_host_id;
2188 new_route->thru = master_context->host_id;
2189 route_list_add (new_route);
2193 /* Route the request */
2194 if (slave_host_id >= route_list_size)
2196 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
2197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2200 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
2201 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
2202 lcfq->lcf->delegated_host_id = delegated_host_id;
2203 lcfq->lcf->slave_host_id = slave_host_id;
2204 route = find_dest_route (slave_host_id);
2205 GNUNET_assert (NULL != route); /* because we add routes carefully */
2206 GNUNET_assert (route->dest < slave_list_size);
2207 GNUNET_assert (NULL != slave_list[route->dest]);
2208 lcfq->lcf->state = INIT;
2209 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
2210 lcfq->lcf->gateway = slave_list[route->dest];
2211 lcfq->lcf->msg = GNUNET_malloc (msize);
2212 (void) memcpy (lcfq->lcf->msg, msg, msize);
2213 GNUNET_SERVER_client_keep (client);
2214 lcfq->lcf->client = client;
2215 if (NULL == lcfq_head)
2217 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2218 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2219 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
2222 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2223 /* FIXME: Adding a new route should happen after the controllers are linked
2225 if (1 != msg->is_subordinate)
2227 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2230 if ((delegated_host_id < route_list_size)
2231 && (NULL != route_list[delegated_host_id]))
2233 GNUNET_break_op (0); /* Are you trying to link delegated host twice
2234 with is subordinate flag set to GNUNET_YES? */
2235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2238 new_route = GNUNET_malloc (sizeof (struct Route));
2239 new_route->dest = delegated_host_id;
2240 new_route->thru = route->dest;
2241 route_list_add (new_route);
2242 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2247 * The task to be executed if the forwarded peer create operation has been
2250 * @param cls the FowardedOperationContext
2251 * @param tc the TaskContext from the scheduler
2254 peer_create_forward_timeout (void *cls,
2255 const struct GNUNET_SCHEDULER_TaskContext *tc)
2257 struct ForwardedOperationContext *fopc = cls;
2259 GNUNET_free (fopc->cls);
2260 forwarded_operation_timeout (fopc, tc);
2265 * Callback to be called when forwarded peer create operation is successfull. We
2266 * have to relay the reply msg back to the client
2268 * @param cls ForwardedOperationContext
2269 * @param msg the peer create success message
2272 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2274 struct ForwardedOperationContext *fopc = cls;
2275 struct Peer *remote_peer;
2277 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
2279 GNUNET_assert (NULL != fopc->cls);
2280 remote_peer = fopc->cls;
2281 peer_list_add (remote_peer);
2283 forwarded_operation_reply_relay (fopc, msg);
2288 * Function to destroy a peer
2290 * @param peer the peer structure to destroy
2293 destroy_peer (struct Peer *peer)
2295 GNUNET_break (0 == peer->reference_cnt);
2296 if (GNUNET_YES == peer->is_remote)
2298 peer_list_remove (peer);
2302 if (GNUNET_YES == peer->details.local.is_running)
2304 GNUNET_TESTING_peer_stop (peer->details.local.peer);
2305 peer->details.local.is_running = GNUNET_NO;
2307 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2308 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2309 peer_list_remove (peer);
2315 * Callback to be called when forwarded peer destroy operation is successfull. We
2316 * have to relay the reply msg back to the client
2318 * @param cls ForwardedOperationContext
2319 * @param msg the peer create success message
2322 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2324 struct ForwardedOperationContext *fopc = cls;
2325 struct Peer *remote_peer;
2327 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS == ntohs (msg->type))
2329 remote_peer = fopc->cls;
2330 GNUNET_assert (NULL != remote_peer);
2331 remote_peer->destroy_flag = GNUNET_YES;
2332 if (0 == remote_peer->reference_cnt)
2333 destroy_peer (remote_peer);
2335 forwarded_operation_reply_relay (fopc, msg);
2341 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
2344 * @param client identification of the client
2345 * @param message the actual message
2348 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
2349 const struct GNUNET_MessageHeader *message)
2351 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
2352 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
2353 struct GNUNET_CONFIGURATION_Handle *cfg;
2354 struct ForwardedOperationContext *fo_ctxt;
2355 struct Route *route;
2360 uint32_t config_size;
2366 msize = ntohs (message->size);
2367 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
2369 GNUNET_break (0); /* We need configuration */
2370 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2373 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
2374 host_id = ntohl (msg->host_id);
2375 peer_id = ntohl (msg->peer_id);
2376 if (UINT32_MAX == peer_id)
2378 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2379 "Cannot create peer with given ID");
2380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2383 if (host_id == master_context->host_id)
2387 /* We are responsible for this peer */
2388 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
2389 config_size = ntohl (msg->config_size);
2390 config = GNUNET_malloc (config_size);
2391 dest_size = config_size;
2394 uncompress ((Bytef *) config, (uLongf *) & dest_size,
2395 (const Bytef *) &msg[1], (uLong) msize)))
2397 GNUNET_break (0); /* uncompression error */
2398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2401 if (config_size != dest_size)
2403 GNUNET_break (0); /* Uncompressed config size mismatch */
2404 GNUNET_free (config);
2405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2408 cfg = GNUNET_CONFIGURATION_create ();
2410 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2412 GNUNET_break (0); /* Configuration parsing error */
2413 GNUNET_free (config);
2414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2417 GNUNET_free (config);
2418 GNUNET_CONFIGURATION_set_value_number (cfg,
2421 (unsigned long long) peer_id);
2422 peer = GNUNET_malloc (sizeof (struct Peer));
2423 peer->is_remote = GNUNET_NO;
2424 peer->details.local.cfg = cfg;
2426 LOG_DEBUG ("Creating peer with id: %u\n",
2427 (unsigned int) peer->id);
2428 peer->details.local.peer =
2429 GNUNET_TESTING_peer_configure (master_context->system,
2430 peer->details.local.cfg, peer->id,
2431 NULL /* Peer id */ ,
2433 if (NULL == peer->details.local.peer)
2435 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2442 peer->details.local.is_running = GNUNET_NO;
2443 peer_list_add (peer);
2445 GNUNET_malloc (sizeof
2446 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2447 reply->header.size =
2448 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2449 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2450 reply->peer_id = msg->peer_id;
2451 reply->operation_id = msg->operation_id;
2452 queue_message (client, &reply->header);
2453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2457 /* Forward peer create request */
2458 route = find_dest_route (host_id);
2462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2466 peer = GNUNET_malloc (sizeof (struct Peer));
2467 peer->is_remote = GNUNET_YES;
2469 peer->details.remote.slave = slave_list[route->dest];
2470 peer->details.remote.remote_host_id = host_id;
2471 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2472 GNUNET_SERVER_client_keep (client);
2473 fo_ctxt->client = client;
2474 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2475 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2476 fo_ctxt->type = OP_PEER_CREATE;
2478 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2479 fo_ctxt->operation_id,
2481 peer_create_success_cb, fo_ctxt);
2482 fo_ctxt->timeout_task =
2483 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2485 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
2486 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2491 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2494 * @param client identification of the client
2495 * @param message the actual message
2498 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2499 const struct GNUNET_MessageHeader *message)
2501 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2502 struct ForwardedOperationContext *fopc;
2506 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2507 peer_id = ntohl (msg->peer_id);
2508 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2509 peer_id, GNUNET_ntohll (msg->operation_id));
2510 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2512 LOG (GNUNET_ERROR_TYPE_ERROR,
2513 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2514 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2515 "Peer doesn't exist");
2516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2519 peer = peer_list[peer_id];
2520 if (GNUNET_YES == peer->is_remote)
2522 /* Forward the destory message to sub controller */
2523 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2524 GNUNET_SERVER_client_keep (client);
2525 fopc->client = client;
2527 fopc->type = OP_PEER_DESTROY;
2528 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2530 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2531 fopc->operation_id, &msg->header,
2532 &peer_destroy_success_cb,
2534 fopc->timeout_task =
2535 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2537 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2541 peer->destroy_flag = GNUNET_YES;
2542 if (0 == peer->reference_cnt)
2543 destroy_peer (peer);
2545 LOG (GNUNET_ERROR_TYPE_DEBUG,
2546 "Delaying peer destroy as peer is currently in use\n");
2547 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2548 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2553 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2556 * @param client identification of the client
2557 * @param message the actual message
2560 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2561 const struct GNUNET_MessageHeader *message)
2563 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2564 struct GNUNET_TESTBED_PeerEventMessage *reply;
2565 struct ForwardedOperationContext *fopc;
2569 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2570 peer_id = ntohl (msg->peer_id);
2571 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2574 LOG (GNUNET_ERROR_TYPE_ERROR,
2575 "Asked to start a non existent peer with id: %u\n", peer_id);
2576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2579 peer = peer_list[peer_id];
2580 if (GNUNET_YES == peer->is_remote)
2582 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2583 GNUNET_SERVER_client_keep (client);
2584 fopc->client = client;
2585 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2586 fopc->type = OP_PEER_START;
2588 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2589 fopc->operation_id, &msg->header,
2590 &forwarded_operation_reply_relay,
2592 fopc->timeout_task =
2593 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2595 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2596 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2599 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2601 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2603 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2606 peer->details.local.is_running = GNUNET_YES;
2607 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2608 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2609 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2610 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2611 reply->host_id = htonl (master_context->host_id);
2612 reply->peer_id = msg->peer_id;
2613 reply->operation_id = msg->operation_id;
2614 queue_message (client, &reply->header);
2615 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2620 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2623 * @param client identification of the client
2624 * @param message the actual message
2627 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2628 const struct GNUNET_MessageHeader *message)
2630 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2631 struct GNUNET_TESTBED_PeerEventMessage *reply;
2632 struct ForwardedOperationContext *fopc;
2636 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PEER_STOP msg\n");
2637 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2638 peer_id = ntohl (msg->peer_id);
2639 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2641 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2643 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2646 peer = peer_list[peer_id];
2647 if (GNUNET_YES == peer->is_remote)
2649 LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarding PEER_STOP msg\n");
2650 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2651 GNUNET_SERVER_client_keep (client);
2652 fopc->client = client;
2653 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2654 fopc->type = OP_PEER_STOP;
2656 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2657 fopc->operation_id, &msg->header,
2658 &forwarded_operation_reply_relay,
2660 fopc->timeout_task =
2661 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2663 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2664 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2667 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2669 LOG (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %u failed\n", peer_id);
2670 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2671 "Peer not running");
2672 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2675 LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %u successfully stopped\n", peer_id);
2676 peer->details.local.is_running = GNUNET_NO;
2677 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2678 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2679 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2680 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2681 reply->host_id = htonl (master_context->host_id);
2682 reply->peer_id = msg->peer_id;
2683 reply->operation_id = msg->operation_id;
2684 queue_message (client, &reply->header);
2685 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2690 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2693 * @param client identification of the client
2694 * @param message the actual message
2697 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2698 const struct GNUNET_MessageHeader *message)
2700 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2701 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2710 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2711 peer_id = ntohl (msg->peer_id);
2712 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2714 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2719 peer = peer_list[peer_id];
2720 if (GNUNET_YES == peer->is_remote)
2722 struct ForwardedOperationContext *fopc;
2724 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2725 GNUNET_SERVER_client_keep (client);
2726 fopc->client = client;
2727 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2728 fopc->type = OP_PEER_INFO;
2730 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2731 fopc->operation_id, &msg->header,
2732 &forwarded_operation_reply_relay,
2734 fopc->timeout_task =
2735 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2737 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2738 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2742 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2744 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2745 GNUNET_free (config);
2748 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2749 reply = GNUNET_realloc (xconfig, msize);
2750 (void) memmove (&reply[1], reply, xc_size);
2751 reply->header.size = htons (msize);
2752 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2753 reply->peer_id = msg->peer_id;
2754 reply->operation_id = msg->operation_id;
2755 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2756 &reply->peer_identity);
2757 reply->config_size = htons ((uint16_t) c_size);
2758 queue_message (client, &reply->header);
2759 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2764 * Cleanup overlay connect context structure
2766 * @param occ the overlay connect context
2769 cleanup_occ (struct OverlayConnectContext *occ)
2771 LOG_DEBUG ("Cleaning up occ\n");
2772 GNUNET_free_non_null (occ->emsg);
2773 GNUNET_free_non_null (occ->hello);
2774 GNUNET_SERVER_client_drop (occ->client);
2775 if (NULL != occ->opc)
2776 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2777 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2778 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2779 if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
2780 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
2781 if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
2782 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2783 if (NULL != occ->ch)
2785 GNUNET_CORE_disconnect (occ->ch);
2786 occ->peer->reference_cnt--;
2788 if (NULL != occ->ghh)
2789 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2790 if (NULL != occ->ohh)
2791 GNUNET_TRANSPORT_offer_hello_cancel (occ->ohh);
2792 if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
2793 GNUNET_SCHEDULER_cancel (occ->tcc.task);
2794 if (NULL != occ->tcc.tch)
2795 GNUNET_TRANSPORT_try_connect_cancel (occ->tcc.tch);
2796 if (NULL != occ->p1th)
2798 GNUNET_TRANSPORT_disconnect (occ->p1th);
2799 occ->peer->reference_cnt--;
2801 if (NULL != occ->tcc.th)
2803 GNUNET_TRANSPORT_disconnect (occ->tcc.th);
2804 peer_list[occ->other_peer_id]->reference_cnt--;
2806 if ((GNUNET_YES == occ->peer->destroy_flag)
2807 && (0 == occ->peer->reference_cnt))
2808 destroy_peer (occ->peer);
2809 if ((NULL == occ->peer2_controller)
2810 && (GNUNET_YES == peer_list[occ->other_peer_id]->destroy_flag)
2811 && (0 == peer_list[occ->other_peer_id]->reference_cnt))
2812 destroy_peer (peer_list[occ->other_peer_id]);
2813 GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
2819 * Task for cleaing up overlay connect context structure
2821 * @param cls the overlay connect context
2822 * @param tc the task context
2825 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2827 struct OverlayConnectContext *occ = cls;
2829 occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
2835 * Task which will be run when overlay connect request has been timed out
2837 * @param cls the OverlayConnectContext
2838 * @param tc the TaskContext
2841 timeout_overlay_connect (void *cls,
2842 const struct GNUNET_SCHEDULER_TaskContext *tc)
2844 struct OverlayConnectContext *occ = cls;
2846 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2847 LOG (GNUNET_ERROR_TYPE_WARNING,
2848 "Timeout while connecting peers %u and %u\n",
2849 occ->peer_id, occ->other_peer_id);
2850 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2857 * Function called to notify transport users that another
2858 * peer connected to us.
2860 * @param cls closure
2861 * @param new_peer the peer that connected
2862 * @param ats performance data
2863 * @param ats_count number of entries in ats (excluding 0-termination)
2866 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2867 const struct GNUNET_ATS_Information *ats,
2868 unsigned int ats_count)
2870 struct OverlayConnectContext *occ = cls;
2871 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2873 char *other_peer_str;
2875 LOG_DEBUG ("Overlay connect notify\n");
2877 memcmp (new_peer, &occ->peer_identity,
2878 sizeof (struct GNUNET_PeerIdentity)))
2880 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2881 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2883 memcmp (new_peer, &occ->other_peer_identity,
2884 sizeof (struct GNUNET_PeerIdentity)))
2886 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2887 new_peer_str, other_peer_str);
2888 GNUNET_free (new_peer_str);
2889 GNUNET_free (other_peer_str);
2892 GNUNET_free (new_peer_str);
2893 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2894 GNUNET_i2s (&occ->peer_identity));
2895 GNUNET_free (other_peer_str);
2896 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2898 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2899 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2901 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2902 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2903 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2904 if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
2906 GNUNET_SCHEDULER_cancel (occ->tcc.task);
2907 occ->tcc.task = GNUNET_SCHEDULER_NO_TASK;
2909 GNUNET_free_non_null (occ->emsg);
2911 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2912 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2914 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2915 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2916 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2917 msg->peer1 = htonl (occ->peer_id);
2918 msg->peer2 = htonl (occ->other_peer_id);
2919 msg->operation_id = GNUNET_htonll (occ->op_id);
2920 queue_message (occ->client, &msg->header);
2921 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
2922 //cleanup_occ (occ);
2927 * Task to ask transport of a peer to connect to another peer
2929 * @param cls the TryConnectContext
2930 * @param tc the scheduler task context
2933 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2937 * Callback to be called with result of the try connect request.
2939 * @param cls the overlay connect context
2940 * @param result GNUNET_OK if message was transmitted to transport service
2941 * GNUNET_SYSERR if message was not transmitted to transport service
2944 try_connect_cb (void *cls, const int result)
2946 struct TryConnectContext *tcc = cls;
2949 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == tcc->task);
2950 tcc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2951 (GNUNET_TIME_UNIT_MILLISECONDS,
2952 pow (10, ++tcc->retries)),
2953 &try_connect_task, tcc);
2958 * Task to ask transport of a peer to connect to another peer
2960 * @param cls the TryConnectContext
2961 * @param tc the scheduler task context
2964 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2966 struct TryConnectContext *tcc = cls;
2968 tcc->task = GNUNET_SCHEDULER_NO_TASK;
2969 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2971 GNUNET_assert (NULL == tcc->tch);
2972 GNUNET_assert (NULL != tcc->pid);
2973 GNUNET_assert (NULL != tcc->th);
2974 tcc->tch = GNUNET_TRANSPORT_try_connect (tcc->th, tcc->pid, &try_connect_cb, tcc);
2979 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2982 * @param cls the OverlayConnectContext
2983 * @param tc the TaskContext from scheduler
2986 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2990 * Task that is run when hello has been sent
2992 * @param cls the overlay connect context
2993 * @param tc the scheduler task context; if tc->reason =
2994 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
2995 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
2998 occ_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3000 struct OverlayConnectContext *occ = cls;
3003 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == occ->send_hello_task);
3004 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
3006 GNUNET_free_non_null (occ->emsg);
3007 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
3008 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
3011 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
3013 GNUNET_free_non_null (occ->emsg);
3014 occ->emsg = GNUNET_strdup ("Timeout while try connect\n");
3015 occ->tcc.pid = &occ->peer_identity;
3016 occ->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &occ->tcc);
3021 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
3024 * @param cls the OverlayConnectContext
3025 * @param tc the TaskContext from scheduler
3028 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3030 struct OverlayConnectContext *occ = cls;
3031 char *other_peer_str;
3033 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
3034 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3036 GNUNET_assert (NULL != occ->hello);
3037 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
3038 if (NULL != occ->peer2_controller)
3040 struct GNUNET_TESTBED_RequestConnectMessage *msg;
3042 uint16_t hello_size;
3044 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
3045 GNUNET_i2s (&occ->peer_identity), other_peer_str);
3046 hello_size = ntohs (occ->hello->size);
3047 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
3048 msg = GNUNET_malloc (msize);
3049 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
3050 msg->header.size = htons (msize);
3051 msg->peer = htonl (occ->other_peer_id);
3052 msg->operation_id = GNUNET_htonll (occ->op_id);
3053 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
3054 sizeof (struct GNUNET_PeerIdentity));
3055 memcpy (msg->hello, occ->hello, hello_size);
3056 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
3060 LOG_DEBUG ("Offering HELLO of %s to %s\n",
3061 GNUNET_i2s (&occ->peer_identity), other_peer_str);
3062 occ->ohh = GNUNET_TRANSPORT_offer_hello (occ->tcc.th,
3066 if (NULL == occ->ohh)
3069 occ->send_hello_task =
3070 GNUNET_SCHEDULER_add_delayed
3071 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3072 100 + GNUNET_CRYPTO_random_u32
3073 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
3077 GNUNET_free (other_peer_str);
3081 * Test for checking whether HELLO message is empty
3083 * @param cls empty flag to set
3084 * @param address the HELLO
3085 * @param expiration expiration of the HELLO
3089 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
3090 struct GNUNET_TIME_Absolute expiration)
3100 * Function called whenever there is an update to the HELLO of peers in the
3101 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
3102 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
3104 * @param cls closure
3105 * @param hello our updated HELLO
3108 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
3110 struct OverlayConnectContext *occ = cls;
3114 msize = ntohs (hello->size);
3116 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
3117 hello, GNUNET_NO, &test_address,
3119 if (GNUNET_YES == empty)
3121 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
3124 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
3125 occ->hello = GNUNET_malloc (msize);
3126 memcpy (occ->hello, hello, msize);
3127 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
3129 GNUNET_TRANSPORT_disconnect (occ->p1th);
3131 occ->peer->reference_cnt--;
3132 GNUNET_free_non_null (occ->emsg);
3133 if (NULL == occ->peer2_controller)
3135 peer_list[occ->other_peer_id]->reference_cnt++;
3137 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
3138 &occ->other_peer_identity, NULL, NULL, NULL,
3140 if (NULL == occ->tcc.th)
3142 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
3143 GNUNET_i2s (&occ->other_peer_identity));
3144 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3145 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3149 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
3150 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
3155 * Function called after GNUNET_CORE_connect has succeeded (or failed
3156 * for good). Note that the private key of the peer is intentionally
3157 * not exposed here; if you need it, your process should try to read
3158 * the private key file directly (which should work if you are
3161 * @param cls closure
3162 * @param server handle to the server, NULL if we failed
3163 * @param my_identity ID of this peer, NULL if we failed
3166 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
3167 const struct GNUNET_PeerIdentity *my_identity)
3169 struct OverlayConnectContext *occ = cls;
3171 GNUNET_free_non_null (occ->emsg);
3172 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
3173 if ((NULL == server) || (NULL == my_identity))
3175 GNUNET_free (occ->emsg);
3178 memcpy (&occ->peer_identity, my_identity,
3179 sizeof (struct GNUNET_PeerIdentity));
3180 occ->peer->reference_cnt++;
3182 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
3183 &occ->peer_identity, NULL, NULL, NULL, NULL);
3184 if (NULL == occ->p1th)
3186 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
3187 GNUNET_i2s (&occ->peer_identity));
3190 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
3191 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
3192 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
3196 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3197 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3203 * Callback to be called when forwarded get peer config operation as part of
3204 * overlay connect is successfull. Connection to Peer 1's core is made and is
3205 * checked for new connection from peer 2
3207 * @param cls ForwardedOperationContext
3208 * @param msg the peer create success message
3211 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
3213 struct OverlayConnectContext *occ = cls;
3214 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
3215 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3220 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
3222 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
3224 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
3225 sizeof (struct GNUNET_PeerIdentity));
3226 GNUNET_free_non_null (occ->emsg);
3227 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3228 occ->peer->reference_cnt++;
3230 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3231 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3232 GNUNET_NO, no_handlers);
3233 if (NULL == occ->ch)
3238 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3240 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3245 * Callback which will be called to after a host registration succeeded or failed
3247 * @param cls the RegisteredHostContext
3248 * @param emsg the error message; NULL if host registration is successful
3251 registeredhost_registration_completion (void *cls, const char *emsg)
3253 struct RegisteredHostContext *rhc = cls;
3254 struct GNUNET_CONFIGURATION_Handle *cfg;
3255 uint32_t peer2_host_id;
3257 /* if (NULL != rhc->focc_dll_head) */
3258 /* process_next_focc (rhc); */
3259 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
3260 GNUNET_assert (RHC_INIT == rhc->state);
3261 GNUNET_assert (NULL == rhc->sub_op);
3262 if ((NULL == rhc->gateway2)
3263 || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
3264 && (NULL != slave_list[peer2_host_id])))
3266 rhc->state = RHC_LINK;
3267 cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
3269 GNUNET_TESTBED_controller_link (rhc,
3270 rhc->gateway->controller,
3277 rhc->state = RHC_GET_CFG;
3278 rhc->sub_op = GNUNET_TESTBED_get_slave_config (rhc,
3279 rhc->gateway2->controller,
3285 * Iterator to match a registered host context
3287 * @param cls pointer 2 pointer of RegisteredHostContext
3288 * @param key current key code
3289 * @param value value in the hash map
3290 * @return GNUNET_YES if we should continue to
3295 reghost_match_iterator (void *cls,
3296 const struct GNUNET_HashCode * key,
3299 struct RegisteredHostContext **rh = cls;
3300 struct RegisteredHostContext *rh_val = value;
3302 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
3313 * Function to generate the hashcode corresponding to a RegisteredHostContext
3315 * @param reg_host the host which is being registered in RegisteredHostContext
3316 * @param host the host of the controller which has to connect to the above rhost
3317 * @return the hashcode
3319 static struct GNUNET_HashCode
3320 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
3321 struct GNUNET_TESTBED_Host *host)
3323 struct GNUNET_HashCode hash;
3324 uint32_t host_ids[2];
3326 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
3327 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
3328 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
3334 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
3337 * @param client identification of the client
3338 * @param message the actual message
3341 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
3342 const struct GNUNET_MessageHeader *message)
3344 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
3345 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3349 struct OverlayConnectContext *occ;
3350 struct GNUNET_TESTBED_Controller *peer2_controller;
3351 uint64_t operation_id;
3354 uint32_t peer2_host_id;
3356 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
3357 p1 = ntohl (msg->peer1);
3358 p2 = ntohl (msg->peer2);
3359 peer2_host_id = ntohl (msg->peer2_host_id);
3360 GNUNET_assert (p1 < peer_list_size);
3361 GNUNET_assert (NULL != peer_list[p1]);
3362 peer = peer_list[p1];
3363 operation_id = GNUNET_ntohll (msg->operation_id);
3364 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
3365 p1, p2, operation_id);
3366 if (GNUNET_YES == peer->is_remote)
3368 struct ForwardedOperationContext *fopc;
3369 struct Route *route_to_peer2_host;
3370 struct Route *route_to_peer1_host;
3372 LOG_DEBUG ("Forwarding overlay connect\n");
3373 route_to_peer2_host = NULL;
3374 route_to_peer1_host = NULL;
3375 route_to_peer2_host = find_dest_route (peer2_host_id);
3376 if ((NULL != route_to_peer2_host)
3377 || (peer2_host_id == master_context->host_id))
3379 /* Peer 2 either below us OR with us */
3380 route_to_peer1_host =
3381 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
3382 /* Because we get this message only if we know where peer 1 is */
3383 GNUNET_assert (NULL != route_to_peer1_host);
3384 if ((peer2_host_id == master_context->host_id)
3385 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
3387 /* Peer2 is either with us OR peer1 and peer2 can be reached through
3388 different gateways */
3389 struct GNUNET_HashCode hash;
3390 struct RegisteredHostContext *rhc;
3393 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
3394 if (NULL != route_to_peer2_host)
3395 rhc->reg_host = host_list[route_to_peer2_host->dest];
3397 rhc->reg_host = host_list[master_context->host_id];
3398 rhc->host = host_list[route_to_peer1_host->dest];
3399 GNUNET_assert (NULL != rhc->reg_host);
3400 GNUNET_assert (NULL != rhc->host);
3401 rhc->gateway = peer->details.remote.slave;
3402 rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
3403 slave_list[route_to_peer2_host->dest];
3404 rhc->state = RHC_INIT;
3405 GNUNET_SERVER_client_keep (client);
3406 rhc->client = client;
3407 hash = hash_hosts (rhc->reg_host, rhc->host);
3408 skip_focc = GNUNET_NO;
3410 GNUNET_CONTAINER_multihashmap_contains
3411 (peer->details.remote.slave->reghost_map, &hash))
3412 || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
3413 (peer->details.remote.slave->reghost_map, &hash,
3414 reghost_match_iterator, &rhc)))
3416 /* create and add a new registerd host context */
3417 /* add the focc to its queue */
3418 GNUNET_CONTAINER_multihashmap_put
3419 (peer->details.remote.slave->reghost_map,
3420 &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3421 GNUNET_assert (NULL != host_list[peer2_host_id]);
3422 queue_host_registration (peer->details.remote.slave,
3423 registeredhost_registration_completion, rhc,
3424 host_list[peer2_host_id]);
3427 /* rhc is now set to the existing one from the hash map by
3428 reghost_match_iterator() */
3429 /* if queue is empty then ignore creating focc and proceed with
3430 normal forwarding */
3431 if (RHC_OL_CONNECT == rhc->state)
3432 skip_focc = GNUNET_YES;
3434 if (GNUNET_NO == skip_focc)
3436 struct ForwardedOverlayConnectContext *focc;
3438 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
3441 focc->peer2_host_id = peer2_host_id;
3442 focc->orig_msg = GNUNET_copy_message (message);
3443 focc->operation_id = operation_id;
3444 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
3447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3452 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
3453 GNUNET_SERVER_client_keep (client);
3454 fopc->client = client;
3455 fopc->operation_id = operation_id;
3456 fopc->type = OP_OVERLAY_CONNECT;
3458 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
3459 operation_id, message,
3460 &forwarded_operation_reply_relay,
3462 fopc->timeout_task =
3463 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
3465 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
3466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3470 peer2_controller = NULL;
3471 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
3473 if ((peer2_host_id >= slave_list_size)
3474 || (NULL ==slave_list[peer2_host_id]))
3476 LOG (GNUNET_ERROR_TYPE_WARNING,
3477 "Configuration of peer2's controller missing for connecting peers"
3478 "%u and %u\n", p1, p2);
3479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3482 peer2_controller = slave_list[peer2_host_id]->controller;
3483 if (NULL == peer2_controller)
3485 GNUNET_break (0); /* What's going on? */
3486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3492 if (GNUNET_YES == peer_list[p2]->is_remote)
3493 peer2_controller = peer_list[p2]->details.remote.slave->controller;
3495 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
3496 GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
3497 GNUNET_SERVER_client_keep (client);
3498 occ->client = client;
3500 occ->other_peer_id = p2;
3501 occ->peer = peer_list[p1];
3502 occ->op_id = GNUNET_ntohll (msg->operation_id);
3503 occ->peer2_controller = peer2_controller;
3504 /* Get the identity of the second peer */
3505 if (NULL != occ->peer2_controller)
3507 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
3510 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
3511 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
3512 cmsg.peer_id = msg->peer2;
3513 cmsg.operation_id = msg->operation_id;
3515 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
3516 occ->op_id, &cmsg.header,
3517 &overlay_connect_get_config,
3520 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
3522 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3523 (GNUNET_TIME_UNIT_SECONDS, 30),
3524 &timeout_overlay_connect, occ);
3525 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3528 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
3529 &occ->other_peer_identity);
3530 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
3531 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3532 occ->peer->reference_cnt++;
3534 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3535 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3536 GNUNET_NO, no_handlers);
3537 if (NULL == occ->ch)
3539 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3542 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3543 (GNUNET_TIME_UNIT_SECONDS, 30),
3544 &timeout_overlay_connect, occ);
3545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3550 * Function to cleanup RequestOverlayConnectContext and any associated tasks
3553 * @param rocc the RequestOverlayConnectContext
3556 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
3558 LOG_DEBUG ("Cleaning up rocc\n");
3559 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
3560 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
3561 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
3562 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
3563 if (NULL != rocc->ohh)
3564 GNUNET_TRANSPORT_offer_hello_cancel (rocc->ohh);
3565 if (NULL != rocc->tcc.tch)
3566 GNUNET_TRANSPORT_try_connect_cancel (rocc->tcc.tch);
3567 if (GNUNET_SCHEDULER_NO_TASK != rocc->tcc.task)
3568 GNUNET_SCHEDULER_cancel (rocc->tcc.task);
3569 GNUNET_TRANSPORT_disconnect (rocc->tcc.th);
3570 rocc->peer->reference_cnt--;
3571 if ((GNUNET_YES == rocc->peer->destroy_flag)
3572 && (0 == rocc->peer->reference_cnt))
3573 destroy_peer (rocc->peer);
3574 GNUNET_free_non_null (rocc->hello);
3575 GNUNET_CONTAINER_DLL_remove (roccq_head, roccq_tail, rocc);
3581 * Task to timeout rocc and cleanit up
3583 * @param cls the RequestOverlayConnectContext
3584 * @param tc the TaskContext from scheduler
3587 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3589 struct RequestOverlayConnectContext *rocc = cls;
3591 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
3592 cleanup_rocc (rocc);
3597 * Function called to notify transport users that another
3598 * peer connected to us.
3600 * @param cls closure
3601 * @param new_peer the peer that connected
3602 * @param ats performance data
3603 * @param ats_count number of entries in ats (excluding 0-termination)
3606 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3607 const struct GNUNET_ATS_Information * ats,
3610 struct RequestOverlayConnectContext *rocc = cls;
3612 LOG_DEBUG ("Request Overlay connect notify\n");
3613 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3615 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3616 cleanup_rocc (rocc);
3621 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3622 * whose identity is in RequestOverlayConnectContext
3624 * @param cls the RequestOverlayConnectContext
3625 * @param tc the TaskContext from scheduler
3628 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3632 * Task that is run when hello has been sent
3634 * @param cls the overlay connect context
3635 * @param tc the scheduler task context; if tc->reason =
3636 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
3637 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
3640 rocc_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3642 struct RequestOverlayConnectContext *rocc = cls;
3645 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rocc->attempt_connect_task_id);
3646 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
3649 rocc->attempt_connect_task_id =
3650 GNUNET_SCHEDULER_add_now (&attempt_connect_task,
3654 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
3656 rocc->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &rocc->tcc);
3661 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3662 * whose identity is in RequestOverlayConnectContext
3664 * @param cls the RequestOverlayConnectContext
3665 * @param tc the TaskContext from scheduler
3668 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3670 struct RequestOverlayConnectContext *rocc = cls;
3672 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3673 rocc->ohh = GNUNET_TRANSPORT_offer_hello (rocc->tcc.th, rocc->hello,
3674 rocc_hello_sent_cb, rocc);
3675 if (NULL == rocc->ohh)
3676 rocc->attempt_connect_task_id =
3677 GNUNET_SCHEDULER_add_delayed
3678 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3679 100 + GNUNET_CRYPTO_random_u32
3680 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
3681 &attempt_connect_task, rocc);
3686 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3689 * @param client identification of the client
3690 * @param message the actual message
3693 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3694 const struct GNUNET_MessageHeader *message)
3696 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3697 struct RequestOverlayConnectContext *rocc;
3703 msize = ntohs (message->size);
3704 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3710 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3711 if ((NULL == msg->hello) ||
3712 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3718 hsize = ntohs (msg->hello->size);
3719 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3725 peer_id = ntohl (msg->peer);
3726 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3728 GNUNET_break_op (0);
3729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3732 if (GNUNET_YES == peer->is_remote)
3734 struct GNUNET_MessageHeader *msg2;
3736 msg2 = GNUNET_copy_message (message);
3737 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3738 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3741 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3742 GNUNET_CONTAINER_DLL_insert_tail (roccq_head, roccq_tail, rocc);
3744 rocc->peer->reference_cnt++;
3745 rocc->tcc.th = GNUNET_TRANSPORT_connect (rocc->peer->details.local.cfg, NULL, rocc,
3746 NULL, &transport_connect_notify, NULL);
3747 if (NULL == rocc->tcc.th)
3751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3754 memcpy (&rocc->a_id, &msg->peer_identity,
3755 sizeof (struct GNUNET_PeerIdentity));
3756 rocc->tcc.pid = &rocc->a_id;
3757 rocc->hello = GNUNET_malloc (hsize);
3758 memcpy (rocc->hello, msg->hello, hsize);
3759 rocc->attempt_connect_task_id =
3760 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3761 rocc->timeout_rocc_task_id =
3762 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3763 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3768 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3771 * @param client identification of the client
3772 * @param message the actual message
3775 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3776 const struct GNUNET_MessageHeader *message)
3778 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3779 struct Slave *slave;
3780 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3784 size_t xconfig_size;
3789 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3790 slave_id = ntohl (msg->slave_id);
3791 op_id = GNUNET_ntohll (msg->operation_id);
3792 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3794 /* FIXME: Add forwardings for this type of message here.. */
3795 send_operation_fail_msg (client, op_id, "Slave not found");
3796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3799 slave = slave_list[slave_id];
3800 if (NULL == slave->cfg)
3802 send_operation_fail_msg (client, op_id,
3803 "Configuration not found (slave not started by me)");
3804 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3807 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3808 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3810 GNUNET_free (config);
3811 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3812 GNUNET_break (reply_size <= UINT16_MAX);
3813 GNUNET_break (config_size <= UINT16_MAX);
3814 reply = GNUNET_realloc (xconfig, reply_size);
3815 (void) memmove (&reply[1], reply, xconfig_size);
3816 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3817 reply->header.size = htons ((uint16_t) reply_size);
3818 reply->slave_id = msg->slave_id;
3819 reply->operation_id = msg->operation_id;
3820 reply->config_size = htons ((uint16_t) config_size);
3821 queue_message (client, &reply->header);
3822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3827 * Iterator over hash map entries.
3829 * @param cls closure
3830 * @param key current key code
3831 * @param value value in the hash map
3832 * @return GNUNET_YES if we should continue to
3837 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3839 struct SharedService *ss = value;
3841 GNUNET_assert (GNUNET_YES ==
3842 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3843 GNUNET_free (ss->name);
3850 * Iterator for freeing hash map entries in a slave's reghost_map
3852 * @param cls handle to the slave
3853 * @param key current key code
3854 * @param value value in the hash map
3855 * @return GNUNET_YES if we should continue to
3860 reghost_free_iterator (void *cls,
3861 const struct GNUNET_HashCode * key,
3864 struct Slave *slave = cls;
3865 struct RegisteredHostContext *rhc = value;
3866 struct ForwardedOverlayConnectContext *focc;
3868 GNUNET_assert (GNUNET_YES ==
3869 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3871 while (NULL != (focc = rhc->focc_dll_head))
3873 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3876 cleanup_focc (focc);
3878 if (NULL != rhc->sub_op)
3879 GNUNET_TESTBED_operation_done (rhc->sub_op);
3880 if (NULL != rhc->client)
3881 GNUNET_SERVER_client_drop (rhc->client);
3882 GNUNET_free (value);
3888 * Task to clean up and shutdown nicely
3891 * @param tc the TaskContext from scheduler
3894 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3896 struct LCFContextQueue *lcfq;
3897 struct OverlayConnectContext *occ;
3898 struct RequestOverlayConnectContext *rocc;
3899 struct ForwardedOperationContext *fopc;
3900 struct MessageQueue *mq_entry;
3903 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3905 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3907 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3908 /* cleanup any remaining forwarded operations */
3909 while (NULL != (fopc = fopcq_head))
3911 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
3912 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
3913 if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
3914 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
3915 GNUNET_SERVER_client_drop (fopc->client);
3918 case OP_PEER_CREATE:
3919 GNUNET_free (fopc->cls);
3923 case OP_PEER_DESTROY:
3925 case OP_OVERLAY_CONNECT:
3926 case OP_LINK_CONTROLLERS:
3927 case OP_GET_SLAVE_CONFIG:
3934 if (NULL != lcfq_head)
3936 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3938 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3939 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3942 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3943 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3945 GNUNET_free (lcfq->lcf->msg);
3946 GNUNET_free (lcfq->lcf);
3947 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3950 while (NULL != (occ = occq_head))
3952 while (NULL != (rocc = roccq_head))
3953 cleanup_rocc (rocc);
3954 /* Clear peer list */
3955 for (id = 0; id < peer_list_size; id++)
3956 if (NULL != peer_list[id])
3958 /* If destroy flag is set it means that this peer should have been
3959 destroyed by a context which we destroy before */
3960 GNUNET_break (GNUNET_NO == peer_list[id]->destroy_flag);
3961 /* counter should be zero as we free all contexts before */
3962 GNUNET_break (0 == peer_list[id]->reference_cnt);
3963 if ( (GNUNET_NO == peer_list[id]->is_remote)
3964 && (GNUNET_YES == peer_list[id]->details.local.is_running))
3965 GNUNET_TESTING_peer_kill (peer_list[id]->details.local.peer);
3967 for (id = 0; id < peer_list_size; id++)
3968 if (NULL != peer_list[id])
3970 if (GNUNET_NO == peer_list[id]->is_remote)
3972 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3973 GNUNET_TESTING_peer_wait (peer_list[id]->details.local.peer);
3974 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3975 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3977 GNUNET_free (peer_list[id]);
3979 GNUNET_free_non_null (peer_list);
3980 /* Clear host list */
3981 for (id = 0; id < host_list_size; id++)
3982 if (NULL != host_list[id])
3983 GNUNET_TESTBED_host_destroy (host_list[id]);
3984 GNUNET_free_non_null (host_list);
3985 /* Clear route list */
3986 for (id = 0; id < route_list_size; id++)
3987 if (NULL != route_list[id])
3988 GNUNET_free (route_list[id]);
3989 GNUNET_free_non_null (route_list);
3990 /* Clear slave_list */
3991 for (id = 0; id < slave_list_size; id++)
3992 if (NULL != slave_list[id])
3994 struct HostRegistration *hr_entry;
3996 while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
3998 GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
3999 slave_list[id]->hr_dll_tail,
4001 GNUNET_free (hr_entry);
4003 if (NULL != slave_list[id]->rhandle)
4004 GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
4005 (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
4006 reghost_free_iterator,
4008 GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
4009 if (NULL != slave_list[id]->cfg)
4010 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
4011 if (NULL != slave_list[id]->controller)
4012 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
4013 if (NULL != slave_list[id]->controller_proc)
4014 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
4015 GNUNET_free (slave_list[id]);
4017 GNUNET_free_non_null (slave_list);
4018 if (NULL != master_context)
4020 GNUNET_free_non_null (master_context->master_ip);
4021 if (NULL != master_context->system)
4022 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
4023 GNUNET_SERVER_client_drop (master_context->client);
4024 GNUNET_free (master_context);
4025 master_context = NULL;
4027 if (NULL != transmit_handle)
4028 GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
4029 while (NULL != (mq_entry = mq_head))
4031 GNUNET_free (mq_entry->msg);
4032 GNUNET_SERVER_client_drop (mq_entry->client);
4033 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
4034 GNUNET_free (mq_entry);
4036 GNUNET_free_non_null (hostname);
4037 GNUNET_CONFIGURATION_destroy (our_config);
4042 * Callback for client disconnect
4045 * @param client the client which has disconnected
4048 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
4050 if (NULL == master_context)
4052 if (client == master_context->client)
4054 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
4055 /* should not be needed as we're terminated by failure to read
4056 * from stdin, but if stdin fails for some reason, this shouldn't
4057 * hurt for now --- might need to revise this later if we ever
4058 * decide that master connections might be temporarily down
4059 * for some reason */
4060 //GNUNET_SCHEDULER_shutdown ();
4068 * @param cls closure
4069 * @param server the initialized server
4070 * @param cfg configuration to use
4073 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
4074 const struct GNUNET_CONFIGURATION_Handle *cfg)
4076 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
4077 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
4078 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
4079 {&handle_configure_shared_service, NULL,
4080 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
4081 {&handle_link_controllers, NULL,
4082 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
4083 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
4084 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
4085 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
4086 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
4087 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
4088 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
4089 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
4090 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
4091 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
4092 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
4093 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
4094 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
4096 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
4097 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
4101 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
4102 (cfg, "testbed", "HOSTNAME", &hostname));
4103 our_config = GNUNET_CONFIGURATION_dup (cfg);
4104 GNUNET_SERVER_add_handlers (server, message_handlers);
4105 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
4106 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
4108 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
4109 GNUNET_SCHEDULER_PRIORITY_IDLE,
4110 &shutdown_task, NULL);
4111 LOG_DEBUG ("Testbed startup complete\n");
4112 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
4117 * The starting point of execution
4120 main (int argc, char *const *argv)
4122 //sleep (15); /* Debugging */
4123 return (GNUNET_OK ==
4124 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
4125 &testbed_run, NULL)) ? 0 : 1;
4128 /* end of gnunet-service-testbed.c */