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 #define LIST_GROW_STEP 10
59 * The client handle associated with this context
61 struct GNUNET_SERVER_Client *client;
64 * The network address of the master controller
69 * The TESTING system handle for starting peers locally
71 struct GNUNET_TESTING_System *system;
74 * Event mask of event to be responded in this context
79 * Our host id according to this context
86 * The message queue for sending messages to clients
91 * The message to be sent
93 struct GNUNET_MessageHeader *msg;
96 * The client to send the message to
98 struct GNUNET_SERVER_Client *client;
101 * next pointer for DLL
103 struct MessageQueue *next;
106 * prev pointer for DLL
108 struct MessageQueue *prev;
113 * The structure for identifying a shared service
118 * The name of the shared service
123 * Number of shared peers per instance of the shared service
128 * Number of peers currently sharing the service
130 uint32_t num_sharing;
145 * The host destination is reachable thru
152 * Structure representing a connected(directly-linked) controller
157 * The controller process handle if we had started the controller
159 struct GNUNET_TESTBED_ControllerProc *controller_proc;
162 * The controller handle
164 struct GNUNET_TESTBED_Controller *controller;
167 * The id of the host this controller is running on
174 * States of LCFContext
179 * The Context has been initialized; Nothing has been done on it
184 * Delegated host has been registered at the forwarding controller
186 DELEGATED_HOST_REGISTERED,
189 * The slave host has been registred at the forwarding controller
191 SLAVE_HOST_REGISTERED,
194 * The context has been finished (may have error)
202 * Link controllers request forwarding context
207 * The serialized and compressed configuration
212 * The gateway which will pass the link message to delegated host
214 struct Slave *gateway;
217 * The host registration handle while registered hosts in this context
219 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
222 * The size of the compressed serialized configuration
227 * The size of the uncompressed configuration
232 * Should the delegated host be started by the slave host?
237 * The state of this context
239 enum LCFContextState state;
244 uint32_t delegated_host_id;
249 uint32_t slave_host_id;
255 * Structure of a queue entry in LCFContext request queue
257 struct LCFContextQueue
262 struct LCFContext *lcf;
267 struct LCFContextQueue *next;
272 struct LCFContextQueue *prev;
277 * A locally started peer
282 * The peer handle from testing API
284 struct GNUNET_TESTING_Peer *peer;
287 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
290 struct GNUNET_CONFIGURATION_Handle *cfg;
293 * Our local reference id for this peer
301 * State information for overlay connect context
311 * Peer 1 has connected to peer0
313 OCC_STATE_PEER0_SUCCESS,
316 * Peer 2 has connected to peer1
318 OCC_STATE_PEER1_SUCCESS,
324 * Context information for connecting 2 peers in overlay
326 struct OverlayConnectContext
329 * The client which has requested for overlay connection
331 struct GNUNET_SERVER_Client *client;
334 * the peer which has to connect to the other peer
341 struct Peer *other_peer;
344 * Transport handle of the first peer to offer second peer's HELLO
346 struct GNUNET_TRANSPORT_Handle *p1th;
349 * Transport handle of other peer to get its HELLO
351 struct GNUNET_TRANSPORT_Handle *p2th;
354 * Core handles of the peer which has to connect to other peer
356 struct GNUNET_CORE_Handle *ch;
359 * HELLO of the other peer
361 struct GNUNET_MessageHeader *hello;
364 * Get hello handle for the other peer
366 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
369 * The peer identity of the first peer
371 struct GNUNET_PeerIdentity peer_identity;
374 * The peer identity of the other peer
376 struct GNUNET_PeerIdentity other_peer_identity;
379 * The id of the operation responsible for creating this context
384 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
387 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
390 * State information for determining whose HELLOs have been successfully
399 * The master context; generated with the first INIT message
401 static struct Context *master_context;
408 * Current Transmit Handle; NULL if no notify transmit exists currently
410 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
417 * The head for the LCF queue
419 static struct LCFContextQueue *lcfq_head;
422 * The tail for the LCF queue
424 static struct LCFContextQueue *lcfq_tail;
427 * The message queue head
429 static struct MessageQueue *mq_head;
432 * The message queue tail
434 static struct MessageQueue *mq_tail;
439 static struct GNUNET_TESTBED_Host **host_list;
444 static struct Route **route_list;
447 * A list of directly linked neighbours
449 static struct Slave **slave_list;
452 * A list of peers we own locally
454 static struct Peer **peer_list;
457 * The hashmap of shared services
459 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
462 * The size of the host list
464 static uint32_t host_list_size;
467 * The size of the route list
469 static uint32_t route_list_size;
472 * The size of directly linked neighbours list
474 static uint32_t slave_list_size;
477 * The size of the peer list
479 static uint32_t peer_list_size;
486 * The lcf_task handle
488 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
491 * The shutdown task handle
493 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
497 * Function called to notify a client about the connection begin ready to queue
498 * more data. "buf" will be NULL and "size" zero if the connection was closed
499 * for writing in the meantime.
502 * @param size number of bytes available in buf
503 * @param buf where the callee should write the message
504 * @return number of bytes written to buf
507 transmit_ready_notify (void *cls, size_t size, void *buf)
509 struct MessageQueue *mq_entry;
511 transmit_handle = NULL;
513 GNUNET_assert (NULL != mq_entry);
516 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
517 size = ntohs (mq_entry->msg->size);
518 memcpy (buf, mq_entry->msg, size);
519 GNUNET_free (mq_entry->msg);
520 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
521 GNUNET_free (mq_entry);
523 if (NULL != mq_entry)
525 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
526 ntohs (mq_entry->msg->size),
527 GNUNET_TIME_UNIT_FOREVER_REL,
528 &transmit_ready_notify, NULL);
534 * Queues a message in send queue for sending to the service
536 * @param client the client to whom the queued message has to be sent
537 * @param msg the message to queue
540 queue_message (struct GNUNET_SERVER_Client *client,
541 struct GNUNET_MessageHeader *msg)
543 struct MessageQueue *mq_entry;
547 type = ntohs (msg->type);
548 size = ntohs (msg->size);
549 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
550 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
551 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
553 mq_entry->client = client;
554 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
556 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
557 if (NULL == transmit_handle)
559 GNUNET_SERVER_notify_transmit_ready (client, size,
560 GNUNET_TIME_UNIT_FOREVER_REL,
561 &transmit_ready_notify, NULL);
566 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
568 * @param ptr the memory block to realloc
569 * @param size the size of ptr
570 * @param new_size the size to which ptr has to be realloc'ed
571 * @return the newly reallocated memory block
574 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
576 ptr = GNUNET_realloc (ptr, new_size);
578 ptr = memset (ptr + size, 0, new_size - size);
584 * Function to add a host to the current list of known hosts
586 * @param host the host to add
587 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
591 host_list_add (struct GNUNET_TESTBED_Host *host)
595 host_id = GNUNET_TESTBED_host_get_id_ (host);
596 if (host_list_size <= host_id)
599 TESTBED_realloc (host_list,
600 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
601 sizeof (struct GNUNET_TESTBED_Host *) *
602 (host_list_size + LIST_GROW_STEP));
603 host_list_size += LIST_GROW_STEP;
605 if (NULL != host_list[host_id])
607 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
608 return GNUNET_SYSERR;
610 host_list[host_id] = host;
616 * Adds a route to the route list
618 * @param route the route to add
621 route_list_add (struct Route *route)
623 if (route->dest >= route_list_size)
626 TESTBED_realloc (route_list,
627 sizeof (struct Route *) * route_list_size,
628 sizeof (struct Route *) *
629 (route_list_size + LIST_GROW_STEP));
630 route_list_size += LIST_GROW_STEP;
632 GNUNET_assert (NULL == route_list[route->dest]);
633 route_list[route->dest] = route;
638 * Adds a slave to the slave array
640 * @param slave the slave controller to add
643 slave_list_add (struct Slave *slave)
645 if (slave->host_id >= slave_list_size)
647 slave_list = TESTBED_realloc (slave_list,
648 sizeof (struct Slave *) *slave_list_size,
649 sizeof (struct Slave *) *
650 (slave_list_size + LIST_GROW_STEP));
651 slave_list_size += LIST_GROW_STEP;
653 GNUNET_assert (NULL == slave_list[slave->host_id]);
654 slave_list[slave->host_id] = slave;
659 * Adds a peer to the peer array
661 * @param peer the peer to add
664 peer_list_add (struct Peer *peer)
666 if (peer->id >= peer_list_size)
668 peer_list = TESTBED_realloc (peer_list,
669 sizeof (struct Peer *) * peer_list_size,
670 sizeof (struct Peer *) *
671 (peer_list_size + LIST_GROW_STEP));
672 peer_list_size += LIST_GROW_STEP;
674 GNUNET_assert (NULL == peer_list[peer->id]);
675 peer_list[peer->id] = peer;
680 * Routes message to a host given its host_id
682 * @param host_id the id of the destination host
683 * @param msg the message to be routed
686 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
693 * The Link Controller forwarding task
695 * @param cls the LCFContext
696 * @param tc the Task context from scheduler
699 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
703 * Completion callback for host registrations while forwarding Link Controller messages
705 * @param cls the LCFContext
706 * @param emsg the error message; NULL if host registration is successful
709 lcf_proc_cc (void *cls, const char *emsg)
711 struct LCFContext *lcf = cls;
714 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
719 goto registration_error;
720 lcf->state = DELEGATED_HOST_REGISTERED;
721 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
723 case DELEGATED_HOST_REGISTERED:
725 goto registration_error;
726 lcf->state = SLAVE_HOST_REGISTERED;
727 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
730 GNUNET_assert (0); /* Shouldn't reach here */
735 LOG (GNUNET_ERROR_TYPE_WARNING,
736 "Host registration failed with message: %s\n", emsg);
737 lcf->state = FINISHED;
738 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
743 * The Link Controller forwarding task
745 * @param cls the LCFContext
746 * @param tc the Task context from scheduler
749 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
751 struct LCFContext *lcf = cls;
752 struct LCFContextQueue *lcfq;
754 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
759 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
760 lcf->gateway->controller))
763 GNUNET_TESTBED_register_host (lcf->gateway->controller,
764 host_list[lcf->delegated_host_id],
769 lcf->state = DELEGATED_HOST_REGISTERED;
770 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
773 case DELEGATED_HOST_REGISTERED:
775 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
776 lcf->gateway->controller))
779 GNUNET_TESTBED_register_host (lcf->gateway->controller,
780 host_list[lcf->slave_host_id],
785 lcf->state = SLAVE_HOST_REGISTERED;
786 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
789 case SLAVE_HOST_REGISTERED:
790 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
791 host_list[lcf->delegated_host_id],
792 host_list[lcf->slave_host_id],
793 lcf->sxcfg, lcf->sxcfg_size,
795 lcf->is_subordinate);
796 lcf->state = FINISHED;
797 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
801 GNUNET_assert (lcfq->lcf == lcf);
802 GNUNET_free (lcf->sxcfg);
804 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
806 if (NULL != lcfq_head)
808 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
814 * Callback for event from slave controllers
816 * @param cls struct Slave *
817 * @param event information about the event
820 slave_event_callback(void *cls,
821 const struct GNUNET_TESTBED_EventInformation *event)
828 * Callback to signal successfull startup of the controller process
830 * @param cls the closure from GNUNET_TESTBED_controller_start()
831 * @param cfg the configuration with which the controller has been started;
832 * NULL if status is not GNUNET_OK
833 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
834 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
837 slave_status_callback (void *cls,
838 const struct GNUNET_CONFIGURATION_Handle *cfg,
841 struct Slave *slave = cls;
843 if (GNUNET_SYSERR == status)
845 slave->controller_proc = NULL;
846 LOG (GNUNET_ERROR_TYPE_WARNING,
847 "Unexpected slave shutdown\n");
848 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
852 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
853 master_context->event_mask,
854 &slave_event_callback, slave);
859 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
862 * @param client identification of the client
863 * @param message the actual message
866 handle_init (void *cls,
867 struct GNUNET_SERVER_Client *client,
868 const struct GNUNET_MessageHeader *message)
870 const struct GNUNET_TESTBED_InitMessage *msg;
871 struct GNUNET_TESTBED_Host *host;
875 if (NULL != master_context)
878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
881 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
882 master_context = GNUNET_malloc (sizeof (struct Context));
883 master_context->client = client;
884 master_context->host_id = ntohl (msg->host_id);
885 GNUNET_assert (GNUNET_OK ==
886 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
887 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
888 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
889 NULL, 0, NI_NUMERICHOST))
891 LOG (GNUNET_ERROR_TYPE_WARNING,
892 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
894 GNUNET_free (master_context->master_ip);
898 if (0 == strcasecmp (master_context->master_ip, "localhost"))
899 { /* Hack for connections via unix sockets */
900 LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
901 GNUNET_free (master_context->master_ip);
902 master_context->master_ip = strdup ("127.0.0.1");
904 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
905 master_context->system =
906 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
907 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
909 host_list_add (host);
910 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
911 GNUNET_SERVER_client_keep (client);
912 LOG_DEBUG ("Created master context with host ID: %u\n",
913 master_context->host_id);
914 GNUNET_SERVER_receive_done (client, GNUNET_OK);
919 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
922 * @param client identification of the client
923 * @param message the actual message
926 handle_add_host (void *cls,
927 struct GNUNET_SERVER_Client *client,
928 const struct GNUNET_MessageHeader *message)
930 struct GNUNET_TESTBED_Host *host;
931 const struct GNUNET_TESTBED_AddHostMessage *msg;
932 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
937 uint16_t username_length;
938 uint16_t hostname_length;
942 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
943 msize = ntohs (msg->header.size);
944 username = (char *) &(msg[1]);
945 username_length = ntohs (msg->user_name_length);
946 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
947 + username_length + 1)); /* msg must contain hostname */
948 if (0 != username_length)
949 GNUNET_assert ('\0' == username[username_length]);
950 username_length = (0 == username_length) ? 0 : username_length + 1;
951 hostname = username + username_length;
952 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
954 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
955 GNUNET_assert (strlen (hostname) == hostname_length - 1);
956 host_id = ntohl (msg->host_id);
957 LOG_DEBUG ("Received ADDHOST message\n");
958 LOG_DEBUG ("-------host id: %u\n", host_id);
959 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
960 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
961 else LOG_DEBUG ("-------username: NULL\n");
962 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
963 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
964 ntohs (msg->ssh_port));
965 GNUNET_SERVER_receive_done (client, GNUNET_OK);
966 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
967 if (GNUNET_OK != host_list_add (host))
969 /* We are unable to add a host */
970 emsg = "A host exists with given host-id";
971 LOG_DEBUG ("%s: %u", emsg, host_id);
972 GNUNET_TESTBED_host_destroy (host);
973 reply_size += strlen (emsg) + 1;
974 reply = GNUNET_malloc (reply_size);
975 memcpy (&reply[1], emsg, strlen (emsg) + 1);
978 reply = GNUNET_malloc (reply_size);
979 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
980 reply->header.size = htons (reply_size);
981 reply->host_id = htonl (host_id);
982 queue_message (client, (struct GNUNET_MessageHeader *) reply);
987 * Iterator over hash map entries.
990 * @param key current key code
991 * @param value value in the hash map
992 * @return GNUNET_YES if we should continue to
996 int ss_exists_iterator (void *cls,
997 const struct GNUNET_HashCode * key,
1000 struct SharedService *queried_ss = cls;
1001 struct SharedService *ss = value;
1003 if (0 == strcmp (ss->name, queried_ss->name))
1011 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1014 * @param client identification of the client
1015 * @param message the actual message
1018 handle_configure_shared_service (void *cls,
1019 struct GNUNET_SERVER_Client *client,
1020 const struct GNUNET_MessageHeader *message)
1022 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1023 struct SharedService *ss;
1025 struct GNUNET_HashCode hash;
1027 uint16_t service_name_size;
1029 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1030 msg_size = ntohs (message->size);
1031 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1037 service_name_size = msg_size -
1038 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1039 service_name = (char *) &msg[1];
1040 if ('\0' != service_name[service_name_size - 1])
1043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1046 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1047 service_name, ntohl (msg->num_peers));
1048 if (ntohl (msg->host_id) != master_context->host_id)
1050 route_message (ntohl (msg->host_id), message);
1051 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1055 ss = GNUNET_malloc (sizeof (struct SharedService));
1056 ss->name = strdup (service_name);
1057 ss->num_shared = ntohl (msg->num_peers);
1058 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1059 if (GNUNET_SYSERR ==
1060 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1061 &ss_exists_iterator, ss))
1063 LOG (GNUNET_ERROR_TYPE_WARNING,
1064 "Service %s already configured as a shared service. "
1065 "Ignoring service sharing request \n", ss->name);
1066 GNUNET_free (ss->name);
1070 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1071 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1076 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1079 * @param client identification of the client
1080 * @param message the actual message
1083 handle_link_controllers (void *cls,
1084 struct GNUNET_SERVER_Client *client,
1085 const struct GNUNET_MessageHeader *message)
1087 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1088 struct GNUNET_CONFIGURATION_Handle *cfg;
1089 struct LCFContextQueue *lcfq;
1090 struct Route *route;
1091 struct Route *new_route;
1095 uint32_t delegated_host_id;
1096 uint32_t slave_host_id;
1099 if (NULL == master_context)
1102 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1105 msize = ntohs (message->size);
1106 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1112 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1113 delegated_host_id = ntohl (msg->delegated_host_id);
1114 if (delegated_host_id == master_context->host_id)
1117 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1121 if ((delegated_host_id >= host_list_size) ||
1122 (NULL == host_list[delegated_host_id]))
1124 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1125 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1128 slave_host_id = ntohl (msg->slave_host_id);
1129 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1131 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1135 if (slave_host_id == delegated_host_id)
1137 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1141 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1142 config_size = ntohs (msg->config_size);
1144 if (slave_host_id == master_context->host_id) /* Link from us */
1146 struct Slave *slave;
1148 if ((delegated_host_id < slave_list_size) &&
1149 (NULL != slave_list[delegated_host_id])) /* We have already added */
1151 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1156 config = GNUNET_malloc (config_size);
1157 dest_size = (uLongf) config_size;
1158 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1159 (const Bytef *) &msg[1], (uLong) msize))
1161 GNUNET_break (0); /* Compression error */
1162 GNUNET_free (config);
1163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1166 if (config_size == dest_size)
1168 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1169 GNUNET_free (config);
1170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1173 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1174 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1177 GNUNET_break (0); /* Configuration parsing error */
1178 GNUNET_free (config);
1179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1182 GNUNET_free (config);
1183 if ((delegated_host_id < slave_list_size) &&
1184 (NULL != slave_list[delegated_host_id]))
1186 GNUNET_break (0); /* Configuration parsing error */
1187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1190 slave = GNUNET_malloc (sizeof (struct Slave));
1191 slave->host_id = delegated_host_id;
1192 slave_list_add (slave);
1193 if (1 == msg->is_subordinate)
1195 slave->controller_proc =
1196 GNUNET_TESTBED_controller_start (master_context->master_ip,
1197 host_list[slave->host_id],
1198 cfg, &slave_status_callback,
1203 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1204 master_context->event_mask,
1205 &slave_event_callback, slave);
1207 GNUNET_CONFIGURATION_destroy (cfg);
1208 new_route = GNUNET_malloc (sizeof (struct Route));
1209 new_route->dest = delegated_host_id;
1210 new_route->thru = master_context->host_id;
1211 route_list_add (new_route);
1212 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1216 /* Route the request */
1217 if (slave_host_id >= route_list_size)
1219 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1223 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1224 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1225 lcfq->lcf->delegated_host_id = delegated_host_id;
1226 lcfq->lcf->slave_host_id = slave_host_id;
1227 while (NULL != (route = route_list[slave_host_id]))
1229 if (route->thru == master_context->host_id)
1231 slave_host_id = route->thru;
1233 GNUNET_assert (NULL != route); /* because we add routes carefully */
1234 GNUNET_assert (route->dest < slave_list_size);
1235 GNUNET_assert (NULL != slave_list[route->dest]);
1236 lcfq->lcf->is_subordinate =
1237 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1238 lcfq->lcf->state = INIT;
1239 lcfq->lcf->gateway = slave_list[route->dest];
1240 lcfq->lcf->sxcfg_size = msize;
1241 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1242 lcfq->lcf->scfg_size = config_size;
1243 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1244 if (NULL == lcfq_head)
1246 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1247 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1248 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1251 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1252 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1253 new_route = GNUNET_malloc (sizeof (struct Route));
1254 new_route->dest = delegated_host_id;
1255 new_route->thru = route->dest;
1256 route_list_add (new_route);
1261 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1264 * @param client identification of the client
1265 * @param message the actual message
1268 handle_peer_create (void *cls,
1269 struct GNUNET_SERVER_Client *client,
1270 const struct GNUNET_MessageHeader *message)
1272 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1273 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1274 struct GNUNET_CONFIGURATION_Handle *cfg;
1278 uint32_t config_size;
1282 msize = ntohs (message->size);
1283 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1285 GNUNET_break (0); /* We need configuration */
1286 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1289 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1290 if (ntohl (msg->host_id) == master_context->host_id)
1295 /* We are responsible for this peer */
1296 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1297 config_size = ntohl (msg->config_size);
1298 config = GNUNET_malloc (config_size);
1299 dest_size = config_size;
1300 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1301 (const Bytef *) &msg[1], (uLong) msize)))
1303 GNUNET_break (0); /* uncompression error */
1304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1307 if (config_size != dest_size)
1309 GNUNET_break (0);/* Uncompressed config size mismatch */
1310 GNUNET_free (config);
1311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1314 cfg = GNUNET_CONFIGURATION_create ();
1315 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1318 GNUNET_break (0); /* Configuration parsing error */
1319 GNUNET_free (config);
1320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1323 GNUNET_free (config);
1324 peer = GNUNET_malloc (sizeof (struct Peer));
1326 peer->id = ntohl (msg->peer_id);
1327 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1328 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1332 if (NULL == peer->peer)
1334 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1341 peer_list_add (peer);
1342 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1343 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1344 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1345 reply->peer_id = msg->peer_id;
1346 reply->operation_id = msg->operation_id;
1347 queue_message (client, &reply->header);
1348 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1352 /* FIXME: Forward the peer to other host */
1354 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1359 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1362 * @param client identification of the client
1363 * @param message the actual message
1366 handle_peer_destroy (void *cls,
1367 struct GNUNET_SERVER_Client *client,
1368 const struct GNUNET_MessageHeader *message)
1370 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1371 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1374 uint16_t reply_size;
1376 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1377 peer_id = ntohl (msg->peer_id);
1378 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1379 peer_id, GNUNET_ntohll (msg->operation_id));
1380 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1383 /* FIXME: Reply with failure event message or forward to slave controller */
1384 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1387 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1388 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1389 GNUNET_free (peer_list[peer_id]);
1390 peer_list[peer_id] = NULL;
1391 for (id = 0; id < LIST_GROW_STEP; id++)
1393 if (((peer_id + id >= peer_list_size) ||
1394 (NULL != peer_list[peer_id])))
1397 if (LIST_GROW_STEP == id)
1399 peer_list_size -= LIST_GROW_STEP;
1400 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1403 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1404 reply = GNUNET_malloc (reply_size);
1405 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1406 reply->header.size = htons (reply_size);
1407 reply->operation_id = msg->operation_id;
1408 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1409 queue_message (client, &reply->header);
1410 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1415 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1418 * @param client identification of the client
1419 * @param message the actual message
1422 handle_peer_start (void *cls,
1423 struct GNUNET_SERVER_Client *client,
1424 const struct GNUNET_MessageHeader *message)
1426 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1427 struct GNUNET_TESTBED_PeerEventMessage *reply;
1430 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1431 peer_id = ntohl (msg->peer_id);
1432 if ((peer_id >= peer_list_size)
1433 || (NULL == peer_list[peer_id]))
1436 /* FIXME: reply with failure message or forward to slave controller */
1437 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1440 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1442 /* FIXME: return FAILURE message */
1444 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1447 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1448 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1449 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1450 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1451 reply->host_id = htonl (master_context->host_id);
1452 reply->peer_id = msg->peer_id;
1453 reply->operation_id = msg->operation_id;
1454 queue_message (client, &reply->header);
1455 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1460 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1463 * @param client identification of the client
1464 * @param message the actual message
1467 handle_peer_stop (void *cls,
1468 struct GNUNET_SERVER_Client *client,
1469 const struct GNUNET_MessageHeader *message)
1471 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1472 struct GNUNET_TESTBED_PeerEventMessage *reply;
1475 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1476 peer_id = ntohl (msg->peer_id);
1477 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1479 GNUNET_break (0); /* FIXME: route to slave? */
1480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1483 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1485 /* FIXME: return FAILURE message */
1487 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1490 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1491 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1492 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1493 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1494 reply->host_id = htonl (master_context->host_id);
1495 reply->peer_id = msg->peer_id;
1496 reply->operation_id = msg->operation_id;
1497 queue_message (client, &reply->header);
1498 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1503 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1506 * @param client identification of the client
1507 * @param message the actual message
1510 handle_peer_get_config (void *cls,
1511 struct GNUNET_SERVER_Client *client,
1512 const struct GNUNET_MessageHeader *message)
1514 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1515 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1523 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1524 peer_id = ntohl (msg->peer_id);
1525 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1527 /* FIXME: return FAILURE message */
1529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1531 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1533 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1534 GNUNET_free (config);
1535 msize = xc_size + sizeof (struct
1536 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1537 reply = GNUNET_realloc (xconfig, msize);
1538 (void) memmove (&reply[1], reply, xc_size);
1539 reply->header.size = htons (msize);
1540 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1541 reply->peer_id = msg->peer_id;
1542 reply->operation_id = msg->operation_id;
1543 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1544 &reply->peer_identity);
1545 reply->config_size = htons ((uint16_t) c_size);
1546 queue_message (client, &reply->header);
1547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1552 * Task for cleaing up overlay connect context structure
1554 * @param cls the overlay connect context
1555 * @param tc the task context
1558 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1560 struct OverlayConnectContext *occ = cls;
1562 if (NULL != occ->hello)
1563 GNUNET_free (occ->hello);
1564 if (NULL != occ->ch)
1565 GNUNET_CORE_disconnect (occ->ch);
1566 if (NULL != occ->ghh)
1567 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1568 if (NULL != occ->p1th)
1569 GNUNET_TRANSPORT_disconnect (occ->p1th);
1570 if (NULL != occ->p2th)
1571 GNUNET_TRANSPORT_disconnect (occ->p2th);
1577 * Function called to notify transport users that another
1578 * peer connected to us.
1580 * @param cls closure
1581 * @param new_peer the peer that connected
1582 * @param ats performance data
1583 * @param ats_count number of entries in ats (excluding 0-termination)
1586 overlay_connect_notify (void *cls,
1587 const struct GNUNET_PeerIdentity * new_peer,
1588 const struct GNUNET_ATS_Information * ats,
1589 unsigned int ats_count)
1591 struct OverlayConnectContext *occ = cls;
1592 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1594 char *other_peer_str;
1596 LOG_DEBUG ("Overlay connect notify\n");
1597 if (0 == memcmp (new_peer, &occ->peer_identity,
1598 sizeof (struct GNUNET_PeerIdentity)))
1600 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1601 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1602 if (0 != memcmp (new_peer, &occ->other_peer_identity,
1603 sizeof (struct GNUNET_PeerIdentity)))
1605 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1606 new_peer_str, other_peer_str);
1607 GNUNET_free (new_peer_str);
1608 GNUNET_free (other_peer_str);
1611 LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1612 GNUNET_free (new_peer_str);
1613 GNUNET_free (other_peer_str);
1614 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1616 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1617 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1619 GNUNET_TRANSPORT_disconnect (occ->p1th);
1621 /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
1622 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1623 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1624 msg->header.size = htons (sizeof (struct
1625 GNUNET_TESTBED_ConnectionEventMessage));
1626 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1627 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1628 msg->peer1 = htonl (occ->peer->id);
1629 msg->peer2 = htonl (occ->other_peer->id);
1630 msg->operation_id = GNUNET_htonll (occ->op_id);
1631 queue_message (occ->client, &msg->header);
1632 GNUNET_SERVER_client_drop (occ->client);
1633 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
1638 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1640 struct OverlayConnectContext *occ = cls;
1641 char *other_peer_str;
1643 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1644 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1646 GNUNET_assert (NULL != occ->hello);
1647 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1648 LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
1649 GNUNET_free (other_peer_str);
1650 GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
1651 GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
1652 occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1657 * Test for checking whether HELLO message is empty
1659 * @param cls empty flag to set
1660 * @param address the HELLO
1661 * @param expiration expiration of the HELLO
1665 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
1666 struct GNUNET_TIME_Absolute expiration)
1676 * Function called whenever there is an update to the
1677 * HELLO of peers in the OverlayConnectClosure
1679 * @param cls closure
1680 * @param hello our updated HELLO
1683 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1685 struct OverlayConnectContext *occ = cls;
1689 msize = ntohs (hello->size);
1692 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1693 "HELLO message of peer %s is of size 0\n",
1694 &occ->other_peer_identity);
1699 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
1700 GNUNET_NO, &test_address, &empty);
1701 if (GNUNET_YES == empty)
1703 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
1706 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
1707 occ->hello = GNUNET_malloc (msize);
1708 memcpy (occ->hello, hello, msize);
1709 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1711 GNUNET_TRANSPORT_disconnect (occ->p2th);
1713 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1718 * Function called after GNUNET_CORE_connect has succeeded (or failed
1719 * for good). Note that the private key of the peer is intentionally
1720 * not exposed here; if you need it, your process should try to read
1721 * the private key file directly (which should work if you are
1724 * @param cls closure
1725 * @param server handle to the server, NULL if we failed
1726 * @param my_identity ID of this peer, NULL if we failed
1729 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
1730 const struct GNUNET_PeerIdentity *my_identity)
1732 struct OverlayConnectContext *occ = cls;
1734 memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
1736 GNUNET_TRANSPORT_connect (occ->peer->cfg, &occ->peer_identity, NULL, NULL,
1738 /* Connect to the transport of 2nd peer and get its HELLO message */
1739 GNUNET_TESTING_peer_get_identity (occ->other_peer->peer,
1740 &occ->other_peer_identity);
1742 GNUNET_TRANSPORT_connect (occ->other_peer->cfg, &occ->other_peer_identity,
1743 NULL, NULL, NULL, NULL);
1744 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->other_peer_identity));
1745 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
1750 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1753 * @param client identification of the client
1754 * @param message the actual message
1757 handle_overlay_connect (void *cls,
1758 struct GNUNET_SERVER_Client *client,
1759 const struct GNUNET_MessageHeader *message)
1761 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1762 struct OverlayConnectContext *occ;
1763 struct GNUNET_CORE_MessageHandler no_handlers[] = {
1769 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1770 p1 = ntohl (msg->peer1);
1771 p2 = ntohl (msg->peer2);
1772 GNUNET_assert (p1 < peer_list_size);
1773 GNUNET_assert (NULL != peer_list[p1]);
1774 GNUNET_assert (p2 < peer_list_size);
1775 GNUNET_assert (NULL != peer_list[p2]);
1776 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1777 GNUNET_SERVER_client_keep (client);
1778 occ->client = client;
1779 occ->state = OCC_STATE_INIT;
1780 occ->peer = peer_list[p1];
1781 occ->other_peer = peer_list[p2];
1782 occ->op_id = GNUNET_ntohll (msg->operation_id);
1783 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
1785 GNUNET_CORE_connect (occ->peer->cfg, occ, &core_startup_cb,
1786 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
1787 GNUNET_NO, no_handlers);
1788 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1793 * Iterator over hash map entries.
1795 * @param cls closure
1796 * @param key current key code
1797 * @param value value in the hash map
1798 * @return GNUNET_YES if we should continue to
1803 ss_map_free_iterator (void *cls,
1804 const struct GNUNET_HashCode * key, void *value)
1806 struct SharedService *ss = value;
1808 GNUNET_assert (GNUNET_YES ==
1809 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1810 GNUNET_free (ss->name);
1817 * Task to clean up and shutdown nicely
1820 * @param tc the TaskContext from scheduler
1823 shutdown_task (void *cls,
1824 const struct GNUNET_SCHEDULER_TaskContext *tc)
1826 struct LCFContextQueue *lcfq;
1829 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1830 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1831 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1833 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1834 if (NULL != lcfq_head)
1836 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1838 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1839 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1841 if (NULL != lcfq_head->lcf->rhandle)
1842 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1844 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1845 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1847 GNUNET_free (lcfq->lcf->sxcfg);
1848 GNUNET_free (lcfq->lcf);
1849 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1852 /* Clear peer list */
1853 for (id = 0; id < peer_list_size; id++)
1854 if (NULL != peer_list[id])
1856 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1857 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1858 GNUNET_free (peer_list[id]);
1860 GNUNET_free_non_null (peer_list);
1861 /* Clear host list */
1862 for (id = 0; id < host_list_size; id++)
1863 if (NULL != host_list[id])
1864 GNUNET_TESTBED_host_destroy (host_list[id]);
1865 GNUNET_free_non_null (host_list);
1866 /* Clear route list */
1867 for (id = 0; id < route_list_size; id++)
1868 if (NULL != route_list[id])
1869 GNUNET_free (route_list[id]);
1870 GNUNET_free_non_null (route_list);
1871 /* Clear slave_list */
1872 for (id = 0; id < slave_list_size; id++)
1873 if (NULL != slave_list[id])
1875 GNUNET_assert (NULL != slave_list[id]->controller);
1876 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1877 if (NULL != slave_list[id]->controller_proc)
1878 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1880 if (NULL != master_context)
1882 GNUNET_free_non_null (master_context->master_ip);
1883 if (NULL != master_context->system)
1884 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1885 GNUNET_free (master_context);
1886 master_context = NULL;
1892 * Callback for client disconnect
1895 * @param client the client which has disconnected
1898 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1900 if (NULL == master_context)
1902 if (client == master_context->client)
1904 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1905 GNUNET_SERVER_client_drop (client);
1906 /* should not be needed as we're terminated by failure to read
1907 from stdin, but if stdin fails for some reason, this shouldn't
1908 hurt for now --- might need to revise this later if we ever
1909 decide that master connections might be temporarily down
1911 //GNUNET_SCHEDULER_shutdown ();
1919 * @param cls closure
1920 * @param server the initialized server
1921 * @param cfg configuration to use
1924 testbed_run (void *cls,
1925 struct GNUNET_SERVER_Handle *server,
1926 const struct GNUNET_CONFIGURATION_Handle *cfg)
1928 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1930 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1931 sizeof (struct GNUNET_TESTBED_InitMessage)},
1932 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1933 {&handle_configure_shared_service, NULL,
1934 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1935 {&handle_link_controllers, NULL,
1936 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1937 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1938 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1939 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1940 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1941 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1942 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1943 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1944 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
1945 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1946 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
1947 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
1951 GNUNET_SERVER_add_handlers (server,
1953 GNUNET_SERVER_disconnect_notify (server,
1954 &client_disconnect_cb,
1956 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1958 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1961 LOG_DEBUG ("Testbed startup complete\n");
1966 * The starting point of execution
1968 int main (int argc, char *const *argv)
1970 //sleep (30); /* Debugging */
1973 GNUNET_SERVICE_run (argc,
1976 GNUNET_SERVICE_OPTION_NONE,