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 * Transport handle of other peer to offer first peer's HELLO
563 struct GNUNET_TRANSPORT_Handle *p2th;
566 * Core handles of the first peer; used to notify when second peer connects to it
568 struct GNUNET_CORE_Handle *ch;
571 * HELLO of the other peer
573 struct GNUNET_MessageHeader *hello;
576 * Get hello handle to acquire HELLO of first peer
578 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
581 * The handle for offering HELLO
583 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
586 * The error message we send if this overlay connect operation has timed out
591 * Operation context for suboperations
593 struct OperationContext *opc;
596 * Controller of peer 2; NULL if the peer is local
598 struct GNUNET_TESTBED_Controller *peer2_controller;
601 * The transport try connect context
603 struct TryConnectContext tcc;
606 * The peer identity of the first peer
608 struct GNUNET_PeerIdentity peer_identity;
611 * The peer identity of the other peer
613 struct GNUNET_PeerIdentity other_peer_identity;
616 * The id of the operation responsible for creating this context
621 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
624 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
627 * The id of the overlay connect timeout task
629 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
632 * The id of the cleanup task
634 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
644 uint32_t other_peer_id;
650 * Context information for RequestOverlayConnect
651 * operations. RequestOverlayConnect is used when peers A, B reside on different
652 * hosts and the host controller for peer B is asked by the host controller of
653 * peer A to make peer B connect to peer A
655 struct RequestOverlayConnectContext
658 * the next pointer for DLL
660 struct RequestOverlayConnectContext *next;
663 * the prev pointer for DLL
665 struct RequestOverlayConnectContext *prev;
668 * The transport handle of peer B
670 struct GNUNET_TRANSPORT_Handle *th;
673 * The peer handle of peer B
680 struct GNUNET_MessageHeader *hello;
683 * The handle for offering HELLO
685 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh;
688 * The handle for transport try connect
690 struct GNUNET_TRANSPORT_TryConnectHandle *tch;
693 * The peer identity of peer A
695 struct GNUNET_PeerIdentity a_id;
698 * Task for offering HELLO of A to B and doing try_connect
700 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
703 * Task to timeout RequestOverlayConnect
705 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
711 * Context information for operations forwarded to subcontrollers
713 struct ForwardedOperationContext
716 * The next pointer for DLL
718 struct ForwardedOperationContext *next;
721 * The prev pointer for DLL
723 struct ForwardedOperationContext *prev;
726 * The generated operation context
728 struct OperationContext *opc;
731 * The client to which we have to reply
733 struct GNUNET_SERVER_Client *client;
741 * Task ID for the timeout task
743 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
746 * The id of the operation that has been forwarded
748 uint64_t operation_id;
751 * The type of the operation which is forwarded
753 enum OperationType type;
759 * Context information used while linking controllers
761 struct LinkControllersContext
764 * The client which initiated the link controller operation
766 struct GNUNET_SERVER_Client *client;
769 * The ID of the operation
771 uint64_t operation_id;
777 * Context information to used during operations which forward the overlay
780 struct ForwardedOverlayConnectContext
783 * next ForwardedOverlayConnectContext in the DLL
785 struct ForwardedOverlayConnectContext *next;
788 * previous ForwardedOverlayConnectContext in the DLL
790 struct ForwardedOverlayConnectContext *prev;
793 * A copy of the original overlay connect message
795 struct GNUNET_MessageHeader *orig_msg;
798 * The id of the operation which created this context information
800 uint64_t operation_id;
813 * Id of the host where peer2 is running
815 uint32_t peer2_host_id;
821 * The master context; generated with the first INIT message
823 static struct Context *master_context;
826 * Our hostname; we give this to all the peers we start
828 static char *hostname;
838 static struct GNUNET_CONFIGURATION_Handle *our_config;
841 * Current Transmit Handle; NULL if no notify transmit exists currently
843 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
850 * The head for the LCF queue
852 static struct LCFContextQueue *lcfq_head;
855 * The tail for the LCF queue
857 static struct LCFContextQueue *lcfq_tail;
860 * The message queue head
862 static struct MessageQueue *mq_head;
865 * The message queue tail
867 static struct MessageQueue *mq_tail;
870 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
872 static struct OverlayConnectContext *occq_head;
875 * DLL tail for OverlayConnectContext DLL
877 static struct OverlayConnectContext *occq_tail;
880 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
883 static struct RequestOverlayConnectContext *roccq_head;
886 * DLL tail for RequectOverlayConnectContext DLL
888 static struct RequestOverlayConnectContext *roccq_tail;
891 * DLL head for forwarded operation contexts
893 static struct ForwardedOperationContext *fopcq_head;
896 * DLL tail for forwarded operation contexts
898 static struct ForwardedOperationContext *fopcq_tail;
903 static struct GNUNET_TESTBED_Host **host_list;
908 static struct Route **route_list;
911 * A list of directly linked neighbours
913 static struct Slave **slave_list;
916 * A list of peers we know about
918 static struct Peer **peer_list;
921 * The hashmap of shared services
923 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
926 * The event mask for the events we listen from sub-controllers
928 static uint64_t event_mask;
931 * The size of the host list
933 static uint32_t host_list_size;
936 * The size of the route list
938 static uint32_t route_list_size;
941 * The size of directly linked neighbours list
943 static uint32_t slave_list_size;
946 * The size of the peer list
948 static uint32_t peer_list_size;
955 * The lcf_task handle
957 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
960 * The shutdown task handle
962 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
966 * Function called to notify a client about the connection begin ready to queue
967 * more data. "buf" will be NULL and "size" zero if the connection was closed
968 * for writing in the meantime.
971 * @param size number of bytes available in buf
972 * @param buf where the callee should write the message
973 * @return number of bytes written to buf
976 transmit_ready_notify (void *cls, size_t size, void *buf)
978 struct MessageQueue *mq_entry;
980 transmit_handle = NULL;
982 GNUNET_assert (NULL != mq_entry);
985 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
986 size = ntohs (mq_entry->msg->size);
987 memcpy (buf, mq_entry->msg, size);
988 GNUNET_free (mq_entry->msg);
989 GNUNET_SERVER_client_drop (mq_entry->client);
990 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
991 GNUNET_free (mq_entry);
993 if (NULL != mq_entry)
995 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
996 ntohs (mq_entry->msg->size),
997 GNUNET_TIME_UNIT_FOREVER_REL,
998 &transmit_ready_notify, NULL);
1004 * Queues a message in send queue for sending to the service
1006 * @param client the client to whom the queued message has to be sent
1007 * @param msg the message to queue
1010 queue_message (struct GNUNET_SERVER_Client *client,
1011 struct GNUNET_MessageHeader *msg)
1013 struct MessageQueue *mq_entry;
1017 type = ntohs (msg->type);
1018 size = ntohs (msg->size);
1019 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
1020 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
1021 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
1022 mq_entry->msg = msg;
1023 mq_entry->client = client;
1024 GNUNET_SERVER_client_keep (client);
1025 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
1027 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
1028 if (NULL == transmit_handle)
1030 GNUNET_SERVER_notify_transmit_ready (client, size,
1031 GNUNET_TIME_UNIT_FOREVER_REL,
1032 &transmit_ready_notify, NULL);
1037 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
1039 * @param ptr the memory block to realloc
1040 * @param size the size of ptr
1041 * @param new_size the size to which ptr has to be realloc'ed
1042 * @return the newly reallocated memory block
1045 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
1047 ptr = GNUNET_realloc (ptr, new_size);
1048 if (new_size > size)
1049 (void) memset (ptr + size, 0, new_size - size);
1055 * Function to add a host to the current list of known hosts
1057 * @param host the host to add
1058 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
1062 host_list_add (struct GNUNET_TESTBED_Host *host)
1067 host_id = GNUNET_TESTBED_host_get_id_ (host);
1068 orig_size = host_list_size;
1069 if (host_list_size <= host_id)
1071 while (host_list_size <= host_id)
1072 host_list_size += LIST_GROW_STEP;
1074 TESTBED_realloc (host_list,
1075 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
1076 sizeof (struct GNUNET_TESTBED_Host *)
1079 if (NULL != host_list[host_id])
1081 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
1082 return GNUNET_SYSERR;
1084 host_list[host_id] = host;
1090 * Adds a route to the route list
1092 * @param route the route to add
1095 route_list_add (struct Route *route)
1099 orig_size = route_list_size;
1100 if (route->dest >= route_list_size)
1102 while (route->dest >= route_list_size)
1103 route_list_size += LIST_GROW_STEP;
1105 TESTBED_realloc (route_list,
1106 sizeof (struct Route *) * orig_size,
1107 sizeof (struct Route *) * route_list_size);
1109 GNUNET_assert (NULL == route_list[route->dest]);
1110 route_list[route->dest] = route;
1115 * Adds a slave to the slave array
1117 * @param slave the slave controller to add
1120 slave_list_add (struct Slave *slave)
1124 orig_size = slave_list_size;
1125 if (slave->host_id >= slave_list_size)
1127 while (slave->host_id >= slave_list_size)
1128 slave_list_size += LIST_GROW_STEP;
1130 TESTBED_realloc (slave_list, sizeof (struct Slave *) * orig_size,
1131 sizeof (struct Slave *) * slave_list_size);
1133 GNUNET_assert (NULL == slave_list[slave->host_id]);
1134 slave_list[slave->host_id] = slave;
1139 * Adds a peer to the peer array
1141 * @param peer the peer to add
1144 peer_list_add (struct Peer *peer)
1148 orig_size = peer_list_size;
1149 if (peer->id >= peer_list_size)
1151 while (peer->id >= peer_list_size)
1152 peer_list_size += LIST_GROW_STEP;
1154 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
1155 sizeof (struct Peer *) * peer_list_size);
1157 GNUNET_assert (NULL == peer_list[peer->id]);
1158 peer_list[peer->id] = peer;
1163 * Removes a the give peer from the peer array
1165 * @param peer the peer to be removed
1168 peer_list_remove (struct Peer *peer)
1173 peer_list[peer->id] = NULL;
1174 orig_size = peer_list_size;
1175 while (peer_list_size >= LIST_GROW_STEP)
1177 for (id = peer_list_size - 1;
1178 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1179 if (NULL != peer_list[id])
1181 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1183 peer_list_size -= LIST_GROW_STEP;
1185 if (orig_size == peer_list_size)
1188 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1193 * Finds the route with directly connected host as destination through which
1194 * the destination host can be reached
1196 * @param host_id the id of the destination host
1197 * @return the route with directly connected destination host; NULL if no route
1200 static struct Route *
1201 find_dest_route (uint32_t host_id)
1203 struct Route *route;
1205 if (route_list_size <= host_id)
1207 while (NULL != (route = route_list[host_id]))
1209 if (route->thru == master_context->host_id)
1211 host_id = route->thru;
1218 * Routes message to a host given its host_id
1220 * @param host_id the id of the destination host
1221 * @param msg the message to be routed
1224 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1231 * Send operation failure message to client
1233 * @param client the client to which the failure message has to be sent to
1234 * @param operation_id the id of the failed operation
1235 * @param emsg the error message; can be NULL
1238 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1239 uint64_t operation_id, const char *emsg)
1241 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1245 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1246 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1248 msg = GNUNET_malloc (msize);
1249 msg->header.size = htons (msize);
1250 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1251 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1252 msg->operation_id = GNUNET_htonll (operation_id);
1254 memcpy (&msg[1], emsg, emsg_len);
1255 queue_message (client, &msg->header);
1260 * Function to send generic operation success message to given client
1262 * @param client the client to send the message to
1263 * @param operation_id the id of the operation which was successful
1266 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1267 uint64_t operation_id)
1269 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1272 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1273 msg = GNUNET_malloc (msize);
1274 msg->header.size = htons (msize);
1275 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1276 msg->operation_id = GNUNET_htonll (operation_id);
1277 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1278 queue_message (client, &msg->header);
1283 * Callback which will be called to after a host registration succeeded or failed
1285 * @param cls the handle to the slave at which the registration is completed
1286 * @param emsg the error message; NULL if host registration is successful
1289 hr_completion (void *cls, const char *emsg);
1293 * Attempts to register the next host in the host registration queue
1295 * @param slave the slave controller whose host registration queue is checked
1296 * for host registrations
1299 register_next_host (struct Slave *slave)
1301 struct HostRegistration *hr;
1303 hr = slave->hr_dll_head;
1304 GNUNET_assert (NULL != hr);
1305 GNUNET_assert (NULL == slave->rhandle);
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Registering host %u at %u\n",
1308 GNUNET_TESTBED_host_get_id_ (hr->host),
1309 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1310 slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
1318 * Callback which will be called to after a host registration succeeded or failed
1320 * @param cls the handle to the slave at which the registration is completed
1321 * @param emsg the error message; NULL if host registration is successful
1324 hr_completion (void *cls, const char *emsg)
1326 struct Slave *slave = cls;
1327 struct HostRegistration *hr;
1329 slave->rhandle = NULL;
1330 hr = slave->hr_dll_head;
1331 GNUNET_assert (NULL != hr);
1332 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 "Registering host %u at %u successful\n",
1334 GNUNET_TESTBED_host_get_id_ (hr->host),
1335 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1336 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
1340 hr->cb (hr->cb_cls, emsg);
1342 if (NULL != slave->hr_dll_head)
1343 register_next_host (slave);
1348 * Adds a host registration's request to a slave's registration queue
1350 * @param slave the slave controller at which the given host has to be
1352 * @param cb the host registration completion callback
1353 * @param cb_cls the closure for the host registration completion callback
1354 * @param host the host which has to be registered
1357 queue_host_registration (struct Slave *slave,
1358 GNUNET_TESTBED_HostRegistrationCompletion cb,
1360 struct GNUNET_TESTBED_Host *host)
1362 struct HostRegistration *hr;
1365 LOG (GNUNET_ERROR_TYPE_DEBUG,
1366 "Queueing host registration for host %u at %u\n",
1367 GNUNET_TESTBED_host_get_id_ (host),
1368 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1369 hr = GNUNET_malloc (sizeof (struct HostRegistration));
1371 hr->cb_cls = cb_cls;
1373 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
1374 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
1377 if (GNUNET_YES == call_register)
1378 register_next_host (slave);
1383 * The Link Controller forwarding task
1385 * @param cls the LCFContext
1386 * @param tc the Task context from scheduler
1389 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1393 * Completion callback for host registrations while forwarding Link Controller messages
1395 * @param cls the LCFContext
1396 * @param emsg the error message; NULL if host registration is successful
1399 lcf_proc_cc (void *cls, const char *emsg)
1401 struct LCFContext *lcf = cls;
1403 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1408 goto registration_error;
1409 lcf->state = DELEGATED_HOST_REGISTERED;
1410 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1412 case DELEGATED_HOST_REGISTERED:
1414 goto registration_error;
1415 lcf->state = SLAVE_HOST_REGISTERED;
1416 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1419 GNUNET_assert (0); /* Shouldn't reach here */
1424 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1426 lcf->state = FINISHED;
1427 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1432 * Callback to relay the reply msg of a forwarded operation back to the client
1434 * @param cls ForwardedOperationContext
1435 * @param msg the message to relay
1438 forwarded_operation_reply_relay (void *cls,
1439 const struct GNUNET_MessageHeader *msg)
1441 struct ForwardedOperationContext *fopc = cls;
1442 struct GNUNET_MessageHeader *dup_msg;
1445 msize = ntohs (msg->size);
1446 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1448 dup_msg = GNUNET_copy_message (msg);
1449 queue_message (fopc->client, dup_msg);
1450 GNUNET_SERVER_client_drop (fopc->client);
1451 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1452 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1458 * Task to free resources when forwarded operation has been timedout
1460 * @param cls the ForwardedOperationContext
1461 * @param tc the task context from scheduler
1464 forwarded_operation_timeout (void *cls,
1465 const struct GNUNET_SCHEDULER_TaskContext *tc)
1467 struct ForwardedOperationContext *fopc = cls;
1469 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1470 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1471 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1472 GNUNET_SERVER_client_drop (fopc->client);
1473 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1479 * The Link Controller forwarding task
1481 * @param cls the LCFContext
1482 * @param tc the Task context from scheduler
1485 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1489 * Callback to be called when forwarded link controllers operation is
1490 * successfull. We have to relay the reply msg back to the client
1492 * @param cls the LCFContext
1493 * @param msg the message to relay
1496 lcf_forwarded_operation_reply_relay (void *cls,
1497 const struct GNUNET_MessageHeader *msg)
1499 struct LCFContext *lcf = cls;
1501 GNUNET_assert (NULL != lcf->fopc);
1502 forwarded_operation_reply_relay (lcf->fopc, msg);
1504 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1505 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1510 * Task to free resources when forwarded link controllers has been timedout
1512 * @param cls the LCFContext
1513 * @param tc the task context from scheduler
1516 lcf_forwarded_operation_timeout (void *cls,
1517 const struct GNUNET_SCHEDULER_TaskContext *tc)
1519 struct LCFContext *lcf = cls;
1521 GNUNET_assert (NULL != lcf->fopc);
1522 lcf->fopc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1523 forwarded_operation_timeout (lcf->fopc, tc);
1525 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1526 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1531 * The Link Controller forwarding task
1533 * @param cls the LCFContext
1534 * @param tc the Task context from scheduler
1537 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1539 struct LCFContext *lcf = cls;
1540 struct LCFContextQueue *lcfq;
1542 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1547 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1548 lcf->gateway->controller))
1550 queue_host_registration (lcf->gateway,
1552 host_list[lcf->delegated_host_id]);
1556 lcf->state = DELEGATED_HOST_REGISTERED;
1557 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1560 case DELEGATED_HOST_REGISTERED:
1562 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1563 lcf->gateway->controller))
1565 queue_host_registration (lcf->gateway,
1567 host_list[lcf->slave_host_id]);
1571 lcf->state = SLAVE_HOST_REGISTERED;
1572 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1575 case SLAVE_HOST_REGISTERED:
1576 lcf->fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1577 lcf->fopc->client = lcf->client;
1578 lcf->fopc->operation_id = lcf->operation_id;
1579 lcf->fopc->type = OP_LINK_CONTROLLERS;
1581 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1584 &lcf_forwarded_operation_reply_relay,
1586 lcf->fopc->timeout_task =
1587 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &lcf_forwarded_operation_timeout,
1589 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, lcf->fopc);
1590 lcf->state = FINISHED;
1594 GNUNET_assert (lcfq->lcf == lcf);
1595 GNUNET_free (lcf->msg);
1597 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1599 if (NULL != lcfq_head)
1601 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1607 * Cleans up ForwardedOverlayConnectContext
1609 * @param focc the ForwardedOverlayConnectContext to cleanup
1612 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1614 GNUNET_free_non_null (focc->orig_msg);
1620 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1622 * @param rhc the RegisteredHostContext
1625 process_next_focc (struct RegisteredHostContext *rhc);
1629 * Timeout task for cancelling a forwarded overlay connect connect
1631 * @param cls the ForwardedOverlayConnectContext
1632 * @param tc the task context from the scheduler
1635 forwarded_overlay_connect_timeout (void *cls,
1636 const struct GNUNET_SCHEDULER_TaskContext
1639 struct ForwardedOperationContext *fopc = cls;
1640 struct RegisteredHostContext *rhc;
1641 struct ForwardedOverlayConnectContext *focc;
1644 focc = rhc->focc_dll_head;
1645 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1646 cleanup_focc (focc);
1647 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
1648 focc->peer1, focc->peer2);
1649 forwarded_operation_timeout (cls, tc);
1650 if (NULL != rhc->focc_dll_head)
1651 process_next_focc (rhc);
1656 * Callback to be called when forwarded overlay connection operation has a reply
1657 * from the sub-controller successfull. We have to relay the reply msg back to
1660 * @param cls ForwardedOperationContext
1661 * @param msg the peer create success message
1664 forwarded_overlay_connect_listener (void *cls,
1665 const struct GNUNET_MessageHeader *msg)
1667 struct ForwardedOperationContext *fopc = cls;
1668 struct RegisteredHostContext *rhc;
1669 struct ForwardedOverlayConnectContext *focc;
1672 forwarded_operation_reply_relay (cls, msg);
1673 focc = rhc->focc_dll_head;
1674 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1675 cleanup_focc (focc);
1676 if (NULL != rhc->focc_dll_head)
1677 process_next_focc (rhc);
1682 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1684 * @param rhc the RegisteredHostContext
1687 process_next_focc (struct RegisteredHostContext *rhc)
1689 struct ForwardedOperationContext *fopc;
1690 struct ForwardedOverlayConnectContext *focc;
1692 focc = rhc->focc_dll_head;
1693 GNUNET_assert (NULL != focc);
1694 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1695 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1696 GNUNET_SERVER_client_keep (rhc->client);
1697 fopc->client = rhc->client;
1698 fopc->operation_id = focc->operation_id;
1700 fopc->type = OP_OVERLAY_CONNECT;
1702 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1703 focc->operation_id, focc->orig_msg,
1704 &forwarded_overlay_connect_listener,
1706 GNUNET_free (focc->orig_msg);
1707 focc->orig_msg = NULL;
1708 fopc->timeout_task =
1709 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1711 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1716 * Callback for event from slave controllers
1718 * @param cls struct Slave *
1719 * @param event information about the event
1722 slave_event_callback (void *cls,
1723 const struct GNUNET_TESTBED_EventInformation *event)
1725 struct RegisteredHostContext *rhc;
1726 struct GNUNET_CONFIGURATION_Handle *cfg;
1727 struct GNUNET_TESTBED_Operation *old_op;
1729 /* We currently only get here when working on RegisteredHostContexts */
1730 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1731 rhc = event->details.operation_finished.op_cls;
1732 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1736 cfg = event->details.operation_finished.generic;
1737 old_op = rhc->sub_op;
1738 rhc->state = RHC_LINK;
1740 GNUNET_TESTBED_controller_link (rhc,
1741 rhc->gateway->controller,
1746 GNUNET_TESTBED_operation_done (old_op);
1749 LOG_DEBUG ("OL: Linking controllers successfull\n");
1750 GNUNET_TESTBED_operation_done (rhc->sub_op);
1752 rhc->state = RHC_OL_CONNECT;
1753 process_next_focc (rhc);
1762 * Callback to signal successfull startup of the controller process
1764 * @param cls the handle to the slave whose status is to be found here
1765 * @param cfg the configuration with which the controller has been started;
1766 * NULL if status is not GNUNET_OK
1767 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1768 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1771 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1774 struct Slave *slave = cls;
1775 struct LinkControllersContext *lcc;
1778 if (GNUNET_SYSERR == status)
1780 slave->controller_proc = NULL;
1781 slave_list[slave->host_id] = NULL;
1782 if (NULL != slave->cfg)
1783 GNUNET_CONFIGURATION_destroy (slave->cfg);
1784 GNUNET_free (slave);
1786 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1787 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1791 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1793 &slave_event_callback, slave);
1794 if (NULL != slave->controller)
1796 send_operation_success_msg (lcc->client, lcc->operation_id);
1797 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1801 send_operation_fail_msg (lcc->client, lcc->operation_id,
1802 "Could not connect to delegated controller");
1803 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1804 slave_list[slave->host_id] = NULL;
1805 GNUNET_free (slave);
1812 if (NULL != lcc->client)
1814 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1815 GNUNET_SERVER_client_drop (lcc->client);
1826 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1829 * @param client identification of the client
1830 * @param message the actual message
1833 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1834 const struct GNUNET_MessageHeader *message)
1836 const struct GNUNET_TESTBED_InitMessage *msg;
1837 struct GNUNET_TESTBED_Host *host;
1838 const char *controller_hostname;
1841 if (NULL != master_context)
1843 LOG_DEBUG ("We are being connected to laterally\n");
1844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1847 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1848 msize = ntohs (message->size);
1849 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1855 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1856 controller_hostname = (const char *) &msg[1];
1857 if ('\0' != controller_hostname[msize - 1])
1860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1863 master_context = GNUNET_malloc (sizeof (struct Context));
1864 GNUNET_SERVER_client_keep (client);
1865 master_context->client = client;
1866 master_context->host_id = ntohl (msg->host_id);
1867 master_context->master_ip = GNUNET_strdup (controller_hostname);
1868 LOG_DEBUG ("Our IP: %s\n", master_context->master_ip);
1869 master_context->system =
1870 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1872 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1873 master_context->master_ip,
1876 host_list_add (host);
1877 LOG_DEBUG ("Created master context with host ID: %u\n",
1878 master_context->host_id);
1879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1884 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1887 * @param client identification of the client
1888 * @param message the actual message
1891 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1892 const struct GNUNET_MessageHeader *message)
1894 struct GNUNET_TESTBED_Host *host;
1895 const struct GNUNET_TESTBED_AddHostMessage *msg;
1896 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1901 uint16_t username_length;
1902 uint16_t hostname_length;
1903 uint16_t reply_size;
1906 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1907 msize = ntohs (msg->header.size);
1908 username = (char *) &msg[1];
1909 username_length = ntohs (msg->user_name_length);
1910 if (0 != username_length)
1912 /* msg must contain hostname */
1913 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1914 username_length + 1));
1915 if (0 != username_length)
1916 GNUNET_assert ('\0' == username[username_length - 1]);
1917 hostname = username + username_length;
1919 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1920 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1921 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1922 host_id = ntohl (msg->host_id);
1923 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1924 LOG_DEBUG ("-------host id: %u\n", host_id);
1925 LOG_DEBUG ("-------hostname: %s\n", hostname);
1926 if (0 != username_length)
1927 LOG_DEBUG ("-------username: %s\n", username);
1930 LOG_DEBUG ("-------username: NULL\n");
1933 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1935 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1936 ntohs (msg->ssh_port));
1937 GNUNET_assert (NULL != host);
1938 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1939 if (GNUNET_OK != host_list_add (host))
1941 /* We are unable to add a host */
1942 emsg = "A host exists with given host-id";
1943 LOG_DEBUG ("%s: %u", emsg, host_id);
1944 GNUNET_TESTBED_host_destroy (host);
1945 reply_size += strlen (emsg) + 1;
1946 reply = GNUNET_malloc (reply_size);
1947 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1951 LOG_DEBUG ("Added host %u at %u\n",
1952 host_id, master_context->host_id);
1953 reply = GNUNET_malloc (reply_size);
1955 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1956 reply->header.size = htons (reply_size);
1957 reply->host_id = htonl (host_id);
1958 queue_message (client, &reply->header);
1959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1964 * Iterator over hash map entries.
1966 * @param cls closure
1967 * @param key current key code
1968 * @param value value in the hash map
1969 * @return GNUNET_YES if we should continue to
1974 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1976 struct SharedService *queried_ss = cls;
1977 struct SharedService *ss = value;
1979 if (0 == strcmp (ss->name, queried_ss->name))
1987 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1990 * @param client identification of the client
1991 * @param message the actual message
1994 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1995 const struct GNUNET_MessageHeader *message)
1997 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1998 struct SharedService *ss;
2000 struct GNUNET_HashCode hash;
2002 uint16_t service_name_size;
2004 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
2005 msg_size = ntohs (message->size);
2006 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
2009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2013 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
2014 service_name = (char *) &msg[1];
2015 if ('\0' != service_name[service_name_size - 1])
2018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2021 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
2022 service_name, ntohl (msg->num_peers));
2023 if (ntohl (msg->host_id) != master_context->host_id)
2025 route_message (ntohl (msg->host_id), message);
2026 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2029 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2030 ss = GNUNET_malloc (sizeof (struct SharedService));
2031 ss->name = strdup (service_name);
2032 ss->num_shared = ntohl (msg->num_peers);
2033 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
2034 if (GNUNET_SYSERR ==
2035 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
2036 &ss_exists_iterator, ss))
2038 LOG (GNUNET_ERROR_TYPE_WARNING,
2039 "Service %s already configured as a shared service. "
2040 "Ignoring service sharing request \n", ss->name);
2041 GNUNET_free (ss->name);
2045 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
2046 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2051 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
2054 * @param client identification of the client
2055 * @param message the actual message
2058 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
2059 const struct GNUNET_MessageHeader *message)
2061 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
2062 struct GNUNET_CONFIGURATION_Handle *cfg;
2063 struct LCFContextQueue *lcfq;
2064 struct Route *route;
2065 struct Route *new_route;
2069 uint32_t delegated_host_id;
2070 uint32_t slave_host_id;
2073 if (NULL == master_context)
2076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2079 msize = ntohs (message->size);
2080 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
2083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2086 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
2087 delegated_host_id = ntohl (msg->delegated_host_id);
2088 if (delegated_host_id == master_context->host_id)
2091 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
2092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2095 if ((delegated_host_id >= host_list_size) ||
2096 (NULL == host_list[delegated_host_id]))
2098 LOG (GNUNET_ERROR_TYPE_WARNING,
2099 "Delegated host %u not registered with us\n", delegated_host_id);
2100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2103 slave_host_id = ntohl (msg->slave_host_id);
2104 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
2106 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
2108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2111 if (slave_host_id == delegated_host_id)
2113 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
2114 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2118 if (slave_host_id == master_context->host_id) /* Link from us */
2120 struct Slave *slave;
2121 struct LinkControllersContext *lcc;
2123 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
2124 config_size = ntohs (msg->config_size);
2125 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
2127 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
2129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2132 config = GNUNET_malloc (config_size);
2133 dest_size = (uLongf) config_size;
2135 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
2138 GNUNET_break (0); /* Compression error */
2139 GNUNET_free (config);
2140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2143 if (config_size != dest_size)
2145 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
2146 GNUNET_free (config);
2147 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2150 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
2152 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2154 GNUNET_break (0); /* Configuration parsing error */
2155 GNUNET_free (config);
2156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2159 GNUNET_free (config);
2160 if ((delegated_host_id < slave_list_size) &&
2161 (NULL != slave_list[delegated_host_id]))
2163 GNUNET_break (0); /* Configuration parsing error */
2164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2167 slave = GNUNET_malloc (sizeof (struct Slave));
2168 slave->host_id = delegated_host_id;
2169 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
2170 slave_list_add (slave);
2171 if (1 != msg->is_subordinate)
2174 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
2176 &slave_event_callback, slave);
2178 if (NULL != slave->controller)
2179 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2181 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2182 "Could not connect to delegated controller");
2183 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2186 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
2187 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
2188 GNUNET_SERVER_client_keep (client);
2189 lcc->client = client;
2191 slave->controller_proc =
2192 GNUNET_TESTBED_controller_start (master_context->master_ip,
2193 host_list[slave->host_id], cfg,
2194 &slave_status_callback, slave);
2195 GNUNET_CONFIGURATION_destroy (cfg);
2196 new_route = GNUNET_malloc (sizeof (struct Route));
2197 new_route->dest = delegated_host_id;
2198 new_route->thru = master_context->host_id;
2199 route_list_add (new_route);
2203 /* Route the request */
2204 if (slave_host_id >= route_list_size)
2206 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
2207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2210 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
2211 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
2212 lcfq->lcf->delegated_host_id = delegated_host_id;
2213 lcfq->lcf->slave_host_id = slave_host_id;
2214 route = find_dest_route (slave_host_id);
2215 GNUNET_assert (NULL != route); /* because we add routes carefully */
2216 GNUNET_assert (route->dest < slave_list_size);
2217 GNUNET_assert (NULL != slave_list[route->dest]);
2218 lcfq->lcf->state = INIT;
2219 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
2220 lcfq->lcf->gateway = slave_list[route->dest];
2221 lcfq->lcf->msg = GNUNET_malloc (msize);
2222 (void) memcpy (lcfq->lcf->msg, msg, msize);
2223 GNUNET_SERVER_client_keep (client);
2224 lcfq->lcf->client = client;
2225 if (NULL == lcfq_head)
2227 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2228 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2229 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
2232 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2233 /* FIXME: Adding a new route should happen after the controllers are linked
2235 if (1 != msg->is_subordinate)
2237 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2240 if ((delegated_host_id < route_list_size)
2241 && (NULL != route_list[delegated_host_id]))
2243 GNUNET_break_op (0); /* Are you trying to link delegated host twice
2244 with is subordinate flag set to GNUNET_YES? */
2245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2248 new_route = GNUNET_malloc (sizeof (struct Route));
2249 new_route->dest = delegated_host_id;
2250 new_route->thru = route->dest;
2251 route_list_add (new_route);
2252 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2257 * The task to be executed if the forwarded peer create operation has been
2260 * @param cls the FowardedOperationContext
2261 * @param tc the TaskContext from the scheduler
2264 peer_create_forward_timeout (void *cls,
2265 const struct GNUNET_SCHEDULER_TaskContext *tc)
2267 struct ForwardedOperationContext *fopc = cls;
2269 GNUNET_free (fopc->cls);
2270 forwarded_operation_timeout (fopc, tc);
2275 * Callback to be called when forwarded peer create operation is successfull. We
2276 * have to relay the reply msg back to the client
2278 * @param cls ForwardedOperationContext
2279 * @param msg the peer create success message
2282 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2284 struct ForwardedOperationContext *fopc = cls;
2285 struct Peer *remote_peer;
2287 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
2289 GNUNET_assert (NULL != fopc->cls);
2290 remote_peer = fopc->cls;
2291 peer_list_add (remote_peer);
2293 forwarded_operation_reply_relay (fopc, msg);
2298 * Function to destroy a peer
2300 * @param peer the peer structure to destroy
2303 destroy_peer (struct Peer *peer)
2305 GNUNET_break (0 == peer->reference_cnt);
2306 if (GNUNET_YES == peer->is_remote)
2308 peer_list_remove (peer);
2312 if (GNUNET_YES == peer->details.local.is_running)
2314 GNUNET_TESTING_peer_stop (peer->details.local.peer);
2315 peer->details.local.is_running = GNUNET_NO;
2317 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2318 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2319 peer_list_remove (peer);
2325 * Callback to be called when forwarded peer destroy operation is successfull. We
2326 * have to relay the reply msg back to the client
2328 * @param cls ForwardedOperationContext
2329 * @param msg the peer create success message
2332 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2334 struct ForwardedOperationContext *fopc = cls;
2335 struct Peer *remote_peer;
2337 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS == ntohs (msg->type))
2339 remote_peer = fopc->cls;
2340 GNUNET_assert (NULL != remote_peer);
2341 remote_peer->destroy_flag = GNUNET_YES;
2342 if (0 == remote_peer->reference_cnt)
2343 destroy_peer (remote_peer);
2345 forwarded_operation_reply_relay (fopc, msg);
2351 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
2354 * @param client identification of the client
2355 * @param message the actual message
2358 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
2359 const struct GNUNET_MessageHeader *message)
2361 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
2362 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
2363 struct GNUNET_CONFIGURATION_Handle *cfg;
2364 struct ForwardedOperationContext *fo_ctxt;
2365 struct Route *route;
2370 uint32_t config_size;
2376 msize = ntohs (message->size);
2377 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
2379 GNUNET_break (0); /* We need configuration */
2380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2383 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
2384 host_id = ntohl (msg->host_id);
2385 peer_id = ntohl (msg->peer_id);
2386 if (UINT32_MAX == peer_id)
2388 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2389 "Cannot create peer with given ID");
2390 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2393 if (host_id == master_context->host_id)
2397 /* We are responsible for this peer */
2398 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
2399 config_size = ntohl (msg->config_size);
2400 config = GNUNET_malloc (config_size);
2401 dest_size = config_size;
2404 uncompress ((Bytef *) config, (uLongf *) & dest_size,
2405 (const Bytef *) &msg[1], (uLong) msize)))
2407 GNUNET_break (0); /* uncompression error */
2408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2411 if (config_size != dest_size)
2413 GNUNET_break (0); /* Uncompressed config size mismatch */
2414 GNUNET_free (config);
2415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2418 cfg = GNUNET_CONFIGURATION_create ();
2420 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2422 GNUNET_break (0); /* Configuration parsing error */
2423 GNUNET_free (config);
2424 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2427 GNUNET_free (config);
2428 peer = GNUNET_malloc (sizeof (struct Peer));
2429 peer->is_remote = GNUNET_NO;
2430 peer->details.local.cfg = cfg;
2432 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
2433 peer->details.local.peer =
2434 GNUNET_TESTING_peer_configure (master_context->system,
2435 peer->details.local.cfg, peer->id,
2436 NULL /* Peer id */ ,
2438 if (NULL == peer->details.local.peer)
2440 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2447 peer->details.local.is_running = GNUNET_NO;
2448 peer_list_add (peer);
2450 GNUNET_malloc (sizeof
2451 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2452 reply->header.size =
2453 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2454 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2455 reply->peer_id = msg->peer_id;
2456 reply->operation_id = msg->operation_id;
2457 queue_message (client, &reply->header);
2458 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2462 /* Forward peer create request */
2463 route = find_dest_route (host_id);
2467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2471 peer = GNUNET_malloc (sizeof (struct Peer));
2472 peer->is_remote = GNUNET_YES;
2474 peer->details.remote.slave = slave_list[route->dest];
2475 peer->details.remote.remote_host_id = host_id;
2476 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2477 GNUNET_SERVER_client_keep (client);
2478 fo_ctxt->client = client;
2479 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2480 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2481 fo_ctxt->type = OP_PEER_CREATE;
2483 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2484 fo_ctxt->operation_id,
2486 peer_create_success_cb, fo_ctxt);
2487 fo_ctxt->timeout_task =
2488 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2490 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
2491 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2496 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2499 * @param client identification of the client
2500 * @param message the actual message
2503 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2504 const struct GNUNET_MessageHeader *message)
2506 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2507 struct ForwardedOperationContext *fopc;
2511 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2512 peer_id = ntohl (msg->peer_id);
2513 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2514 peer_id, GNUNET_ntohll (msg->operation_id));
2515 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2517 LOG (GNUNET_ERROR_TYPE_ERROR,
2518 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2519 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2520 "Peer doesn't exist");
2521 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2524 peer = peer_list[peer_id];
2525 if (GNUNET_YES == peer->is_remote)
2527 /* Forward the destory message to sub controller */
2528 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2529 GNUNET_SERVER_client_keep (client);
2530 fopc->client = client;
2532 fopc->type = OP_PEER_DESTROY;
2533 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2535 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2536 fopc->operation_id, &msg->header,
2537 &peer_destroy_success_cb,
2539 fopc->timeout_task =
2540 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2542 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2546 peer->destroy_flag = GNUNET_YES;
2547 if (0 == peer->reference_cnt)
2548 destroy_peer (peer);
2550 LOG (GNUNET_ERROR_TYPE_WARNING,
2551 "Delaying peer destroy as peer is currently in use\n");
2552 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2553 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2558 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2561 * @param client identification of the client
2562 * @param message the actual message
2565 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2566 const struct GNUNET_MessageHeader *message)
2568 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2569 struct GNUNET_TESTBED_PeerEventMessage *reply;
2570 struct ForwardedOperationContext *fopc;
2574 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2575 peer_id = ntohl (msg->peer_id);
2576 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2579 LOG (GNUNET_ERROR_TYPE_ERROR,
2580 "Asked to start a non existent peer with id: %u\n", peer_id);
2581 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2584 peer = peer_list[peer_id];
2585 if (GNUNET_YES == peer->is_remote)
2587 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2588 GNUNET_SERVER_client_keep (client);
2589 fopc->client = client;
2590 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2591 fopc->type = OP_PEER_START;
2593 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2594 fopc->operation_id, &msg->header,
2595 &forwarded_operation_reply_relay,
2597 fopc->timeout_task =
2598 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2600 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2601 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2604 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2606 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2608 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2611 peer->details.local.is_running = GNUNET_YES;
2612 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2613 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2614 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2615 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2616 reply->host_id = htonl (master_context->host_id);
2617 reply->peer_id = msg->peer_id;
2618 reply->operation_id = msg->operation_id;
2619 queue_message (client, &reply->header);
2620 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2625 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2628 * @param client identification of the client
2629 * @param message the actual message
2632 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2633 const struct GNUNET_MessageHeader *message)
2635 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2636 struct GNUNET_TESTBED_PeerEventMessage *reply;
2637 struct ForwardedOperationContext *fopc;
2641 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2642 peer_id = ntohl (msg->peer_id);
2643 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2645 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2650 peer = peer_list[peer_id];
2651 if (GNUNET_YES == peer->is_remote)
2653 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2654 GNUNET_SERVER_client_keep (client);
2655 fopc->client = client;
2656 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2657 fopc->type = OP_PEER_STOP;
2659 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2660 fopc->operation_id, &msg->header,
2661 &forwarded_operation_reply_relay,
2663 fopc->timeout_task =
2664 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2666 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2667 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2670 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2672 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2673 "Peer not running");
2674 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2677 peer->details.local.is_running = GNUNET_NO;
2678 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2679 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2680 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2681 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2682 reply->host_id = htonl (master_context->host_id);
2683 reply->peer_id = msg->peer_id;
2684 reply->operation_id = msg->operation_id;
2685 queue_message (client, &reply->header);
2686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2691 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2694 * @param client identification of the client
2695 * @param message the actual message
2698 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2699 const struct GNUNET_MessageHeader *message)
2701 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2702 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2711 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2712 peer_id = ntohl (msg->peer_id);
2713 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2715 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2720 peer = peer_list[peer_id];
2721 if (GNUNET_YES == peer->is_remote)
2723 struct ForwardedOperationContext *fopc;
2725 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2726 GNUNET_SERVER_client_keep (client);
2727 fopc->client = client;
2728 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2729 fopc->type = OP_PEER_INFO;
2731 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2732 fopc->operation_id, &msg->header,
2733 &forwarded_operation_reply_relay,
2735 fopc->timeout_task =
2736 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2738 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2739 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2743 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2745 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2746 GNUNET_free (config);
2749 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2750 reply = GNUNET_realloc (xconfig, msize);
2751 (void) memmove (&reply[1], reply, xc_size);
2752 reply->header.size = htons (msize);
2753 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2754 reply->peer_id = msg->peer_id;
2755 reply->operation_id = msg->operation_id;
2756 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2757 &reply->peer_identity);
2758 reply->config_size = htons ((uint16_t) c_size);
2759 queue_message (client, &reply->header);
2760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2765 * Cleanup overlay connect context structure
2767 * @param occ the overlay connect context
2770 cleanup_occ (struct OverlayConnectContext *occ)
2772 LOG_DEBUG ("Cleaning up occ\n");
2773 GNUNET_free_non_null (occ->emsg);
2774 GNUNET_free_non_null (occ->hello);
2775 GNUNET_SERVER_client_drop (occ->client);
2776 if (NULL != occ->opc)
2777 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2778 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2779 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2780 if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
2781 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
2782 if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
2783 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2784 if (NULL != occ->ch)
2786 GNUNET_CORE_disconnect (occ->ch);
2787 occ->peer->reference_cnt--;
2789 if (NULL != occ->ghh)
2790 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2791 if (NULL != occ->ohh)
2792 GNUNET_TRANSPORT_offer_hello_cancel (occ->ohh);
2793 if (GNUNET_SCHEDULER_NO_TASK != occ->tcc.task)
2794 GNUNET_SCHEDULER_cancel (occ->tcc.task);
2795 if (NULL != occ->tcc.tch)
2796 GNUNET_TRANSPORT_try_connect_cancel (occ->tcc.tch);
2797 if (NULL != occ->p1th)
2799 GNUNET_TRANSPORT_disconnect (occ->p1th);
2800 occ->peer->reference_cnt--;
2802 if (NULL != occ->p2th)
2804 GNUNET_TRANSPORT_disconnect (occ->p2th);
2805 peer_list[occ->other_peer_id]->reference_cnt--;
2807 if ((GNUNET_YES == occ->peer->destroy_flag)
2808 && (0 == occ->peer->reference_cnt))
2809 destroy_peer (occ->peer);
2810 if ((NULL == occ->peer2_controller)
2811 && (GNUNET_YES == peer_list[occ->other_peer_id]->destroy_flag)
2812 && (0 == peer_list[occ->other_peer_id]->reference_cnt))
2813 destroy_peer (peer_list[occ->other_peer_id]);
2814 GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
2820 * Task for cleaing up overlay connect context structure
2822 * @param cls the overlay connect context
2823 * @param tc the task context
2826 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2828 struct OverlayConnectContext *occ = cls;
2830 occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
2836 * Task which will be run when overlay connect request has been timed out
2838 * @param cls the OverlayConnectContext
2839 * @param tc the TaskContext
2842 timeout_overlay_connect (void *cls,
2843 const struct GNUNET_SCHEDULER_TaskContext *tc)
2845 struct OverlayConnectContext *occ = cls;
2847 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2848 LOG (GNUNET_ERROR_TYPE_WARNING,
2849 "Timeout while connecting peers %u and %u\n",
2850 occ->peer_id, occ->other_peer_id);
2851 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2858 * Function called to notify transport users that another
2859 * peer connected to us.
2861 * @param cls closure
2862 * @param new_peer the peer that connected
2863 * @param ats performance data
2864 * @param ats_count number of entries in ats (excluding 0-termination)
2867 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2868 const struct GNUNET_ATS_Information *ats,
2869 unsigned int ats_count)
2871 struct OverlayConnectContext *occ = cls;
2872 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2874 char *other_peer_str;
2876 LOG_DEBUG ("Overlay connect notify\n");
2878 memcmp (new_peer, &occ->peer_identity,
2879 sizeof (struct GNUNET_PeerIdentity)))
2881 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2882 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2884 memcmp (new_peer, &occ->other_peer_identity,
2885 sizeof (struct GNUNET_PeerIdentity)))
2887 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2888 new_peer_str, other_peer_str);
2889 GNUNET_free (new_peer_str);
2890 GNUNET_free (other_peer_str);
2893 GNUNET_free (new_peer_str);
2894 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2895 GNUNET_i2s (&occ->peer_identity));
2896 GNUNET_free (other_peer_str);
2897 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2899 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2900 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2902 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2903 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2904 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2905 GNUNET_free_non_null (occ->emsg);
2907 if (NULL != occ->p2th)
2909 GNUNET_TRANSPORT_disconnect (occ->p2th);
2911 peer_list[occ->other_peer_id]->reference_cnt--;
2913 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2914 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2916 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2917 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2918 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2919 msg->peer1 = htonl (occ->peer_id);
2920 msg->peer2 = htonl (occ->other_peer_id);
2921 msg->operation_id = GNUNET_htonll (occ->op_id);
2922 queue_message (occ->client, &msg->header);
2923 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
2924 //cleanup_occ (occ);
2929 * Task to ask transport of a peer to connect to another peer
2931 * @param cls the TryConnectContext
2932 * @param tc the scheduler task context
2935 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2939 * Callback to be called with result of the try connect request.
2941 * @param cls the overlay connect context
2942 * @param result GNUNET_OK if message was transmitted to transport service
2943 * GNUNET_SYSERR if message was not transmitted to transport service
2946 try_connect_cb (void *cls, const int result)
2948 struct TryConnectContext *tcc = cls;
2951 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == tcc->task);
2952 tcc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2953 (GNUNET_TIME_UNIT_MILLISECONDS,
2954 pow (10, ++tcc->retries)),
2955 &try_connect_task, tcc);
2960 * Task to ask transport of a peer to connect to another peer
2962 * @param cls the TryConnectContext
2963 * @param tc the scheduler task context
2966 try_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2968 struct TryConnectContext *tcc = cls;
2970 tcc->task = GNUNET_SCHEDULER_NO_TASK;
2971 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2973 GNUNET_assert (NULL == tcc->tch);
2974 GNUNET_assert (NULL != tcc->pid);
2975 GNUNET_assert (NULL != tcc->th);
2976 tcc->tch = GNUNET_TRANSPORT_try_connect (tcc->th, tcc->pid, &try_connect_cb, tcc);
2981 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2984 * @param cls the OverlayConnectContext
2985 * @param tc the TaskContext from scheduler
2988 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2992 * Task that is run when hello has been sent
2994 * @param cls the overlay connect context
2995 * @param tc the scheduler task context; if tc->reason =
2996 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
2997 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
3000 occ_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3002 struct OverlayConnectContext *occ = cls;
3005 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == occ->send_hello_task);
3006 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
3008 GNUNET_free_non_null (occ->emsg);
3009 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
3010 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
3013 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
3015 GNUNET_free_non_null (occ->emsg);
3016 occ->emsg = GNUNET_strdup ("Timeout while try connect\n");
3017 occ->tcc.pid = &occ->peer_identity;
3018 occ->tcc.th = occ->p2th;
3019 occ->tcc.task = GNUNET_SCHEDULER_add_now (&try_connect_task, &occ->tcc);
3024 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
3027 * @param cls the OverlayConnectContext
3028 * @param tc the TaskContext from scheduler
3031 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3033 struct OverlayConnectContext *occ = cls;
3034 char *other_peer_str;
3036 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
3037 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3039 GNUNET_assert (NULL != occ->hello);
3040 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
3041 if (NULL != occ->peer2_controller)
3043 struct GNUNET_TESTBED_RequestConnectMessage *msg;
3045 uint16_t hello_size;
3047 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
3048 GNUNET_i2s (&occ->peer_identity), other_peer_str);
3049 hello_size = ntohs (occ->hello->size);
3050 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
3051 msg = GNUNET_malloc (msize);
3052 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
3053 msg->header.size = htons (msize);
3054 msg->peer = htonl (occ->other_peer_id);
3055 msg->operation_id = GNUNET_htonll (occ->op_id);
3056 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
3057 sizeof (struct GNUNET_PeerIdentity));
3058 memcpy (msg->hello, occ->hello, hello_size);
3059 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
3063 LOG_DEBUG ("Offering HELLO of %s to %s\n",
3064 GNUNET_i2s (&occ->peer_identity), other_peer_str);
3065 occ->ohh = GNUNET_TRANSPORT_offer_hello (occ->p2th,
3069 if (NULL == occ->ohh)
3072 occ->send_hello_task =
3073 GNUNET_SCHEDULER_add_delayed
3074 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3075 100 + GNUNET_CRYPTO_random_u32
3076 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
3080 GNUNET_free (other_peer_str);
3084 * Test for checking whether HELLO message is empty
3086 * @param cls empty flag to set
3087 * @param address the HELLO
3088 * @param expiration expiration of the HELLO
3092 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
3093 struct GNUNET_TIME_Absolute expiration)
3103 * Function called whenever there is an update to the HELLO of peers in the
3104 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
3105 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
3107 * @param cls closure
3108 * @param hello our updated HELLO
3111 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
3113 struct OverlayConnectContext *occ = cls;
3117 msize = ntohs (hello->size);
3119 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
3120 hello, GNUNET_NO, &test_address,
3122 if (GNUNET_YES == empty)
3124 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
3127 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
3128 occ->hello = GNUNET_malloc (msize);
3129 memcpy (occ->hello, hello, msize);
3130 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
3132 GNUNET_TRANSPORT_disconnect (occ->p1th);
3134 occ->peer->reference_cnt--;
3135 GNUNET_free_non_null (occ->emsg);
3136 if (NULL == occ->peer2_controller)
3138 peer_list[occ->other_peer_id]->reference_cnt++;
3140 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
3141 &occ->other_peer_identity, NULL, NULL, NULL,
3143 if (NULL == occ->p2th)
3145 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
3146 GNUNET_i2s (&occ->other_peer_identity));
3147 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3148 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3152 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
3153 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
3158 * Function called after GNUNET_CORE_connect has succeeded (or failed
3159 * for good). Note that the private key of the peer is intentionally
3160 * not exposed here; if you need it, your process should try to read
3161 * the private key file directly (which should work if you are
3164 * @param cls closure
3165 * @param server handle to the server, NULL if we failed
3166 * @param my_identity ID of this peer, NULL if we failed
3169 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
3170 const struct GNUNET_PeerIdentity *my_identity)
3172 struct OverlayConnectContext *occ = cls;
3174 GNUNET_free_non_null (occ->emsg);
3175 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
3176 if ((NULL == server) || (NULL == my_identity))
3178 GNUNET_free (occ->emsg);
3181 memcpy (&occ->peer_identity, my_identity,
3182 sizeof (struct GNUNET_PeerIdentity));
3183 occ->peer->reference_cnt++;
3185 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
3186 &occ->peer_identity, NULL, NULL, NULL, NULL);
3187 if (NULL == occ->p1th)
3189 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
3190 GNUNET_i2s (&occ->peer_identity));
3193 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
3194 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
3195 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
3199 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3200 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3206 * Callback to be called when forwarded get peer config operation as part of
3207 * overlay connect is successfull. Connection to Peer 1's core is made and is
3208 * checked for new connection from peer 2
3210 * @param cls ForwardedOperationContext
3211 * @param msg the peer create success message
3214 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
3216 struct OverlayConnectContext *occ = cls;
3217 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
3218 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3223 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
3225 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
3227 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
3228 sizeof (struct GNUNET_PeerIdentity));
3229 GNUNET_free_non_null (occ->emsg);
3230 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3231 occ->peer->reference_cnt++;
3233 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3234 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3235 GNUNET_NO, no_handlers);
3236 if (NULL == occ->ch)
3241 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3243 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3248 * Callback which will be called to after a host registration succeeded or failed
3250 * @param cls the RegisteredHostContext
3251 * @param emsg the error message; NULL if host registration is successful
3254 registeredhost_registration_completion (void *cls, const char *emsg)
3256 struct RegisteredHostContext *rhc = cls;
3257 struct GNUNET_CONFIGURATION_Handle *cfg;
3258 uint32_t peer2_host_id;
3260 /* if (NULL != rhc->focc_dll_head) */
3261 /* process_next_focc (rhc); */
3262 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
3263 GNUNET_assert (RHC_INIT == rhc->state);
3264 GNUNET_assert (NULL == rhc->sub_op);
3265 if ((NULL == rhc->gateway2)
3266 || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
3267 && (NULL != slave_list[peer2_host_id])))
3269 rhc->state = RHC_LINK;
3270 cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
3272 GNUNET_TESTBED_controller_link (rhc,
3273 rhc->gateway->controller,
3280 rhc->state = RHC_GET_CFG;
3281 rhc->sub_op = GNUNET_TESTBED_get_slave_config (rhc,
3282 rhc->gateway2->controller,
3288 * Iterator to match a registered host context
3290 * @param cls pointer 2 pointer of RegisteredHostContext
3291 * @param key current key code
3292 * @param value value in the hash map
3293 * @return GNUNET_YES if we should continue to
3298 reghost_match_iterator (void *cls,
3299 const struct GNUNET_HashCode * key,
3302 struct RegisteredHostContext **rh = cls;
3303 struct RegisteredHostContext *rh_val = value;
3305 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
3316 * Function to generate the hashcode corresponding to a RegisteredHostContext
3318 * @param reg_host the host which is being registered in RegisteredHostContext
3319 * @param host the host of the controller which has to connect to the above rhost
3320 * @return the hashcode
3322 static struct GNUNET_HashCode
3323 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
3324 struct GNUNET_TESTBED_Host *host)
3326 struct GNUNET_HashCode hash;
3327 uint32_t host_ids[2];
3329 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
3330 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
3331 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
3337 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
3340 * @param client identification of the client
3341 * @param message the actual message
3344 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
3345 const struct GNUNET_MessageHeader *message)
3347 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
3348 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3352 struct OverlayConnectContext *occ;
3353 struct GNUNET_TESTBED_Controller *peer2_controller;
3354 uint64_t operation_id;
3357 uint32_t peer2_host_id;
3359 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
3360 p1 = ntohl (msg->peer1);
3361 p2 = ntohl (msg->peer2);
3362 peer2_host_id = ntohl (msg->peer2_host_id);
3363 GNUNET_assert (p1 < peer_list_size);
3364 GNUNET_assert (NULL != peer_list[p1]);
3365 peer = peer_list[p1];
3366 operation_id = GNUNET_ntohll (msg->operation_id);
3367 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
3368 p1, p2, operation_id);
3369 if (GNUNET_YES == peer->is_remote)
3371 struct ForwardedOperationContext *fopc;
3372 struct Route *route_to_peer2_host;
3373 struct Route *route_to_peer1_host;
3375 LOG_DEBUG ("Forwarding overlay connect\n");
3376 route_to_peer2_host = NULL;
3377 route_to_peer1_host = NULL;
3378 route_to_peer2_host = find_dest_route (peer2_host_id);
3379 if ((NULL != route_to_peer2_host)
3380 || (peer2_host_id == master_context->host_id))
3382 /* Peer 2 either below us OR with us */
3383 route_to_peer1_host =
3384 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
3385 /* Because we get this message only if we know where peer 1 is */
3386 GNUNET_assert (NULL != route_to_peer1_host);
3387 if ((peer2_host_id == master_context->host_id)
3388 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
3390 /* Peer2 is either with us OR peer1 and peer2 can be reached through
3391 different gateways */
3392 struct GNUNET_HashCode hash;
3393 struct RegisteredHostContext *rhc;
3396 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
3397 if (NULL != route_to_peer2_host)
3398 rhc->reg_host = host_list[route_to_peer2_host->dest];
3400 rhc->reg_host = host_list[master_context->host_id];
3401 rhc->host = host_list[route_to_peer1_host->dest];
3402 GNUNET_assert (NULL != rhc->reg_host);
3403 GNUNET_assert (NULL != rhc->host);
3404 rhc->gateway = peer->details.remote.slave;
3405 rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
3406 slave_list[route_to_peer2_host->dest];
3407 rhc->state = RHC_INIT;
3408 GNUNET_SERVER_client_keep (client);
3409 rhc->client = client;
3410 hash = hash_hosts (rhc->reg_host, rhc->host);
3411 skip_focc = GNUNET_NO;
3413 GNUNET_CONTAINER_multihashmap_contains
3414 (peer->details.remote.slave->reghost_map, &hash))
3415 || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
3416 (peer->details.remote.slave->reghost_map, &hash,
3417 reghost_match_iterator, &rhc)))
3419 /* create and add a new registerd host context */
3420 /* add the focc to its queue */
3421 GNUNET_CONTAINER_multihashmap_put
3422 (peer->details.remote.slave->reghost_map,
3423 &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3424 GNUNET_assert (NULL != host_list[peer2_host_id]);
3425 queue_host_registration (peer->details.remote.slave,
3426 registeredhost_registration_completion, rhc,
3427 host_list[peer2_host_id]);
3430 /* rhc is now set to the existing one from the hash map by
3431 reghost_match_iterator() */
3432 /* if queue is empty then ignore creating focc and proceed with
3433 normal forwarding */
3434 if (RHC_OL_CONNECT == rhc->state)
3435 skip_focc = GNUNET_YES;
3437 if (GNUNET_NO == skip_focc)
3439 struct ForwardedOverlayConnectContext *focc;
3441 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
3444 focc->peer2_host_id = peer2_host_id;
3445 focc->orig_msg = GNUNET_copy_message (message);
3446 focc->operation_id = operation_id;
3447 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
3450 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3455 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
3456 GNUNET_SERVER_client_keep (client);
3457 fopc->client = client;
3458 fopc->operation_id = operation_id;
3459 fopc->type = OP_OVERLAY_CONNECT;
3461 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
3462 operation_id, message,
3463 &forwarded_operation_reply_relay,
3465 fopc->timeout_task =
3466 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
3468 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
3469 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3473 peer2_controller = NULL;
3474 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
3476 if ((peer2_host_id >= slave_list_size)
3477 || (NULL ==slave_list[peer2_host_id]))
3479 LOG (GNUNET_ERROR_TYPE_WARNING,
3480 "Configuration of peer2's controller missing for connecting peers"
3481 "%u and %u\n", p1, p2);
3482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3485 peer2_controller = slave_list[peer2_host_id]->controller;
3486 if (NULL == peer2_controller)
3488 GNUNET_break (0); /* What's going on? */
3489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3495 if (GNUNET_YES == peer_list[p2]->is_remote)
3496 peer2_controller = peer_list[p2]->details.remote.slave->controller;
3498 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
3499 GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
3500 GNUNET_SERVER_client_keep (client);
3501 occ->client = client;
3503 occ->other_peer_id = p2;
3504 occ->peer = peer_list[p1];
3505 occ->op_id = GNUNET_ntohll (msg->operation_id);
3506 occ->peer2_controller = peer2_controller;
3507 /* Get the identity of the second peer */
3508 if (NULL != occ->peer2_controller)
3510 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
3513 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
3514 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
3515 cmsg.peer_id = msg->peer2;
3516 cmsg.operation_id = msg->operation_id;
3518 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
3519 occ->op_id, &cmsg.header,
3520 &overlay_connect_get_config,
3523 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
3525 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3526 (GNUNET_TIME_UNIT_SECONDS, 30),
3527 &timeout_overlay_connect, occ);
3528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3531 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
3532 &occ->other_peer_identity);
3533 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
3534 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3535 occ->peer->reference_cnt++;
3537 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3538 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3539 GNUNET_NO, no_handlers);
3540 if (NULL == occ->ch)
3542 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3545 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3546 (GNUNET_TIME_UNIT_SECONDS, 30),
3547 &timeout_overlay_connect, occ);
3548 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3553 * Function to cleanup RequestOverlayConnectContext and any associated tasks
3556 * @param rocc the RequestOverlayConnectContext
3559 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
3561 LOG_DEBUG ("Cleaning up rocc\n");
3562 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
3563 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
3564 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
3565 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
3566 if (NULL != rocc->tch)
3567 GNUNET_TRANSPORT_try_connect_cancel (rocc->tch);
3568 if (NULL != rocc->ohh)
3569 GNUNET_TRANSPORT_offer_hello_cancel (rocc->ohh);
3570 GNUNET_TRANSPORT_disconnect (rocc->th);
3571 rocc->peer->reference_cnt--;
3572 if ((GNUNET_YES == rocc->peer->destroy_flag)
3573 && (0 == rocc->peer->reference_cnt))
3574 destroy_peer (rocc->peer);
3575 GNUNET_free_non_null (rocc->hello);
3576 GNUNET_CONTAINER_DLL_remove (roccq_head, roccq_tail, rocc);
3582 * Task to timeout rocc and cleanit up
3584 * @param cls the RequestOverlayConnectContext
3585 * @param tc the TaskContext from scheduler
3588 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3590 struct RequestOverlayConnectContext *rocc = cls;
3592 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
3593 cleanup_rocc (rocc);
3598 * Function called to notify transport users that another
3599 * peer connected to us.
3601 * @param cls closure
3602 * @param new_peer the peer that connected
3603 * @param ats performance data
3604 * @param ats_count number of entries in ats (excluding 0-termination)
3607 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3608 const struct GNUNET_ATS_Information * ats,
3611 struct RequestOverlayConnectContext *rocc = cls;
3613 LOG_DEBUG ("Request Overlay connect notify\n");
3614 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3616 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3617 cleanup_rocc (rocc);
3622 * Callback to be called with result of the try connect request.
3624 * @param cls the overlay connect context
3625 * @param result GNUNET_OK if message was transmitted to transport service
3626 * GNUNET_SYSERR if message was not transmitted to transport service
3629 rocc_try_connect_cb (void *cls, const int result)
3631 struct RequestOverlayConnectContext *rocc = cls;
3634 rocc->tch = GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id,
3635 &rocc_try_connect_cb, rocc);
3640 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3641 * whose identity is in RequestOverlayConnectContext
3643 * @param cls the RequestOverlayConnectContext
3644 * @param tc the TaskContext from scheduler
3647 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3651 * Task that is run when hello has been sent
3653 * @param cls the overlay connect context
3654 * @param tc the scheduler task context; if tc->reason =
3655 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
3656 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
3659 rocc_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3661 struct RequestOverlayConnectContext *rocc = cls;
3664 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == rocc->attempt_connect_task_id);
3665 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
3666 goto schedule_attempt_connect;
3667 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
3669 rocc->tch = GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id,
3670 &rocc_try_connect_cb, rocc);
3671 if (NULL != rocc->tch)
3675 schedule_attempt_connect:
3676 rocc->attempt_connect_task_id =
3677 GNUNET_SCHEDULER_add_now (&attempt_connect_task,
3683 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3684 * whose identity is in RequestOverlayConnectContext
3686 * @param cls the RequestOverlayConnectContext
3687 * @param tc the TaskContext from scheduler
3690 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3692 struct RequestOverlayConnectContext *rocc = cls;
3694 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3695 rocc->ohh = GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello,
3696 rocc_hello_sent_cb, rocc);
3697 if (NULL == rocc->ohh)
3698 rocc->attempt_connect_task_id =
3699 GNUNET_SCHEDULER_add_delayed
3700 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3701 100 + GNUNET_CRYPTO_random_u32
3702 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
3703 &attempt_connect_task, rocc);
3708 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3711 * @param client identification of the client
3712 * @param message the actual message
3715 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3716 const struct GNUNET_MessageHeader *message)
3718 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3719 struct RequestOverlayConnectContext *rocc;
3725 msize = ntohs (message->size);
3726 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3732 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3733 if ((NULL == msg->hello) ||
3734 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3740 hsize = ntohs (msg->hello->size);
3741 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3747 peer_id = ntohl (msg->peer);
3748 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3750 GNUNET_break_op (0);
3751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3754 if (GNUNET_YES == peer->is_remote)
3756 struct GNUNET_MessageHeader *msg2;
3758 msg2 = GNUNET_copy_message (message);
3759 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3763 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3764 GNUNET_CONTAINER_DLL_insert_tail (roccq_head, roccq_tail, rocc);
3766 rocc->peer->reference_cnt++;
3767 rocc->th = GNUNET_TRANSPORT_connect (rocc->peer->details.local.cfg, NULL, rocc,
3768 NULL, &transport_connect_notify, NULL);
3769 if (NULL == rocc->th)
3773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3776 memcpy (&rocc->a_id, &msg->peer_identity,
3777 sizeof (struct GNUNET_PeerIdentity));
3778 rocc->hello = GNUNET_malloc (hsize);
3779 memcpy (rocc->hello, msg->hello, hsize);
3780 rocc->attempt_connect_task_id =
3781 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3782 rocc->timeout_rocc_task_id =
3783 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3789 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3792 * @param client identification of the client
3793 * @param message the actual message
3796 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3797 const struct GNUNET_MessageHeader *message)
3799 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3800 struct Slave *slave;
3801 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3805 size_t xconfig_size;
3810 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3811 slave_id = ntohl (msg->slave_id);
3812 op_id = GNUNET_ntohll (msg->operation_id);
3813 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3815 /* FIXME: Add forwardings for this type of message here.. */
3816 send_operation_fail_msg (client, op_id, "Slave not found");
3817 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3820 slave = slave_list[slave_id];
3821 if (NULL == slave->cfg)
3823 send_operation_fail_msg (client, op_id,
3824 "Configuration not found (slave not started by me)");
3825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3828 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3829 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3831 GNUNET_free (config);
3832 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3833 GNUNET_break (reply_size <= UINT16_MAX);
3834 GNUNET_break (config_size <= UINT16_MAX);
3835 reply = GNUNET_realloc (xconfig, reply_size);
3836 (void) memmove (&reply[1], reply, xconfig_size);
3837 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3838 reply->header.size = htons ((uint16_t) reply_size);
3839 reply->slave_id = msg->slave_id;
3840 reply->operation_id = msg->operation_id;
3841 reply->config_size = htons ((uint16_t) config_size);
3842 queue_message (client, &reply->header);
3843 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3848 * Iterator over hash map entries.
3850 * @param cls closure
3851 * @param key current key code
3852 * @param value value in the hash map
3853 * @return GNUNET_YES if we should continue to
3858 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3860 struct SharedService *ss = value;
3862 GNUNET_assert (GNUNET_YES ==
3863 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3864 GNUNET_free (ss->name);
3871 * Iterator for freeing hash map entries in a slave's reghost_map
3873 * @param cls handle to the slave
3874 * @param key current key code
3875 * @param value value in the hash map
3876 * @return GNUNET_YES if we should continue to
3881 reghost_free_iterator (void *cls,
3882 const struct GNUNET_HashCode * key,
3885 struct Slave *slave = cls;
3886 struct RegisteredHostContext *rhc = value;
3887 struct ForwardedOverlayConnectContext *focc;
3889 GNUNET_assert (GNUNET_YES ==
3890 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3892 while (NULL != (focc = rhc->focc_dll_head))
3894 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3897 cleanup_focc (focc);
3899 if (NULL != rhc->sub_op)
3900 GNUNET_TESTBED_operation_done (rhc->sub_op);
3901 if (NULL != rhc->client)
3902 GNUNET_SERVER_client_drop (rhc->client);
3903 GNUNET_free (value);
3909 * Task to clean up and shutdown nicely
3912 * @param tc the TaskContext from scheduler
3915 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3917 struct LCFContextQueue *lcfq;
3918 struct OverlayConnectContext *occ;
3919 struct RequestOverlayConnectContext *rocc;
3920 struct ForwardedOperationContext *fopc;
3921 struct MessageQueue *mq_entry;
3924 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3925 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3926 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3928 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3929 /* cleanup any remaining forwarded operations */
3930 while (NULL != (fopc = fopcq_head))
3932 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
3933 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
3934 if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
3935 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
3936 GNUNET_SERVER_client_drop (fopc->client);
3939 case OP_PEER_CREATE:
3940 GNUNET_free (fopc->cls);
3944 case OP_PEER_DESTROY:
3946 case OP_OVERLAY_CONNECT:
3947 case OP_LINK_CONTROLLERS:
3948 case OP_GET_SLAVE_CONFIG:
3955 if (NULL != lcfq_head)
3957 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3959 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3960 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3963 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3964 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3966 GNUNET_free (lcfq->lcf->msg);
3967 GNUNET_free (lcfq->lcf);
3968 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3971 while (NULL != (occ = occq_head))
3973 while (NULL != (rocc = roccq_head))
3974 cleanup_rocc (rocc);
3975 /* Clear peer list */
3976 for (id = 0; id < peer_list_size; id++)
3977 if (NULL != peer_list[id])
3979 /* If destroy flag is set it means that this peer should have been
3980 destroyed by a context which we destroy before */
3981 GNUNET_break (GNUNET_NO == peer_list[id]->destroy_flag);
3982 /* counter should be zero as we free all contexts before */
3983 GNUNET_break (0 == peer_list[id]->reference_cnt);
3984 if (GNUNET_NO == peer_list[id]->is_remote)
3986 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3987 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
3988 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3989 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3991 GNUNET_free (peer_list[id]);
3993 GNUNET_free_non_null (peer_list);
3994 /* Clear host list */
3995 for (id = 0; id < host_list_size; id++)
3996 if (NULL != host_list[id])
3997 GNUNET_TESTBED_host_destroy (host_list[id]);
3998 GNUNET_free_non_null (host_list);
3999 /* Clear route list */
4000 for (id = 0; id < route_list_size; id++)
4001 if (NULL != route_list[id])
4002 GNUNET_free (route_list[id]);
4003 GNUNET_free_non_null (route_list);
4004 /* Clear slave_list */
4005 for (id = 0; id < slave_list_size; id++)
4006 if (NULL != slave_list[id])
4008 struct HostRegistration *hr_entry;
4010 while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
4012 GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
4013 slave_list[id]->hr_dll_tail,
4015 GNUNET_free (hr_entry);
4017 if (NULL != slave_list[id]->rhandle)
4018 GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
4019 (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
4020 reghost_free_iterator,
4022 GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
4023 if (NULL != slave_list[id]->cfg)
4024 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
4025 if (NULL != slave_list[id]->controller)
4026 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
4027 if (NULL != slave_list[id]->controller_proc)
4028 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
4029 GNUNET_free (slave_list[id]);
4031 GNUNET_free_non_null (slave_list);
4032 if (NULL != master_context)
4034 GNUNET_free_non_null (master_context->master_ip);
4035 if (NULL != master_context->system)
4036 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
4037 GNUNET_SERVER_client_drop (master_context->client);
4038 GNUNET_free (master_context);
4039 master_context = NULL;
4041 if (NULL != transmit_handle)
4042 GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
4043 while (NULL != (mq_entry = mq_head))
4045 GNUNET_free (mq_entry->msg);
4046 GNUNET_SERVER_client_drop (mq_entry->client);
4047 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
4048 GNUNET_free (mq_entry);
4050 GNUNET_free_non_null (hostname);
4051 GNUNET_CONFIGURATION_destroy (our_config);
4056 * Callback for client disconnect
4059 * @param client the client which has disconnected
4062 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
4064 if (NULL == master_context)
4066 if (client == master_context->client)
4068 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
4069 /* should not be needed as we're terminated by failure to read
4070 * from stdin, but if stdin fails for some reason, this shouldn't
4071 * hurt for now --- might need to revise this later if we ever
4072 * decide that master connections might be temporarily down
4073 * for some reason */
4074 //GNUNET_SCHEDULER_shutdown ();
4082 * @param cls closure
4083 * @param server the initialized server
4084 * @param cfg configuration to use
4087 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
4088 const struct GNUNET_CONFIGURATION_Handle *cfg)
4090 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
4091 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
4092 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
4093 {&handle_configure_shared_service, NULL,
4094 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
4095 {&handle_link_controllers, NULL,
4096 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
4097 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
4098 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
4099 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
4100 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
4101 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
4102 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
4103 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
4104 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
4105 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
4106 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
4107 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
4108 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
4110 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
4111 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
4115 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
4116 (cfg, "testbed", "HOSTNAME", &hostname));
4117 our_config = GNUNET_CONFIGURATION_dup (cfg);
4118 GNUNET_SERVER_add_handlers (server, message_handlers);
4119 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
4120 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
4122 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
4123 GNUNET_SCHEDULER_PRIORITY_IDLE,
4124 &shutdown_task, NULL);
4125 LOG_DEBUG ("Testbed startup complete\n");
4126 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
4131 * The starting point of execution
4134 main (int argc, char *const *argv)
4136 //sleep (15); /* Debugging */
4137 return (GNUNET_OK ==
4138 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
4139 &testbed_run, NULL)) ? 0 : 1;
4142 /* end of gnunet-service-testbed.c */