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"
33 #include "gnunet_testbed_service.h"
34 #include "testbed_api_hosts.h"
35 #include "gnunet_testing_lib-new.h"
40 #define LOG(kind,...) \
41 GNUNET_log (kind, __VA_ARGS__)
46 #define LOG_DEBUG(...) \
47 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
50 #define LIST_GROW_STEP 10
55 * The client handle associated with this context
57 struct GNUNET_SERVER_Client *client;
60 * Event mask of event to be responded in this context
65 * Our host id according to this context
72 * The message queue for sending messages to clients
77 * The message to be sent
79 struct GNUNET_MessageHeader *msg;
82 * The client to send the message to
84 struct GNUNET_SERVER_Client *client;
87 * next pointer for DLL
89 struct MessageQueue *next;
92 * prev pointer for DLL
94 struct MessageQueue *prev;
99 * The structure for identifying a shared service
104 * The name of the shared service
109 * Number of shared peers per instance of the shared service
114 * Number of peers currently sharing the service
116 uint32_t num_sharing;
131 * The host destination is reachable thru
138 * Structure representing a connected(directly-linked) controller
143 * The controller process handle if we had started the controller
145 struct GNUNET_TESTBED_ControllerProc *controller_proc;
148 * The controller handle
150 struct GNUNET_TESTBED_Controller *controller;
153 * The id of the host this controller is running on
160 * States of LCFContext
165 * The Context has been initialized; Nothing has been done on it
170 * Delegated host has been registered at the forwarding controller
172 DELEGATED_HOST_REGISTERED,
175 * The context has been finished (may have error)
183 * Link controllers request forwarding context
188 * The serialized and compressed configuration
193 * The gateway which will pass the link message to delegated host
195 struct Slave *gateway;
198 * The host registration handle while registered hosts in this context
200 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
203 * The size of the compressed serialized configuration
208 * The size of the uncompressed configuration
213 * Should the delegated host be started by the slave host?
218 * The state of this context
220 enum LCFContextState state;
225 uint32_t delegated_host_id;
232 * Structure of a queue entry in LCFContext request queue
234 struct LCFContextQueue
239 struct LCFContext *lcf;
244 struct LCFContextQueue *next;
249 struct LCFContextQueue *prev;
254 * A locally started peer
259 * The peer handle from testing API
261 struct GNUNET_TESTING_Peer *peer;
264 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
267 struct GNUNET_CONFIGURATION_Handle *cfg;
270 * Our local reference id for this peer
278 * The master context; generated with the first INIT message
280 static struct Context *master_context;
289 static struct GNUNET_DISK_FileHandle *fh;
292 * Current Transmit Handle; NULL if no notify transmit exists currently
294 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
301 * The head for the LCF queue
303 static struct LCFContextQueue *lcfq_head;
306 * The tail for the LCF queue
308 static struct LCFContextQueue *lcfq_tail;
311 * The message queue head
313 static struct MessageQueue *mq_head;
316 * The message queue tail
318 static struct MessageQueue *mq_tail;
323 static struct GNUNET_TESTBED_Host **host_list;
328 static struct Route **route_list;
331 * A list of directly linked neighbours
333 static struct Slave **slave_list;
336 * A list of peers we own locally
338 static struct Peer **peer_list;
341 * The hashmap of shared services
343 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
346 * The size of the host list
348 static uint32_t host_list_size;
351 * The size of the route list
353 static uint32_t route_list_size;
356 * The size of directly linked neighbours list
358 static uint32_t slave_list_size;
361 * The size of the peer list
363 static uint32_t peer_list_size;
370 * The lcf_task handle
372 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
375 * The shutdown task handle
377 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
384 * Handle to the local testing system - for starting peers locally
386 static struct GNUNET_TESTING_System *test_system;
389 * Our configuration; we also use this as template for starting other controllers
391 static struct GNUNET_CONFIGURATION_Handle *config;
395 * Function called to notify a client about the connection begin ready to queue
396 * more data. "buf" will be NULL and "size" zero if the connection was closed
397 * for writing in the meantime.
400 * @param size number of bytes available in buf
401 * @param buf where the callee should write the message
402 * @return number of bytes written to buf
405 transmit_ready_notify (void *cls, size_t size, void *buf)
407 struct MessageQueue *mq_entry;
409 transmit_handle = NULL;
411 GNUNET_assert (NULL != mq_entry);
414 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
415 size = ntohs (mq_entry->msg->size);
416 memcpy (buf, mq_entry->msg, size);
417 GNUNET_free (mq_entry->msg);
418 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
419 GNUNET_free (mq_entry);
421 if (NULL != mq_entry)
423 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
424 ntohs (mq_entry->msg->size),
425 GNUNET_TIME_UNIT_FOREVER_REL,
426 &transmit_ready_notify, NULL);
432 * Queues a message in send queue for sending to the service
434 * @param client the client to whom the queued message has to be sent
435 * @param msg the message to queue
438 queue_message (struct GNUNET_SERVER_Client *client,
439 struct GNUNET_MessageHeader *msg)
441 struct MessageQueue *mq_entry;
445 type = ntohs (msg->type);
446 size = ntohs (msg->size);
447 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
448 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
449 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
451 mq_entry->client = client;
452 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
454 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
455 if (NULL == transmit_handle)
457 GNUNET_SERVER_notify_transmit_ready (client, size,
458 GNUNET_TIME_UNIT_FOREVER_REL,
459 &transmit_ready_notify, NULL);
464 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
466 * @param ptr the memory block to realloc
467 * @param size the size of ptr
468 * @param new_size the size to which ptr has to be realloc'ed
469 * @return the newly reallocated memory block
472 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
474 ptr = GNUNET_realloc (ptr, new_size);
476 ptr = memset (ptr + size, 0, new_size - size);
482 * Function to add a host to the current list of known hosts
484 * @param host the host to add
485 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
489 host_list_add (struct GNUNET_TESTBED_Host *host)
493 host_id = GNUNET_TESTBED_host_get_id_ (host);
494 if (host_list_size <= host_id)
497 TESTBED_realloc (host_list,
498 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
499 sizeof (struct GNUNET_TESTBED_Host *) *
500 (host_list_size + LIST_GROW_STEP));
501 host_list_size += LIST_GROW_STEP;
503 if (NULL != host_list[host_id])
505 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
506 return GNUNET_SYSERR;
508 host_list[host_id] = host;
514 * Adds a route to the route list
516 * @param route the route to add
519 route_list_add (struct Route *route)
521 if (route->dest >= route_list_size)
524 TESTBED_realloc (route_list,
525 sizeof (struct Route *) * route_list_size,
526 sizeof (struct Route *) *
527 (route_list_size + LIST_GROW_STEP));
528 route_list_size += LIST_GROW_STEP;
530 GNUNET_assert (NULL == route_list[route->dest]);
531 route_list[route->dest] = route;
536 * Adds a slave to the slave array
538 * @param route the route to add
541 slave_list_add (struct Slave *slave)
543 if (slave->host_id >= slave_list_size)
545 slave_list = TESTBED_realloc (slave_list,
546 sizeof (struct Slave *) *slave_list_size,
547 sizeof (struct Slave *) *
548 (slave_list_size + LIST_GROW_STEP));
549 slave_list_size += LIST_GROW_STEP;
551 GNUNET_assert (NULL == slave_list[slave->host_id]);
552 slave_list[slave->host_id] = slave;
557 * Adds a peer to the peer array
559 * @param route the route to add
562 peer_list_add (struct Peer *peer)
564 if (peer->id >= peer_list_size)
566 peer_list = TESTBED_realloc (peer_list,
567 sizeof (struct Peer *) * peer_list_size,
568 sizeof (struct Peer *) *
569 (peer_list_size + LIST_GROW_STEP));
570 peer_list_size += LIST_GROW_STEP;
572 GNUNET_assert (NULL == peer_list[peer->id]);
573 peer_list[peer->id] = peer;
578 * Routes message to a host given its host_id
580 * @param host_id the id of the destination host
581 * @param msg the message to be routed
584 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
591 * The Link Controller forwarding task
593 * @param cls the LCFContext
594 * @param tc the Task context from scheduler
597 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
601 * Completion callback for host registrations while forwarding Link Controller messages
603 * @param cls the LCFContext
604 * @param emsg the error message; NULL if host registration is successful
607 lcf_proc_cc (void *cls, const char *emsg)
609 struct LCFContext *lcf = cls;
612 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
618 LOG (GNUNET_ERROR_TYPE_WARNING,
619 "Host registration failed with message: %s\n", emsg);
620 lcf->state = FINISHED;
621 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
624 lcf->state = DELEGATED_HOST_REGISTERED;
625 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
628 GNUNET_assert (0); /* Shouldn't reach here */
634 * The Link Controller forwarding task
636 * @param cls the LCFContext
637 * @param tc the Task context from scheduler
640 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
642 struct LCFContext *lcf = cls;
643 struct LCFContextQueue *lcfq;
645 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
650 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
651 lcf->gateway->controller))
654 GNUNET_TESTBED_register_host (lcf->gateway->controller,
655 host_list[lcf->delegated_host_id],
660 lcf->state = DELEGATED_HOST_REGISTERED;
661 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
664 case DELEGATED_HOST_REGISTERED:
665 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
666 host_list[lcf->delegated_host_id],
667 host_list[lcf->gateway->host_id],
668 lcf->sxcfg, lcf->sxcfg_size,
670 lcf->is_subordinate);
671 lcf->state = FINISHED;
674 GNUNET_assert (lcfq->lcf == lcf);
675 GNUNET_free (lcf->sxcfg);
677 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
679 if (NULL != lcfq_head)
681 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
687 * Callback for event from slave controllers
689 * @param cls struct Slave *
690 * @param event information about the event
693 slave_event_callback(void *cls,
694 const struct GNUNET_TESTBED_EventInformation *event)
701 * Callback for unexpected slave shutdowns
704 * @param emsg error message if available; can be NULL, which does NOT mean
705 * that there was no error
708 slave_shutdown_handler (void *cls, const char *emsg)
712 slave = (struct Slave *) cls;
713 slave->controller_proc = NULL;
714 LOG (GNUNET_ERROR_TYPE_WARNING,
715 "Unexpected slave shutdown\n");
717 LOG (GNUNET_ERROR_TYPE_WARNING, "Error: %s\n", emsg);
718 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
723 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
726 * @param client identification of the client
727 * @param message the actual message
730 handle_init (void *cls,
731 struct GNUNET_SERVER_Client *client,
732 const struct GNUNET_MessageHeader *message)
734 const struct GNUNET_TESTBED_InitMessage *msg;
735 struct GNUNET_TESTBED_Host *host;
737 if (NULL != master_context)
740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
743 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
744 master_context = GNUNET_malloc (sizeof (struct Context));
745 master_context->client = client;
746 master_context->host_id = ntohl (msg->host_id);
747 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
749 host_list_add (host);
750 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
751 GNUNET_SERVER_client_keep (client);
752 LOG_DEBUG ("Created master context with host ID: %u\n",
753 master_context->host_id);
754 GNUNET_SERVER_receive_done (client, GNUNET_OK);
759 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
762 * @param client identification of the client
763 * @param message the actual message
766 handle_add_host (void *cls,
767 struct GNUNET_SERVER_Client *client,
768 const struct GNUNET_MessageHeader *message)
770 struct GNUNET_TESTBED_Host *host;
771 const struct GNUNET_TESTBED_AddHostMessage *msg;
772 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
777 uint16_t username_length;
778 uint16_t hostname_length;
781 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
782 username_length = ntohs (msg->user_name_length);
783 username_length = (0 == username_length) ? 0 : username_length + 1;
784 username = (char *) &(msg[1]);
785 hostname = username + username_length;
786 if (ntohs (message->size) <=
787 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
790 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
793 hostname_length = ntohs (message->size)
794 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
795 if (strlen (hostname) != hostname_length - 1)
798 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
801 host_id = ntohl (msg->host_id);
802 LOG_DEBUG ("Received ADDHOST message\n");
803 LOG_DEBUG ("-------host id: %u\n", host_id);
804 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
805 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
806 else LOG_DEBUG ("-------username: NULL\n");
807 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
808 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
809 ntohs (msg->ssh_port));
810 GNUNET_SERVER_receive_done (client, GNUNET_OK);
811 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
812 if (GNUNET_OK != host_list_add (host))
814 /* We are unable to add a host */
815 emsg = "A host exists with given host-id";
816 LOG_DEBUG ("%s: %u", emsg, host_id);
817 GNUNET_TESTBED_host_destroy (host);
818 reply_size += strlen (emsg) + 1;
819 reply = GNUNET_malloc (reply_size);
820 memcpy (&reply[1], emsg, strlen (emsg) + 1);
823 reply = GNUNET_malloc (reply_size);
824 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
825 reply->header.size = htons (reply_size);
826 reply->host_id = htonl (host_id);
827 queue_message (client, (struct GNUNET_MessageHeader *) reply);
832 * Iterator over hash map entries.
835 * @param key current key code
836 * @param value value in the hash map
837 * @return GNUNET_YES if we should continue to
841 int ss_exists_iterator (void *cls,
842 const struct GNUNET_HashCode * key,
845 struct SharedService *queried_ss = cls;
846 struct SharedService *ss = value;
848 if (0 == strcmp (ss->name, queried_ss->name))
856 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
859 * @param client identification of the client
860 * @param message the actual message
863 handle_configure_shared_service (void *cls,
864 struct GNUNET_SERVER_Client *client,
865 const struct GNUNET_MessageHeader *message)
867 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
868 struct SharedService *ss;
870 struct GNUNET_HashCode hash;
872 uint16_t service_name_size;
874 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
875 msg_size = ntohs (message->size);
876 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
882 service_name_size = msg_size -
883 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
884 service_name = (char *) &msg[1];
885 if ('\0' != service_name[service_name_size - 1])
888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
891 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
892 service_name, ntohl (msg->num_peers));
893 if (ntohl (msg->host_id) != master_context->host_id)
895 route_message (ntohl (msg->host_id), message);
896 GNUNET_SERVER_receive_done (client, GNUNET_OK);
899 GNUNET_SERVER_receive_done (client, GNUNET_OK);
900 ss = GNUNET_malloc (sizeof (struct SharedService));
901 ss->name = strdup (service_name);
902 ss->num_shared = ntohl (msg->num_peers);
903 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
905 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
906 &ss_exists_iterator, ss))
908 LOG (GNUNET_ERROR_TYPE_WARNING,
909 "Service %s already configured as a shared service. "
910 "Ignoring service sharing request \n", ss->name);
911 GNUNET_free (ss->name);
915 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
921 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
924 * @param client identification of the client
925 * @param message the actual message
928 handle_link_controllers (void *cls,
929 struct GNUNET_SERVER_Client *client,
930 const struct GNUNET_MessageHeader *message)
932 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
933 struct GNUNET_CONFIGURATION_Handle *cfg;
934 struct LCFContextQueue *lcfq;
936 struct Route *new_route;
940 uint32_t delegated_host_id;
941 uint32_t slave_host_id;
944 if (NULL == master_context)
947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
950 msize = ntohs (message->size);
951 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
957 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
958 delegated_host_id = ntohl (msg->delegated_host_id);
959 if (delegated_host_id == master_context->host_id)
962 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
966 if ((delegated_host_id >= host_list_size) ||
967 (NULL == host_list[delegated_host_id]))
969 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
973 slave_host_id = ntohl (msg->slave_host_id);
974 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
976 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
980 if (slave_host_id == delegated_host_id)
982 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
986 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
987 config_size = ntohs (msg->config_size);
989 if (slave_host_id == master_context->host_id) /* Link from us */
993 if ((delegated_host_id < slave_list_size) &&
994 (NULL != slave_list[delegated_host_id])) /* We have already added */
996 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1001 config = GNUNET_malloc (config_size);
1002 dest_size = (uLongf) config_size;
1003 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1004 (const Bytef *) &msg[1], (uLong) msize))
1006 GNUNET_break (0); /* Compression error */
1007 GNUNET_free (config);
1008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1011 if (config_size == dest_size)
1013 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1014 GNUNET_free (config);
1015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1017 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1018 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1021 GNUNET_break (0); /* Configuration parsing error */
1022 GNUNET_free (config);
1023 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1026 GNUNET_free (config);
1027 if ((delegated_host_id < slave_list_size) &&
1028 (NULL != slave_list[delegated_host_id]))
1030 GNUNET_break (0); /* Configuration parsing error */
1031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1034 slave = GNUNET_malloc (sizeof (struct Slave));
1035 slave->host_id = delegated_host_id;
1036 slave_list_add (slave);
1037 if (1 == msg->is_subordinate)
1039 slave->controller_proc =
1040 GNUNET_TESTBED_controller_start (test_system,
1041 host_list[delegated_host_id],
1042 cfg, &slave_shutdown_handler,
1046 GNUNET_TESTBED_controller_connect (cfg, host_list[delegated_host_id],
1047 master_context->event_mask,
1048 &slave_event_callback, slave);
1049 GNUNET_CONFIGURATION_destroy (cfg);
1050 new_route = GNUNET_malloc (sizeof (struct Route));
1051 new_route->dest = delegated_host_id;
1052 new_route->thru = master_context->host_id;
1053 route_list_add (new_route);
1054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1058 /* Route the request */
1059 if (slave_host_id >= route_list_size)
1061 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1065 while (NULL != (route = route_list[slave_host_id]))
1067 if (route->thru == master_context->host_id)
1069 slave_host_id = route->thru;
1071 GNUNET_assert (NULL != route); /* because we add routes carefully */
1072 GNUNET_assert (route->dest < slave_list_size);
1073 GNUNET_assert (NULL != slave_list[route->dest]);
1074 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1075 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1076 lcfq->lcf->delegated_host_id = delegated_host_id;
1077 lcfq->lcf->is_subordinate =
1078 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1079 lcfq->lcf->state = INIT;
1080 lcfq->lcf->gateway = slave_list[route->dest];
1081 lcfq->lcf->sxcfg_size = msize;
1082 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1083 lcfq->lcf->scfg_size = config_size;
1084 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1085 if (NULL == lcfq_head)
1087 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1088 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1089 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1092 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1093 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1094 new_route = GNUNET_malloc (sizeof (struct Route));
1095 new_route->dest = delegated_host_id;
1096 new_route->thru = route->dest;
1097 route_list_add (new_route);
1102 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1105 * @param client identification of the client
1106 * @param message the actual message
1109 handle_peer_create (void *cls,
1110 struct GNUNET_SERVER_Client *client,
1111 const struct GNUNET_MessageHeader *message)
1113 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1114 struct GNUNET_CONFIGURATION_Handle *cfg;
1118 uint32_t config_size;
1122 msize = ntohs (message->size);
1123 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1125 GNUNET_break (0); /* We need configuration */
1126 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1129 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1130 if (ntohl (msg->host_id) == master_context->host_id)
1135 /* We are responsible for this peer */
1136 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1137 config_size = ntohl (msg->config_size);
1138 config = GNUNET_malloc (config_size);
1139 dest_size = config_size;
1140 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1141 (const Bytef *) &msg[1], (uLong) msize)))
1143 GNUNET_break (0); /* uncompression error */
1144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1147 if (config_size != dest_size)
1149 GNUNET_break (0);/* Uncompressed config size mismatch */
1150 GNUNET_free (config);
1151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1154 cfg = GNUNET_CONFIGURATION_create ();
1155 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1158 GNUNET_break (0); /* Configuration parsing error */
1159 GNUNET_free (config);
1160 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1163 GNUNET_free (config);
1164 peer = GNUNET_malloc (sizeof (struct Peer));
1166 peer->id = ntohl (msg->peer_id);
1167 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1168 peer->peer = GNUNET_TESTING_peer_configure (test_system, peer->cfg,
1172 if (NULL == peer->peer)
1174 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1180 peer_list_add (peer);
1181 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1185 /* Forward the peer to other host */
1187 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1192 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1195 * @param client identification of the client
1196 * @param message the actual message
1199 handle_peer_destroy (void *cls,
1200 struct GNUNET_SERVER_Client *client,
1201 const struct GNUNET_MessageHeader *message)
1203 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1204 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1207 uint16_t reply_size;
1209 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1210 peer_id = ntohl (msg->peer_id);
1211 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1212 peer_id, GNUNET_ntohll (msg->operation_id));
1213 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1216 /* Reply with failure event message */
1218 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1219 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1220 GNUNET_free (peer_list[peer_id]);
1221 peer_list[peer_id] = NULL;
1222 for (id = 0; id < LIST_GROW_STEP; id++)
1224 if (((peer_id + id >= peer_list_size) ||
1225 (NULL != peer_list[peer_id])))
1228 if (LIST_GROW_STEP == id)
1230 peer_list_size -= LIST_GROW_STEP;
1231 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1234 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1235 reply = GNUNET_malloc (reply_size);
1236 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1237 reply->header.size = htons (reply_size);
1238 reply->operation_id = msg->operation_id;
1239 queue_message (client, (struct GNUNET_MessageHeader *) reply);
1245 * Iterator over hash map entries.
1247 * @param cls closure
1248 * @param key current key code
1249 * @param value value in the hash map
1250 * @return GNUNET_YES if we should continue to
1255 ss_map_free_iterator (void *cls,
1256 const struct GNUNET_HashCode * key, void *value)
1258 struct SharedService *ss = value;
1260 GNUNET_assert (GNUNET_YES ==
1261 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1262 GNUNET_free (ss->name);
1269 * Task to clean up and shutdown nicely
1272 * @param tc the TaskContext from scheduler
1275 shutdown_task (void *cls,
1276 const struct GNUNET_SCHEDULER_TaskContext *tc)
1278 struct LCFContextQueue *lcfq;
1281 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1282 GNUNET_SCHEDULER_shutdown ();
1283 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1284 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1286 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1287 GNUNET_TESTING_system_destroy (test_system, GNUNET_YES);
1290 GNUNET_DISK_file_close (fh);
1293 if (NULL != lcfq_head)
1295 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1297 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1298 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1300 if (NULL != lcfq_head->lcf->rhandle)
1301 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1303 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1304 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1306 GNUNET_free (lcfq->lcf->sxcfg);
1307 GNUNET_free (lcfq->lcf);
1308 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1311 /* Clear peer list */
1312 for (id = 0; id < peer_list_size; id++)
1313 if (NULL != peer_list[id])
1315 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1316 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1317 GNUNET_free (peer_list[id]);
1319 GNUNET_free_non_null (peer_list);
1320 /* Clear host list */
1321 for (id = 0; id < host_list_size; id++)
1322 if (NULL != host_list[id])
1323 GNUNET_TESTBED_host_destroy (host_list[id]);
1324 GNUNET_free_non_null (host_list);
1325 /* Clear route list */
1326 for (id = 0; id < route_list_size; id++)
1327 if (NULL != route_list[id])
1328 GNUNET_free (route_list[id]);
1329 GNUNET_free_non_null (route_list);
1330 /* Clear slave_list */
1331 for (id = 0; id < slave_list_size; id++)
1332 if (NULL != slave_list[id])
1334 GNUNET_assert (NULL != slave_list[id]->controller);
1335 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1336 if (NULL != slave_list[id]->controller_proc)
1337 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1339 GNUNET_free_non_null (master_context);
1344 * Debug shutdown task in case of stdin getting closed
1347 * @param tc the TaskContext from scheduler
1350 shutdown_task_ (void *cls,
1351 const struct GNUNET_SCHEDULER_TaskContext *tc)
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "STDIN closed ...\n");
1354 shutdown_task (cls, tc);
1359 * Callback for client disconnect
1362 * @param client the client which has disconnected
1365 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1367 if (NULL == master_context)
1369 if (client == master_context->client)
1371 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1372 GNUNET_SERVER_client_drop (client);
1373 /* should not be needed as we're terminated by failure to read
1374 from stdin, but if stdin fails for some reason, this shouldn't
1375 hurt for now --- might need to revise this later if we ever
1376 decide that master connections might be temporarily down
1378 GNUNET_SCHEDULER_shutdown ();
1386 * @param cls closure
1387 * @param server the initialized server
1388 * @param cfg configuration to use
1391 testbed_run (void *cls,
1392 struct GNUNET_SERVER_Handle *server,
1393 const struct GNUNET_CONFIGURATION_Handle *cfg)
1395 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1397 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1398 sizeof (struct GNUNET_TESTBED_InitMessage)},
1399 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1400 {&handle_configure_shared_service, NULL,
1401 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1402 {&handle_link_controllers, NULL,
1403 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1404 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1405 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1406 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1410 config = GNUNET_CONFIGURATION_dup (cfg);
1411 GNUNET_SERVER_add_handlers (server,
1413 GNUNET_SERVER_disconnect_notify (server,
1414 &client_disconnect_cb,
1416 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1417 test_system = GNUNET_TESTING_system_create ("testbed", NULL);
1419 fh = GNUNET_DISK_get_handle_from_native (stdin);
1422 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1427 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1431 LOG_DEBUG ("Testbed startup complete\n");
1436 * The starting point of execution
1438 int main (int argc, char *const *argv)
1442 GNUNET_SERVICE_run (argc,
1445 GNUNET_SERVICE_OPTION_NONE,