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"
34 #include "gnunet_testbed_service.h"
36 #include "testbed_api.h"
37 #include "testbed_api_hosts.h"
38 #include "gnunet_testing_lib-new.h"
43 #define LOG(kind,...) \
44 GNUNET_log (kind, __VA_ARGS__)
49 #define LOG_DEBUG(...) \
50 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
53 #define LIST_GROW_STEP 10
58 * The client handle associated with this context
60 struct GNUNET_SERVER_Client *client;
63 * The network address of the master controller
68 * The TESTING system handle for starting peers locally
70 struct GNUNET_TESTING_System *system;
73 * Event mask of event to be responded in this context
78 * Our host id according to this context
85 * The message queue for sending messages to clients
90 * The message to be sent
92 struct GNUNET_MessageHeader *msg;
95 * The client to send the message to
97 struct GNUNET_SERVER_Client *client;
100 * next pointer for DLL
102 struct MessageQueue *next;
105 * prev pointer for DLL
107 struct MessageQueue *prev;
112 * The structure for identifying a shared service
117 * The name of the shared service
122 * Number of shared peers per instance of the shared service
127 * Number of peers currently sharing the service
129 uint32_t num_sharing;
144 * The host destination is reachable thru
151 * Structure representing a connected(directly-linked) controller
156 * The controller process handle if we had started the controller
158 struct GNUNET_TESTBED_ControllerProc *controller_proc;
161 * The controller handle
163 struct GNUNET_TESTBED_Controller *controller;
166 * The id of the host this controller is running on
173 * States of LCFContext
178 * The Context has been initialized; Nothing has been done on it
183 * Delegated host has been registered at the forwarding controller
185 DELEGATED_HOST_REGISTERED,
188 * The slave host has been registred at the forwarding controller
190 SLAVE_HOST_REGISTERED,
193 * The context has been finished (may have error)
201 * Link controllers request forwarding context
206 * The serialized and compressed configuration
211 * The gateway which will pass the link message to delegated host
213 struct Slave *gateway;
216 * The host registration handle while registered hosts in this context
218 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
221 * The size of the compressed serialized configuration
226 * The size of the uncompressed configuration
231 * Should the delegated host be started by the slave host?
236 * The state of this context
238 enum LCFContextState state;
243 uint32_t delegated_host_id;
248 uint32_t slave_host_id;
254 * Structure of a queue entry in LCFContext request queue
256 struct LCFContextQueue
261 struct LCFContext *lcf;
266 struct LCFContextQueue *next;
271 struct LCFContextQueue *prev;
276 * A locally started peer
281 * The peer handle from testing API
283 struct GNUNET_TESTING_Peer *peer;
286 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
289 struct GNUNET_CONFIGURATION_Handle *cfg;
292 * Our local reference id for this peer
300 * State information for overlay connect context
310 * Peer 1 has connected to peer0
312 OCC_STATE_PEER0_SUCCESS,
315 * Peer 2 has connected to peer1
317 OCC_STATE_PEER1_SUCCESS,
323 * Context information for connecting 2 peers in overlay
325 struct OverlayConnectContext
328 * The client which has requested for overlay connection
330 struct GNUNET_SERVER_Client *client;
335 struct Peer * peers[2];
338 * Transport handle of peers
340 struct GNUNET_TRANSPORT_Handle *peers_transport[2];
343 * Core handles of peers
345 struct GNUNET_CORE_Handle *peers_core[2];
350 struct GNUNET_MessageHeader *peers_hello[2];
353 * Get hello handle for peers
355 struct GNUNET_TRANSPORT_GetHelloHandle *peers_ghh[2];
358 * The peer identity of peers
360 struct GNUNET_PeerIdentity peers_identity[2];
363 * The id of the operation responsible for creating this context
368 * State information for determining whose HELLOs have been successfully
377 * Wrapper around OverlayConnectContext
379 struct OverlayConnectContextWrapper
382 * The overlay connect context
384 struct OverlayConnectContext *occ;
387 * The peer number in reference; should be 0 or 1
394 * The master context; generated with the first INIT message
396 static struct Context *master_context;
403 * Current Transmit Handle; NULL if no notify transmit exists currently
405 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
412 * The head for the LCF queue
414 static struct LCFContextQueue *lcfq_head;
417 * The tail for the LCF queue
419 static struct LCFContextQueue *lcfq_tail;
422 * The message queue head
424 static struct MessageQueue *mq_head;
427 * The message queue tail
429 static struct MessageQueue *mq_tail;
434 static struct GNUNET_TESTBED_Host **host_list;
439 static struct Route **route_list;
442 * A list of directly linked neighbours
444 static struct Slave **slave_list;
447 * A list of peers we own locally
449 static struct Peer **peer_list;
452 * The hashmap of shared services
454 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
457 * The size of the host list
459 static uint32_t host_list_size;
462 * The size of the route list
464 static uint32_t route_list_size;
467 * The size of directly linked neighbours list
469 static uint32_t slave_list_size;
472 * The size of the peer list
474 static uint32_t peer_list_size;
481 * The lcf_task handle
483 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
486 * The shutdown task handle
488 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
492 * Function called to notify a client about the connection begin ready to queue
493 * more data. "buf" will be NULL and "size" zero if the connection was closed
494 * for writing in the meantime.
497 * @param size number of bytes available in buf
498 * @param buf where the callee should write the message
499 * @return number of bytes written to buf
502 transmit_ready_notify (void *cls, size_t size, void *buf)
504 struct MessageQueue *mq_entry;
506 transmit_handle = NULL;
508 GNUNET_assert (NULL != mq_entry);
511 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
512 size = ntohs (mq_entry->msg->size);
513 memcpy (buf, mq_entry->msg, size);
514 GNUNET_free (mq_entry->msg);
515 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
516 GNUNET_free (mq_entry);
518 if (NULL != mq_entry)
520 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
521 ntohs (mq_entry->msg->size),
522 GNUNET_TIME_UNIT_FOREVER_REL,
523 &transmit_ready_notify, NULL);
529 * Queues a message in send queue for sending to the service
531 * @param client the client to whom the queued message has to be sent
532 * @param msg the message to queue
535 queue_message (struct GNUNET_SERVER_Client *client,
536 struct GNUNET_MessageHeader *msg)
538 struct MessageQueue *mq_entry;
542 type = ntohs (msg->type);
543 size = ntohs (msg->size);
544 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
545 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
546 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
548 mq_entry->client = client;
549 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
551 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
552 if (NULL == transmit_handle)
554 GNUNET_SERVER_notify_transmit_ready (client, size,
555 GNUNET_TIME_UNIT_FOREVER_REL,
556 &transmit_ready_notify, NULL);
561 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
563 * @param ptr the memory block to realloc
564 * @param size the size of ptr
565 * @param new_size the size to which ptr has to be realloc'ed
566 * @return the newly reallocated memory block
569 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
571 ptr = GNUNET_realloc (ptr, new_size);
573 ptr = memset (ptr + size, 0, new_size - size);
579 * Function to add a host to the current list of known hosts
581 * @param host the host to add
582 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
586 host_list_add (struct GNUNET_TESTBED_Host *host)
590 host_id = GNUNET_TESTBED_host_get_id_ (host);
591 if (host_list_size <= host_id)
594 TESTBED_realloc (host_list,
595 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
596 sizeof (struct GNUNET_TESTBED_Host *) *
597 (host_list_size + LIST_GROW_STEP));
598 host_list_size += LIST_GROW_STEP;
600 if (NULL != host_list[host_id])
602 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
603 return GNUNET_SYSERR;
605 host_list[host_id] = host;
611 * Adds a route to the route list
613 * @param route the route to add
616 route_list_add (struct Route *route)
618 if (route->dest >= route_list_size)
621 TESTBED_realloc (route_list,
622 sizeof (struct Route *) * route_list_size,
623 sizeof (struct Route *) *
624 (route_list_size + LIST_GROW_STEP));
625 route_list_size += LIST_GROW_STEP;
627 GNUNET_assert (NULL == route_list[route->dest]);
628 route_list[route->dest] = route;
633 * Adds a slave to the slave array
635 * @param slave the slave controller to add
638 slave_list_add (struct Slave *slave)
640 if (slave->host_id >= slave_list_size)
642 slave_list = TESTBED_realloc (slave_list,
643 sizeof (struct Slave *) *slave_list_size,
644 sizeof (struct Slave *) *
645 (slave_list_size + LIST_GROW_STEP));
646 slave_list_size += LIST_GROW_STEP;
648 GNUNET_assert (NULL == slave_list[slave->host_id]);
649 slave_list[slave->host_id] = slave;
654 * Adds a peer to the peer array
656 * @param peer the peer to add
659 peer_list_add (struct Peer *peer)
661 if (peer->id >= peer_list_size)
663 peer_list = TESTBED_realloc (peer_list,
664 sizeof (struct Peer *) * peer_list_size,
665 sizeof (struct Peer *) *
666 (peer_list_size + LIST_GROW_STEP));
667 peer_list_size += LIST_GROW_STEP;
669 GNUNET_assert (NULL == peer_list[peer->id]);
670 peer_list[peer->id] = peer;
675 * Routes message to a host given its host_id
677 * @param host_id the id of the destination host
678 * @param msg the message to be routed
681 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
688 * The Link Controller forwarding task
690 * @param cls the LCFContext
691 * @param tc the Task context from scheduler
694 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
698 * Completion callback for host registrations while forwarding Link Controller messages
700 * @param cls the LCFContext
701 * @param emsg the error message; NULL if host registration is successful
704 lcf_proc_cc (void *cls, const char *emsg)
706 struct LCFContext *lcf = cls;
709 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
714 goto registration_error;
715 lcf->state = DELEGATED_HOST_REGISTERED;
716 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
718 case DELEGATED_HOST_REGISTERED:
720 goto registration_error;
721 lcf->state = SLAVE_HOST_REGISTERED;
722 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
725 GNUNET_assert (0); /* Shouldn't reach here */
730 LOG (GNUNET_ERROR_TYPE_WARNING,
731 "Host registration failed with message: %s\n", emsg);
732 lcf->state = FINISHED;
733 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
738 * The Link Controller forwarding task
740 * @param cls the LCFContext
741 * @param tc the Task context from scheduler
744 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
746 struct LCFContext *lcf = cls;
747 struct LCFContextQueue *lcfq;
749 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
754 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
755 lcf->gateway->controller))
758 GNUNET_TESTBED_register_host (lcf->gateway->controller,
759 host_list[lcf->delegated_host_id],
764 lcf->state = DELEGATED_HOST_REGISTERED;
765 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
768 case DELEGATED_HOST_REGISTERED:
770 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
771 lcf->gateway->controller))
774 GNUNET_TESTBED_register_host (lcf->gateway->controller,
775 host_list[lcf->slave_host_id],
780 lcf->state = SLAVE_HOST_REGISTERED;
781 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
784 case SLAVE_HOST_REGISTERED:
785 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
786 host_list[lcf->delegated_host_id],
787 host_list[lcf->slave_host_id],
788 lcf->sxcfg, lcf->sxcfg_size,
790 lcf->is_subordinate);
791 lcf->state = FINISHED;
792 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
796 GNUNET_assert (lcfq->lcf == lcf);
797 GNUNET_free (lcf->sxcfg);
799 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
801 if (NULL != lcfq_head)
803 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
809 * Callback for event from slave controllers
811 * @param cls struct Slave *
812 * @param event information about the event
815 slave_event_callback(void *cls,
816 const struct GNUNET_TESTBED_EventInformation *event)
823 * Callback to signal successfull startup of the controller process
825 * @param cls the closure from GNUNET_TESTBED_controller_start()
826 * @param cfg the configuration with which the controller has been started;
827 * NULL if status is not GNUNET_OK
828 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
829 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
832 slave_status_callback (void *cls,
833 const struct GNUNET_CONFIGURATION_Handle *cfg,
836 struct Slave *slave = cls;
838 if (GNUNET_SYSERR == status)
840 slave->controller_proc = NULL;
841 LOG (GNUNET_ERROR_TYPE_WARNING,
842 "Unexpected slave shutdown\n");
843 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
847 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
848 master_context->event_mask,
849 &slave_event_callback, slave);
854 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
857 * @param client identification of the client
858 * @param message the actual message
861 handle_init (void *cls,
862 struct GNUNET_SERVER_Client *client,
863 const struct GNUNET_MessageHeader *message)
865 const struct GNUNET_TESTBED_InitMessage *msg;
866 struct GNUNET_TESTBED_Host *host;
870 if (NULL != master_context)
873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
876 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
877 master_context = GNUNET_malloc (sizeof (struct Context));
878 master_context->client = client;
879 master_context->host_id = ntohl (msg->host_id);
880 GNUNET_assert (GNUNET_OK ==
881 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
882 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
883 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
884 NULL, 0, NI_NUMERICHOST))
886 LOG (GNUNET_ERROR_TYPE_WARNING,
887 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
889 GNUNET_free (master_context->master_ip);
893 if (0 == strcasecmp (master_context->master_ip, "localhost"))
894 { /* Hack for connections via unix sockets */
895 LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
896 GNUNET_free (master_context->master_ip);
897 master_context->master_ip = strdup ("127.0.0.1");
899 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
900 master_context->system =
901 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
902 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
904 host_list_add (host);
905 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
906 GNUNET_SERVER_client_keep (client);
907 LOG_DEBUG ("Created master context with host ID: %u\n",
908 master_context->host_id);
909 GNUNET_SERVER_receive_done (client, GNUNET_OK);
914 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
917 * @param client identification of the client
918 * @param message the actual message
921 handle_add_host (void *cls,
922 struct GNUNET_SERVER_Client *client,
923 const struct GNUNET_MessageHeader *message)
925 struct GNUNET_TESTBED_Host *host;
926 const struct GNUNET_TESTBED_AddHostMessage *msg;
927 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
932 uint16_t username_length;
933 uint16_t hostname_length;
937 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
938 msize = ntohs (msg->header.size);
939 username = (char *) &(msg[1]);
940 username_length = ntohs (msg->user_name_length);
941 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
942 + username_length + 1)); /* msg must contain hostname */
943 if (0 != username_length)
944 GNUNET_assert ('\0' == username[username_length]);
945 username_length = (0 == username_length) ? 0 : username_length + 1;
946 hostname = username + username_length;
947 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
949 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
950 GNUNET_assert (strlen (hostname) == hostname_length - 1);
951 host_id = ntohl (msg->host_id);
952 LOG_DEBUG ("Received ADDHOST message\n");
953 LOG_DEBUG ("-------host id: %u\n", host_id);
954 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
955 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
956 else LOG_DEBUG ("-------username: NULL\n");
957 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
958 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
959 ntohs (msg->ssh_port));
960 GNUNET_SERVER_receive_done (client, GNUNET_OK);
961 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
962 if (GNUNET_OK != host_list_add (host))
964 /* We are unable to add a host */
965 emsg = "A host exists with given host-id";
966 LOG_DEBUG ("%s: %u", emsg, host_id);
967 GNUNET_TESTBED_host_destroy (host);
968 reply_size += strlen (emsg) + 1;
969 reply = GNUNET_malloc (reply_size);
970 memcpy (&reply[1], emsg, strlen (emsg) + 1);
973 reply = GNUNET_malloc (reply_size);
974 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
975 reply->header.size = htons (reply_size);
976 reply->host_id = htonl (host_id);
977 queue_message (client, (struct GNUNET_MessageHeader *) reply);
982 * Iterator over hash map entries.
985 * @param key current key code
986 * @param value value in the hash map
987 * @return GNUNET_YES if we should continue to
991 int ss_exists_iterator (void *cls,
992 const struct GNUNET_HashCode * key,
995 struct SharedService *queried_ss = cls;
996 struct SharedService *ss = value;
998 if (0 == strcmp (ss->name, queried_ss->name))
1006 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1009 * @param client identification of the client
1010 * @param message the actual message
1013 handle_configure_shared_service (void *cls,
1014 struct GNUNET_SERVER_Client *client,
1015 const struct GNUNET_MessageHeader *message)
1017 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1018 struct SharedService *ss;
1020 struct GNUNET_HashCode hash;
1022 uint16_t service_name_size;
1024 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1025 msg_size = ntohs (message->size);
1026 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1032 service_name_size = msg_size -
1033 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1034 service_name = (char *) &msg[1];
1035 if ('\0' != service_name[service_name_size - 1])
1038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1041 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1042 service_name, ntohl (msg->num_peers));
1043 if (ntohl (msg->host_id) != master_context->host_id)
1045 route_message (ntohl (msg->host_id), message);
1046 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1049 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1050 ss = GNUNET_malloc (sizeof (struct SharedService));
1051 ss->name = strdup (service_name);
1052 ss->num_shared = ntohl (msg->num_peers);
1053 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1054 if (GNUNET_SYSERR ==
1055 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1056 &ss_exists_iterator, ss))
1058 LOG (GNUNET_ERROR_TYPE_WARNING,
1059 "Service %s already configured as a shared service. "
1060 "Ignoring service sharing request \n", ss->name);
1061 GNUNET_free (ss->name);
1065 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1071 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1074 * @param client identification of the client
1075 * @param message the actual message
1078 handle_link_controllers (void *cls,
1079 struct GNUNET_SERVER_Client *client,
1080 const struct GNUNET_MessageHeader *message)
1082 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1083 struct GNUNET_CONFIGURATION_Handle *cfg;
1084 struct LCFContextQueue *lcfq;
1085 struct Route *route;
1086 struct Route *new_route;
1090 uint32_t delegated_host_id;
1091 uint32_t slave_host_id;
1094 if (NULL == master_context)
1097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1100 msize = ntohs (message->size);
1101 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1107 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1108 delegated_host_id = ntohl (msg->delegated_host_id);
1109 if (delegated_host_id == master_context->host_id)
1112 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1113 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1116 if ((delegated_host_id >= host_list_size) ||
1117 (NULL == host_list[delegated_host_id]))
1119 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1123 slave_host_id = ntohl (msg->slave_host_id);
1124 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1126 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1130 if (slave_host_id == delegated_host_id)
1132 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1136 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1137 config_size = ntohs (msg->config_size);
1139 if (slave_host_id == master_context->host_id) /* Link from us */
1141 struct Slave *slave;
1143 if ((delegated_host_id < slave_list_size) &&
1144 (NULL != slave_list[delegated_host_id])) /* We have already added */
1146 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1151 config = GNUNET_malloc (config_size);
1152 dest_size = (uLongf) config_size;
1153 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1154 (const Bytef *) &msg[1], (uLong) msize))
1156 GNUNET_break (0); /* Compression error */
1157 GNUNET_free (config);
1158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1161 if (config_size == dest_size)
1163 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1164 GNUNET_free (config);
1165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1168 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1169 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1172 GNUNET_break (0); /* Configuration parsing error */
1173 GNUNET_free (config);
1174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1177 GNUNET_free (config);
1178 if ((delegated_host_id < slave_list_size) &&
1179 (NULL != slave_list[delegated_host_id]))
1181 GNUNET_break (0); /* Configuration parsing error */
1182 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1185 slave = GNUNET_malloc (sizeof (struct Slave));
1186 slave->host_id = delegated_host_id;
1187 slave_list_add (slave);
1188 if (1 == msg->is_subordinate)
1190 slave->controller_proc =
1191 GNUNET_TESTBED_controller_start (master_context->master_ip,
1192 host_list[slave->host_id],
1193 cfg, &slave_status_callback,
1198 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1199 master_context->event_mask,
1200 &slave_event_callback, slave);
1202 GNUNET_CONFIGURATION_destroy (cfg);
1203 new_route = GNUNET_malloc (sizeof (struct Route));
1204 new_route->dest = delegated_host_id;
1205 new_route->thru = master_context->host_id;
1206 route_list_add (new_route);
1207 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1211 /* Route the request */
1212 if (slave_host_id >= route_list_size)
1214 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1218 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1219 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1220 lcfq->lcf->delegated_host_id = delegated_host_id;
1221 lcfq->lcf->slave_host_id = slave_host_id;
1222 while (NULL != (route = route_list[slave_host_id]))
1224 if (route->thru == master_context->host_id)
1226 slave_host_id = route->thru;
1228 GNUNET_assert (NULL != route); /* because we add routes carefully */
1229 GNUNET_assert (route->dest < slave_list_size);
1230 GNUNET_assert (NULL != slave_list[route->dest]);
1231 lcfq->lcf->is_subordinate =
1232 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1233 lcfq->lcf->state = INIT;
1234 lcfq->lcf->gateway = slave_list[route->dest];
1235 lcfq->lcf->sxcfg_size = msize;
1236 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1237 lcfq->lcf->scfg_size = config_size;
1238 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1239 if (NULL == lcfq_head)
1241 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1242 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1243 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1246 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1247 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1248 new_route = GNUNET_malloc (sizeof (struct Route));
1249 new_route->dest = delegated_host_id;
1250 new_route->thru = route->dest;
1251 route_list_add (new_route);
1256 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1259 * @param client identification of the client
1260 * @param message the actual message
1263 handle_peer_create (void *cls,
1264 struct GNUNET_SERVER_Client *client,
1265 const struct GNUNET_MessageHeader *message)
1267 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1268 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1269 struct GNUNET_CONFIGURATION_Handle *cfg;
1273 uint32_t config_size;
1277 msize = ntohs (message->size);
1278 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1280 GNUNET_break (0); /* We need configuration */
1281 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1284 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1285 if (ntohl (msg->host_id) == master_context->host_id)
1290 /* We are responsible for this peer */
1291 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1292 config_size = ntohl (msg->config_size);
1293 config = GNUNET_malloc (config_size);
1294 dest_size = config_size;
1295 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1296 (const Bytef *) &msg[1], (uLong) msize)))
1298 GNUNET_break (0); /* uncompression error */
1299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1302 if (config_size != dest_size)
1304 GNUNET_break (0);/* Uncompressed config size mismatch */
1305 GNUNET_free (config);
1306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1309 cfg = GNUNET_CONFIGURATION_create ();
1310 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1313 GNUNET_break (0); /* Configuration parsing error */
1314 GNUNET_free (config);
1315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1318 GNUNET_free (config);
1319 peer = GNUNET_malloc (sizeof (struct Peer));
1321 peer->id = ntohl (msg->peer_id);
1322 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1323 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1327 if (NULL == peer->peer)
1329 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1336 peer_list_add (peer);
1337 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1338 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1339 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1340 reply->peer_id = msg->peer_id;
1341 reply->operation_id = msg->operation_id;
1342 queue_message (client, &reply->header);
1343 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1347 /* FIXME: Forward the peer to other host */
1349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1354 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1357 * @param client identification of the client
1358 * @param message the actual message
1361 handle_peer_destroy (void *cls,
1362 struct GNUNET_SERVER_Client *client,
1363 const struct GNUNET_MessageHeader *message)
1365 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1366 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1369 uint16_t reply_size;
1371 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1372 peer_id = ntohl (msg->peer_id);
1373 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1374 peer_id, GNUNET_ntohll (msg->operation_id));
1375 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1378 /* FIXME: Reply with failure event message or forward to slave controller */
1379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1382 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1383 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1384 GNUNET_free (peer_list[peer_id]);
1385 peer_list[peer_id] = NULL;
1386 for (id = 0; id < LIST_GROW_STEP; id++)
1388 if (((peer_id + id >= peer_list_size) ||
1389 (NULL != peer_list[peer_id])))
1392 if (LIST_GROW_STEP == id)
1394 peer_list_size -= LIST_GROW_STEP;
1395 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1398 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1399 reply = GNUNET_malloc (reply_size);
1400 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1401 reply->header.size = htons (reply_size);
1402 reply->operation_id = msg->operation_id;
1403 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1404 queue_message (client, &reply->header);
1405 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1410 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1413 * @param client identification of the client
1414 * @param message the actual message
1417 handle_peer_start (void *cls,
1418 struct GNUNET_SERVER_Client *client,
1419 const struct GNUNET_MessageHeader *message)
1421 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1422 struct GNUNET_TESTBED_PeerEventMessage *reply;
1425 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1426 peer_id = ntohl (msg->peer_id);
1427 if ((peer_id >= peer_list_size)
1428 || (NULL == peer_list[peer_id]))
1431 /* FIXME: reply with failure message or forward to slave controller */
1432 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1435 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1437 /* FIXME: return FAILURE message */
1439 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1442 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1443 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1444 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1445 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1446 reply->host_id = htonl (master_context->host_id);
1447 reply->peer_id = msg->peer_id;
1448 reply->operation_id = msg->operation_id;
1449 queue_message (client, &reply->header);
1450 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1455 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1458 * @param client identification of the client
1459 * @param message the actual message
1462 handle_peer_stop (void *cls,
1463 struct GNUNET_SERVER_Client *client,
1464 const struct GNUNET_MessageHeader *message)
1466 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1467 struct GNUNET_TESTBED_PeerEventMessage *reply;
1470 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1471 peer_id = ntohl (msg->peer_id);
1472 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1474 GNUNET_break (0); /* FIXME: route to slave? */
1475 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1478 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1480 /* FIXME: return FAILURE message */
1482 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1485 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1486 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1487 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1488 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1489 reply->host_id = htonl (master_context->host_id);
1490 reply->peer_id = msg->peer_id;
1491 reply->operation_id = msg->operation_id;
1492 queue_message (client, &reply->header);
1493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1498 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1501 * @param client identification of the client
1502 * @param message the actual message
1505 handle_peer_get_config (void *cls,
1506 struct GNUNET_SERVER_Client *client,
1507 const struct GNUNET_MessageHeader *message)
1509 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1510 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1518 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1519 peer_id = ntohl (msg->peer_id);
1520 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1522 /* FIXME: return FAILURE message */
1524 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1526 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1528 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1529 GNUNET_free (config);
1530 msize = xc_size + sizeof (struct
1531 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1532 reply = GNUNET_realloc (xconfig, msize);
1533 (void) memmove (&reply[1], reply, xc_size);
1534 reply->header.size = htons (msize);
1535 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1536 reply->peer_id = msg->peer_id;
1537 reply->operation_id = msg->operation_id;
1538 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1539 &reply->peer_identity);
1540 reply->config_size = htons ((uint16_t) c_size);
1541 queue_message (client, &reply->header);
1542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1550 * Function called to notify transport users that another
1551 * peer connected to us.
1553 * @param cls closure
1554 * @param new_peer the peer that connected
1555 * @param ats performance data
1556 * @param ats_count number of entries in ats (excluding 0-termination)
1559 overlay_connect_notify (void *cls,
1560 const struct GNUNET_PeerIdentity * new_peer,
1561 const struct GNUNET_ATS_Information * ats,
1562 unsigned int ats_count)
1564 struct OverlayConnectContextWrapper *occw = cls;
1565 struct OverlayConnectContext *occ;
1566 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1568 char *other_peer_str;
1571 LOG_DEBUG ("Overlay connect notify\n");
1574 if (0 == memcmp (new_peer, &occ->peers_identity[peer],
1575 sizeof (struct GNUNET_PeerIdentity)))
1577 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1578 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->peers_identity[peer ^ 1]));
1579 if (0 != memcmp (new_peer, &occ->peers_identity[peer ^ 1],
1580 sizeof (struct GNUNET_PeerIdentity)))
1582 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1583 new_peer_str, other_peer_str);
1584 GNUNET_free (new_peer_str);
1585 GNUNET_free (other_peer_str);
1589 LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1590 GNUNET_free (new_peer_str);
1591 GNUNET_free (other_peer_str);
1592 GNUNET_TRANSPORT_disconnect (occ->peers_transport[peer]);
1593 occ->peers_transport[peer] = NULL;
1594 if (NULL != occ->peers_transport[peer ^ 1])
1596 /* Peers are connected - now send overlay connect success message */
1597 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1598 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1599 msg->header.size = htons (sizeof (struct
1600 GNUNET_TESTBED_ConnectionEventMessage));
1601 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1602 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1603 msg->peer1 = occ->peers[0]->id;
1604 msg->peer2 = occ->peers[1]->id;
1605 msg->operation_id = GNUNET_htonll (occ->op_id);
1606 queue_message (occ->client, &msg->header);
1607 GNUNET_SERVER_client_drop (occ->client);
1613 * For peers that haven't yet connected, send
1614 * CORE connect requests.
1616 * @param cls the 'struct SendHelloContext *'
1617 * @param tc scheduler context
1620 send_core_connect_requests (void *cls,
1621 const struct GNUNET_SCHEDULER_TaskContext *tc)
1623 struct OverlayConnectContext *occ = cls;
1626 LOG_DEBUG ("Sending connect requests to transport\n");
1627 for (peer = 0; peer < 2; peer++)
1628 GNUNET_TRANSPORT_try_connect (occ->peers_transport[peer],
1629 &occ->peers_identity[peer ^ 1]);
1634 * Exchanges HELLO of peers among them
1636 * @param occ the OverlayConnectContext to look into for HELLO messages
1639 exchange_hellos (struct OverlayConnectContext *occ)
1641 struct GNUNET_CORE_MessageHandler no_handlers[] = {
1644 struct OverlayConnectContextWrapper *occw;
1647 GNUNET_assert (NULL != occ->peers_hello[0]);
1648 GNUNET_assert (NULL != occ->peers_hello[1]);
1649 LOG_DEBUG ("Exchanging hellos\n");
1650 for (peer = 0; peer < 2; peer++)
1652 GNUNET_TRANSPORT_offer_hello (occ->peers_transport[peer],
1653 occ->peers_hello[peer ^ 1],
1655 GNUNET_free (occ->peers_hello[peer ^ 1]);
1656 occ->peers_hello[peer ^ 1] = NULL;
1657 occw = GNUNET_malloc (sizeof (struct OverlayConnectContextWrapper));
1660 occ->peers_core[peer] =
1661 GNUNET_CORE_connect (occ->peers[peer]->cfg,
1662 occw, NULL, &overlay_connect_notify, NULL,
1663 NULL, GNUNET_NO, NULL, GNUNET_NO, no_handlers);
1665 if ((NULL == occ->peers_hello[peer]) && (NULL == occ->peers_hello[peer ^ 1]))
1666 GNUNET_SCHEDULER_add_now (&send_core_connect_requests, occ);
1671 * Function called whenever there is an update to the
1672 * HELLO of peers in the OverlayConnectClosure
1674 * @param cls closure
1675 * @param hello our updated HELLO
1678 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1680 struct OverlayConnectContextWrapper *occw = cls;
1681 struct OverlayConnectContext *occ;
1685 msize = ntohs (hello->size);
1687 GNUNET_assert (peer <= 1);
1690 occ->peers_hello[peer] = GNUNET_malloc (msize);
1691 memcpy (occ->peers_hello[peer], hello, msize);
1692 GNUNET_TRANSPORT_get_hello_cancel (occ->peers_ghh[peer]);
1693 occ->peers_ghh[peer] = NULL;
1694 if (NULL != occ->peers_hello[peer ^ 1])
1695 exchange_hellos (occ);
1700 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1703 * @param client identification of the client
1704 * @param message the actual message
1707 handle_overlay_connect (void *cls,
1708 struct GNUNET_SERVER_Client *client,
1709 const struct GNUNET_MessageHeader *message)
1711 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1712 struct OverlayConnectContext *occ;
1713 struct OverlayConnectContextWrapper *occw;
1718 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1719 p1 = ntohl (msg->peer1);
1720 p2 = ntohl (msg->peer2);
1721 GNUNET_assert (p1 < peer_list_size);
1722 GNUNET_assert (NULL != peer_list[p1]);
1723 GNUNET_assert (p2 < peer_list_size);
1724 GNUNET_assert (NULL != peer_list[p2]);
1725 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1726 GNUNET_SERVER_client_keep (client);
1727 occ->client = client;
1728 occ->state = OCC_STATE_INIT;
1729 occ->peers[0] = peer_list[p1];
1730 occ->peers[1] = peer_list[p2];
1731 occ->op_id = GNUNET_ntohll (msg->operation_id);
1732 for (peer = 0; peer < 2; peer++)
1734 GNUNET_TESTING_peer_get_identity (occ->peers[peer]->peer,
1735 &occ->peers_identity[peer]);
1736 occ->peers_transport[peer] =
1737 GNUNET_TRANSPORT_connect (occ->peers[peer]->cfg,
1738 &occ->peers_identity[peer],
1739 NULL, NULL, NULL, NULL);
1740 occw = GNUNET_malloc (sizeof (struct OverlayConnectContextWrapper));
1743 occ->peers_ghh[peer] =
1744 GNUNET_TRANSPORT_get_hello (occ->peers_transport[peer],
1745 &hello_update_cb, occw);
1747 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1752 * Iterator over hash map entries.
1754 * @param cls closure
1755 * @param key current key code
1756 * @param value value in the hash map
1757 * @return GNUNET_YES if we should continue to
1762 ss_map_free_iterator (void *cls,
1763 const struct GNUNET_HashCode * key, void *value)
1765 struct SharedService *ss = value;
1767 GNUNET_assert (GNUNET_YES ==
1768 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1769 GNUNET_free (ss->name);
1776 * Task to clean up and shutdown nicely
1779 * @param tc the TaskContext from scheduler
1782 shutdown_task (void *cls,
1783 const struct GNUNET_SCHEDULER_TaskContext *tc)
1785 struct LCFContextQueue *lcfq;
1788 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1789 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1790 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1792 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1793 if (NULL != lcfq_head)
1795 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1797 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1798 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1800 if (NULL != lcfq_head->lcf->rhandle)
1801 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1803 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1804 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1806 GNUNET_free (lcfq->lcf->sxcfg);
1807 GNUNET_free (lcfq->lcf);
1808 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1811 /* Clear peer list */
1812 for (id = 0; id < peer_list_size; id++)
1813 if (NULL != peer_list[id])
1815 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1816 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1817 GNUNET_free (peer_list[id]);
1819 GNUNET_free_non_null (peer_list);
1820 /* Clear host list */
1821 for (id = 0; id < host_list_size; id++)
1822 if (NULL != host_list[id])
1823 GNUNET_TESTBED_host_destroy (host_list[id]);
1824 GNUNET_free_non_null (host_list);
1825 /* Clear route list */
1826 for (id = 0; id < route_list_size; id++)
1827 if (NULL != route_list[id])
1828 GNUNET_free (route_list[id]);
1829 GNUNET_free_non_null (route_list);
1830 /* Clear slave_list */
1831 for (id = 0; id < slave_list_size; id++)
1832 if (NULL != slave_list[id])
1834 GNUNET_assert (NULL != slave_list[id]->controller);
1835 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1836 if (NULL != slave_list[id]->controller_proc)
1837 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1839 if (NULL != master_context)
1841 GNUNET_free_non_null (master_context->master_ip);
1842 if (NULL != master_context->system)
1843 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1844 GNUNET_free (master_context);
1845 master_context = NULL;
1851 * Callback for client disconnect
1854 * @param client the client which has disconnected
1857 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1859 if (NULL == master_context)
1861 if (client == master_context->client)
1863 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1864 GNUNET_SERVER_client_drop (client);
1865 /* should not be needed as we're terminated by failure to read
1866 from stdin, but if stdin fails for some reason, this shouldn't
1867 hurt for now --- might need to revise this later if we ever
1868 decide that master connections might be temporarily down
1870 //GNUNET_SCHEDULER_shutdown ();
1878 * @param cls closure
1879 * @param server the initialized server
1880 * @param cfg configuration to use
1883 testbed_run (void *cls,
1884 struct GNUNET_SERVER_Handle *server,
1885 const struct GNUNET_CONFIGURATION_Handle *cfg)
1887 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1889 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1890 sizeof (struct GNUNET_TESTBED_InitMessage)},
1891 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1892 {&handle_configure_shared_service, NULL,
1893 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1894 {&handle_link_controllers, NULL,
1895 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1896 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1897 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1898 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1899 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1900 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1901 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1902 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1903 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
1904 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1905 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
1906 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
1910 GNUNET_SERVER_add_handlers (server,
1912 GNUNET_SERVER_disconnect_notify (server,
1913 &client_disconnect_cb,
1915 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1917 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1920 LOG_DEBUG ("Testbed startup complete\n");
1925 * The starting point of execution
1927 int main (int argc, char *const *argv)
1929 //sleep (60); /* Debugging */
1932 GNUNET_SERVICE_run (argc,
1935 GNUNET_SERVICE_OPTION_NONE,