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 * Event mask of event to be responded in this context
89 * Our host id according to this context
96 * The message queue for sending messages to clients
101 * The message to be sent
103 struct GNUNET_MessageHeader *msg;
106 * The client to send the message to
108 struct GNUNET_SERVER_Client *client;
111 * next pointer for DLL
113 struct MessageQueue *next;
116 * prev pointer for DLL
118 struct MessageQueue *prev;
123 * The structure for identifying a shared service
128 * The name of the shared service
133 * Number of shared peers per instance of the shared service
138 * Number of peers currently sharing the service
140 uint32_t num_sharing;
155 * The destination host is reachable thru
162 * Context information used while linking controllers
164 struct LinkControllersContext;
168 * Structure representing a connected(directly-linked) controller
173 * The controller process handle if we had started the controller
175 struct GNUNET_TESTBED_ControllerProc *controller_proc;
178 * The controller handle
180 struct GNUNET_TESTBED_Controller *controller;
183 * The configuration of the slave. Will be NULL for slave which we didn't
186 struct GNUNET_CONFIGURATION_Handle *cfg;
189 * handle to lcc which is associated with this slave startup. Should be set to
190 * NULL when the slave has successfully started up
192 struct LinkControllersContext *lcc;
195 * The id of the host this controller is running on
202 * States of LCFContext
207 * The Context has been initialized; Nothing has been done on it
212 * Delegated host has been registered at the forwarding controller
214 DELEGATED_HOST_REGISTERED,
217 * The slave host has been registred at the forwarding controller
219 SLAVE_HOST_REGISTERED,
222 * The context has been finished (may have error)
229 * Link controllers request forwarding context
234 * The gateway which will pass the link message to delegated host
236 struct Slave *gateway;
239 * The controller link message that has to be forwarded to
241 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
244 * The client which has asked to perform this operation
246 struct GNUNET_SERVER_Client *client;
249 * The host registration handle while registered hosts in this context
251 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
254 * The id of the operation which created this context
256 uint64_t operation_id;
259 * The state of this context
261 enum LCFContextState state;
266 uint32_t delegated_host_id;
271 uint32_t slave_host_id;
277 * Structure of a queue entry in LCFContext request queue
279 struct LCFContextQueue
284 struct LCFContext *lcf;
289 struct LCFContextQueue *next;
294 struct LCFContextQueue *prev;
299 * A locally started peer
308 * The peer handle from testing API
310 struct GNUNET_TESTING_Peer *peer;
313 * The modified (by GNUNET_TESTING_peer_configure) configuration this
314 * peer is configured with
316 struct GNUNET_CONFIGURATION_Handle *cfg;
319 * Is the peer running
328 * The controller this peer is started through
330 struct GNUNET_TESTBED_Controller *controller;
337 * Is this peer locally created?
342 * Our local reference id for this peer
350 * Context information for connecting 2 peers in overlay
352 struct OverlayConnectContext
355 * The client which has requested for overlay connection
357 struct GNUNET_SERVER_Client *client;
360 * the peer which has to connect to the other peer
365 * Transport handle of the first peer to get its HELLO
367 struct GNUNET_TRANSPORT_Handle *p1th;
370 * Transport handle of other peer to offer first peer's HELLO
372 struct GNUNET_TRANSPORT_Handle *p2th;
375 * Core handles of the first peer; used to notify when second peer connects to it
377 struct GNUNET_CORE_Handle *ch;
380 * HELLO of the other peer
382 struct GNUNET_MessageHeader *hello;
385 * Get hello handle to acquire HELLO of first peer
387 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
390 * The error message we send if this overlay connect operation has timed out
395 * Operation context for suboperations
397 struct OperationContext *opc;
400 * Controller of peer 2; NULL if the peer is local
402 struct GNUNET_TESTBED_Controller *peer2_controller;
405 * The peer identity of the first peer
407 struct GNUNET_PeerIdentity peer_identity;
410 * The peer identity of the other peer
412 struct GNUNET_PeerIdentity other_peer_identity;
415 * The id of the operation responsible for creating this context
420 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
423 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
426 * The id of the overlay connect timeout task
428 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
438 uint32_t other_peer_id;
441 * Number of times we tried to send hello; used to increase delay in offering
449 * Context information for RequestOverlayConnect
450 * operations. RequestOverlayConnect is used when peers A, B reside on different
451 * hosts and the host controller for peer B is asked by the host controller of
452 * peer A to make peer B connect to peer A
454 struct RequestOverlayConnectContext
457 * The transport handle of peer B
459 struct GNUNET_TRANSPORT_Handle *th;
464 struct GNUNET_MessageHeader *hello;
467 * The peer identity of peer A
469 struct GNUNET_PeerIdentity a_id;
472 * Task for offering HELLO of A to B and doing try_connect
474 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
477 * Task to timeout RequestOverlayConnect
479 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
482 * Number of times we tried to send hello; used to increase delay in offering
491 * Context information for operations forwarded to subcontrollers
493 struct ForwardedOperationContext
496 * The generated operation context
498 struct OperationContext *opc;
501 * The client to which we have to reply
503 struct GNUNET_SERVER_Client *client;
511 * Task ID for the timeout task
513 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
516 * The id of the operation that has been forwarded
518 uint64_t operation_id;
524 * Context information used while linking controllers
526 struct LinkControllersContext
529 * The client which initiated the link controller operation
531 struct GNUNET_SERVER_Client *client;
534 * The ID of the operation
536 uint64_t operation_id;
543 * The master context; generated with the first INIT message
545 static struct Context *master_context;
548 * Our hostname; we give this to all the peers we start
550 static char *hostname;
557 * Current Transmit Handle; NULL if no notify transmit exists currently
559 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
566 * The head for the LCF queue
568 static struct LCFContextQueue *lcfq_head;
571 * The tail for the LCF queue
573 static struct LCFContextQueue *lcfq_tail;
576 * The message queue head
578 static struct MessageQueue *mq_head;
581 * The message queue tail
583 static struct MessageQueue *mq_tail;
588 static struct GNUNET_TESTBED_Host **host_list;
593 static struct Route **route_list;
596 * A list of directly linked neighbours
598 static struct Slave **slave_list;
601 * A list of peers we own locally
603 static struct Peer **peer_list;
606 * The hashmap of shared services
608 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
611 * The size of the host list
613 static uint32_t host_list_size;
616 * The size of the route list
618 static uint32_t route_list_size;
621 * The size of directly linked neighbours list
623 static uint32_t slave_list_size;
626 * The size of the peer list
628 static uint32_t peer_list_size;
635 * The lcf_task handle
637 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
640 * The shutdown task handle
642 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
646 * Function called to notify a client about the connection begin ready to queue
647 * more data. "buf" will be NULL and "size" zero if the connection was closed
648 * for writing in the meantime.
651 * @param size number of bytes available in buf
652 * @param buf where the callee should write the message
653 * @return number of bytes written to buf
656 transmit_ready_notify (void *cls, size_t size, void *buf)
658 struct MessageQueue *mq_entry;
660 transmit_handle = NULL;
662 GNUNET_assert (NULL != mq_entry);
665 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
666 size = ntohs (mq_entry->msg->size);
667 memcpy (buf, mq_entry->msg, size);
668 GNUNET_free (mq_entry->msg);
669 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
670 GNUNET_free (mq_entry);
672 if (NULL != mq_entry)
674 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
675 ntohs (mq_entry->msg->size),
676 GNUNET_TIME_UNIT_FOREVER_REL,
677 &transmit_ready_notify, NULL);
683 * Queues a message in send queue for sending to the service
685 * @param client the client to whom the queued message has to be sent
686 * @param msg the message to queue
689 queue_message (struct GNUNET_SERVER_Client *client,
690 struct GNUNET_MessageHeader *msg)
692 struct MessageQueue *mq_entry;
696 type = ntohs (msg->type);
697 size = ntohs (msg->size);
698 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
699 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
700 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
702 mq_entry->client = client;
703 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
705 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
706 if (NULL == transmit_handle)
708 GNUNET_SERVER_notify_transmit_ready (client, size,
709 GNUNET_TIME_UNIT_FOREVER_REL,
710 &transmit_ready_notify, NULL);
715 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
717 * @param ptr the memory block to realloc
718 * @param size the size of ptr
719 * @param new_size the size to which ptr has to be realloc'ed
720 * @return the newly reallocated memory block
723 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
725 ptr = GNUNET_realloc (ptr, new_size);
727 (void) memset (ptr + size, 0, new_size - size);
733 * Function to add a host to the current list of known hosts
735 * @param host the host to add
736 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
740 host_list_add (struct GNUNET_TESTBED_Host *host)
745 host_id = GNUNET_TESTBED_host_get_id_ (host);
746 orig_size = host_list_size;
747 if (host_list_size <= host_id)
749 while (host_list_size <= host_id)
750 host_list_size += LIST_GROW_STEP;
752 TESTBED_realloc (host_list,
753 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
754 sizeof (struct GNUNET_TESTBED_Host *)
757 if (NULL != host_list[host_id])
759 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
760 return GNUNET_SYSERR;
762 host_list[host_id] = host;
768 * Adds a route to the route list
770 * @param route the route to add
773 route_list_add (struct Route *route)
777 orig_size = route_list_size;
778 if (route->dest >= route_list_size)
780 while (route->dest >= route_list_size)
781 route_list_size += LIST_GROW_STEP;
783 TESTBED_realloc (route_list,
784 sizeof (struct Route *) * orig_size,
785 sizeof (struct Route *) * route_list_size);
787 GNUNET_assert (NULL == route_list[route->dest]);
788 route_list[route->dest] = route;
793 * Adds a slave to the slave array
795 * @param slave the slave controller to add
798 slave_list_add (struct Slave *slave)
800 if (slave->host_id >= slave_list_size)
803 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
804 sizeof (struct Slave *) * (slave_list_size +
806 slave_list_size += LIST_GROW_STEP;
808 GNUNET_assert (NULL == slave_list[slave->host_id]);
809 slave_list[slave->host_id] = slave;
814 * Adds a peer to the peer array
816 * @param peer the peer to add
819 peer_list_add (struct Peer *peer)
823 orig_size = peer_list_size;
824 if (peer->id >= peer_list_size)
826 while (peer->id >= peer_list_size)
827 peer_list_size += LIST_GROW_STEP;
829 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
830 sizeof (struct Peer *) * peer_list_size);
832 GNUNET_assert (NULL == peer_list[peer->id]);
833 peer_list[peer->id] = peer;
838 * Removes a the give peer from the peer array
840 * @param peer the peer to be removed
843 peer_list_remove (struct Peer *peer)
848 peer_list[peer->id] = NULL;
849 orig_size = peer_list_size;
850 while (peer_list_size >= LIST_GROW_STEP)
852 for (id = peer_list_size - 1;
853 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
854 if (NULL != peer_list[id])
856 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
858 peer_list_size -= LIST_GROW_STEP;
860 if (orig_size == peer_list_size)
863 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
868 * Finds the route with directly connected host as destination through which
869 * the destination host can be reached
871 * @param host_id the id of the destination host
872 * @return the route with directly connected destination host; NULL if no route
875 static struct Route *
876 find_dest_route (uint32_t host_id)
880 while (NULL != (route = route_list[host_id]))
882 if (route->thru == master_context->host_id)
884 host_id = route->thru;
891 * Routes message to a host given its host_id
893 * @param host_id the id of the destination host
894 * @param msg the message to be routed
897 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
904 * Send operation failure message to client
906 * @param client the client to which the failure message has to be sent to
907 * @param operation_id the id of the failed operation
908 * @param emsg the error message; can be NULL
911 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
912 uint64_t operation_id, const char *emsg)
914 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
918 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
919 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
921 msg = GNUNET_malloc (msize);
922 msg->header.size = htons (msize);
923 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
924 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
925 msg->operation_id = GNUNET_htonll (operation_id);
927 memcpy (&msg[1], emsg, emsg_len);
928 queue_message (client, &msg->header);
933 * Function to send generic operation success message to given client
935 * @param client the client to send the message to
936 * @param operation_id the id of the operation which was successful
939 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
940 uint64_t operation_id)
942 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
945 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
946 msg = GNUNET_malloc (msize);
947 msg->header.size = htons (msize);
948 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
949 msg->operation_id = GNUNET_htonll (operation_id);
950 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
951 queue_message (client, &msg->header);
956 * The Link Controller forwarding task
958 * @param cls the LCFContext
959 * @param tc the Task context from scheduler
962 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
966 * Completion callback for host registrations while forwarding Link Controller messages
968 * @param cls the LCFContext
969 * @param emsg the error message; NULL if host registration is successful
972 lcf_proc_cc (void *cls, const char *emsg)
974 struct LCFContext *lcf = cls;
977 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
982 goto registration_error;
983 lcf->state = DELEGATED_HOST_REGISTERED;
984 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
986 case DELEGATED_HOST_REGISTERED:
988 goto registration_error;
989 lcf->state = SLAVE_HOST_REGISTERED;
990 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
993 GNUNET_assert (0); /* Shouldn't reach here */
998 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
1000 lcf->state = FINISHED;
1001 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1006 * Callback to be called when forwarded link controllers operation is
1007 * successfull. We have to relay the reply msg back to the client
1009 * @param cls ForwardedOperationContext
1010 * @param msg the peer create success message
1013 forwarded_operation_reply_relay (void *cls,
1014 const struct GNUNET_MessageHeader *msg)
1016 struct ForwardedOperationContext *fopc = cls;
1017 struct GNUNET_MessageHeader *dup_msg;
1020 msize = ntohs (msg->size);
1021 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1023 dup_msg = GNUNET_malloc (msize);
1024 (void) memcpy (dup_msg, msg, msize);
1025 queue_message (fopc->client, dup_msg);
1026 GNUNET_SERVER_client_drop (fopc->client);
1027 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1033 * Task to free resources when forwarded link controllers has been timedout
1035 * @param cls the ForwardedOperationContext
1036 * @param tc the task context from scheduler
1039 forwarded_operation_timeout (void *cls,
1040 const struct GNUNET_SCHEDULER_TaskContext *tc)
1042 struct ForwardedOperationContext *fopc = cls;
1044 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1045 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1046 GNUNET_SERVER_client_drop (fopc->client);
1052 * The Link Controller forwarding task
1054 * @param cls the LCFContext
1055 * @param tc the Task context from scheduler
1058 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1060 struct LCFContext *lcf = cls;
1061 struct LCFContextQueue *lcfq;
1062 struct ForwardedOperationContext *fopc;
1064 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1069 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1070 lcf->gateway->controller))
1073 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1074 host_list[lcf->delegated_host_id],
1079 lcf->state = DELEGATED_HOST_REGISTERED;
1080 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1083 case DELEGATED_HOST_REGISTERED:
1085 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1086 lcf->gateway->controller))
1089 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1090 host_list[lcf->slave_host_id],
1095 lcf->state = SLAVE_HOST_REGISTERED;
1096 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1099 case SLAVE_HOST_REGISTERED:
1100 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1101 fopc->client = lcf->client;
1102 fopc->operation_id = lcf->operation_id;
1104 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1107 &forwarded_operation_reply_relay,
1109 fopc->timeout_task =
1110 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1112 lcf->state = FINISHED;
1113 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1117 GNUNET_assert (lcfq->lcf == lcf);
1118 GNUNET_free (lcf->msg);
1120 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1122 if (NULL != lcfq_head)
1124 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1130 * Callback for event from slave controllers
1132 * @param cls struct Slave *
1133 * @param event information about the event
1136 slave_event_callback (void *cls,
1137 const struct GNUNET_TESTBED_EventInformation *event)
1144 * Callback to signal successfull startup of the controller process
1146 * @param cls the handle to the slave whose status is to be found here
1147 * @param cfg the configuration with which the controller has been started;
1148 * NULL if status is not GNUNET_OK
1149 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1150 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1153 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1156 struct Slave *slave = cls;
1157 struct LinkControllersContext *lcc;
1160 if (GNUNET_SYSERR == status)
1162 slave->controller_proc = NULL;
1163 slave_list[slave->host_id] = NULL;
1164 if (NULL != slave->cfg)
1165 GNUNET_CONFIGURATION_destroy (slave->cfg);
1166 GNUNET_free (slave);
1168 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1169 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1173 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1174 master_context->event_mask,
1175 &slave_event_callback, slave);
1176 if (NULL != slave->controller)
1178 send_operation_success_msg (lcc->client, lcc->operation_id);
1179 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1183 send_operation_fail_msg (lcc->client, lcc->operation_id,
1184 "Could not connect to delegated controller");
1185 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1186 slave_list[slave->host_id] = NULL;
1187 GNUNET_free (slave);
1194 if (NULL != lcc->client)
1196 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1197 GNUNET_SERVER_client_drop (lcc->client);
1208 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1211 * @param client identification of the client
1212 * @param message the actual message
1215 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1216 const struct GNUNET_MessageHeader *message)
1218 const struct GNUNET_TESTBED_InitMessage *msg;
1219 struct GNUNET_TESTBED_Host *host;
1220 const char *controller_hostname;
1223 if (NULL != master_context)
1225 LOG_DEBUG ("We are being connected to laterally\n");
1226 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1229 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1230 msize = ntohs (message->size);
1231 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1237 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1238 controller_hostname = (const char *) &msg[1];
1239 if ('\0' != controller_hostname[msize - 1])
1242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1245 master_context = GNUNET_malloc (sizeof (struct Context));
1246 master_context->client = client;
1247 master_context->host_id = ntohl (msg->host_id);
1248 master_context->master_ip = GNUNET_strdup (controller_hostname);
1249 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1250 master_context->system =
1251 GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
1253 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1255 host_list_add (host);
1256 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1257 GNUNET_SERVER_client_keep (client);
1258 LOG_DEBUG ("Created master context with host ID: %u\n",
1259 master_context->host_id);
1260 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1265 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1268 * @param client identification of the client
1269 * @param message the actual message
1272 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1273 const struct GNUNET_MessageHeader *message)
1275 struct GNUNET_TESTBED_Host *host;
1276 const struct GNUNET_TESTBED_AddHostMessage *msg;
1277 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1282 uint16_t username_length;
1283 uint16_t hostname_length;
1284 uint16_t reply_size;
1287 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1288 msize = ntohs (msg->header.size);
1289 username = (char *) &(msg[1]);
1290 username_length = ntohs (msg->user_name_length);
1291 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1292 if (0 != username_length)
1293 GNUNET_assert ('\0' == username[username_length]);
1294 username_length = (0 == username_length) ? 0 : username_length + 1;
1295 hostname = username + username_length;
1297 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1298 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1299 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1300 host_id = ntohl (msg->host_id);
1301 LOG_DEBUG ("Received ADDHOST message\n");
1302 LOG_DEBUG ("-------host id: %u\n", host_id);
1303 LOG_DEBUG ("-------hostname: %s\n", hostname);
1304 if (0 != username_length)
1305 LOG_DEBUG ("-------username: %s\n", username);
1308 LOG_DEBUG ("-------username: NULL\n");
1311 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1313 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1314 ntohs (msg->ssh_port));
1315 GNUNET_assert (NULL != host);
1316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1317 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1318 if (GNUNET_OK != host_list_add (host))
1320 /* We are unable to add a host */
1321 emsg = "A host exists with given host-id";
1322 LOG_DEBUG ("%s: %u", emsg, host_id);
1323 GNUNET_TESTBED_host_destroy (host);
1324 reply_size += strlen (emsg) + 1;
1325 reply = GNUNET_malloc (reply_size);
1326 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1329 reply = GNUNET_malloc (reply_size);
1330 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1331 reply->header.size = htons (reply_size);
1332 reply->host_id = htonl (host_id);
1333 queue_message (client, &reply->header);
1338 * Iterator over hash map entries.
1340 * @param cls closure
1341 * @param key current key code
1342 * @param value value in the hash map
1343 * @return GNUNET_YES if we should continue to
1348 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1350 struct SharedService *queried_ss = cls;
1351 struct SharedService *ss = value;
1353 if (0 == strcmp (ss->name, queried_ss->name))
1361 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1364 * @param client identification of the client
1365 * @param message the actual message
1368 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1369 const struct GNUNET_MessageHeader *message)
1371 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1372 struct SharedService *ss;
1374 struct GNUNET_HashCode hash;
1376 uint16_t service_name_size;
1378 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1379 msg_size = ntohs (message->size);
1380 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1387 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1388 service_name = (char *) &msg[1];
1389 if ('\0' != service_name[service_name_size - 1])
1392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1395 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1396 service_name, ntohl (msg->num_peers));
1397 if (ntohl (msg->host_id) != master_context->host_id)
1399 route_message (ntohl (msg->host_id), message);
1400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1403 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1404 ss = GNUNET_malloc (sizeof (struct SharedService));
1405 ss->name = strdup (service_name);
1406 ss->num_shared = ntohl (msg->num_peers);
1407 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1408 if (GNUNET_SYSERR ==
1409 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1410 &ss_exists_iterator, ss))
1412 LOG (GNUNET_ERROR_TYPE_WARNING,
1413 "Service %s already configured as a shared service. "
1414 "Ignoring service sharing request \n", ss->name);
1415 GNUNET_free (ss->name);
1419 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1425 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1428 * @param client identification of the client
1429 * @param message the actual message
1432 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1433 const struct GNUNET_MessageHeader *message)
1435 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1436 struct GNUNET_CONFIGURATION_Handle *cfg;
1437 struct LCFContextQueue *lcfq;
1438 struct Route *route;
1439 struct Route *new_route;
1443 uint32_t delegated_host_id;
1444 uint32_t slave_host_id;
1447 if (NULL == master_context)
1450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1453 msize = ntohs (message->size);
1454 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1460 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1461 delegated_host_id = ntohl (msg->delegated_host_id);
1462 if (delegated_host_id == master_context->host_id)
1465 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1469 if ((delegated_host_id >= host_list_size) ||
1470 (NULL == host_list[delegated_host_id]))
1472 LOG (GNUNET_ERROR_TYPE_WARNING,
1473 "Delegated host %u not registered with us\n", delegated_host_id);
1474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1477 slave_host_id = ntohl (msg->slave_host_id);
1478 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1480 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1484 if (slave_host_id == delegated_host_id)
1486 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1491 if (slave_host_id == master_context->host_id) /* Link from us */
1493 struct Slave *slave;
1494 struct LinkControllersContext *lcc;
1496 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1497 config_size = ntohs (msg->config_size);
1498 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1500 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1505 config = GNUNET_malloc (config_size);
1506 dest_size = (uLongf) config_size;
1508 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1511 GNUNET_break (0); /* Compression error */
1512 GNUNET_free (config);
1513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1516 if (config_size != dest_size)
1518 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1519 GNUNET_free (config);
1520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1523 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1525 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1527 GNUNET_break (0); /* Configuration parsing error */
1528 GNUNET_free (config);
1529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1532 GNUNET_free (config);
1533 if ((delegated_host_id < slave_list_size) &&
1534 (NULL != slave_list[delegated_host_id]))
1536 GNUNET_break (0); /* Configuration parsing error */
1537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1540 slave = GNUNET_malloc (sizeof (struct Slave));
1541 slave->host_id = delegated_host_id;
1542 slave_list_add (slave);
1543 if (1 != msg->is_subordinate)
1546 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1547 master_context->event_mask,
1548 &slave_event_callback, slave);
1549 GNUNET_CONFIGURATION_destroy (cfg);
1550 if (NULL != slave->controller)
1551 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1553 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1554 "Could not connect to delegated controller");
1555 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1558 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1559 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1560 GNUNET_SERVER_client_keep (client);
1561 lcc->client = client;
1563 slave->controller_proc =
1564 GNUNET_TESTBED_controller_start (master_context->master_ip,
1565 host_list[slave->host_id], cfg,
1566 &slave_status_callback, slave);
1567 GNUNET_CONFIGURATION_destroy (cfg);
1568 new_route = GNUNET_malloc (sizeof (struct Route));
1569 new_route->dest = delegated_host_id;
1570 new_route->thru = master_context->host_id;
1571 route_list_add (new_route);
1575 /* Route the request */
1576 if (slave_host_id >= route_list_size)
1578 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1582 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1583 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1584 lcfq->lcf->delegated_host_id = delegated_host_id;
1585 lcfq->lcf->slave_host_id = slave_host_id;
1586 route = find_dest_route (slave_host_id);
1587 GNUNET_assert (NULL != route); /* because we add routes carefully */
1588 GNUNET_assert (route->dest < slave_list_size);
1589 GNUNET_assert (NULL != slave_list[route->dest]);
1590 lcfq->lcf->state = INIT;
1591 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1592 lcfq->lcf->gateway = slave_list[route->dest];
1593 lcfq->lcf->msg = GNUNET_malloc (msize);
1594 (void) memcpy (lcfq->lcf->msg, msg, msize);
1595 GNUNET_SERVER_client_keep (client);
1596 lcfq->lcf->client = client;
1597 if (NULL == lcfq_head)
1599 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1600 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1601 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1604 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1605 /* FIXME: Adding a new route should happen after the controllers are linked
1607 if (1 != msg->is_subordinate)
1609 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1612 if ((delegated_host_id < route_list_size)
1613 && (NULL != route_list[delegated_host_id]))
1615 GNUNET_break_op (0); /* Are you trying to link delegated host twice
1616 with is subordinate flag set to GNUNET_YES? */
1617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1620 new_route = GNUNET_malloc (sizeof (struct Route));
1621 new_route->dest = delegated_host_id;
1622 new_route->thru = route->dest;
1623 route_list_add (new_route);
1624 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1629 * The task to be executed if the forwarded peer create operation has been
1632 * @param cls the FowardedOperationContext
1633 * @param tc the TaskContext from the scheduler
1636 peer_create_forward_timeout (void *cls,
1637 const struct GNUNET_SCHEDULER_TaskContext *tc)
1639 struct ForwardedOperationContext *fo_ctxt = cls;
1641 /* send error msg to client */
1642 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1643 GNUNET_SERVER_client_drop (fo_ctxt->client);
1644 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1645 GNUNET_free (fo_ctxt);
1650 * Callback to be called when forwarded peer create operation is
1651 * successfull. We have to relay the reply msg back to the client
1653 * @param cls ForwardedOperationContext
1654 * @param msg the peer create success message
1657 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1659 struct ForwardedOperationContext *fo_ctxt = cls;
1660 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1661 struct GNUNET_MessageHeader *dup_msg;
1665 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1666 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1669 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1670 peer = GNUNET_malloc (sizeof (struct Peer));
1671 peer->is_remote = GNUNET_YES;
1672 peer->id = ntohl (success_msg->peer_id);
1673 GNUNET_assert (NULL != fo_ctxt->cls);
1674 peer->details.remote.controller = fo_ctxt->cls;
1675 peer_list_add (peer);
1677 msize = ntohs (msg->size);
1678 dup_msg = GNUNET_malloc (msize);
1679 (void) memcpy (dup_msg, msg, msize);
1680 queue_message (fo_ctxt->client, dup_msg);
1681 GNUNET_SERVER_client_drop (fo_ctxt->client);
1682 GNUNET_free (fo_ctxt);
1688 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1691 * @param client identification of the client
1692 * @param message the actual message
1695 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1696 const struct GNUNET_MessageHeader *message)
1698 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1699 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1700 struct GNUNET_CONFIGURATION_Handle *cfg;
1701 struct ForwardedOperationContext *fo_ctxt;
1702 struct Route *route;
1707 uint32_t config_size;
1713 msize = ntohs (message->size);
1714 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1716 GNUNET_break (0); /* We need configuration */
1717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1720 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1721 host_id = ntohl (msg->host_id);
1722 peer_id = ntohl (msg->peer_id);
1723 if (UINT32_MAX == peer_id)
1725 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1726 "Cannot create peer with given ID");
1727 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1730 if (host_id == master_context->host_id)
1734 /* We are responsible for this peer */
1735 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1736 config_size = ntohl (msg->config_size);
1737 config = GNUNET_malloc (config_size);
1738 dest_size = config_size;
1741 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1742 (const Bytef *) &msg[1], (uLong) msize)))
1744 GNUNET_break (0); /* uncompression error */
1745 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1748 if (config_size != dest_size)
1750 GNUNET_break (0); /* Uncompressed config size mismatch */
1751 GNUNET_free (config);
1752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1755 cfg = GNUNET_CONFIGURATION_create ();
1757 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1759 GNUNET_break (0); /* Configuration parsing error */
1760 GNUNET_free (config);
1761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1764 GNUNET_free (config);
1765 peer = GNUNET_malloc (sizeof (struct Peer));
1766 peer->is_remote = GNUNET_NO;
1767 peer->details.local.cfg = cfg;
1769 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1770 peer->details.local.peer =
1771 GNUNET_TESTING_peer_configure (master_context->system,
1772 peer->details.local.cfg, peer->id,
1773 NULL /* Peer id */ ,
1775 if (NULL == peer->details.local.peer)
1777 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1784 peer->details.local.is_running = GNUNET_NO;
1785 peer_list_add (peer);
1787 GNUNET_malloc (sizeof
1788 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1789 reply->header.size =
1790 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1791 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1792 reply->peer_id = msg->peer_id;
1793 reply->operation_id = msg->operation_id;
1794 queue_message (client, &reply->header);
1795 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1799 /* Forward peer create request */
1800 route = find_dest_route (host_id);
1804 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1807 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1808 GNUNET_SERVER_client_keep (client);
1809 fo_ctxt->client = client;
1810 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1811 fo_ctxt->cls = slave_list[route->dest]->controller;
1813 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1814 [route->dest]->controller,
1815 fo_ctxt->operation_id,
1817 peer_create_success_cb, fo_ctxt);
1818 fo_ctxt->timeout_task =
1819 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1827 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1830 * @param client identification of the client
1831 * @param message the actual message
1834 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1835 const struct GNUNET_MessageHeader *message)
1837 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1838 struct ForwardedOperationContext *fopc;
1842 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1843 peer_id = ntohl (msg->peer_id);
1844 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1845 peer_id, GNUNET_ntohll (msg->operation_id));
1846 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1848 LOG (GNUNET_ERROR_TYPE_ERROR,
1849 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1850 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1851 "Peer doesn't exist");
1852 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1855 peer = peer_list[peer_id];
1856 if (GNUNET_YES == peer->is_remote)
1858 /* Forward the destory message to sub controller */
1859 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1860 GNUNET_SERVER_client_keep (client);
1861 fopc->client = client;
1862 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1864 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1865 fopc->operation_id, &msg->header,
1866 &forwarded_operation_reply_relay,
1868 fopc->timeout_task =
1869 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1871 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1874 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1875 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1876 peer_list_remove (peer);
1878 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1884 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1887 * @param client identification of the client
1888 * @param message the actual message
1891 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1892 const struct GNUNET_MessageHeader *message)
1894 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1895 struct GNUNET_TESTBED_PeerEventMessage *reply;
1896 struct ForwardedOperationContext *fopc;
1900 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1901 peer_id = ntohl (msg->peer_id);
1902 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1905 LOG (GNUNET_ERROR_TYPE_ERROR,
1906 "Asked to start a non existent peer with id: %u\n", peer_id);
1907 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1910 peer = peer_list[peer_id];
1911 if (GNUNET_YES == peer->is_remote)
1913 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1914 GNUNET_SERVER_client_keep (client);
1915 fopc->client = client;
1916 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1918 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1919 fopc->operation_id, &msg->header,
1920 &forwarded_operation_reply_relay,
1922 fopc->timeout_task =
1923 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1925 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1928 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1930 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1932 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1935 peer->details.local.is_running = GNUNET_YES;
1936 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1937 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1938 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1939 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1940 reply->host_id = htonl (master_context->host_id);
1941 reply->peer_id = msg->peer_id;
1942 reply->operation_id = msg->operation_id;
1943 queue_message (client, &reply->header);
1944 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1949 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1952 * @param client identification of the client
1953 * @param message the actual message
1956 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1957 const struct GNUNET_MessageHeader *message)
1959 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1960 struct GNUNET_TESTBED_PeerEventMessage *reply;
1961 struct ForwardedOperationContext *fopc;
1965 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1966 peer_id = ntohl (msg->peer_id);
1967 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1969 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1971 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1974 peer = peer_list[peer_id];
1975 if (GNUNET_YES == peer->is_remote)
1977 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1978 GNUNET_SERVER_client_keep (client);
1979 fopc->client = client;
1980 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1982 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1983 fopc->operation_id, &msg->header,
1984 &forwarded_operation_reply_relay,
1986 fopc->timeout_task =
1987 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1989 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1992 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1994 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1995 "Peer not running");
1996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1999 peer->details.local.is_running = GNUNET_NO;
2000 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2001 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
2002 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
2003 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
2004 reply->host_id = htonl (master_context->host_id);
2005 reply->peer_id = msg->peer_id;
2006 reply->operation_id = msg->operation_id;
2007 queue_message (client, &reply->header);
2008 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2013 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2016 * @param client identification of the client
2017 * @param message the actual message
2020 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2021 const struct GNUNET_MessageHeader *message)
2023 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2024 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2033 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2034 peer_id = ntohl (msg->peer_id);
2035 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2037 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2039 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2042 peer = peer_list[peer_id];
2043 if (GNUNET_YES == peer->is_remote)
2045 /* FIXME: forward to sub controller */
2047 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2051 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2053 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2054 GNUNET_free (config);
2057 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2058 reply = GNUNET_realloc (xconfig, msize);
2059 (void) memmove (&reply[1], reply, xc_size);
2060 reply->header.size = htons (msize);
2061 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2062 reply->peer_id = msg->peer_id;
2063 reply->operation_id = msg->operation_id;
2064 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2065 &reply->peer_identity);
2066 reply->config_size = htons ((uint16_t) c_size);
2067 queue_message (client, &reply->header);
2068 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2073 * Task for cleaing up overlay connect context structure
2075 * @param cls the overlay connect context
2076 * @param tc the task context
2079 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2081 struct OverlayConnectContext *occ = cls;
2083 LOG_DEBUG ("Cleaning up occ\n");
2084 GNUNET_free_non_null (occ->emsg);
2085 GNUNET_free_non_null (occ->hello);
2086 GNUNET_SERVER_client_drop (occ->client);
2087 if (NULL != occ->opc)
2088 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2089 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2090 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2091 if (NULL != occ->ch)
2092 GNUNET_CORE_disconnect (occ->ch);
2093 if (NULL != occ->ghh)
2094 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2095 if (NULL != occ->p1th)
2096 GNUNET_TRANSPORT_disconnect (occ->p1th);
2097 if (NULL != occ->p2th)
2098 GNUNET_TRANSPORT_disconnect (occ->p2th);
2104 * Task which will be run when overlay connect request has been timed out
2106 * @param cls the OverlayConnectContext
2107 * @param tc the TaskContext
2110 timeout_overlay_connect (void *cls,
2111 const struct GNUNET_SCHEDULER_TaskContext *tc)
2113 struct OverlayConnectContext *occ = cls;
2115 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2116 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2117 occ_cleanup (occ, tc);
2123 * Function called to notify transport users that another
2124 * peer connected to us.
2126 * @param cls closure
2127 * @param new_peer the peer that connected
2128 * @param ats performance data
2129 * @param ats_count number of entries in ats (excluding 0-termination)
2132 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2133 const struct GNUNET_ATS_Information *ats,
2134 unsigned int ats_count)
2136 struct OverlayConnectContext *occ = cls;
2137 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2139 char *other_peer_str;
2141 LOG_DEBUG ("Overlay connect notify\n");
2143 memcmp (new_peer, &occ->peer_identity,
2144 sizeof (struct GNUNET_PeerIdentity)))
2146 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2147 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2149 memcmp (new_peer, &occ->other_peer_identity,
2150 sizeof (struct GNUNET_PeerIdentity)))
2152 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2153 new_peer_str, other_peer_str);
2154 GNUNET_free (new_peer_str);
2155 GNUNET_free (other_peer_str);
2158 GNUNET_free (new_peer_str);
2159 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2160 GNUNET_i2s (&occ->peer_identity));
2161 GNUNET_free (other_peer_str);
2162 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2164 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2165 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2167 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2168 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2169 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2170 GNUNET_free_non_null (occ->emsg);
2172 if (NULL != occ->p2th)
2173 GNUNET_TRANSPORT_disconnect (occ->p2th);
2175 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2176 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2178 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2179 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2180 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2181 msg->peer1 = htonl (occ->peer_id);
2182 msg->peer2 = htonl (occ->other_peer_id);
2183 msg->operation_id = GNUNET_htonll (occ->op_id);
2184 queue_message (occ->client, &msg->header);
2185 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2190 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2193 * @param cls the OverlayConnectContext
2194 * @param tc the TaskContext from scheduler
2197 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2199 struct OverlayConnectContext *occ = cls;
2200 char *other_peer_str;
2202 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2203 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2205 GNUNET_assert (NULL != occ->hello);
2206 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2207 if (NULL != occ->peer2_controller)
2209 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2211 uint16_t hello_size;
2213 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2214 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2215 hello_size = ntohs (occ->hello->size);
2216 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2217 msg = GNUNET_malloc (msize);
2218 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2219 msg->header.size = htons (msize);
2220 msg->peer = htonl (occ->other_peer_id);
2221 msg->operation_id = GNUNET_htonll (occ->op_id);
2222 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2223 sizeof (struct GNUNET_PeerIdentity));
2224 memcpy (msg->hello, occ->hello, hello_size);
2225 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2229 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2230 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2231 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2232 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2233 occ->send_hello_task =
2234 GNUNET_SCHEDULER_add_delayed
2235 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2236 100 * (pow (2, occ->retries++))),
2239 GNUNET_free (other_peer_str);
2243 * Test for checking whether HELLO message is empty
2245 * @param cls empty flag to set
2246 * @param address the HELLO
2247 * @param expiration expiration of the HELLO
2251 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2252 struct GNUNET_TIME_Absolute expiration)
2262 * Function called whenever there is an update to the HELLO of peers in the
2263 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2264 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2266 * @param cls closure
2267 * @param hello our updated HELLO
2270 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2272 struct OverlayConnectContext *occ = cls;
2276 msize = ntohs (hello->size);
2278 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2279 hello, GNUNET_NO, &test_address,
2281 if (GNUNET_YES == empty)
2283 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2286 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2287 occ->hello = GNUNET_malloc (msize);
2288 memcpy (occ->hello, hello, msize);
2289 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2291 GNUNET_TRANSPORT_disconnect (occ->p1th);
2293 GNUNET_free_non_null (occ->emsg);
2294 if (NULL == occ->peer2_controller)
2297 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2298 &occ->other_peer_identity, NULL, NULL, NULL,
2300 if (NULL == occ->p2th)
2302 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2303 GNUNET_i2s (&occ->other_peer_identity));
2304 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2305 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2309 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2310 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2315 * Function called after GNUNET_CORE_connect has succeeded (or failed
2316 * for good). Note that the private key of the peer is intentionally
2317 * not exposed here; if you need it, your process should try to read
2318 * the private key file directly (which should work if you are
2321 * @param cls closure
2322 * @param server handle to the server, NULL if we failed
2323 * @param my_identity ID of this peer, NULL if we failed
2326 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2327 const struct GNUNET_PeerIdentity *my_identity)
2329 struct OverlayConnectContext *occ = cls;
2331 GNUNET_free_non_null (occ->emsg);
2332 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2333 if ((NULL == server) || (NULL == my_identity))
2335 GNUNET_free (occ->emsg);
2338 memcpy (&occ->peer_identity, my_identity,
2339 sizeof (struct GNUNET_PeerIdentity));
2341 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2342 &occ->peer_identity, NULL, NULL, NULL, NULL);
2343 if (NULL == occ->p1th)
2345 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2346 GNUNET_i2s (&occ->peer_identity));
2349 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2350 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2351 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2355 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2356 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2362 * Callback to be called when forwarded get peer config operation as part of
2363 * overlay connect is successfull. Connection to Peer 1's core is made and is
2364 * checked for new connection from peer 2
2366 * @param cls ForwardedOperationContext
2367 * @param msg the peer create success message
2370 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2372 struct OverlayConnectContext *occ = cls;
2373 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2374 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2379 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2381 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2383 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2384 sizeof (struct GNUNET_PeerIdentity));
2385 GNUNET_free_non_null (occ->emsg);
2386 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2388 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2389 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2390 GNUNET_NO, no_handlers);
2391 if (NULL == occ->ch)
2396 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2398 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2403 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2406 * @param client identification of the client
2407 * @param message the actual message
2410 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2411 const struct GNUNET_MessageHeader *message)
2413 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2414 struct OverlayConnectContext *occ;
2415 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2419 uint64_t operation_id;
2423 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2424 p1 = ntohl (msg->peer1);
2425 p2 = ntohl (msg->peer2);
2426 GNUNET_assert (p1 < peer_list_size);
2427 GNUNET_assert (NULL != peer_list[p1]);
2428 peer = peer_list[p1];
2429 operation_id = GNUNET_ntohll (msg->operation_id);
2430 if (GNUNET_YES == peer->is_remote)
2432 struct ForwardedOperationContext *fopc;
2434 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2435 GNUNET_SERVER_client_keep (client);
2436 fopc->client = client;
2437 fopc->operation_id = operation_id;
2438 LOG_DEBUG ("Forwarding overlay connect\n");
2440 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
2441 operation_id, message,
2442 &forwarded_operation_reply_relay,
2444 fopc->timeout_task =
2445 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2450 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2451 GNUNET_SERVER_client_keep (client);
2452 occ->client = client;
2454 occ->other_peer_id = p2;
2455 occ->peer = peer_list[p1];
2456 occ->op_id = GNUNET_ntohll (msg->operation_id);
2457 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
2459 uint32_t peer2_host_id;
2461 peer2_host_id = ntohl (msg->peer2_host_id);
2462 if ((peer2_host_id >= slave_list_size)
2463 || (NULL ==slave_list[peer2_host_id]))
2466 GNUNET_SERVER_client_drop (client);
2468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2471 occ->peer2_controller = slave_list[peer2_host_id]->controller;
2472 if (NULL == occ->peer2_controller)
2475 GNUNET_SERVER_client_drop (client);
2477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2483 if (GNUNET_YES == peer_list[occ->other_peer_id]->is_remote)
2484 occ->peer2_controller = peer_list[occ->other_peer_id]->details.remote.controller;
2486 /* Get the identity of the second peer */
2487 if (NULL != occ->peer2_controller)
2489 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2492 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2493 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2494 cmsg.peer_id = msg->peer2;
2495 cmsg.operation_id = msg->operation_id;
2497 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
2498 occ->op_id, &cmsg.header,
2499 &overlay_connect_get_config,
2502 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2504 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2505 (GNUNET_TIME_UNIT_SECONDS, 30),
2506 &timeout_overlay_connect, occ);
2507 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2510 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
2511 &occ->other_peer_identity);
2512 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2513 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2515 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2516 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2517 GNUNET_NO, no_handlers);
2518 if (NULL == occ->ch)
2520 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2523 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2524 (GNUNET_TIME_UNIT_SECONDS, 30),
2525 &timeout_overlay_connect, occ);
2526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2531 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2534 * @param rocc the RequestOverlayConnectContext
2537 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2539 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2540 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2541 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2542 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2543 GNUNET_TRANSPORT_disconnect (rocc->th);
2544 GNUNET_free_non_null (rocc->hello);
2550 * Task to timeout rocc and cleanit up
2552 * @param cls the RequestOverlayConnectContext
2553 * @param tc the TaskContext from scheduler
2556 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2558 struct RequestOverlayConnectContext *rocc = cls;
2560 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2561 cleanup_rocc (rocc);
2566 * Function called to notify transport users that another
2567 * peer connected to us.
2569 * @param cls closure
2570 * @param new_peer the peer that connected
2571 * @param ats performance data
2572 * @param ats_count number of entries in ats (excluding 0-termination)
2575 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2576 const struct GNUNET_ATS_Information * ats,
2579 struct RequestOverlayConnectContext *rocc = cls;
2581 LOG_DEBUG ("Request Overlay connect notify\n");
2582 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2584 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
2585 cleanup_rocc (rocc);
2590 * Task to offer the HELLO message to the peer and ask it to connect to the peer
2591 * whose identity is in RequestOverlayConnectContext
2593 * @param cls the RequestOverlayConnectContext
2594 * @param tc the TaskContext from scheduler
2597 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2599 struct RequestOverlayConnectContext *rocc = cls;
2601 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
2602 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
2603 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
2604 rocc->attempt_connect_task_id =
2605 GNUNET_SCHEDULER_add_delayed
2606 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
2607 100 * (pow (2, rocc->retries++))),
2608 &attempt_connect_task, rocc);
2613 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2616 * @param client identification of the client
2617 * @param message the actual message
2620 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2621 const struct GNUNET_MessageHeader *message)
2623 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2624 struct RequestOverlayConnectContext *rocc;
2630 msize = ntohs (message->size);
2631 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
2634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2637 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2638 if ((NULL == msg->hello) ||
2639 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2645 hsize = ntohs (msg->hello->size);
2646 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
2649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2652 peer_id = ntohl (msg->peer);
2653 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2655 GNUNET_break_op (0);
2656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2659 if (GNUNET_NO != peer->is_remote)
2662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2665 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
2666 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
2667 NULL, &transport_connect_notify, NULL);
2668 if (NULL == rocc->th)
2672 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2675 memcpy (&rocc->a_id, &msg->peer_identity,
2676 sizeof (struct GNUNET_PeerIdentity));
2677 rocc->hello = GNUNET_malloc (hsize);
2678 memcpy (rocc->hello, msg->hello, hsize);
2679 /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
2680 /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
2681 rocc->attempt_connect_task_id =
2682 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
2683 rocc->timeout_rocc_task_id =
2684 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
2685 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2690 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
2693 * @param client identification of the client
2694 * @param message the actual message
2697 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2698 const struct GNUNET_MessageHeader *message)
2700 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
2701 struct Slave *slave;
2702 struct GNUNET_TESTBED_SlaveConfiguration *reply;
2706 size_t xconfig_size;
2711 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
2712 slave_id = ntohl (msg->slave_id);
2713 op_id = GNUNET_ntohll (msg->operation_id);
2714 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
2716 send_operation_fail_msg (client, op_id, "Slave not found");
2717 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2720 slave = slave_list[slave_id];
2721 if (NULL == slave->cfg)
2723 send_operation_fail_msg (client, op_id,
2724 "Configuration not found (slave not started by me)");
2725 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2728 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
2729 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
2731 GNUNET_free (config);
2732 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
2733 GNUNET_break (reply_size <= UINT16_MAX);
2734 GNUNET_break (config_size <= UINT16_MAX);
2735 reply = GNUNET_realloc (xconfig, reply_size);
2736 (void) memmove (&reply[1], reply, xconfig_size);
2737 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
2738 reply->header.size = htons ((uint16_t) reply_size);
2739 reply->slave_id = msg->slave_id;
2740 reply->operation_id = msg->operation_id;
2741 reply->config_size = htons ((uint16_t) config_size);
2742 queue_message (client, &reply->header);
2743 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2748 * Iterator over hash map entries.
2750 * @param cls closure
2751 * @param key current key code
2752 * @param value value in the hash map
2753 * @return GNUNET_YES if we should continue to
2758 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2760 struct SharedService *ss = value;
2762 GNUNET_assert (GNUNET_YES ==
2763 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2764 GNUNET_free (ss->name);
2771 * Task to clean up and shutdown nicely
2774 * @param tc the TaskContext from scheduler
2777 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2779 struct LCFContextQueue *lcfq;
2782 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2783 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2784 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2786 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2787 if (NULL != lcfq_head)
2789 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2791 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2792 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2794 if (NULL != lcfq_head->lcf->rhandle)
2795 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2797 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2798 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2800 GNUNET_free (lcfq->lcf->msg);
2801 GNUNET_free (lcfq->lcf);
2802 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2805 /* Clear peer list */
2806 for (id = 0; id < peer_list_size; id++)
2807 if (NULL != peer_list[id])
2809 if (GNUNET_NO == peer_list[id]->is_remote)
2811 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2812 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2813 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2814 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2816 GNUNET_free (peer_list[id]);
2818 GNUNET_free_non_null (peer_list);
2819 /* Clear host list */
2820 for (id = 0; id < host_list_size; id++)
2821 if (NULL != host_list[id])
2822 GNUNET_TESTBED_host_destroy (host_list[id]);
2823 GNUNET_free_non_null (host_list);
2824 /* Clear route list */
2825 for (id = 0; id < route_list_size; id++)
2826 if (NULL != route_list[id])
2827 GNUNET_free (route_list[id]);
2828 GNUNET_free_non_null (route_list);
2829 /* Clear slave_list */
2830 for (id = 0; id < slave_list_size; id++)
2831 if (NULL != slave_list[id])
2833 if (NULL != slave_list[id]->cfg)
2834 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
2835 if (NULL != slave_list[id]->controller)
2836 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2837 if (NULL != slave_list[id]->controller_proc)
2838 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2839 GNUNET_free (slave_list[id]);
2841 GNUNET_free_non_null (slave_list);
2842 if (NULL != master_context)
2844 GNUNET_free_non_null (master_context->master_ip);
2845 if (NULL != master_context->system)
2846 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2847 GNUNET_free (master_context);
2848 master_context = NULL;
2850 GNUNET_free_non_null (hostname);
2855 * Callback for client disconnect
2858 * @param client the client which has disconnected
2861 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2863 if (NULL == master_context)
2865 if (client == master_context->client)
2867 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2868 GNUNET_SERVER_client_drop (client);
2869 /* should not be needed as we're terminated by failure to read
2870 * from stdin, but if stdin fails for some reason, this shouldn't
2871 * hurt for now --- might need to revise this later if we ever
2872 * decide that master connections might be temporarily down
2873 * for some reason */
2874 //GNUNET_SCHEDULER_shutdown ();
2882 * @param cls closure
2883 * @param server the initialized server
2884 * @param cfg configuration to use
2887 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2888 const struct GNUNET_CONFIGURATION_Handle *cfg)
2890 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2891 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2892 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2893 {&handle_configure_shared_service, NULL,
2894 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2895 {&handle_link_controllers, NULL,
2896 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2897 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2898 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2899 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2900 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2901 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2902 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2903 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2904 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2905 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2906 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2907 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2908 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2910 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
2911 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
2915 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string
2916 (cfg, "testbed", "HOSTNAME", &hostname));
2917 GNUNET_SERVER_add_handlers (server, message_handlers);
2918 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2919 ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
2921 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2922 &shutdown_task, NULL);
2923 LOG_DEBUG ("Testbed startup complete\n");
2928 * The starting point of execution
2931 main (int argc, char *const *argv)
2933 //sleep (15); /* Debugging */
2934 return (GNUNET_OK ==
2935 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2936 &testbed_run, NULL)) ? 0 : 1;
2939 /* end of gnunet-service-testbed.c */