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 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
982 dup_msg = GNUNET_malloc (msize);
983 (void) memcpy (dup_msg, msg, msize);
984 queue_message (fopc->client, dup_msg);
985 GNUNET_SERVER_client_drop (fopc->client);
986 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
992 * Task to free resources when forwarded link controllers has been timedout
994 * @param cls the ForwardedOperationContext
995 * @param tc the task context from scheduler
998 forwarded_operation_timeout (void *cls,
999 const struct GNUNET_SCHEDULER_TaskContext *tc)
1001 struct ForwardedOperationContext *fopc = cls;
1003 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1004 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1005 GNUNET_SERVER_client_drop (fopc->client);
1011 * The Link Controller forwarding task
1013 * @param cls the LCFContext
1014 * @param tc the Task context from scheduler
1017 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1019 struct LCFContext *lcf = cls;
1020 struct LCFContextQueue *lcfq;
1021 struct ForwardedOperationContext *fopc;
1023 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1028 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1029 lcf->gateway->controller))
1032 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1033 host_list[lcf->delegated_host_id],
1038 lcf->state = DELEGATED_HOST_REGISTERED;
1039 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1042 case DELEGATED_HOST_REGISTERED:
1044 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1045 lcf->gateway->controller))
1048 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1049 host_list[lcf->slave_host_id],
1054 lcf->state = SLAVE_HOST_REGISTERED;
1055 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1058 case SLAVE_HOST_REGISTERED:
1059 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1060 fopc->client = lcf->client;
1061 fopc->operation_id = lcf->operation_id;
1063 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1066 &forwarded_operation_reply_relay,
1068 fopc->timeout_task =
1069 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1071 lcf->state = FINISHED;
1072 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1076 GNUNET_assert (lcfq->lcf == lcf);
1077 GNUNET_free (lcf->msg);
1079 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1081 if (NULL != lcfq_head)
1083 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1089 * Callback for event from slave controllers
1091 * @param cls struct Slave *
1092 * @param event information about the event
1095 slave_event_callback (void *cls,
1096 const struct GNUNET_TESTBED_EventInformation *event)
1103 * Callback to signal successfull startup of the controller process
1105 * @param cls the closure from GNUNET_TESTBED_controller_start()
1106 * @param cfg the configuration with which the controller has been started;
1107 * NULL if status is not GNUNET_OK
1108 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1109 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1112 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1115 struct LinkControllersContext *lcc = cls;
1117 if (GNUNET_SYSERR == status)
1119 lcc->slave->controller_proc = NULL;
1120 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1121 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1124 lcc->slave->controller =
1125 GNUNET_TESTBED_controller_connect (cfg, host_list[lcc->slave->host_id],
1126 master_context->event_mask,
1127 &slave_event_callback, lcc->slave);
1128 if (NULL != lcc->slave->controller)
1129 send_operation_success_msg (lcc->client, lcc->operation_id);
1131 send_operation_fail_msg (lcc->client, lcc->operation_id,
1132 "Could not connect to delegated controller");
1133 GNUNET_SERVER_client_drop (lcc->client);
1139 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1142 * @param client identification of the client
1143 * @param message the actual message
1146 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1147 const struct GNUNET_MessageHeader *message)
1149 const struct GNUNET_TESTBED_InitMessage *msg;
1150 struct GNUNET_TESTBED_Host *host;
1151 const char *controller_hostname;
1154 if (NULL != master_context)
1157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1160 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1161 msize = ntohs (message->size);
1162 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1168 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1169 controller_hostname = (const char *) &msg[1];
1170 if ('\0' != controller_hostname[msize - 1])
1173 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1176 master_context = GNUNET_malloc (sizeof (struct Context));
1177 master_context->client = client;
1178 master_context->host_id = ntohl (msg->host_id);
1179 master_context->master_ip = GNUNET_strdup (controller_hostname);
1180 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1181 master_context->system =
1182 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1184 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1186 host_list_add (host);
1187 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1188 GNUNET_SERVER_client_keep (client);
1189 LOG_DEBUG ("Created master context with host ID: %u\n",
1190 master_context->host_id);
1191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1196 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1199 * @param client identification of the client
1200 * @param message the actual message
1203 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1204 const struct GNUNET_MessageHeader *message)
1206 struct GNUNET_TESTBED_Host *host;
1207 const struct GNUNET_TESTBED_AddHostMessage *msg;
1208 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1213 uint16_t username_length;
1214 uint16_t hostname_length;
1215 uint16_t reply_size;
1218 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1219 msize = ntohs (msg->header.size);
1220 username = (char *) &(msg[1]);
1221 username_length = ntohs (msg->user_name_length);
1222 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1223 if (0 != username_length)
1224 GNUNET_assert ('\0' == username[username_length]);
1225 username_length = (0 == username_length) ? 0 : username_length + 1;
1226 hostname = username + username_length;
1228 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1229 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1230 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1231 host_id = ntohl (msg->host_id);
1232 LOG_DEBUG ("Received ADDHOST message\n");
1233 LOG_DEBUG ("-------host id: %u\n", host_id);
1234 LOG_DEBUG ("-------hostname: %s\n", hostname);
1235 if (0 != username_length)
1236 LOG_DEBUG ("-------username: %s\n", username);
1239 LOG_DEBUG ("-------username: NULL\n");
1242 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1244 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1245 ntohs (msg->ssh_port));
1246 GNUNET_assert (NULL != host);
1247 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1248 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1249 if (GNUNET_OK != host_list_add (host))
1251 /* We are unable to add a host */
1252 emsg = "A host exists with given host-id";
1253 LOG_DEBUG ("%s: %u", emsg, host_id);
1254 GNUNET_TESTBED_host_destroy (host);
1255 reply_size += strlen (emsg) + 1;
1256 reply = GNUNET_malloc (reply_size);
1257 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1260 reply = GNUNET_malloc (reply_size);
1261 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1262 reply->header.size = htons (reply_size);
1263 reply->host_id = htonl (host_id);
1264 queue_message (client, &reply->header);
1269 * Iterator over hash map entries.
1271 * @param cls closure
1272 * @param key current key code
1273 * @param value value in the hash map
1274 * @return GNUNET_YES if we should continue to
1279 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1281 struct SharedService *queried_ss = cls;
1282 struct SharedService *ss = value;
1284 if (0 == strcmp (ss->name, queried_ss->name))
1292 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1295 * @param client identification of the client
1296 * @param message the actual message
1299 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1300 const struct GNUNET_MessageHeader *message)
1302 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1303 struct SharedService *ss;
1305 struct GNUNET_HashCode hash;
1307 uint16_t service_name_size;
1309 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1310 msg_size = ntohs (message->size);
1311 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1318 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1319 service_name = (char *) &msg[1];
1320 if ('\0' != service_name[service_name_size - 1])
1323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1326 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1327 service_name, ntohl (msg->num_peers));
1328 if (ntohl (msg->host_id) != master_context->host_id)
1330 route_message (ntohl (msg->host_id), message);
1331 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1335 ss = GNUNET_malloc (sizeof (struct SharedService));
1336 ss->name = strdup (service_name);
1337 ss->num_shared = ntohl (msg->num_peers);
1338 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1339 if (GNUNET_SYSERR ==
1340 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1341 &ss_exists_iterator, ss))
1343 LOG (GNUNET_ERROR_TYPE_WARNING,
1344 "Service %s already configured as a shared service. "
1345 "Ignoring service sharing request \n", ss->name);
1346 GNUNET_free (ss->name);
1350 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1356 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1359 * @param client identification of the client
1360 * @param message the actual message
1363 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1364 const struct GNUNET_MessageHeader *message)
1366 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1367 struct GNUNET_CONFIGURATION_Handle *cfg;
1368 struct LCFContextQueue *lcfq;
1369 struct Route *route;
1370 struct Route *new_route;
1374 uint32_t delegated_host_id;
1375 uint32_t slave_host_id;
1378 if (NULL == master_context)
1381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1384 msize = ntohs (message->size);
1385 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1391 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1392 delegated_host_id = ntohl (msg->delegated_host_id);
1393 if (delegated_host_id == master_context->host_id)
1396 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1400 if ((delegated_host_id >= host_list_size) ||
1401 (NULL == host_list[delegated_host_id]))
1403 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1407 slave_host_id = ntohl (msg->slave_host_id);
1408 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1410 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1414 if (slave_host_id == delegated_host_id)
1416 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1421 if (slave_host_id == master_context->host_id) /* Link from us */
1423 struct Slave *slave;
1424 struct LinkControllersContext *lcc;
1426 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1427 config_size = ntohs (msg->config_size);
1428 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1430 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1435 config = GNUNET_malloc (config_size);
1436 dest_size = (uLongf) config_size;
1438 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1441 GNUNET_break (0); /* Compression error */
1442 GNUNET_free (config);
1443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1446 if (config_size != dest_size)
1448 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1449 GNUNET_free (config);
1450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1453 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1455 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1457 GNUNET_break (0); /* Configuration parsing error */
1458 GNUNET_free (config);
1459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1462 GNUNET_free (config);
1463 if ((delegated_host_id < slave_list_size) &&
1464 (NULL != slave_list[delegated_host_id]))
1466 GNUNET_break (0); /* Configuration parsing error */
1467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1470 slave = GNUNET_malloc (sizeof (struct Slave));
1471 slave->host_id = delegated_host_id;
1472 slave_list_add (slave);
1473 if (1 != msg->is_subordinate)
1476 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1477 master_context->event_mask,
1478 &slave_event_callback, slave);
1479 GNUNET_CONFIGURATION_destroy (cfg);
1480 if (NULL != slave->controller)
1481 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1483 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1484 "Could not connect to delegated controller");
1485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1488 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1489 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1490 GNUNET_SERVER_client_keep (client);
1491 lcc->client = client;
1493 slave->controller_proc =
1494 GNUNET_TESTBED_controller_start (master_context->master_ip,
1495 host_list[slave->host_id], cfg,
1496 &slave_status_callback, lcc);
1497 GNUNET_CONFIGURATION_destroy (cfg);
1498 new_route = GNUNET_malloc (sizeof (struct Route));
1499 new_route->dest = delegated_host_id;
1500 new_route->thru = master_context->host_id;
1501 route_list_add (new_route);
1502 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1506 /* Route the request */
1507 if (slave_host_id >= route_list_size)
1509 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1513 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1514 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1515 lcfq->lcf->delegated_host_id = delegated_host_id;
1516 lcfq->lcf->slave_host_id = slave_host_id;
1517 route = find_dest_route (slave_host_id);
1518 GNUNET_assert (NULL != route); /* because we add routes carefully */
1519 GNUNET_assert (route->dest < slave_list_size);
1520 GNUNET_assert (NULL != slave_list[route->dest]);
1521 lcfq->lcf->state = INIT;
1522 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1523 lcfq->lcf->gateway = slave_list[route->dest];
1524 lcfq->lcf->msg = GNUNET_malloc (msize);
1525 (void) memcpy (lcfq->lcf->msg, msg, msize);
1526 GNUNET_SERVER_client_keep (client);
1527 lcfq->lcf->client = client;
1528 if (NULL == lcfq_head)
1530 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1531 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1532 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1535 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1536 /* FIXME: Adding a new route should happen after the controllers are linked
1538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1539 new_route = GNUNET_malloc (sizeof (struct Route));
1540 new_route->dest = delegated_host_id;
1541 new_route->thru = route->dest;
1542 route_list_add (new_route);
1547 * The task to be executed if the forwarded peer create operation has been
1550 * @param cls the FowardedOperationContext
1551 * @param tc the TaskContext from the scheduler
1554 peer_create_forward_timeout (void *cls,
1555 const struct GNUNET_SCHEDULER_TaskContext *tc)
1557 struct ForwardedOperationContext *fo_ctxt = cls;
1559 /* send error msg to client */
1560 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1561 GNUNET_SERVER_client_drop (fo_ctxt->client);
1562 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1563 GNUNET_free (fo_ctxt);
1568 * Callback to be called when forwarded peer create operation is
1569 * successfull. We have to relay the reply msg back to the client
1571 * @param cls ForwardedOperationContext
1572 * @param msg the peer create success message
1575 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1577 struct ForwardedOperationContext *fo_ctxt = cls;
1578 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1579 struct GNUNET_MessageHeader *dup_msg;
1583 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1584 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1587 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1588 peer = GNUNET_malloc (sizeof (struct Peer));
1589 peer->is_remote = GNUNET_YES;
1590 peer->id = ntohl (success_msg->peer_id);
1591 GNUNET_assert (NULL != fo_ctxt->cls);
1592 peer->details.remote.controller = fo_ctxt->cls;
1593 peer_list_add (peer);
1595 msize = ntohs (msg->size);
1596 dup_msg = GNUNET_malloc (msize);
1597 (void) memcpy (dup_msg, msg, msize);
1598 queue_message (fo_ctxt->client, dup_msg);
1599 GNUNET_SERVER_client_drop (fo_ctxt->client);
1600 GNUNET_free (fo_ctxt);
1606 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1609 * @param client identification of the client
1610 * @param message the actual message
1613 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1614 const struct GNUNET_MessageHeader *message)
1616 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1617 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1618 struct GNUNET_CONFIGURATION_Handle *cfg;
1619 struct ForwardedOperationContext *fo_ctxt;
1620 struct Route *route;
1625 uint32_t config_size;
1631 msize = ntohs (message->size);
1632 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1634 GNUNET_break (0); /* We need configuration */
1635 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1638 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1639 host_id = ntohl (msg->host_id);
1640 peer_id = ntohl (msg->peer_id);
1641 if (UINT32_MAX == peer_id)
1643 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1644 "Cannot create peer with given ID");
1645 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1648 if (host_id == master_context->host_id)
1652 /* We are responsible for this peer */
1653 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1654 config_size = ntohl (msg->config_size);
1655 config = GNUNET_malloc (config_size);
1656 dest_size = config_size;
1659 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1660 (const Bytef *) &msg[1], (uLong) msize)))
1662 GNUNET_break (0); /* uncompression error */
1663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1666 if (config_size != dest_size)
1668 GNUNET_break (0); /* Uncompressed config size mismatch */
1669 GNUNET_free (config);
1670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1673 cfg = GNUNET_CONFIGURATION_create ();
1675 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1677 GNUNET_break (0); /* Configuration parsing error */
1678 GNUNET_free (config);
1679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1682 GNUNET_free (config);
1683 peer = GNUNET_malloc (sizeof (struct Peer));
1684 peer->is_remote = GNUNET_NO;
1685 peer->details.local.cfg = cfg;
1687 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1688 peer->details.local.peer =
1689 GNUNET_TESTING_peer_configure (master_context->system,
1690 peer->details.local.cfg, peer->id,
1691 NULL /* Peer id */ ,
1693 if (NULL == peer->details.local.peer)
1695 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1702 peer->details.local.is_running = GNUNET_NO;
1703 peer_list_add (peer);
1705 GNUNET_malloc (sizeof
1706 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1707 reply->header.size =
1708 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1709 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1710 reply->peer_id = msg->peer_id;
1711 reply->operation_id = msg->operation_id;
1712 queue_message (client, &reply->header);
1713 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1717 /* Forward peer create request */
1718 route = find_dest_route (host_id);
1722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1725 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1726 GNUNET_SERVER_client_keep (client);
1727 fo_ctxt->client = client;
1728 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1729 fo_ctxt->cls = slave_list[route->dest]->controller;
1731 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1732 [route->dest]->controller,
1733 fo_ctxt->operation_id,
1735 peer_create_success_cb, fo_ctxt);
1736 fo_ctxt->timeout_task =
1737 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1745 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1748 * @param client identification of the client
1749 * @param message the actual message
1752 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1753 const struct GNUNET_MessageHeader *message)
1755 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1756 struct ForwardedOperationContext *fopc;
1760 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1761 peer_id = ntohl (msg->peer_id);
1762 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1763 peer_id, GNUNET_ntohll (msg->operation_id));
1764 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1766 LOG (GNUNET_ERROR_TYPE_ERROR,
1767 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1768 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1769 "Peer doesn't exist");
1770 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1773 peer = peer_list[peer_id];
1774 if (GNUNET_YES == peer->is_remote)
1776 /* Forward the destory message to sub controller */
1777 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1778 GNUNET_SERVER_client_keep (client);
1779 fopc->client = client;
1780 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1782 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1783 fopc->operation_id, &msg->header,
1784 &forwarded_operation_reply_relay,
1786 fopc->timeout_task =
1787 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1789 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1792 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1793 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1794 peer_list_remove (peer);
1796 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1797 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1802 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1805 * @param client identification of the client
1806 * @param message the actual message
1809 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1810 const struct GNUNET_MessageHeader *message)
1812 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1813 struct GNUNET_TESTBED_PeerEventMessage *reply;
1814 struct ForwardedOperationContext *fopc;
1818 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1819 peer_id = ntohl (msg->peer_id);
1820 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1823 LOG (GNUNET_ERROR_TYPE_ERROR,
1824 "Asked to start a non existent peer with id: %u\n", peer_id);
1825 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1828 peer = peer_list[peer_id];
1829 if (GNUNET_YES == peer->is_remote)
1831 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1832 GNUNET_SERVER_client_keep (client);
1833 fopc->client = client;
1834 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1836 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1837 fopc->operation_id, &msg->header,
1838 &forwarded_operation_reply_relay,
1840 fopc->timeout_task =
1841 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1843 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1846 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1848 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1850 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1853 peer->details.local.is_running = GNUNET_YES;
1854 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1855 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1856 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1857 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1858 reply->host_id = htonl (master_context->host_id);
1859 reply->peer_id = msg->peer_id;
1860 reply->operation_id = msg->operation_id;
1861 queue_message (client, &reply->header);
1862 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1867 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1870 * @param client identification of the client
1871 * @param message the actual message
1874 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1875 const struct GNUNET_MessageHeader *message)
1877 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1878 struct GNUNET_TESTBED_PeerEventMessage *reply;
1879 struct ForwardedOperationContext *fopc;
1883 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1884 peer_id = ntohl (msg->peer_id);
1885 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1887 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1892 peer = peer_list[peer_id];
1893 if (GNUNET_YES == peer->is_remote)
1895 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1896 GNUNET_SERVER_client_keep (client);
1897 fopc->client = client;
1898 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1900 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1901 fopc->operation_id, &msg->header,
1902 &forwarded_operation_reply_relay,
1904 fopc->timeout_task =
1905 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1907 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1910 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1912 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1913 "Peer not running");
1914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1917 peer->details.local.is_running = GNUNET_NO;
1918 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1919 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1920 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1921 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1922 reply->host_id = htonl (master_context->host_id);
1923 reply->peer_id = msg->peer_id;
1924 reply->operation_id = msg->operation_id;
1925 queue_message (client, &reply->header);
1926 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1931 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1934 * @param client identification of the client
1935 * @param message the actual message
1938 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1939 const struct GNUNET_MessageHeader *message)
1941 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1942 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1951 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1952 peer_id = ntohl (msg->peer_id);
1953 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1955 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1957 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1960 peer = peer_list[peer_id];
1961 if (GNUNET_YES == peer->is_remote)
1963 /* FIXME: forward to sub controller */
1965 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1969 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
1971 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1972 GNUNET_free (config);
1975 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1976 reply = GNUNET_realloc (xconfig, msize);
1977 (void) memmove (&reply[1], reply, xc_size);
1978 reply->header.size = htons (msize);
1979 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1980 reply->peer_id = msg->peer_id;
1981 reply->operation_id = msg->operation_id;
1982 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
1983 &reply->peer_identity);
1984 reply->config_size = htons ((uint16_t) c_size);
1985 queue_message (client, &reply->header);
1986 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1991 * Task for cleaing up overlay connect context structure
1993 * @param cls the overlay connect context
1994 * @param tc the task context
1997 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1999 struct OverlayConnectContext *occ = cls;
2001 LOG_DEBUG ("Cleaning up occ\n");
2002 GNUNET_free_non_null (occ->emsg);
2003 GNUNET_free_non_null (occ->hello);
2004 if (NULL != occ->opc)
2005 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2006 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2007 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2008 if (NULL != occ->ch)
2009 GNUNET_CORE_disconnect (occ->ch);
2010 if (NULL != occ->ghh)
2011 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2012 if (NULL != occ->p1th)
2013 GNUNET_TRANSPORT_disconnect (occ->p1th);
2014 if (NULL != occ->p2th)
2015 GNUNET_TRANSPORT_disconnect (occ->p2th);
2021 * Task which will be run when overlay connect request has been timed out
2023 * @param cls the OverlayConnectContext
2024 * @param tc the TaskContext
2027 timeout_overlay_connect (void *cls,
2028 const struct GNUNET_SCHEDULER_TaskContext *tc)
2030 struct OverlayConnectContext *occ = cls;
2032 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2033 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2034 GNUNET_SERVER_client_drop (occ->client);
2035 occ_cleanup (occ, tc);
2041 * Function called to notify transport users that another
2042 * peer connected to us.
2044 * @param cls closure
2045 * @param new_peer the peer that connected
2046 * @param ats performance data
2047 * @param ats_count number of entries in ats (excluding 0-termination)
2050 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2051 const struct GNUNET_ATS_Information *ats,
2052 unsigned int ats_count)
2054 struct OverlayConnectContext *occ = cls;
2055 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2057 char *other_peer_str;
2059 LOG_DEBUG ("Overlay connect notify\n");
2061 memcmp (new_peer, &occ->peer_identity,
2062 sizeof (struct GNUNET_PeerIdentity)))
2064 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2065 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2067 memcmp (new_peer, &occ->other_peer_identity,
2068 sizeof (struct GNUNET_PeerIdentity)))
2070 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2071 new_peer_str, other_peer_str);
2072 GNUNET_free (new_peer_str);
2073 GNUNET_free (other_peer_str);
2076 GNUNET_free (new_peer_str);
2077 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2078 GNUNET_i2s (&occ->peer_identity));
2079 GNUNET_free (other_peer_str);
2080 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2082 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2083 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2085 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2086 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2087 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2088 GNUNET_free_non_null (occ->emsg);
2090 if (NULL != occ->p2th)
2091 GNUNET_TRANSPORT_disconnect (occ->p2th);
2093 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2094 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2096 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2097 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2098 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2099 msg->peer1 = htonl (occ->peer->id);
2100 msg->peer2 = htonl (occ->other_peer->id);
2101 msg->operation_id = GNUNET_htonll (occ->op_id);
2102 queue_message (occ->client, &msg->header);
2103 GNUNET_SERVER_client_drop (occ->client);
2104 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2109 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2112 * @param cls the OverlayConnectContext
2113 * @param tc the TaskContext from scheduler
2116 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2118 struct OverlayConnectContext *occ = cls;
2119 char *other_peer_str;
2121 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2122 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2124 GNUNET_assert (NULL != occ->hello);
2125 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2126 if (GNUNET_YES == occ->other_peer->is_remote)
2128 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2130 uint16_t hello_size;
2132 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2133 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2134 hello_size = ntohs (occ->hello->size);
2135 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2136 msg = GNUNET_malloc (msize);
2137 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2138 msg->header.size = htons (msize);
2139 msg->peer = htonl (occ->other_peer->id);
2140 msg->operation_id = GNUNET_htonll (occ->op_id);
2141 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2142 sizeof (struct GNUNET_PeerIdentity));
2143 memcpy (msg->hello, occ->hello, hello_size);
2144 GNUNET_TESTBED_queue_message_ (occ->other_peer->details.remote.controller,
2149 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2150 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2151 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2152 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2153 occ->send_hello_task =
2154 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2157 GNUNET_free (other_peer_str);
2161 * Test for checking whether HELLO message is empty
2163 * @param cls empty flag to set
2164 * @param address the HELLO
2165 * @param expiration expiration of the HELLO
2169 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2170 struct GNUNET_TIME_Absolute expiration)
2180 * Function called whenever there is an update to the HELLO of peers in the
2181 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2182 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2184 * @param cls closure
2185 * @param hello our updated HELLO
2188 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2190 struct OverlayConnectContext *occ = cls;
2194 msize = ntohs (hello->size);
2196 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2197 hello, GNUNET_NO, &test_address,
2199 if (GNUNET_YES == empty)
2201 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2204 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2205 occ->hello = GNUNET_malloc (msize);
2206 memcpy (occ->hello, hello, msize);
2207 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2209 GNUNET_TRANSPORT_disconnect (occ->p1th);
2211 GNUNET_free_non_null (occ->emsg);
2212 if (GNUNET_NO == occ->other_peer->is_remote)
2215 GNUNET_TRANSPORT_connect (occ->other_peer->details.local.cfg,
2216 &occ->other_peer_identity, NULL, NULL, NULL,
2218 if (NULL == occ->p2th)
2220 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2221 GNUNET_i2s (&occ->other_peer_identity));
2222 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2223 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2227 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2228 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2233 * Function called after GNUNET_CORE_connect has succeeded (or failed
2234 * for good). Note that the private key of the peer is intentionally
2235 * not exposed here; if you need it, your process should try to read
2236 * the private key file directly (which should work if you are
2239 * @param cls closure
2240 * @param server handle to the server, NULL if we failed
2241 * @param my_identity ID of this peer, NULL if we failed
2244 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2245 const struct GNUNET_PeerIdentity *my_identity)
2247 struct OverlayConnectContext *occ = cls;
2249 GNUNET_free_non_null (occ->emsg);
2250 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2251 if ((NULL == server) || (NULL == my_identity))
2253 GNUNET_free (occ->emsg);
2256 memcpy (&occ->peer_identity, my_identity,
2257 sizeof (struct GNUNET_PeerIdentity));
2259 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2260 &occ->peer_identity, NULL, NULL, NULL, NULL);
2261 if (NULL == occ->p1th)
2263 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2264 GNUNET_i2s (&occ->peer_identity));
2267 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2268 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2269 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2273 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2274 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2280 * Callback to be called when forwarded get peer config operation as part of
2281 * overlay connect is successfull. Connection to Peer 1's core is made and is
2282 * checked for new connection from peer 2
2284 * @param cls ForwardedOperationContext
2285 * @param msg the peer create success message
2288 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2290 struct OverlayConnectContext *occ = cls;
2291 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2292 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2297 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2299 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2301 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2302 sizeof (struct GNUNET_PeerIdentity));
2303 GNUNET_free_non_null (occ->emsg);
2304 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2306 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2307 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2308 GNUNET_NO, no_handlers);
2309 if (NULL == occ->ch)
2314 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2316 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2321 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2324 * @param client identification of the client
2325 * @param message the actual message
2328 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2329 const struct GNUNET_MessageHeader *message)
2331 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2332 struct OverlayConnectContext *occ;
2333 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2339 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2340 p1 = ntohl (msg->peer1);
2341 p2 = ntohl (msg->peer2);
2342 GNUNET_assert (p1 < peer_list_size);
2343 GNUNET_assert (NULL != peer_list[p1]);
2344 GNUNET_assert (p2 < peer_list_size);
2345 GNUNET_assert (NULL != peer_list[p2]);
2346 /* FIXME: Add cases where we have to forward overlay connect message to sub
2348 GNUNET_assert (GNUNET_NO == peer_list[p1]->is_remote);
2349 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2350 GNUNET_SERVER_client_keep (client);
2351 occ->client = client;
2352 occ->peer = peer_list[p1];
2353 occ->other_peer = peer_list[p2];
2354 occ->op_id = GNUNET_ntohll (msg->operation_id);
2355 /* Get the identity of the second peer */
2356 if (GNUNET_YES == occ->other_peer->is_remote)
2358 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2361 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2362 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2363 cmsg.peer_id = msg->peer2;
2364 cmsg.operation_id = msg->operation_id;
2366 GNUNET_TESTBED_forward_operation_msg_ (occ->other_peer->details.remote.controller,
2367 occ->op_id, &cmsg.header,
2368 &overlay_connect_get_config,
2371 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2373 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2374 (GNUNET_TIME_UNIT_SECONDS, 30),
2375 &timeout_overlay_connect, occ);
2376 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2379 GNUNET_TESTING_peer_get_identity (occ->other_peer->details.local.peer,
2380 &occ->other_peer_identity);
2381 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2382 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2384 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2385 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2386 GNUNET_NO, no_handlers);
2387 if (NULL == occ->ch)
2389 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2392 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2393 (GNUNET_TIME_UNIT_SECONDS, 30),
2394 &timeout_overlay_connect, occ);
2395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2400 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2407 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2409 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2410 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2411 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2412 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2413 GNUNET_TRANSPORT_disconnect (rocc->th);
2414 GNUNET_free_non_null (rocc->hello);
2420 * Task to timeout rocc and cleanit up
2422 * @param cls the RequestOverlayConnectContext
2423 * @param tc the TaskContext from scheduler
2426 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2428 struct RequestOverlayConnectContext *rocc = cls;
2430 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2431 cleanup_rocc (rocc);
2436 * Function called to notify transport users that another
2437 * peer connected to us.
2439 * @param cls closure
2440 * @param peer the peer that connected
2441 * @param ats performance data
2442 * @param ats_count number of entries in ats (excluding 0-termination)
2445 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2446 const struct GNUNET_ATS_Information * ats,
2449 struct RequestOverlayConnectContext *rocc = cls;
2451 LOG_DEBUG ("Request Overlay connect notify\n");
2452 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2456 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
2457 cleanup_rocc (rocc);
2462 * Task to offer the HELLO message to the peer and ask it to connect to the peer
2463 * whose identity is in RequestOverlayConnectContext
2465 * @param cls the RequestOverlayConnectContext
2466 * @param tc the TaskContext from scheduler
2469 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2471 struct RequestOverlayConnectContext *rocc = cls;
2473 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
2474 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
2475 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
2476 rocc->attempt_connect_task_id =
2477 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2478 &attempt_connect_task, rocc);
2483 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2486 * @param client identification of the client
2487 * @param message the actual message
2490 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2491 const struct GNUNET_MessageHeader *message)
2493 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2494 struct RequestOverlayConnectContext *rocc;
2500 msize = ntohs (message->size);
2501 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
2504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2507 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2508 if ((NULL == msg->hello) ||
2509 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2515 hsize = ntohs (msg->hello->size);
2516 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
2519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2522 peer_id = ntohl (msg->peer);
2523 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2525 GNUNET_break_op (0);
2526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2529 if (GNUNET_NO != peer->is_remote)
2532 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2535 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
2536 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
2537 NULL, &transport_connect_notify, NULL);
2538 if (NULL == rocc->th)
2542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2545 memcpy (&rocc->a_id, &msg->peer_identity,
2546 sizeof (struct GNUNET_PeerIdentity));
2547 rocc->hello = GNUNET_malloc (hsize);
2548 memcpy (rocc->hello, msg->hello, hsize);
2549 /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
2550 /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
2551 rocc->attempt_connect_task_id =
2552 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
2553 rocc->timeout_rocc_task_id =
2554 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
2555 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2560 * Iterator over hash map entries.
2562 * @param cls closure
2563 * @param key current key code
2564 * @param value value in the hash map
2565 * @return GNUNET_YES if we should continue to
2570 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2572 struct SharedService *ss = value;
2574 GNUNET_assert (GNUNET_YES ==
2575 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2576 GNUNET_free (ss->name);
2583 * Task to clean up and shutdown nicely
2586 * @param tc the TaskContext from scheduler
2589 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2591 struct LCFContextQueue *lcfq;
2594 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2595 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2596 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2598 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2599 if (NULL != lcfq_head)
2601 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2603 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2604 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2606 if (NULL != lcfq_head->lcf->rhandle)
2607 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2609 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2610 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2612 GNUNET_free (lcfq->lcf->msg);
2613 GNUNET_free (lcfq->lcf);
2614 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2617 /* Clear peer list */
2618 for (id = 0; id < peer_list_size; id++)
2619 if (NULL != peer_list[id])
2621 if (GNUNET_NO == peer_list[id]->is_remote)
2623 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2624 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2625 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2626 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2628 GNUNET_free (peer_list[id]);
2630 GNUNET_free_non_null (peer_list);
2631 /* Clear host list */
2632 for (id = 0; id < host_list_size; id++)
2633 if (NULL != host_list[id])
2634 GNUNET_TESTBED_host_destroy (host_list[id]);
2635 GNUNET_free_non_null (host_list);
2636 /* Clear route list */
2637 for (id = 0; id < route_list_size; id++)
2638 if (NULL != route_list[id])
2639 GNUNET_free (route_list[id]);
2640 GNUNET_free_non_null (route_list);
2641 /* Clear slave_list */
2642 for (id = 0; id < slave_list_size; id++)
2643 if (NULL != slave_list[id])
2645 if (NULL != slave_list[id]->controller)
2646 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2647 if (NULL != slave_list[id]->controller_proc)
2648 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2650 if (NULL != master_context)
2652 GNUNET_free_non_null (master_context->master_ip);
2653 if (NULL != master_context->system)
2654 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2655 GNUNET_free (master_context);
2656 master_context = NULL;
2662 * Callback for client disconnect
2665 * @param client the client which has disconnected
2668 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2670 if (NULL == master_context)
2672 if (client == master_context->client)
2674 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2675 GNUNET_SERVER_client_drop (client);
2676 /* should not be needed as we're terminated by failure to read
2677 * from stdin, but if stdin fails for some reason, this shouldn't
2678 * hurt for now --- might need to revise this later if we ever
2679 * decide that master connections might be temporarily down
2680 * for some reason */
2681 //GNUNET_SCHEDULER_shutdown ();
2689 * @param cls closure
2690 * @param server the initialized server
2691 * @param cfg configuration to use
2694 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2695 const struct GNUNET_CONFIGURATION_Handle *cfg)
2697 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2698 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2699 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2700 {&handle_configure_shared_service, NULL,
2701 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2702 {&handle_link_controllers, NULL,
2703 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2704 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2705 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2706 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2707 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2708 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2709 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2710 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2711 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2712 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2713 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2714 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2715 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2720 GNUNET_SERVER_add_handlers (server, message_handlers);
2721 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2722 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2724 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2725 &shutdown_task, NULL);
2726 LOG_DEBUG ("Testbed startup complete\n");
2731 * The starting point of execution
2734 main (int argc, char *const *argv)
2736 //sleep (15); /* Debugging */
2737 return (GNUNET_OK ==
2738 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2739 &testbed_run, NULL)) ? 0 : 1;
2742 /* end of gnunet-service-testbed.c */