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"
32 #include "gnunet_testbed_service.h"
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
36 #include "gnunet_testing_lib-new.h"
41 #define LOG(kind,...) \
42 GNUNET_log (kind, __VA_ARGS__)
47 #define LOG_DEBUG(...) \
48 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
51 #define LIST_GROW_STEP 10
56 * The client handle associated with this context
58 struct GNUNET_SERVER_Client *client;
61 * The network address of the master controller
66 * The TESTING system handle for starting peers locally
68 struct GNUNET_TESTING_System *system;
71 * Event mask of event to be responded in this context
76 * Our host id according to this context
83 * The message queue for sending messages to clients
88 * The message to be sent
90 struct GNUNET_MessageHeader *msg;
93 * The client to send the message to
95 struct GNUNET_SERVER_Client *client;
98 * next pointer for DLL
100 struct MessageQueue *next;
103 * prev pointer for DLL
105 struct MessageQueue *prev;
110 * The structure for identifying a shared service
115 * The name of the shared service
120 * Number of shared peers per instance of the shared service
125 * Number of peers currently sharing the service
127 uint32_t num_sharing;
142 * The host destination is reachable thru
149 * Structure representing a connected(directly-linked) controller
154 * The controller process handle if we had started the controller
156 struct GNUNET_TESTBED_ControllerProc *controller_proc;
159 * The controller handle
161 struct GNUNET_TESTBED_Controller *controller;
164 * The id of the host this controller is running on
171 * Slave startup context
176 * The slave corresponding to this context
181 * The configuration used as a template while startup
183 struct GNUNET_CONFIGURATION_Handle *cfg;
188 * States of LCFContext
193 * The Context has been initialized; Nothing has been done on it
198 * Delegated host has been registered at the forwarding controller
200 DELEGATED_HOST_REGISTERED,
203 * The context has been finished (may have error)
211 * Link controllers request forwarding context
216 * The serialized and compressed configuration
221 * The gateway which will pass the link message to delegated host
223 struct Slave *gateway;
226 * The host registration handle while registered hosts in this context
228 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
231 * The size of the compressed serialized configuration
236 * The size of the uncompressed configuration
241 * Should the delegated host be started by the slave host?
246 * The state of this context
248 enum LCFContextState state;
253 uint32_t delegated_host_id;
260 * Structure of a queue entry in LCFContext request queue
262 struct LCFContextQueue
267 struct LCFContext *lcf;
272 struct LCFContextQueue *next;
277 struct LCFContextQueue *prev;
282 * A locally started peer
287 * The peer handle from testing API
289 struct GNUNET_TESTING_Peer *peer;
292 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
295 struct GNUNET_CONFIGURATION_Handle *cfg;
298 * Our local reference id for this peer
306 * The master context; generated with the first INIT message
308 static struct Context *master_context;
317 static struct GNUNET_DISK_FileHandle *fh;
320 * Current Transmit Handle; NULL if no notify transmit exists currently
322 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
329 * The head for the LCF queue
331 static struct LCFContextQueue *lcfq_head;
334 * The tail for the LCF queue
336 static struct LCFContextQueue *lcfq_tail;
339 * The message queue head
341 static struct MessageQueue *mq_head;
344 * The message queue tail
346 static struct MessageQueue *mq_tail;
351 static struct GNUNET_TESTBED_Host **host_list;
356 static struct Route **route_list;
359 * A list of directly linked neighbours
361 static struct Slave **slave_list;
364 * A list of peers we own locally
366 static struct Peer **peer_list;
369 * The hashmap of shared services
371 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
374 * The size of the host list
376 static uint32_t host_list_size;
379 * The size of the route list
381 static uint32_t route_list_size;
384 * The size of directly linked neighbours list
386 static uint32_t slave_list_size;
389 * The size of the peer list
391 static uint32_t peer_list_size;
398 * The lcf_task handle
400 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
403 * The shutdown task handle
405 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
412 * Our configuration; we also use this as template for starting other controllers
414 static struct GNUNET_CONFIGURATION_Handle *config;
418 * Function called to notify a client about the connection begin ready to queue
419 * more data. "buf" will be NULL and "size" zero if the connection was closed
420 * for writing in the meantime.
423 * @param size number of bytes available in buf
424 * @param buf where the callee should write the message
425 * @return number of bytes written to buf
428 transmit_ready_notify (void *cls, size_t size, void *buf)
430 struct MessageQueue *mq_entry;
432 transmit_handle = NULL;
434 GNUNET_assert (NULL != mq_entry);
437 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
438 size = ntohs (mq_entry->msg->size);
439 memcpy (buf, mq_entry->msg, size);
440 GNUNET_free (mq_entry->msg);
441 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
442 GNUNET_free (mq_entry);
444 if (NULL != mq_entry)
446 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
447 ntohs (mq_entry->msg->size),
448 GNUNET_TIME_UNIT_FOREVER_REL,
449 &transmit_ready_notify, NULL);
455 * Queues a message in send queue for sending to the service
457 * @param client the client to whom the queued message has to be sent
458 * @param msg the message to queue
461 queue_message (struct GNUNET_SERVER_Client *client,
462 struct GNUNET_MessageHeader *msg)
464 struct MessageQueue *mq_entry;
468 type = ntohs (msg->type);
469 size = ntohs (msg->size);
470 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
471 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
472 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
474 mq_entry->client = client;
475 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
477 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
478 if (NULL == transmit_handle)
480 GNUNET_SERVER_notify_transmit_ready (client, size,
481 GNUNET_TIME_UNIT_FOREVER_REL,
482 &transmit_ready_notify, NULL);
487 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
489 * @param ptr the memory block to realloc
490 * @param size the size of ptr
491 * @param new_size the size to which ptr has to be realloc'ed
492 * @return the newly reallocated memory block
495 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
497 ptr = GNUNET_realloc (ptr, new_size);
499 ptr = memset (ptr + size, 0, new_size - size);
505 * Function to add a host to the current list of known hosts
507 * @param host the host to add
508 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
512 host_list_add (struct GNUNET_TESTBED_Host *host)
516 host_id = GNUNET_TESTBED_host_get_id_ (host);
517 if (host_list_size <= host_id)
520 TESTBED_realloc (host_list,
521 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
522 sizeof (struct GNUNET_TESTBED_Host *) *
523 (host_list_size + LIST_GROW_STEP));
524 host_list_size += LIST_GROW_STEP;
526 if (NULL != host_list[host_id])
528 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
529 return GNUNET_SYSERR;
531 host_list[host_id] = host;
537 * Adds a route to the route list
539 * @param route the route to add
542 route_list_add (struct Route *route)
544 if (route->dest >= route_list_size)
547 TESTBED_realloc (route_list,
548 sizeof (struct Route *) * route_list_size,
549 sizeof (struct Route *) *
550 (route_list_size + LIST_GROW_STEP));
551 route_list_size += LIST_GROW_STEP;
553 GNUNET_assert (NULL == route_list[route->dest]);
554 route_list[route->dest] = route;
559 * Adds a slave to the slave array
561 * @param slave the slave controller to add
564 slave_list_add (struct Slave *slave)
566 if (slave->host_id >= slave_list_size)
568 slave_list = TESTBED_realloc (slave_list,
569 sizeof (struct Slave *) *slave_list_size,
570 sizeof (struct Slave *) *
571 (slave_list_size + LIST_GROW_STEP));
572 slave_list_size += LIST_GROW_STEP;
574 GNUNET_assert (NULL == slave_list[slave->host_id]);
575 slave_list[slave->host_id] = slave;
580 * Adds a peer to the peer array
582 * @param peer the peer to add
585 peer_list_add (struct Peer *peer)
587 if (peer->id >= peer_list_size)
589 peer_list = TESTBED_realloc (peer_list,
590 sizeof (struct Peer *) * peer_list_size,
591 sizeof (struct Peer *) *
592 (peer_list_size + LIST_GROW_STEP));
593 peer_list_size += LIST_GROW_STEP;
595 GNUNET_assert (NULL == peer_list[peer->id]);
596 peer_list[peer->id] = peer;
601 * Routes message to a host given its host_id
603 * @param host_id the id of the destination host
604 * @param msg the message to be routed
607 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
614 * The Link Controller forwarding task
616 * @param cls the LCFContext
617 * @param tc the Task context from scheduler
620 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
624 * Completion callback for host registrations while forwarding Link Controller messages
626 * @param cls the LCFContext
627 * @param emsg the error message; NULL if host registration is successful
630 lcf_proc_cc (void *cls, const char *emsg)
632 struct LCFContext *lcf = cls;
635 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
641 LOG (GNUNET_ERROR_TYPE_WARNING,
642 "Host registration failed with message: %s\n", emsg);
643 lcf->state = FINISHED;
644 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
647 lcf->state = DELEGATED_HOST_REGISTERED;
648 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
651 GNUNET_assert (0); /* Shouldn't reach here */
657 * The Link Controller forwarding task
659 * @param cls the LCFContext
660 * @param tc the Task context from scheduler
663 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
665 struct LCFContext *lcf = cls;
666 struct LCFContextQueue *lcfq;
668 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
673 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
674 lcf->gateway->controller))
677 GNUNET_TESTBED_register_host (lcf->gateway->controller,
678 host_list[lcf->delegated_host_id],
683 lcf->state = DELEGATED_HOST_REGISTERED;
684 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
687 case DELEGATED_HOST_REGISTERED:
688 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
689 host_list[lcf->delegated_host_id],
690 host_list[lcf->gateway->host_id],
691 lcf->sxcfg, lcf->sxcfg_size,
693 lcf->is_subordinate);
694 lcf->state = FINISHED;
697 GNUNET_assert (lcfq->lcf == lcf);
698 GNUNET_free (lcf->sxcfg);
700 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
702 if (NULL != lcfq_head)
704 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
710 * Callback for event from slave controllers
712 * @param cls struct Slave *
713 * @param event information about the event
716 slave_event_callback(void *cls,
717 const struct GNUNET_TESTBED_EventInformation *event)
724 * Callback to signal successfull startup of the controller process
726 * @param cls the closure from GNUNET_TESTBED_controller_start()
727 * @param cfg the configuration with which the controller has been started;
728 * NULL if status is not GNUNET_OK
729 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
730 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
733 slave_status_callback (void *cls,
734 const struct GNUNET_CONFIGURATION_Handle *cfg,
737 struct SlaveContext *sc = cls;
739 if (GNUNET_SYSERR == status)
741 sc->slave->controller_proc = NULL;
742 LOG (GNUNET_ERROR_TYPE_WARNING,
743 "Unexpected slave shutdown\n");
744 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
747 GNUNET_CONFIGURATION_destroy (sc->cfg);
748 sc->slave->controller =
749 GNUNET_TESTBED_controller_connect (cfg, host_list[sc->slave->host_id],
750 master_context->event_mask,
751 &slave_event_callback, sc->slave);
757 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
760 * @param client identification of the client
761 * @param message the actual message
764 handle_init (void *cls,
765 struct GNUNET_SERVER_Client *client,
766 const struct GNUNET_MessageHeader *message)
768 const struct GNUNET_TESTBED_InitMessage *msg;
769 struct GNUNET_TESTBED_Host *host;
773 if (NULL != master_context)
776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
779 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
780 master_context = GNUNET_malloc (sizeof (struct Context));
781 master_context->client = client;
782 master_context->host_id = ntohl (msg->host_id);
783 GNUNET_assert (GNUNET_OK ==
784 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
785 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
786 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
787 NULL, 0, NI_NUMERICHOST))
789 LOG (GNUNET_ERROR_TYPE_WARNING,
790 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
795 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
796 master_context->system =
797 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
798 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
800 host_list_add (host);
801 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
802 GNUNET_SERVER_client_keep (client);
803 LOG_DEBUG ("Created master context with host ID: %u\n",
804 master_context->host_id);
805 GNUNET_SERVER_receive_done (client, GNUNET_OK);
810 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
813 * @param client identification of the client
814 * @param message the actual message
817 handle_add_host (void *cls,
818 struct GNUNET_SERVER_Client *client,
819 const struct GNUNET_MessageHeader *message)
821 struct GNUNET_TESTBED_Host *host;
822 const struct GNUNET_TESTBED_AddHostMessage *msg;
823 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
828 uint16_t username_length;
829 uint16_t hostname_length;
832 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
833 username_length = ntohs (msg->user_name_length);
834 username_length = (0 == username_length) ? 0 : username_length + 1;
835 username = (char *) &(msg[1]);
836 hostname = username + username_length;
837 if (ntohs (message->size) <=
838 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
841 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
844 hostname_length = ntohs (message->size)
845 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
846 if (strlen (hostname) != hostname_length - 1)
849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
852 host_id = ntohl (msg->host_id);
853 LOG_DEBUG ("Received ADDHOST message\n");
854 LOG_DEBUG ("-------host id: %u\n", host_id);
855 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
856 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
857 else LOG_DEBUG ("-------username: NULL\n");
858 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
859 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
860 ntohs (msg->ssh_port));
861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
862 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
863 if (GNUNET_OK != host_list_add (host))
865 /* We are unable to add a host */
866 emsg = "A host exists with given host-id";
867 LOG_DEBUG ("%s: %u", emsg, host_id);
868 GNUNET_TESTBED_host_destroy (host);
869 reply_size += strlen (emsg) + 1;
870 reply = GNUNET_malloc (reply_size);
871 memcpy (&reply[1], emsg, strlen (emsg) + 1);
874 reply = GNUNET_malloc (reply_size);
875 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
876 reply->header.size = htons (reply_size);
877 reply->host_id = htonl (host_id);
878 queue_message (client, (struct GNUNET_MessageHeader *) reply);
883 * Iterator over hash map entries.
886 * @param key current key code
887 * @param value value in the hash map
888 * @return GNUNET_YES if we should continue to
892 int ss_exists_iterator (void *cls,
893 const struct GNUNET_HashCode * key,
896 struct SharedService *queried_ss = cls;
897 struct SharedService *ss = value;
899 if (0 == strcmp (ss->name, queried_ss->name))
907 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
910 * @param client identification of the client
911 * @param message the actual message
914 handle_configure_shared_service (void *cls,
915 struct GNUNET_SERVER_Client *client,
916 const struct GNUNET_MessageHeader *message)
918 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
919 struct SharedService *ss;
921 struct GNUNET_HashCode hash;
923 uint16_t service_name_size;
925 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
926 msg_size = ntohs (message->size);
927 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
933 service_name_size = msg_size -
934 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
935 service_name = (char *) &msg[1];
936 if ('\0' != service_name[service_name_size - 1])
939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
942 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
943 service_name, ntohl (msg->num_peers));
944 if (ntohl (msg->host_id) != master_context->host_id)
946 route_message (ntohl (msg->host_id), message);
947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
950 GNUNET_SERVER_receive_done (client, GNUNET_OK);
951 ss = GNUNET_malloc (sizeof (struct SharedService));
952 ss->name = strdup (service_name);
953 ss->num_shared = ntohl (msg->num_peers);
954 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
956 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
957 &ss_exists_iterator, ss))
959 LOG (GNUNET_ERROR_TYPE_WARNING,
960 "Service %s already configured as a shared service. "
961 "Ignoring service sharing request \n", ss->name);
962 GNUNET_free (ss->name);
966 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
972 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
975 * @param client identification of the client
976 * @param message the actual message
979 handle_link_controllers (void *cls,
980 struct GNUNET_SERVER_Client *client,
981 const struct GNUNET_MessageHeader *message)
983 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
984 struct GNUNET_CONFIGURATION_Handle *cfg;
985 struct SlaveContext *sc;
986 struct LCFContextQueue *lcfq;
988 struct Route *new_route;
992 uint32_t delegated_host_id;
993 uint32_t slave_host_id;
996 if (NULL == master_context)
999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1002 msize = ntohs (message->size);
1003 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1009 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1010 delegated_host_id = ntohl (msg->delegated_host_id);
1011 if (delegated_host_id == master_context->host_id)
1014 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1018 if ((delegated_host_id >= host_list_size) ||
1019 (NULL == host_list[delegated_host_id]))
1021 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1025 slave_host_id = ntohl (msg->slave_host_id);
1026 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1028 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1032 if (slave_host_id == delegated_host_id)
1034 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1038 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1039 config_size = ntohs (msg->config_size);
1041 if (slave_host_id == master_context->host_id) /* Link from us */
1043 struct Slave *slave;
1045 if ((delegated_host_id < slave_list_size) &&
1046 (NULL != slave_list[delegated_host_id])) /* We have already added */
1048 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1053 config = GNUNET_malloc (config_size);
1054 dest_size = (uLongf) config_size;
1055 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1056 (const Bytef *) &msg[1], (uLong) msize))
1058 GNUNET_break (0); /* Compression error */
1059 GNUNET_free (config);
1060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1063 if (config_size == dest_size)
1065 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1066 GNUNET_free (config);
1067 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1069 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1070 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1073 GNUNET_break (0); /* Configuration parsing error */
1074 GNUNET_free (config);
1075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1078 GNUNET_free (config);
1079 if ((delegated_host_id < slave_list_size) &&
1080 (NULL != slave_list[delegated_host_id]))
1082 GNUNET_break (0); /* Configuration parsing error */
1083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1086 slave = GNUNET_malloc (sizeof (struct Slave));
1087 slave->host_id = delegated_host_id;
1088 slave_list_add (slave);
1089 sc = GNUNET_malloc (sizeof (struct SlaveContext));
1092 if (1 == msg->is_subordinate)
1094 slave->controller_proc =
1095 GNUNET_TESTBED_controller_start (master_context->master_ip,
1096 host_list[slave->host_id],
1097 cfg, &slave_status_callback,
1100 new_route = GNUNET_malloc (sizeof (struct Route));
1101 new_route->dest = delegated_host_id;
1102 new_route->thru = master_context->host_id;
1103 route_list_add (new_route);
1104 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1108 /* Route the request */
1109 if (slave_host_id >= route_list_size)
1111 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1112 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1115 while (NULL != (route = route_list[slave_host_id]))
1117 if (route->thru == master_context->host_id)
1119 slave_host_id = route->thru;
1121 GNUNET_assert (NULL != route); /* because we add routes carefully */
1122 GNUNET_assert (route->dest < slave_list_size);
1123 GNUNET_assert (NULL != slave_list[route->dest]);
1124 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1125 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1126 lcfq->lcf->delegated_host_id = delegated_host_id;
1127 lcfq->lcf->is_subordinate =
1128 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1129 lcfq->lcf->state = INIT;
1130 lcfq->lcf->gateway = slave_list[route->dest];
1131 lcfq->lcf->sxcfg_size = msize;
1132 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1133 lcfq->lcf->scfg_size = config_size;
1134 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1135 if (NULL == lcfq_head)
1137 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1138 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1139 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1142 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1143 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1144 new_route = GNUNET_malloc (sizeof (struct Route));
1145 new_route->dest = delegated_host_id;
1146 new_route->thru = route->dest;
1147 route_list_add (new_route);
1152 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1155 * @param client identification of the client
1156 * @param message the actual message
1159 handle_peer_create (void *cls,
1160 struct GNUNET_SERVER_Client *client,
1161 const struct GNUNET_MessageHeader *message)
1163 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1164 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1165 struct GNUNET_CONFIGURATION_Handle *cfg;
1169 uint32_t config_size;
1173 msize = ntohs (message->size);
1174 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1176 GNUNET_break (0); /* We need configuration */
1177 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1180 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1181 if (ntohl (msg->host_id) == master_context->host_id)
1186 /* We are responsible for this peer */
1187 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1188 config_size = ntohl (msg->config_size);
1189 config = GNUNET_malloc (config_size);
1190 dest_size = config_size;
1191 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1192 (const Bytef *) &msg[1], (uLong) msize)))
1194 GNUNET_break (0); /* uncompression error */
1195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1198 if (config_size != dest_size)
1200 GNUNET_break (0);/* Uncompressed config size mismatch */
1201 GNUNET_free (config);
1202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1205 cfg = GNUNET_CONFIGURATION_create ();
1206 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1209 GNUNET_break (0); /* Configuration parsing error */
1210 GNUNET_free (config);
1211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1214 GNUNET_free (config);
1215 peer = GNUNET_malloc (sizeof (struct Peer));
1217 peer->id = ntohl (msg->peer_id);
1218 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1219 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1223 if (NULL == peer->peer)
1225 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1231 peer_list_add (peer);
1232 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1233 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1234 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1235 reply->peer_id = msg->peer_id;
1236 reply->operation_id = msg->operation_id;
1237 queue_message (client, &reply->header);
1238 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1242 /* FIXME: Forward the peer to other host */
1244 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1249 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1252 * @param client identification of the client
1253 * @param message the actual message
1256 handle_peer_destroy (void *cls,
1257 struct GNUNET_SERVER_Client *client,
1258 const struct GNUNET_MessageHeader *message)
1260 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1261 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1264 uint16_t reply_size;
1266 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1267 peer_id = ntohl (msg->peer_id);
1268 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1269 peer_id, GNUNET_ntohll (msg->operation_id));
1270 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1273 /* FIXME: Reply with failure event message or forward to slave controller */
1274 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1277 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1278 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1279 GNUNET_free (peer_list[peer_id]);
1280 peer_list[peer_id] = NULL;
1281 for (id = 0; id < LIST_GROW_STEP; id++)
1283 if (((peer_id + id >= peer_list_size) ||
1284 (NULL != peer_list[peer_id])))
1287 if (LIST_GROW_STEP == id)
1289 peer_list_size -= LIST_GROW_STEP;
1290 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1293 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1294 reply = GNUNET_malloc (reply_size);
1295 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1296 reply->header.size = htons (reply_size);
1297 reply->operation_id = msg->operation_id;
1298 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1299 queue_message (client, &reply->header);
1300 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1305 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1308 * @param client identification of the client
1309 * @param message the actual message
1312 handle_peer_start (void *cls,
1313 struct GNUNET_SERVER_Client *client,
1314 const struct GNUNET_MessageHeader *message)
1316 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1317 struct GNUNET_TESTBED_PeerEventMessage *reply;
1320 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1321 peer_id = ntohl (msg->peer_id);
1322 if ((peer_id >= peer_list_size)
1323 || (NULL == peer_list[peer_id]))
1326 /* FIXME: reply with failure message or forward to slave controller */
1327 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1330 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1332 /* FIXME: return FAILURE message */
1334 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1337 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1338 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1339 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1340 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1341 reply->host_id = htonl (master_context->host_id);
1342 reply->peer_id = msg->peer_id;
1343 reply->operation_id = msg->operation_id;
1344 queue_message (client, &reply->header);
1345 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1350 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1353 * @param client identification of the client
1354 * @param message the actual message
1357 handle_peer_stop (void *cls,
1358 struct GNUNET_SERVER_Client *client,
1359 const struct GNUNET_MessageHeader *message)
1361 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1362 struct GNUNET_TESTBED_PeerEventMessage *reply;
1365 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1366 peer_id = ntohl (msg->peer_id);
1367 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1369 GNUNET_break (0); /* FIXME: route to slave? */
1370 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1373 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1375 /* FIXME: return FAILURE message */
1377 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1380 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1381 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1382 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1383 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1384 reply->host_id = htonl (master_context->host_id);
1385 reply->peer_id = msg->peer_id;
1386 reply->operation_id = msg->operation_id;
1387 queue_message (client, &reply->header);
1388 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1393 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1396 * @param client identification of the client
1397 * @param message the actual message
1400 handle_peer_get_config (void *cls,
1401 struct GNUNET_SERVER_Client *client,
1402 const struct GNUNET_MessageHeader *message)
1404 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1405 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1413 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1414 peer_id = ntohl (msg->peer_id);
1415 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1417 /* FIXME: return FAILURE message */
1419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1421 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1423 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1424 GNUNET_free (config);
1425 msize = xc_size + sizeof (struct
1426 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1427 reply = GNUNET_realloc (xconfig, msize);
1428 (void) memmove (&reply[1], reply, xc_size);
1429 reply->header.size = htons (msize);
1430 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1431 reply->peer_id = msg->peer_id;
1432 reply->operation_id = msg->operation_id;
1433 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1434 &reply->peer_identity);
1435 reply->config_size = htons ((uint16_t) c_size);
1436 queue_message (client, &reply->header);
1437 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1442 * Iterator over hash map entries.
1444 * @param cls closure
1445 * @param key current key code
1446 * @param value value in the hash map
1447 * @return GNUNET_YES if we should continue to
1452 ss_map_free_iterator (void *cls,
1453 const struct GNUNET_HashCode * key, void *value)
1455 struct SharedService *ss = value;
1457 GNUNET_assert (GNUNET_YES ==
1458 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1459 GNUNET_free (ss->name);
1466 * Task to clean up and shutdown nicely
1469 * @param tc the TaskContext from scheduler
1472 shutdown_task (void *cls,
1473 const struct GNUNET_SCHEDULER_TaskContext *tc)
1475 struct LCFContextQueue *lcfq;
1478 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1479 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1480 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1482 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1485 GNUNET_DISK_file_close (fh);
1488 if (NULL != lcfq_head)
1490 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1492 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1493 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1495 if (NULL != lcfq_head->lcf->rhandle)
1496 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1498 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1499 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1501 GNUNET_free (lcfq->lcf->sxcfg);
1502 GNUNET_free (lcfq->lcf);
1503 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1506 /* Clear peer list */
1507 for (id = 0; id < peer_list_size; id++)
1508 if (NULL != peer_list[id])
1510 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1511 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1512 GNUNET_free (peer_list[id]);
1514 GNUNET_free_non_null (peer_list);
1515 /* Clear host list */
1516 for (id = 0; id < host_list_size; id++)
1517 if (NULL != host_list[id])
1518 GNUNET_TESTBED_host_destroy (host_list[id]);
1519 GNUNET_free_non_null (host_list);
1520 /* Clear route list */
1521 for (id = 0; id < route_list_size; id++)
1522 if (NULL != route_list[id])
1523 GNUNET_free (route_list[id]);
1524 GNUNET_free_non_null (route_list);
1525 /* Clear slave_list */
1526 for (id = 0; id < slave_list_size; id++)
1527 if (NULL != slave_list[id])
1529 GNUNET_assert (NULL != slave_list[id]->controller);
1530 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1531 if (NULL != slave_list[id]->controller_proc)
1532 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1534 if (NULL != master_context)
1536 GNUNET_free_non_null (master_context->master_ip);
1537 if (NULL != master_context->system)
1538 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1544 * Debug shutdown task in case of stdin getting closed
1547 * @param tc the TaskContext from scheduler
1550 shutdown_task_ (void *cls,
1551 const struct GNUNET_SCHEDULER_TaskContext *tc)
1553 LOG (GNUNET_ERROR_TYPE_DEBUG, "STDIN closed ...\n");
1554 shutdown_task (cls, tc);
1559 * Callback for client disconnect
1562 * @param client the client which has disconnected
1565 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1567 if (NULL == master_context)
1569 if (client == master_context->client)
1571 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1572 GNUNET_SERVER_client_drop (client);
1573 /* should not be needed as we're terminated by failure to read
1574 from stdin, but if stdin fails for some reason, this shouldn't
1575 hurt for now --- might need to revise this later if we ever
1576 decide that master connections might be temporarily down
1578 GNUNET_SCHEDULER_shutdown ();
1586 * @param cls closure
1587 * @param server the initialized server
1588 * @param cfg configuration to use
1591 testbed_run (void *cls,
1592 struct GNUNET_SERVER_Handle *server,
1593 const struct GNUNET_CONFIGURATION_Handle *cfg)
1595 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1597 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1598 sizeof (struct GNUNET_TESTBED_InitMessage)},
1599 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1600 {&handle_configure_shared_service, NULL,
1601 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1602 {&handle_link_controllers, NULL,
1603 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1604 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1605 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1606 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1607 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1608 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1609 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1610 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1611 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
1612 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1616 config = GNUNET_CONFIGURATION_dup (cfg);
1617 GNUNET_SERVER_add_handlers (server,
1619 GNUNET_SERVER_disconnect_notify (server,
1620 &client_disconnect_cb,
1622 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1623 fh = GNUNET_DISK_get_handle_from_native (stdin);
1626 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1631 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1635 LOG_DEBUG ("Testbed startup complete\n");
1640 * The starting point of execution
1642 int main (int argc, char *const *argv)
1646 GNUNET_SERVICE_run (argc,
1649 GNUNET_SERVICE_OPTION_NONE,