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)
211 * Link controllers request forwarding context
216 * The gateway which will pass the link message to delegated host
218 struct Slave *gateway;
221 * The controller link message that has to be forwarded to
223 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
226 * The client which has asked to perform this operation
228 struct GNUNET_SERVER_Client *client;
231 * The host registration handle while registered hosts in this context
233 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
236 * The id of the operation which created this context
238 uint64_t operation_id;
241 * The state of this context
243 enum LCFContextState state;
248 uint32_t delegated_host_id;
253 uint32_t slave_host_id;
259 * Structure of a queue entry in LCFContext request queue
261 struct LCFContextQueue
266 struct LCFContext *lcf;
271 struct LCFContextQueue *next;
276 struct LCFContextQueue *prev;
281 * A locally started peer
290 * The peer handle from testing API
292 struct GNUNET_TESTING_Peer *peer;
295 * The modified (by GNUNET_TESTING_peer_configure) configuration this
296 * peer is configured with
298 struct GNUNET_CONFIGURATION_Handle *cfg;
305 * The controller this peer is started through
307 struct GNUNET_TESTBED_Controller *controller;
314 * Our local reference id for this peer
319 * Is this peer local created?
327 * Context information for connecting 2 peers in overlay
329 struct OverlayConnectContext
332 * The client which has requested for overlay connection
334 struct GNUNET_SERVER_Client *client;
337 * the peer which has to connect to the other peer
344 struct Peer *other_peer;
347 * Transport handle of the first peer to get its HELLO
349 struct GNUNET_TRANSPORT_Handle *p1th;
352 * Transport handle of other peer to offer first peer's HELLO
354 struct GNUNET_TRANSPORT_Handle *p2th;
357 * Core handles of the first peer; used to notify when second peer connects to it
359 struct GNUNET_CORE_Handle *ch;
362 * HELLO of the other peer
364 struct GNUNET_MessageHeader *hello;
367 * Get hello handle to acquire HELLO of first peer
369 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
372 * The error message we send if this overlay connect operation has timed out
377 * The peer identity of the first peer
379 struct GNUNET_PeerIdentity peer_identity;
382 * The peer identity of the other peer
384 struct GNUNET_PeerIdentity other_peer_identity;
387 * The id of the operation responsible for creating this context
392 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
395 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
398 * The id of the overlay connect timeout task
400 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
406 * Context information for operations forwarded to subcontrollers
408 struct ForwardedOperationContext
411 * The generated operation context
413 struct OperationContext *opc;
416 * The client to which we have to reply
418 struct GNUNET_SERVER_Client *client;
426 * Task ID for the timeout task
428 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
431 * The id of the operation that has been forwarded
433 uint64_t operation_id;
439 * Context information used while linking controllers
441 struct LinkControllersContext
444 * The client which initiated the link controller operation
446 struct GNUNET_SERVER_Client *client;
449 * The ID of the operation
451 uint64_t operation_id;
454 * Pointer to the slave handle if we are directly starting/connecting to the controller
462 * The master context; generated with the first INIT message
464 static struct Context *master_context;
471 * Current Transmit Handle; NULL if no notify transmit exists currently
473 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
480 * The head for the LCF queue
482 static struct LCFContextQueue *lcfq_head;
485 * The tail for the LCF queue
487 static struct LCFContextQueue *lcfq_tail;
490 * The message queue head
492 static struct MessageQueue *mq_head;
495 * The message queue tail
497 static struct MessageQueue *mq_tail;
502 static struct GNUNET_TESTBED_Host **host_list;
507 static struct Route **route_list;
510 * A list of directly linked neighbours
512 static struct Slave **slave_list;
515 * A list of peers we own locally
517 static struct Peer **peer_list;
520 * The hashmap of shared services
522 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
525 * The size of the host list
527 static uint32_t host_list_size;
530 * The size of the route list
532 static uint32_t route_list_size;
535 * The size of directly linked neighbours list
537 static uint32_t slave_list_size;
540 * The size of the peer list
542 static uint32_t peer_list_size;
549 * The lcf_task handle
551 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
554 * The shutdown task handle
556 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
560 * Function called to notify a client about the connection begin ready to queue
561 * more data. "buf" will be NULL and "size" zero if the connection was closed
562 * for writing in the meantime.
565 * @param size number of bytes available in buf
566 * @param buf where the callee should write the message
567 * @return number of bytes written to buf
570 transmit_ready_notify (void *cls, size_t size, void *buf)
572 struct MessageQueue *mq_entry;
574 transmit_handle = NULL;
576 GNUNET_assert (NULL != mq_entry);
579 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
580 size = ntohs (mq_entry->msg->size);
581 memcpy (buf, mq_entry->msg, size);
582 GNUNET_free (mq_entry->msg);
583 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
584 GNUNET_free (mq_entry);
586 if (NULL != mq_entry)
588 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
589 ntohs (mq_entry->msg->size),
590 GNUNET_TIME_UNIT_FOREVER_REL,
591 &transmit_ready_notify, NULL);
597 * Queues a message in send queue for sending to the service
599 * @param client the client to whom the queued message has to be sent
600 * @param msg the message to queue
603 queue_message (struct GNUNET_SERVER_Client *client,
604 struct GNUNET_MessageHeader *msg)
606 struct MessageQueue *mq_entry;
610 type = ntohs (msg->type);
611 size = ntohs (msg->size);
612 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
613 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
614 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
616 mq_entry->client = client;
617 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
619 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
620 if (NULL == transmit_handle)
622 GNUNET_SERVER_notify_transmit_ready (client, size,
623 GNUNET_TIME_UNIT_FOREVER_REL,
624 &transmit_ready_notify, NULL);
629 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
631 * @param ptr the memory block to realloc
632 * @param size the size of ptr
633 * @param new_size the size to which ptr has to be realloc'ed
634 * @return the newly reallocated memory block
637 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
639 ptr = GNUNET_realloc (ptr, new_size);
641 ptr = memset (ptr + size, 0, new_size - size);
647 * Function to add a host to the current list of known hosts
649 * @param host the host to add
650 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
654 host_list_add (struct GNUNET_TESTBED_Host *host)
658 host_id = GNUNET_TESTBED_host_get_id_ (host);
659 if (host_list_size <= host_id)
662 TESTBED_realloc (host_list,
663 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
664 sizeof (struct GNUNET_TESTBED_Host *) *
665 (host_list_size + LIST_GROW_STEP));
666 host_list_size += LIST_GROW_STEP;
668 if (NULL != host_list[host_id])
670 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
671 return GNUNET_SYSERR;
673 host_list[host_id] = host;
679 * Adds a route to the route list
681 * @param route the route to add
684 route_list_add (struct Route *route)
686 if (route->dest >= route_list_size)
689 TESTBED_realloc (route_list, sizeof (struct Route *) * route_list_size,
690 sizeof (struct Route *) * (route_list_size +
692 route_list_size += LIST_GROW_STEP;
694 GNUNET_assert (NULL == route_list[route->dest]);
695 route_list[route->dest] = route;
700 * Adds a slave to the slave array
702 * @param slave the slave controller to add
705 slave_list_add (struct Slave *slave)
707 if (slave->host_id >= slave_list_size)
710 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
711 sizeof (struct Slave *) * (slave_list_size +
713 slave_list_size += LIST_GROW_STEP;
715 GNUNET_assert (NULL == slave_list[slave->host_id]);
716 slave_list[slave->host_id] = slave;
721 * Adds a peer to the peer array
723 * @param peer the peer to add
726 peer_list_add (struct Peer *peer)
730 orig_size = peer_list_size;
731 if (peer->id >= peer_list_size)
733 while (peer->id >= peer_list_size)
734 peer_list_size += LIST_GROW_STEP;
736 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
737 sizeof (struct Peer *) * peer_list_size);
739 GNUNET_assert (NULL == peer_list[peer->id]);
740 peer_list[peer->id] = peer;
745 * Removes a the give peer from the peer array
747 * @param peer the peer to be removed
750 peer_list_remove (struct Peer *peer)
755 peer_list[peer->id] = NULL;
756 orig_size = peer_list_size;
757 while (peer_list_size >= LIST_GROW_STEP)
759 for (id = peer_list_size - 1;
760 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
761 if (NULL != peer_list[id])
763 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
765 peer_list_size -= LIST_GROW_STEP;
767 if (orig_size == peer_list_size)
770 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
775 * Finds the route with directly connected host as destination through which
776 * the destination host can be reached
778 * @param host_id the id of the destination host
779 * @return the route with directly connected destination host; NULL if no route
782 static struct Route *
783 find_dest_route (uint32_t host_id)
787 while (NULL != (route = route_list[host_id]))
789 if (route->thru == master_context->host_id)
791 host_id = route->thru;
798 * Routes message to a host given its host_id
800 * @param host_id the id of the destination host
801 * @param msg the message to be routed
804 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
811 * Send operation failure message to client
813 * @param client the client to which the failure message has to be sent to
814 * @param operation_id the id of the failed operation
815 * @param emsg the error message; can be NULL
818 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
819 uint64_t operation_id, const char *emsg)
821 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
825 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
826 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
828 msg = GNUNET_malloc (msize);
829 msg->header.size = htons (msize);
830 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
831 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
832 msg->operation_id = GNUNET_htonll (operation_id);
834 memcpy (&msg[1], emsg, emsg_len);
835 queue_message (client, &msg->header);
840 * Function to send generic operation success message to given client
842 * @param client the client to send the message to
843 * @param operation_id the id of the operation which was successful
846 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
847 uint64_t operation_id)
849 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
852 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
853 msg = GNUNET_malloc (msize);
854 msg->header.size = htons (msize);
855 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
856 msg->operation_id = GNUNET_htonll (operation_id);
857 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
858 queue_message (client, &msg->header);
863 * The Link Controller forwarding task
865 * @param cls the LCFContext
866 * @param tc the Task context from scheduler
869 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
873 * Completion callback for host registrations while forwarding Link Controller messages
875 * @param cls the LCFContext
876 * @param emsg the error message; NULL if host registration is successful
879 lcf_proc_cc (void *cls, const char *emsg)
881 struct LCFContext *lcf = cls;
884 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
889 goto registration_error;
890 lcf->state = DELEGATED_HOST_REGISTERED;
891 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
893 case DELEGATED_HOST_REGISTERED:
895 goto registration_error;
896 lcf->state = SLAVE_HOST_REGISTERED;
897 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
900 GNUNET_assert (0); /* Shouldn't reach here */
905 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
907 lcf->state = FINISHED;
908 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
913 * Callback to be called when forwarded link controllers operation is
914 * successfull. We have to relay the reply msg back to the client
916 * @param cls ForwardedOperationContext
917 * @param msg the peer create success message
920 forwarded_operation_reply_relay (void *cls,
921 const struct GNUNET_MessageHeader *msg)
923 struct ForwardedOperationContext *fopc = cls;
924 struct GNUNET_MessageHeader *dup_msg;
927 msize = ntohs (msg->size);
928 dup_msg = GNUNET_malloc (msize);
929 (void) memcpy (dup_msg, msg, msize);
930 queue_message (fopc->client, dup_msg);
931 GNUNET_SERVER_client_drop (fopc->client);
932 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
938 * Task to free resources when forwarded link controllers has been timedout
940 * @param cls the ForwardedOperationContext
941 * @param tc the task context from scheduler
944 forwarded_operation_timeout (void *cls,
945 const struct GNUNET_SCHEDULER_TaskContext *tc)
947 struct ForwardedOperationContext *fopc = cls;
949 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
950 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
951 GNUNET_SERVER_client_drop (fopc->client);
957 * The Link Controller forwarding task
959 * @param cls the LCFContext
960 * @param tc the Task context from scheduler
963 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
965 struct LCFContext *lcf = cls;
966 struct LCFContextQueue *lcfq;
967 struct ForwardedOperationContext *fopc;
969 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
974 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
975 lcf->gateway->controller))
978 GNUNET_TESTBED_register_host (lcf->gateway->controller,
979 host_list[lcf->delegated_host_id],
984 lcf->state = DELEGATED_HOST_REGISTERED;
985 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
988 case DELEGATED_HOST_REGISTERED:
990 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
991 lcf->gateway->controller))
994 GNUNET_TESTBED_register_host (lcf->gateway->controller,
995 host_list[lcf->slave_host_id],
1000 lcf->state = SLAVE_HOST_REGISTERED;
1001 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1004 case SLAVE_HOST_REGISTERED:
1005 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1006 fopc->client = lcf->client;
1007 fopc->operation_id = lcf->operation_id;
1009 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1012 &forwarded_operation_reply_relay,
1014 fopc->timeout_task =
1015 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1017 lcf->state = FINISHED;
1018 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1022 GNUNET_assert (lcfq->lcf == lcf);
1023 GNUNET_free (lcf->msg);
1025 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1027 if (NULL != lcfq_head)
1029 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1035 * Callback for event from slave controllers
1037 * @param cls struct Slave *
1038 * @param event information about the event
1041 slave_event_callback (void *cls,
1042 const struct GNUNET_TESTBED_EventInformation *event)
1049 * Callback to signal successfull startup of the controller process
1051 * @param cls the closure from GNUNET_TESTBED_controller_start()
1052 * @param cfg the configuration with which the controller has been started;
1053 * NULL if status is not GNUNET_OK
1054 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1055 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1058 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1061 struct LinkControllersContext *lcc = cls;
1063 if (GNUNET_SYSERR == status)
1065 lcc->slave->controller_proc = NULL;
1066 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1067 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1070 lcc->slave->controller =
1071 GNUNET_TESTBED_controller_connect (cfg, host_list[lcc->slave->host_id],
1072 master_context->event_mask,
1073 &slave_event_callback, lcc->slave);
1074 if (NULL != lcc->slave->controller)
1075 send_operation_success_msg (lcc->client, lcc->operation_id);
1077 send_operation_fail_msg (lcc->client, lcc->operation_id,
1078 "Could not connect to delegated controller");
1079 GNUNET_SERVER_client_drop (lcc->client);
1085 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1088 * @param client identification of the client
1089 * @param message the actual message
1092 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1093 const struct GNUNET_MessageHeader *message)
1095 const struct GNUNET_TESTBED_InitMessage *msg;
1096 struct GNUNET_TESTBED_Host *host;
1097 const char *controller_hostname;
1100 if (NULL != master_context)
1103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1106 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1107 msize = ntohs (message->size);
1108 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1114 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1115 controller_hostname = (const char *) &msg[1];
1116 if ('\0' != controller_hostname[msize - 1])
1119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1122 master_context = GNUNET_malloc (sizeof (struct Context));
1123 master_context->client = client;
1124 master_context->host_id = ntohl (msg->host_id);
1125 master_context->master_ip = GNUNET_strdup (controller_hostname);
1126 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1127 master_context->system =
1128 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1130 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1132 host_list_add (host);
1133 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1134 GNUNET_SERVER_client_keep (client);
1135 LOG_DEBUG ("Created master context with host ID: %u\n",
1136 master_context->host_id);
1137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1142 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1145 * @param client identification of the client
1146 * @param message the actual message
1149 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1150 const struct GNUNET_MessageHeader *message)
1152 struct GNUNET_TESTBED_Host *host;
1153 const struct GNUNET_TESTBED_AddHostMessage *msg;
1154 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1159 uint16_t username_length;
1160 uint16_t hostname_length;
1161 uint16_t reply_size;
1164 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1165 msize = ntohs (msg->header.size);
1166 username = (char *) &(msg[1]);
1167 username_length = ntohs (msg->user_name_length);
1168 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1169 if (0 != username_length)
1170 GNUNET_assert ('\0' == username[username_length]);
1171 username_length = (0 == username_length) ? 0 : username_length + 1;
1172 hostname = username + username_length;
1174 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1175 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1176 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1177 host_id = ntohl (msg->host_id);
1178 LOG_DEBUG ("Received ADDHOST message\n");
1179 LOG_DEBUG ("-------host id: %u\n", host_id);
1180 if (NULL != hostname)
1181 LOG_DEBUG ("-------hostname: %s\n", hostname);
1182 if (0 != username_length)
1183 LOG_DEBUG ("-------username: %s\n", username);
1186 LOG_DEBUG ("-------username: NULL\n");
1189 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1191 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1192 ntohs (msg->ssh_port));
1193 GNUNET_assert (NULL != host);
1194 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1195 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1196 if (GNUNET_OK != host_list_add (host))
1198 /* We are unable to add a host */
1199 emsg = "A host exists with given host-id";
1200 LOG_DEBUG ("%s: %u", emsg, host_id);
1201 GNUNET_TESTBED_host_destroy (host);
1202 reply_size += strlen (emsg) + 1;
1203 reply = GNUNET_malloc (reply_size);
1204 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1207 reply = GNUNET_malloc (reply_size);
1208 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1209 reply->header.size = htons (reply_size);
1210 reply->host_id = htonl (host_id);
1211 queue_message (client, &reply->header);
1216 * Iterator over hash map entries.
1218 * @param cls closure
1219 * @param key current key code
1220 * @param value value in the hash map
1221 * @return GNUNET_YES if we should continue to
1226 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1228 struct SharedService *queried_ss = cls;
1229 struct SharedService *ss = value;
1231 if (0 == strcmp (ss->name, queried_ss->name))
1239 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1242 * @param client identification of the client
1243 * @param message the actual message
1246 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1247 const struct GNUNET_MessageHeader *message)
1249 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1250 struct SharedService *ss;
1252 struct GNUNET_HashCode hash;
1254 uint16_t service_name_size;
1256 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1257 msg_size = ntohs (message->size);
1258 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1265 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1266 service_name = (char *) &msg[1];
1267 if ('\0' != service_name[service_name_size - 1])
1270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1273 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1274 service_name, ntohl (msg->num_peers));
1275 if (ntohl (msg->host_id) != master_context->host_id)
1277 route_message (ntohl (msg->host_id), message);
1278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1281 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1282 ss = GNUNET_malloc (sizeof (struct SharedService));
1283 ss->name = strdup (service_name);
1284 ss->num_shared = ntohl (msg->num_peers);
1285 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1286 if (GNUNET_SYSERR ==
1287 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1288 &ss_exists_iterator, ss))
1290 LOG (GNUNET_ERROR_TYPE_WARNING,
1291 "Service %s already configured as a shared service. "
1292 "Ignoring service sharing request \n", ss->name);
1293 GNUNET_free (ss->name);
1297 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1298 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1303 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1306 * @param client identification of the client
1307 * @param message the actual message
1310 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1311 const struct GNUNET_MessageHeader *message)
1313 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1314 struct GNUNET_CONFIGURATION_Handle *cfg;
1315 struct LCFContextQueue *lcfq;
1316 struct Route *route;
1317 struct Route *new_route;
1321 uint32_t delegated_host_id;
1322 uint32_t slave_host_id;
1325 if (NULL == master_context)
1328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1331 msize = ntohs (message->size);
1332 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1335 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1338 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1339 delegated_host_id = ntohl (msg->delegated_host_id);
1340 if (delegated_host_id == master_context->host_id)
1343 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1347 if ((delegated_host_id >= host_list_size) ||
1348 (NULL == host_list[delegated_host_id]))
1350 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1354 slave_host_id = ntohl (msg->slave_host_id);
1355 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1357 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1361 if (slave_host_id == delegated_host_id)
1363 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1364 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1368 if (slave_host_id == master_context->host_id) /* Link from us */
1370 struct Slave *slave;
1371 struct LinkControllersContext *lcc;
1373 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1374 config_size = ntohs (msg->config_size);
1375 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1377 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1382 config = GNUNET_malloc (config_size);
1383 dest_size = (uLongf) config_size;
1385 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1388 GNUNET_break (0); /* Compression error */
1389 GNUNET_free (config);
1390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1393 if (config_size != dest_size)
1395 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1396 GNUNET_free (config);
1397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1400 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1402 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1404 GNUNET_break (0); /* Configuration parsing error */
1405 GNUNET_free (config);
1406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1409 GNUNET_free (config);
1410 if ((delegated_host_id < slave_list_size) &&
1411 (NULL != slave_list[delegated_host_id]))
1413 GNUNET_break (0); /* Configuration parsing error */
1414 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1417 slave = GNUNET_malloc (sizeof (struct Slave));
1418 slave->host_id = delegated_host_id;
1419 slave_list_add (slave);
1420 if (1 != msg->is_subordinate)
1423 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1424 master_context->event_mask,
1425 &slave_event_callback, slave);
1426 GNUNET_CONFIGURATION_destroy (cfg);
1427 if (NULL != slave->controller)
1428 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1430 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1431 "Could not connect to delegated controller");
1432 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1435 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1436 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1437 GNUNET_SERVER_client_keep (client);
1438 lcc->client = client;
1440 slave->controller_proc =
1441 GNUNET_TESTBED_controller_start (master_context->master_ip,
1442 host_list[slave->host_id], cfg,
1443 &slave_status_callback, lcc);
1444 GNUNET_CONFIGURATION_destroy (cfg);
1445 new_route = GNUNET_malloc (sizeof (struct Route));
1446 new_route->dest = delegated_host_id;
1447 new_route->thru = master_context->host_id;
1448 route_list_add (new_route);
1449 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1453 /* Route the request */
1454 if (slave_host_id >= route_list_size)
1456 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1460 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1461 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1462 lcfq->lcf->delegated_host_id = delegated_host_id;
1463 lcfq->lcf->slave_host_id = slave_host_id;
1464 route = find_dest_route (slave_host_id);
1465 GNUNET_assert (NULL != route); /* because we add routes carefully */
1466 GNUNET_assert (route->dest < slave_list_size);
1467 GNUNET_assert (NULL != slave_list[route->dest]);
1468 lcfq->lcf->state = INIT;
1469 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1470 lcfq->lcf->gateway = slave_list[route->dest];
1471 lcfq->lcf->msg = GNUNET_malloc (msize);
1472 (void) memcpy (lcfq->lcf->msg, msg, msize);
1473 GNUNET_SERVER_client_keep (client);
1474 lcfq->lcf->client = client;
1475 if (NULL == lcfq_head)
1477 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1478 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1479 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1482 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1483 /* FIXME: Adding a new route should happen after the controllers are linked
1485 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1486 new_route = GNUNET_malloc (sizeof (struct Route));
1487 new_route->dest = delegated_host_id;
1488 new_route->thru = route->dest;
1489 route_list_add (new_route);
1494 * The task to be executed if the forwarded peer create operation has been
1497 * @param cls the FowardedOperationContext
1498 * @param tc the TaskContext from the scheduler
1501 peer_create_forward_timeout (void *cls,
1502 const struct GNUNET_SCHEDULER_TaskContext *tc)
1504 struct ForwardedOperationContext *fo_ctxt = cls;
1506 /* send error msg to client */
1507 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1508 GNUNET_SERVER_client_drop (fo_ctxt->client);
1509 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1510 GNUNET_free (fo_ctxt);
1515 * Callback to be called when forwarded peer create operation is
1516 * successfull. We have to relay the reply msg back to the client
1518 * @param cls ForwardedOperationContext
1519 * @param msg the peer create success message
1522 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1524 struct ForwardedOperationContext *fo_ctxt = cls;
1525 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1526 struct GNUNET_MessageHeader *dup_msg;
1530 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1531 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1534 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1535 peer = GNUNET_malloc (sizeof (struct Peer));
1536 peer->is_remote = GNUNET_YES;
1537 peer->id = ntohl (success_msg->peer_id);
1538 GNUNET_assert (NULL != fo_ctxt->cls);
1539 peer->details.remote.controller = fo_ctxt->cls;
1540 peer_list_add (peer);
1542 msize = ntohs (msg->size);
1543 dup_msg = GNUNET_malloc (msize);
1544 (void) memcpy (dup_msg, msg, msize);
1545 queue_message (fo_ctxt->client, dup_msg);
1546 GNUNET_SERVER_client_drop (fo_ctxt->client);
1547 GNUNET_free (fo_ctxt);
1553 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1556 * @param client identification of the client
1557 * @param message the actual message
1560 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1561 const struct GNUNET_MessageHeader *message)
1563 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1564 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1565 struct GNUNET_CONFIGURATION_Handle *cfg;
1566 struct ForwardedOperationContext *fo_ctxt;
1567 struct Route *route;
1572 uint32_t config_size;
1578 msize = ntohs (message->size);
1579 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1581 GNUNET_break (0); /* We need configuration */
1582 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1585 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1586 host_id = ntohl (msg->host_id);
1587 peer_id = ntohl (msg->peer_id);
1588 if (UINT32_MAX == peer_id)
1590 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1591 "Cannot create peer with given ID");
1592 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1595 if (host_id == master_context->host_id)
1599 /* We are responsible for this peer */
1600 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1601 config_size = ntohl (msg->config_size);
1602 config = GNUNET_malloc (config_size);
1603 dest_size = config_size;
1606 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1607 (const Bytef *) &msg[1], (uLong) msize)))
1609 GNUNET_break (0); /* uncompression error */
1610 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1613 if (config_size != dest_size)
1615 GNUNET_break (0); /* Uncompressed config size mismatch */
1616 GNUNET_free (config);
1617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1620 cfg = GNUNET_CONFIGURATION_create ();
1622 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1624 GNUNET_break (0); /* Configuration parsing error */
1625 GNUNET_free (config);
1626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1629 GNUNET_free (config);
1630 peer = GNUNET_malloc (sizeof (struct Peer));
1631 peer->is_remote = GNUNET_NO;
1632 peer->details.local.cfg = cfg;
1634 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1635 peer->details.local.peer =
1636 GNUNET_TESTING_peer_configure (master_context->system,
1637 peer->details.local.cfg, peer->id,
1638 NULL /* Peer id */ ,
1640 if (NULL == peer->details.local.peer)
1642 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1646 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1649 peer_list_add (peer);
1651 GNUNET_malloc (sizeof
1652 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1653 reply->header.size =
1654 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1655 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1656 reply->peer_id = msg->peer_id;
1657 reply->operation_id = msg->operation_id;
1658 queue_message (client, &reply->header);
1659 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1663 /* Forward peer create request */
1664 route = find_dest_route (host_id);
1668 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1671 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1672 GNUNET_SERVER_client_keep (client);
1673 fo_ctxt->client = client;
1674 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1675 fo_ctxt->cls = slave_list[route->dest]->controller;
1677 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1678 [route->dest]->controller,
1679 fo_ctxt->operation_id,
1681 peer_create_success_cb, fo_ctxt);
1682 fo_ctxt->timeout_task =
1683 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1691 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1694 * @param client identification of the client
1695 * @param message the actual message
1698 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1699 const struct GNUNET_MessageHeader *message)
1701 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1702 struct ForwardedOperationContext *fopc;
1706 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1707 peer_id = ntohl (msg->peer_id);
1708 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1709 peer_id, GNUNET_ntohll (msg->operation_id));
1710 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1712 LOG (GNUNET_ERROR_TYPE_ERROR,
1713 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1714 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1715 "Peer doesn't exist");
1716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1719 peer = peer_list[peer_id];
1720 if (GNUNET_YES == peer->is_remote)
1722 /* Forward the destory message to sub controller */
1723 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1724 GNUNET_SERVER_client_keep (client);
1725 fopc->client = client;
1726 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1728 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1729 fopc->operation_id, &msg->header,
1730 &forwarded_operation_reply_relay,
1732 fopc->timeout_task =
1733 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1735 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1738 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1739 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1740 peer_list_remove (peer);
1742 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1743 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1748 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1751 * @param client identification of the client
1752 * @param message the actual message
1755 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1756 const struct GNUNET_MessageHeader *message)
1758 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1759 struct GNUNET_TESTBED_PeerEventMessage *reply;
1760 struct ForwardedOperationContext *fopc;
1764 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1765 peer_id = ntohl (msg->peer_id);
1766 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1769 LOG (GNUNET_ERROR_TYPE_ERROR,
1770 "Asked to start a non existent peer with id: %u\n", peer_id);
1771 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1774 peer = peer_list[peer_id];
1775 if (GNUNET_YES == peer->is_remote)
1777 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1778 GNUNET_SERVER_client_keep (client);
1779 fopc->client = client;
1780 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1782 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1783 fopc->operation_id, &msg->header,
1784 &forwarded_operation_reply_relay,
1786 fopc->timeout_task =
1787 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1789 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1792 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1794 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1799 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1800 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1801 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1802 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1803 reply->host_id = htonl (master_context->host_id);
1804 reply->peer_id = msg->peer_id;
1805 reply->operation_id = msg->operation_id;
1806 queue_message (client, &reply->header);
1807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1812 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1815 * @param client identification of the client
1816 * @param message the actual message
1819 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1820 const struct GNUNET_MessageHeader *message)
1822 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1823 struct GNUNET_TESTBED_PeerEventMessage *reply;
1824 struct ForwardedOperationContext *fopc;
1828 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1829 peer_id = ntohl (msg->peer_id);
1830 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1832 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1834 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1837 peer = peer_list[peer_id];
1838 if (GNUNET_YES == peer->is_remote)
1840 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1841 GNUNET_SERVER_client_keep (client);
1842 fopc->client = client;
1843 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1845 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1846 fopc->operation_id, &msg->header,
1847 &forwarded_operation_reply_relay,
1849 fopc->timeout_task =
1850 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1852 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1855 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1857 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1858 "Peer not running");
1859 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1862 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1863 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1864 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1865 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1866 reply->host_id = htonl (master_context->host_id);
1867 reply->peer_id = msg->peer_id;
1868 reply->operation_id = msg->operation_id;
1869 queue_message (client, &reply->header);
1870 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1875 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1878 * @param client identification of the client
1879 * @param message the actual message
1882 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1883 const struct GNUNET_MessageHeader *message)
1885 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1886 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1895 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1896 peer_id = ntohl (msg->peer_id);
1897 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1899 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1901 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1904 peer = peer_list[peer_id];
1905 if (GNUNET_YES == peer->is_remote)
1907 /* FIXME: forward to sub controller */
1909 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1913 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
1915 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1916 GNUNET_free (config);
1919 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1920 reply = GNUNET_realloc (xconfig, msize);
1921 (void) memmove (&reply[1], reply, xc_size);
1922 reply->header.size = htons (msize);
1923 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1924 reply->peer_id = msg->peer_id;
1925 reply->operation_id = msg->operation_id;
1926 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
1927 &reply->peer_identity);
1928 reply->config_size = htons ((uint16_t) c_size);
1929 queue_message (client, &reply->header);
1930 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1935 * Task for cleaing up overlay connect context structure
1937 * @param cls the overlay connect context
1938 * @param tc the task context
1941 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1943 struct OverlayConnectContext *occ = cls;
1945 GNUNET_free_non_null (occ->emsg);
1946 GNUNET_free_non_null (occ->hello);
1947 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1948 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1949 if (NULL != occ->ch)
1950 GNUNET_CORE_disconnect (occ->ch);
1951 if (NULL != occ->ghh)
1952 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1953 if (NULL != occ->p1th)
1954 GNUNET_TRANSPORT_disconnect (occ->p1th);
1955 if (NULL != occ->p2th)
1956 GNUNET_TRANSPORT_disconnect (occ->p2th);
1962 * Task which will be run when overlay connect request has been timed out
1964 * @param cls the OverlayConnectContext
1965 * @param tc the TaskContext
1968 timeout_overlay_connect (void *cls,
1969 const struct GNUNET_SCHEDULER_TaskContext *tc)
1971 struct OverlayConnectContext *occ = cls;
1973 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1974 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1975 GNUNET_SERVER_client_drop (occ->client);
1976 occ_cleanup (occ, tc);
1982 * Function called to notify transport users that another
1983 * peer connected to us.
1985 * @param cls closure
1986 * @param new_peer the peer that connected
1987 * @param ats performance data
1988 * @param ats_count number of entries in ats (excluding 0-termination)
1991 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
1992 const struct GNUNET_ATS_Information *ats,
1993 unsigned int ats_count)
1995 struct OverlayConnectContext *occ = cls;
1996 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1998 char *other_peer_str;
2000 LOG_DEBUG ("Overlay connect notify\n");
2002 memcmp (new_peer, &occ->peer_identity,
2003 sizeof (struct GNUNET_PeerIdentity)))
2005 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2006 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2008 memcmp (new_peer, &occ->other_peer_identity,
2009 sizeof (struct GNUNET_PeerIdentity)))
2011 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n", new_peer_str,
2013 GNUNET_free (new_peer_str);
2014 GNUNET_free (other_peer_str);
2017 GNUNET_free (new_peer_str);
2018 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2019 GNUNET_i2s (&occ->peer_identity));
2020 GNUNET_free (other_peer_str);
2021 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2023 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2024 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2026 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2027 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2028 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2029 GNUNET_free_non_null (occ->emsg);
2031 GNUNET_TRANSPORT_disconnect (occ->p2th);
2033 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2034 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2036 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2037 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2038 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2039 msg->peer1 = htonl (occ->peer->id);
2040 msg->peer2 = htonl (occ->other_peer->id);
2041 msg->operation_id = GNUNET_htonll (occ->op_id);
2042 queue_message (occ->client, &msg->header);
2043 GNUNET_SERVER_client_drop (occ->client);
2044 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2049 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2052 * @param cls the OverlayConnectContext
2053 * @param tc the TaskContext from scheduler
2056 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2058 struct OverlayConnectContext *occ = cls;
2059 char *other_peer_str;
2061 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2062 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2064 GNUNET_assert (NULL != occ->hello);
2065 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2066 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2067 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2068 GNUNET_free (other_peer_str);
2069 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2070 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2071 occ->send_hello_task =
2072 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &send_hello, occ);
2076 * Test for checking whether HELLO message is empty
2078 * @param cls empty flag to set
2079 * @param address the HELLO
2080 * @param expiration expiration of the HELLO
2084 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2085 struct GNUNET_TIME_Absolute expiration)
2095 * Function called whenever there is an update to the
2096 * HELLO of peers in the OverlayConnectClosure
2098 * @param cls closure
2099 * @param hello our updated HELLO
2102 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2104 struct OverlayConnectContext *occ = cls;
2108 msize = ntohs (hello->size);
2110 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2111 hello, GNUNET_NO, &test_address,
2113 if (GNUNET_YES == empty)
2115 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2118 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2119 occ->hello = GNUNET_malloc (msize);
2120 memcpy (occ->hello, hello, msize);
2121 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2123 GNUNET_TRANSPORT_disconnect (occ->p1th);
2125 GNUNET_free_non_null (occ->emsg);
2126 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2127 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2132 * Function called after GNUNET_CORE_connect has succeeded (or failed
2133 * for good). Note that the private key of the peer is intentionally
2134 * not exposed here; if you need it, your process should try to read
2135 * the private key file directly (which should work if you are
2138 * @param cls closure
2139 * @param server handle to the server, NULL if we failed
2140 * @param my_identity ID of this peer, NULL if we failed
2143 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2144 const struct GNUNET_PeerIdentity *my_identity)
2146 struct OverlayConnectContext *occ = cls;
2148 GNUNET_free_non_null (occ->emsg);
2149 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2150 if ((NULL == server) || (NULL == my_identity))
2152 GNUNET_free (occ->emsg);
2154 memcpy (&occ->peer_identity, my_identity,
2155 sizeof (struct GNUNET_PeerIdentity));
2157 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2158 &occ->peer_identity, NULL, NULL, NULL, NULL);
2159 /* Connect to the transport of 2nd peer to offer 1st peer's HELLO */
2160 GNUNET_TESTING_peer_get_identity (occ->other_peer->details.local.peer,
2161 &occ->other_peer_identity);
2163 GNUNET_TRANSPORT_connect (occ->other_peer->details.local.cfg,
2164 &occ->other_peer_identity, NULL, NULL, NULL,
2166 if ((NULL == occ->p1th) || (NULL == occ->p2th))
2168 occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
2171 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2172 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2173 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2177 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2178 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2183 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2186 * @param client identification of the client
2187 * @param message the actual message
2190 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2191 const struct GNUNET_MessageHeader *message)
2193 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2194 struct OverlayConnectContext *occ;
2195 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2201 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2202 p1 = ntohl (msg->peer1);
2203 p2 = ntohl (msg->peer2);
2204 GNUNET_assert (p1 < peer_list_size);
2205 GNUNET_assert (NULL != peer_list[p1]);
2206 GNUNET_assert (p2 < peer_list_size);
2207 GNUNET_assert (NULL != peer_list[p2]);
2208 /* FIXME: Add cases where we have to forward overlay connect message to sub
2210 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2211 GNUNET_SERVER_client_keep (client);
2212 occ->client = client;
2213 occ->peer = peer_list[p1];
2214 occ->other_peer = peer_list[p2];
2215 occ->op_id = GNUNET_ntohll (msg->operation_id);
2216 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2217 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2219 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2220 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2221 GNUNET_NO, no_handlers);
2222 if (NULL == occ->ch)
2224 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2227 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2228 (GNUNET_TIME_UNIT_SECONDS, 30),
2229 &timeout_overlay_connect, occ);
2230 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2235 * Iterator over hash map entries.
2237 * @param cls closure
2238 * @param key current key code
2239 * @param value value in the hash map
2240 * @return GNUNET_YES if we should continue to
2245 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2247 struct SharedService *ss = value;
2249 GNUNET_assert (GNUNET_YES ==
2250 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2251 GNUNET_free (ss->name);
2258 * Task to clean up and shutdown nicely
2261 * @param tc the TaskContext from scheduler
2264 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2266 struct LCFContextQueue *lcfq;
2269 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2270 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2271 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2273 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2274 if (NULL != lcfq_head)
2276 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2278 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2279 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2281 if (NULL != lcfq_head->lcf->rhandle)
2282 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2284 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2285 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2287 GNUNET_free (lcfq->lcf->msg);
2288 GNUNET_free (lcfq->lcf);
2289 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2292 /* Clear peer list */
2293 for (id = 0; id < peer_list_size; id++)
2294 if (NULL != peer_list[id])
2296 if (GNUNET_NO == peer_list[id]->is_remote)
2298 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2299 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2301 GNUNET_free (peer_list[id]);
2303 GNUNET_free_non_null (peer_list);
2304 /* Clear host list */
2305 for (id = 0; id < host_list_size; id++)
2306 if (NULL != host_list[id])
2307 GNUNET_TESTBED_host_destroy (host_list[id]);
2308 GNUNET_free_non_null (host_list);
2309 /* Clear route list */
2310 for (id = 0; id < route_list_size; id++)
2311 if (NULL != route_list[id])
2312 GNUNET_free (route_list[id]);
2313 GNUNET_free_non_null (route_list);
2314 /* Clear slave_list */
2315 for (id = 0; id < slave_list_size; id++)
2316 if (NULL != slave_list[id])
2318 if (NULL != slave_list[id]->controller)
2319 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2320 if (NULL != slave_list[id]->controller_proc)
2321 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2323 if (NULL != master_context)
2325 GNUNET_free_non_null (master_context->master_ip);
2326 if (NULL != master_context->system)
2327 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2328 GNUNET_free (master_context);
2329 master_context = NULL;
2335 * Callback for client disconnect
2338 * @param client the client which has disconnected
2341 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2343 if (NULL == master_context)
2345 if (client == master_context->client)
2347 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2348 GNUNET_SERVER_client_drop (client);
2349 /* should not be needed as we're terminated by failure to read
2350 * from stdin, but if stdin fails for some reason, this shouldn't
2351 * hurt for now --- might need to revise this later if we ever
2352 * decide that master connections might be temporarily down
2353 * for some reason */
2354 //GNUNET_SCHEDULER_shutdown ();
2362 * @param cls closure
2363 * @param server the initialized server
2364 * @param cfg configuration to use
2367 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2368 const struct GNUNET_CONFIGURATION_Handle *cfg)
2370 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2371 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2372 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2373 {&handle_configure_shared_service, NULL,
2374 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2375 {&handle_link_controllers, NULL,
2376 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2377 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2378 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2379 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2380 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2381 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2382 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2383 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2384 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2385 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2386 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2387 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2391 GNUNET_SERVER_add_handlers (server, message_handlers);
2392 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2393 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2395 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2396 &shutdown_task, NULL);
2397 LOG_DEBUG ("Testbed startup complete\n");
2402 * The starting point of execution
2405 main (int argc, char *const *argv)
2407 //sleep (15); /* Debugging */
2408 return (GNUNET_OK ==
2409 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2410 &testbed_run, NULL)) ? 0 : 1;
2413 /* end of gnunet-service-testbed.c */