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,
1404 "Delegated host %u not registered with us\n", delegated_host_id);
1405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1408 slave_host_id = ntohl (msg->slave_host_id);
1409 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1411 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1415 if (slave_host_id == delegated_host_id)
1417 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1422 if (slave_host_id == master_context->host_id) /* Link from us */
1424 struct Slave *slave;
1425 struct LinkControllersContext *lcc;
1427 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1428 config_size = ntohs (msg->config_size);
1429 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1431 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1436 config = GNUNET_malloc (config_size);
1437 dest_size = (uLongf) config_size;
1439 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1442 GNUNET_break (0); /* Compression error */
1443 GNUNET_free (config);
1444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1447 if (config_size != dest_size)
1449 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1450 GNUNET_free (config);
1451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1454 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1456 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1458 GNUNET_break (0); /* Configuration parsing error */
1459 GNUNET_free (config);
1460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1463 GNUNET_free (config);
1464 if ((delegated_host_id < slave_list_size) &&
1465 (NULL != slave_list[delegated_host_id]))
1467 GNUNET_break (0); /* Configuration parsing error */
1468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1471 slave = GNUNET_malloc (sizeof (struct Slave));
1472 slave->host_id = delegated_host_id;
1473 slave_list_add (slave);
1474 if (1 != msg->is_subordinate)
1477 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1478 master_context->event_mask,
1479 &slave_event_callback, slave);
1480 GNUNET_CONFIGURATION_destroy (cfg);
1481 if (NULL != slave->controller)
1482 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1484 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1485 "Could not connect to delegated controller");
1486 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1489 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1490 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1491 GNUNET_SERVER_client_keep (client);
1492 lcc->client = client;
1494 slave->controller_proc =
1495 GNUNET_TESTBED_controller_start (master_context->master_ip,
1496 host_list[slave->host_id], cfg,
1497 &slave_status_callback, lcc);
1498 GNUNET_CONFIGURATION_destroy (cfg);
1499 new_route = GNUNET_malloc (sizeof (struct Route));
1500 new_route->dest = delegated_host_id;
1501 new_route->thru = master_context->host_id;
1502 route_list_add (new_route);
1503 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1507 /* Route the request */
1508 if (slave_host_id >= route_list_size)
1510 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1514 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1515 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1516 lcfq->lcf->delegated_host_id = delegated_host_id;
1517 lcfq->lcf->slave_host_id = slave_host_id;
1518 route = find_dest_route (slave_host_id);
1519 GNUNET_assert (NULL != route); /* because we add routes carefully */
1520 GNUNET_assert (route->dest < slave_list_size);
1521 GNUNET_assert (NULL != slave_list[route->dest]);
1522 lcfq->lcf->state = INIT;
1523 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1524 lcfq->lcf->gateway = slave_list[route->dest];
1525 lcfq->lcf->msg = GNUNET_malloc (msize);
1526 (void) memcpy (lcfq->lcf->msg, msg, msize);
1527 GNUNET_SERVER_client_keep (client);
1528 lcfq->lcf->client = client;
1529 if (NULL == lcfq_head)
1531 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1532 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1533 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1536 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1537 /* FIXME: Adding a new route should happen after the controllers are linked
1539 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1540 new_route = GNUNET_malloc (sizeof (struct Route));
1541 new_route->dest = delegated_host_id;
1542 new_route->thru = route->dest;
1543 route_list_add (new_route);
1548 * The task to be executed if the forwarded peer create operation has been
1551 * @param cls the FowardedOperationContext
1552 * @param tc the TaskContext from the scheduler
1555 peer_create_forward_timeout (void *cls,
1556 const struct GNUNET_SCHEDULER_TaskContext *tc)
1558 struct ForwardedOperationContext *fo_ctxt = cls;
1560 /* send error msg to client */
1561 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1562 GNUNET_SERVER_client_drop (fo_ctxt->client);
1563 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1564 GNUNET_free (fo_ctxt);
1569 * Callback to be called when forwarded peer create operation is
1570 * successfull. We have to relay the reply msg back to the client
1572 * @param cls ForwardedOperationContext
1573 * @param msg the peer create success message
1576 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1578 struct ForwardedOperationContext *fo_ctxt = cls;
1579 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1580 struct GNUNET_MessageHeader *dup_msg;
1584 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1585 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1588 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1589 peer = GNUNET_malloc (sizeof (struct Peer));
1590 peer->is_remote = GNUNET_YES;
1591 peer->id = ntohl (success_msg->peer_id);
1592 GNUNET_assert (NULL != fo_ctxt->cls);
1593 peer->details.remote.controller = fo_ctxt->cls;
1594 peer_list_add (peer);
1596 msize = ntohs (msg->size);
1597 dup_msg = GNUNET_malloc (msize);
1598 (void) memcpy (dup_msg, msg, msize);
1599 queue_message (fo_ctxt->client, dup_msg);
1600 GNUNET_SERVER_client_drop (fo_ctxt->client);
1601 GNUNET_free (fo_ctxt);
1607 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1610 * @param client identification of the client
1611 * @param message the actual message
1614 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1615 const struct GNUNET_MessageHeader *message)
1617 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1618 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1619 struct GNUNET_CONFIGURATION_Handle *cfg;
1620 struct ForwardedOperationContext *fo_ctxt;
1621 struct Route *route;
1626 uint32_t config_size;
1632 msize = ntohs (message->size);
1633 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1635 GNUNET_break (0); /* We need configuration */
1636 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1639 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1640 host_id = ntohl (msg->host_id);
1641 peer_id = ntohl (msg->peer_id);
1642 if (UINT32_MAX == peer_id)
1644 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1645 "Cannot create peer with given ID");
1646 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1649 if (host_id == master_context->host_id)
1653 /* We are responsible for this peer */
1654 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1655 config_size = ntohl (msg->config_size);
1656 config = GNUNET_malloc (config_size);
1657 dest_size = config_size;
1660 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1661 (const Bytef *) &msg[1], (uLong) msize)))
1663 GNUNET_break (0); /* uncompression error */
1664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1667 if (config_size != dest_size)
1669 GNUNET_break (0); /* Uncompressed config size mismatch */
1670 GNUNET_free (config);
1671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1674 cfg = GNUNET_CONFIGURATION_create ();
1676 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1678 GNUNET_break (0); /* Configuration parsing error */
1679 GNUNET_free (config);
1680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1683 GNUNET_free (config);
1684 peer = GNUNET_malloc (sizeof (struct Peer));
1685 peer->is_remote = GNUNET_NO;
1686 peer->details.local.cfg = cfg;
1688 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1689 peer->details.local.peer =
1690 GNUNET_TESTING_peer_configure (master_context->system,
1691 peer->details.local.cfg, peer->id,
1692 NULL /* Peer id */ ,
1694 if (NULL == peer->details.local.peer)
1696 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1703 peer->details.local.is_running = GNUNET_NO;
1704 peer_list_add (peer);
1706 GNUNET_malloc (sizeof
1707 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1708 reply->header.size =
1709 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1710 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1711 reply->peer_id = msg->peer_id;
1712 reply->operation_id = msg->operation_id;
1713 queue_message (client, &reply->header);
1714 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1718 /* Forward peer create request */
1719 route = find_dest_route (host_id);
1723 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1726 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1727 GNUNET_SERVER_client_keep (client);
1728 fo_ctxt->client = client;
1729 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1730 fo_ctxt->cls = slave_list[route->dest]->controller;
1732 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1733 [route->dest]->controller,
1734 fo_ctxt->operation_id,
1736 peer_create_success_cb, fo_ctxt);
1737 fo_ctxt->timeout_task =
1738 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1741 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1746 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1749 * @param client identification of the client
1750 * @param message the actual message
1753 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1754 const struct GNUNET_MessageHeader *message)
1756 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1757 struct ForwardedOperationContext *fopc;
1761 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1762 peer_id = ntohl (msg->peer_id);
1763 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1764 peer_id, GNUNET_ntohll (msg->operation_id));
1765 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1767 LOG (GNUNET_ERROR_TYPE_ERROR,
1768 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1769 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1770 "Peer doesn't exist");
1771 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1774 peer = peer_list[peer_id];
1775 if (GNUNET_YES == peer->is_remote)
1777 /* Forward the destory message to sub controller */
1778 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1779 GNUNET_SERVER_client_keep (client);
1780 fopc->client = client;
1781 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1783 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1784 fopc->operation_id, &msg->header,
1785 &forwarded_operation_reply_relay,
1787 fopc->timeout_task =
1788 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1790 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1793 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1794 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1795 peer_list_remove (peer);
1797 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1798 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1803 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1806 * @param client identification of the client
1807 * @param message the actual message
1810 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1811 const struct GNUNET_MessageHeader *message)
1813 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1814 struct GNUNET_TESTBED_PeerEventMessage *reply;
1815 struct ForwardedOperationContext *fopc;
1819 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1820 peer_id = ntohl (msg->peer_id);
1821 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1824 LOG (GNUNET_ERROR_TYPE_ERROR,
1825 "Asked to start a non existent peer with id: %u\n", peer_id);
1826 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1829 peer = peer_list[peer_id];
1830 if (GNUNET_YES == peer->is_remote)
1832 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1833 GNUNET_SERVER_client_keep (client);
1834 fopc->client = client;
1835 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1837 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1838 fopc->operation_id, &msg->header,
1839 &forwarded_operation_reply_relay,
1841 fopc->timeout_task =
1842 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1844 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1847 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1849 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1851 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1854 peer->details.local.is_running = GNUNET_YES;
1855 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1856 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1857 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1858 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1859 reply->host_id = htonl (master_context->host_id);
1860 reply->peer_id = msg->peer_id;
1861 reply->operation_id = msg->operation_id;
1862 queue_message (client, &reply->header);
1863 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1868 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1871 * @param client identification of the client
1872 * @param message the actual message
1875 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1876 const struct GNUNET_MessageHeader *message)
1878 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1879 struct GNUNET_TESTBED_PeerEventMessage *reply;
1880 struct ForwardedOperationContext *fopc;
1884 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1885 peer_id = ntohl (msg->peer_id);
1886 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1888 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1890 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1893 peer = peer_list[peer_id];
1894 if (GNUNET_YES == peer->is_remote)
1896 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1897 GNUNET_SERVER_client_keep (client);
1898 fopc->client = client;
1899 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1901 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1902 fopc->operation_id, &msg->header,
1903 &forwarded_operation_reply_relay,
1905 fopc->timeout_task =
1906 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1908 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1911 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1913 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1914 "Peer not running");
1915 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1918 peer->details.local.is_running = GNUNET_NO;
1919 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1920 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1921 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1922 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1923 reply->host_id = htonl (master_context->host_id);
1924 reply->peer_id = msg->peer_id;
1925 reply->operation_id = msg->operation_id;
1926 queue_message (client, &reply->header);
1927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1932 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1935 * @param client identification of the client
1936 * @param message the actual message
1939 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1940 const struct GNUNET_MessageHeader *message)
1942 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1943 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1952 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1953 peer_id = ntohl (msg->peer_id);
1954 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1956 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1958 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1961 peer = peer_list[peer_id];
1962 if (GNUNET_YES == peer->is_remote)
1964 /* FIXME: forward to sub controller */
1966 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1970 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
1972 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1973 GNUNET_free (config);
1976 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1977 reply = GNUNET_realloc (xconfig, msize);
1978 (void) memmove (&reply[1], reply, xc_size);
1979 reply->header.size = htons (msize);
1980 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1981 reply->peer_id = msg->peer_id;
1982 reply->operation_id = msg->operation_id;
1983 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
1984 &reply->peer_identity);
1985 reply->config_size = htons ((uint16_t) c_size);
1986 queue_message (client, &reply->header);
1987 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1992 * Task for cleaing up overlay connect context structure
1994 * @param cls the overlay connect context
1995 * @param tc the task context
1998 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2000 struct OverlayConnectContext *occ = cls;
2002 LOG_DEBUG ("Cleaning up occ\n");
2003 GNUNET_free_non_null (occ->emsg);
2004 GNUNET_free_non_null (occ->hello);
2005 GNUNET_SERVER_client_drop (occ->client);
2006 if (NULL != occ->opc)
2007 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2008 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2009 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2010 if (NULL != occ->ch)
2011 GNUNET_CORE_disconnect (occ->ch);
2012 if (NULL != occ->ghh)
2013 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2014 if (NULL != occ->p1th)
2015 GNUNET_TRANSPORT_disconnect (occ->p1th);
2016 if (NULL != occ->p2th)
2017 GNUNET_TRANSPORT_disconnect (occ->p2th);
2023 * Task which will be run when overlay connect request has been timed out
2025 * @param cls the OverlayConnectContext
2026 * @param tc the TaskContext
2029 timeout_overlay_connect (void *cls,
2030 const struct GNUNET_SCHEDULER_TaskContext *tc)
2032 struct OverlayConnectContext *occ = cls;
2034 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2035 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2036 occ_cleanup (occ, tc);
2042 * Function called to notify transport users that another
2043 * peer connected to us.
2045 * @param cls closure
2046 * @param new_peer the peer that connected
2047 * @param ats performance data
2048 * @param ats_count number of entries in ats (excluding 0-termination)
2051 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2052 const struct GNUNET_ATS_Information *ats,
2053 unsigned int ats_count)
2055 struct OverlayConnectContext *occ = cls;
2056 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2058 char *other_peer_str;
2060 LOG_DEBUG ("Overlay connect notify\n");
2062 memcmp (new_peer, &occ->peer_identity,
2063 sizeof (struct GNUNET_PeerIdentity)))
2065 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2066 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2068 memcmp (new_peer, &occ->other_peer_identity,
2069 sizeof (struct GNUNET_PeerIdentity)))
2071 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2072 new_peer_str, other_peer_str);
2073 GNUNET_free (new_peer_str);
2074 GNUNET_free (other_peer_str);
2077 GNUNET_free (new_peer_str);
2078 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2079 GNUNET_i2s (&occ->peer_identity));
2080 GNUNET_free (other_peer_str);
2081 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2083 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2084 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2086 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2087 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2088 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2089 GNUNET_free_non_null (occ->emsg);
2091 if (NULL != occ->p2th)
2092 GNUNET_TRANSPORT_disconnect (occ->p2th);
2094 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2095 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2097 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2098 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2099 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2100 msg->peer1 = htonl (occ->peer->id);
2101 msg->peer2 = htonl (occ->other_peer->id);
2102 msg->operation_id = GNUNET_htonll (occ->op_id);
2103 queue_message (occ->client, &msg->header);
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[] = {
2337 uint64_t operation_id;
2341 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2342 p1 = ntohl (msg->peer1);
2343 p2 = ntohl (msg->peer2);
2344 GNUNET_assert (p1 < peer_list_size);
2345 GNUNET_assert (NULL != peer_list[p1]);
2346 peer = peer_list[p1];
2347 operation_id = GNUNET_ntohll (msg->operation_id);
2348 if (GNUNET_YES == peer->is_remote)
2350 struct ForwardedOperationContext *fopc;
2352 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2353 GNUNET_SERVER_client_keep (client);
2354 fopc->client = client;
2355 fopc->operation_id = operation_id;
2357 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
2358 operation_id, message,
2359 &forwarded_operation_reply_relay,
2361 fopc->timeout_task =
2362 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2364 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2367 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2368 GNUNET_SERVER_client_keep (client);
2369 occ->client = client;
2370 occ->peer = peer_list[p1];
2371 occ->other_peer = peer_list[p2];
2372 occ->op_id = GNUNET_ntohll (msg->operation_id);
2373 /* Get the identity of the second peer */
2374 if (GNUNET_YES == occ->other_peer->is_remote)
2376 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2379 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2380 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2381 cmsg.peer_id = msg->peer2;
2382 cmsg.operation_id = msg->operation_id;
2384 GNUNET_TESTBED_forward_operation_msg_ (occ->other_peer->details.remote.controller,
2385 occ->op_id, &cmsg.header,
2386 &overlay_connect_get_config,
2389 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2391 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2392 (GNUNET_TIME_UNIT_SECONDS, 30),
2393 &timeout_overlay_connect, occ);
2394 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2397 GNUNET_TESTING_peer_get_identity (occ->other_peer->details.local.peer,
2398 &occ->other_peer_identity);
2399 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2400 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2402 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2403 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2404 GNUNET_NO, no_handlers);
2405 if (NULL == occ->ch)
2407 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2410 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2411 (GNUNET_TIME_UNIT_SECONDS, 30),
2412 &timeout_overlay_connect, occ);
2413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2418 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2421 * @param rocc the RequestOverlayConnectContext
2424 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2426 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2427 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2428 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2429 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2430 GNUNET_TRANSPORT_disconnect (rocc->th);
2431 GNUNET_free_non_null (rocc->hello);
2437 * Task to timeout rocc and cleanit up
2439 * @param cls the RequestOverlayConnectContext
2440 * @param tc the TaskContext from scheduler
2443 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2445 struct RequestOverlayConnectContext *rocc = cls;
2447 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2448 cleanup_rocc (rocc);
2453 * Function called to notify transport users that another
2454 * peer connected to us.
2456 * @param cls closure
2457 * @param new_peer the peer that connected
2458 * @param ats performance data
2459 * @param ats_count number of entries in ats (excluding 0-termination)
2462 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2463 const struct GNUNET_ATS_Information * ats,
2466 struct RequestOverlayConnectContext *rocc = cls;
2468 LOG_DEBUG ("Request Overlay connect notify\n");
2469 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2473 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
2474 cleanup_rocc (rocc);
2479 * Task to offer the HELLO message to the peer and ask it to connect to the peer
2480 * whose identity is in RequestOverlayConnectContext
2482 * @param cls the RequestOverlayConnectContext
2483 * @param tc the TaskContext from scheduler
2486 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2488 struct RequestOverlayConnectContext *rocc = cls;
2490 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
2491 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
2492 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
2493 rocc->attempt_connect_task_id =
2494 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2495 &attempt_connect_task, rocc);
2500 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2503 * @param client identification of the client
2504 * @param message the actual message
2507 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2508 const struct GNUNET_MessageHeader *message)
2510 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2511 struct RequestOverlayConnectContext *rocc;
2517 msize = ntohs (message->size);
2518 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
2521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2524 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2525 if ((NULL == msg->hello) ||
2526 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2532 hsize = ntohs (msg->hello->size);
2533 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
2536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2539 peer_id = ntohl (msg->peer);
2540 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2542 GNUNET_break_op (0);
2543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2546 if (GNUNET_NO != peer->is_remote)
2549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2552 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
2553 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
2554 NULL, &transport_connect_notify, NULL);
2555 if (NULL == rocc->th)
2559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2562 memcpy (&rocc->a_id, &msg->peer_identity,
2563 sizeof (struct GNUNET_PeerIdentity));
2564 rocc->hello = GNUNET_malloc (hsize);
2565 memcpy (rocc->hello, msg->hello, hsize);
2566 /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
2567 /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
2568 rocc->attempt_connect_task_id =
2569 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
2570 rocc->timeout_rocc_task_id =
2571 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
2572 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2577 * Iterator over hash map entries.
2579 * @param cls closure
2580 * @param key current key code
2581 * @param value value in the hash map
2582 * @return GNUNET_YES if we should continue to
2587 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2589 struct SharedService *ss = value;
2591 GNUNET_assert (GNUNET_YES ==
2592 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2593 GNUNET_free (ss->name);
2600 * Task to clean up and shutdown nicely
2603 * @param tc the TaskContext from scheduler
2606 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2608 struct LCFContextQueue *lcfq;
2611 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2612 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2613 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2615 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2616 if (NULL != lcfq_head)
2618 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2620 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2621 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2623 if (NULL != lcfq_head->lcf->rhandle)
2624 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2626 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2627 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2629 GNUNET_free (lcfq->lcf->msg);
2630 GNUNET_free (lcfq->lcf);
2631 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2634 /* Clear peer list */
2635 for (id = 0; id < peer_list_size; id++)
2636 if (NULL != peer_list[id])
2638 if (GNUNET_NO == peer_list[id]->is_remote)
2640 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2641 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2642 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2643 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2645 GNUNET_free (peer_list[id]);
2647 GNUNET_free_non_null (peer_list);
2648 /* Clear host list */
2649 for (id = 0; id < host_list_size; id++)
2650 if (NULL != host_list[id])
2651 GNUNET_TESTBED_host_destroy (host_list[id]);
2652 GNUNET_free_non_null (host_list);
2653 /* Clear route list */
2654 for (id = 0; id < route_list_size; id++)
2655 if (NULL != route_list[id])
2656 GNUNET_free (route_list[id]);
2657 GNUNET_free_non_null (route_list);
2658 /* Clear slave_list */
2659 for (id = 0; id < slave_list_size; id++)
2660 if (NULL != slave_list[id])
2662 if (NULL != slave_list[id]->controller)
2663 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2664 if (NULL != slave_list[id]->controller_proc)
2665 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2667 if (NULL != master_context)
2669 GNUNET_free_non_null (master_context->master_ip);
2670 if (NULL != master_context->system)
2671 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2672 GNUNET_free (master_context);
2673 master_context = NULL;
2679 * Callback for client disconnect
2682 * @param client the client which has disconnected
2685 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2687 if (NULL == master_context)
2689 if (client == master_context->client)
2691 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2692 GNUNET_SERVER_client_drop (client);
2693 /* should not be needed as we're terminated by failure to read
2694 * from stdin, but if stdin fails for some reason, this shouldn't
2695 * hurt for now --- might need to revise this later if we ever
2696 * decide that master connections might be temporarily down
2697 * for some reason */
2698 //GNUNET_SCHEDULER_shutdown ();
2706 * @param cls closure
2707 * @param server the initialized server
2708 * @param cfg configuration to use
2711 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2712 const struct GNUNET_CONFIGURATION_Handle *cfg)
2714 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2715 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2716 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2717 {&handle_configure_shared_service, NULL,
2718 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2719 {&handle_link_controllers, NULL,
2720 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2721 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2722 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2723 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2724 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2725 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2726 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2727 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2728 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2729 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2730 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2731 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2732 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2737 GNUNET_SERVER_add_handlers (server, message_handlers);
2738 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2739 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2741 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2742 &shutdown_task, NULL);
2743 LOG_DEBUG ("Testbed startup complete\n");
2748 * The starting point of execution
2751 main (int argc, char *const *argv)
2753 //sleep (15); /* Debugging */
2754 return (GNUNET_OK ==
2755 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2756 &testbed_run, NULL)) ? 0 : 1;
2759 /* end of gnunet-service-testbed.c */