2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/gnunet-service-testbed.c
23 * @brief implementation of the TESTBED service
24 * @author Sree Harsha Totakura
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
30 #include "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_hello_lib.h"
35 #include "gnunet_testbed_service.h"
37 #include "testbed_api.h"
38 #include "testbed_api_hosts.h"
39 #include "gnunet_testing_lib-new.h"
44 #define LOG(kind,...) \
45 GNUNET_log (kind, __VA_ARGS__)
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
54 * By how much should the arrays lists grow
56 #define LIST_GROW_STEP 10
59 * Default timeout for operations which may take some time
61 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
64 * The main context information associated with the client which started us
69 * The client handle associated with this context
71 struct GNUNET_SERVER_Client *client;
74 * The network address of the master controller
79 * The TESTING system handle for starting peers locally
81 struct GNUNET_TESTING_System *system;
84 * Event mask of event to be responded in this context
89 * Our host id according to this context
96 * The message queue for sending messages to clients
101 * The message to be sent
103 struct GNUNET_MessageHeader *msg;
106 * The client to send the message to
108 struct GNUNET_SERVER_Client *client;
111 * next pointer for DLL
113 struct MessageQueue *next;
116 * prev pointer for DLL
118 struct MessageQueue *prev;
123 * The structure for identifying a shared service
128 * The name of the shared service
133 * Number of shared peers per instance of the shared service
138 * Number of peers currently sharing the service
140 uint32_t num_sharing;
155 * The host destination is reachable thru
162 * Structure representing a connected(directly-linked) controller
167 * The controller process handle if we had started the controller
169 struct GNUNET_TESTBED_ControllerProc *controller_proc;
172 * The controller handle
174 struct GNUNET_TESTBED_Controller *controller;
177 * The id of the host this controller is running on
184 * States of LCFContext
189 * The Context has been initialized; Nothing has been done on it
194 * Delegated host has been registered at the forwarding controller
196 DELEGATED_HOST_REGISTERED,
199 * The slave host has been registred at the forwarding controller
201 SLAVE_HOST_REGISTERED,
204 * The context has been finished (may have error)
212 * Link controllers request forwarding context
217 * The gateway which will pass the link message to delegated host
219 struct Slave *gateway;
222 * The controller link message that has to be forwarded to
224 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
227 * The client which has asked to perform this operation
229 struct GNUNET_SERVER_Client *client;
232 * The host registration handle while registered hosts in this context
234 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
237 * The id of the operation which created this context
239 uint64_t operation_id;
242 * The state of this context
244 enum LCFContextState state;
249 uint32_t delegated_host_id;
254 uint32_t slave_host_id;
260 * Structure of a queue entry in LCFContext request queue
262 struct LCFContextQueue
267 struct LCFContext *lcf;
272 struct LCFContextQueue *next;
277 struct LCFContextQueue *prev;
282 * A locally started peer
291 * The peer handle from testing API
293 struct GNUNET_TESTING_Peer *peer;
296 * The modified (by GNUNET_TESTING_peer_configure) configuration this
297 * peer is configured with
299 struct GNUNET_CONFIGURATION_Handle *cfg;
306 * The controller this peer is started through
308 struct GNUNET_TESTBED_Controller *controller;
315 * Our local reference id for this peer
320 * Is this peer local created?
328 * State information for overlay connect context
338 * Peer 1 has connected to peer0
340 OCC_STATE_PEER0_SUCCESS,
343 * Peer 2 has connected to peer1
345 OCC_STATE_PEER1_SUCCESS,
351 * Context information for connecting 2 peers in overlay
353 struct OverlayConnectContext
356 * The client which has requested for overlay connection
358 struct GNUNET_SERVER_Client *client;
361 * the peer which has to connect to the other peer
368 struct Peer *other_peer;
371 * Transport handle of the first peer to offer second peer's HELLO
373 struct GNUNET_TRANSPORT_Handle *p1th;
376 * Transport handle of other peer to get its HELLO
378 struct GNUNET_TRANSPORT_Handle *p2th;
381 * Core handles of the peer which has to connect to other peer
383 struct GNUNET_CORE_Handle *ch;
386 * HELLO of the other peer
388 struct GNUNET_MessageHeader *hello;
391 * Get hello handle for the other peer
393 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
396 * The error message we send if this overlay connect operation has timed out
401 * The peer identity of the first peer
403 struct GNUNET_PeerIdentity peer_identity;
406 * The peer identity of the other peer
408 struct GNUNET_PeerIdentity other_peer_identity;
411 * The id of the operation responsible for creating this context
416 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
419 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
422 * The id of the overlay connect timeout task
424 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
427 * State information for determining whose HELLOs have been successfully
436 * Context information for operations forwarded to subcontrollers
438 struct ForwardedOperationContext
441 * The generated operation context
443 struct OperationContext *opc;
446 * The client to which we have to reply
448 struct GNUNET_SERVER_Client *client;
456 * Task ID for the timeout task
458 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
461 * The id of the operation that has been forwarded
463 uint64_t operation_id;
469 * Context information used while linking controllers
471 struct LinkControllersContext
474 * The client which initiated the link controller operation
476 struct GNUNET_SERVER_Client *client;
479 * The ID of the operation
481 uint64_t operation_id;
484 * Pointer to the slave handle if we are directly starting/connecting to the controller
492 * The master context; generated with the first INIT message
494 static struct Context *master_context;
501 * Current Transmit Handle; NULL if no notify transmit exists currently
503 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
510 * The head for the LCF queue
512 static struct LCFContextQueue *lcfq_head;
515 * The tail for the LCF queue
517 static struct LCFContextQueue *lcfq_tail;
520 * The message queue head
522 static struct MessageQueue *mq_head;
525 * The message queue tail
527 static struct MessageQueue *mq_tail;
532 static struct GNUNET_TESTBED_Host **host_list;
537 static struct Route **route_list;
540 * A list of directly linked neighbours
542 static struct Slave **slave_list;
545 * A list of peers we own locally
547 static struct Peer **peer_list;
550 * The hashmap of shared services
552 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
555 * The size of the host list
557 static uint32_t host_list_size;
560 * The size of the route list
562 static uint32_t route_list_size;
565 * The size of directly linked neighbours list
567 static uint32_t slave_list_size;
570 * The size of the peer list
572 static uint32_t peer_list_size;
579 * The lcf_task handle
581 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
584 * The shutdown task handle
586 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
590 * Function called to notify a client about the connection begin ready to queue
591 * more data. "buf" will be NULL and "size" zero if the connection was closed
592 * for writing in the meantime.
595 * @param size number of bytes available in buf
596 * @param buf where the callee should write the message
597 * @return number of bytes written to buf
600 transmit_ready_notify (void *cls, size_t size, void *buf)
602 struct MessageQueue *mq_entry;
604 transmit_handle = NULL;
606 GNUNET_assert (NULL != mq_entry);
609 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
610 size = ntohs (mq_entry->msg->size);
611 memcpy (buf, mq_entry->msg, size);
612 GNUNET_free (mq_entry->msg);
613 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
614 GNUNET_free (mq_entry);
616 if (NULL != mq_entry)
618 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
619 ntohs (mq_entry->msg->size),
620 GNUNET_TIME_UNIT_FOREVER_REL,
621 &transmit_ready_notify, NULL);
627 * Queues a message in send queue for sending to the service
629 * @param client the client to whom the queued message has to be sent
630 * @param msg the message to queue
633 queue_message (struct GNUNET_SERVER_Client *client,
634 struct GNUNET_MessageHeader *msg)
636 struct MessageQueue *mq_entry;
640 type = ntohs (msg->type);
641 size = ntohs (msg->size);
642 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
643 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
644 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
646 mq_entry->client = client;
647 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
649 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
650 if (NULL == transmit_handle)
652 GNUNET_SERVER_notify_transmit_ready (client, size,
653 GNUNET_TIME_UNIT_FOREVER_REL,
654 &transmit_ready_notify, NULL);
659 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
661 * @param ptr the memory block to realloc
662 * @param size the size of ptr
663 * @param new_size the size to which ptr has to be realloc'ed
664 * @return the newly reallocated memory block
667 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
669 ptr = GNUNET_realloc (ptr, new_size);
671 ptr = memset (ptr + size, 0, new_size - size);
677 * Function to add a host to the current list of known hosts
679 * @param host the host to add
680 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
684 host_list_add (struct GNUNET_TESTBED_Host *host)
688 host_id = GNUNET_TESTBED_host_get_id_ (host);
689 if (host_list_size <= host_id)
692 TESTBED_realloc (host_list,
693 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
694 sizeof (struct GNUNET_TESTBED_Host *) *
695 (host_list_size + LIST_GROW_STEP));
696 host_list_size += LIST_GROW_STEP;
698 if (NULL != host_list[host_id])
700 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
701 return GNUNET_SYSERR;
703 host_list[host_id] = host;
709 * Adds a route to the route list
711 * @param route the route to add
714 route_list_add (struct Route *route)
716 if (route->dest >= route_list_size)
719 TESTBED_realloc (route_list,
720 sizeof (struct Route *) * route_list_size,
721 sizeof (struct Route *) *
722 (route_list_size + LIST_GROW_STEP));
723 route_list_size += LIST_GROW_STEP;
725 GNUNET_assert (NULL == route_list[route->dest]);
726 route_list[route->dest] = route;
731 * Adds a slave to the slave array
733 * @param slave the slave controller to add
736 slave_list_add (struct Slave *slave)
738 if (slave->host_id >= slave_list_size)
740 slave_list = TESTBED_realloc (slave_list,
741 sizeof (struct Slave *) *slave_list_size,
742 sizeof (struct Slave *) *
743 (slave_list_size + LIST_GROW_STEP));
744 slave_list_size += LIST_GROW_STEP;
746 GNUNET_assert (NULL == slave_list[slave->host_id]);
747 slave_list[slave->host_id] = slave;
752 * Adds a peer to the peer array
754 * @param peer the peer to add
757 peer_list_add (struct Peer *peer)
759 if (peer->id >= peer_list_size)
761 peer_list = TESTBED_realloc (peer_list,
762 sizeof (struct Peer *) * peer_list_size,
763 sizeof (struct Peer *) *
764 (peer_list_size + LIST_GROW_STEP));
765 peer_list_size += LIST_GROW_STEP;
767 GNUNET_assert (NULL == peer_list[peer->id]);
768 peer_list[peer->id] = peer;
773 * Removes a the give peer from the peer array
775 * @param peer the peer to be removed
778 peer_list_remove (struct Peer *peer)
782 peer_list[peer->id] = NULL;
783 while (peer_list_size >= LIST_GROW_STEP)
785 for (id = peer_list_size - 1;
786 id > peer_list_size - LIST_GROW_STEP; id--)
787 if (NULL != peer_list[id])
789 if (id != peer_list_size - LIST_GROW_STEP)
791 peer_list_size -= LIST_GROW_STEP;
793 peer_list = GNUNET_realloc (peer_list, sizeof (struct GNUNET_TESTBED_Peer*)
799 * Finds the route with directly connected host as destination through which
800 * the destination host can be reached
802 * @param host_id the id of the destination host
803 * @return the route with directly connected destination host; NULL if no route
806 static struct Route *
807 find_dest_route (uint32_t host_id)
811 while(NULL != (route = route_list[host_id]))
813 if (route->thru == master_context->host_id)
815 host_id = route->thru;
822 * Routes message to a host given its host_id
824 * @param host_id the id of the destination host
825 * @param msg the message to be routed
828 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
835 * Send operation failure message to client
837 * @param client the client to which the failure message has to be sent to
838 * @param operation_id the id of the failed operation
839 * @param emsg the error message; can be NULL
842 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
843 uint64_t operation_id,
846 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
850 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
851 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
853 msg = GNUNET_malloc (msize);
854 msg->header.size = htons (msize);
855 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT);
856 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
857 msg->operation_id = GNUNET_htonll (operation_id);
859 memcpy (&msg[1], emsg, emsg_len);
860 queue_message (client, &msg->header);
865 * Function to send generic operation success message to given client
867 * @param client the client to send the message to
868 * @param operation_id the id of the operation which was successful
871 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
872 uint64_t operation_id)
874 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
877 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
878 msg = GNUNET_malloc (msize);
879 msg->header.size = htons (msize);
880 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
881 msg->operation_id = GNUNET_htonll (operation_id);
882 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
883 queue_message (client, &msg->header);
888 * The Link Controller forwarding task
890 * @param cls the LCFContext
891 * @param tc the Task context from scheduler
894 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
898 * Completion callback for host registrations while forwarding Link Controller messages
900 * @param cls the LCFContext
901 * @param emsg the error message; NULL if host registration is successful
904 lcf_proc_cc (void *cls, const char *emsg)
906 struct LCFContext *lcf = cls;
909 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
914 goto registration_error;
915 lcf->state = DELEGATED_HOST_REGISTERED;
916 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
918 case DELEGATED_HOST_REGISTERED:
920 goto registration_error;
921 lcf->state = SLAVE_HOST_REGISTERED;
922 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
925 GNUNET_assert (0); /* Shouldn't reach here */
930 LOG (GNUNET_ERROR_TYPE_WARNING,
931 "Host registration failed with message: %s\n", emsg);
932 lcf->state = FINISHED;
933 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
938 * Callback to be called when forwarded link controllers operation is
939 * successfull. We have to relay the reply msg back to the client
941 * @param cls ForwardedOperationContext
942 * @param msg the peer create success message
945 forwarded_operation_reply_relay (void *cls,
946 const struct GNUNET_MessageHeader *msg)
948 struct ForwardedOperationContext *fopc = cls;
949 struct GNUNET_MessageHeader *dup_msg;
952 msize = ntohs (msg->size);
953 dup_msg = GNUNET_malloc (msize);
954 (void) memcpy (dup_msg, msg, msize);
955 queue_message (fopc->client, dup_msg);
956 GNUNET_SERVER_client_drop (fopc->client);
957 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
963 * Task to free resources when forwarded link controllers has been timedout
965 * @param cls the ForwardedOperationContext
966 * @param tc the task context from scheduler
969 forwarded_operation_timeout (void *cls,
970 const struct GNUNET_SCHEDULER_TaskContext
973 struct ForwardedOperationContext *fopc = cls;
975 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
976 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
977 GNUNET_SERVER_client_drop (fopc->client);
983 * The Link Controller forwarding task
985 * @param cls the LCFContext
986 * @param tc the Task context from scheduler
989 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
991 struct LCFContext *lcf = cls;
992 struct LCFContextQueue *lcfq;
993 struct ForwardedOperationContext *fopc;
995 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1000 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1001 lcf->gateway->controller))
1004 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1005 host_list[lcf->delegated_host_id],
1010 lcf->state = DELEGATED_HOST_REGISTERED;
1011 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1014 case DELEGATED_HOST_REGISTERED:
1016 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1017 lcf->gateway->controller))
1020 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1021 host_list[lcf->slave_host_id],
1026 lcf->state = SLAVE_HOST_REGISTERED;
1027 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1030 case SLAVE_HOST_REGISTERED:
1031 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1032 fopc->client = lcf->client;
1033 fopc->operation_id = lcf->operation_id;
1035 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1038 &forwarded_operation_reply_relay,
1040 fopc->timeout_task =
1041 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1042 &forwarded_operation_timeout, fopc);
1043 lcf->state = FINISHED;
1044 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1048 GNUNET_assert (lcfq->lcf == lcf);
1049 GNUNET_free (lcf->msg);
1051 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1053 if (NULL != lcfq_head)
1055 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1061 * Callback for event from slave controllers
1063 * @param cls struct Slave *
1064 * @param event information about the event
1067 slave_event_callback (void *cls,
1068 const struct GNUNET_TESTBED_EventInformation *event)
1075 * Callback to signal successfull startup of the controller process
1077 * @param cls the closure from GNUNET_TESTBED_controller_start()
1078 * @param cfg the configuration with which the controller has been started;
1079 * NULL if status is not GNUNET_OK
1080 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1081 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1084 slave_status_callback (void *cls,
1085 const struct GNUNET_CONFIGURATION_Handle *cfg,
1088 struct LinkControllersContext *lcc = cls;
1090 if (GNUNET_SYSERR == status)
1092 lcc->slave->controller_proc = NULL;
1093 LOG (GNUNET_ERROR_TYPE_WARNING,
1094 "Unexpected slave shutdown\n");
1095 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1098 lcc->slave->controller =
1099 GNUNET_TESTBED_controller_connect (cfg, host_list[lcc->slave->host_id],
1100 master_context->event_mask,
1101 &slave_event_callback, lcc->slave);
1102 if (NULL != lcc->slave->controller)
1103 send_operation_success_msg (lcc->client, lcc->operation_id);
1105 send_operation_fail_msg (lcc->client, lcc->operation_id,
1106 "Could not connect to delegated controller");
1107 GNUNET_SERVER_client_drop (lcc->client);
1113 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1116 * @param client identification of the client
1117 * @param message the actual message
1120 handle_init (void *cls,
1121 struct GNUNET_SERVER_Client *client,
1122 const struct GNUNET_MessageHeader *message)
1124 const struct GNUNET_TESTBED_InitMessage *msg;
1125 struct GNUNET_TESTBED_Host *host;
1126 const char *controller_hostname;
1129 if (NULL != master_context)
1132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1135 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1136 msize = ntohs (message->size);
1137 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1143 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1144 controller_hostname = (const char *) &msg[1];
1145 if ('\0' != controller_hostname[msize - 1])
1148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1151 master_context = GNUNET_malloc (sizeof (struct Context));
1152 master_context->client = client;
1153 master_context->host_id = ntohl (msg->host_id);
1154 master_context->master_ip = GNUNET_strdup (controller_hostname);
1155 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1156 master_context->system =
1157 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1158 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1160 host_list_add (host);
1161 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1162 GNUNET_SERVER_client_keep (client);
1163 LOG_DEBUG ("Created master context with host ID: %u\n",
1164 master_context->host_id);
1165 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1170 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1173 * @param client identification of the client
1174 * @param message the actual message
1177 handle_add_host (void *cls,
1178 struct GNUNET_SERVER_Client *client,
1179 const struct GNUNET_MessageHeader *message)
1181 struct GNUNET_TESTBED_Host *host;
1182 const struct GNUNET_TESTBED_AddHostMessage *msg;
1183 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1188 uint16_t username_length;
1189 uint16_t hostname_length;
1190 uint16_t reply_size;
1193 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1194 msize = ntohs (msg->header.size);
1195 username = (char *) &(msg[1]);
1196 username_length = ntohs (msg->user_name_length);
1197 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1198 + username_length + 1)); /* msg must contain hostname */
1199 if (0 != username_length)
1200 GNUNET_assert ('\0' == username[username_length]);
1201 username_length = (0 == username_length) ? 0 : username_length + 1;
1202 hostname = username + username_length;
1203 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1205 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1206 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1207 host_id = ntohl (msg->host_id);
1208 LOG_DEBUG ("Received ADDHOST message\n");
1209 LOG_DEBUG ("-------host id: %u\n", host_id);
1210 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
1211 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
1214 LOG_DEBUG ("-------username: NULL\n");
1217 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1218 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1219 ntohs (msg->ssh_port));
1220 GNUNET_assert (NULL != host);
1221 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1222 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1223 if (GNUNET_OK != host_list_add (host))
1225 /* We are unable to add a host */
1226 emsg = "A host exists with given host-id";
1227 LOG_DEBUG ("%s: %u", emsg, host_id);
1228 GNUNET_TESTBED_host_destroy (host);
1229 reply_size += strlen (emsg) + 1;
1230 reply = GNUNET_malloc (reply_size);
1231 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1234 reply = GNUNET_malloc (reply_size);
1235 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1236 reply->header.size = htons (reply_size);
1237 reply->host_id = htonl (host_id);
1238 queue_message (client, &reply->header);
1243 * Iterator over hash map entries.
1245 * @param cls closure
1246 * @param key current key code
1247 * @param value value in the hash map
1248 * @return GNUNET_YES if we should continue to
1252 int ss_exists_iterator (void *cls,
1253 const struct GNUNET_HashCode * key,
1256 struct SharedService *queried_ss = cls;
1257 struct SharedService *ss = value;
1259 if (0 == strcmp (ss->name, queried_ss->name))
1267 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1270 * @param client identification of the client
1271 * @param message the actual message
1274 handle_configure_shared_service (void *cls,
1275 struct GNUNET_SERVER_Client *client,
1276 const struct GNUNET_MessageHeader *message)
1278 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1279 struct SharedService *ss;
1281 struct GNUNET_HashCode hash;
1283 uint16_t service_name_size;
1285 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1286 msg_size = ntohs (message->size);
1287 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1293 service_name_size = msg_size -
1294 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1295 service_name = (char *) &msg[1];
1296 if ('\0' != service_name[service_name_size - 1])
1299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1302 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1303 service_name, ntohl (msg->num_peers));
1304 if (ntohl (msg->host_id) != master_context->host_id)
1306 route_message (ntohl (msg->host_id), message);
1307 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1310 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1311 ss = GNUNET_malloc (sizeof (struct SharedService));
1312 ss->name = strdup (service_name);
1313 ss->num_shared = ntohl (msg->num_peers);
1314 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1315 if (GNUNET_SYSERR ==
1316 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1317 &ss_exists_iterator, ss))
1319 LOG (GNUNET_ERROR_TYPE_WARNING,
1320 "Service %s already configured as a shared service. "
1321 "Ignoring service sharing request \n", ss->name);
1322 GNUNET_free (ss->name);
1326 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1327 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1332 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1335 * @param client identification of the client
1336 * @param message the actual message
1339 handle_link_controllers (void *cls,
1340 struct GNUNET_SERVER_Client *client,
1341 const struct GNUNET_MessageHeader *message)
1343 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1344 struct GNUNET_CONFIGURATION_Handle *cfg;
1345 struct LCFContextQueue *lcfq;
1346 struct Route *route;
1347 struct Route *new_route;
1351 uint32_t delegated_host_id;
1352 uint32_t slave_host_id;
1355 if (NULL == master_context)
1358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1361 msize = ntohs (message->size);
1362 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1368 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1369 delegated_host_id = ntohl (msg->delegated_host_id);
1370 if (delegated_host_id == master_context->host_id)
1373 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1377 if ((delegated_host_id >= host_list_size) ||
1378 (NULL == host_list[delegated_host_id]))
1380 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1384 slave_host_id = ntohl (msg->slave_host_id);
1385 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1387 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1391 if (slave_host_id == delegated_host_id)
1393 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1398 if (slave_host_id == master_context->host_id) /* Link from us */
1400 struct Slave *slave;
1402 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1403 config_size = ntohs (msg->config_size);
1404 if ((delegated_host_id < slave_list_size) &&
1405 (NULL != slave_list[delegated_host_id])) /* We have already added */
1407 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1412 config = GNUNET_malloc (config_size);
1413 dest_size = (uLongf) config_size;
1414 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1415 (const Bytef *) &msg[1], (uLong) msize))
1417 GNUNET_break (0); /* Compression error */
1418 GNUNET_free (config);
1419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1422 if (config_size != dest_size)
1424 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1425 GNUNET_free (config);
1426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1429 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1430 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1433 GNUNET_break (0); /* Configuration parsing error */
1434 GNUNET_free (config);
1435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1438 GNUNET_free (config);
1439 if ((delegated_host_id < slave_list_size) &&
1440 (NULL != slave_list[delegated_host_id]))
1442 GNUNET_break (0); /* Configuration parsing error */
1443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1446 slave = GNUNET_malloc (sizeof (struct Slave));
1447 slave->host_id = delegated_host_id;
1448 slave_list_add (slave);
1449 if (1 == msg->is_subordinate)
1451 struct LinkControllersContext *lcc;
1452 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1453 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1454 GNUNET_SERVER_client_keep (client);
1455 lcc->client = client;
1457 slave->controller_proc =
1458 GNUNET_TESTBED_controller_start (master_context->master_ip,
1459 host_list[slave->host_id],
1460 cfg, &slave_status_callback,
1465 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1466 master_context->event_mask,
1467 &slave_event_callback, slave);
1468 if (NULL != slave->controller)
1469 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1471 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1472 "Could not connect to delegated controller");
1474 GNUNET_CONFIGURATION_destroy (cfg);
1475 new_route = GNUNET_malloc (sizeof (struct Route));
1476 new_route->dest = delegated_host_id;
1477 new_route->thru = master_context->host_id;
1478 route_list_add (new_route);
1479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1483 /* Route the request */
1484 if (slave_host_id >= route_list_size)
1486 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1490 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1491 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1492 lcfq->lcf->delegated_host_id = delegated_host_id;
1493 lcfq->lcf->slave_host_id = slave_host_id;
1494 route = find_dest_route (slave_host_id);
1495 GNUNET_assert (NULL != route); /* because we add routes carefully */
1496 GNUNET_assert (route->dest < slave_list_size);
1497 GNUNET_assert (NULL != slave_list[route->dest]);
1498 lcfq->lcf->state = INIT;
1499 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1500 lcfq->lcf->gateway = slave_list[route->dest];
1501 lcfq->lcf->msg = GNUNET_malloc (msize);
1502 (void) memcpy (lcfq->lcf->msg, msg, msize);
1503 GNUNET_SERVER_client_keep (client);
1504 lcfq->lcf->client = client;
1505 if (NULL == lcfq_head)
1507 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1508 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1509 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1512 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1513 /* FIXME: Adding a new route should happen after the controllers are linked
1515 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1516 new_route = GNUNET_malloc (sizeof (struct Route));
1517 new_route->dest = delegated_host_id;
1518 new_route->thru = route->dest;
1519 route_list_add (new_route);
1524 * The task to be executed if the forwarded peer create operation has been
1527 * @param cls the FowardedOperationContext
1528 * @param tc the TaskContext from the scheduler
1531 peer_create_forward_timeout (void *cls,
1532 const struct GNUNET_SCHEDULER_TaskContext *tc)
1534 struct ForwardedOperationContext *fo_ctxt = cls;
1536 /* send error msg to client */
1537 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id,
1539 GNUNET_SERVER_client_drop (fo_ctxt->client);
1540 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1541 GNUNET_free (fo_ctxt);
1546 * Callback to be called when forwarded peer create operation is
1547 * successfull. We have to relay the reply msg back to the client
1549 * @param cls ForwardedOperationContext
1550 * @param msg the peer create success message
1553 peer_create_success_cb (void *cls,
1554 const struct GNUNET_MessageHeader *msg)
1556 struct ForwardedOperationContext *fo_ctxt = cls;
1557 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1558 struct GNUNET_MessageHeader *dup_msg;
1562 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1563 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1566 = (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1567 peer = GNUNET_malloc (sizeof (struct Peer));
1568 peer->is_remote = GNUNET_YES;
1569 peer->id = ntohl (success_msg->peer_id);
1570 GNUNET_assert (NULL != fo_ctxt->cls);
1571 peer->details.remote.controller = fo_ctxt->cls;
1572 peer_list_add (peer);
1574 msize = ntohs (msg->size);
1575 dup_msg = GNUNET_malloc (msize);
1576 (void) memcpy (dup_msg, msg, msize);
1577 queue_message (fo_ctxt->client, dup_msg);
1578 GNUNET_SERVER_client_drop (fo_ctxt->client);
1579 GNUNET_free (fo_ctxt);
1585 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1588 * @param client identification of the client
1589 * @param message the actual message
1592 handle_peer_create (void *cls,
1593 struct GNUNET_SERVER_Client *client,
1594 const struct GNUNET_MessageHeader *message)
1596 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1597 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1598 struct GNUNET_CONFIGURATION_Handle *cfg;
1599 struct ForwardedOperationContext *fo_ctxt;
1600 struct Route *route;
1605 uint32_t config_size;
1610 msize = ntohs (message->size);
1611 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1613 GNUNET_break (0); /* We need configuration */
1614 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1617 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1618 host_id = ntohl (msg->host_id);
1619 if (host_id == master_context->host_id)
1623 /* We are responsible for this peer */
1624 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1625 config_size = ntohl (msg->config_size);
1626 config = GNUNET_malloc (config_size);
1627 dest_size = config_size;
1628 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1629 (const Bytef *) &msg[1], (uLong) msize)))
1631 GNUNET_break (0); /* uncompression error */
1632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1635 if (config_size != dest_size)
1637 GNUNET_break (0);/* Uncompressed config size mismatch */
1638 GNUNET_free (config);
1639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1642 cfg = GNUNET_CONFIGURATION_create ();
1643 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1646 GNUNET_break (0); /* Configuration parsing error */
1647 GNUNET_free (config);
1648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1651 GNUNET_free (config);
1652 peer = GNUNET_malloc (sizeof (struct Peer));
1653 peer->is_remote = GNUNET_NO;
1654 peer->details.local.cfg = cfg;
1655 peer->id = ntohl (msg->peer_id);
1656 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1657 peer->details.local.peer =
1658 GNUNET_TESTING_peer_configure (master_context->system,
1659 peer->details.local.cfg, peer->id,
1662 if (NULL == peer->details.local.peer)
1664 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1671 peer_list_add (peer);
1672 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1673 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1674 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1675 reply->peer_id = msg->peer_id;
1676 reply->operation_id = msg->operation_id;
1677 queue_message (client, &reply->header);
1678 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1682 /* Forward peer create request */
1683 route = find_dest_route (host_id);
1687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1690 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1691 GNUNET_SERVER_client_keep (client);
1692 fo_ctxt->client = client;
1693 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1694 fo_ctxt->cls = slave_list[route->dest]->controller;
1696 GNUNET_TESTBED_forward_operation_msg_ (slave_list[route->dest]->controller,
1697 fo_ctxt->operation_id,
1699 peer_create_success_cb, fo_ctxt);
1700 fo_ctxt->timeout_task =
1701 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1702 &peer_create_forward_timeout, fo_ctxt);
1704 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1709 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1712 * @param client identification of the client
1713 * @param message the actual message
1716 handle_peer_destroy (void *cls,
1717 struct GNUNET_SERVER_Client *client,
1718 const struct GNUNET_MessageHeader *message)
1720 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1721 struct ForwardedOperationContext *fopc;
1725 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1726 peer_id = ntohl (msg->peer_id);
1727 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1728 peer_id, GNUNET_ntohll (msg->operation_id));
1729 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1731 LOG (GNUNET_ERROR_TYPE_ERROR,
1732 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1736 peer = peer_list[peer_id];
1737 if (GNUNET_YES == peer->is_remote)
1739 /* Forward the destory message to sub controller */
1740 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1741 GNUNET_SERVER_client_keep (client);
1742 fopc->client = client;
1743 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1745 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1748 &forwarded_operation_reply_relay,
1750 fopc->timeout_task =
1751 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1752 &forwarded_operation_timeout, fopc);
1753 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1756 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1757 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1758 peer_list_remove (peer);
1760 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1761 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1766 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1769 * @param client identification of the client
1770 * @param message the actual message
1773 handle_peer_start (void *cls,
1774 struct GNUNET_SERVER_Client *client,
1775 const struct GNUNET_MessageHeader *message)
1777 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1778 struct GNUNET_TESTBED_PeerEventMessage *reply;
1779 struct ForwardedOperationContext *fopc;
1783 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1784 peer_id = ntohl (msg->peer_id);
1785 if ((peer_id >= peer_list_size)
1786 || (NULL == peer_list[peer_id]))
1789 LOG (GNUNET_ERROR_TYPE_ERROR,
1790 "Asked to start a non existent peer with id: %u\n", peer_id);
1791 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1794 peer = peer_list[peer_id];
1795 if (GNUNET_YES == peer->is_remote)
1797 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1798 GNUNET_SERVER_client_keep (client);
1799 fopc->client = client;
1800 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1802 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1805 &forwarded_operation_reply_relay,
1807 fopc->timeout_task =
1808 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1809 &forwarded_operation_timeout, fopc);
1810 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1814 GNUNET_TESTING_peer_start (peer->details.local.peer))
1816 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1818 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1821 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1822 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1823 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1824 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1825 reply->host_id = htonl (master_context->host_id);
1826 reply->peer_id = msg->peer_id;
1827 reply->operation_id = msg->operation_id;
1828 queue_message (client, &reply->header);
1829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1834 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1837 * @param client identification of the client
1838 * @param message the actual message
1841 handle_peer_stop (void *cls,
1842 struct GNUNET_SERVER_Client *client,
1843 const struct GNUNET_MessageHeader *message)
1845 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1846 struct GNUNET_TESTBED_PeerEventMessage *reply;
1847 struct ForwardedOperationContext *fopc;
1851 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1852 peer_id = ntohl (msg->peer_id);
1853 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1855 GNUNET_break (0); /* FIXME: route to slave? */
1856 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1859 peer = peer_list[peer_id];
1860 if (GNUNET_YES == peer->is_remote)
1862 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1863 GNUNET_SERVER_client_keep (client);
1864 fopc->client = client;
1865 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1867 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1870 &forwarded_operation_reply_relay,
1872 fopc->timeout_task =
1873 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1874 &forwarded_operation_timeout, fopc);
1875 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1879 GNUNET_TESTING_peer_stop (peer->details.local.peer))
1881 /* FIXME: return FAILURE message */
1883 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1886 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1887 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1888 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1889 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1890 reply->host_id = htonl (master_context->host_id);
1891 reply->peer_id = msg->peer_id;
1892 reply->operation_id = msg->operation_id;
1893 queue_message (client, &reply->header);
1894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1899 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1902 * @param client identification of the client
1903 * @param message the actual message
1906 handle_peer_get_config (void *cls,
1907 struct GNUNET_SERVER_Client *client,
1908 const struct GNUNET_MessageHeader *message)
1910 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1911 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1920 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1921 peer_id = ntohl (msg->peer_id);
1922 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1924 /* FIXME: return FAILURE message */
1926 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1929 peer = peer_list[peer_id];
1930 if (GNUNET_YES == peer->is_remote)
1932 /* FIXME: forward to sub controller */
1934 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1938 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
1940 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1941 GNUNET_free (config);
1942 msize = xc_size + sizeof (struct
1943 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1944 reply = GNUNET_realloc (xconfig, msize);
1945 (void) memmove (&reply[1], reply, xc_size);
1946 reply->header.size = htons (msize);
1947 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1948 reply->peer_id = msg->peer_id;
1949 reply->operation_id = msg->operation_id;
1950 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
1951 &reply->peer_identity);
1952 reply->config_size = htons ((uint16_t) c_size);
1953 queue_message (client, &reply->header);
1954 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1959 * Task for cleaing up overlay connect context structure
1961 * @param cls the overlay connect context
1962 * @param tc the task context
1965 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1967 struct OverlayConnectContext *occ = cls;
1969 GNUNET_free_non_null (occ->emsg);
1970 GNUNET_free_non_null (occ->hello);
1971 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1972 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1973 if (NULL != occ->ch)
1974 GNUNET_CORE_disconnect (occ->ch);
1975 if (NULL != occ->ghh)
1976 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1977 if (NULL != occ->p1th)
1978 GNUNET_TRANSPORT_disconnect (occ->p1th);
1979 if (NULL != occ->p2th)
1980 GNUNET_TRANSPORT_disconnect (occ->p2th);
1986 * Task which will be run when overlay connect request has been timed out
1992 timeout_overlay_connect (void *cls,
1993 const struct GNUNET_SCHEDULER_TaskContext *tc)
1995 struct OverlayConnectContext *occ = cls;
1997 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1998 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1999 GNUNET_SERVER_client_drop (occ->client);
2000 occ_cleanup (occ, tc);
2006 * Function called to notify transport users that another
2007 * peer connected to us.
2009 * @param cls closure
2010 * @param new_peer the peer that connected
2011 * @param ats performance data
2012 * @param ats_count number of entries in ats (excluding 0-termination)
2015 overlay_connect_notify (void *cls,
2016 const struct GNUNET_PeerIdentity * new_peer,
2017 const struct GNUNET_ATS_Information * ats,
2018 unsigned int ats_count)
2020 struct OverlayConnectContext *occ = cls;
2021 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2023 char *other_peer_str;
2025 LOG_DEBUG ("Overlay connect notify\n");
2026 if (0 == memcmp (new_peer, &occ->peer_identity,
2027 sizeof (struct GNUNET_PeerIdentity)))
2029 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2030 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2031 if (0 != memcmp (new_peer, &occ->other_peer_identity,
2032 sizeof (struct GNUNET_PeerIdentity)))
2034 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
2035 new_peer_str, other_peer_str);
2036 GNUNET_free (new_peer_str);
2037 GNUNET_free (other_peer_str);
2040 LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
2041 GNUNET_free (new_peer_str);
2042 GNUNET_free (other_peer_str);
2043 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2045 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2046 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2048 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2049 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2050 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2051 GNUNET_free_non_null (occ->emsg);
2053 GNUNET_TRANSPORT_disconnect (occ->p1th);
2055 /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
2056 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2057 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2058 msg->header.size = htons (sizeof (struct
2059 GNUNET_TESTBED_ConnectionEventMessage));
2060 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2061 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2062 msg->peer1 = htonl (occ->peer->id);
2063 msg->peer2 = htonl (occ->other_peer->id);
2064 msg->operation_id = GNUNET_htonll (occ->op_id);
2065 queue_message (occ->client, &msg->header);
2066 GNUNET_SERVER_client_drop (occ->client);
2067 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2072 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2074 struct OverlayConnectContext *occ = cls;
2075 char *other_peer_str;
2077 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2078 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2080 GNUNET_assert (NULL != occ->hello);
2081 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2082 LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
2083 GNUNET_free (other_peer_str);
2084 GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
2085 GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
2086 occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
2091 * Test for checking whether HELLO message is empty
2093 * @param cls empty flag to set
2094 * @param address the HELLO
2095 * @param expiration expiration of the HELLO
2099 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2100 struct GNUNET_TIME_Absolute expiration)
2110 * Function called whenever there is an update to the
2111 * HELLO of peers in the OverlayConnectClosure
2113 * @param cls closure
2114 * @param hello our updated HELLO
2117 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2119 struct OverlayConnectContext *occ = cls;
2123 msize = ntohs (hello->size);
2126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2127 "HELLO message of peer %s is of size 0\n",
2128 &occ->other_peer_identity);
2133 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
2134 GNUNET_NO, &test_address, &empty);
2135 if (GNUNET_YES == empty)
2137 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
2140 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
2141 occ->hello = GNUNET_malloc (msize);
2142 memcpy (occ->hello, hello, msize);
2143 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2145 GNUNET_TRANSPORT_disconnect (occ->p2th);
2147 GNUNET_free_non_null (occ->emsg);
2148 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2149 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2154 * Function called after GNUNET_CORE_connect has succeeded (or failed
2155 * for good). Note that the private key of the peer is intentionally
2156 * not exposed here; if you need it, your process should try to read
2157 * the private key file directly (which should work if you are
2160 * @param cls closure
2161 * @param server handle to the server, NULL if we failed
2162 * @param my_identity ID of this peer, NULL if we failed
2165 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
2166 const struct GNUNET_PeerIdentity *my_identity)
2168 struct OverlayConnectContext *occ = cls;
2170 GNUNET_free_non_null (occ->emsg);
2172 memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
2174 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2175 &occ->peer_identity, NULL, NULL, NULL, NULL);
2176 /* Connect to the transport of 2nd peer and get its HELLO message */
2177 GNUNET_TESTING_peer_get_identity (occ->other_peer->details.local.peer,
2178 &occ->other_peer_identity);
2180 GNUNET_TRANSPORT_connect (occ->other_peer->details.local.cfg,
2181 &occ->other_peer_identity,
2182 NULL, NULL, NULL, NULL);
2183 if ((NULL == occ->p1th) || (NULL == occ->p2th))
2185 occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
2188 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s
2189 (&occ->other_peer_identity));
2190 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2191 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
2195 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2196 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2201 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2204 * @param client identification of the client
2205 * @param message the actual message
2208 handle_overlay_connect (void *cls,
2209 struct GNUNET_SERVER_Client *client,
2210 const struct GNUNET_MessageHeader *message)
2212 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2213 struct OverlayConnectContext *occ;
2214 struct GNUNET_CORE_MessageHandler no_handlers[] = {
2220 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2221 p1 = ntohl (msg->peer1);
2222 p2 = ntohl (msg->peer2);
2223 GNUNET_assert (p1 < peer_list_size);
2224 GNUNET_assert (NULL != peer_list[p1]);
2225 GNUNET_assert (p2 < peer_list_size);
2226 GNUNET_assert (NULL != peer_list[p2]);
2227 /* FIXME: Add cases where we have to forward overlay connect message to sub
2229 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2230 GNUNET_SERVER_client_keep (client);
2231 occ->client = client;
2232 occ->state = OCC_STATE_INIT;
2233 occ->peer = peer_list[p1];
2234 occ->other_peer = peer_list[p2];
2235 occ->op_id = GNUNET_ntohll (msg->operation_id);
2237 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2238 (GNUNET_TIME_UNIT_SECONDS, 30),
2239 &timeout_overlay_connect, occ);
2240 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2241 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2243 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2244 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2245 GNUNET_NO, no_handlers);
2246 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2251 * Iterator over hash map entries.
2253 * @param cls closure
2254 * @param key current key code
2255 * @param value value in the hash map
2256 * @return GNUNET_YES if we should continue to
2261 ss_map_free_iterator (void *cls,
2262 const struct GNUNET_HashCode * key, void *value)
2264 struct SharedService *ss = value;
2266 GNUNET_assert (GNUNET_YES ==
2267 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2268 GNUNET_free (ss->name);
2275 * Task to clean up and shutdown nicely
2278 * @param tc the TaskContext from scheduler
2281 shutdown_task (void *cls,
2282 const struct GNUNET_SCHEDULER_TaskContext *tc)
2284 struct LCFContextQueue *lcfq;
2287 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2288 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2289 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2291 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2292 if (NULL != lcfq_head)
2294 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2296 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2297 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2299 if (NULL != lcfq_head->lcf->rhandle)
2300 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2302 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2303 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2305 GNUNET_free (lcfq->lcf->msg);
2306 GNUNET_free (lcfq->lcf);
2307 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2310 /* Clear peer list */
2311 for (id = 0; id < peer_list_size; id++)
2312 if (NULL != peer_list[id])
2314 if (GNUNET_NO == peer_list[id]->is_remote)
2316 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2317 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2319 GNUNET_free (peer_list[id]);
2321 GNUNET_free_non_null (peer_list);
2322 /* Clear host list */
2323 for (id = 0; id < host_list_size; id++)
2324 if (NULL != host_list[id])
2325 GNUNET_TESTBED_host_destroy (host_list[id]);
2326 GNUNET_free_non_null (host_list);
2327 /* Clear route list */
2328 for (id = 0; id < route_list_size; id++)
2329 if (NULL != route_list[id])
2330 GNUNET_free (route_list[id]);
2331 GNUNET_free_non_null (route_list);
2332 /* Clear slave_list */
2333 for (id = 0; id < slave_list_size; id++)
2334 if (NULL != slave_list[id])
2336 if (NULL != slave_list[id]->controller)
2337 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2338 if (NULL != slave_list[id]->controller_proc)
2339 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2341 if (NULL != master_context)
2343 GNUNET_free_non_null (master_context->master_ip);
2344 if (NULL != master_context->system)
2345 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2346 GNUNET_free (master_context);
2347 master_context = NULL;
2353 * Callback for client disconnect
2356 * @param client the client which has disconnected
2359 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2361 if (NULL == master_context)
2363 if (client == master_context->client)
2365 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2366 GNUNET_SERVER_client_drop (client);
2367 /* should not be needed as we're terminated by failure to read
2368 from stdin, but if stdin fails for some reason, this shouldn't
2369 hurt for now --- might need to revise this later if we ever
2370 decide that master connections might be temporarily down
2372 //GNUNET_SCHEDULER_shutdown ();
2380 * @param cls closure
2381 * @param server the initialized server
2382 * @param cfg configuration to use
2385 testbed_run (void *cls,
2386 struct GNUNET_SERVER_Handle *server,
2387 const struct GNUNET_CONFIGURATION_Handle *cfg)
2389 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
2391 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2392 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2393 {&handle_configure_shared_service, NULL,
2394 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2395 {&handle_link_controllers, NULL,
2396 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2397 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2398 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2399 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2400 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2401 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2402 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2403 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2404 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2405 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2406 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2407 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2411 GNUNET_SERVER_add_handlers (server,
2413 GNUNET_SERVER_disconnect_notify (server,
2414 &client_disconnect_cb,
2416 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2418 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2421 LOG_DEBUG ("Testbed startup complete\n");
2426 * The starting point of execution
2428 int main (int argc, char *const *argv)
2430 //sleep (15); /* Debugging */
2433 GNUNET_SERVICE_run (argc,
2436 GNUNET_SERVICE_OPTION_NONE,