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"
33 #include "gnunet_testbed_service.h"
35 #include "testbed_api.h"
36 #include "testbed_api_hosts.h"
37 #include "gnunet_testing_lib-new.h"
42 #define LOG(kind,...) \
43 GNUNET_log (kind, __VA_ARGS__)
48 #define LOG_DEBUG(...) \
49 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52 #define LIST_GROW_STEP 10
57 * The client handle associated with this context
59 struct GNUNET_SERVER_Client *client;
62 * The network address of the master controller
67 * The TESTING system handle for starting peers locally
69 struct GNUNET_TESTING_System *system;
72 * Event mask of event to be responded in this context
77 * Our host id according to this context
84 * The message queue for sending messages to clients
89 * The message to be sent
91 struct GNUNET_MessageHeader *msg;
94 * The client to send the message to
96 struct GNUNET_SERVER_Client *client;
99 * next pointer for DLL
101 struct MessageQueue *next;
104 * prev pointer for DLL
106 struct MessageQueue *prev;
111 * The structure for identifying a shared service
116 * The name of the shared service
121 * Number of shared peers per instance of the shared service
126 * Number of peers currently sharing the service
128 uint32_t num_sharing;
143 * The host destination is reachable thru
150 * Structure representing a connected(directly-linked) controller
155 * The controller process handle if we had started the controller
157 struct GNUNET_TESTBED_ControllerProc *controller_proc;
160 * The controller handle
162 struct GNUNET_TESTBED_Controller *controller;
165 * The id of the host this controller is running on
172 * States of LCFContext
177 * The Context has been initialized; Nothing has been done on it
182 * Delegated host has been registered at the forwarding controller
184 DELEGATED_HOST_REGISTERED,
187 * The slave host has been registred at the forwarding controller
189 SLAVE_HOST_REGISTERED,
192 * The context has been finished (may have error)
200 * Link controllers request forwarding context
205 * The serialized and compressed configuration
210 * The gateway which will pass the link message to delegated host
212 struct Slave *gateway;
215 * The host registration handle while registered hosts in this context
217 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
220 * The size of the compressed serialized configuration
225 * The size of the uncompressed configuration
230 * Should the delegated host be started by the slave host?
235 * The state of this context
237 enum LCFContextState state;
242 uint32_t delegated_host_id;
247 uint32_t slave_host_id;
253 * Structure of a queue entry in LCFContext request queue
255 struct LCFContextQueue
260 struct LCFContext *lcf;
265 struct LCFContextQueue *next;
270 struct LCFContextQueue *prev;
275 * A locally started peer
280 * The peer handle from testing API
282 struct GNUNET_TESTING_Peer *peer;
285 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
288 struct GNUNET_CONFIGURATION_Handle *cfg;
291 * Our local reference id for this peer
299 * State information for overlay connect context
309 * Peer 1 has connected to peer0
311 OCC_STATE_PEER0_SUCCESS,
314 * Peer 2 has connected to peer1
316 OCC_STATE_PEER1_SUCCESS,
322 * Context information for connecting 2 peers in overlay
324 struct OverlayConnectContext
329 struct Peer * peers[2];
332 * Transport handle of peers
334 struct GNUNET_TRANSPORT_Handle *peers_transport[2];
339 struct GNUNET_MessageHeader *peers_hello[2];
342 * Get hello handle for peers
344 struct GNUNET_TRANSPORT_GetHelloHandle *peers_ghh[2];
347 * The peer identity of peers
349 struct GNUNET_PeerIdentity peers_identity[2];
352 * State information for determining whose HELLOs have been successfully
361 * Wrapper around OverlayConnectContext
363 struct OverlayConnectContextWrapper
366 * The overlay connect context
368 struct OverlayConnectContext *occ;
371 * The peer number in reference; should be 0 or 1
378 * The master context; generated with the first INIT message
380 static struct Context *master_context;
387 * Current Transmit Handle; NULL if no notify transmit exists currently
389 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
396 * The head for the LCF queue
398 static struct LCFContextQueue *lcfq_head;
401 * The tail for the LCF queue
403 static struct LCFContextQueue *lcfq_tail;
406 * The message queue head
408 static struct MessageQueue *mq_head;
411 * The message queue tail
413 static struct MessageQueue *mq_tail;
418 static struct GNUNET_TESTBED_Host **host_list;
423 static struct Route **route_list;
426 * A list of directly linked neighbours
428 static struct Slave **slave_list;
431 * A list of peers we own locally
433 static struct Peer **peer_list;
436 * The hashmap of shared services
438 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
441 * The size of the host list
443 static uint32_t host_list_size;
446 * The size of the route list
448 static uint32_t route_list_size;
451 * The size of directly linked neighbours list
453 static uint32_t slave_list_size;
456 * The size of the peer list
458 static uint32_t peer_list_size;
465 * The lcf_task handle
467 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
470 * The shutdown task handle
472 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
476 * Function called to notify a client about the connection begin ready to queue
477 * more data. "buf" will be NULL and "size" zero if the connection was closed
478 * for writing in the meantime.
481 * @param size number of bytes available in buf
482 * @param buf where the callee should write the message
483 * @return number of bytes written to buf
486 transmit_ready_notify (void *cls, size_t size, void *buf)
488 struct MessageQueue *mq_entry;
490 transmit_handle = NULL;
492 GNUNET_assert (NULL != mq_entry);
495 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
496 size = ntohs (mq_entry->msg->size);
497 memcpy (buf, mq_entry->msg, size);
498 GNUNET_free (mq_entry->msg);
499 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
500 GNUNET_free (mq_entry);
502 if (NULL != mq_entry)
504 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
505 ntohs (mq_entry->msg->size),
506 GNUNET_TIME_UNIT_FOREVER_REL,
507 &transmit_ready_notify, NULL);
513 * Queues a message in send queue for sending to the service
515 * @param client the client to whom the queued message has to be sent
516 * @param msg the message to queue
519 queue_message (struct GNUNET_SERVER_Client *client,
520 struct GNUNET_MessageHeader *msg)
522 struct MessageQueue *mq_entry;
526 type = ntohs (msg->type);
527 size = ntohs (msg->size);
528 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
529 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
530 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
532 mq_entry->client = client;
533 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
535 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
536 if (NULL == transmit_handle)
538 GNUNET_SERVER_notify_transmit_ready (client, size,
539 GNUNET_TIME_UNIT_FOREVER_REL,
540 &transmit_ready_notify, NULL);
545 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
547 * @param ptr the memory block to realloc
548 * @param size the size of ptr
549 * @param new_size the size to which ptr has to be realloc'ed
550 * @return the newly reallocated memory block
553 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
555 ptr = GNUNET_realloc (ptr, new_size);
557 ptr = memset (ptr + size, 0, new_size - size);
563 * Function to add a host to the current list of known hosts
565 * @param host the host to add
566 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
570 host_list_add (struct GNUNET_TESTBED_Host *host)
574 host_id = GNUNET_TESTBED_host_get_id_ (host);
575 if (host_list_size <= host_id)
578 TESTBED_realloc (host_list,
579 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
580 sizeof (struct GNUNET_TESTBED_Host *) *
581 (host_list_size + LIST_GROW_STEP));
582 host_list_size += LIST_GROW_STEP;
584 if (NULL != host_list[host_id])
586 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
587 return GNUNET_SYSERR;
589 host_list[host_id] = host;
595 * Adds a route to the route list
597 * @param route the route to add
600 route_list_add (struct Route *route)
602 if (route->dest >= route_list_size)
605 TESTBED_realloc (route_list,
606 sizeof (struct Route *) * route_list_size,
607 sizeof (struct Route *) *
608 (route_list_size + LIST_GROW_STEP));
609 route_list_size += LIST_GROW_STEP;
611 GNUNET_assert (NULL == route_list[route->dest]);
612 route_list[route->dest] = route;
617 * Adds a slave to the slave array
619 * @param slave the slave controller to add
622 slave_list_add (struct Slave *slave)
624 if (slave->host_id >= slave_list_size)
626 slave_list = TESTBED_realloc (slave_list,
627 sizeof (struct Slave *) *slave_list_size,
628 sizeof (struct Slave *) *
629 (slave_list_size + LIST_GROW_STEP));
630 slave_list_size += LIST_GROW_STEP;
632 GNUNET_assert (NULL == slave_list[slave->host_id]);
633 slave_list[slave->host_id] = slave;
638 * Adds a peer to the peer array
640 * @param peer the peer to add
643 peer_list_add (struct Peer *peer)
645 if (peer->id >= peer_list_size)
647 peer_list = TESTBED_realloc (peer_list,
648 sizeof (struct Peer *) * peer_list_size,
649 sizeof (struct Peer *) *
650 (peer_list_size + LIST_GROW_STEP));
651 peer_list_size += LIST_GROW_STEP;
653 GNUNET_assert (NULL == peer_list[peer->id]);
654 peer_list[peer->id] = peer;
659 * Routes message to a host given its host_id
661 * @param host_id the id of the destination host
662 * @param msg the message to be routed
665 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
672 * The Link Controller forwarding task
674 * @param cls the LCFContext
675 * @param tc the Task context from scheduler
678 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
682 * Completion callback for host registrations while forwarding Link Controller messages
684 * @param cls the LCFContext
685 * @param emsg the error message; NULL if host registration is successful
688 lcf_proc_cc (void *cls, const char *emsg)
690 struct LCFContext *lcf = cls;
693 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
698 goto registration_error;
699 lcf->state = DELEGATED_HOST_REGISTERED;
700 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
702 case DELEGATED_HOST_REGISTERED:
704 goto registration_error;
705 lcf->state = SLAVE_HOST_REGISTERED;
706 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
709 GNUNET_assert (0); /* Shouldn't reach here */
714 LOG (GNUNET_ERROR_TYPE_WARNING,
715 "Host registration failed with message: %s\n", emsg);
716 lcf->state = FINISHED;
717 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
722 * The Link Controller forwarding task
724 * @param cls the LCFContext
725 * @param tc the Task context from scheduler
728 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
730 struct LCFContext *lcf = cls;
731 struct LCFContextQueue *lcfq;
733 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
738 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
739 lcf->gateway->controller))
742 GNUNET_TESTBED_register_host (lcf->gateway->controller,
743 host_list[lcf->delegated_host_id],
748 lcf->state = DELEGATED_HOST_REGISTERED;
749 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
752 case DELEGATED_HOST_REGISTERED:
754 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
755 lcf->gateway->controller))
758 GNUNET_TESTBED_register_host (lcf->gateway->controller,
759 host_list[lcf->slave_host_id],
764 lcf->state = SLAVE_HOST_REGISTERED;
765 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
768 case SLAVE_HOST_REGISTERED:
769 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
770 host_list[lcf->delegated_host_id],
771 host_list[lcf->slave_host_id],
772 lcf->sxcfg, lcf->sxcfg_size,
774 lcf->is_subordinate);
775 lcf->state = FINISHED;
776 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
780 GNUNET_assert (lcfq->lcf == lcf);
781 GNUNET_free (lcf->sxcfg);
783 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
785 if (NULL != lcfq_head)
787 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
793 * Callback for event from slave controllers
795 * @param cls struct Slave *
796 * @param event information about the event
799 slave_event_callback(void *cls,
800 const struct GNUNET_TESTBED_EventInformation *event)
807 * Callback to signal successfull startup of the controller process
809 * @param cls the closure from GNUNET_TESTBED_controller_start()
810 * @param cfg the configuration with which the controller has been started;
811 * NULL if status is not GNUNET_OK
812 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
813 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
816 slave_status_callback (void *cls,
817 const struct GNUNET_CONFIGURATION_Handle *cfg,
820 struct Slave *slave = cls;
822 if (GNUNET_SYSERR == status)
824 slave->controller_proc = NULL;
825 LOG (GNUNET_ERROR_TYPE_WARNING,
826 "Unexpected slave shutdown\n");
827 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
831 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
832 master_context->event_mask,
833 &slave_event_callback, slave);
838 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
841 * @param client identification of the client
842 * @param message the actual message
845 handle_init (void *cls,
846 struct GNUNET_SERVER_Client *client,
847 const struct GNUNET_MessageHeader *message)
849 const struct GNUNET_TESTBED_InitMessage *msg;
850 struct GNUNET_TESTBED_Host *host;
854 if (NULL != master_context)
857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
860 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
861 master_context = GNUNET_malloc (sizeof (struct Context));
862 master_context->client = client;
863 master_context->host_id = ntohl (msg->host_id);
864 GNUNET_assert (GNUNET_OK ==
865 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
866 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
867 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
868 NULL, 0, NI_NUMERICHOST))
870 LOG (GNUNET_ERROR_TYPE_WARNING,
871 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
873 GNUNET_free (master_context->master_ip);
877 if (0 == strcasecmp (master_context->master_ip, "localhost"))
878 { /* Hack for connections via unix sockets */
879 LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
880 GNUNET_free (master_context->master_ip);
881 master_context->master_ip = strdup ("127.0.0.1");
883 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
884 master_context->system =
885 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
886 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
888 host_list_add (host);
889 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
890 GNUNET_SERVER_client_keep (client);
891 LOG_DEBUG ("Created master context with host ID: %u\n",
892 master_context->host_id);
893 GNUNET_SERVER_receive_done (client, GNUNET_OK);
898 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
901 * @param client identification of the client
902 * @param message the actual message
905 handle_add_host (void *cls,
906 struct GNUNET_SERVER_Client *client,
907 const struct GNUNET_MessageHeader *message)
909 struct GNUNET_TESTBED_Host *host;
910 const struct GNUNET_TESTBED_AddHostMessage *msg;
911 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
916 uint16_t username_length;
917 uint16_t hostname_length;
921 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
922 msize = ntohs (msg->header.size);
923 username = (char *) &(msg[1]);
924 username_length = ntohs (msg->user_name_length);
925 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
926 + username_length + 1)); /* msg must contain hostname */
927 if (0 != username_length)
928 GNUNET_assert ('\0' == username[username_length]);
929 username_length = (0 == username_length) ? 0 : username_length + 1;
930 hostname = username + username_length;
931 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
933 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
934 GNUNET_assert (strlen (hostname) == hostname_length - 1);
935 host_id = ntohl (msg->host_id);
936 LOG_DEBUG ("Received ADDHOST message\n");
937 LOG_DEBUG ("-------host id: %u\n", host_id);
938 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
939 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
940 else LOG_DEBUG ("-------username: NULL\n");
941 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
942 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
943 ntohs (msg->ssh_port));
944 GNUNET_SERVER_receive_done (client, GNUNET_OK);
945 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
946 if (GNUNET_OK != host_list_add (host))
948 /* We are unable to add a host */
949 emsg = "A host exists with given host-id";
950 LOG_DEBUG ("%s: %u", emsg, host_id);
951 GNUNET_TESTBED_host_destroy (host);
952 reply_size += strlen (emsg) + 1;
953 reply = GNUNET_malloc (reply_size);
954 memcpy (&reply[1], emsg, strlen (emsg) + 1);
957 reply = GNUNET_malloc (reply_size);
958 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
959 reply->header.size = htons (reply_size);
960 reply->host_id = htonl (host_id);
961 queue_message (client, (struct GNUNET_MessageHeader *) reply);
966 * Iterator over hash map entries.
969 * @param key current key code
970 * @param value value in the hash map
971 * @return GNUNET_YES if we should continue to
975 int ss_exists_iterator (void *cls,
976 const struct GNUNET_HashCode * key,
979 struct SharedService *queried_ss = cls;
980 struct SharedService *ss = value;
982 if (0 == strcmp (ss->name, queried_ss->name))
990 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
993 * @param client identification of the client
994 * @param message the actual message
997 handle_configure_shared_service (void *cls,
998 struct GNUNET_SERVER_Client *client,
999 const struct GNUNET_MessageHeader *message)
1001 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1002 struct SharedService *ss;
1004 struct GNUNET_HashCode hash;
1006 uint16_t service_name_size;
1008 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1009 msg_size = ntohs (message->size);
1010 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1016 service_name_size = msg_size -
1017 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1018 service_name = (char *) &msg[1];
1019 if ('\0' != service_name[service_name_size - 1])
1022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1025 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1026 service_name, ntohl (msg->num_peers));
1027 if (ntohl (msg->host_id) != master_context->host_id)
1029 route_message (ntohl (msg->host_id), message);
1030 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1034 ss = GNUNET_malloc (sizeof (struct SharedService));
1035 ss->name = strdup (service_name);
1036 ss->num_shared = ntohl (msg->num_peers);
1037 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1038 if (GNUNET_SYSERR ==
1039 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1040 &ss_exists_iterator, ss))
1042 LOG (GNUNET_ERROR_TYPE_WARNING,
1043 "Service %s already configured as a shared service. "
1044 "Ignoring service sharing request \n", ss->name);
1045 GNUNET_free (ss->name);
1049 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1050 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1055 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1058 * @param client identification of the client
1059 * @param message the actual message
1062 handle_link_controllers (void *cls,
1063 struct GNUNET_SERVER_Client *client,
1064 const struct GNUNET_MessageHeader *message)
1066 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1067 struct GNUNET_CONFIGURATION_Handle *cfg;
1068 struct LCFContextQueue *lcfq;
1069 struct Route *route;
1070 struct Route *new_route;
1074 uint32_t delegated_host_id;
1075 uint32_t slave_host_id;
1078 if (NULL == master_context)
1081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1084 msize = ntohs (message->size);
1085 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1091 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1092 delegated_host_id = ntohl (msg->delegated_host_id);
1093 if (delegated_host_id == master_context->host_id)
1096 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1100 if ((delegated_host_id >= host_list_size) ||
1101 (NULL == host_list[delegated_host_id]))
1103 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1107 slave_host_id = ntohl (msg->slave_host_id);
1108 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1110 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1114 if (slave_host_id == delegated_host_id)
1116 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1120 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1121 config_size = ntohs (msg->config_size);
1123 if (slave_host_id == master_context->host_id) /* Link from us */
1125 struct Slave *slave;
1127 if ((delegated_host_id < slave_list_size) &&
1128 (NULL != slave_list[delegated_host_id])) /* We have already added */
1130 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1135 config = GNUNET_malloc (config_size);
1136 dest_size = (uLongf) config_size;
1137 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1138 (const Bytef *) &msg[1], (uLong) msize))
1140 GNUNET_break (0); /* Compression error */
1141 GNUNET_free (config);
1142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1145 if (config_size == dest_size)
1147 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1148 GNUNET_free (config);
1149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1152 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1153 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1156 GNUNET_break (0); /* Configuration parsing error */
1157 GNUNET_free (config);
1158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1161 GNUNET_free (config);
1162 if ((delegated_host_id < slave_list_size) &&
1163 (NULL != slave_list[delegated_host_id]))
1165 GNUNET_break (0); /* Configuration parsing error */
1166 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1169 slave = GNUNET_malloc (sizeof (struct Slave));
1170 slave->host_id = delegated_host_id;
1171 slave_list_add (slave);
1172 if (1 == msg->is_subordinate)
1174 slave->controller_proc =
1175 GNUNET_TESTBED_controller_start (master_context->master_ip,
1176 host_list[slave->host_id],
1177 cfg, &slave_status_callback,
1182 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1183 master_context->event_mask,
1184 &slave_event_callback, slave);
1186 GNUNET_CONFIGURATION_destroy (cfg);
1187 new_route = GNUNET_malloc (sizeof (struct Route));
1188 new_route->dest = delegated_host_id;
1189 new_route->thru = master_context->host_id;
1190 route_list_add (new_route);
1191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1195 /* Route the request */
1196 if (slave_host_id >= route_list_size)
1198 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1202 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1203 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1204 lcfq->lcf->delegated_host_id = delegated_host_id;
1205 lcfq->lcf->slave_host_id = slave_host_id;
1206 while (NULL != (route = route_list[slave_host_id]))
1208 if (route->thru == master_context->host_id)
1210 slave_host_id = route->thru;
1212 GNUNET_assert (NULL != route); /* because we add routes carefully */
1213 GNUNET_assert (route->dest < slave_list_size);
1214 GNUNET_assert (NULL != slave_list[route->dest]);
1215 lcfq->lcf->is_subordinate =
1216 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1217 lcfq->lcf->state = INIT;
1218 lcfq->lcf->gateway = slave_list[route->dest];
1219 lcfq->lcf->sxcfg_size = msize;
1220 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1221 lcfq->lcf->scfg_size = config_size;
1222 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1223 if (NULL == lcfq_head)
1225 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1226 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1227 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1230 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1231 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1232 new_route = GNUNET_malloc (sizeof (struct Route));
1233 new_route->dest = delegated_host_id;
1234 new_route->thru = route->dest;
1235 route_list_add (new_route);
1240 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1243 * @param client identification of the client
1244 * @param message the actual message
1247 handle_peer_create (void *cls,
1248 struct GNUNET_SERVER_Client *client,
1249 const struct GNUNET_MessageHeader *message)
1251 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1252 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1253 struct GNUNET_CONFIGURATION_Handle *cfg;
1257 uint32_t config_size;
1261 msize = ntohs (message->size);
1262 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1264 GNUNET_break (0); /* We need configuration */
1265 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1268 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1269 if (ntohl (msg->host_id) == master_context->host_id)
1274 /* We are responsible for this peer */
1275 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1276 config_size = ntohl (msg->config_size);
1277 config = GNUNET_malloc (config_size);
1278 dest_size = config_size;
1279 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1280 (const Bytef *) &msg[1], (uLong) msize)))
1282 GNUNET_break (0); /* uncompression error */
1283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1286 if (config_size != dest_size)
1288 GNUNET_break (0);/* Uncompressed config size mismatch */
1289 GNUNET_free (config);
1290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1293 cfg = GNUNET_CONFIGURATION_create ();
1294 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1297 GNUNET_break (0); /* Configuration parsing error */
1298 GNUNET_free (config);
1299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1302 GNUNET_free (config);
1303 peer = GNUNET_malloc (sizeof (struct Peer));
1305 peer->id = ntohl (msg->peer_id);
1306 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1307 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1311 if (NULL == peer->peer)
1313 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1317 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1320 peer_list_add (peer);
1321 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1322 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1323 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1324 reply->peer_id = msg->peer_id;
1325 reply->operation_id = msg->operation_id;
1326 queue_message (client, &reply->header);
1327 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1331 /* FIXME: Forward the peer to other host */
1333 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1338 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1341 * @param client identification of the client
1342 * @param message the actual message
1345 handle_peer_destroy (void *cls,
1346 struct GNUNET_SERVER_Client *client,
1347 const struct GNUNET_MessageHeader *message)
1349 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1350 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1353 uint16_t reply_size;
1355 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1356 peer_id = ntohl (msg->peer_id);
1357 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1358 peer_id, GNUNET_ntohll (msg->operation_id));
1359 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1362 /* FIXME: Reply with failure event message or forward to slave controller */
1363 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1366 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1367 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1368 GNUNET_free (peer_list[peer_id]);
1369 peer_list[peer_id] = NULL;
1370 for (id = 0; id < LIST_GROW_STEP; id++)
1372 if (((peer_id + id >= peer_list_size) ||
1373 (NULL != peer_list[peer_id])))
1376 if (LIST_GROW_STEP == id)
1378 peer_list_size -= LIST_GROW_STEP;
1379 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1382 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1383 reply = GNUNET_malloc (reply_size);
1384 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1385 reply->header.size = htons (reply_size);
1386 reply->operation_id = msg->operation_id;
1387 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1388 queue_message (client, &reply->header);
1389 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1394 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1397 * @param client identification of the client
1398 * @param message the actual message
1401 handle_peer_start (void *cls,
1402 struct GNUNET_SERVER_Client *client,
1403 const struct GNUNET_MessageHeader *message)
1405 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1406 struct GNUNET_TESTBED_PeerEventMessage *reply;
1409 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1410 peer_id = ntohl (msg->peer_id);
1411 if ((peer_id >= peer_list_size)
1412 || (NULL == peer_list[peer_id]))
1415 /* FIXME: reply with failure message or forward to slave controller */
1416 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1419 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1421 /* FIXME: return FAILURE message */
1423 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1426 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1427 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1428 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1429 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1430 reply->host_id = htonl (master_context->host_id);
1431 reply->peer_id = msg->peer_id;
1432 reply->operation_id = msg->operation_id;
1433 queue_message (client, &reply->header);
1434 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1439 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1442 * @param client identification of the client
1443 * @param message the actual message
1446 handle_peer_stop (void *cls,
1447 struct GNUNET_SERVER_Client *client,
1448 const struct GNUNET_MessageHeader *message)
1450 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1451 struct GNUNET_TESTBED_PeerEventMessage *reply;
1454 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1455 peer_id = ntohl (msg->peer_id);
1456 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1458 GNUNET_break (0); /* FIXME: route to slave? */
1459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1462 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1464 /* FIXME: return FAILURE message */
1466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1469 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1470 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1471 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1472 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1473 reply->host_id = htonl (master_context->host_id);
1474 reply->peer_id = msg->peer_id;
1475 reply->operation_id = msg->operation_id;
1476 queue_message (client, &reply->header);
1477 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1482 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1485 * @param client identification of the client
1486 * @param message the actual message
1489 handle_peer_get_config (void *cls,
1490 struct GNUNET_SERVER_Client *client,
1491 const struct GNUNET_MessageHeader *message)
1493 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1494 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1502 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1503 peer_id = ntohl (msg->peer_id);
1504 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1506 /* FIXME: return FAILURE message */
1508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1510 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1512 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1513 GNUNET_free (config);
1514 msize = xc_size + sizeof (struct
1515 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1516 reply = GNUNET_realloc (xconfig, msize);
1517 (void) memmove (&reply[1], reply, xc_size);
1518 reply->header.size = htons (msize);
1519 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1520 reply->peer_id = msg->peer_id;
1521 reply->operation_id = msg->operation_id;
1522 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1523 &reply->peer_identity);
1524 reply->config_size = htons ((uint16_t) c_size);
1525 queue_message (client, &reply->header);
1526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1531 * Task to be executed when HELLO has been sent
1533 * @param cls overlay connect context wrapper
1534 * @param tc the task context from scheduler
1537 overlay_connect_finalize (void *cls,
1538 const struct GNUNET_SCHEDULER_TaskContext *tc)
1540 struct OverlayConnectContextWrapper *occw = cls;
1541 struct OverlayConnectContext *occ;
1547 GNUNET_free (occ->peers_hello[peer ^ 1]);
1552 * Exchanges HELLO of peers among them
1554 * @param occ the OverlayConnectContext to look into for HELLO messages
1557 exchange_hellos (struct OverlayConnectContext *occ)
1559 struct OverlayConnectContextWrapper *occw;
1562 GNUNET_assert (NULL != occ->peers_hello[0]);
1563 GNUNET_assert (NULL != occ->peers_hello[1]);
1564 LOG_DEBUG ("Exchanging hellos\n");
1565 for (peer = 0; peer < 2; peer++)
1567 occw = GNUNET_malloc (sizeof (struct OverlayConnectContextWrapper));
1570 GNUNET_TRANSPORT_offer_hello (occ->peers_transport[peer],
1571 occ->peers_hello[peer ^ 1],
1572 &overlay_connect_finalize, occw);
1578 * Function called whenever there is an update to the
1579 * HELLO of peers in the OverlayConnectClosure
1581 * @param cls closure
1582 * @param hello our updated HELLO
1585 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1587 struct OverlayConnectContextWrapper *occw = cls;
1588 struct OverlayConnectContext *occ;
1592 msize = ntohs (hello->size);
1594 GNUNET_assert (peer <= 1);
1596 occ->peers_hello[peer] = GNUNET_malloc (msize);
1597 memcpy (occ->peers_hello[peer], hello, msize);
1598 GNUNET_TRANSPORT_get_hello_cancel (occ->peers_ghh[peer]);
1599 occ->peers_ghh[peer] = NULL;
1600 if (NULL != occ->peers_hello[peer ^ 1])
1601 exchange_hellos (occ);
1606 * Function called to notify transport users that another
1607 * peer connected to us.
1609 * @param cls closure
1610 * @param peer the peer that connected
1611 * @param ats performance data
1612 * @param ats_count number of entries in ats (excluding 0-termination)
1615 overlay_connect_notify (void *cls,
1616 const struct GNUNET_PeerIdentity * peer,
1617 const struct GNUNET_ATS_Information * ats,
1620 GNUNET_break (0); /* To be implemented */
1625 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1628 * @param client identification of the client
1629 * @param message the actual message
1632 handle_overlay_connect (void *cls,
1633 struct GNUNET_SERVER_Client *client,
1634 const struct GNUNET_MessageHeader *message)
1636 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1637 struct OverlayConnectContext *occ;
1638 struct OverlayConnectContextWrapper *occw;
1643 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1644 p1 = ntohl (msg->peer1);
1645 p2 = ntohl (msg->peer2);
1646 GNUNET_assert (p1 < peer_list_size);
1647 GNUNET_assert (NULL != peer_list[p1]);
1648 GNUNET_assert (p2 < peer_list_size);
1649 GNUNET_assert (NULL != peer_list[p2]);
1650 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1651 occ->state = OCC_STATE_INIT;
1652 occ->peers[0] = peer_list[p1];
1653 occ->peers[1] = peer_list[p2];
1654 for (peer = 0; peer < 2; peer++)
1656 occw = GNUNET_malloc (sizeof (struct OverlayConnectContextWrapper));
1659 GNUNET_TESTING_peer_get_identity (occ->peers[peer]->peer,
1660 &occ->peers_identity[peer]);
1661 occ->peers_transport[peer] =
1662 GNUNET_TRANSPORT_connect (occ->peers[peer]->cfg,
1663 &occ->peers_identity[peer], occw, NULL,
1664 &overlay_connect_notify, NULL);
1665 occ->peers_ghh[peer] =
1666 GNUNET_TRANSPORT_get_hello (occ->peers_transport[peer],
1667 &hello_update_cb, occw);
1673 * Iterator over hash map entries.
1675 * @param cls closure
1676 * @param key current key code
1677 * @param value value in the hash map
1678 * @return GNUNET_YES if we should continue to
1683 ss_map_free_iterator (void *cls,
1684 const struct GNUNET_HashCode * key, void *value)
1686 struct SharedService *ss = value;
1688 GNUNET_assert (GNUNET_YES ==
1689 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1690 GNUNET_free (ss->name);
1697 * Task to clean up and shutdown nicely
1700 * @param tc the TaskContext from scheduler
1703 shutdown_task (void *cls,
1704 const struct GNUNET_SCHEDULER_TaskContext *tc)
1706 struct LCFContextQueue *lcfq;
1709 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1710 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1711 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1713 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1714 if (NULL != lcfq_head)
1716 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1718 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1719 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1721 if (NULL != lcfq_head->lcf->rhandle)
1722 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1724 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1725 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1727 GNUNET_free (lcfq->lcf->sxcfg);
1728 GNUNET_free (lcfq->lcf);
1729 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1732 /* Clear peer list */
1733 for (id = 0; id < peer_list_size; id++)
1734 if (NULL != peer_list[id])
1736 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1737 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1738 GNUNET_free (peer_list[id]);
1740 GNUNET_free_non_null (peer_list);
1741 /* Clear host list */
1742 for (id = 0; id < host_list_size; id++)
1743 if (NULL != host_list[id])
1744 GNUNET_TESTBED_host_destroy (host_list[id]);
1745 GNUNET_free_non_null (host_list);
1746 /* Clear route list */
1747 for (id = 0; id < route_list_size; id++)
1748 if (NULL != route_list[id])
1749 GNUNET_free (route_list[id]);
1750 GNUNET_free_non_null (route_list);
1751 /* Clear slave_list */
1752 for (id = 0; id < slave_list_size; id++)
1753 if (NULL != slave_list[id])
1755 GNUNET_assert (NULL != slave_list[id]->controller);
1756 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1757 if (NULL != slave_list[id]->controller_proc)
1758 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1760 if (NULL != master_context)
1762 GNUNET_free_non_null (master_context->master_ip);
1763 if (NULL != master_context->system)
1764 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1765 GNUNET_free (master_context);
1766 master_context = NULL;
1772 * Callback for client disconnect
1775 * @param client the client which has disconnected
1778 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1780 if (NULL == master_context)
1782 if (client == master_context->client)
1784 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1785 GNUNET_SERVER_client_drop (client);
1786 /* should not be needed as we're terminated by failure to read
1787 from stdin, but if stdin fails for some reason, this shouldn't
1788 hurt for now --- might need to revise this later if we ever
1789 decide that master connections might be temporarily down
1791 //GNUNET_SCHEDULER_shutdown ();
1799 * @param cls closure
1800 * @param server the initialized server
1801 * @param cfg configuration to use
1804 testbed_run (void *cls,
1805 struct GNUNET_SERVER_Handle *server,
1806 const struct GNUNET_CONFIGURATION_Handle *cfg)
1808 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1810 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1811 sizeof (struct GNUNET_TESTBED_InitMessage)},
1812 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1813 {&handle_configure_shared_service, NULL,
1814 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1815 {&handle_link_controllers, NULL,
1816 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1817 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1818 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1819 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1820 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1821 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1822 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1823 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1824 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
1825 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1826 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
1827 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
1831 GNUNET_SERVER_add_handlers (server,
1833 GNUNET_SERVER_disconnect_notify (server,
1834 &client_disconnect_cb,
1836 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1838 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1841 LOG_DEBUG ("Testbed startup complete\n");
1846 * The starting point of execution
1848 int main (int argc, char *const *argv)
1852 GNUNET_SERVICE_run (argc,
1855 GNUNET_SERVICE_OPTION_NONE,