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 * Timeout of Transport try_connect requests
66 #define TRANSPORT_TRY_CONNECT_TIMEOUT \
67 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
70 * The main context information associated with the client which started us
75 * The client handle associated with this context
77 struct GNUNET_SERVER_Client *client;
80 * The network address of the master controller
85 * The TESTING system handle for starting peers locally
87 struct GNUNET_TESTING_System *system;
90 * Event mask of event to be responded in this context
95 * Our host id according to this context
102 * The message queue for sending messages to clients
107 * The message to be sent
109 struct GNUNET_MessageHeader *msg;
112 * The client to send the message to
114 struct GNUNET_SERVER_Client *client;
117 * next pointer for DLL
119 struct MessageQueue *next;
122 * prev pointer for DLL
124 struct MessageQueue *prev;
129 * The structure for identifying a shared service
134 * The name of the shared service
139 * Number of shared peers per instance of the shared service
144 * Number of peers currently sharing the service
146 uint32_t num_sharing;
161 * The destination host is reachable thru
168 * Context information used while linking controllers
170 struct LinkControllersContext;
174 * Structure representing a connected(directly-linked) controller
179 * The controller process handle if we had started the controller
181 struct GNUNET_TESTBED_ControllerProc *controller_proc;
184 * The controller handle
186 struct GNUNET_TESTBED_Controller *controller;
189 * The configuration of the slave. Will be NULL for slave which we didn't
192 struct GNUNET_CONFIGURATION_Handle *cfg;
195 * handle to lcc which is associated with this slave startup. Should be set to
196 * NULL when the slave has successfully started up
198 struct LinkControllersContext *lcc;
201 * The id of the host this controller is running on
208 * States of LCFContext
213 * The Context has been initialized; Nothing has been done on it
218 * Delegated host has been registered at the forwarding controller
220 DELEGATED_HOST_REGISTERED,
223 * The slave host has been registred at the forwarding controller
225 SLAVE_HOST_REGISTERED,
228 * The context has been finished (may have error)
235 * Link controllers request forwarding context
240 * The gateway which will pass the link message to delegated host
242 struct Slave *gateway;
245 * The controller link message that has to be forwarded to
247 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
250 * The client which has asked to perform this operation
252 struct GNUNET_SERVER_Client *client;
255 * The host registration handle while registered hosts in this context
257 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
260 * The id of the operation which created this context
262 uint64_t operation_id;
265 * The state of this context
267 enum LCFContextState state;
272 uint32_t delegated_host_id;
277 uint32_t slave_host_id;
283 * Structure of a queue entry in LCFContext request queue
285 struct LCFContextQueue
290 struct LCFContext *lcf;
295 struct LCFContextQueue *next;
300 struct LCFContextQueue *prev;
305 * A locally started peer
314 * The peer handle from testing API
316 struct GNUNET_TESTING_Peer *peer;
319 * The modified (by GNUNET_TESTING_peer_configure) configuration this
320 * peer is configured with
322 struct GNUNET_CONFIGURATION_Handle *cfg;
325 * Is the peer running
334 * The controller this peer is started through
336 struct GNUNET_TESTBED_Controller *controller;
343 * Is this peer locally created?
348 * Our local reference id for this peer
356 * Context information for connecting 2 peers in overlay
358 struct OverlayConnectContext
361 * The client which has requested for overlay connection
363 struct GNUNET_SERVER_Client *client;
366 * the peer which has to connect to the other peer
371 * Transport handle of the first peer to get its HELLO
373 struct GNUNET_TRANSPORT_Handle *p1th;
376 * Transport handle of other peer to offer first peer's HELLO
378 struct GNUNET_TRANSPORT_Handle *p2th;
381 * Core handles of the first peer; used to notify when second peer connects to it
383 struct GNUNET_CORE_Handle *ch;
386 * HELLO of the other peer
388 struct GNUNET_MessageHeader *hello;
391 * Get hello handle to acquire HELLO of first peer
393 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
396 * The error message we send if this overlay connect operation has timed out
401 * Operation context for suboperations
403 struct OperationContext *opc;
406 * Controller of peer 2; NULL if the peer is local
408 struct GNUNET_TESTBED_Controller *peer2_controller;
411 * The peer identity of the first peer
413 struct GNUNET_PeerIdentity peer_identity;
416 * The peer identity of the other peer
418 struct GNUNET_PeerIdentity other_peer_identity;
421 * The id of the operation responsible for creating this context
426 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
429 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
432 * The id of the overlay connect timeout task
434 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
444 uint32_t other_peer_id;
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;
485 * Context information for operations forwarded to subcontrollers
487 struct ForwardedOperationContext
490 * The generated operation context
492 struct OperationContext *opc;
495 * The client to which we have to reply
497 struct GNUNET_SERVER_Client *client;
505 * Task ID for the timeout task
507 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
510 * The id of the operation that has been forwarded
512 uint64_t operation_id;
518 * Context information used while linking controllers
520 struct LinkControllersContext
523 * The client which initiated the link controller operation
525 struct GNUNET_SERVER_Client *client;
528 * The ID of the operation
530 uint64_t operation_id;
537 * The master context; generated with the first INIT message
539 static struct Context *master_context;
546 * Current Transmit Handle; NULL if no notify transmit exists currently
548 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
555 * The head for the LCF queue
557 static struct LCFContextQueue *lcfq_head;
560 * The tail for the LCF queue
562 static struct LCFContextQueue *lcfq_tail;
565 * The message queue head
567 static struct MessageQueue *mq_head;
570 * The message queue tail
572 static struct MessageQueue *mq_tail;
577 static struct GNUNET_TESTBED_Host **host_list;
582 static struct Route **route_list;
585 * A list of directly linked neighbours
587 static struct Slave **slave_list;
590 * A list of peers we own locally
592 static struct Peer **peer_list;
595 * The hashmap of shared services
597 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
600 * The size of the host list
602 static uint32_t host_list_size;
605 * The size of the route list
607 static uint32_t route_list_size;
610 * The size of directly linked neighbours list
612 static uint32_t slave_list_size;
615 * The size of the peer list
617 static uint32_t peer_list_size;
624 * The lcf_task handle
626 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
629 * The shutdown task handle
631 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
635 * Function called to notify a client about the connection begin ready to queue
636 * more data. "buf" will be NULL and "size" zero if the connection was closed
637 * for writing in the meantime.
640 * @param size number of bytes available in buf
641 * @param buf where the callee should write the message
642 * @return number of bytes written to buf
645 transmit_ready_notify (void *cls, size_t size, void *buf)
647 struct MessageQueue *mq_entry;
649 transmit_handle = NULL;
651 GNUNET_assert (NULL != mq_entry);
654 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
655 size = ntohs (mq_entry->msg->size);
656 memcpy (buf, mq_entry->msg, size);
657 GNUNET_free (mq_entry->msg);
658 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
659 GNUNET_free (mq_entry);
661 if (NULL != mq_entry)
663 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
664 ntohs (mq_entry->msg->size),
665 GNUNET_TIME_UNIT_FOREVER_REL,
666 &transmit_ready_notify, NULL);
672 * Queues a message in send queue for sending to the service
674 * @param client the client to whom the queued message has to be sent
675 * @param msg the message to queue
678 queue_message (struct GNUNET_SERVER_Client *client,
679 struct GNUNET_MessageHeader *msg)
681 struct MessageQueue *mq_entry;
685 type = ntohs (msg->type);
686 size = ntohs (msg->size);
687 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
688 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
689 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
691 mq_entry->client = client;
692 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
694 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
695 if (NULL == transmit_handle)
697 GNUNET_SERVER_notify_transmit_ready (client, size,
698 GNUNET_TIME_UNIT_FOREVER_REL,
699 &transmit_ready_notify, NULL);
704 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
706 * @param ptr the memory block to realloc
707 * @param size the size of ptr
708 * @param new_size the size to which ptr has to be realloc'ed
709 * @return the newly reallocated memory block
712 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
714 ptr = GNUNET_realloc (ptr, new_size);
716 (void) memset (ptr + size, 0, new_size - size);
722 * Function to add a host to the current list of known hosts
724 * @param host the host to add
725 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
729 host_list_add (struct GNUNET_TESTBED_Host *host)
734 host_id = GNUNET_TESTBED_host_get_id_ (host);
735 orig_size = host_list_size;
736 if (host_list_size <= host_id)
738 while (host_list_size <= host_id)
739 host_list_size += LIST_GROW_STEP;
741 TESTBED_realloc (host_list,
742 sizeof (struct GNUNET_TESTBED_Host *) * orig_size,
743 sizeof (struct GNUNET_TESTBED_Host *)
746 if (NULL != host_list[host_id])
748 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
749 return GNUNET_SYSERR;
751 host_list[host_id] = host;
757 * Adds a route to the route list
759 * @param route the route to add
762 route_list_add (struct Route *route)
766 orig_size = route_list_size;
767 if (route->dest >= route_list_size)
769 while (route->dest >= route_list_size)
770 route_list_size += LIST_GROW_STEP;
772 TESTBED_realloc (route_list,
773 sizeof (struct Route *) * orig_size,
774 sizeof (struct Route *) * route_list_size);
776 GNUNET_assert (NULL == route_list[route->dest]);
777 route_list[route->dest] = route;
782 * Adds a slave to the slave array
784 * @param slave the slave controller to add
787 slave_list_add (struct Slave *slave)
789 if (slave->host_id >= slave_list_size)
792 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
793 sizeof (struct Slave *) * (slave_list_size +
795 slave_list_size += LIST_GROW_STEP;
797 GNUNET_assert (NULL == slave_list[slave->host_id]);
798 slave_list[slave->host_id] = slave;
803 * Adds a peer to the peer array
805 * @param peer the peer to add
808 peer_list_add (struct Peer *peer)
812 orig_size = peer_list_size;
813 if (peer->id >= peer_list_size)
815 while (peer->id >= peer_list_size)
816 peer_list_size += LIST_GROW_STEP;
818 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
819 sizeof (struct Peer *) * peer_list_size);
821 GNUNET_assert (NULL == peer_list[peer->id]);
822 peer_list[peer->id] = peer;
827 * Removes a the give peer from the peer array
829 * @param peer the peer to be removed
832 peer_list_remove (struct Peer *peer)
837 peer_list[peer->id] = NULL;
838 orig_size = peer_list_size;
839 while (peer_list_size >= LIST_GROW_STEP)
841 for (id = peer_list_size - 1;
842 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
843 if (NULL != peer_list[id])
845 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
847 peer_list_size -= LIST_GROW_STEP;
849 if (orig_size == peer_list_size)
852 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
857 * Finds the route with directly connected host as destination through which
858 * the destination host can be reached
860 * @param host_id the id of the destination host
861 * @return the route with directly connected destination host; NULL if no route
864 static struct Route *
865 find_dest_route (uint32_t host_id)
869 while (NULL != (route = route_list[host_id]))
871 if (route->thru == master_context->host_id)
873 host_id = route->thru;
880 * Routes message to a host given its host_id
882 * @param host_id the id of the destination host
883 * @param msg the message to be routed
886 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
893 * Send operation failure message to client
895 * @param client the client to which the failure message has to be sent to
896 * @param operation_id the id of the failed operation
897 * @param emsg the error message; can be NULL
900 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
901 uint64_t operation_id, const char *emsg)
903 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
907 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
908 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
910 msg = GNUNET_malloc (msize);
911 msg->header.size = htons (msize);
912 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
913 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
914 msg->operation_id = GNUNET_htonll (operation_id);
916 memcpy (&msg[1], emsg, emsg_len);
917 queue_message (client, &msg->header);
922 * Function to send generic operation success message to given client
924 * @param client the client to send the message to
925 * @param operation_id the id of the operation which was successful
928 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
929 uint64_t operation_id)
931 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
934 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
935 msg = GNUNET_malloc (msize);
936 msg->header.size = htons (msize);
937 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
938 msg->operation_id = GNUNET_htonll (operation_id);
939 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
940 queue_message (client, &msg->header);
945 * The Link Controller forwarding task
947 * @param cls the LCFContext
948 * @param tc the Task context from scheduler
951 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
955 * Completion callback for host registrations while forwarding Link Controller messages
957 * @param cls the LCFContext
958 * @param emsg the error message; NULL if host registration is successful
961 lcf_proc_cc (void *cls, const char *emsg)
963 struct LCFContext *lcf = cls;
966 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
971 goto registration_error;
972 lcf->state = DELEGATED_HOST_REGISTERED;
973 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
975 case DELEGATED_HOST_REGISTERED:
977 goto registration_error;
978 lcf->state = SLAVE_HOST_REGISTERED;
979 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
982 GNUNET_assert (0); /* Shouldn't reach here */
987 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
989 lcf->state = FINISHED;
990 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
995 * Callback to be called when forwarded link controllers operation is
996 * successfull. We have to relay the reply msg back to the client
998 * @param cls ForwardedOperationContext
999 * @param msg the peer create success message
1002 forwarded_operation_reply_relay (void *cls,
1003 const struct GNUNET_MessageHeader *msg)
1005 struct ForwardedOperationContext *fopc = cls;
1006 struct GNUNET_MessageHeader *dup_msg;
1009 msize = ntohs (msg->size);
1010 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1012 dup_msg = GNUNET_malloc (msize);
1013 (void) memcpy (dup_msg, msg, msize);
1014 queue_message (fopc->client, dup_msg);
1015 GNUNET_SERVER_client_drop (fopc->client);
1016 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1022 * Task to free resources when forwarded link controllers has been timedout
1024 * @param cls the ForwardedOperationContext
1025 * @param tc the task context from scheduler
1028 forwarded_operation_timeout (void *cls,
1029 const struct GNUNET_SCHEDULER_TaskContext *tc)
1031 struct ForwardedOperationContext *fopc = cls;
1033 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1034 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1035 GNUNET_SERVER_client_drop (fopc->client);
1041 * The Link Controller forwarding task
1043 * @param cls the LCFContext
1044 * @param tc the Task context from scheduler
1047 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1049 struct LCFContext *lcf = cls;
1050 struct LCFContextQueue *lcfq;
1051 struct ForwardedOperationContext *fopc;
1053 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1058 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1059 lcf->gateway->controller))
1062 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1063 host_list[lcf->delegated_host_id],
1068 lcf->state = DELEGATED_HOST_REGISTERED;
1069 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1072 case DELEGATED_HOST_REGISTERED:
1074 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1075 lcf->gateway->controller))
1078 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1079 host_list[lcf->slave_host_id],
1084 lcf->state = SLAVE_HOST_REGISTERED;
1085 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1088 case SLAVE_HOST_REGISTERED:
1089 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1090 fopc->client = lcf->client;
1091 fopc->operation_id = lcf->operation_id;
1093 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1096 &forwarded_operation_reply_relay,
1098 fopc->timeout_task =
1099 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1101 lcf->state = FINISHED;
1102 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1106 GNUNET_assert (lcfq->lcf == lcf);
1107 GNUNET_free (lcf->msg);
1109 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1111 if (NULL != lcfq_head)
1113 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1119 * Callback for event from slave controllers
1121 * @param cls struct Slave *
1122 * @param event information about the event
1125 slave_event_callback (void *cls,
1126 const struct GNUNET_TESTBED_EventInformation *event)
1133 * Callback to signal successfull startup of the controller process
1135 * @param cls the handle to the slave whose status is to be found here
1136 * @param cfg the configuration with which the controller has been started;
1137 * NULL if status is not GNUNET_OK
1138 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1139 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1142 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1145 struct Slave *slave = cls;
1146 struct LinkControllersContext *lcc;
1149 if (GNUNET_SYSERR == status)
1151 slave->controller_proc = NULL;
1152 slave_list[slave->host_id] = NULL;
1153 if (NULL != slave->cfg)
1154 GNUNET_CONFIGURATION_destroy (slave->cfg);
1155 GNUNET_free (slave);
1157 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1158 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1162 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1163 master_context->event_mask,
1164 &slave_event_callback, slave);
1165 if (NULL != slave->controller)
1167 send_operation_success_msg (lcc->client, lcc->operation_id);
1168 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1172 send_operation_fail_msg (lcc->client, lcc->operation_id,
1173 "Could not connect to delegated controller");
1174 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1175 slave_list[slave->host_id] = NULL;
1176 GNUNET_free (slave);
1183 if (NULL != lcc->client)
1185 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1186 GNUNET_SERVER_client_drop (lcc->client);
1197 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1200 * @param client identification of the client
1201 * @param message the actual message
1204 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1205 const struct GNUNET_MessageHeader *message)
1207 const struct GNUNET_TESTBED_InitMessage *msg;
1208 struct GNUNET_TESTBED_Host *host;
1209 const char *controller_hostname;
1212 if (NULL != master_context)
1214 LOG_DEBUG ("We are being connected to laterally\n");
1215 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1218 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1219 msize = ntohs (message->size);
1220 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1226 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1227 controller_hostname = (const char *) &msg[1];
1228 if ('\0' != controller_hostname[msize - 1])
1231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1234 master_context = GNUNET_malloc (sizeof (struct Context));
1235 master_context->client = client;
1236 master_context->host_id = ntohl (msg->host_id);
1237 master_context->master_ip = GNUNET_strdup (controller_hostname);
1238 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1239 master_context->system =
1240 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1242 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1244 host_list_add (host);
1245 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1246 GNUNET_SERVER_client_keep (client);
1247 LOG_DEBUG ("Created master context with host ID: %u\n",
1248 master_context->host_id);
1249 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1254 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1257 * @param client identification of the client
1258 * @param message the actual message
1261 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1262 const struct GNUNET_MessageHeader *message)
1264 struct GNUNET_TESTBED_Host *host;
1265 const struct GNUNET_TESTBED_AddHostMessage *msg;
1266 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1271 uint16_t username_length;
1272 uint16_t hostname_length;
1273 uint16_t reply_size;
1276 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1277 msize = ntohs (msg->header.size);
1278 username = (char *) &(msg[1]);
1279 username_length = ntohs (msg->user_name_length);
1280 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1281 if (0 != username_length)
1282 GNUNET_assert ('\0' == username[username_length]);
1283 username_length = (0 == username_length) ? 0 : username_length + 1;
1284 hostname = username + username_length;
1286 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1287 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1288 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1289 host_id = ntohl (msg->host_id);
1290 LOG_DEBUG ("Received ADDHOST message\n");
1291 LOG_DEBUG ("-------host id: %u\n", host_id);
1292 LOG_DEBUG ("-------hostname: %s\n", hostname);
1293 if (0 != username_length)
1294 LOG_DEBUG ("-------username: %s\n", username);
1297 LOG_DEBUG ("-------username: NULL\n");
1300 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1302 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1303 ntohs (msg->ssh_port));
1304 GNUNET_assert (NULL != host);
1305 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1306 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1307 if (GNUNET_OK != host_list_add (host))
1309 /* We are unable to add a host */
1310 emsg = "A host exists with given host-id";
1311 LOG_DEBUG ("%s: %u", emsg, host_id);
1312 GNUNET_TESTBED_host_destroy (host);
1313 reply_size += strlen (emsg) + 1;
1314 reply = GNUNET_malloc (reply_size);
1315 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1318 reply = GNUNET_malloc (reply_size);
1319 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1320 reply->header.size = htons (reply_size);
1321 reply->host_id = htonl (host_id);
1322 queue_message (client, &reply->header);
1327 * Iterator over hash map entries.
1329 * @param cls closure
1330 * @param key current key code
1331 * @param value value in the hash map
1332 * @return GNUNET_YES if we should continue to
1337 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1339 struct SharedService *queried_ss = cls;
1340 struct SharedService *ss = value;
1342 if (0 == strcmp (ss->name, queried_ss->name))
1350 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1353 * @param client identification of the client
1354 * @param message the actual message
1357 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1358 const struct GNUNET_MessageHeader *message)
1360 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1361 struct SharedService *ss;
1363 struct GNUNET_HashCode hash;
1365 uint16_t service_name_size;
1367 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1368 msg_size = ntohs (message->size);
1369 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1376 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1377 service_name = (char *) &msg[1];
1378 if ('\0' != service_name[service_name_size - 1])
1381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1384 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1385 service_name, ntohl (msg->num_peers));
1386 if (ntohl (msg->host_id) != master_context->host_id)
1388 route_message (ntohl (msg->host_id), message);
1389 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1392 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1393 ss = GNUNET_malloc (sizeof (struct SharedService));
1394 ss->name = strdup (service_name);
1395 ss->num_shared = ntohl (msg->num_peers);
1396 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1397 if (GNUNET_SYSERR ==
1398 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1399 &ss_exists_iterator, ss))
1401 LOG (GNUNET_ERROR_TYPE_WARNING,
1402 "Service %s already configured as a shared service. "
1403 "Ignoring service sharing request \n", ss->name);
1404 GNUNET_free (ss->name);
1408 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1414 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1417 * @param client identification of the client
1418 * @param message the actual message
1421 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1422 const struct GNUNET_MessageHeader *message)
1424 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1425 struct GNUNET_CONFIGURATION_Handle *cfg;
1426 struct LCFContextQueue *lcfq;
1427 struct Route *route;
1428 struct Route *new_route;
1432 uint32_t delegated_host_id;
1433 uint32_t slave_host_id;
1436 if (NULL == master_context)
1439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1442 msize = ntohs (message->size);
1443 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1449 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1450 delegated_host_id = ntohl (msg->delegated_host_id);
1451 if (delegated_host_id == master_context->host_id)
1454 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1458 if ((delegated_host_id >= host_list_size) ||
1459 (NULL == host_list[delegated_host_id]))
1461 LOG (GNUNET_ERROR_TYPE_WARNING,
1462 "Delegated host %u not registered with us\n", delegated_host_id);
1463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1466 slave_host_id = ntohl (msg->slave_host_id);
1467 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1469 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1470 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1473 if (slave_host_id == delegated_host_id)
1475 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1476 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1480 if (slave_host_id == master_context->host_id) /* Link from us */
1482 struct Slave *slave;
1483 struct LinkControllersContext *lcc;
1485 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1486 config_size = ntohs (msg->config_size);
1487 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1489 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1494 config = GNUNET_malloc (config_size);
1495 dest_size = (uLongf) config_size;
1497 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1500 GNUNET_break (0); /* Compression error */
1501 GNUNET_free (config);
1502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1505 if (config_size != dest_size)
1507 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1508 GNUNET_free (config);
1509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1512 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1514 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1516 GNUNET_break (0); /* Configuration parsing error */
1517 GNUNET_free (config);
1518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1521 GNUNET_free (config);
1522 if ((delegated_host_id < slave_list_size) &&
1523 (NULL != slave_list[delegated_host_id]))
1525 GNUNET_break (0); /* Configuration parsing error */
1526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1529 slave = GNUNET_malloc (sizeof (struct Slave));
1530 slave->host_id = delegated_host_id;
1531 slave_list_add (slave);
1532 if (1 != msg->is_subordinate)
1535 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1536 master_context->event_mask,
1537 &slave_event_callback, slave);
1538 GNUNET_CONFIGURATION_destroy (cfg);
1539 if (NULL != slave->controller)
1540 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1542 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1543 "Could not connect to delegated controller");
1544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1547 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1548 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1549 GNUNET_SERVER_client_keep (client);
1550 lcc->client = client;
1552 slave->controller_proc =
1553 GNUNET_TESTBED_controller_start (master_context->master_ip,
1554 host_list[slave->host_id], cfg,
1555 &slave_status_callback, slave);
1556 GNUNET_CONFIGURATION_destroy (cfg);
1557 new_route = GNUNET_malloc (sizeof (struct Route));
1558 new_route->dest = delegated_host_id;
1559 new_route->thru = master_context->host_id;
1560 route_list_add (new_route);
1564 /* Route the request */
1565 if (slave_host_id >= route_list_size)
1567 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1571 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1572 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1573 lcfq->lcf->delegated_host_id = delegated_host_id;
1574 lcfq->lcf->slave_host_id = slave_host_id;
1575 route = find_dest_route (slave_host_id);
1576 GNUNET_assert (NULL != route); /* because we add routes carefully */
1577 GNUNET_assert (route->dest < slave_list_size);
1578 GNUNET_assert (NULL != slave_list[route->dest]);
1579 lcfq->lcf->state = INIT;
1580 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1581 lcfq->lcf->gateway = slave_list[route->dest];
1582 lcfq->lcf->msg = GNUNET_malloc (msize);
1583 (void) memcpy (lcfq->lcf->msg, msg, msize);
1584 GNUNET_SERVER_client_keep (client);
1585 lcfq->lcf->client = client;
1586 if (NULL == lcfq_head)
1588 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1589 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1590 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1593 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1594 /* FIXME: Adding a new route should happen after the controllers are linked
1596 if (1 != msg->is_subordinate)
1598 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1601 if ((delegated_host_id < route_list_size)
1602 && (NULL != route_list[delegated_host_id]))
1604 GNUNET_break_op (0); /* Are you trying to link delegated host twice
1605 with is subordinate flag set to GNUNET_YES? */
1606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1609 new_route = GNUNET_malloc (sizeof (struct Route));
1610 new_route->dest = delegated_host_id;
1611 new_route->thru = route->dest;
1612 route_list_add (new_route);
1613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1618 * The task to be executed if the forwarded peer create operation has been
1621 * @param cls the FowardedOperationContext
1622 * @param tc the TaskContext from the scheduler
1625 peer_create_forward_timeout (void *cls,
1626 const struct GNUNET_SCHEDULER_TaskContext *tc)
1628 struct ForwardedOperationContext *fo_ctxt = cls;
1630 /* send error msg to client */
1631 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1632 GNUNET_SERVER_client_drop (fo_ctxt->client);
1633 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1634 GNUNET_free (fo_ctxt);
1639 * Callback to be called when forwarded peer create operation is
1640 * successfull. We have to relay the reply msg back to the client
1642 * @param cls ForwardedOperationContext
1643 * @param msg the peer create success message
1646 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1648 struct ForwardedOperationContext *fo_ctxt = cls;
1649 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1650 struct GNUNET_MessageHeader *dup_msg;
1654 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1655 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1658 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1659 peer = GNUNET_malloc (sizeof (struct Peer));
1660 peer->is_remote = GNUNET_YES;
1661 peer->id = ntohl (success_msg->peer_id);
1662 GNUNET_assert (NULL != fo_ctxt->cls);
1663 peer->details.remote.controller = fo_ctxt->cls;
1664 peer_list_add (peer);
1666 msize = ntohs (msg->size);
1667 dup_msg = GNUNET_malloc (msize);
1668 (void) memcpy (dup_msg, msg, msize);
1669 queue_message (fo_ctxt->client, dup_msg);
1670 GNUNET_SERVER_client_drop (fo_ctxt->client);
1671 GNUNET_free (fo_ctxt);
1677 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1680 * @param client identification of the client
1681 * @param message the actual message
1684 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1685 const struct GNUNET_MessageHeader *message)
1687 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1688 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1689 struct GNUNET_CONFIGURATION_Handle *cfg;
1690 struct ForwardedOperationContext *fo_ctxt;
1691 struct Route *route;
1696 uint32_t config_size;
1702 msize = ntohs (message->size);
1703 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1705 GNUNET_break (0); /* We need configuration */
1706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1709 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1710 host_id = ntohl (msg->host_id);
1711 peer_id = ntohl (msg->peer_id);
1712 if (UINT32_MAX == peer_id)
1714 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1715 "Cannot create peer with given ID");
1716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1719 if (host_id == master_context->host_id)
1723 /* We are responsible for this peer */
1724 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1725 config_size = ntohl (msg->config_size);
1726 config = GNUNET_malloc (config_size);
1727 dest_size = config_size;
1730 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1731 (const Bytef *) &msg[1], (uLong) msize)))
1733 GNUNET_break (0); /* uncompression error */
1734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1737 if (config_size != dest_size)
1739 GNUNET_break (0); /* Uncompressed config size mismatch */
1740 GNUNET_free (config);
1741 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1744 cfg = GNUNET_CONFIGURATION_create ();
1746 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1748 GNUNET_break (0); /* Configuration parsing error */
1749 GNUNET_free (config);
1750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1753 GNUNET_free (config);
1754 peer = GNUNET_malloc (sizeof (struct Peer));
1755 peer->is_remote = GNUNET_NO;
1756 peer->details.local.cfg = cfg;
1758 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1759 peer->details.local.peer =
1760 GNUNET_TESTING_peer_configure (master_context->system,
1761 peer->details.local.cfg, peer->id,
1762 NULL /* Peer id */ ,
1764 if (NULL == peer->details.local.peer)
1766 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1773 peer->details.local.is_running = GNUNET_NO;
1774 peer_list_add (peer);
1776 GNUNET_malloc (sizeof
1777 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1778 reply->header.size =
1779 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1780 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1781 reply->peer_id = msg->peer_id;
1782 reply->operation_id = msg->operation_id;
1783 queue_message (client, &reply->header);
1784 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1788 /* Forward peer create request */
1789 route = find_dest_route (host_id);
1793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1796 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1797 GNUNET_SERVER_client_keep (client);
1798 fo_ctxt->client = client;
1799 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1800 fo_ctxt->cls = slave_list[route->dest]->controller;
1802 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1803 [route->dest]->controller,
1804 fo_ctxt->operation_id,
1806 peer_create_success_cb, fo_ctxt);
1807 fo_ctxt->timeout_task =
1808 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1816 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1819 * @param client identification of the client
1820 * @param message the actual message
1823 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1824 const struct GNUNET_MessageHeader *message)
1826 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1827 struct ForwardedOperationContext *fopc;
1831 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1832 peer_id = ntohl (msg->peer_id);
1833 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1834 peer_id, GNUNET_ntohll (msg->operation_id));
1835 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1837 LOG (GNUNET_ERROR_TYPE_ERROR,
1838 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1839 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1840 "Peer doesn't exist");
1841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1844 peer = peer_list[peer_id];
1845 if (GNUNET_YES == peer->is_remote)
1847 /* Forward the destory message to sub controller */
1848 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1849 GNUNET_SERVER_client_keep (client);
1850 fopc->client = client;
1851 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1853 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1854 fopc->operation_id, &msg->header,
1855 &forwarded_operation_reply_relay,
1857 fopc->timeout_task =
1858 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1863 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1864 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1865 peer_list_remove (peer);
1867 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1873 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1876 * @param client identification of the client
1877 * @param message the actual message
1880 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1881 const struct GNUNET_MessageHeader *message)
1883 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1884 struct GNUNET_TESTBED_PeerEventMessage *reply;
1885 struct ForwardedOperationContext *fopc;
1889 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1890 peer_id = ntohl (msg->peer_id);
1891 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1894 LOG (GNUNET_ERROR_TYPE_ERROR,
1895 "Asked to start a non existent peer with id: %u\n", peer_id);
1896 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1899 peer = peer_list[peer_id];
1900 if (GNUNET_YES == peer->is_remote)
1902 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1903 GNUNET_SERVER_client_keep (client);
1904 fopc->client = client;
1905 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1907 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1908 fopc->operation_id, &msg->header,
1909 &forwarded_operation_reply_relay,
1911 fopc->timeout_task =
1912 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1917 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1919 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1921 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1924 peer->details.local.is_running = GNUNET_YES;
1925 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1926 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1927 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1928 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1929 reply->host_id = htonl (master_context->host_id);
1930 reply->peer_id = msg->peer_id;
1931 reply->operation_id = msg->operation_id;
1932 queue_message (client, &reply->header);
1933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1938 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1941 * @param client identification of the client
1942 * @param message the actual message
1945 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1946 const struct GNUNET_MessageHeader *message)
1948 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1949 struct GNUNET_TESTBED_PeerEventMessage *reply;
1950 struct ForwardedOperationContext *fopc;
1954 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1955 peer_id = ntohl (msg->peer_id);
1956 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1958 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1960 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1963 peer = peer_list[peer_id];
1964 if (GNUNET_YES == peer->is_remote)
1966 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1967 GNUNET_SERVER_client_keep (client);
1968 fopc->client = client;
1969 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1971 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1972 fopc->operation_id, &msg->header,
1973 &forwarded_operation_reply_relay,
1975 fopc->timeout_task =
1976 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1978 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1981 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1983 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1984 "Peer not running");
1985 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1988 peer->details.local.is_running = GNUNET_NO;
1989 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1990 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1991 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1992 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1993 reply->host_id = htonl (master_context->host_id);
1994 reply->peer_id = msg->peer_id;
1995 reply->operation_id = msg->operation_id;
1996 queue_message (client, &reply->header);
1997 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2002 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
2005 * @param client identification of the client
2006 * @param message the actual message
2009 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2010 const struct GNUNET_MessageHeader *message)
2012 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
2013 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2022 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2023 peer_id = ntohl (msg->peer_id);
2024 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2026 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2028 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2031 peer = peer_list[peer_id];
2032 if (GNUNET_YES == peer->is_remote)
2034 /* FIXME: forward to sub controller */
2036 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2040 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2042 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2043 GNUNET_free (config);
2046 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2047 reply = GNUNET_realloc (xconfig, msize);
2048 (void) memmove (&reply[1], reply, xc_size);
2049 reply->header.size = htons (msize);
2050 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2051 reply->peer_id = msg->peer_id;
2052 reply->operation_id = msg->operation_id;
2053 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2054 &reply->peer_identity);
2055 reply->config_size = htons ((uint16_t) c_size);
2056 queue_message (client, &reply->header);
2057 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2062 * Task for cleaing up overlay connect context structure
2064 * @param cls the overlay connect context
2065 * @param tc the task context
2068 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2070 struct OverlayConnectContext *occ = cls;
2072 LOG_DEBUG ("Cleaning up occ\n");
2073 GNUNET_free_non_null (occ->emsg);
2074 GNUNET_free_non_null (occ->hello);
2075 GNUNET_SERVER_client_drop (occ->client);
2076 if (NULL != occ->opc)
2077 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2078 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2079 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2080 if (NULL != occ->ch)
2081 GNUNET_CORE_disconnect (occ->ch);
2082 if (NULL != occ->ghh)
2083 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2084 if (NULL != occ->p1th)
2085 GNUNET_TRANSPORT_disconnect (occ->p1th);
2086 if (NULL != occ->p2th)
2087 GNUNET_TRANSPORT_disconnect (occ->p2th);
2093 * Task which will be run when overlay connect request has been timed out
2095 * @param cls the OverlayConnectContext
2096 * @param tc the TaskContext
2099 timeout_overlay_connect (void *cls,
2100 const struct GNUNET_SCHEDULER_TaskContext *tc)
2102 struct OverlayConnectContext *occ = cls;
2104 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2105 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2106 occ_cleanup (occ, tc);
2112 * Function called to notify transport users that another
2113 * peer connected to us.
2115 * @param cls closure
2116 * @param new_peer the peer that connected
2117 * @param ats performance data
2118 * @param ats_count number of entries in ats (excluding 0-termination)
2121 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2122 const struct GNUNET_ATS_Information *ats,
2123 unsigned int ats_count)
2125 struct OverlayConnectContext *occ = cls;
2126 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2128 char *other_peer_str;
2130 LOG_DEBUG ("Overlay connect notify\n");
2132 memcmp (new_peer, &occ->peer_identity,
2133 sizeof (struct GNUNET_PeerIdentity)))
2135 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2136 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2138 memcmp (new_peer, &occ->other_peer_identity,
2139 sizeof (struct GNUNET_PeerIdentity)))
2141 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2142 new_peer_str, other_peer_str);
2143 GNUNET_free (new_peer_str);
2144 GNUNET_free (other_peer_str);
2147 GNUNET_free (new_peer_str);
2148 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2149 GNUNET_i2s (&occ->peer_identity));
2150 GNUNET_free (other_peer_str);
2151 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2153 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2154 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2156 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2157 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2158 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2159 GNUNET_free_non_null (occ->emsg);
2161 if (NULL != occ->p2th)
2162 GNUNET_TRANSPORT_disconnect (occ->p2th);
2164 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2165 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2167 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2168 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2169 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2170 msg->peer1 = htonl (occ->peer_id);
2171 msg->peer2 = htonl (occ->other_peer_id);
2172 msg->operation_id = GNUNET_htonll (occ->op_id);
2173 queue_message (occ->client, &msg->header);
2174 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2179 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2182 * @param cls the OverlayConnectContext
2183 * @param tc the TaskContext from scheduler
2186 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2188 struct OverlayConnectContext *occ = cls;
2189 char *other_peer_str;
2191 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2192 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2194 GNUNET_assert (NULL != occ->hello);
2195 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2196 if (NULL != occ->peer2_controller)
2198 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2200 uint16_t hello_size;
2202 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2203 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2204 hello_size = ntohs (occ->hello->size);
2205 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2206 msg = GNUNET_malloc (msize);
2207 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2208 msg->header.size = htons (msize);
2209 msg->peer = htonl (occ->other_peer_id);
2210 msg->operation_id = GNUNET_htonll (occ->op_id);
2211 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2212 sizeof (struct GNUNET_PeerIdentity));
2213 memcpy (msg->hello, occ->hello, hello_size);
2214 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2218 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2219 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2220 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2221 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2222 occ->send_hello_task =
2223 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2226 GNUNET_free (other_peer_str);
2230 * Test for checking whether HELLO message is empty
2232 * @param cls empty flag to set
2233 * @param address the HELLO
2234 * @param expiration expiration of the HELLO
2238 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2239 struct GNUNET_TIME_Absolute expiration)
2249 * Function called whenever there is an update to the HELLO of peers in the
2250 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2251 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2253 * @param cls closure
2254 * @param hello our updated HELLO
2257 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2259 struct OverlayConnectContext *occ = cls;
2263 msize = ntohs (hello->size);
2265 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2266 hello, GNUNET_NO, &test_address,
2268 if (GNUNET_YES == empty)
2270 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2273 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2274 occ->hello = GNUNET_malloc (msize);
2275 memcpy (occ->hello, hello, msize);
2276 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2278 GNUNET_TRANSPORT_disconnect (occ->p1th);
2280 GNUNET_free_non_null (occ->emsg);
2281 if (NULL == occ->peer2_controller)
2284 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2285 &occ->other_peer_identity, NULL, NULL, NULL,
2287 if (NULL == occ->p2th)
2289 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2290 GNUNET_i2s (&occ->other_peer_identity));
2291 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2292 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2296 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2297 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2302 * Function called after GNUNET_CORE_connect has succeeded (or failed
2303 * for good). Note that the private key of the peer is intentionally
2304 * not exposed here; if you need it, your process should try to read
2305 * the private key file directly (which should work if you are
2308 * @param cls closure
2309 * @param server handle to the server, NULL if we failed
2310 * @param my_identity ID of this peer, NULL if we failed
2313 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2314 const struct GNUNET_PeerIdentity *my_identity)
2316 struct OverlayConnectContext *occ = cls;
2318 GNUNET_free_non_null (occ->emsg);
2319 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2320 if ((NULL == server) || (NULL == my_identity))
2322 GNUNET_free (occ->emsg);
2325 memcpy (&occ->peer_identity, my_identity,
2326 sizeof (struct GNUNET_PeerIdentity));
2328 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2329 &occ->peer_identity, NULL, NULL, NULL, NULL);
2330 if (NULL == occ->p1th)
2332 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2333 GNUNET_i2s (&occ->peer_identity));
2336 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2337 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2338 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2342 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2343 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2349 * Callback to be called when forwarded get peer config operation as part of
2350 * overlay connect is successfull. Connection to Peer 1's core is made and is
2351 * checked for new connection from peer 2
2353 * @param cls ForwardedOperationContext
2354 * @param msg the peer create success message
2357 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2359 struct OverlayConnectContext *occ = cls;
2360 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2361 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2366 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2368 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2370 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2371 sizeof (struct GNUNET_PeerIdentity));
2372 GNUNET_free_non_null (occ->emsg);
2373 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2375 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2376 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2377 GNUNET_NO, no_handlers);
2378 if (NULL == occ->ch)
2383 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2385 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2390 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2393 * @param client identification of the client
2394 * @param message the actual message
2397 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2398 const struct GNUNET_MessageHeader *message)
2400 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2401 struct OverlayConnectContext *occ;
2402 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2406 uint64_t operation_id;
2410 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2411 p1 = ntohl (msg->peer1);
2412 p2 = ntohl (msg->peer2);
2413 GNUNET_assert (p1 < peer_list_size);
2414 GNUNET_assert (NULL != peer_list[p1]);
2415 peer = peer_list[p1];
2416 operation_id = GNUNET_ntohll (msg->operation_id);
2417 if (GNUNET_YES == peer->is_remote)
2419 struct ForwardedOperationContext *fopc;
2421 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2422 GNUNET_SERVER_client_keep (client);
2423 fopc->client = client;
2424 fopc->operation_id = operation_id;
2425 LOG_DEBUG ("Forwarding overlay connect\n");
2427 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
2428 operation_id, message,
2429 &forwarded_operation_reply_relay,
2431 fopc->timeout_task =
2432 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2437 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2438 GNUNET_SERVER_client_keep (client);
2439 occ->client = client;
2441 occ->other_peer_id = p2;
2442 occ->peer = peer_list[p1];
2443 occ->op_id = GNUNET_ntohll (msg->operation_id);
2444 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
2446 uint32_t peer2_host_id;
2448 peer2_host_id = ntohl (msg->peer2_host_id);
2449 if ((peer2_host_id >= slave_list_size)
2450 || (NULL ==slave_list[peer2_host_id]))
2453 GNUNET_SERVER_client_drop (client);
2455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2458 occ->peer2_controller = slave_list[peer2_host_id]->controller;
2459 if (NULL == occ->peer2_controller)
2462 GNUNET_SERVER_client_drop (client);
2464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2470 if (GNUNET_YES == peer_list[occ->other_peer_id]->is_remote)
2471 occ->peer2_controller = peer_list[occ->other_peer_id]->details.remote.controller;
2473 /* Get the identity of the second peer */
2474 if (NULL != occ->peer2_controller)
2476 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2479 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2480 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2481 cmsg.peer_id = msg->peer2;
2482 cmsg.operation_id = msg->operation_id;
2484 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
2485 occ->op_id, &cmsg.header,
2486 &overlay_connect_get_config,
2489 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2491 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2492 (GNUNET_TIME_UNIT_SECONDS, 30),
2493 &timeout_overlay_connect, occ);
2494 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2497 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
2498 &occ->other_peer_identity);
2499 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2500 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2502 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2503 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2504 GNUNET_NO, no_handlers);
2505 if (NULL == occ->ch)
2507 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2510 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2511 (GNUNET_TIME_UNIT_SECONDS, 30),
2512 &timeout_overlay_connect, occ);
2513 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2518 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2521 * @param rocc the RequestOverlayConnectContext
2524 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2526 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2527 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2528 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2529 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2530 GNUNET_TRANSPORT_disconnect (rocc->th);
2531 GNUNET_free_non_null (rocc->hello);
2537 * Task to timeout rocc and cleanit up
2539 * @param cls the RequestOverlayConnectContext
2540 * @param tc the TaskContext from scheduler
2543 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2545 struct RequestOverlayConnectContext *rocc = cls;
2547 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2548 cleanup_rocc (rocc);
2553 * Function called to notify transport users that another
2554 * peer connected to us.
2556 * @param cls closure
2557 * @param new_peer the peer that connected
2558 * @param ats performance data
2559 * @param ats_count number of entries in ats (excluding 0-termination)
2562 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2563 const struct GNUNET_ATS_Information * ats,
2566 struct RequestOverlayConnectContext *rocc = cls;
2568 LOG_DEBUG ("Request Overlay connect notify\n");
2569 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2571 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
2572 cleanup_rocc (rocc);
2577 * Task to offer the HELLO message to the peer and ask it to connect to the peer
2578 * whose identity is in RequestOverlayConnectContext
2580 * @param cls the RequestOverlayConnectContext
2581 * @param tc the TaskContext from scheduler
2584 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2586 struct RequestOverlayConnectContext *rocc = cls;
2588 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
2589 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
2590 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
2591 rocc->attempt_connect_task_id =
2592 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2593 &attempt_connect_task, rocc);
2598 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2601 * @param client identification of the client
2602 * @param message the actual message
2605 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2606 const struct GNUNET_MessageHeader *message)
2608 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2609 struct RequestOverlayConnectContext *rocc;
2615 msize = ntohs (message->size);
2616 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
2619 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2622 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2623 if ((NULL == msg->hello) ||
2624 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2630 hsize = ntohs (msg->hello->size);
2631 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
2634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2637 peer_id = ntohl (msg->peer);
2638 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2640 GNUNET_break_op (0);
2641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2644 if (GNUNET_NO != peer->is_remote)
2647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2650 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
2651 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
2652 NULL, &transport_connect_notify, NULL);
2653 if (NULL == rocc->th)
2657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2660 memcpy (&rocc->a_id, &msg->peer_identity,
2661 sizeof (struct GNUNET_PeerIdentity));
2662 rocc->hello = GNUNET_malloc (hsize);
2663 memcpy (rocc->hello, msg->hello, hsize);
2664 /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
2665 /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
2666 rocc->attempt_connect_task_id =
2667 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
2668 rocc->timeout_rocc_task_id =
2669 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
2670 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2675 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
2678 * @param client identification of the client
2679 * @param message the actual message
2682 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2683 const struct GNUNET_MessageHeader *message)
2685 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
2686 struct Slave *slave;
2687 struct GNUNET_TESTBED_SlaveConfiguration *reply;
2691 size_t xconfig_size;
2696 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
2697 slave_id = ntohl (msg->slave_id);
2698 op_id = GNUNET_ntohll (msg->operation_id);
2699 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
2701 send_operation_fail_msg (client, op_id, "Slave not found");
2702 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2705 slave = slave_list[slave_id];
2706 if (NULL == slave->cfg)
2708 send_operation_fail_msg (client, op_id,
2709 "Configuration not found (slave not started by me)");
2710 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2713 config = GNUNET_CONFIGURATION_serialize (slave->cfg, &config_size);
2714 xconfig_size = GNUNET_TESTBED_compress_config_ (config, config_size,
2716 GNUNET_free (config);
2717 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
2718 GNUNET_break (reply_size <= UINT16_MAX);
2719 GNUNET_break (config_size <= UINT16_MAX);
2720 reply = GNUNET_realloc (xconfig, reply_size);
2721 (void) memmove (&reply[1], reply, xconfig_size);
2722 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG);
2723 reply->header.size = htons ((uint16_t) reply_size);
2724 reply->slave_id = msg->slave_id;
2725 reply->operation_id = msg->operation_id;
2726 reply->config_size = htons ((uint16_t) config_size);
2727 queue_message (client, &reply->header);
2728 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2733 * Iterator over hash map entries.
2735 * @param cls closure
2736 * @param key current key code
2737 * @param value value in the hash map
2738 * @return GNUNET_YES if we should continue to
2743 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2745 struct SharedService *ss = value;
2747 GNUNET_assert (GNUNET_YES ==
2748 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2749 GNUNET_free (ss->name);
2756 * Task to clean up and shutdown nicely
2759 * @param tc the TaskContext from scheduler
2762 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2764 struct LCFContextQueue *lcfq;
2767 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2768 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2769 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2771 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2772 if (NULL != lcfq_head)
2774 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2776 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2777 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2779 if (NULL != lcfq_head->lcf->rhandle)
2780 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2782 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2783 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2785 GNUNET_free (lcfq->lcf->msg);
2786 GNUNET_free (lcfq->lcf);
2787 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2790 /* Clear peer list */
2791 for (id = 0; id < peer_list_size; id++)
2792 if (NULL != peer_list[id])
2794 if (GNUNET_NO == peer_list[id]->is_remote)
2796 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2797 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2798 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2799 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2801 GNUNET_free (peer_list[id]);
2803 GNUNET_free_non_null (peer_list);
2804 /* Clear host list */
2805 for (id = 0; id < host_list_size; id++)
2806 if (NULL != host_list[id])
2807 GNUNET_TESTBED_host_destroy (host_list[id]);
2808 GNUNET_free_non_null (host_list);
2809 /* Clear route list */
2810 for (id = 0; id < route_list_size; id++)
2811 if (NULL != route_list[id])
2812 GNUNET_free (route_list[id]);
2813 GNUNET_free_non_null (route_list);
2814 /* Clear slave_list */
2815 for (id = 0; id < slave_list_size; id++)
2816 if (NULL != slave_list[id])
2818 if (NULL != slave_list[id]->cfg)
2819 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
2820 if (NULL != slave_list[id]->controller)
2821 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2822 if (NULL != slave_list[id]->controller_proc)
2823 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2824 GNUNET_free (slave_list[id]);
2826 GNUNET_free_non_null (slave_list);
2827 if (NULL != master_context)
2829 GNUNET_free_non_null (master_context->master_ip);
2830 if (NULL != master_context->system)
2831 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2832 GNUNET_free (master_context);
2833 master_context = NULL;
2839 * Callback for client disconnect
2842 * @param client the client which has disconnected
2845 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2847 if (NULL == master_context)
2849 if (client == master_context->client)
2851 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2852 GNUNET_SERVER_client_drop (client);
2853 /* should not be needed as we're terminated by failure to read
2854 * from stdin, but if stdin fails for some reason, this shouldn't
2855 * hurt for now --- might need to revise this later if we ever
2856 * decide that master connections might be temporarily down
2857 * for some reason */
2858 //GNUNET_SCHEDULER_shutdown ();
2866 * @param cls closure
2867 * @param server the initialized server
2868 * @param cfg configuration to use
2871 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2872 const struct GNUNET_CONFIGURATION_Handle *cfg)
2874 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2875 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2876 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2877 {&handle_configure_shared_service, NULL,
2878 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2879 {&handle_link_controllers, NULL,
2880 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2881 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2882 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2883 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2884 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2885 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2886 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2887 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2888 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2889 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2890 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2891 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2892 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2894 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
2895 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
2899 GNUNET_SERVER_add_handlers (server, message_handlers);
2900 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2901 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2903 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2904 &shutdown_task, NULL);
2905 LOG_DEBUG ("Testbed startup complete\n");
2910 * The starting point of execution
2913 main (int argc, char *const *argv)
2915 //sleep (15); /* Debugging */
2916 return (GNUNET_OK ==
2917 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2918 &testbed_run, NULL)) ? 0 : 1;
2921 /* end of gnunet-service-testbed.c */