2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed.c
23 * @brief implementation of the TESTBED service
24 * @author Sree Harsha Totakura
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_hello_lib.h"
35 #include "gnunet_testbed_service.h"
37 #include "testbed_api.h"
38 #include "testbed_api_hosts.h"
39 #include "gnunet_testing_lib-new.h"
44 #define LOG(kind,...) \
45 GNUNET_log (kind, __VA_ARGS__)
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
54 * By how much should the arrays lists grow
56 #define LIST_GROW_STEP 10
59 * Default timeout for operations which may take some time
61 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
64 * The main context information associated with the client which started us
69 * The client handle associated with this context
71 struct GNUNET_SERVER_Client *client;
74 * The network address of the master controller
79 * The TESTING system handle for starting peers locally
81 struct GNUNET_TESTING_System *system;
84 * Our host id according to this context
91 * The message queue for sending messages to clients
96 * The message to be sent
98 struct GNUNET_MessageHeader *msg;
101 * The client to send the message to
103 struct GNUNET_SERVER_Client *client;
106 * next pointer for DLL
108 struct MessageQueue *next;
111 * prev pointer for DLL
113 struct MessageQueue *prev;
118 * The structure for identifying a shared service
123 * The name of the shared service
128 * Number of shared peers per instance of the shared service
133 * Number of peers currently sharing the service
135 uint32_t num_sharing;
150 * The destination host is reachable thru
157 * Context information used while linking controllers
159 struct LinkControllersContext;
163 * A DLL of host registrations to be made
165 struct HostRegistration
168 * next registration in the DLL
170 struct HostRegistration *next;
173 * previous registration in the DLL
175 struct HostRegistration *prev;
178 * The callback to call after this registration's status is available
180 GNUNET_TESTBED_HostRegistrationCompletion cb;
183 * The closure for the above callback
188 * The host that has to be registered
190 struct GNUNET_TESTBED_Host *host;
195 * This context information will be created for each host that is registered at
196 * slave controllers during overlay connects.
198 struct RegisteredHostContext
201 * The host which is being registered
203 struct GNUNET_TESTBED_Host *reg_host;
206 * The host of the controller which has to connect to the above rhost
208 struct GNUNET_TESTBED_Host *host;
211 * The gateway to which this operation is forwarded to
213 struct Slave *gateway;
216 * The gateway through which peer2's controller can be reached
218 struct Slave *gateway2;
221 * Handle for sub-operations
223 struct GNUNET_TESTBED_Operation *sub_op;
226 * The client which initiated the link controller operation
228 struct GNUNET_SERVER_Client *client;
231 * Head of the ForwardedOverlayConnectContext DLL
233 struct ForwardedOverlayConnectContext *focc_dll_head;
236 * Tail of the ForwardedOverlayConnectContext DLL
238 struct ForwardedOverlayConnectContext *focc_dll_tail;
241 * Enumeration of states for this context
251 * State where we attempt to get peer2's controller configuration
256 * State where we attempt to link the controller of peer 1 to the controller
262 * State where we attempt to do the overlay connection again
272 * Structure representing a connected(directly-linked) controller
277 * The controller process handle if we had started the controller
279 struct GNUNET_TESTBED_ControllerProc *controller_proc;
282 * The controller handle
284 struct GNUNET_TESTBED_Controller *controller;
287 * The configuration of the slave. Cannot be NULL
289 struct GNUNET_CONFIGURATION_Handle *cfg;
292 * handle to lcc which is associated with this slave startup. Should be set to
293 * NULL when the slave has successfully started up
295 struct LinkControllersContext *lcc;
298 * Head of the host registration DLL
300 struct HostRegistration *hr_dll_head;
303 * Tail of the host registration DLL
305 struct HostRegistration *hr_dll_tail;
308 * The current host registration handle
310 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
313 * Hashmap to hold Registered host contexts
315 struct GNUNET_CONTAINER_MultiHashMap *reghost_map;
318 * The id of the host this controller is running on
326 * States of LCFContext
331 * The Context has been initialized; Nothing has been done on it
336 * Delegated host has been registered at the forwarding controller
338 DELEGATED_HOST_REGISTERED,
341 * The slave host has been registred at the forwarding controller
343 SLAVE_HOST_REGISTERED,
346 * The context has been finished (may have error)
353 * Link controllers request forwarding context
358 * The gateway which will pass the link message to delegated host
360 struct Slave *gateway;
363 * The controller link message that has to be forwarded to
365 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
368 * The client which has asked to perform this operation
370 struct GNUNET_SERVER_Client *client;
373 * The id of the operation which created this context
375 uint64_t operation_id;
378 * The state of this context
380 enum LCFContextState state;
385 uint32_t delegated_host_id;
390 uint32_t slave_host_id;
396 * Structure of a queue entry in LCFContext request queue
398 struct LCFContextQueue
403 struct LCFContext *lcf;
408 struct LCFContextQueue *next;
413 struct LCFContextQueue *prev;
428 * The peer handle from testing API
430 struct GNUNET_TESTING_Peer *peer;
433 * The modified (by GNUNET_TESTING_peer_configure) configuration this
434 * peer is configured with
436 struct GNUNET_CONFIGURATION_Handle *cfg;
439 * Is the peer running
448 * The slave this peer is started through
453 * The id of the remote host this peer is running on
455 uint32_t remote_host_id;
462 * Is this peer locally created?
467 * Our local reference id for this peer
475 * Context information for connecting 2 peers in overlay
477 struct OverlayConnectContext
480 * The next pointer for maintaining a DLL
482 struct OverlayConnectContext *next;
485 * The prev pointer for maintaining a DLL
487 struct OverlayConnectContext *prev;
490 * The client which has requested for overlay connection
492 struct GNUNET_SERVER_Client *client;
495 * the peer which has to connect to the other peer
500 * Transport handle of the first peer to get its HELLO
502 struct GNUNET_TRANSPORT_Handle *p1th;
505 * Transport handle of other peer to offer first peer's HELLO
507 struct GNUNET_TRANSPORT_Handle *p2th;
510 * Core handles of the first peer; used to notify when second peer connects to it
512 struct GNUNET_CORE_Handle *ch;
515 * HELLO of the other peer
517 struct GNUNET_MessageHeader *hello;
520 * Get hello handle to acquire HELLO of first peer
522 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
525 * The error message we send if this overlay connect operation has timed out
530 * Operation context for suboperations
532 struct OperationContext *opc;
535 * Controller of peer 2; NULL if the peer is local
537 struct GNUNET_TESTBED_Controller *peer2_controller;
540 * The peer identity of the first peer
542 struct GNUNET_PeerIdentity peer_identity;
545 * The peer identity of the other peer
547 struct GNUNET_PeerIdentity other_peer_identity;
550 * The id of the operation responsible for creating this context
555 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
558 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
561 * The id of the overlay connect timeout task
563 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
566 * The id of the cleanup task
568 GNUNET_SCHEDULER_TaskIdentifier cleanup_task;
578 uint32_t other_peer_id;
581 * Number of times we tried to send hello; used to increase delay in offering
589 * Context information for RequestOverlayConnect
590 * operations. RequestOverlayConnect is used when peers A, B reside on different
591 * hosts and the host controller for peer B is asked by the host controller of
592 * peer A to make peer B connect to peer A
594 struct RequestOverlayConnectContext
597 * The transport handle of peer B
599 struct GNUNET_TRANSPORT_Handle *th;
604 struct GNUNET_MessageHeader *hello;
607 * The peer identity of peer A
609 struct GNUNET_PeerIdentity a_id;
612 * Task for offering HELLO of A to B and doing try_connect
614 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
617 * Task to timeout RequestOverlayConnect
619 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
622 * Number of times we tried to send hello; used to increase delay in offering
631 * Context information for operations forwarded to subcontrollers
633 struct ForwardedOperationContext
636 * The generated operation context
638 struct OperationContext *opc;
641 * The client to which we have to reply
643 struct GNUNET_SERVER_Client *client;
651 * Task ID for the timeout task
653 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
656 * The id of the operation that has been forwarded
658 uint64_t operation_id;
664 * Context information used while linking controllers
666 struct LinkControllersContext
669 * The client which initiated the link controller operation
671 struct GNUNET_SERVER_Client *client;
674 * The ID of the operation
676 uint64_t operation_id;
682 * Context information to used during operations which forward the overlay
685 struct ForwardedOverlayConnectContext
688 * next ForwardedOverlayConnectContext in the DLL
690 struct ForwardedOverlayConnectContext *next;
693 * previous ForwardedOverlayConnectContext in the DLL
695 struct ForwardedOverlayConnectContext *prev;
698 * A copy of the original overlay connect message
700 struct GNUNET_MessageHeader *orig_msg;
703 * The id of the operation which created this context information
705 uint64_t operation_id;
718 * Id of the host where peer2 is running
720 uint32_t peer2_host_id;
726 * The master context; generated with the first INIT message
728 static struct Context *master_context;
731 * Our hostname; we give this to all the peers we start
733 static char *hostname;
743 static struct GNUNET_CONFIGURATION_Handle *our_config;
746 * Current Transmit Handle; NULL if no notify transmit exists currently
748 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
755 * The head for the LCF queue
757 static struct LCFContextQueue *lcfq_head;
760 * The tail for the LCF queue
762 static struct LCFContextQueue *lcfq_tail;
765 * The message queue head
767 static struct MessageQueue *mq_head;
770 * The message queue tail
772 static struct MessageQueue *mq_tail;
775 * DLL head for OverlayConnectContext DLL - to be used to clean up during shutdown
777 static struct OverlayConnectContext *occq_head;
780 * DLL tail for OverlayConnectContext DLL
782 static struct OverlayConnectContext *occq_tail;
787 static struct GNUNET_TESTBED_Host **host_list;
792 static struct Route **route_list;
795 * A list of directly linked neighbours
797 static struct Slave **slave_list;
800 * A list of peers we know about
802 static struct Peer **peer_list;
805 * The hashmap of shared services
807 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
810 * The event mask for the events we listen from sub-controllers
812 static uint64_t event_mask;
815 * The size of the host list
817 static uint32_t host_list_size;
820 * The size of the route list
822 static uint32_t route_list_size;
825 * The size of directly linked neighbours list
827 static uint32_t slave_list_size;
830 * The size of the peer list
832 static uint32_t peer_list_size;
839 * The lcf_task handle
841 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
844 * The shutdown task handle
846 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
850 * Function called to notify a client about the connection begin ready to queue
851 * more data. "buf" will be NULL and "size" zero if the connection was closed
852 * for writing in the meantime.
855 * @param size number of bytes available in buf
856 * @param buf where the callee should write the message
857 * @return number of bytes written to buf
860 transmit_ready_notify (void *cls, size_t size, void *buf)
862 struct MessageQueue *mq_entry;
864 transmit_handle = NULL;
866 GNUNET_assert (NULL != mq_entry);
869 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
870 size = ntohs (mq_entry->msg->size);
871 memcpy (buf, mq_entry->msg, size);
872 GNUNET_free (mq_entry->msg);
873 GNUNET_SERVER_client_drop (mq_entry->client);
874 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
875 GNUNET_free (mq_entry);
877 if (NULL != mq_entry)
879 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
880 ntohs (mq_entry->msg->size),
881 GNUNET_TIME_UNIT_FOREVER_REL,
882 &transmit_ready_notify, NULL);
888 * Queues a message in send queue for sending to the service
890 * @param client the client to whom the queued message has to be sent
891 * @param msg the message to queue
894 queue_message (struct GNUNET_SERVER_Client *client,
895 struct GNUNET_MessageHeader *msg)
897 struct MessageQueue *mq_entry;
901 type = ntohs (msg->type);
902 size = ntohs (msg->size);
903 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
904 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
905 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
907 mq_entry->client = client;
908 GNUNET_SERVER_client_keep (client);
909 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
911 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
912 if (NULL == transmit_handle)
914 GNUNET_SERVER_notify_transmit_ready (client, size,
915 GNUNET_TIME_UNIT_FOREVER_REL,
916 &transmit_ready_notify, NULL);
921 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
923 * @param ptr the memory block to realloc
924 * @param size the size of ptr
925 * @param new_size the size to which ptr has to be realloc'ed
926 * @return the newly reallocated memory block
929 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
931 ptr = GNUNET_realloc (ptr, new_size);
933 (void) memset (ptr + size, 0, new_size - size);
939 * Function to add a host to the current list of known hosts
941 * @param host the host to add
942 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
946 host_list_add (struct GNUNET_TESTBED_Host *host)
951 host_id = GNUNET_TESTBED_host_get_id_ (host);
952 orig_size = host_list_size;
953 if (host_list_size <= host_id)
955 while (host_list_size <= host_id)
956 host_list_size += LIST_GROW_STEP;
958 TESTBED_realloc (host_list,
959 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
960 sizeof (struct GNUNET_TESTBED_Host *)
963 if (NULL != host_list[host_id])
965 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
966 return GNUNET_SYSERR;
968 host_list[host_id] = host;
974 * Adds a route to the route list
976 * @param route the route to add
979 route_list_add (struct Route *route)
983 orig_size = route_list_size;
984 if (route->dest >= route_list_size)
986 while (route->dest >= route_list_size)
987 route_list_size += LIST_GROW_STEP;
989 TESTBED_realloc (route_list,
990 sizeof (struct Route *) * orig_size,
991 sizeof (struct Route *) * route_list_size);
993 GNUNET_assert (NULL == route_list[route->dest]);
994 route_list[route->dest] = route;
999 * Adds a slave to the slave array
1001 * @param slave the slave controller to add
1004 slave_list_add (struct Slave *slave)
1008 orig_size = slave_list_size;
1009 if (slave->host_id >= slave_list_size)
1011 while (slave->host_id >= slave_list_size)
1012 slave_list_size += LIST_GROW_STEP;
1014 TESTBED_realloc (slave_list, sizeof (struct Slave *) * orig_size,
1015 sizeof (struct Slave *) * slave_list_size);
1017 GNUNET_assert (NULL == slave_list[slave->host_id]);
1018 slave_list[slave->host_id] = slave;
1023 * Adds a peer to the peer array
1025 * @param peer the peer to add
1028 peer_list_add (struct Peer *peer)
1032 orig_size = peer_list_size;
1033 if (peer->id >= peer_list_size)
1035 while (peer->id >= peer_list_size)
1036 peer_list_size += LIST_GROW_STEP;
1038 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
1039 sizeof (struct Peer *) * peer_list_size);
1041 GNUNET_assert (NULL == peer_list[peer->id]);
1042 peer_list[peer->id] = peer;
1047 * Removes a the give peer from the peer array
1049 * @param peer the peer to be removed
1052 peer_list_remove (struct Peer *peer)
1057 peer_list[peer->id] = NULL;
1058 orig_size = peer_list_size;
1059 while (peer_list_size >= LIST_GROW_STEP)
1061 for (id = peer_list_size - 1;
1062 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
1063 if (NULL != peer_list[id])
1065 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
1067 peer_list_size -= LIST_GROW_STEP;
1069 if (orig_size == peer_list_size)
1072 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
1077 * Finds the route with directly connected host as destination through which
1078 * the destination host can be reached
1080 * @param host_id the id of the destination host
1081 * @return the route with directly connected destination host; NULL if no route
1084 static struct Route *
1085 find_dest_route (uint32_t host_id)
1087 struct Route *route;
1089 if (route_list_size <= host_id)
1091 while (NULL != (route = route_list[host_id]))
1093 if (route->thru == master_context->host_id)
1095 host_id = route->thru;
1102 * Routes message to a host given its host_id
1104 * @param host_id the id of the destination host
1105 * @param msg the message to be routed
1108 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
1115 * Send operation failure message to client
1117 * @param client the client to which the failure message has to be sent to
1118 * @param operation_id the id of the failed operation
1119 * @param emsg the error message; can be NULL
1122 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
1123 uint64_t operation_id, const char *emsg)
1125 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
1129 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
1130 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
1132 msg = GNUNET_malloc (msize);
1133 msg->header.size = htons (msize);
1134 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
1135 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1136 msg->operation_id = GNUNET_htonll (operation_id);
1138 memcpy (&msg[1], emsg, emsg_len);
1139 queue_message (client, &msg->header);
1144 * Function to send generic operation success message to given client
1146 * @param client the client to send the message to
1147 * @param operation_id the id of the operation which was successful
1150 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
1151 uint64_t operation_id)
1153 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
1156 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1157 msg = GNUNET_malloc (msize);
1158 msg->header.size = htons (msize);
1159 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1160 msg->operation_id = GNUNET_htonll (operation_id);
1161 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1162 queue_message (client, &msg->header);
1167 * Callback which will be called to after a host registration succeeded or failed
1169 * @param cls the handle to the slave at which the registration is completed
1170 * @param emsg the error message; NULL if host registration is successful
1173 hr_completion (void *cls, const char *emsg);
1177 * Attempts to register the next host in the host registration queue
1179 * @param slave the slave controller whose host registration queue is checked
1180 * for host registrations
1183 register_next_host (struct Slave *slave)
1185 struct HostRegistration *hr;
1187 hr = slave->hr_dll_head;
1188 GNUNET_assert (NULL != hr);
1189 GNUNET_assert (NULL == slave->rhandle);
1190 LOG (GNUNET_ERROR_TYPE_DEBUG,
1191 "Registering host %u at %u\n",
1192 GNUNET_TESTBED_host_get_id_ (hr->host),
1193 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1194 slave->rhandle = GNUNET_TESTBED_register_host (slave->controller,
1202 * Callback which will be called to after a host registration succeeded or failed
1204 * @param cls the handle to the slave at which the registration is completed
1205 * @param emsg the error message; NULL if host registration is successful
1208 hr_completion (void *cls, const char *emsg)
1210 struct Slave *slave = cls;
1211 struct HostRegistration *hr;
1213 slave->rhandle = NULL;
1214 hr = slave->hr_dll_head;
1215 GNUNET_assert (NULL != hr);
1216 LOG (GNUNET_ERROR_TYPE_DEBUG,
1217 "Registering host %u at %u successful\n",
1218 GNUNET_TESTBED_host_get_id_ (hr->host),
1219 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1220 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head,
1224 hr->cb (hr->cb_cls, emsg);
1226 if (NULL != slave->hr_dll_head)
1227 register_next_host (slave);
1232 * Adds a host registration's request to a slave's registration queue
1234 * @param slave the slave controller at which the given host has to be
1236 * @param cb the host registration completion callback
1237 * @param cb_cls the closure for the host registration completion callback
1238 * @param host the host which has to be registered
1241 queue_host_registration (struct Slave *slave,
1242 GNUNET_TESTBED_HostRegistrationCompletion cb,
1244 struct GNUNET_TESTBED_Host *host)
1246 struct HostRegistration *hr;
1249 LOG (GNUNET_ERROR_TYPE_DEBUG,
1250 "Queueing host registration for host %u at %u\n",
1251 GNUNET_TESTBED_host_get_id_ (host),
1252 GNUNET_TESTBED_host_get_id_ (host_list[slave->host_id]));
1253 hr = GNUNET_malloc (sizeof (struct HostRegistration));
1255 hr->cb_cls = cb_cls;
1257 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
1258 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head,
1261 if (GNUNET_YES == call_register)
1262 register_next_host (slave);
1267 * The Link Controller forwarding task
1269 * @param cls the LCFContext
1270 * @param tc the Task context from scheduler
1273 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1277 * Completion callback for host registrations while forwarding Link Controller messages
1279 * @param cls the LCFContext
1280 * @param emsg the error message; NULL if host registration is successful
1283 lcf_proc_cc (void *cls, const char *emsg)
1285 struct LCFContext *lcf = cls;
1287 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1292 goto registration_error;
1293 lcf->state = DELEGATED_HOST_REGISTERED;
1294 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1296 case DELEGATED_HOST_REGISTERED:
1298 goto registration_error;
1299 lcf->state = SLAVE_HOST_REGISTERED;
1300 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1303 GNUNET_assert (0); /* Shouldn't reach here */
1308 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1310 lcf->state = FINISHED;
1311 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1316 * Callback to be called when forwarded link controllers operation is
1317 * successfull. We have to relay the reply msg back to the client
1319 * @param cls ForwardedOperationContext
1320 * @param msg the peer create success message
1323 forwarded_operation_reply_relay (void *cls,
1324 const struct GNUNET_MessageHeader *msg)
1326 struct ForwardedOperationContext *fopc = cls;
1327 struct GNUNET_MessageHeader *dup_msg;
1330 msize = ntohs (msg->size);
1331 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1333 dup_msg = GNUNET_malloc (msize);
1334 (void) memcpy (dup_msg, msg, msize);
1335 queue_message (fopc->client, dup_msg);
1336 GNUNET_SERVER_client_drop (fopc->client);
1337 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1343 * Task to free resources when forwarded link controllers has been timedout
1345 * @param cls the ForwardedOperationContext
1346 * @param tc the task context from scheduler
1349 forwarded_operation_timeout (void *cls,
1350 const struct GNUNET_SCHEDULER_TaskContext *tc)
1352 struct ForwardedOperationContext *fopc = cls;
1354 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1355 LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
1356 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1357 GNUNET_SERVER_client_drop (fopc->client);
1363 * The Link Controller forwarding task
1365 * @param cls the LCFContext
1366 * @param tc the Task context from scheduler
1369 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1371 struct LCFContext *lcf = cls;
1372 struct LCFContextQueue *lcfq;
1373 struct ForwardedOperationContext *fopc;
1375 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1380 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1381 lcf->gateway->controller))
1383 queue_host_registration (lcf->gateway,
1385 host_list[lcf->delegated_host_id]);
1389 lcf->state = DELEGATED_HOST_REGISTERED;
1390 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1393 case DELEGATED_HOST_REGISTERED:
1395 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1396 lcf->gateway->controller))
1398 queue_host_registration (lcf->gateway,
1400 host_list[lcf->slave_host_id]);
1404 lcf->state = SLAVE_HOST_REGISTERED;
1405 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1408 case SLAVE_HOST_REGISTERED:
1409 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1410 fopc->client = lcf->client;
1411 fopc->operation_id = lcf->operation_id;
1413 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1416 &forwarded_operation_reply_relay,
1418 fopc->timeout_task =
1419 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1421 lcf->state = FINISHED;
1422 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1426 GNUNET_assert (lcfq->lcf == lcf);
1427 GNUNET_free (lcf->msg);
1429 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1431 if (NULL != lcfq_head)
1433 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1439 * Cleans up ForwardedOverlayConnectContext
1441 * @param focc the ForwardedOverlayConnectContext to cleanup
1444 cleanup_focc (struct ForwardedOverlayConnectContext *focc)
1446 GNUNET_free_non_null (focc->orig_msg);
1452 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1454 * @param rhc the RegisteredHostContext
1457 process_next_focc (struct RegisteredHostContext *rhc);
1461 * Timeout task for cancelling a forwarded overlay connect connect
1463 * @param cls the ForwardedOverlayConnectContext
1464 * @param tc the task context from the scheduler
1467 forwarded_overlay_connect_timeout (void *cls,
1468 const struct GNUNET_SCHEDULER_TaskContext
1471 struct ForwardedOperationContext *fopc = cls;
1472 struct RegisteredHostContext *rhc;
1473 struct ForwardedOverlayConnectContext *focc;
1476 focc = rhc->focc_dll_head;
1477 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1478 cleanup_focc (focc);
1479 LOG_DEBUG ("Overlay linking between peers %u and %u failed\n",
1480 focc->peer1, focc->peer2);
1481 forwarded_operation_timeout (cls, tc);
1482 if (NULL != rhc->focc_dll_head)
1483 process_next_focc (rhc);
1488 * Callback to be called when forwarded overlay connection operation has a reply
1489 * from the sub-controller successfull. We have to relay the reply msg back to
1492 * @param cls ForwardedOperationContext
1493 * @param msg the peer create success message
1496 forwarded_overlay_connect_listener (void *cls,
1497 const struct GNUNET_MessageHeader *msg)
1499 struct ForwardedOperationContext *fopc = cls;
1500 struct RegisteredHostContext *rhc;
1501 struct ForwardedOverlayConnectContext *focc;
1504 forwarded_operation_reply_relay (cls, msg);
1505 focc = rhc->focc_dll_head;
1506 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
1507 cleanup_focc (focc);
1508 if (NULL != rhc->focc_dll_head)
1509 process_next_focc (rhc);
1514 * Processes a forwarded overlay connect context in the queue of the given RegisteredHostContext
1516 * @param rhc the RegisteredHostContext
1519 process_next_focc (struct RegisteredHostContext *rhc)
1521 struct ForwardedOperationContext *fopc;
1522 struct ForwardedOverlayConnectContext *focc;
1524 focc = rhc->focc_dll_head;
1525 GNUNET_assert (NULL != focc);
1526 GNUNET_assert (RHC_OL_CONNECT == rhc->state);
1527 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1528 GNUNET_SERVER_client_keep (rhc->client);
1529 fopc->client = rhc->client;
1530 fopc->operation_id = focc->operation_id;
1533 GNUNET_TESTBED_forward_operation_msg_ (rhc->gateway->controller,
1534 focc->operation_id, focc->orig_msg,
1535 &forwarded_overlay_connect_listener,
1537 GNUNET_free (focc->orig_msg);
1538 focc->orig_msg = NULL;
1539 fopc->timeout_task =
1540 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_overlay_connect_timeout,
1546 * Callback for event from slave controllers
1548 * @param cls struct Slave *
1549 * @param event information about the event
1552 slave_event_callback (void *cls,
1553 const struct GNUNET_TESTBED_EventInformation *event)
1555 struct RegisteredHostContext *rhc;
1556 struct GNUNET_CONFIGURATION_Handle *cfg;
1557 struct GNUNET_TESTBED_Operation *old_op;
1559 /* We currently only get here when working on RegisteredHostContexts */
1560 GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
1561 rhc = event->details.operation_finished.op_cls;
1562 GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
1566 cfg = event->details.operation_finished.generic;
1567 old_op = rhc->sub_op;
1568 rhc->state = RHC_LINK;
1570 GNUNET_TESTBED_controller_link (rhc,
1571 rhc->gateway->controller,
1576 GNUNET_TESTBED_operation_done (old_op);
1579 LOG_DEBUG ("OL: Linking controllers successfull\n");
1580 GNUNET_TESTBED_operation_done (rhc->sub_op);
1582 rhc->state = RHC_OL_CONNECT;
1583 process_next_focc (rhc);
1592 * Callback to signal successfull startup of the controller process
1594 * @param cls the handle to the slave whose status is to be found here
1595 * @param cfg the configuration with which the controller has been started;
1596 * NULL if status is not GNUNET_OK
1597 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1598 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1601 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1604 struct Slave *slave = cls;
1605 struct LinkControllersContext *lcc;
1608 if (GNUNET_SYSERR == status)
1610 slave->controller_proc = NULL;
1611 slave_list[slave->host_id] = NULL;
1612 if (NULL != slave->cfg)
1613 GNUNET_CONFIGURATION_destroy (slave->cfg);
1614 GNUNET_free (slave);
1616 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1617 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1621 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1623 &slave_event_callback, slave);
1624 if (NULL != slave->controller)
1626 send_operation_success_msg (lcc->client, lcc->operation_id);
1627 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1631 send_operation_fail_msg (lcc->client, lcc->operation_id,
1632 "Could not connect to delegated controller");
1633 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1634 slave_list[slave->host_id] = NULL;
1635 GNUNET_free (slave);
1642 if (NULL != lcc->client)
1644 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1645 GNUNET_SERVER_client_drop (lcc->client);
1656 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1659 * @param client identification of the client
1660 * @param message the actual message
1663 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1664 const struct GNUNET_MessageHeader *message)
1666 const struct GNUNET_TESTBED_InitMessage *msg;
1667 struct GNUNET_TESTBED_Host *host;
1668 const char *controller_hostname;
1671 if (NULL != master_context)
1673 LOG_DEBUG ("We are being connected to laterally\n");
1674 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1677 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1678 msize = ntohs (message->size);
1679 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1682 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1685 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1686 controller_hostname = (const char *) &msg[1];
1687 if ('\0' != controller_hostname[msize - 1])
1690 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1693 master_context = GNUNET_malloc (sizeof (struct Context));
1694 master_context->client = client;
1695 master_context->host_id = ntohl (msg->host_id);
1696 master_context->master_ip = GNUNET_strdup (controller_hostname);
1697 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1698 master_context->system =
1699 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1701 GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1702 master_context->master_ip,
1705 host_list_add (host);
1706 GNUNET_SERVER_client_keep (client);
1707 LOG_DEBUG ("Created master context with host ID: %u\n",
1708 master_context->host_id);
1709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1714 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1717 * @param client identification of the client
1718 * @param message the actual message
1721 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1722 const struct GNUNET_MessageHeader *message)
1724 struct GNUNET_TESTBED_Host *host;
1725 const struct GNUNET_TESTBED_AddHostMessage *msg;
1726 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1731 uint16_t username_length;
1732 uint16_t hostname_length;
1733 uint16_t reply_size;
1736 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1737 msize = ntohs (msg->header.size);
1738 username = (char *) &msg[1];
1739 username_length = ntohs (msg->user_name_length);
1740 if (0 != username_length)
1742 /* msg must contain hostname */
1743 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1744 username_length + 1));
1745 if (0 != username_length)
1746 GNUNET_assert ('\0' == username[username_length - 1]);
1747 hostname = username + username_length;
1749 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1750 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1751 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1752 host_id = ntohl (msg->host_id);
1753 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1754 LOG_DEBUG ("-------host id: %u\n", host_id);
1755 LOG_DEBUG ("-------hostname: %s\n", hostname);
1756 if (0 != username_length)
1757 LOG_DEBUG ("-------username: %s\n", username);
1760 LOG_DEBUG ("-------username: NULL\n");
1763 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1765 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1766 ntohs (msg->ssh_port));
1767 GNUNET_assert (NULL != host);
1768 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1769 if (GNUNET_OK != host_list_add (host))
1771 /* We are unable to add a host */
1772 emsg = "A host exists with given host-id";
1773 LOG_DEBUG ("%s: %u", emsg, host_id);
1774 GNUNET_TESTBED_host_destroy (host);
1775 reply_size += strlen (emsg) + 1;
1776 reply = GNUNET_malloc (reply_size);
1777 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1781 LOG_DEBUG ("Added host %u at %u\n",
1782 host_id, master_context->host_id);
1783 reply = GNUNET_malloc (reply_size);
1785 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1786 reply->header.size = htons (reply_size);
1787 reply->host_id = htonl (host_id);
1788 queue_message (client, &reply->header);
1789 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1794 * Iterator over hash map entries.
1796 * @param cls closure
1797 * @param key current key code
1798 * @param value value in the hash map
1799 * @return GNUNET_YES if we should continue to
1804 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1806 struct SharedService *queried_ss = cls;
1807 struct SharedService *ss = value;
1809 if (0 == strcmp (ss->name, queried_ss->name))
1817 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1820 * @param client identification of the client
1821 * @param message the actual message
1824 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1825 const struct GNUNET_MessageHeader *message)
1827 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1828 struct SharedService *ss;
1830 struct GNUNET_HashCode hash;
1832 uint16_t service_name_size;
1834 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1835 msg_size = ntohs (message->size);
1836 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1843 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1844 service_name = (char *) &msg[1];
1845 if ('\0' != service_name[service_name_size - 1])
1848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1851 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1852 service_name, ntohl (msg->num_peers));
1853 if (ntohl (msg->host_id) != master_context->host_id)
1855 route_message (ntohl (msg->host_id), message);
1856 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1859 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1860 ss = GNUNET_malloc (sizeof (struct SharedService));
1861 ss->name = strdup (service_name);
1862 ss->num_shared = ntohl (msg->num_peers);
1863 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1864 if (GNUNET_SYSERR ==
1865 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1866 &ss_exists_iterator, ss))
1868 LOG (GNUNET_ERROR_TYPE_WARNING,
1869 "Service %s already configured as a shared service. "
1870 "Ignoring service sharing request \n", ss->name);
1871 GNUNET_free (ss->name);
1875 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1876 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1881 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1884 * @param client identification of the client
1885 * @param message the actual message
1888 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1889 const struct GNUNET_MessageHeader *message)
1891 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1892 struct GNUNET_CONFIGURATION_Handle *cfg;
1893 struct LCFContextQueue *lcfq;
1894 struct Route *route;
1895 struct Route *new_route;
1899 uint32_t delegated_host_id;
1900 uint32_t slave_host_id;
1903 if (NULL == master_context)
1906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1909 msize = ntohs (message->size);
1910 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1916 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1917 delegated_host_id = ntohl (msg->delegated_host_id);
1918 if (delegated_host_id == master_context->host_id)
1921 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1925 if ((delegated_host_id >= host_list_size) ||
1926 (NULL == host_list[delegated_host_id]))
1928 LOG (GNUNET_ERROR_TYPE_WARNING,
1929 "Delegated host %u not registered with us\n", delegated_host_id);
1930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1933 slave_host_id = ntohl (msg->slave_host_id);
1934 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1936 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1941 if (slave_host_id == delegated_host_id)
1943 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1948 if (slave_host_id == master_context->host_id) /* Link from us */
1950 struct Slave *slave;
1951 struct LinkControllersContext *lcc;
1953 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1954 config_size = ntohs (msg->config_size);
1955 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1957 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1962 config = GNUNET_malloc (config_size);
1963 dest_size = (uLongf) config_size;
1965 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1968 GNUNET_break (0); /* Compression error */
1969 GNUNET_free (config);
1970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1973 if (config_size != dest_size)
1975 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1976 GNUNET_free (config);
1977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1980 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1982 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1984 GNUNET_break (0); /* Configuration parsing error */
1985 GNUNET_free (config);
1986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1989 GNUNET_free (config);
1990 if ((delegated_host_id < slave_list_size) &&
1991 (NULL != slave_list[delegated_host_id]))
1993 GNUNET_break (0); /* Configuration parsing error */
1994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1997 slave = GNUNET_malloc (sizeof (struct Slave));
1998 slave->host_id = delegated_host_id;
1999 slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
2000 slave_list_add (slave);
2001 if (1 != msg->is_subordinate)
2004 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
2006 &slave_event_callback, slave);
2008 if (NULL != slave->controller)
2009 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2011 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2012 "Could not connect to delegated controller");
2013 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2016 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
2017 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
2018 GNUNET_SERVER_client_keep (client);
2019 lcc->client = client;
2021 slave->controller_proc =
2022 GNUNET_TESTBED_controller_start (master_context->master_ip,
2023 host_list[slave->host_id], cfg,
2024 &slave_status_callback, slave);
2025 GNUNET_CONFIGURATION_destroy (cfg);
2026 new_route = GNUNET_malloc (sizeof (struct Route));
2027 new_route->dest = delegated_host_id;
2028 new_route->thru = master_context->host_id;
2029 route_list_add (new_route);
2033 /* Route the request */
2034 if (slave_host_id >= route_list_size)
2036 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
2037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2040 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
2041 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
2042 lcfq->lcf->delegated_host_id = delegated_host_id;
2043 lcfq->lcf->slave_host_id = slave_host_id;
2044 route = find_dest_route (slave_host_id);
2045 GNUNET_assert (NULL != route); /* because we add routes carefully */
2046 GNUNET_assert (route->dest < slave_list_size);
2047 GNUNET_assert (NULL != slave_list[route->dest]);
2048 lcfq->lcf->state = INIT;
2049 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
2050 lcfq->lcf->gateway = slave_list[route->dest];
2051 lcfq->lcf->msg = GNUNET_malloc (msize);
2052 (void) memcpy (lcfq->lcf->msg, msg, msize);
2053 GNUNET_SERVER_client_keep (client);
2054 lcfq->lcf->client = client;
2055 if (NULL == lcfq_head)
2057 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2058 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2059 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
2062 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
2063 /* FIXME: Adding a new route should happen after the controllers are linked
2065 if (1 != msg->is_subordinate)
2067 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2070 if ((delegated_host_id < route_list_size)
2071 && (NULL != route_list[delegated_host_id]))
2073 GNUNET_break_op (0); /* Are you trying to link delegated host twice
2074 with is subordinate flag set to GNUNET_YES? */
2075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2078 new_route = GNUNET_malloc (sizeof (struct Route));
2079 new_route->dest = delegated_host_id;
2080 new_route->thru = route->dest;
2081 route_list_add (new_route);
2082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2087 * The task to be executed if the forwarded peer create operation has been
2090 * @param cls the FowardedOperationContext
2091 * @param tc the TaskContext from the scheduler
2094 peer_create_forward_timeout (void *cls,
2095 const struct GNUNET_SCHEDULER_TaskContext *tc)
2097 struct ForwardedOperationContext *fopc = cls;
2099 GNUNET_free (fopc->cls);
2100 forwarded_operation_timeout (fopc, tc);
2105 * Callback to be called when forwarded peer create operation is successfull. We
2106 * have to relay the reply msg back to the client
2108 * @param cls ForwardedOperationContext
2109 * @param msg the peer create success message
2112 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2114 struct ForwardedOperationContext *fopc = cls;
2115 struct GNUNET_MessageHeader *dup_msg;
2116 struct Peer *remote_peer;
2118 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2119 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
2121 GNUNET_assert (NULL != fopc->cls);
2122 remote_peer = fopc->cls;
2123 peer_list_add (remote_peer);
2125 dup_msg = GNUNET_copy_message (msg);
2126 queue_message (fopc->client, dup_msg);
2127 GNUNET_SERVER_client_drop (fopc->client);
2133 * Callback to be called when forwarded peer destroy operation is successfull. We
2134 * have to relay the reply msg back to the client
2136 * @param cls ForwardedOperationContext
2137 * @param msg the peer create success message
2140 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
2142 struct ForwardedOperationContext *fopc = cls;
2143 struct GNUNET_MessageHeader *dup_msg;
2144 struct Peer *remote_peer;
2146 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2147 if (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS == ntohs (msg->type))
2149 remote_peer = fopc->cls;
2150 GNUNET_assert (NULL != remote_peer);
2151 peer_list_remove (remote_peer);
2153 dup_msg = GNUNET_copy_message (msg);
2154 queue_message (fopc->client, dup_msg);
2155 GNUNET_SERVER_client_drop (fopc->client);
2162 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
2165 * @param client identification of the client
2166 * @param message the actual message
2169 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
2170 const struct GNUNET_MessageHeader *message)
2172 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
2173 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
2174 struct GNUNET_CONFIGURATION_Handle *cfg;
2175 struct ForwardedOperationContext *fo_ctxt;
2176 struct Route *route;
2181 uint32_t config_size;
2187 msize = ntohs (message->size);
2188 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
2190 GNUNET_break (0); /* We need configuration */
2191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2194 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
2195 host_id = ntohl (msg->host_id);
2196 peer_id = ntohl (msg->peer_id);
2197 if (UINT32_MAX == peer_id)
2199 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2200 "Cannot create peer with given ID");
2201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2204 if (host_id == master_context->host_id)
2208 /* We are responsible for this peer */
2209 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
2210 config_size = ntohl (msg->config_size);
2211 config = GNUNET_malloc (config_size);
2212 dest_size = config_size;
2215 uncompress ((Bytef *) config, (uLongf *) & dest_size,
2216 (const Bytef *) &msg[1], (uLong) msize)))
2218 GNUNET_break (0); /* uncompression error */
2219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2222 if (config_size != dest_size)
2224 GNUNET_break (0); /* Uncompressed config size mismatch */
2225 GNUNET_free (config);
2226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2229 cfg = GNUNET_CONFIGURATION_create ();
2231 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
2233 GNUNET_break (0); /* Configuration parsing error */
2234 GNUNET_free (config);
2235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2238 GNUNET_free (config);
2239 peer = GNUNET_malloc (sizeof (struct Peer));
2240 peer->is_remote = GNUNET_NO;
2241 peer->details.local.cfg = cfg;
2243 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
2244 peer->details.local.peer =
2245 GNUNET_TESTING_peer_configure (master_context->system,
2246 peer->details.local.cfg, peer->id,
2247 NULL /* Peer id */ ,
2249 if (NULL == peer->details.local.peer)
2251 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
2255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2258 peer->details.local.is_running = GNUNET_NO;
2259 peer_list_add (peer);
2261 GNUNET_malloc (sizeof
2262 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2263 reply->header.size =
2264 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
2265 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
2266 reply->peer_id = msg->peer_id;
2267 reply->operation_id = msg->operation_id;
2268 queue_message (client, &reply->header);
2269 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2273 /* Forward peer create request */
2274 route = find_dest_route (host_id);
2278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2282 peer = GNUNET_malloc (sizeof (struct Peer));
2283 peer->is_remote = GNUNET_YES;
2285 peer->details.remote.slave = slave_list[route->dest];
2286 peer->details.remote.remote_host_id = host_id;
2287 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2288 GNUNET_SERVER_client_keep (client);
2289 fo_ctxt->client = client;
2290 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
2291 fo_ctxt->cls = peer; //slave_list[route->dest]->controller;
2293 GNUNET_TESTBED_forward_operation_msg_ (slave_list [route->dest]->controller,
2294 fo_ctxt->operation_id,
2296 peer_create_success_cb, fo_ctxt);
2297 fo_ctxt->timeout_task =
2298 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
2300 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2305 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2308 * @param client identification of the client
2309 * @param message the actual message
2312 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
2313 const struct GNUNET_MessageHeader *message)
2315 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
2316 struct ForwardedOperationContext *fopc;
2320 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
2321 peer_id = ntohl (msg->peer_id);
2322 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
2323 peer_id, GNUNET_ntohll (msg->operation_id));
2324 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
2326 LOG (GNUNET_ERROR_TYPE_ERROR,
2327 "Asked to destroy a non existent peer with id: %u\n", peer_id);
2328 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2329 "Peer doesn't exist");
2330 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2333 peer = peer_list[peer_id];
2334 if (GNUNET_YES == peer->is_remote)
2336 /* Forward the destory message to sub controller */
2337 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2338 GNUNET_SERVER_client_keep (client);
2339 fopc->client = client;
2341 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2343 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2344 fopc->operation_id, &msg->header,
2345 &peer_destroy_success_cb,
2347 fopc->timeout_task =
2348 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2353 if (GNUNET_YES == peer->details.local.is_running)
2355 GNUNET_TESTING_peer_stop (peer->details.local.peer);
2356 peer->details.local.is_running = GNUNET_NO;
2358 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
2359 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
2360 peer_list_remove (peer);
2362 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
2363 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2368 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2371 * @param client identification of the client
2372 * @param message the actual message
2375 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
2376 const struct GNUNET_MessageHeader *message)
2378 const struct GNUNET_TESTBED_PeerStartMessage *msg;
2379 struct GNUNET_TESTBED_PeerEventMessage *reply;
2380 struct ForwardedOperationContext *fopc;
2384 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
2385 peer_id = ntohl (msg->peer_id);
2386 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2389 LOG (GNUNET_ERROR_TYPE_ERROR,
2390 "Asked to start a non existent peer with id: %u\n", peer_id);
2391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2394 peer = peer_list[peer_id];
2395 if (GNUNET_YES == peer->is_remote)
2397 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2398 GNUNET_SERVER_client_keep (client);
2399 fopc->client = client;
2400 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2402 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2403 fopc->operation_id, &msg->header,
2404 &forwarded_operation_reply_relay,
2406 fopc->timeout_task =
2407 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2409 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2412 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
2414 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2416 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2419 peer->details.local.is_running = GNUNET_YES;
2420 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2421 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2422 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2423 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
2424 reply->host_id = htonl (master_context->host_id);
2425 reply->peer_id = msg->peer_id;
2426 reply->operation_id = msg->operation_id;
2427 queue_message (client, &reply->header);
2428 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2433 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
2436 * @param client identification of the client
2437 * @param message the actual message
2440 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
2441 const struct GNUNET_MessageHeader *message)
2443 const struct GNUNET_TESTBED_PeerStopMessage *msg;
2444 struct GNUNET_TESTBED_PeerEventMessage *reply;
2445 struct ForwardedOperationContext *fopc;
2449 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
2450 peer_id = ntohl (msg->peer_id);
2451 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2453 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2455 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2458 peer = peer_list[peer_id];
2459 if (GNUNET_YES == peer->is_remote)
2461 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2462 GNUNET_SERVER_client_keep (client);
2463 fopc->client = client;
2464 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2466 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2467 fopc->operation_id, &msg->header,
2468 &forwarded_operation_reply_relay,
2470 fopc->timeout_task =
2471 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2473 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2476 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
2478 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2479 "Peer not running");
2480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2483 peer->details.local.is_running = GNUNET_NO;
2484 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2485 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2486 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2487 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2488 reply->host_id = htonl (master_context->host_id);
2489 reply->peer_id = msg->peer_id;
2490 reply->operation_id = msg->operation_id;
2491 queue_message (client, &reply->header);
2492 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2497 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2500 * @param client identification of the client
2501 * @param message the actual message
2504 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2505 const struct GNUNET_MessageHeader *message)
2507 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2508 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2517 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2518 peer_id = ntohl (msg->peer_id);
2519 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2521 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2523 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2526 peer = peer_list[peer_id];
2527 if (GNUNET_YES == peer->is_remote)
2529 struct ForwardedOperationContext *fopc;
2531 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2532 GNUNET_SERVER_client_keep (client);
2533 fopc->client = client;
2534 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
2536 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
2537 fopc->operation_id, &msg->header,
2538 &forwarded_operation_reply_relay,
2540 fopc->timeout_task =
2541 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2547 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2549 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2550 GNUNET_free (config);
2553 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2554 reply = GNUNET_realloc (xconfig, msize);
2555 (void) memmove (&reply[1], reply, xc_size);
2556 reply->header.size = htons (msize);
2557 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2558 reply->peer_id = msg->peer_id;
2559 reply->operation_id = msg->operation_id;
2560 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2561 &reply->peer_identity);
2562 reply->config_size = htons ((uint16_t) c_size);
2563 queue_message (client, &reply->header);
2564 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2569 * Cleanup overlay connect context structure
2571 * @param occ the overlay connect context
2574 cleanup_occ (struct OverlayConnectContext *occ)
2576 LOG_DEBUG ("Cleaning up occ\n");
2577 GNUNET_free_non_null (occ->emsg);
2578 GNUNET_free_non_null (occ->hello);
2579 GNUNET_SERVER_client_drop (occ->client);
2580 if (NULL != occ->opc)
2581 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2582 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2583 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2584 if (GNUNET_SCHEDULER_NO_TASK != occ->cleanup_task)
2585 GNUNET_SCHEDULER_cancel (occ->cleanup_task);
2586 if (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task)
2587 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2588 if (NULL != occ->ch)
2589 GNUNET_CORE_disconnect (occ->ch);
2590 if (NULL != occ->ghh)
2591 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2592 if (NULL != occ->p1th)
2593 GNUNET_TRANSPORT_disconnect (occ->p1th);
2594 if (NULL != occ->p2th)
2595 GNUNET_TRANSPORT_disconnect (occ->p2th);
2596 GNUNET_CONTAINER_DLL_remove (occq_head, occq_tail, occ);
2602 * Task for cleaing up overlay connect context structure
2604 * @param cls the overlay connect context
2605 * @param tc the task context
2608 do_cleanup_occ (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2610 struct OverlayConnectContext *occ = cls;
2612 occ->cleanup_task = GNUNET_SCHEDULER_NO_TASK;
2618 * Task which will be run when overlay connect request has been timed out
2620 * @param cls the OverlayConnectContext
2621 * @param tc the TaskContext
2624 timeout_overlay_connect (void *cls,
2625 const struct GNUNET_SCHEDULER_TaskContext *tc)
2627 struct OverlayConnectContext *occ = cls;
2629 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2630 LOG (GNUNET_ERROR_TYPE_WARNING,
2631 "Timeout while connecting peers %u and %u\n",
2632 occ->peer_id, occ->other_peer_id);
2633 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2640 * Function called to notify transport users that another
2641 * peer connected to us.
2643 * @param cls closure
2644 * @param new_peer the peer that connected
2645 * @param ats performance data
2646 * @param ats_count number of entries in ats (excluding 0-termination)
2649 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2650 const struct GNUNET_ATS_Information *ats,
2651 unsigned int ats_count)
2653 struct OverlayConnectContext *occ = cls;
2654 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2656 char *other_peer_str;
2658 LOG_DEBUG ("Overlay connect notify\n");
2660 memcmp (new_peer, &occ->peer_identity,
2661 sizeof (struct GNUNET_PeerIdentity)))
2663 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2664 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2666 memcmp (new_peer, &occ->other_peer_identity,
2667 sizeof (struct GNUNET_PeerIdentity)))
2669 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2670 new_peer_str, other_peer_str);
2671 GNUNET_free (new_peer_str);
2672 GNUNET_free (other_peer_str);
2675 GNUNET_free (new_peer_str);
2676 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2677 GNUNET_i2s (&occ->peer_identity));
2678 GNUNET_free (other_peer_str);
2679 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2681 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2682 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2684 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2685 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2686 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2687 GNUNET_free_non_null (occ->emsg);
2689 if (NULL != occ->p2th)
2690 GNUNET_TRANSPORT_disconnect (occ->p2th);
2692 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2693 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2695 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2696 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2697 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2698 msg->peer1 = htonl (occ->peer_id);
2699 msg->peer2 = htonl (occ->other_peer_id);
2700 msg->operation_id = GNUNET_htonll (occ->op_id);
2701 queue_message (occ->client, &msg->header);
2702 occ->cleanup_task = GNUNET_SCHEDULER_add_now (&do_cleanup_occ, occ);
2703 //cleanup_occ (occ);
2708 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2711 * @param cls the OverlayConnectContext
2712 * @param tc the TaskContext from scheduler
2715 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2717 struct OverlayConnectContext *occ = cls;
2718 char *other_peer_str;
2720 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2721 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2723 GNUNET_assert (NULL != occ->hello);
2724 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2725 if (NULL != occ->peer2_controller)
2727 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2729 uint16_t hello_size;
2731 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2732 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2733 hello_size = ntohs (occ->hello->size);
2734 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2735 msg = GNUNET_malloc (msize);
2736 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2737 msg->header.size = htons (msize);
2738 msg->peer = htonl (occ->other_peer_id);
2739 msg->operation_id = GNUNET_htonll (occ->op_id);
2740 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2741 sizeof (struct GNUNET_PeerIdentity));
2742 memcpy (msg->hello, occ->hello, hello_size);
2743 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2747 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2748 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2749 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2750 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2751 occ->send_hello_task =
2752 GNUNET_SCHEDULER_add_delayed
2753 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2754 100 * (pow (2, occ->retries++))),
2757 GNUNET_free (other_peer_str);
2761 * Test for checking whether HELLO message is empty
2763 * @param cls empty flag to set
2764 * @param address the HELLO
2765 * @param expiration expiration of the HELLO
2769 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2770 struct GNUNET_TIME_Absolute expiration)
2780 * Function called whenever there is an update to the HELLO of peers in the
2781 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2782 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2784 * @param cls closure
2785 * @param hello our updated HELLO
2788 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2790 struct OverlayConnectContext *occ = cls;
2794 msize = ntohs (hello->size);
2796 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2797 hello, GNUNET_NO, &test_address,
2799 if (GNUNET_YES == empty)
2801 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2804 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2805 occ->hello = GNUNET_malloc (msize);
2806 memcpy (occ->hello, hello, msize);
2807 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2809 GNUNET_TRANSPORT_disconnect (occ->p1th);
2811 GNUNET_free_non_null (occ->emsg);
2812 if (NULL == occ->peer2_controller)
2815 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2816 &occ->other_peer_identity, NULL, NULL, NULL,
2818 if (NULL == occ->p2th)
2820 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2821 GNUNET_i2s (&occ->other_peer_identity));
2822 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2823 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2827 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2828 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2833 * Function called after GNUNET_CORE_connect has succeeded (or failed
2834 * for good). Note that the private key of the peer is intentionally
2835 * not exposed here; if you need it, your process should try to read
2836 * the private key file directly (which should work if you are
2839 * @param cls closure
2840 * @param server handle to the server, NULL if we failed
2841 * @param my_identity ID of this peer, NULL if we failed
2844 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2845 const struct GNUNET_PeerIdentity *my_identity)
2847 struct OverlayConnectContext *occ = cls;
2849 GNUNET_free_non_null (occ->emsg);
2850 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2851 if ((NULL == server) || (NULL == my_identity))
2853 GNUNET_free (occ->emsg);
2856 memcpy (&occ->peer_identity, my_identity,
2857 sizeof (struct GNUNET_PeerIdentity));
2859 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2860 &occ->peer_identity, NULL, NULL, NULL, NULL);
2861 if (NULL == occ->p1th)
2863 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2864 GNUNET_i2s (&occ->peer_identity));
2867 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2868 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2869 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2873 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2874 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2880 * Callback to be called when forwarded get peer config operation as part of
2881 * overlay connect is successfull. Connection to Peer 1's core is made and is
2882 * checked for new connection from peer 2
2884 * @param cls ForwardedOperationContext
2885 * @param msg the peer create success message
2888 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2890 struct OverlayConnectContext *occ = cls;
2891 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2892 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2897 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2899 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2901 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2902 sizeof (struct GNUNET_PeerIdentity));
2903 GNUNET_free_non_null (occ->emsg);
2904 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2906 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2907 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2908 GNUNET_NO, no_handlers);
2909 if (NULL == occ->ch)
2914 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2916 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2921 * Callback which will be called to after a host registration succeeded or failed
2923 * @param cls the RegisteredHostContext
2924 * @param emsg the error message; NULL if host registration is successful
2927 registeredhost_registration_completion (void *cls, const char *emsg)
2929 struct RegisteredHostContext *rhc = cls;
2930 struct GNUNET_CONFIGURATION_Handle *cfg;
2931 uint32_t peer2_host_id;
2933 /* if (NULL != rhc->focc_dll_head) */
2934 /* process_next_focc (rhc); */
2935 peer2_host_id = GNUNET_TESTBED_host_get_id_ (rhc->reg_host);
2936 GNUNET_assert (RHC_INIT == rhc->state);
2937 GNUNET_assert (NULL == rhc->sub_op);
2938 if ((NULL == rhc->gateway2)
2939 || ((peer2_host_id < slave_list_size) /* Check if we have the needed config */
2940 && (NULL != slave_list[peer2_host_id])))
2942 rhc->state = RHC_LINK;
2943 cfg = (NULL == rhc->gateway2) ? our_config : slave_list[peer2_host_id]->cfg;
2945 GNUNET_TESTBED_controller_link (rhc,
2946 rhc->gateway->controller,
2953 rhc->state = RHC_GET_CFG;
2954 rhc->sub_op = GNUNET_TESTBED_get_slave_config (rhc,
2955 rhc->gateway2->controller,
2961 * Iterator to match a registered host context
2963 * @param cls pointer 2 pointer of RegisteredHostContext
2964 * @param key current key code
2965 * @param value value in the hash map
2966 * @return GNUNET_YES if we should continue to
2971 reghost_match_iterator (void *cls,
2972 const struct GNUNET_HashCode * key,
2975 struct RegisteredHostContext **rh = cls;
2976 struct RegisteredHostContext *rh_val = value;
2978 if ((rh_val->host == (*rh)->host) && (rh_val->reg_host == (*rh)->reg_host))
2989 * Function to generate the hashcode corresponding to a RegisteredHostContext
2991 * @param reg_host the host which is being registered in RegisteredHostContext
2992 * @param host the host of the controller which has to connect to the above rhost
2993 * @return the hashcode
2995 static struct GNUNET_HashCode
2996 hash_hosts (struct GNUNET_TESTBED_Host *reg_host,
2997 struct GNUNET_TESTBED_Host *host)
2999 struct GNUNET_HashCode hash;
3000 uint32_t host_ids[2];
3002 host_ids[0] = GNUNET_TESTBED_host_get_id_ (reg_host);
3003 host_ids[1] = GNUNET_TESTBED_host_get_id_ (host);
3004 GNUNET_CRYPTO_hash (host_ids, sizeof (host_ids), &hash);
3010 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
3013 * @param client identification of the client
3014 * @param message the actual message
3017 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
3018 const struct GNUNET_MessageHeader *message)
3020 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
3021 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
3025 struct OverlayConnectContext *occ;
3026 struct GNUNET_TESTBED_Controller *peer2_controller;
3027 uint64_t operation_id;
3030 uint32_t peer2_host_id;
3032 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
3033 p1 = ntohl (msg->peer1);
3034 p2 = ntohl (msg->peer2);
3035 peer2_host_id = ntohl (msg->peer2_host_id);
3036 GNUNET_assert (p1 < peer_list_size);
3037 GNUNET_assert (NULL != peer_list[p1]);
3038 peer = peer_list[p1];
3039 operation_id = GNUNET_ntohll (msg->operation_id);
3040 LOG_DEBUG ("Received overlay connect for peers %u and %u with op id: 0x%lx\n",
3041 p1, p2, operation_id);
3042 if (GNUNET_YES == peer->is_remote)
3044 struct ForwardedOperationContext *fopc;
3045 struct Route *route_to_peer2_host;
3046 struct Route *route_to_peer1_host;
3048 LOG_DEBUG ("Forwarding overlay connect\n");
3049 route_to_peer2_host = NULL;
3050 route_to_peer1_host = NULL;
3051 route_to_peer2_host = find_dest_route (peer2_host_id);
3052 if ((NULL != route_to_peer2_host)
3053 || (peer2_host_id == master_context->host_id))
3055 route_to_peer1_host =
3056 find_dest_route (peer_list[p1]->details.remote.remote_host_id);
3057 GNUNET_assert (NULL != route_to_peer1_host);
3058 if ((peer2_host_id == master_context->host_id)
3059 || (route_to_peer2_host->dest != route_to_peer1_host->dest))
3061 struct GNUNET_HashCode hash;
3062 struct RegisteredHostContext *rhc;
3065 rhc = GNUNET_malloc (sizeof (struct RegisteredHostContext));
3066 if (NULL != route_to_peer2_host)
3067 rhc->reg_host = host_list[route_to_peer2_host->dest];
3069 rhc->reg_host = host_list[master_context->host_id];
3070 rhc->host = host_list[route_to_peer1_host->dest];
3071 GNUNET_assert (NULL != rhc->reg_host);
3072 GNUNET_assert (NULL != rhc->host);
3073 rhc->gateway = peer->details.remote.slave;
3074 rhc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
3075 slave_list[route_to_peer2_host->dest];
3076 rhc->state = RHC_INIT;
3077 GNUNET_SERVER_client_keep (client);
3078 rhc->client = client;
3079 hash = hash_hosts (rhc->reg_host, rhc->host);
3080 skip_focc = GNUNET_NO;
3082 GNUNET_CONTAINER_multihashmap_contains
3083 (peer->details.remote.slave->reghost_map, &hash))
3084 || (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple
3085 (peer->details.remote.slave->reghost_map, &hash,
3086 reghost_match_iterator, &rhc)))
3088 /* create and add a new registerd host context */
3089 /* add the focc to its queue */
3090 GNUNET_CONTAINER_multihashmap_put
3091 (peer->details.remote.slave->reghost_map,
3092 &hash, rhc, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
3093 GNUNET_assert (NULL != host_list[peer2_host_id]);
3094 queue_host_registration (peer->details.remote.slave,
3095 registeredhost_registration_completion, rhc,
3096 host_list[peer2_host_id]);
3099 /* rhc is now set to the existing one from the hash map by
3100 reghost_match_iterator */
3101 /* if queue is empty then ignore creating focc and proceed with
3102 normal forwarding */
3103 if (NULL == rhc->focc_dll_head)
3104 skip_focc = GNUNET_YES;
3106 if (GNUNET_NO == skip_focc)
3108 struct ForwardedOverlayConnectContext *focc;
3111 msize = sizeof (struct GNUNET_TESTBED_OverlayConnectMessage);
3112 focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
3115 focc->peer2_host_id = peer2_host_id;
3116 focc->orig_msg = GNUNET_malloc (msize);
3117 (void) memcpy (focc->orig_msg, message, msize);
3118 focc->operation_id = operation_id;
3119 GNUNET_CONTAINER_DLL_insert_tail (rhc->focc_dll_head,
3122 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3127 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
3128 GNUNET_SERVER_client_keep (client);
3129 fopc->client = client;
3130 fopc->operation_id = operation_id;
3132 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.slave->controller,
3133 operation_id, message,
3134 &forwarded_operation_reply_relay,
3136 fopc->timeout_task =
3137 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
3139 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3143 peer2_controller = NULL;
3144 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
3146 if ((peer2_host_id >= slave_list_size)
3147 || (NULL ==slave_list[peer2_host_id]))
3149 struct GNUNET_TESTBED_NeedControllerConfig *reply;
3151 LOG_DEBUG ("Need controller configuration for connecting peers %u and %u\n",
3153 reply = GNUNET_malloc (sizeof (struct
3154 GNUNET_TESTBED_NeedControllerConfig));
3155 reply->header.size = htons (sizeof (struct
3156 GNUNET_TESTBED_NeedControllerConfig));
3157 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_NEEDCONTROLLERCONFIG);
3158 reply->controller_host_id = msg->peer2_host_id;
3159 reply->operation_id = msg->operation_id;
3160 queue_message (client, &reply->header);
3161 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3166 //occ->peer2_controller = slave_list[peer2_host_id]->controller;
3167 peer2_controller = slave_list[peer2_host_id]->controller;
3168 if (NULL == peer2_controller)
3170 GNUNET_break (0); /* What's going on? */
3171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3178 if (GNUNET_YES == peer_list[p2]->is_remote)
3179 peer2_controller = peer_list[p2]->details.remote.slave->controller;
3181 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
3182 GNUNET_CONTAINER_DLL_insert_tail (occq_head, occq_tail, occ);
3183 GNUNET_SERVER_client_keep (client);
3184 occ->client = client;
3186 occ->other_peer_id = p2;
3187 occ->peer = peer_list[p1];
3188 occ->op_id = GNUNET_ntohll (msg->operation_id);
3189 occ->peer2_controller = peer2_controller;
3190 /* Get the identity of the second peer */
3191 if (NULL != occ->peer2_controller)
3193 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
3196 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
3197 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
3198 cmsg.peer_id = msg->peer2;
3199 cmsg.operation_id = msg->operation_id;
3201 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
3202 occ->op_id, &cmsg.header,
3203 &overlay_connect_get_config,
3206 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
3208 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3209 (GNUNET_TIME_UNIT_SECONDS, 30),
3210 &timeout_overlay_connect, occ);
3211 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3214 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
3215 &occ->other_peer_identity);
3216 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
3217 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
3219 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
3220 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
3221 GNUNET_NO, no_handlers);
3222 if (NULL == occ->ch)
3224 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
3227 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3228 (GNUNET_TIME_UNIT_SECONDS, 30),
3229 &timeout_overlay_connect, occ);
3230 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3235 * Function to cleanup RequestOverlayConnectContext and any associated tasks
3238 * @param rocc the RequestOverlayConnectContext
3241 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
3243 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
3244 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
3245 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
3246 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
3247 GNUNET_TRANSPORT_disconnect (rocc->th);
3248 GNUNET_free_non_null (rocc->hello);
3254 * Task to timeout rocc and cleanit up
3256 * @param cls the RequestOverlayConnectContext
3257 * @param tc the TaskContext from scheduler
3260 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3262 struct RequestOverlayConnectContext *rocc = cls;
3264 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
3265 cleanup_rocc (rocc);
3270 * Function called to notify transport users that another
3271 * peer connected to us.
3273 * @param cls closure
3274 * @param new_peer the peer that connected
3275 * @param ats performance data
3276 * @param ats_count number of entries in ats (excluding 0-termination)
3279 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
3280 const struct GNUNET_ATS_Information * ats,
3283 struct RequestOverlayConnectContext *rocc = cls;
3285 LOG_DEBUG ("Request Overlay connect notify\n");
3286 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
3288 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
3289 cleanup_rocc (rocc);
3294 * Task to offer the HELLO message to the peer and ask it to connect to the peer
3295 * whose identity is in RequestOverlayConnectContext
3297 * @param cls the RequestOverlayConnectContext
3298 * @param tc the TaskContext from scheduler
3301 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3303 struct RequestOverlayConnectContext *rocc = cls;
3305 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
3306 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
3307 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
3308 rocc->attempt_connect_task_id =
3309 GNUNET_SCHEDULER_add_delayed
3310 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
3311 100 * (pow (2, rocc->retries++))),
3312 &attempt_connect_task, rocc);
3317 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
3320 * @param client identification of the client
3321 * @param message the actual message
3324 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
3325 const struct GNUNET_MessageHeader *message)
3327 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
3328 struct RequestOverlayConnectContext *rocc;
3334 msize = ntohs (message->size);
3335 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
3338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3341 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
3342 if ((NULL == msg->hello) ||
3343 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
3346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3349 hsize = ntohs (msg->hello->size);
3350 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
3353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3356 peer_id = ntohl (msg->peer);
3357 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
3359 GNUNET_break_op (0);
3360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3363 if (GNUNET_YES == peer->is_remote)
3365 struct GNUNET_MessageHeader *msg2;
3367 msg2 = GNUNET_malloc (msize);
3368 (void) memcpy (msg2, message, msize);
3369 GNUNET_TESTBED_queue_message_ (peer->details.remote.slave->controller, msg2);
3370 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3373 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
3374 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
3375 NULL, &transport_connect_notify, NULL);
3376 if (NULL == rocc->th)
3380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
3383 memcpy (&rocc->a_id, &msg->peer_identity,
3384 sizeof (struct GNUNET_PeerIdentity));
3385 rocc->hello = GNUNET_malloc (hsize);
3386 memcpy (rocc->hello, msg->hello, hsize);
3387 rocc->attempt_connect_task_id =
3388 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
3389 rocc->timeout_rocc_task_id =
3390 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
3391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3396 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
3399 * @param client identification of the client
3400 * @param message the actual message
3403 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
3404 const struct GNUNET_MessageHeader *message)
3406 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
3407 struct Slave *slave;
3408 struct GNUNET_TESTBED_SlaveConfiguration *reply;
3412 size_t xconfig_size;
3417 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
3418 slave_id = ntohl (msg->slave_id);
3419 op_id = GNUNET_ntohll (msg->operation_id);
3420 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
3422 send_operation_fail_msg (client, op_id, "Slave not found");
3423 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3426 slave = slave_list[slave_id];
3427 if (NULL == slave->cfg)
3429 send_operation_fail_msg (client, op_id,
3430 "Configuration not found (slave not started by me)");
3431 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3434 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
3435 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
3437 GNUNET_free (config);
3438 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
3439 GNUNET_break (reply_size <= UINT16_MAX);
3440 GNUNET_break (config_size <= UINT16_MAX);
3441 reply = GNUNET_realloc (xconfig, reply_size);
3442 (void) memmove (&reply[1], reply, xconfig_size);
3443 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
3444 reply->header.size = htons ((uint16_t) reply_size);
3445 reply->slave_id = msg->slave_id;
3446 reply->operation_id = msg->operation_id;
3447 reply->config_size = htons ((uint16_t) config_size);
3448 queue_message (client, &reply->header);
3449 GNUNET_SERVER_receive_done (client, GNUNET_OK);
3454 * Iterator over hash map entries.
3456 * @param cls closure
3457 * @param key current key code
3458 * @param value value in the hash map
3459 * @return GNUNET_YES if we should continue to
3464 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
3466 struct SharedService *ss = value;
3468 GNUNET_assert (GNUNET_YES ==
3469 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
3470 GNUNET_free (ss->name);
3477 * Iterator for freeing hash map entries in a slave's reghost_map
3479 * @param cls handle to the slave
3480 * @param key current key code
3481 * @param value value in the hash map
3482 * @return GNUNET_YES if we should continue to
3487 reghost_free_iterator (void *cls,
3488 const struct GNUNET_HashCode * key,
3491 struct Slave *slave = cls;
3492 struct RegisteredHostContext *rhc = value;
3493 struct ForwardedOverlayConnectContext *focc;
3495 GNUNET_assert (GNUNET_YES ==
3496 GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map,
3498 while (NULL != (focc = rhc->focc_dll_head))
3500 GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head,
3503 cleanup_focc (focc);
3505 if (NULL != rhc->sub_op)
3506 GNUNET_TESTBED_operation_cancel (rhc->sub_op);
3507 if (NULL != rhc->client)
3508 GNUNET_SERVER_client_drop (rhc->client);
3509 GNUNET_free (value);
3515 * Task to clean up and shutdown nicely
3518 * @param tc the TaskContext from scheduler
3521 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3523 struct LCFContextQueue *lcfq;
3524 struct OverlayConnectContext *occ;
3527 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
3528 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
3529 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
3531 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
3532 if (NULL != lcfq_head)
3534 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
3536 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
3537 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
3540 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
3541 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
3543 GNUNET_free (lcfq->lcf->msg);
3544 GNUNET_free (lcfq->lcf);
3545 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
3548 while (NULL != (occ = occq_head))
3550 /* Clear peer list */
3551 for (id = 0; id < peer_list_size; id++)
3552 if (NULL != peer_list[id])
3554 if (GNUNET_NO == peer_list[id]->is_remote)
3556 if (GNUNET_YES == peer_list[id]->details.local.is_running)
3557 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
3558 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
3559 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
3561 GNUNET_free (peer_list[id]);
3563 GNUNET_free_non_null (peer_list);
3564 /* Clear host list */
3565 for (id = 0; id < host_list_size; id++)
3566 if (NULL != host_list[id])
3567 GNUNET_TESTBED_host_destroy (host_list[id]);
3568 GNUNET_free_non_null (host_list);
3569 /* Clear route list */
3570 for (id = 0; id < route_list_size; id++)
3571 if (NULL != route_list[id])
3572 GNUNET_free (route_list[id]);
3573 GNUNET_free_non_null (route_list);
3574 /* Clear slave_list */
3575 for (id = 0; id < slave_list_size; id++)
3576 if (NULL != slave_list[id])
3578 struct HostRegistration *hr_entry;
3580 while (NULL != (hr_entry = slave_list[id]->hr_dll_head))
3582 GNUNET_CONTAINER_DLL_remove (slave_list[id]->hr_dll_head,
3583 slave_list[id]->hr_dll_tail,
3585 GNUNET_free (hr_entry);
3587 if (NULL != slave_list[id]->rhandle)
3588 GNUNET_TESTBED_cancel_registration (slave_list[id]->rhandle);
3589 (void) GNUNET_CONTAINER_multihashmap_iterate (slave_list[id]->reghost_map,
3590 reghost_free_iterator,
3592 GNUNET_CONTAINER_multihashmap_destroy (slave_list[id]->reghost_map);
3593 if (NULL != slave_list[id]->cfg)
3594 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
3595 if (NULL != slave_list[id]->controller)
3596 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
3597 if (NULL != slave_list[id]->controller_proc)
3598 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
3599 GNUNET_free (slave_list[id]);
3601 GNUNET_free_non_null (slave_list);
3602 if (NULL != master_context)
3604 GNUNET_free_non_null (master_context->master_ip);
3605 if (NULL != master_context->system)
3606 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
3607 GNUNET_free (master_context);
3608 master_context = NULL;
3610 GNUNET_free_non_null (hostname);
3611 GNUNET_CONFIGURATION_destroy (our_config);
3616 * Callback for client disconnect
3619 * @param client the client which has disconnected
3622 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
3624 if (NULL == master_context)
3626 if (client == master_context->client)
3628 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
3629 GNUNET_SERVER_client_drop (client);
3630 /* should not be needed as we're terminated by failure to read
3631 * from stdin, but if stdin fails for some reason, this shouldn't
3632 * hurt for now --- might need to revise this later if we ever
3633 * decide that master connections might be temporarily down
3634 * for some reason */
3635 //GNUNET_SCHEDULER_shutdown ();
3643 * @param cls closure
3644 * @param server the initialized server
3645 * @param cfg configuration to use
3648 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
3649 const struct GNUNET_CONFIGURATION_Handle *cfg)
3651 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
3652 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
3653 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
3654 {&handle_configure_shared_service, NULL,
3655 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
3656 {&handle_link_controllers, NULL,
3657 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
3658 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
3659 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
3660 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
3661 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
3662 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
3663 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
3664 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
3665 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
3666 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
3667 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
3668 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
3669 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
3671 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
3672 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
3676 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
3677 (cfg, "testbed", "HOSTNAME", &hostname));
3678 our_config = GNUNET_CONFIGURATION_dup (cfg);
3679 GNUNET_SERVER_add_handlers (server, message_handlers);
3680 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
3681 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
3683 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
3684 GNUNET_SCHEDULER_PRIORITY_IDLE,
3685 &shutdown_task, NULL);
3686 LOG_DEBUG ("Testbed startup complete\n");
3687 event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
3692 * The starting point of execution
3695 main (int argc, char *const *argv)
3697 //sleep (15); /* Debugging */
3698 return (GNUNET_OK ==
3699 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
3700 &testbed_run, NULL)) ? 0 : 1;
3703 /* end of gnunet-service-testbed.c */