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 connecting 2 peers in overlay
500 struct OverlayConnectContext
503 * The next pointer for maintaining a DLL
505 struct OverlayConnectContext *next;
508 * The prev pointer for maintaining a DLL
510 struct OverlayConnectContext *prev;
513 * The client which has requested for overlay connection
515 struct GNUNET_SERVER_Client *client;
518 * the peer which has to connect to the other peer
523 * Transport handle of the first peer to get its HELLO
525 struct GNUNET_TRANSPORT_Handle *p1th;
528 * Transport handle of other peer to offer first peer's HELLO
530 struct GNUNET_TRANSPORT_Handle *p2th;
533 * Core handles of the first peer; used to notify when second peer connects to it
535 struct GNUNET_CORE_Handle *ch;
538 * HELLO of the other peer
540 struct GNUNET_MessageHeader *hello;
543 * Get hello handle to acquire HELLO of first peer
545 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
548 * The error message we send if this overlay connect operation has timed out
553 * Operation context for suboperations
555 struct OperationContext *opc;
558 * Controller of peer 2; NULL if the peer is local
560 struct GNUNET_TESTBED_Controller *peer2_controller;
563 * The peer identity of the first peer
565 struct GNUNET_PeerIdentity peer_identity;
568 * The peer identity of the other peer
570 struct GNUNET_PeerIdentity other_peer_identity;
573 * The id of the operation responsible for creating this context
578 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
581 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
584 * The id of the overlay connect timeout task
586 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
589 * The id of the cleanup task
591 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
601 uint32_t other_peer_id;
607 * Context information for RequestOverlayConnect
608 * operations. RequestOverlayConnect is used when peers A, B reside on different
609 * hosts and the host controller for peer B is asked by the host controller of
610 * peer A to make peer B connect to peer A
612 struct RequestOverlayConnectContext
615 * the next pointer for DLL
617 struct RequestOverlayConnectContext *next;
620 * the prev pointer for DLL
622 struct RequestOverlayConnectContext *prev;
625 * The transport handle of peer B
627 struct GNUNET_TRANSPORT_Handle *th;
632 struct GNUNET_MessageHeader *hello;
635 * The peer identity of peer A
637 struct GNUNET_PeerIdentity a_id;
640 * Task for offering HELLO of A to B and doing try_connect
642 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
645 * Task to timeout RequestOverlayConnect
647 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
653 * Context information for operations forwarded to subcontrollers
655 struct ForwardedOperationContext
658 * The next pointer for DLL
660 struct ForwardedOperationContext *next;
663 * The prev pointer for DLL
665 struct ForwardedOperationContext *prev;
668 * The generated operation context
670 struct OperationContext *opc;
673 * The client to which we have to reply
675 struct GNUNET_SERVER_Client *client;
683 * Task ID for the timeout task
685 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
688 * The id of the operation that has been forwarded
690 uint64_t operation_id;
693 * The type of the operation which is forwarded
695 enum OperationType type;
701 * Context information used while linking controllers
703 struct LinkControllersContext
706 * The client which initiated the link controller operation
708 struct GNUNET_SERVER_Client *client;
711 * The ID of the operation
713 uint64_t operation_id;
719 * Context information to used during operations which forward the overlay
722 struct ForwardedOverlayConnectContext
725 * next ForwardedOverlayConnectContext in the DLL
727 struct ForwardedOverlayConnectContext *next;
730 * previous ForwardedOverlayConnectContext in the DLL
732 struct ForwardedOverlayConnectContext *prev;
735 * A copy of the original overlay connect message
737 struct GNUNET_MessageHeader *orig_msg;
740 * The id of the operation which created this context information
742 uint64_t operation_id;
755 * Id of the host where peer2 is running
757 uint32_t peer2_host_id;
763 * The master context; generated with the first INIT message
765 static struct Context *master_context;
768 * Our hostname; we give this to all the peers we start
770 static char *hostname;
780 static struct GNUNET_CONFIGURATION_Handle *our_config;
783 * Current Transmit Handle; NULL if no notify transmit exists currently
785 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
792 * The head for the LCF queue
794 static struct LCFContextQueue *lcfq_head;
797 * The tail for the LCF queue
799 static struct LCFContextQueue *lcfq_tail;
802 * The message queue head
804 static struct MessageQueue *mq_head;
807 * The message queue tail
809 static struct MessageQueue *mq_tail;
812 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
814 static struct OverlayConnectContext *occq_head;
817 * DLL tail for OverlayConnectContext DLL
819 static struct OverlayConnectContext *occq_tail;
822 * DLL head for RequectOverlayConnectContext DLL - to be used to clean up during
825 static struct RequestOverlayConnectContext *roccq_head;
828 * DLL tail for RequectOverlayConnectContext DLL
830 static struct RequestOverlayConnectContext *roccq_tail;
833 * DLL head for forwarded operation contexts
835 static struct ForwardedOperationContext *fopcq_head;
838 * DLL tail for forwarded operation contexts
840 static struct ForwardedOperationContext *fopcq_tail;
845 static struct GNUNET_TESTBED_Host **host_list;
850 static struct Route **route_list;
853 * A list of directly linked neighbours
855 static struct Slave **slave_list;
858 * A list of peers we know about
860 static struct Peer **peer_list;
863 * The hashmap of shared services
865 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
868 * The event mask for the events we listen from sub-controllers
870 static uint64_t event_mask;
873 * The size of the host list
875 static uint32_t host_list_size;
878 * The size of the route list
880 static uint32_t route_list_size;
883 * The size of directly linked neighbours list
885 static uint32_t slave_list_size;
888 * The size of the peer list
890 static uint32_t peer_list_size;
897 * The lcf_task handle
899 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
902 * The shutdown task handle
904 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
908 * Function called to notify a client about the connection begin ready to queue
909 * more data. "buf" will be NULL and "size" zero if the connection was closed
910 * for writing in the meantime.
913 * @param size number of bytes available in buf
914 * @param buf where the callee should write the message
915 * @return number of bytes written to buf
918 transmit_ready_notify (void *cls, size_t size, void *buf)
920 struct MessageQueue *mq_entry;
922 transmit_handle = NULL;
924 GNUNET_assert (NULL != mq_entry);
927 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
928 size = ntohs (mq_entry->msg->size);
929 memcpy (buf, mq_entry->msg, size);
930 GNUNET_free (mq_entry->msg);
931 GNUNET_SERVER_client_drop (mq_entry->client);
932 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
933 GNUNET_free (mq_entry);
935 if (NULL != mq_entry)
937 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
938 ntohs (mq_entry->msg->size),
939 GNUNET_TIME_UNIT_FOREVER_REL,
940 &transmit_ready_notify, NULL);
946 * Queues a message in send queue for sending to the service
948 * @param client the client to whom the queued message has to be sent
949 * @param msg the message to queue
952 queue_message (struct GNUNET_SERVER_Client *client,
953 struct GNUNET_MessageHeader *msg)
955 struct MessageQueue *mq_entry;
959 type = ntohs (msg->type);
960 size = ntohs (msg->size);
961 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
962 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
963 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
965 mq_entry->client = client;
966 GNUNET_SERVER_client_keep (client);
967 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
969 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
970 if (NULL == transmit_handle)
972 GNUNET_SERVER_notify_transmit_ready (client, size,
973 GNUNET_TIME_UNIT_FOREVER_REL,
974 &transmit_ready_notify, NULL);
979 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
981 * @param ptr the memory block to realloc
982 * @param size the size of ptr
983 * @param new_size the size to which ptr has to be realloc'ed
984 * @return the newly reallocated memory block
987 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
989 ptr = GNUNET_realloc (ptr, new_size);
991 (void) memset (ptr + size, 0, new_size - size);
997 * Function to add a host to the current list of known hosts
999 * @param host the host to add
1000 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
1004 host_list_add (struct GNUNET_TESTBED_Host *host)
1009 host_id = GNUNET_TESTBED_host_get_id_ (host);
1010 orig_size = host_list_size;
1011 if (host_list_size <= host_id)
1013 while (host_list_size <= host_id)
1014 host_list_size += LIST_GROW_STEP;
1016 TESTBED_realloc (host_list,
1017 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
1018 sizeof (struct GNUNET_TESTBED_Host *)
1021 if (NULL != host_list[host_id])
1023 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
1024 return GNUNET_SYSERR;
1026 host_list[host_id] = host;
1032 * Adds a route to the route list
1034 * @param route the route to add
1037 route_list_add (struct Route *route)
1041 orig_size = route_list_size;
1042 if (route->dest >= route_list_size)
1044 while (route->dest >= route_list_size)
1045 route_list_size += LIST_GROW_STEP;
1047 TESTBED_realloc (route_list,
1048 sizeof (struct Route *) * orig_size,
1049 sizeof (struct Route *) * route_list_size);
1051 GNUNET_assert (NULL == route_list[route->dest]);
1052 route_list[route->dest] = route;
1057 * Adds a slave to the slave array
1059 * @param slave the slave controller to add
1062 slave_list_add (struct Slave *slave)
1066 orig_size = slave_list_size;
1067 if (slave->host_id >= slave_list_size)
1069 while (slave->host_id >= slave_list_size)
1070 slave_list_size += LIST_GROW_STEP;
1072 TESTBED_realloc (slave_list, sizeof (struct Slave *) * orig_size,
1073 sizeof (struct Slave *) * slave_list_size);
1075 GNUNET_assert (NULL == slave_list[slave->host_id]);
1076 slave_list[slave->host_id] = slave;
1081 * Adds a peer to the peer array
1083 * @param peer the peer to add
1086 peer_list_add (struct Peer *peer)
1090 orig_size = peer_list_size;
1091 if (peer->id >= peer_list_size)
1093 while (peer->id >= peer_list_size)
1094 peer_list_size += LIST_GROW_STEP;
1096 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
1097 sizeof (struct Peer *) * peer_list_size);
1099 GNUNET_assert (NULL == peer_list[peer->id]);
1100 peer_list[peer->id] = peer;
1105 * Removes a the give peer from the peer array
1107 * @param peer the peer to be removed
1110 peer_list_remove (struct Peer *peer)
1115 peer_list[peer->id] = NULL;
1116 orig_size = peer_list_size;
1117 while (peer_list_size >= LIST_GROW_STEP)
1119 for (id = peer_list_size - 1;
1120 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1121 if (NULL != peer_list[id])
1123 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1125 peer_list_size -= LIST_GROW_STEP;
1127 if (orig_size == peer_list_size)
1130 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1135 * Finds the route with directly connected host as destination through which
1136 * the destination host can be reached
1138 * @param host_id the id of the destination host
1139 * @return the route with directly connected destination host; NULL if no route
1142 static struct Route *
1143 find_dest_route (uint32_t host_id)
1145 struct Route *route;
1147 if (route_list_size <= host_id)
1149 while (NULL != (route = route_list[host_id]))
1151 if (route->thru == master_context->host_id)
1153 host_id = route->thru;
1160 * Routes message to a host given its host_id
1162 * @param host_id the id of the destination host
1163 * @param msg the message to be routed
1166 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1173 * Send operation failure message to client
1175 * @param client the client to which the failure message has to be sent to
1176 * @param operation_id the id of the failed operation
1177 * @param emsg the error message; can be NULL
1180 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1181 uint64_t operation_id, const char *emsg)
1183 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1187 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1188 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1190 msg = GNUNET_malloc (msize);
1191 msg->header.size = htons (msize);
1192 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1193 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1194 msg->operation_id = GNUNET_htonll (operation_id);
1196 memcpy (&msg[1], emsg, emsg_len);
1197 queue_message (client, &msg->header);
1202 * Function to send generic operation success message to given client
1204 * @param client the client to send the message to
1205 * @param operation_id the id of the operation which was successful
1208 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1209 uint64_t operation_id)
1211 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1214 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1215 msg = GNUNET_malloc (msize);
1216 msg->header.size = htons (msize);
1217 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1218 msg->operation_id = GNUNET_htonll (operation_id);
1219 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1220 queue_message (client, &msg->header);
1225 * Callback which will be called to after a host registration succeeded or failed
1227 * @param cls the handle to the slave at which the registration is completed
1228 * @param emsg the error message; NULL if host registration is successful
1231 hr_completion (void *cls, const char *emsg);
1235 * Attempts to register the next host in the host registration queue
1237 * @param slave the slave controller whose host registration queue is checked
1238 * for host registrations
1241 register_next_host (struct Slave *slave)
1243 struct HostRegistration *hr;
1245 hr = slave->hr_dll_head;
1246 GNUNET_assert (NULL != hr);
1247 GNUNET_assert (NULL == slave->rhandle);
1248 LOG (GNUNET_ERROR_TYPE_DEBUG,
1249 "Registering host %u at %u\n",
1250 GNUNET_TESTBED_host_get_id_ (hr->host),
1251 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1252 slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
1260 * Callback which will be called to after a host registration succeeded or failed
1262 * @param cls the handle to the slave at which the registration is completed
1263 * @param emsg the error message; NULL if host registration is successful
1266 hr_completion (void *cls, const char *emsg)
1268 struct Slave *slave = cls;
1269 struct HostRegistration *hr;
1271 slave->rhandle = NULL;
1272 hr = slave->hr_dll_head;
1273 GNUNET_assert (NULL != hr);
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "Registering host %u at %u successful\n",
1276 GNUNET_TESTBED_host_get_id_ (hr->host),
1277 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1278 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
1282 hr->cb (hr->cb_cls, emsg);
1284 if (NULL != slave->hr_dll_head)
1285 register_next_host (slave);
1290 * Adds a host registration's request to a slave's registration queue
1292 * @param slave the slave controller at which the given host has to be
1294 * @param cb the host registration completion callback
1295 * @param cb_cls the closure for the host registration completion callback
1296 * @param host the host which has to be registered
1299 queue_host_registration (struct Slave *slave,
1300 GNUNET_TESTBED_HostRegistrationCompletion cb,
1302 struct GNUNET_TESTBED_Host *host)
1304 struct HostRegistration *hr;
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Queueing host registration for host %u at %u\n",
1309 GNUNET_TESTBED_host_get_id_ (host),
1310 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1311 hr = GNUNET_malloc (sizeof (struct HostRegistration));
1313 hr->cb_cls = cb_cls;
1315 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
1316 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
1319 if (GNUNET_YES == call_register)
1320 register_next_host (slave);
1325 * The Link Controller forwarding task
1327 * @param cls the LCFContext
1328 * @param tc the Task context from scheduler
1331 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1335 * Completion callback for host registrations while forwarding Link Controller messages
1337 * @param cls the LCFContext
1338 * @param emsg the error message; NULL if host registration is successful
1341 lcf_proc_cc (void *cls, const char *emsg)
1343 struct LCFContext *lcf = cls;
1345 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1350 goto registration_error;
1351 lcf->state = DELEGATED_HOST_REGISTERED;
1352 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1354 case DELEGATED_HOST_REGISTERED:
1356 goto registration_error;
1357 lcf->state = SLAVE_HOST_REGISTERED;
1358 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1361 GNUNET_assert (0); /* Shouldn't reach here */
1366 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1368 lcf->state = FINISHED;
1369 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1374 * Callback to relay the reply msg of a forwarded operation back to the client
1376 * @param cls ForwardedOperationContext
1377 * @param msg the message to relay
1380 forwarded_operation_reply_relay (void *cls,
1381 const struct GNUNET_MessageHeader *msg)
1383 struct ForwardedOperationContext *fopc = cls;
1384 struct GNUNET_MessageHeader *dup_msg;
1387 msize = ntohs (msg->size);
1388 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1390 dup_msg = GNUNET_copy_message (msg);
1391 queue_message (fopc->client, dup_msg);
1392 GNUNET_SERVER_client_drop (fopc->client);
1393 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1394 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1400 * Task to free resources when forwarded operation has been timedout
1402 * @param cls the ForwardedOperationContext
1403 * @param tc the task context from scheduler
1406 forwarded_operation_timeout (void *cls,
1407 const struct GNUNET_SCHEDULER_TaskContext *tc)
1409 struct ForwardedOperationContext *fopc = cls;
1411 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1412 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1413 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1414 GNUNET_SERVER_client_drop (fopc->client);
1415 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
1421 * The Link Controller forwarding task
1423 * @param cls the LCFContext
1424 * @param tc the Task context from scheduler
1427 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1431 * Callback to be called when forwarded link controllers operation is
1432 * successfull. We have to relay the reply msg back to the client
1434 * @param cls the LCFContext
1435 * @param msg the message to relay
1438 lcf_forwarded_operation_reply_relay (void *cls,
1439 const struct GNUNET_MessageHeader *msg)
1441 struct LCFContext *lcf = cls;
1443 GNUNET_assert (NULL != lcf->fopc);
1444 forwarded_operation_reply_relay (lcf->fopc, msg);
1446 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1447 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1452 * Task to free resources when forwarded link controllers has been timedout
1454 * @param cls the LCFContext
1455 * @param tc the task context from scheduler
1458 lcf_forwarded_operation_timeout (void *cls,
1459 const struct GNUNET_SCHEDULER_TaskContext *tc)
1461 struct LCFContext *lcf = cls;
1463 GNUNET_assert (NULL != lcf->fopc);
1464 lcf->fopc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1465 forwarded_operation_timeout (lcf->fopc, tc);
1467 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1468 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1473 * The Link Controller forwarding task
1475 * @param cls the LCFContext
1476 * @param tc the Task context from scheduler
1479 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1481 struct LCFContext *lcf = cls;
1482 struct LCFContextQueue *lcfq;
1484 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1489 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1490 lcf->gateway->controller))
1492 queue_host_registration (lcf->gateway,
1494 host_list[lcf->delegated_host_id]);
1498 lcf->state = DELEGATED_HOST_REGISTERED;
1499 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1502 case DELEGATED_HOST_REGISTERED:
1504 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1505 lcf->gateway->controller))
1507 queue_host_registration (lcf->gateway,
1509 host_list[lcf->slave_host_id]);
1513 lcf->state = SLAVE_HOST_REGISTERED;
1514 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1517 case SLAVE_HOST_REGISTERED:
1518 lcf->fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1519 lcf->fopc->client = lcf->client;
1520 lcf->fopc->operation_id = lcf->operation_id;
1521 lcf->fopc->type = OP_LINK_CONTROLLERS;
1523 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1526 &lcf_forwarded_operation_reply_relay,
1528 lcf->fopc->timeout_task =
1529 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &lcf_forwarded_operation_timeout,
1531 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, lcf->fopc);
1532 lcf->state = FINISHED;
1536 GNUNET_assert (lcfq->lcf == lcf);
1537 GNUNET_free (lcf->msg);
1539 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1541 if (NULL != lcfq_head)
1543 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1549 * Cleans up ForwardedOverlayConnectContext
1551 * @param focc the ForwardedOverlayConnectContext to cleanup
1554 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1556 GNUNET_free_non_null (focc->orig_msg);
1562 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1564 * @param rhc the RegisteredHostContext
1567 process_next_focc (struct RegisteredHostContext *rhc);
1571 * Timeout task for cancelling a forwarded overlay connect connect
1573 * @param cls the ForwardedOverlayConnectContext
1574 * @param tc the task context from the scheduler
1577 forwarded_overlay_connect_timeout (void *cls,
1578 const struct GNUNET_SCHEDULER_TaskContext
1581 struct ForwardedOperationContext *fopc = cls;
1582 struct RegisteredHostContext *rhc;
1583 struct ForwardedOverlayConnectContext *focc;
1586 focc = rhc->focc_dll_head;
1587 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1588 cleanup_focc (focc);
1589 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
1590 focc->peer1, focc->peer2);
1591 forwarded_operation_timeout (cls, tc);
1592 if (NULL != rhc->focc_dll_head)
1593 process_next_focc (rhc);
1598 * Callback to be called when forwarded overlay connection operation has a reply
1599 * from the sub-controller successfull. We have to relay the reply msg back to
1602 * @param cls ForwardedOperationContext
1603 * @param msg the peer create success message
1606 forwarded_overlay_connect_listener (void *cls,
1607 const struct GNUNET_MessageHeader *msg)
1609 struct ForwardedOperationContext *fopc = cls;
1610 struct RegisteredHostContext *rhc;
1611 struct ForwardedOverlayConnectContext *focc;
1614 forwarded_operation_reply_relay (cls, msg);
1615 focc = rhc->focc_dll_head;
1616 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1617 cleanup_focc (focc);
1618 if (NULL != rhc->focc_dll_head)
1619 process_next_focc (rhc);
1624 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1626 * @param rhc the RegisteredHostContext
1629 process_next_focc (struct RegisteredHostContext *rhc)
1631 struct ForwardedOperationContext *fopc;
1632 struct ForwardedOverlayConnectContext *focc;
1634 focc = rhc->focc_dll_head;
1635 GNUNET_assert (NULL != focc);
1636 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1637 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1638 GNUNET_SERVER_client_keep (rhc->client);
1639 fopc->client = rhc->client;
1640 fopc->operation_id = focc->operation_id;
1642 fopc->type = OP_OVERLAY_CONNECT;
1644 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1645 focc->operation_id, focc->orig_msg,
1646 &forwarded_overlay_connect_listener,
1648 GNUNET_free (focc->orig_msg);
1649 focc->orig_msg = NULL;
1650 fopc->timeout_task =
1651 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1653 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1658 * Callback for event from slave controllers
1660 * @param cls struct Slave *
1661 * @param event information about the event
1664 slave_event_callback (void *cls,
1665 const struct GNUNET_TESTBED_EventInformation *event)
1667 struct RegisteredHostContext *rhc;
1668 struct GNUNET_CONFIGURATION_Handle *cfg;
1669 struct GNUNET_TESTBED_Operation *old_op;
1671 /* We currently only get here when working on RegisteredHostContexts */
1672 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1673 rhc = event->details.operation_finished.op_cls;
1674 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1678 cfg = event->details.operation_finished.generic;
1679 old_op = rhc->sub_op;
1680 rhc->state = RHC_LINK;
1682 GNUNET_TESTBED_controller_link (rhc,
1683 rhc->gateway->controller,
1688 GNUNET_TESTBED_operation_done (old_op);
1691 LOG_DEBUG ("OL: Linking controllers successfull\n");
1692 GNUNET_TESTBED_operation_done (rhc->sub_op);
1694 rhc->state = RHC_OL_CONNECT;
1695 process_next_focc (rhc);
1704 * Callback to signal successfull startup of the controller process
1706 * @param cls the handle to the slave whose status is to be found here
1707 * @param cfg the configuration with which the controller has been started;
1708 * NULL if status is not GNUNET_OK
1709 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1710 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1713 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1716 struct Slave *slave = cls;
1717 struct LinkControllersContext *lcc;
1720 if (GNUNET_SYSERR == status)
1722 slave->controller_proc = NULL;
1723 slave_list[slave->host_id] = NULL;
1724 if (NULL != slave->cfg)
1725 GNUNET_CONFIGURATION_destroy (slave->cfg);
1726 GNUNET_free (slave);
1728 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1729 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1733 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1735 &slave_event_callback, slave);
1736 if (NULL != slave->controller)
1738 send_operation_success_msg (lcc->client, lcc->operation_id);
1739 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1743 send_operation_fail_msg (lcc->client, lcc->operation_id,
1744 "Could not connect to delegated controller");
1745 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1746 slave_list[slave->host_id] = NULL;
1747 GNUNET_free (slave);
1754 if (NULL != lcc->client)
1756 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1757 GNUNET_SERVER_client_drop (lcc->client);
1768 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1771 * @param client identification of the client
1772 * @param message the actual message
1775 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1776 const struct GNUNET_MessageHeader *message)
1778 const struct GNUNET_TESTBED_InitMessage *msg;
1779 struct GNUNET_TESTBED_Host *host;
1780 const char *controller_hostname;
1783 if (NULL != master_context)
1785 LOG_DEBUG ("We are being connected to laterally\n");
1786 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1789 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1790 msize = ntohs (message->size);
1791 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1797 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1798 controller_hostname = (const char *) &msg[1];
1799 if ('\0' != controller_hostname[msize - 1])
1802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1805 master_context = GNUNET_malloc (sizeof (struct Context));
1806 GNUNET_SERVER_client_keep (client);
1807 master_context->client = client;
1808 master_context->host_id = ntohl (msg->host_id);
1809 master_context->master_ip = GNUNET_strdup (controller_hostname);
1810 LOG_DEBUG ("Our IP: %s\n", master_context->master_ip);
1811 master_context->system =
1812 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1814 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1815 master_context->master_ip,
1818 host_list_add (host);
1819 LOG_DEBUG ("Created master context with host ID: %u\n",
1820 master_context->host_id);
1821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1826 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1829 * @param client identification of the client
1830 * @param message the actual message
1833 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1834 const struct GNUNET_MessageHeader *message)
1836 struct GNUNET_TESTBED_Host *host;
1837 const struct GNUNET_TESTBED_AddHostMessage *msg;
1838 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1843 uint16_t username_length;
1844 uint16_t hostname_length;
1845 uint16_t reply_size;
1848 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1849 msize = ntohs (msg->header.size);
1850 username = (char *) &msg[1];
1851 username_length = ntohs (msg->user_name_length);
1852 if (0 != username_length)
1854 /* msg must contain hostname */
1855 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1856 username_length + 1));
1857 if (0 != username_length)
1858 GNUNET_assert ('\0' == username[username_length - 1]);
1859 hostname = username + username_length;
1861 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1862 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1863 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1864 host_id = ntohl (msg->host_id);
1865 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1866 LOG_DEBUG ("-------host id: %u\n", host_id);
1867 LOG_DEBUG ("-------hostname: %s\n", hostname);
1868 if (0 != username_length)
1869 LOG_DEBUG ("-------username: %s\n", username);
1872 LOG_DEBUG ("-------username: NULL\n");
1875 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1877 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1878 ntohs (msg->ssh_port));
1879 GNUNET_assert (NULL != host);
1880 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1881 if (GNUNET_OK != host_list_add (host))
1883 /* We are unable to add a host */
1884 emsg = "A host exists with given host-id";
1885 LOG_DEBUG ("%s: %u", emsg, host_id);
1886 GNUNET_TESTBED_host_destroy (host);
1887 reply_size += strlen (emsg) + 1;
1888 reply = GNUNET_malloc (reply_size);
1889 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1893 LOG_DEBUG ("Added host %u at %u\n",
1894 host_id, master_context->host_id);
1895 reply = GNUNET_malloc (reply_size);
1897 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1898 reply->header.size = htons (reply_size);
1899 reply->host_id = htonl (host_id);
1900 queue_message (client, &reply->header);
1901 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1906 * Iterator over hash map entries.
1908 * @param cls closure
1909 * @param key current key code
1910 * @param value value in the hash map
1911 * @return GNUNET_YES if we should continue to
1916 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1918 struct SharedService *queried_ss = cls;
1919 struct SharedService *ss = value;
1921 if (0 == strcmp (ss->name, queried_ss->name))
1929 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1932 * @param client identification of the client
1933 * @param message the actual message
1936 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1937 const struct GNUNET_MessageHeader *message)
1939 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1940 struct SharedService *ss;
1942 struct GNUNET_HashCode hash;
1944 uint16_t service_name_size;
1946 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1947 msg_size = ntohs (message->size);
1948 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1955 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1956 service_name = (char *) &msg[1];
1957 if ('\0' != service_name[service_name_size - 1])
1960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1963 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1964 service_name, ntohl (msg->num_peers));
1965 if (ntohl (msg->host_id) != master_context->host_id)
1967 route_message (ntohl (msg->host_id), message);
1968 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1971 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1972 ss = GNUNET_malloc (sizeof (struct SharedService));
1973 ss->name = strdup (service_name);
1974 ss->num_shared = ntohl (msg->num_peers);
1975 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1976 if (GNUNET_SYSERR ==
1977 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1978 &ss_exists_iterator, ss))
1980 LOG (GNUNET_ERROR_TYPE_WARNING,
1981 "Service %s already configured as a shared service. "
1982 "Ignoring service sharing request \n", ss->name);
1983 GNUNET_free (ss->name);
1987 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1988 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1993 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1996 * @param client identification of the client
1997 * @param message the actual message
2000 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
2001 const struct GNUNET_MessageHeader *message)
2003 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
2004 struct GNUNET_CONFIGURATION_Handle *cfg;
2005 struct LCFContextQueue *lcfq;
2006 struct Route *route;
2007 struct Route *new_route;
2011 uint32_t delegated_host_id;
2012 uint32_t slave_host_id;
2015 if (NULL == master_context)
2018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2021 msize = ntohs (message->size);
2022 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
2025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2028 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
2029 delegated_host_id = ntohl (msg->delegated_host_id);
2030 if (delegated_host_id == master_context->host_id)
2033 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
2034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2037 if ((delegated_host_id >= host_list_size) ||
2038 (NULL == host_list[delegated_host_id]))
2040 LOG (GNUNET_ERROR_TYPE_WARNING,
2041 "Delegated host %u not registered with us\n", delegated_host_id);
2042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2045 slave_host_id = ntohl (msg->slave_host_id);
2046 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
2048 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
2050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2053 if (slave_host_id == delegated_host_id)
2055 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
2056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2060 if (slave_host_id == master_context->host_id) /* Link from us */
2062 struct Slave *slave;
2063 struct LinkControllersContext *lcc;
2065 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
2066 config_size = ntohs (msg->config_size);
2067 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
2069 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
2071 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2074 config = GNUNET_malloc (config_size);
2075 dest_size = (uLongf) config_size;
2077 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
2080 GNUNET_break (0); /* Compression error */
2081 GNUNET_free (config);
2082 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2085 if (config_size != dest_size)
2087 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
2088 GNUNET_free (config);
2089 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2092 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
2094 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2096 GNUNET_break (0); /* Configuration parsing error */
2097 GNUNET_free (config);
2098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2101 GNUNET_free (config);
2102 if ((delegated_host_id < slave_list_size) &&
2103 (NULL != slave_list[delegated_host_id]))
2105 GNUNET_break (0); /* Configuration parsing error */
2106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2109 slave = GNUNET_malloc (sizeof (struct Slave));
2110 slave->host_id = delegated_host_id;
2111 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
2112 slave_list_add (slave);
2113 if (1 != msg->is_subordinate)
2116 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
2118 &slave_event_callback, slave);
2120 if (NULL != slave->controller)
2121 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2123 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2124 "Could not connect to delegated controller");
2125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2128 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
2129 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
2130 GNUNET_SERVER_client_keep (client);
2131 lcc->client = client;
2133 slave->controller_proc =
2134 GNUNET_TESTBED_controller_start (master_context->master_ip,
2135 host_list[slave->host_id], cfg,
2136 &slave_status_callback, slave);
2137 GNUNET_CONFIGURATION_destroy (cfg);
2138 new_route = GNUNET_malloc (sizeof (struct Route));
2139 new_route->dest = delegated_host_id;
2140 new_route->thru = master_context->host_id;
2141 route_list_add (new_route);
2145 /* Route the request */
2146 if (slave_host_id >= route_list_size)
2148 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
2149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2152 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
2153 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
2154 lcfq->lcf->delegated_host_id = delegated_host_id;
2155 lcfq->lcf->slave_host_id = slave_host_id;
2156 route = find_dest_route (slave_host_id);
2157 GNUNET_assert (NULL != route); /* because we add routes carefully */
2158 GNUNET_assert (route->dest < slave_list_size);
2159 GNUNET_assert (NULL != slave_list[route->dest]);
2160 lcfq->lcf->state = INIT;
2161 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
2162 lcfq->lcf->gateway = slave_list[route->dest];
2163 lcfq->lcf->msg = GNUNET_malloc (msize);
2164 (void) memcpy (lcfq->lcf->msg, msg, msize);
2165 GNUNET_SERVER_client_keep (client);
2166 lcfq->lcf->client = client;
2167 if (NULL == lcfq_head)
2169 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2170 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2171 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
2174 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2175 /* FIXME: Adding a new route should happen after the controllers are linked
2177 if (1 != msg->is_subordinate)
2179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2182 if ((delegated_host_id < route_list_size)
2183 && (NULL != route_list[delegated_host_id]))
2185 GNUNET_break_op (0); /* Are you trying to link delegated host twice
2186 with is subordinate flag set to GNUNET_YES? */
2187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2190 new_route = GNUNET_malloc (sizeof (struct Route));
2191 new_route->dest = delegated_host_id;
2192 new_route->thru = route->dest;
2193 route_list_add (new_route);
2194 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2199 * The task to be executed if the forwarded peer create operation has been
2202 * @param cls the FowardedOperationContext
2203 * @param tc the TaskContext from the scheduler
2206 peer_create_forward_timeout (void *cls,
2207 const struct GNUNET_SCHEDULER_TaskContext *tc)
2209 struct ForwardedOperationContext *fopc = cls;
2211 GNUNET_free (fopc->cls);
2212 forwarded_operation_timeout (fopc, tc);
2217 * Callback to be called when forwarded peer create operation is successfull. We
2218 * have to relay the reply msg back to the client
2220 * @param cls ForwardedOperationContext
2221 * @param msg the peer create success message
2224 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2226 struct ForwardedOperationContext *fopc = cls;
2227 struct Peer *remote_peer;
2229 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
2231 GNUNET_assert (NULL != fopc->cls);
2232 remote_peer = fopc->cls;
2233 peer_list_add (remote_peer);
2235 forwarded_operation_reply_relay (fopc, msg);
2240 * Function to destroy a peer
2242 * @param peer the peer structure to destroy
2245 destroy_peer (struct Peer *peer)
2247 GNUNET_break (0 == peer->reference_cnt);
2248 if (GNUNET_YES == peer->is_remote)
2250 peer_list_remove (peer);
2254 if (GNUNET_YES == peer->details.local.is_running)
2256 GNUNET_TESTING_peer_stop (peer->details.local.peer);
2257 peer->details.local.is_running = GNUNET_NO;
2259 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2260 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2261 peer_list_remove (peer);
2267 * Callback to be called when forwarded peer destroy operation is successfull. We
2268 * have to relay the reply msg back to the client
2270 * @param cls ForwardedOperationContext
2271 * @param msg the peer create success message
2274 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2276 struct ForwardedOperationContext *fopc = cls;
2277 struct Peer *remote_peer;
2279 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS == ntohs (msg->type))
2281 remote_peer = fopc->cls;
2282 GNUNET_assert (NULL != remote_peer);
2283 remote_peer->destroy_flag = GNUNET_YES;
2284 if (0 == remote_peer->reference_cnt)
2285 destroy_peer (remote_peer);
2287 forwarded_operation_reply_relay (fopc, msg);
2293 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
2296 * @param client identification of the client
2297 * @param message the actual message
2300 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
2301 const struct GNUNET_MessageHeader *message)
2303 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
2304 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
2305 struct GNUNET_CONFIGURATION_Handle *cfg;
2306 struct ForwardedOperationContext *fo_ctxt;
2307 struct Route *route;
2312 uint32_t config_size;
2318 msize = ntohs (message->size);
2319 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
2321 GNUNET_break (0); /* We need configuration */
2322 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2325 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
2326 host_id = ntohl (msg->host_id);
2327 peer_id = ntohl (msg->peer_id);
2328 if (UINT32_MAX == peer_id)
2330 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2331 "Cannot create peer with given ID");
2332 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2335 if (host_id == master_context->host_id)
2339 /* We are responsible for this peer */
2340 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
2341 config_size = ntohl (msg->config_size);
2342 config = GNUNET_malloc (config_size);
2343 dest_size = config_size;
2346 uncompress ((Bytef *) config, (uLongf *) & dest_size,
2347 (const Bytef *) &msg[1], (uLong) msize)))
2349 GNUNET_break (0); /* uncompression error */
2350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2353 if (config_size != dest_size)
2355 GNUNET_break (0); /* Uncompressed config size mismatch */
2356 GNUNET_free (config);
2357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2360 cfg = GNUNET_CONFIGURATION_create ();
2362 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2364 GNUNET_break (0); /* Configuration parsing error */
2365 GNUNET_free (config);
2366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2369 GNUNET_free (config);
2370 peer = GNUNET_malloc (sizeof (struct Peer));
2371 peer->is_remote = GNUNET_NO;
2372 peer->details.local.cfg = cfg;
2374 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
2375 peer->details.local.peer =
2376 GNUNET_TESTING_peer_configure (master_context->system,
2377 peer->details.local.cfg, peer->id,
2378 NULL /* Peer id */ ,
2380 if (NULL == peer->details.local.peer)
2382 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2389 peer->details.local.is_running = GNUNET_NO;
2390 peer_list_add (peer);
2392 GNUNET_malloc (sizeof
2393 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2394 reply->header.size =
2395 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2396 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2397 reply->peer_id = msg->peer_id;
2398 reply->operation_id = msg->operation_id;
2399 queue_message (client, &reply->header);
2400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2404 /* Forward peer create request */
2405 route = find_dest_route (host_id);
2409 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2413 peer = GNUNET_malloc (sizeof (struct Peer));
2414 peer->is_remote = GNUNET_YES;
2416 peer->details.remote.slave = slave_list[route->dest];
2417 peer->details.remote.remote_host_id = host_id;
2418 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2419 GNUNET_SERVER_client_keep (client);
2420 fo_ctxt->client = client;
2421 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2422 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2423 fo_ctxt->type = OP_PEER_CREATE;
2425 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2426 fo_ctxt->operation_id,
2428 peer_create_success_cb, fo_ctxt);
2429 fo_ctxt->timeout_task =
2430 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2432 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
2433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2438 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2441 * @param client identification of the client
2442 * @param message the actual message
2445 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2446 const struct GNUNET_MessageHeader *message)
2448 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2449 struct ForwardedOperationContext *fopc;
2453 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2454 peer_id = ntohl (msg->peer_id);
2455 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2456 peer_id, GNUNET_ntohll (msg->operation_id));
2457 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2459 LOG (GNUNET_ERROR_TYPE_ERROR,
2460 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2461 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2462 "Peer doesn't exist");
2463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2466 peer = peer_list[peer_id];
2467 if (GNUNET_YES == peer->is_remote)
2469 /* Forward the destory message to sub controller */
2470 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2471 GNUNET_SERVER_client_keep (client);
2472 fopc->client = client;
2474 fopc->type = OP_PEER_DESTROY;
2475 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2477 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2478 fopc->operation_id, &msg->header,
2479 &peer_destroy_success_cb,
2481 fopc->timeout_task =
2482 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2484 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2488 peer->destroy_flag = GNUNET_YES;
2489 if (0 == peer->reference_cnt)
2490 destroy_peer (peer);
2492 LOG (GNUNET_ERROR_TYPE_WARNING,
2493 "Delaying peer destroy as peer is currently in use\n");
2494 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2495 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2500 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2503 * @param client identification of the client
2504 * @param message the actual message
2507 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2508 const struct GNUNET_MessageHeader *message)
2510 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2511 struct GNUNET_TESTBED_PeerEventMessage *reply;
2512 struct ForwardedOperationContext *fopc;
2516 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2517 peer_id = ntohl (msg->peer_id);
2518 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2521 LOG (GNUNET_ERROR_TYPE_ERROR,
2522 "Asked to start a non existent peer with id: %u\n", peer_id);
2523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2526 peer = peer_list[peer_id];
2527 if (GNUNET_YES == peer->is_remote)
2529 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2530 GNUNET_SERVER_client_keep (client);
2531 fopc->client = client;
2532 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2533 fopc->type = OP_PEER_START;
2535 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2536 fopc->operation_id, &msg->header,
2537 &forwarded_operation_reply_relay,
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 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2548 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2550 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2553 peer->details.local.is_running = GNUNET_YES;
2554 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2555 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2556 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2557 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2558 reply->host_id = htonl (master_context->host_id);
2559 reply->peer_id = msg->peer_id;
2560 reply->operation_id = msg->operation_id;
2561 queue_message (client, &reply->header);
2562 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2567 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2570 * @param client identification of the client
2571 * @param message the actual message
2574 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2575 const struct GNUNET_MessageHeader *message)
2577 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2578 struct GNUNET_TESTBED_PeerEventMessage *reply;
2579 struct ForwardedOperationContext *fopc;
2583 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2584 peer_id = ntohl (msg->peer_id);
2585 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2587 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2589 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2592 peer = peer_list[peer_id];
2593 if (GNUNET_YES == peer->is_remote)
2595 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2596 GNUNET_SERVER_client_keep (client);
2597 fopc->client = client;
2598 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2599 fopc->type = OP_PEER_STOP;
2601 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2602 fopc->operation_id, &msg->header,
2603 &forwarded_operation_reply_relay,
2605 fopc->timeout_task =
2606 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2608 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2609 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2612 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2614 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2615 "Peer not running");
2616 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2619 peer->details.local.is_running = GNUNET_NO;
2620 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2621 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2622 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2623 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2624 reply->host_id = htonl (master_context->host_id);
2625 reply->peer_id = msg->peer_id;
2626 reply->operation_id = msg->operation_id;
2627 queue_message (client, &reply->header);
2628 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2633 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2636 * @param client identification of the client
2637 * @param message the actual message
2640 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2641 const struct GNUNET_MessageHeader *message)
2643 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2644 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2653 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2654 peer_id = ntohl (msg->peer_id);
2655 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2657 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2659 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2662 peer = peer_list[peer_id];
2663 if (GNUNET_YES == peer->is_remote)
2665 struct ForwardedOperationContext *fopc;
2667 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2668 GNUNET_SERVER_client_keep (client);
2669 fopc->client = client;
2670 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2671 fopc->type = OP_PEER_INFO;
2673 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2674 fopc->operation_id, &msg->header,
2675 &forwarded_operation_reply_relay,
2677 fopc->timeout_task =
2678 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2680 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
2681 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2685 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2687 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2688 GNUNET_free (config);
2691 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2692 reply = GNUNET_realloc (xconfig, msize);
2693 (void) memmove (&reply[1], reply, xc_size);
2694 reply->header.size = htons (msize);
2695 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2696 reply->peer_id = msg->peer_id;
2697 reply->operation_id = msg->operation_id;
2698 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2699 &reply->peer_identity);
2700 reply->config_size = htons ((uint16_t) c_size);
2701 queue_message (client, &reply->header);
2702 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2707 * Cleanup overlay connect context structure
2709 * @param occ the overlay connect context
2712 cleanup_occ (struct OverlayConnectContext *occ)
2714 LOG_DEBUG ("Cleaning up occ\n");
2715 GNUNET_free_non_null (occ->emsg);
2716 GNUNET_free_non_null (occ->hello);
2717 GNUNET_SERVER_client_drop (occ->client);
2718 if (NULL != occ->opc)
2719 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2720 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2721 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2722 if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
2723 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
2724 if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
2725 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2726 if (NULL != occ->ch)
2728 GNUNET_CORE_disconnect (occ->ch);
2729 occ->peer->reference_cnt--;
2731 if (NULL != occ->ghh)
2732 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2733 if (NULL != occ->p1th)
2735 GNUNET_TRANSPORT_disconnect (occ->p1th);
2736 occ->peer->reference_cnt--;
2738 if (NULL != occ->p2th)
2740 GNUNET_TRANSPORT_disconnect (occ->p2th);
2741 peer_list[occ->other_peer_id]->reference_cnt--;
2743 if ((GNUNET_YES == occ->peer->destroy_flag)
2744 && (0 == occ->peer->reference_cnt))
2745 destroy_peer (occ->peer);
2746 if ((NULL == occ->peer2_controller)
2747 && (GNUNET_YES == peer_list[occ->other_peer_id]->destroy_flag)
2748 && (0 == peer_list[occ->other_peer_id]->reference_cnt))
2749 destroy_peer (peer_list[occ->other_peer_id]);
2750 GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
2756 * Task for cleaing up overlay connect context structure
2758 * @param cls the overlay connect context
2759 * @param tc the task context
2762 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2764 struct OverlayConnectContext *occ = cls;
2766 occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
2772 * Task which will be run when overlay connect request has been timed out
2774 * @param cls the OverlayConnectContext
2775 * @param tc the TaskContext
2778 timeout_overlay_connect (void *cls,
2779 const struct GNUNET_SCHEDULER_TaskContext *tc)
2781 struct OverlayConnectContext *occ = cls;
2783 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2784 LOG (GNUNET_ERROR_TYPE_WARNING,
2785 "Timeout while connecting peers %u and %u\n",
2786 occ->peer_id, occ->other_peer_id);
2787 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2794 * Function called to notify transport users that another
2795 * peer connected to us.
2797 * @param cls closure
2798 * @param new_peer the peer that connected
2799 * @param ats performance data
2800 * @param ats_count number of entries in ats (excluding 0-termination)
2803 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2804 const struct GNUNET_ATS_Information *ats,
2805 unsigned int ats_count)
2807 struct OverlayConnectContext *occ = cls;
2808 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2810 char *other_peer_str;
2812 LOG_DEBUG ("Overlay connect notify\n");
2814 memcmp (new_peer, &occ->peer_identity,
2815 sizeof (struct GNUNET_PeerIdentity)))
2817 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2818 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2820 memcmp (new_peer, &occ->other_peer_identity,
2821 sizeof (struct GNUNET_PeerIdentity)))
2823 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2824 new_peer_str, other_peer_str);
2825 GNUNET_free (new_peer_str);
2826 GNUNET_free (other_peer_str);
2829 GNUNET_free (new_peer_str);
2830 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2831 GNUNET_i2s (&occ->peer_identity));
2832 GNUNET_free (other_peer_str);
2833 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2835 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2836 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2838 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2839 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2840 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2841 GNUNET_free_non_null (occ->emsg);
2843 if (NULL != occ->p2th)
2845 GNUNET_TRANSPORT_disconnect (occ->p2th);
2846 peer_list[occ->other_peer_id]->reference_cnt--;
2849 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2850 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2852 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2853 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2854 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2855 msg->peer1 = htonl (occ->peer_id);
2856 msg->peer2 = htonl (occ->other_peer_id);
2857 msg->operation_id = GNUNET_htonll (occ->op_id);
2858 queue_message (occ->client, &msg->header);
2859 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
2860 //cleanup_occ (occ);
2865 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2868 * @param cls the OverlayConnectContext
2869 * @param tc the TaskContext from scheduler
2872 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2876 * Task that is run when hello has been sent
2878 * @param cls the overlay connect context
2879 * @param tc the scheduler task context; if tc->reason =
2880 * GNUNET_SCHEDULER_REASON_TIMEOUT then sending HELLO failed; if
2881 * GNUNET_SCHEDULER_REASON_READ_READY is succeeded
2883 //static FIXME: uncomment when using
2885 occ_hello_sent_cb (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2887 struct OverlayConnectContext *occ = cls;
2890 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == occ->send_hello_task);
2891 if (GNUNET_SCHEDULER_REASON_TIMEOUT == tc->reason)
2893 occ->send_hello_task =
2894 GNUNET_SCHEDULER_add_delayed
2895 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2896 100 + GNUNET_CRYPTO_random_u32
2897 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
2901 if (GNUNET_SCHEDULER_REASON_READ_READY != tc->reason)
2903 ret = GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2904 if (GNUNET_OK == ret)
2906 if (GNUNET_SYSERR == ret)
2908 occ->send_hello_task =
2909 GNUNET_SCHEDULER_add_delayed
2910 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2911 100 + GNUNET_CRYPTO_random_u32
2912 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
2918 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2921 * @param cls the OverlayConnectContext
2922 * @param tc the TaskContext from scheduler
2925 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2927 struct OverlayConnectContext *occ = cls;
2928 char *other_peer_str;
2930 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2931 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2933 GNUNET_assert (NULL != occ->hello);
2934 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2935 if (NULL != occ->peer2_controller)
2937 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2939 uint16_t hello_size;
2941 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2942 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2943 hello_size = ntohs (occ->hello->size);
2944 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2945 msg = GNUNET_malloc (msize);
2946 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2947 msg->header.size = htons (msize);
2948 msg->peer = htonl (occ->other_peer_id);
2949 msg->operation_id = GNUNET_htonll (occ->op_id);
2950 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2951 sizeof (struct GNUNET_PeerIdentity));
2952 memcpy (msg->hello, occ->hello, hello_size);
2953 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2957 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2958 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2959 /* FIXME: To be replaced by */
2960 /* occ->offer_hello_handle = GNUNET_TRANSPORT_offer_hello (occ->p2th, */
2962 /* occ_hello_sent_cb, */
2965 /* FIXME: once offer_hello offers a handle to cancel remove the following lines */
2966 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2967 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity, NULL, NULL); /*FIXME TRY_CONNECT change */
2968 occ->send_hello_task =
2969 GNUNET_SCHEDULER_add_delayed
2970 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2971 100 + GNUNET_CRYPTO_random_u32
2972 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
2975 GNUNET_free (other_peer_str);
2979 * Test for checking whether HELLO message is empty
2981 * @param cls empty flag to set
2982 * @param address the HELLO
2983 * @param expiration expiration of the HELLO
2987 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2988 struct GNUNET_TIME_Absolute expiration)
2998 * Function called whenever there is an update to the HELLO of peers in the
2999 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
3000 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
3002 * @param cls closure
3003 * @param hello our updated HELLO
3006 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
3008 struct OverlayConnectContext *occ = cls;
3012 msize = ntohs (hello->size);
3014 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
3015 hello, GNUNET_NO, &test_address,
3017 if (GNUNET_YES == empty)
3019 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
3022 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
3023 occ->hello = GNUNET_malloc (msize);
3024 memcpy (occ->hello, hello, msize);
3025 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
3027 GNUNET_TRANSPORT_disconnect (occ->p1th);
3029 occ->peer->reference_cnt--;
3030 GNUNET_free_non_null (occ->emsg);
3031 if (NULL == occ->peer2_controller)
3033 peer_list[occ->other_peer_id]->reference_cnt++;
3035 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
3036 &occ->other_peer_identity, NULL, NULL, NULL,
3038 if (NULL == occ->p2th)
3040 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
3041 GNUNET_i2s (&occ->other_peer_identity));
3042 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3043 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3047 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
3048 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
3053 * Function called after GNUNET_CORE_connect has succeeded (or failed
3054 * for good). Note that the private key of the peer is intentionally
3055 * not exposed here; if you need it, your process should try to read
3056 * the private key file directly (which should work if you are
3059 * @param cls closure
3060 * @param server handle to the server, NULL if we failed
3061 * @param my_identity ID of this peer, NULL if we failed
3064 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
3065 const struct GNUNET_PeerIdentity *my_identity)
3067 struct OverlayConnectContext *occ = cls;
3069 GNUNET_free_non_null (occ->emsg);
3070 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
3071 if ((NULL == server) || (NULL == my_identity))
3073 GNUNET_free (occ->emsg);
3076 memcpy (&occ->peer_identity, my_identity,
3077 sizeof (struct GNUNET_PeerIdentity));
3078 occ->peer->reference_cnt++;
3080 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
3081 &occ->peer_identity, NULL, NULL, NULL, NULL);
3082 if (NULL == occ->p1th)
3084 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
3085 GNUNET_i2s (&occ->peer_identity));
3088 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
3089 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
3090 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
3094 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3095 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3101 * Callback to be called when forwarded get peer config operation as part of
3102 * overlay connect is successfull. Connection to Peer 1's core is made and is
3103 * checked for new connection from peer 2
3105 * @param cls ForwardedOperationContext
3106 * @param msg the peer create success message
3109 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
3111 struct OverlayConnectContext *occ = cls;
3112 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
3113 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3118 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
3120 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
3122 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
3123 sizeof (struct GNUNET_PeerIdentity));
3124 GNUNET_free_non_null (occ->emsg);
3125 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3126 occ->peer->reference_cnt++;
3128 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3129 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3130 GNUNET_NO, no_handlers);
3131 if (NULL == occ->ch)
3136 GNUNET_SCHEDULER_cancel (occ->timeout_task);
3138 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3143 * Callback which will be called to after a host registration succeeded or failed
3145 * @param cls the RegisteredHostContext
3146 * @param emsg the error message; NULL if host registration is successful
3149 registeredhost_registration_completion (void *cls, const char *emsg)
3151 struct RegisteredHostContext *rhc = cls;
3152 struct GNUNET_CONFIGURATION_Handle *cfg;
3153 uint32_t peer2_host_id;
3155 /* if (NULL != rhc->focc_dll_head) */
3156 /* process_next_focc (rhc); */
3157 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
3158 GNUNET_assert (RHC_INIT == rhc->state);
3159 GNUNET_assert (NULL == rhc->sub_op);
3160 if ((NULL == rhc->gateway2)
3161 || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
3162 && (NULL != slave_list[peer2_host_id])))
3164 rhc->state = RHC_LINK;
3165 cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
3167 GNUNET_TESTBED_controller_link (rhc,
3168 rhc->gateway->controller,
3175 rhc->state = RHC_GET_CFG;
3176 rhc->sub_op = GNUNET_TESTBED_get_slave_config (rhc,
3177 rhc->gateway2->controller,
3183 * Iterator to match a registered host context
3185 * @param cls pointer 2 pointer of RegisteredHostContext
3186 * @param key current key code
3187 * @param value value in the hash map
3188 * @return GNUNET_YES if we should continue to
3193 reghost_match_iterator (void *cls,
3194 const struct GNUNET_HashCode * key,
3197 struct RegisteredHostContext **rh = cls;
3198 struct RegisteredHostContext *rh_val = value;
3200 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
3211 * Function to generate the hashcode corresponding to a RegisteredHostContext
3213 * @param reg_host the host which is being registered in RegisteredHostContext
3214 * @param host the host of the controller which has to connect to the above rhost
3215 * @return the hashcode
3217 static struct GNUNET_HashCode
3218 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
3219 struct GNUNET_TESTBED_Host *host)
3221 struct GNUNET_HashCode hash;
3222 uint32_t host_ids[2];
3224 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
3225 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
3226 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
3232 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
3235 * @param client identification of the client
3236 * @param message the actual message
3239 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
3240 const struct GNUNET_MessageHeader *message)
3242 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
3243 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3247 struct OverlayConnectContext *occ;
3248 struct GNUNET_TESTBED_Controller *peer2_controller;
3249 uint64_t operation_id;
3252 uint32_t peer2_host_id;
3254 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
3255 p1 = ntohl (msg->peer1);
3256 p2 = ntohl (msg->peer2);
3257 peer2_host_id = ntohl (msg->peer2_host_id);
3258 GNUNET_assert (p1 < peer_list_size);
3259 GNUNET_assert (NULL != peer_list[p1]);
3260 peer = peer_list[p1];
3261 operation_id = GNUNET_ntohll (msg->operation_id);
3262 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
3263 p1, p2, operation_id);
3264 if (GNUNET_YES == peer->is_remote)
3266 struct ForwardedOperationContext *fopc;
3267 struct Route *route_to_peer2_host;
3268 struct Route *route_to_peer1_host;
3270 LOG_DEBUG ("Forwarding overlay connect\n");
3271 route_to_peer2_host = NULL;
3272 route_to_peer1_host = NULL;
3273 route_to_peer2_host = find_dest_route (peer2_host_id);
3274 if ((NULL != route_to_peer2_host)
3275 || (peer2_host_id == master_context->host_id))
3277 /* Peer 2 either below us OR with us */
3278 route_to_peer1_host =
3279 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
3280 /* Because we get this message only if we know where peer 1 is */
3281 GNUNET_assert (NULL != route_to_peer1_host);
3282 if ((peer2_host_id == master_context->host_id)
3283 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
3285 /* Peer2 is either with us OR peer1 and peer2 can be reached through
3286 different gateways */
3287 struct GNUNET_HashCode hash;
3288 struct RegisteredHostContext *rhc;
3291 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
3292 if (NULL != route_to_peer2_host)
3293 rhc->reg_host = host_list[route_to_peer2_host->dest];
3295 rhc->reg_host = host_list[master_context->host_id];
3296 rhc->host = host_list[route_to_peer1_host->dest];
3297 GNUNET_assert (NULL != rhc->reg_host);
3298 GNUNET_assert (NULL != rhc->host);
3299 rhc->gateway = peer->details.remote.slave;
3300 rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
3301 slave_list[route_to_peer2_host->dest];
3302 rhc->state = RHC_INIT;
3303 GNUNET_SERVER_client_keep (client);
3304 rhc->client = client;
3305 hash = hash_hosts (rhc->reg_host, rhc->host);
3306 skip_focc = GNUNET_NO;
3308 GNUNET_CONTAINER_multihashmap_contains
3309 (peer->details.remote.slave->reghost_map, &hash))
3310 || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
3311 (peer->details.remote.slave->reghost_map, &hash,
3312 reghost_match_iterator, &rhc)))
3314 /* create and add a new registerd host context */
3315 /* add the focc to its queue */
3316 GNUNET_CONTAINER_multihashmap_put
3317 (peer->details.remote.slave->reghost_map,
3318 &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3319 GNUNET_assert (NULL != host_list[peer2_host_id]);
3320 queue_host_registration (peer->details.remote.slave,
3321 registeredhost_registration_completion, rhc,
3322 host_list[peer2_host_id]);
3325 /* rhc is now set to the existing one from the hash map by
3326 reghost_match_iterator() */
3327 /* if queue is empty then ignore creating focc and proceed with
3328 normal forwarding */
3329 if (RHC_OL_CONNECT == rhc->state)
3330 skip_focc = GNUNET_YES;
3332 if (GNUNET_NO == skip_focc)
3334 struct ForwardedOverlayConnectContext *focc;
3336 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
3339 focc->peer2_host_id = peer2_host_id;
3340 focc->orig_msg = GNUNET_copy_message (message);
3341 focc->operation_id = operation_id;
3342 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
3345 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3350 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
3351 GNUNET_SERVER_client_keep (client);
3352 fopc->client = client;
3353 fopc->operation_id = operation_id;
3354 fopc->type = OP_OVERLAY_CONNECT;
3356 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
3357 operation_id, message,
3358 &forwarded_operation_reply_relay,
3360 fopc->timeout_task =
3361 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
3363 GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
3364 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3368 peer2_controller = NULL;
3369 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
3371 if ((peer2_host_id >= slave_list_size)
3372 || (NULL ==slave_list[peer2_host_id]))
3374 LOG (GNUNET_ERROR_TYPE_WARNING,
3375 "Configuration of peer2's controller missing for connecting peers"
3376 "%u and %u\n", p1, p2);
3377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3380 peer2_controller = slave_list[peer2_host_id]->controller;
3381 if (NULL == peer2_controller)
3383 GNUNET_break (0); /* What's going on? */
3384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3390 if (GNUNET_YES == peer_list[p2]->is_remote)
3391 peer2_controller = peer_list[p2]->details.remote.slave->controller;
3393 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
3394 GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
3395 GNUNET_SERVER_client_keep (client);
3396 occ->client = client;
3398 occ->other_peer_id = p2;
3399 occ->peer = peer_list[p1];
3400 occ->op_id = GNUNET_ntohll (msg->operation_id);
3401 occ->peer2_controller = peer2_controller;
3402 /* Get the identity of the second peer */
3403 if (NULL != occ->peer2_controller)
3405 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
3408 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
3409 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
3410 cmsg.peer_id = msg->peer2;
3411 cmsg.operation_id = msg->operation_id;
3413 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
3414 occ->op_id, &cmsg.header,
3415 &overlay_connect_get_config,
3418 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
3420 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3421 (GNUNET_TIME_UNIT_SECONDS, 30),
3422 &timeout_overlay_connect, occ);
3423 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3426 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
3427 &occ->other_peer_identity);
3428 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
3429 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3430 occ->peer->reference_cnt++;
3432 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3433 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3434 GNUNET_NO, no_handlers);
3435 if (NULL == occ->ch)
3437 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3440 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3441 (GNUNET_TIME_UNIT_SECONDS, 30),
3442 &timeout_overlay_connect, occ);
3443 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3448 * Function to cleanup RequestOverlayConnectContext and any associated tasks
3451 * @param rocc the RequestOverlayConnectContext
3454 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
3456 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
3457 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
3458 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
3459 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
3460 GNUNET_TRANSPORT_disconnect (rocc->th);
3461 GNUNET_free_non_null (rocc->hello);
3462 GNUNET_CONTAINER_DLL_remove (roccq_head, roccq_tail, rocc);
3468 * Task to timeout rocc and cleanit up
3470 * @param cls the RequestOverlayConnectContext
3471 * @param tc the TaskContext from scheduler
3474 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3476 struct RequestOverlayConnectContext *rocc = cls;
3478 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
3479 cleanup_rocc (rocc);
3484 * Function called to notify transport users that another
3485 * peer connected to us.
3487 * @param cls closure
3488 * @param new_peer the peer that connected
3489 * @param ats performance data
3490 * @param ats_count number of entries in ats (excluding 0-termination)
3493 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3494 const struct GNUNET_ATS_Information * ats,
3497 struct RequestOverlayConnectContext *rocc = cls;
3499 LOG_DEBUG ("Request Overlay connect notify\n");
3500 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3502 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3503 cleanup_rocc (rocc);
3508 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3509 * whose identity is in RequestOverlayConnectContext
3511 * @param cls the RequestOverlayConnectContext
3512 * @param tc the TaskContext from scheduler
3515 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3517 struct RequestOverlayConnectContext *rocc = cls;
3519 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3520 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
3521 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id, NULL, NULL); /*FIXME TRY_CONNECT change */
3522 rocc->attempt_connect_task_id =
3523 GNUNET_SCHEDULER_add_delayed
3524 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3525 100 + GNUNET_CRYPTO_random_u32
3526 (GNUNET_CRYPTO_QUALITY_WEAK, 500)),
3527 &attempt_connect_task, rocc);
3532 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3535 * @param client identification of the client
3536 * @param message the actual message
3539 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3540 const struct GNUNET_MessageHeader *message)
3542 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3543 struct RequestOverlayConnectContext *rocc;
3549 msize = ntohs (message->size);
3550 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3556 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3557 if ((NULL == msg->hello) ||
3558 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3564 hsize = ntohs (msg->hello->size);
3565 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3571 peer_id = ntohl (msg->peer);
3572 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3574 GNUNET_break_op (0);
3575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3578 if (GNUNET_YES == peer->is_remote)
3580 struct GNUNET_MessageHeader *msg2;
3582 msg2 = GNUNET_copy_message (message);
3583 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3584 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3587 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3588 GNUNET_CONTAINER_DLL_insert_tail (roccq_head, roccq_tail, rocc);
3589 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
3590 NULL, &transport_connect_notify, NULL);
3591 if (NULL == rocc->th)
3595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3598 memcpy (&rocc->a_id, &msg->peer_identity,
3599 sizeof (struct GNUNET_PeerIdentity));
3600 rocc->hello = GNUNET_malloc (hsize);
3601 memcpy (rocc->hello, msg->hello, hsize);
3602 rocc->attempt_connect_task_id =
3603 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3604 rocc->timeout_rocc_task_id =
3605 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3606 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3611 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3614 * @param client identification of the client
3615 * @param message the actual message
3618 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3619 const struct GNUNET_MessageHeader *message)
3621 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3622 struct Slave *slave;
3623 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3627 size_t xconfig_size;
3632 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3633 slave_id = ntohl (msg->slave_id);
3634 op_id = GNUNET_ntohll (msg->operation_id);
3635 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3637 /* FIXME: Add forwardings for this type of message here.. */
3638 send_operation_fail_msg (client, op_id, "Slave not found");
3639 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3642 slave = slave_list[slave_id];
3643 if (NULL == slave->cfg)
3645 send_operation_fail_msg (client, op_id,
3646 "Configuration not found (slave not started by me)");
3647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3650 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3651 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3653 GNUNET_free (config);
3654 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3655 GNUNET_break (reply_size <= UINT16_MAX);
3656 GNUNET_break (config_size <= UINT16_MAX);
3657 reply = GNUNET_realloc (xconfig, reply_size);
3658 (void) memmove (&reply[1], reply, xconfig_size);
3659 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3660 reply->header.size = htons ((uint16_t) reply_size);
3661 reply->slave_id = msg->slave_id;
3662 reply->operation_id = msg->operation_id;
3663 reply->config_size = htons ((uint16_t) config_size);
3664 queue_message (client, &reply->header);
3665 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3670 * Iterator over hash map entries.
3672 * @param cls closure
3673 * @param key current key code
3674 * @param value value in the hash map
3675 * @return GNUNET_YES if we should continue to
3680 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3682 struct SharedService *ss = value;
3684 GNUNET_assert (GNUNET_YES ==
3685 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3686 GNUNET_free (ss->name);
3693 * Iterator for freeing hash map entries in a slave's reghost_map
3695 * @param cls handle to the slave
3696 * @param key current key code
3697 * @param value value in the hash map
3698 * @return GNUNET_YES if we should continue to
3703 reghost_free_iterator (void *cls,
3704 const struct GNUNET_HashCode * key,
3707 struct Slave *slave = cls;
3708 struct RegisteredHostContext *rhc = value;
3709 struct ForwardedOverlayConnectContext *focc;
3711 GNUNET_assert (GNUNET_YES ==
3712 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3714 while (NULL != (focc = rhc->focc_dll_head))
3716 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3719 cleanup_focc (focc);
3721 if (NULL != rhc->sub_op)
3722 GNUNET_TESTBED_operation_done (rhc->sub_op);
3723 if (NULL != rhc->client)
3724 GNUNET_SERVER_client_drop (rhc->client);
3725 GNUNET_free (value);
3731 * Task to clean up and shutdown nicely
3734 * @param tc the TaskContext from scheduler
3737 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3739 struct LCFContextQueue *lcfq;
3740 struct OverlayConnectContext *occ;
3741 struct RequestOverlayConnectContext *rocc;
3742 struct ForwardedOperationContext *fopc;
3743 struct MessageQueue *mq_entry;
3746 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3747 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3748 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3750 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3751 /* cleanup any remaining forwarded operations */
3752 while (NULL != (fopc = fopcq_head))
3754 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
3755 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
3756 if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
3757 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
3758 GNUNET_SERVER_client_drop (fopc->client);
3761 case OP_PEER_CREATE:
3762 GNUNET_free (fopc->cls);
3766 case OP_PEER_DESTROY:
3768 case OP_OVERLAY_CONNECT:
3769 case OP_LINK_CONTROLLERS:
3770 case OP_GET_SLAVE_CONFIG:
3777 if (NULL != lcfq_head)
3779 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3781 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3782 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3785 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3786 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3788 GNUNET_free (lcfq->lcf->msg);
3789 GNUNET_free (lcfq->lcf);
3790 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3793 while (NULL != (occ = occq_head))
3795 while (NULL != (rocc = roccq_head))
3796 cleanup_rocc (rocc);
3797 /* Clear peer list */
3798 for (id = 0; id < peer_list_size; id++)
3799 if (NULL != peer_list[id])
3801 /* If destroy flag is set it means that this peer should have been
3802 destroyed by a context which we destroy before */
3803 GNUNET_break (GNUNET_NO == peer_list[id]->destroy_flag);
3804 /* counter should be zero as we free all contexts before */
3805 GNUNET_break (0 == peer_list[id]->reference_cnt);
3806 if (GNUNET_NO == peer_list[id]->is_remote)
3808 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3809 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
3810 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3811 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3813 GNUNET_free (peer_list[id]);
3815 GNUNET_free_non_null (peer_list);
3816 /* Clear host list */
3817 for (id = 0; id < host_list_size; id++)
3818 if (NULL != host_list[id])
3819 GNUNET_TESTBED_host_destroy (host_list[id]);
3820 GNUNET_free_non_null (host_list);
3821 /* Clear route list */
3822 for (id = 0; id < route_list_size; id++)
3823 if (NULL != route_list[id])
3824 GNUNET_free (route_list[id]);
3825 GNUNET_free_non_null (route_list);
3826 /* Clear slave_list */
3827 for (id = 0; id < slave_list_size; id++)
3828 if (NULL != slave_list[id])
3830 struct HostRegistration *hr_entry;
3832 while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
3834 GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
3835 slave_list[id]->hr_dll_tail,
3837 GNUNET_free (hr_entry);
3839 if (NULL != slave_list[id]->rhandle)
3840 GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
3841 (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
3842 reghost_free_iterator,
3844 GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
3845 if (NULL != slave_list[id]->cfg)
3846 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
3847 if (NULL != slave_list[id]->controller)
3848 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
3849 if (NULL != slave_list[id]->controller_proc)
3850 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
3851 GNUNET_free (slave_list[id]);
3853 GNUNET_free_non_null (slave_list);
3854 if (NULL != master_context)
3856 GNUNET_free_non_null (master_context->master_ip);
3857 if (NULL != master_context->system)
3858 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
3859 GNUNET_SERVER_client_drop (master_context->client);
3860 GNUNET_free (master_context);
3861 master_context = NULL;
3863 if (NULL != transmit_handle)
3864 GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
3865 while (NULL != (mq_entry = mq_head))
3867 GNUNET_free (mq_entry->msg);
3868 GNUNET_SERVER_client_drop (mq_entry->client);
3869 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
3870 GNUNET_free (mq_entry);
3872 GNUNET_free_non_null (hostname);
3873 GNUNET_CONFIGURATION_destroy (our_config);
3878 * Callback for client disconnect
3881 * @param client the client which has disconnected
3884 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
3886 if (NULL == master_context)
3888 if (client == master_context->client)
3890 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
3891 /* should not be needed as we're terminated by failure to read
3892 * from stdin, but if stdin fails for some reason, this shouldn't
3893 * hurt for now --- might need to revise this later if we ever
3894 * decide that master connections might be temporarily down
3895 * for some reason */
3896 //GNUNET_SCHEDULER_shutdown ();
3904 * @param cls closure
3905 * @param server the initialized server
3906 * @param cfg configuration to use
3909 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
3910 const struct GNUNET_CONFIGURATION_Handle *cfg)
3912 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
3913 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
3914 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
3915 {&handle_configure_shared_service, NULL,
3916 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
3917 {&handle_link_controllers, NULL,
3918 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
3919 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
3920 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
3921 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
3922 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
3923 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
3924 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
3925 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
3926 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
3927 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
3928 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
3929 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
3930 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
3932 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
3933 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
3937 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
3938 (cfg, "testbed", "HOSTNAME", &hostname));
3939 our_config = GNUNET_CONFIGURATION_dup (cfg);
3940 GNUNET_SERVER_add_handlers (server, message_handlers);
3941 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
3942 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
3944 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
3945 GNUNET_SCHEDULER_PRIORITY_IDLE,
3946 &shutdown_task, NULL);
3947 LOG_DEBUG ("Testbed startup complete\n");
3948 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
3953 * The starting point of execution
3956 main (int argc, char *const *argv)
3958 //sleep (15); /* Debugging */
3959 return (GNUNET_OK ==
3960 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
3961 &testbed_run, NULL)) ? 0 : 1;
3964 /* end of gnunet-service-testbed.c */