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 host destination is reachable thru
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 id of the host this controller is running on
190 * States of LCFContext
195 * The Context has been initialized; Nothing has been done on it
200 * Delegated host has been registered at the forwarding controller
202 DELEGATED_HOST_REGISTERED,
205 * The slave host has been registred at the forwarding controller
207 SLAVE_HOST_REGISTERED,
210 * The context has been finished (may have error)
217 * Link controllers request forwarding context
222 * The gateway which will pass the link message to delegated host
224 struct Slave *gateway;
227 * The controller link message that has to be forwarded to
229 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
232 * The client which has asked to perform this operation
234 struct GNUNET_SERVER_Client *client;
237 * The host registration handle while registered hosts in this context
239 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
242 * The id of the operation which created this context
244 uint64_t operation_id;
247 * The state of this context
249 enum LCFContextState state;
254 uint32_t delegated_host_id;
259 uint32_t slave_host_id;
265 * Structure of a queue entry in LCFContext request queue
267 struct LCFContextQueue
272 struct LCFContext *lcf;
277 struct LCFContextQueue *next;
282 struct LCFContextQueue *prev;
287 * A locally started peer
296 * The peer handle from testing API
298 struct GNUNET_TESTING_Peer *peer;
301 * The modified (by GNUNET_TESTING_peer_configure) configuration this
302 * peer is configured with
304 struct GNUNET_CONFIGURATION_Handle *cfg;
307 * Is the peer running
316 * The controller this peer is started through
318 struct GNUNET_TESTBED_Controller *controller;
325 * Is this peer locally created?
330 * Our local reference id for this peer
338 * Context information for connecting 2 peers in overlay
340 struct OverlayConnectContext
343 * The client which has requested for overlay connection
345 struct GNUNET_SERVER_Client *client;
348 * the peer which has to connect to the other peer
355 struct Peer *other_peer;
358 * Transport handle of the first peer to get its HELLO
360 struct GNUNET_TRANSPORT_Handle *p1th;
363 * Transport handle of other peer to offer first peer's HELLO
365 struct GNUNET_TRANSPORT_Handle *p2th;
368 * Core handles of the first peer; used to notify when second peer connects to it
370 struct GNUNET_CORE_Handle *ch;
373 * HELLO of the other peer
375 struct GNUNET_MessageHeader *hello;
378 * Get hello handle to acquire HELLO of first peer
380 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
383 * The error message we send if this overlay connect operation has timed out
388 * Operation context for suboperations
390 struct OperationContext *opc;
393 * The peer identity of the first peer
395 struct GNUNET_PeerIdentity peer_identity;
398 * The peer identity of the other peer
400 struct GNUNET_PeerIdentity other_peer_identity;
403 * The id of the operation responsible for creating this context
408 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
411 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
414 * The id of the overlay connect timeout task
416 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
422 * Context information for RequestOverlayConnect
423 * operations. RequestOverlayConnect is used when peers A, B reside on different
424 * hosts and the host controller for peer B is asked by the host controller of
425 * peer A to make peer B connect to peer A
427 struct RequestOverlayConnectContext
430 * The transport handle of peer B
432 struct GNUNET_TRANSPORT_Handle *th;
437 struct GNUNET_MessageHeader *hello;
440 * The peer identity of peer A
442 struct GNUNET_PeerIdentity a_id;
445 * Task for offering HELLO of A to B and doing try_connect
447 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
450 * Task to timeout RequestOverlayConnect
452 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
458 * Context information for operations forwarded to subcontrollers
460 struct ForwardedOperationContext
463 * The generated operation context
465 struct OperationContext *opc;
468 * The client to which we have to reply
470 struct GNUNET_SERVER_Client *client;
478 * Task ID for the timeout task
480 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
483 * The id of the operation that has been forwarded
485 uint64_t operation_id;
491 * Context information used while linking controllers
493 struct LinkControllersContext
496 * The client which initiated the link controller operation
498 struct GNUNET_SERVER_Client *client;
501 * The ID of the operation
503 uint64_t operation_id;
506 * Pointer to the slave handle if we are directly starting/connecting to the controller
514 * The master context; generated with the first INIT message
516 static struct Context *master_context;
523 * Current Transmit Handle; NULL if no notify transmit exists currently
525 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
532 * The head for the LCF queue
534 static struct LCFContextQueue *lcfq_head;
537 * The tail for the LCF queue
539 static struct LCFContextQueue *lcfq_tail;
542 * The message queue head
544 static struct MessageQueue *mq_head;
547 * The message queue tail
549 static struct MessageQueue *mq_tail;
554 static struct GNUNET_TESTBED_Host **host_list;
559 static struct Route **route_list;
562 * A list of directly linked neighbours
564 static struct Slave **slave_list;
567 * A list of peers we own locally
569 static struct Peer **peer_list;
572 * The hashmap of shared services
574 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
577 * The size of the host list
579 static uint32_t host_list_size;
582 * The size of the route list
584 static uint32_t route_list_size;
587 * The size of directly linked neighbours list
589 static uint32_t slave_list_size;
592 * The size of the peer list
594 static uint32_t peer_list_size;
601 * The lcf_task handle
603 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
606 * The shutdown task handle
608 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
612 * Function called to notify a client about the connection begin ready to queue
613 * more data. "buf" will be NULL and "size" zero if the connection was closed
614 * for writing in the meantime.
617 * @param size number of bytes available in buf
618 * @param buf where the callee should write the message
619 * @return number of bytes written to buf
622 transmit_ready_notify (void *cls, size_t size, void *buf)
624 struct MessageQueue *mq_entry;
626 transmit_handle = NULL;
628 GNUNET_assert (NULL != mq_entry);
631 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
632 size = ntohs (mq_entry->msg->size);
633 memcpy (buf, mq_entry->msg, size);
634 GNUNET_free (mq_entry->msg);
635 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
636 GNUNET_free (mq_entry);
638 if (NULL != mq_entry)
640 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
641 ntohs (mq_entry->msg->size),
642 GNUNET_TIME_UNIT_FOREVER_REL,
643 &transmit_ready_notify, NULL);
649 * Queues a message in send queue for sending to the service
651 * @param client the client to whom the queued message has to be sent
652 * @param msg the message to queue
655 queue_message (struct GNUNET_SERVER_Client *client,
656 struct GNUNET_MessageHeader *msg)
658 struct MessageQueue *mq_entry;
662 type = ntohs (msg->type);
663 size = ntohs (msg->size);
664 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
665 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
666 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
668 mq_entry->client = client;
669 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
671 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
672 if (NULL == transmit_handle)
674 GNUNET_SERVER_notify_transmit_ready (client, size,
675 GNUNET_TIME_UNIT_FOREVER_REL,
676 &transmit_ready_notify, NULL);
681 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
683 * @param ptr the memory block to realloc
684 * @param size the size of ptr
685 * @param new_size the size to which ptr has to be realloc'ed
686 * @return the newly reallocated memory block
689 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
691 ptr = GNUNET_realloc (ptr, new_size);
693 ptr = memset (ptr + size, 0, new_size - size);
699 * Function to add a host to the current list of known hosts
701 * @param host the host to add
702 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
706 host_list_add (struct GNUNET_TESTBED_Host *host)
710 host_id = GNUNET_TESTBED_host_get_id_ (host);
711 if (host_list_size <= host_id)
714 TESTBED_realloc (host_list,
715 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
716 sizeof (struct GNUNET_TESTBED_Host *) *
717 (host_list_size + LIST_GROW_STEP));
718 host_list_size += LIST_GROW_STEP;
720 if (NULL != host_list[host_id])
722 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
723 return GNUNET_SYSERR;
725 host_list[host_id] = host;
731 * Adds a route to the route list
733 * @param route the route to add
736 route_list_add (struct Route *route)
738 if (route->dest >= route_list_size)
741 TESTBED_realloc (route_list, sizeof (struct Route *) * route_list_size,
742 sizeof (struct Route *) * (route_list_size +
744 route_list_size += LIST_GROW_STEP;
746 GNUNET_assert (NULL == route_list[route->dest]);
747 route_list[route->dest] = route;
752 * Adds a slave to the slave array
754 * @param slave the slave controller to add
757 slave_list_add (struct Slave *slave)
759 if (slave->host_id >= slave_list_size)
762 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
763 sizeof (struct Slave *) * (slave_list_size +
765 slave_list_size += LIST_GROW_STEP;
767 GNUNET_assert (NULL == slave_list[slave->host_id]);
768 slave_list[slave->host_id] = slave;
773 * Adds a peer to the peer array
775 * @param peer the peer to add
778 peer_list_add (struct Peer *peer)
782 orig_size = peer_list_size;
783 if (peer->id >= peer_list_size)
785 while (peer->id >= peer_list_size)
786 peer_list_size += LIST_GROW_STEP;
788 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
789 sizeof (struct Peer *) * peer_list_size);
791 GNUNET_assert (NULL == peer_list[peer->id]);
792 peer_list[peer->id] = peer;
797 * Removes a the give peer from the peer array
799 * @param peer the peer to be removed
802 peer_list_remove (struct Peer *peer)
807 peer_list[peer->id] = NULL;
808 orig_size = peer_list_size;
809 while (peer_list_size >= LIST_GROW_STEP)
811 for (id = peer_list_size - 1;
812 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
813 if (NULL != peer_list[id])
815 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
817 peer_list_size -= LIST_GROW_STEP;
819 if (orig_size == peer_list_size)
822 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
827 * Finds the route with directly connected host as destination through which
828 * the destination host can be reached
830 * @param host_id the id of the destination host
831 * @return the route with directly connected destination host; NULL if no route
834 static struct Route *
835 find_dest_route (uint32_t host_id)
839 while (NULL != (route = route_list[host_id]))
841 if (route->thru == master_context->host_id)
843 host_id = route->thru;
850 * Routes message to a host given its host_id
852 * @param host_id the id of the destination host
853 * @param msg the message to be routed
856 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
863 * Send operation failure message to client
865 * @param client the client to which the failure message has to be sent to
866 * @param operation_id the id of the failed operation
867 * @param emsg the error message; can be NULL
870 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
871 uint64_t operation_id, const char *emsg)
873 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
877 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
878 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
880 msg = GNUNET_malloc (msize);
881 msg->header.size = htons (msize);
882 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
883 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
884 msg->operation_id = GNUNET_htonll (operation_id);
886 memcpy (&msg[1], emsg, emsg_len);
887 queue_message (client, &msg->header);
892 * Function to send generic operation success message to given client
894 * @param client the client to send the message to
895 * @param operation_id the id of the operation which was successful
898 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
899 uint64_t operation_id)
901 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
904 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
905 msg = GNUNET_malloc (msize);
906 msg->header.size = htons (msize);
907 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
908 msg->operation_id = GNUNET_htonll (operation_id);
909 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
910 queue_message (client, &msg->header);
915 * The Link Controller forwarding task
917 * @param cls the LCFContext
918 * @param tc the Task context from scheduler
921 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
925 * Completion callback for host registrations while forwarding Link Controller messages
927 * @param cls the LCFContext
928 * @param emsg the error message; NULL if host registration is successful
931 lcf_proc_cc (void *cls, const char *emsg)
933 struct LCFContext *lcf = cls;
936 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
941 goto registration_error;
942 lcf->state = DELEGATED_HOST_REGISTERED;
943 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
945 case DELEGATED_HOST_REGISTERED:
947 goto registration_error;
948 lcf->state = SLAVE_HOST_REGISTERED;
949 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
952 GNUNET_assert (0); /* Shouldn't reach here */
957 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
959 lcf->state = FINISHED;
960 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
965 * Callback to be called when forwarded link controllers operation is
966 * successfull. We have to relay the reply msg back to the client
968 * @param cls ForwardedOperationContext
969 * @param msg the peer create success message
972 forwarded_operation_reply_relay (void *cls,
973 const struct GNUNET_MessageHeader *msg)
975 struct ForwardedOperationContext *fopc = cls;
976 struct GNUNET_MessageHeader *dup_msg;
979 msize = ntohs (msg->size);
980 dup_msg = GNUNET_malloc (msize);
981 (void) memcpy (dup_msg, msg, msize);
982 queue_message (fopc->client, dup_msg);
983 GNUNET_SERVER_client_drop (fopc->client);
984 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
990 * Task to free resources when forwarded link controllers has been timedout
992 * @param cls the ForwardedOperationContext
993 * @param tc the task context from scheduler
996 forwarded_operation_timeout (void *cls,
997 const struct GNUNET_SCHEDULER_TaskContext *tc)
999 struct ForwardedOperationContext *fopc = cls;
1001 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1002 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1003 GNUNET_SERVER_client_drop (fopc->client);
1009 * The Link Controller forwarding task
1011 * @param cls the LCFContext
1012 * @param tc the Task context from scheduler
1015 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1017 struct LCFContext *lcf = cls;
1018 struct LCFContextQueue *lcfq;
1019 struct ForwardedOperationContext *fopc;
1021 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1026 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1027 lcf->gateway->controller))
1030 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1031 host_list[lcf->delegated_host_id],
1036 lcf->state = DELEGATED_HOST_REGISTERED;
1037 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1040 case DELEGATED_HOST_REGISTERED:
1042 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1043 lcf->gateway->controller))
1046 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1047 host_list[lcf->slave_host_id],
1052 lcf->state = SLAVE_HOST_REGISTERED;
1053 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1056 case SLAVE_HOST_REGISTERED:
1057 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1058 fopc->client = lcf->client;
1059 fopc->operation_id = lcf->operation_id;
1061 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1064 &forwarded_operation_reply_relay,
1066 fopc->timeout_task =
1067 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1069 lcf->state = FINISHED;
1070 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1074 GNUNET_assert (lcfq->lcf == lcf);
1075 GNUNET_free (lcf->msg);
1077 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1079 if (NULL != lcfq_head)
1081 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1087 * Callback for event from slave controllers
1089 * @param cls struct Slave *
1090 * @param event information about the event
1093 slave_event_callback (void *cls,
1094 const struct GNUNET_TESTBED_EventInformation *event)
1101 * Callback to signal successfull startup of the controller process
1103 * @param cls the closure from GNUNET_TESTBED_controller_start()
1104 * @param cfg the configuration with which the controller has been started;
1105 * NULL if status is not GNUNET_OK
1106 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1107 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1110 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1113 struct LinkControllersContext *lcc = cls;
1115 if (GNUNET_SYSERR == status)
1117 lcc->slave->controller_proc = NULL;
1118 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1119 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1122 lcc->slave->controller =
1123 GNUNET_TESTBED_controller_connect (cfg, host_list[lcc->slave->host_id],
1124 master_context->event_mask,
1125 &slave_event_callback, lcc->slave);
1126 if (NULL != lcc->slave->controller)
1127 send_operation_success_msg (lcc->client, lcc->operation_id);
1129 send_operation_fail_msg (lcc->client, lcc->operation_id,
1130 "Could not connect to delegated controller");
1131 GNUNET_SERVER_client_drop (lcc->client);
1137 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1140 * @param client identification of the client
1141 * @param message the actual message
1144 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1145 const struct GNUNET_MessageHeader *message)
1147 const struct GNUNET_TESTBED_InitMessage *msg;
1148 struct GNUNET_TESTBED_Host *host;
1149 const char *controller_hostname;
1152 if (NULL != master_context)
1155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1158 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1159 msize = ntohs (message->size);
1160 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1166 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1167 controller_hostname = (const char *) &msg[1];
1168 if ('\0' != controller_hostname[msize - 1])
1171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1174 master_context = GNUNET_malloc (sizeof (struct Context));
1175 master_context->client = client;
1176 master_context->host_id = ntohl (msg->host_id);
1177 master_context->master_ip = GNUNET_strdup (controller_hostname);
1178 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1179 master_context->system =
1180 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1182 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1184 host_list_add (host);
1185 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1186 GNUNET_SERVER_client_keep (client);
1187 LOG_DEBUG ("Created master context with host ID: %u\n",
1188 master_context->host_id);
1189 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1194 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1197 * @param client identification of the client
1198 * @param message the actual message
1201 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1202 const struct GNUNET_MessageHeader *message)
1204 struct GNUNET_TESTBED_Host *host;
1205 const struct GNUNET_TESTBED_AddHostMessage *msg;
1206 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1211 uint16_t username_length;
1212 uint16_t hostname_length;
1213 uint16_t reply_size;
1216 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1217 msize = ntohs (msg->header.size);
1218 username = (char *) &(msg[1]);
1219 username_length = ntohs (msg->user_name_length);
1220 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1221 if (0 != username_length)
1222 GNUNET_assert ('\0' == username[username_length]);
1223 username_length = (0 == username_length) ? 0 : username_length + 1;
1224 hostname = username + username_length;
1226 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1227 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1228 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1229 host_id = ntohl (msg->host_id);
1230 LOG_DEBUG ("Received ADDHOST message\n");
1231 LOG_DEBUG ("-------host id: %u\n", host_id);
1232 LOG_DEBUG ("-------hostname: %s\n", hostname);
1233 if (0 != username_length)
1234 LOG_DEBUG ("-------username: %s\n", username);
1237 LOG_DEBUG ("-------username: NULL\n");
1240 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1242 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1243 ntohs (msg->ssh_port));
1244 GNUNET_assert (NULL != host);
1245 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1246 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1247 if (GNUNET_OK != host_list_add (host))
1249 /* We are unable to add a host */
1250 emsg = "A host exists with given host-id";
1251 LOG_DEBUG ("%s: %u", emsg, host_id);
1252 GNUNET_TESTBED_host_destroy (host);
1253 reply_size += strlen (emsg) + 1;
1254 reply = GNUNET_malloc (reply_size);
1255 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1258 reply = GNUNET_malloc (reply_size);
1259 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1260 reply->header.size = htons (reply_size);
1261 reply->host_id = htonl (host_id);
1262 queue_message (client, &reply->header);
1267 * Iterator over hash map entries.
1269 * @param cls closure
1270 * @param key current key code
1271 * @param value value in the hash map
1272 * @return GNUNET_YES if we should continue to
1277 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1279 struct SharedService *queried_ss = cls;
1280 struct SharedService *ss = value;
1282 if (0 == strcmp (ss->name, queried_ss->name))
1290 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1293 * @param client identification of the client
1294 * @param message the actual message
1297 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1298 const struct GNUNET_MessageHeader *message)
1300 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1301 struct SharedService *ss;
1303 struct GNUNET_HashCode hash;
1305 uint16_t service_name_size;
1307 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1308 msg_size = ntohs (message->size);
1309 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1316 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1317 service_name = (char *) &msg[1];
1318 if ('\0' != service_name[service_name_size - 1])
1321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1324 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1325 service_name, ntohl (msg->num_peers));
1326 if (ntohl (msg->host_id) != master_context->host_id)
1328 route_message (ntohl (msg->host_id), message);
1329 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1332 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1333 ss = GNUNET_malloc (sizeof (struct SharedService));
1334 ss->name = strdup (service_name);
1335 ss->num_shared = ntohl (msg->num_peers);
1336 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1337 if (GNUNET_SYSERR ==
1338 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1339 &ss_exists_iterator, ss))
1341 LOG (GNUNET_ERROR_TYPE_WARNING,
1342 "Service %s already configured as a shared service. "
1343 "Ignoring service sharing request \n", ss->name);
1344 GNUNET_free (ss->name);
1348 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1349 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1354 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1357 * @param client identification of the client
1358 * @param message the actual message
1361 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1362 const struct GNUNET_MessageHeader *message)
1364 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1365 struct GNUNET_CONFIGURATION_Handle *cfg;
1366 struct LCFContextQueue *lcfq;
1367 struct Route *route;
1368 struct Route *new_route;
1372 uint32_t delegated_host_id;
1373 uint32_t slave_host_id;
1376 if (NULL == master_context)
1379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1382 msize = ntohs (message->size);
1383 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1389 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1390 delegated_host_id = ntohl (msg->delegated_host_id);
1391 if (delegated_host_id == master_context->host_id)
1394 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1398 if ((delegated_host_id >= host_list_size) ||
1399 (NULL == host_list[delegated_host_id]))
1401 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1405 slave_host_id = ntohl (msg->slave_host_id);
1406 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1408 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1412 if (slave_host_id == delegated_host_id)
1414 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1419 if (slave_host_id == master_context->host_id) /* Link from us */
1421 struct Slave *slave;
1422 struct LinkControllersContext *lcc;
1424 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1425 config_size = ntohs (msg->config_size);
1426 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1428 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1433 config = GNUNET_malloc (config_size);
1434 dest_size = (uLongf) config_size;
1436 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1439 GNUNET_break (0); /* Compression error */
1440 GNUNET_free (config);
1441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1444 if (config_size != dest_size)
1446 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1447 GNUNET_free (config);
1448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1451 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1453 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1455 GNUNET_break (0); /* Configuration parsing error */
1456 GNUNET_free (config);
1457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1460 GNUNET_free (config);
1461 if ((delegated_host_id < slave_list_size) &&
1462 (NULL != slave_list[delegated_host_id]))
1464 GNUNET_break (0); /* Configuration parsing error */
1465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1468 slave = GNUNET_malloc (sizeof (struct Slave));
1469 slave->host_id = delegated_host_id;
1470 slave_list_add (slave);
1471 if (1 != msg->is_subordinate)
1474 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1475 master_context->event_mask,
1476 &slave_event_callback, slave);
1477 GNUNET_CONFIGURATION_destroy (cfg);
1478 if (NULL != slave->controller)
1479 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1481 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1482 "Could not connect to delegated controller");
1483 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1486 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1487 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1488 GNUNET_SERVER_client_keep (client);
1489 lcc->client = client;
1491 slave->controller_proc =
1492 GNUNET_TESTBED_controller_start (master_context->master_ip,
1493 host_list[slave->host_id], cfg,
1494 &slave_status_callback, lcc);
1495 GNUNET_CONFIGURATION_destroy (cfg);
1496 new_route = GNUNET_malloc (sizeof (struct Route));
1497 new_route->dest = delegated_host_id;
1498 new_route->thru = master_context->host_id;
1499 route_list_add (new_route);
1500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1504 /* Route the request */
1505 if (slave_host_id >= route_list_size)
1507 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1511 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1512 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1513 lcfq->lcf->delegated_host_id = delegated_host_id;
1514 lcfq->lcf->slave_host_id = slave_host_id;
1515 route = find_dest_route (slave_host_id);
1516 GNUNET_assert (NULL != route); /* because we add routes carefully */
1517 GNUNET_assert (route->dest < slave_list_size);
1518 GNUNET_assert (NULL != slave_list[route->dest]);
1519 lcfq->lcf->state = INIT;
1520 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1521 lcfq->lcf->gateway = slave_list[route->dest];
1522 lcfq->lcf->msg = GNUNET_malloc (msize);
1523 (void) memcpy (lcfq->lcf->msg, msg, msize);
1524 GNUNET_SERVER_client_keep (client);
1525 lcfq->lcf->client = client;
1526 if (NULL == lcfq_head)
1528 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1529 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1530 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1533 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1534 /* FIXME: Adding a new route should happen after the controllers are linked
1536 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1537 new_route = GNUNET_malloc (sizeof (struct Route));
1538 new_route->dest = delegated_host_id;
1539 new_route->thru = route->dest;
1540 route_list_add (new_route);
1545 * The task to be executed if the forwarded peer create operation has been
1548 * @param cls the FowardedOperationContext
1549 * @param tc the TaskContext from the scheduler
1552 peer_create_forward_timeout (void *cls,
1553 const struct GNUNET_SCHEDULER_TaskContext *tc)
1555 struct ForwardedOperationContext *fo_ctxt = cls;
1557 /* send error msg to client */
1558 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1559 GNUNET_SERVER_client_drop (fo_ctxt->client);
1560 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1561 GNUNET_free (fo_ctxt);
1566 * Callback to be called when forwarded peer create operation is
1567 * successfull. We have to relay the reply msg back to the client
1569 * @param cls ForwardedOperationContext
1570 * @param msg the peer create success message
1573 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1575 struct ForwardedOperationContext *fo_ctxt = cls;
1576 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1577 struct GNUNET_MessageHeader *dup_msg;
1581 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1582 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1585 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1586 peer = GNUNET_malloc (sizeof (struct Peer));
1587 peer->is_remote = GNUNET_YES;
1588 peer->id = ntohl (success_msg->peer_id);
1589 GNUNET_assert (NULL != fo_ctxt->cls);
1590 peer->details.remote.controller = fo_ctxt->cls;
1591 peer_list_add (peer);
1593 msize = ntohs (msg->size);
1594 dup_msg = GNUNET_malloc (msize);
1595 (void) memcpy (dup_msg, msg, msize);
1596 queue_message (fo_ctxt->client, dup_msg);
1597 GNUNET_SERVER_client_drop (fo_ctxt->client);
1598 GNUNET_free (fo_ctxt);
1604 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1607 * @param client identification of the client
1608 * @param message the actual message
1611 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1612 const struct GNUNET_MessageHeader *message)
1614 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1615 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1616 struct GNUNET_CONFIGURATION_Handle *cfg;
1617 struct ForwardedOperationContext *fo_ctxt;
1618 struct Route *route;
1623 uint32_t config_size;
1629 msize = ntohs (message->size);
1630 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1632 GNUNET_break (0); /* We need configuration */
1633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1636 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1637 host_id = ntohl (msg->host_id);
1638 peer_id = ntohl (msg->peer_id);
1639 if (UINT32_MAX == peer_id)
1641 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1642 "Cannot create peer with given ID");
1643 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1646 if (host_id == master_context->host_id)
1650 /* We are responsible for this peer */
1651 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1652 config_size = ntohl (msg->config_size);
1653 config = GNUNET_malloc (config_size);
1654 dest_size = config_size;
1657 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1658 (const Bytef *) &msg[1], (uLong) msize)))
1660 GNUNET_break (0); /* uncompression error */
1661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1664 if (config_size != dest_size)
1666 GNUNET_break (0); /* Uncompressed config size mismatch */
1667 GNUNET_free (config);
1668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1671 cfg = GNUNET_CONFIGURATION_create ();
1673 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1675 GNUNET_break (0); /* Configuration parsing error */
1676 GNUNET_free (config);
1677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1680 GNUNET_free (config);
1681 peer = GNUNET_malloc (sizeof (struct Peer));
1682 peer->is_remote = GNUNET_NO;
1683 peer->details.local.cfg = cfg;
1685 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1686 peer->details.local.peer =
1687 GNUNET_TESTING_peer_configure (master_context->system,
1688 peer->details.local.cfg, peer->id,
1689 NULL /* Peer id */ ,
1691 if (NULL == peer->details.local.peer)
1693 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1697 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1700 peer->details.local.is_running = GNUNET_NO;
1701 peer_list_add (peer);
1703 GNUNET_malloc (sizeof
1704 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1705 reply->header.size =
1706 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1707 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1708 reply->peer_id = msg->peer_id;
1709 reply->operation_id = msg->operation_id;
1710 queue_message (client, &reply->header);
1711 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1715 /* Forward peer create request */
1716 route = find_dest_route (host_id);
1720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1723 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1724 GNUNET_SERVER_client_keep (client);
1725 fo_ctxt->client = client;
1726 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1727 fo_ctxt->cls = slave_list[route->dest]->controller;
1729 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1730 [route->dest]->controller,
1731 fo_ctxt->operation_id,
1733 peer_create_success_cb, fo_ctxt);
1734 fo_ctxt->timeout_task =
1735 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1738 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1743 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1746 * @param client identification of the client
1747 * @param message the actual message
1750 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1751 const struct GNUNET_MessageHeader *message)
1753 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1754 struct ForwardedOperationContext *fopc;
1758 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1759 peer_id = ntohl (msg->peer_id);
1760 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1761 peer_id, GNUNET_ntohll (msg->operation_id));
1762 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1764 LOG (GNUNET_ERROR_TYPE_ERROR,
1765 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1766 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1767 "Peer doesn't exist");
1768 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1771 peer = peer_list[peer_id];
1772 if (GNUNET_YES == peer->is_remote)
1774 /* Forward the destory message to sub controller */
1775 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1776 GNUNET_SERVER_client_keep (client);
1777 fopc->client = client;
1778 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1780 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1781 fopc->operation_id, &msg->header,
1782 &forwarded_operation_reply_relay,
1784 fopc->timeout_task =
1785 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1787 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1790 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1791 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1792 peer_list_remove (peer);
1794 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1795 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1800 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1803 * @param client identification of the client
1804 * @param message the actual message
1807 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1808 const struct GNUNET_MessageHeader *message)
1810 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1811 struct GNUNET_TESTBED_PeerEventMessage *reply;
1812 struct ForwardedOperationContext *fopc;
1816 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1817 peer_id = ntohl (msg->peer_id);
1818 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1821 LOG (GNUNET_ERROR_TYPE_ERROR,
1822 "Asked to start a non existent peer with id: %u\n", peer_id);
1823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1826 peer = peer_list[peer_id];
1827 if (GNUNET_YES == peer->is_remote)
1829 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1830 GNUNET_SERVER_client_keep (client);
1831 fopc->client = client;
1832 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1834 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1835 fopc->operation_id, &msg->header,
1836 &forwarded_operation_reply_relay,
1838 fopc->timeout_task =
1839 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1844 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1846 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1848 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1851 peer->details.local.is_running = GNUNET_YES;
1852 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1853 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1854 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1855 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1856 reply->host_id = htonl (master_context->host_id);
1857 reply->peer_id = msg->peer_id;
1858 reply->operation_id = msg->operation_id;
1859 queue_message (client, &reply->header);
1860 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1865 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1868 * @param client identification of the client
1869 * @param message the actual message
1872 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1873 const struct GNUNET_MessageHeader *message)
1875 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1876 struct GNUNET_TESTBED_PeerEventMessage *reply;
1877 struct ForwardedOperationContext *fopc;
1881 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1882 peer_id = ntohl (msg->peer_id);
1883 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1885 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1887 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1890 peer = peer_list[peer_id];
1891 if (GNUNET_YES == peer->is_remote)
1893 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1894 GNUNET_SERVER_client_keep (client);
1895 fopc->client = client;
1896 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1898 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1899 fopc->operation_id, &msg->header,
1900 &forwarded_operation_reply_relay,
1902 fopc->timeout_task =
1903 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1905 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1908 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1910 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1911 "Peer not running");
1912 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1915 peer->details.local.is_running = GNUNET_NO;
1916 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1917 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1918 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1919 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1920 reply->host_id = htonl (master_context->host_id);
1921 reply->peer_id = msg->peer_id;
1922 reply->operation_id = msg->operation_id;
1923 queue_message (client, &reply->header);
1924 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1929 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1932 * @param client identification of the client
1933 * @param message the actual message
1936 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1937 const struct GNUNET_MessageHeader *message)
1939 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1940 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1949 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1950 peer_id = ntohl (msg->peer_id);
1951 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1953 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1958 peer = peer_list[peer_id];
1959 if (GNUNET_YES == peer->is_remote)
1961 /* FIXME: forward to sub controller */
1963 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1967 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
1969 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1970 GNUNET_free (config);
1973 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1974 reply = GNUNET_realloc (xconfig, msize);
1975 (void) memmove (&reply[1], reply, xc_size);
1976 reply->header.size = htons (msize);
1977 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1978 reply->peer_id = msg->peer_id;
1979 reply->operation_id = msg->operation_id;
1980 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
1981 &reply->peer_identity);
1982 reply->config_size = htons ((uint16_t) c_size);
1983 queue_message (client, &reply->header);
1984 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1989 * Task for cleaing up overlay connect context structure
1991 * @param cls the overlay connect context
1992 * @param tc the task context
1995 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1997 struct OverlayConnectContext *occ = cls;
1999 LOG_DEBUG ("Cleaning up occ\n");
2000 GNUNET_free_non_null (occ->emsg);
2001 GNUNET_free_non_null (occ->hello);
2002 if (NULL != occ->opc)
2003 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2004 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2005 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2006 if (NULL != occ->ch)
2007 GNUNET_CORE_disconnect (occ->ch);
2008 if (NULL != occ->ghh)
2009 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2010 if (NULL != occ->p1th)
2011 GNUNET_TRANSPORT_disconnect (occ->p1th);
2012 if (NULL != occ->p2th)
2013 GNUNET_TRANSPORT_disconnect (occ->p2th);
2019 * Task which will be run when overlay connect request has been timed out
2021 * @param cls the OverlayConnectContext
2022 * @param tc the TaskContext
2025 timeout_overlay_connect (void *cls,
2026 const struct GNUNET_SCHEDULER_TaskContext *tc)
2028 struct OverlayConnectContext *occ = cls;
2030 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2031 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2032 GNUNET_SERVER_client_drop (occ->client);
2033 occ_cleanup (occ, tc);
2039 * Function called to notify transport users that another
2040 * peer connected to us.
2042 * @param cls closure
2043 * @param new_peer the peer that connected
2044 * @param ats performance data
2045 * @param ats_count number of entries in ats (excluding 0-termination)
2048 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2049 const struct GNUNET_ATS_Information *ats,
2050 unsigned int ats_count)
2052 struct OverlayConnectContext *occ = cls;
2053 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2055 char *other_peer_str;
2057 LOG_DEBUG ("Overlay connect notify\n");
2059 memcmp (new_peer, &occ->peer_identity,
2060 sizeof (struct GNUNET_PeerIdentity)))
2062 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2063 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2065 memcmp (new_peer, &occ->other_peer_identity,
2066 sizeof (struct GNUNET_PeerIdentity)))
2068 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2069 new_peer_str, other_peer_str);
2070 GNUNET_free (new_peer_str);
2071 GNUNET_free (other_peer_str);
2074 GNUNET_free (new_peer_str);
2075 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2076 GNUNET_i2s (&occ->peer_identity));
2077 GNUNET_free (other_peer_str);
2078 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2080 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2081 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2083 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2084 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2085 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2086 GNUNET_free_non_null (occ->emsg);
2088 if (NULL != occ->p2th)
2089 GNUNET_TRANSPORT_disconnect (occ->p2th);
2091 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2092 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2094 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2095 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2096 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2097 msg->peer1 = htonl (occ->peer->id);
2098 msg->peer2 = htonl (occ->other_peer->id);
2099 msg->operation_id = GNUNET_htonll (occ->op_id);
2100 queue_message (occ->client, &msg->header);
2101 GNUNET_SERVER_client_drop (occ->client);
2102 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2107 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2110 * @param cls the OverlayConnectContext
2111 * @param tc the TaskContext from scheduler
2114 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2116 struct OverlayConnectContext *occ = cls;
2117 char *other_peer_str;
2119 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2120 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2122 GNUNET_assert (NULL != occ->hello);
2123 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2124 if (GNUNET_YES == occ->other_peer->is_remote)
2126 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2128 uint16_t hello_size;
2130 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2131 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2132 hello_size = ntohs (occ->hello->size);
2133 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2134 msg = GNUNET_malloc (msize);
2135 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2136 msg->header.size = htons (msize);
2137 msg->peer = htonl (occ->peer->id);
2138 msg->operation_id = GNUNET_htonll (occ->op_id);
2139 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2140 sizeof (struct GNUNET_PeerIdentity));
2141 memcpy (msg->hello, occ->hello, hello_size);
2142 GNUNET_TESTBED_queue_message_ (occ->other_peer->details.remote.controller,
2147 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2148 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2149 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2150 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2152 GNUNET_free (other_peer_str);
2153 occ->send_hello_task =
2154 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2159 * Test for checking whether HELLO message is empty
2161 * @param cls empty flag to set
2162 * @param address the HELLO
2163 * @param expiration expiration of the HELLO
2167 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2168 struct GNUNET_TIME_Absolute expiration)
2178 * Function called whenever there is an update to the HELLO of peers in the
2179 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2180 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2182 * @param cls closure
2183 * @param hello our updated HELLO
2186 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2188 struct OverlayConnectContext *occ = cls;
2192 msize = ntohs (hello->size);
2194 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2195 hello, GNUNET_NO, &test_address,
2197 if (GNUNET_YES == empty)
2199 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2202 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2203 occ->hello = GNUNET_malloc (msize);
2204 memcpy (occ->hello, hello, msize);
2205 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2207 GNUNET_TRANSPORT_disconnect (occ->p1th);
2209 GNUNET_free_non_null (occ->emsg);
2210 if (GNUNET_NO == occ->other_peer->is_remote)
2213 GNUNET_TRANSPORT_connect (occ->other_peer->details.local.cfg,
2214 &occ->other_peer_identity, NULL, NULL, NULL,
2216 if (NULL == occ->p2th)
2218 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2219 GNUNET_i2s (&occ->other_peer_identity));
2220 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2221 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2225 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2226 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2231 * Function called after GNUNET_CORE_connect has succeeded (or failed
2232 * for good). Note that the private key of the peer is intentionally
2233 * not exposed here; if you need it, your process should try to read
2234 * the private key file directly (which should work if you are
2237 * @param cls closure
2238 * @param server handle to the server, NULL if we failed
2239 * @param my_identity ID of this peer, NULL if we failed
2242 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2243 const struct GNUNET_PeerIdentity *my_identity)
2245 struct OverlayConnectContext *occ = cls;
2247 GNUNET_free_non_null (occ->emsg);
2248 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2249 if ((NULL == server) || (NULL == my_identity))
2251 GNUNET_free (occ->emsg);
2254 memcpy (&occ->peer_identity, my_identity,
2255 sizeof (struct GNUNET_PeerIdentity));
2257 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2258 &occ->peer_identity, NULL, NULL, NULL, NULL);
2259 if (NULL == occ->p1th)
2261 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2262 GNUNET_i2s (&occ->peer_identity));
2265 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2266 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2267 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2271 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2272 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2278 * Callback to be called when forwarded get peer config operation as part of
2279 * overlay connect is successfull. Connection to Peer 1's core is made and is
2280 * checked for new connection from peer 2
2282 * @param cls ForwardedOperationContext
2283 * @param msg the peer create success message
2286 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2288 struct OverlayConnectContext *occ = cls;
2289 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2290 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2295 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2297 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2299 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2300 sizeof (struct GNUNET_PeerIdentity));
2301 GNUNET_free_non_null (occ->emsg);
2302 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2304 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2305 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2306 GNUNET_NO, no_handlers);
2307 if (NULL == occ->ch)
2312 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2314 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2319 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2322 * @param client identification of the client
2323 * @param message the actual message
2326 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2327 const struct GNUNET_MessageHeader *message)
2329 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2330 struct OverlayConnectContext *occ;
2331 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2337 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2338 p1 = ntohl (msg->peer1);
2339 p2 = ntohl (msg->peer2);
2340 GNUNET_assert (p1 < peer_list_size);
2341 GNUNET_assert (NULL != peer_list[p1]);
2342 GNUNET_assert (p2 < peer_list_size);
2343 GNUNET_assert (NULL != peer_list[p2]);
2344 /* FIXME: Add cases where we have to forward overlay connect message to sub
2346 GNUNET_assert (GNUNET_NO == peer_list[p1]->is_remote);
2347 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2348 GNUNET_SERVER_client_keep (client);
2349 occ->client = client;
2350 occ->peer = peer_list[p1];
2351 occ->other_peer = peer_list[p2];
2352 occ->op_id = GNUNET_ntohll (msg->operation_id);
2353 /* Get the identity of the second peer */
2354 if (GNUNET_YES == occ->other_peer->is_remote)
2356 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2359 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2360 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2361 cmsg.peer_id = msg->peer2;
2362 cmsg.operation_id = msg->operation_id;
2364 GNUNET_TESTBED_forward_operation_msg_ (occ->other_peer->details.remote.controller,
2365 occ->op_id, &cmsg.header,
2366 &overlay_connect_get_config,
2369 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2371 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2372 (GNUNET_TIME_UNIT_SECONDS, 30),
2373 &timeout_overlay_connect, occ);
2376 GNUNET_TESTING_peer_get_identity (occ->other_peer->details.local.peer,
2377 &occ->other_peer_identity);
2378 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2379 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2381 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2382 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2383 GNUNET_NO, no_handlers);
2384 if (NULL == occ->ch)
2386 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2389 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2390 (GNUNET_TIME_UNIT_SECONDS, 30),
2391 &timeout_overlay_connect, occ);
2392 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2397 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2404 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2406 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2407 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2408 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2409 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2410 GNUNET_TRANSPORT_disconnect (rocc->th);
2411 GNUNET_free_non_null (rocc->hello);
2417 * Task to timeout rocc and cleanit up
2419 * @param cls the RequestOverlayConnectContext
2420 * @param tc the TaskContext from scheduler
2423 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2425 struct RequestOverlayConnectContext *rocc = cls;
2427 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2428 cleanup_rocc (rocc);
2433 * Function called to notify transport users that another
2434 * peer connected to us.
2436 * @param cls closure
2437 * @param peer the peer that connected
2438 * @param ats performance data
2439 * @param ats_count number of entries in ats (excluding 0-termination)
2442 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2443 const struct GNUNET_ATS_Information * ats,
2446 struct RequestOverlayConnectContext *rocc = cls;
2448 LOG_DEBUG ("Request Overlay connect notify\n");
2449 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2453 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
2454 cleanup_rocc (rocc);
2459 * Task to offer the HELLO message to the peer and ask it to connect to the peer
2460 * whose identity is in RequestOverlayConnectContext
2462 * @param cls the RequestOverlayConnectContext
2463 * @param tc the TaskContext from scheduler
2466 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2468 struct RequestOverlayConnectContext *rocc = cls;
2470 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
2471 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
2472 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
2473 rocc->attempt_connect_task_id =
2474 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2475 &attempt_connect_task, rocc);
2480 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2483 * @param client identification of the client
2484 * @param message the actual message
2487 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2488 const struct GNUNET_MessageHeader *message)
2490 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2491 struct RequestOverlayConnectContext *rocc;
2497 msize = ntohs (message->size);
2498 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
2501 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2504 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2505 if ((NULL == msg->hello) ||
2506 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2512 hsize = ntohs (msg->hello->size);
2513 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
2516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2519 peer_id = ntohl (msg->peer);
2520 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2522 GNUNET_break_op (0);
2523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2526 if (GNUNET_NO != peer->is_remote)
2529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2532 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
2533 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
2534 NULL, &transport_connect_notify, NULL);
2535 if (NULL == rocc->th)
2539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2542 memcpy (&rocc->a_id, &msg->peer_identity,
2543 sizeof (struct GNUNET_PeerIdentity));
2544 rocc->hello = GNUNET_malloc (hsize);
2545 memcpy (rocc->hello, msg->hello, hsize);
2546 /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
2547 /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
2548 rocc->attempt_connect_task_id =
2549 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
2550 rocc->timeout_rocc_task_id =
2551 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
2552 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2557 * Iterator over hash map entries.
2559 * @param cls closure
2560 * @param key current key code
2561 * @param value value in the hash map
2562 * @return GNUNET_YES if we should continue to
2567 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2569 struct SharedService *ss = value;
2571 GNUNET_assert (GNUNET_YES ==
2572 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2573 GNUNET_free (ss->name);
2580 * Task to clean up and shutdown nicely
2583 * @param tc the TaskContext from scheduler
2586 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2588 struct LCFContextQueue *lcfq;
2591 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2592 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2593 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2595 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2596 if (NULL != lcfq_head)
2598 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2600 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2601 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2603 if (NULL != lcfq_head->lcf->rhandle)
2604 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2606 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2607 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2609 GNUNET_free (lcfq->lcf->msg);
2610 GNUNET_free (lcfq->lcf);
2611 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2614 /* Clear peer list */
2615 for (id = 0; id < peer_list_size; id++)
2616 if (NULL != peer_list[id])
2618 if (GNUNET_NO == peer_list[id]->is_remote)
2620 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2621 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2622 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2623 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2625 GNUNET_free (peer_list[id]);
2627 GNUNET_free_non_null (peer_list);
2628 /* Clear host list */
2629 for (id = 0; id < host_list_size; id++)
2630 if (NULL != host_list[id])
2631 GNUNET_TESTBED_host_destroy (host_list[id]);
2632 GNUNET_free_non_null (host_list);
2633 /* Clear route list */
2634 for (id = 0; id < route_list_size; id++)
2635 if (NULL != route_list[id])
2636 GNUNET_free (route_list[id]);
2637 GNUNET_free_non_null (route_list);
2638 /* Clear slave_list */
2639 for (id = 0; id < slave_list_size; id++)
2640 if (NULL != slave_list[id])
2642 if (NULL != slave_list[id]->controller)
2643 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2644 if (NULL != slave_list[id]->controller_proc)
2645 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2647 if (NULL != master_context)
2649 GNUNET_free_non_null (master_context->master_ip);
2650 if (NULL != master_context->system)
2651 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2652 GNUNET_free (master_context);
2653 master_context = NULL;
2659 * Callback for client disconnect
2662 * @param client the client which has disconnected
2665 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2667 if (NULL == master_context)
2669 if (client == master_context->client)
2671 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2672 GNUNET_SERVER_client_drop (client);
2673 /* should not be needed as we're terminated by failure to read
2674 * from stdin, but if stdin fails for some reason, this shouldn't
2675 * hurt for now --- might need to revise this later if we ever
2676 * decide that master connections might be temporarily down
2677 * for some reason */
2678 //GNUNET_SCHEDULER_shutdown ();
2686 * @param cls closure
2687 * @param server the initialized server
2688 * @param cfg configuration to use
2691 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2692 const struct GNUNET_CONFIGURATION_Handle *cfg)
2694 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2695 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2696 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2697 {&handle_configure_shared_service, NULL,
2698 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2699 {&handle_link_controllers, NULL,
2700 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2701 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2702 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2703 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2704 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2705 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2706 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2707 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2708 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2709 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2710 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2711 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2712 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2713 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2717 GNUNET_SERVER_add_handlers (server, message_handlers);
2718 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2719 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2721 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2722 &shutdown_task, NULL);
2723 LOG_DEBUG ("Testbed startup complete\n");
2728 * The starting point of execution
2731 main (int argc, char *const *argv)
2733 //sleep (15); /* Debugging */
2734 return (GNUNET_OK ==
2735 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2736 &testbed_run, NULL)) ? 0 : 1;
2739 /* end of gnunet-service-testbed.c */