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 context has been finished (may have error)
195 * Link controllers request forwarding context
200 * The serialized and compressed configuration
205 * The gateway which will pass the link message to delegated host
207 struct Slave *gateway;
210 * The host registration handle while registered hosts in this context
212 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
215 * The size of the compressed serialized configuration
220 * The size of the uncompressed configuration
225 * Should the delegated host be started by the slave host?
230 * The state of this context
232 enum LCFContextState state;
237 uint32_t delegated_host_id;
244 * Structure of a queue entry in LCFContext request queue
246 struct LCFContextQueue
251 struct LCFContext *lcf;
256 struct LCFContextQueue *next;
261 struct LCFContextQueue *prev;
266 * A locally started peer
271 * The peer handle from testing API
273 struct GNUNET_TESTING_Peer *peer;
276 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
279 struct GNUNET_CONFIGURATION_Handle *cfg;
282 * Our local reference id for this peer
290 * Context information for connecting 2 peers in overlay
292 struct OverlayConnectContext
305 * Transport handle of peer1
307 struct GNUNET_TRANSPORT_Handle *peer1_transport;
310 * Transport handle of peer2
312 struct GNUNET_TRANSPORT_Handle *peer2_transport;
317 struct GNUNET_MessageHeader *peer1_hello;
322 struct GNUNET_MessageHeader *peer2_hello;
325 * Get hello handle for peer1
327 struct GNUNET_TRANSPORT_GetHelloHandle *peer1_ghh;
330 * Get hello handle for peer2
332 struct GNUNET_TRANSPORT_GetHelloHandle *peer2_ghh;
337 * The master context; generated with the first INIT message
339 static struct Context *master_context;
346 * Current Transmit Handle; NULL if no notify transmit exists currently
348 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
355 * The head for the LCF queue
357 static struct LCFContextQueue *lcfq_head;
360 * The tail for the LCF queue
362 static struct LCFContextQueue *lcfq_tail;
365 * The message queue head
367 static struct MessageQueue *mq_head;
370 * The message queue tail
372 static struct MessageQueue *mq_tail;
377 static struct GNUNET_TESTBED_Host **host_list;
382 static struct Route **route_list;
385 * A list of directly linked neighbours
387 static struct Slave **slave_list;
390 * A list of peers we own locally
392 static struct Peer **peer_list;
395 * The hashmap of shared services
397 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
400 * The size of the host list
402 static uint32_t host_list_size;
405 * The size of the route list
407 static uint32_t route_list_size;
410 * The size of directly linked neighbours list
412 static uint32_t slave_list_size;
415 * The size of the peer list
417 static uint32_t peer_list_size;
424 * The lcf_task handle
426 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
429 * The shutdown task handle
431 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
435 * Function called to notify a client about the connection begin ready to queue
436 * more data. "buf" will be NULL and "size" zero if the connection was closed
437 * for writing in the meantime.
440 * @param size number of bytes available in buf
441 * @param buf where the callee should write the message
442 * @return number of bytes written to buf
445 transmit_ready_notify (void *cls, size_t size, void *buf)
447 struct MessageQueue *mq_entry;
449 transmit_handle = NULL;
451 GNUNET_assert (NULL != mq_entry);
454 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
455 size = ntohs (mq_entry->msg->size);
456 memcpy (buf, mq_entry->msg, size);
457 GNUNET_free (mq_entry->msg);
458 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
459 GNUNET_free (mq_entry);
461 if (NULL != mq_entry)
463 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
464 ntohs (mq_entry->msg->size),
465 GNUNET_TIME_UNIT_FOREVER_REL,
466 &transmit_ready_notify, NULL);
472 * Queues a message in send queue for sending to the service
474 * @param client the client to whom the queued message has to be sent
475 * @param msg the message to queue
478 queue_message (struct GNUNET_SERVER_Client *client,
479 struct GNUNET_MessageHeader *msg)
481 struct MessageQueue *mq_entry;
485 type = ntohs (msg->type);
486 size = ntohs (msg->size);
487 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
488 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
489 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
491 mq_entry->client = client;
492 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
494 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
495 if (NULL == transmit_handle)
497 GNUNET_SERVER_notify_transmit_ready (client, size,
498 GNUNET_TIME_UNIT_FOREVER_REL,
499 &transmit_ready_notify, NULL);
504 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
506 * @param ptr the memory block to realloc
507 * @param size the size of ptr
508 * @param new_size the size to which ptr has to be realloc'ed
509 * @return the newly reallocated memory block
512 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
514 ptr = GNUNET_realloc (ptr, new_size);
516 ptr = memset (ptr + size, 0, new_size - size);
522 * Function to add a host to the current list of known hosts
524 * @param host the host to add
525 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
529 host_list_add (struct GNUNET_TESTBED_Host *host)
533 host_id = GNUNET_TESTBED_host_get_id_ (host);
534 if (host_list_size <= host_id)
537 TESTBED_realloc (host_list,
538 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
539 sizeof (struct GNUNET_TESTBED_Host *) *
540 (host_list_size + LIST_GROW_STEP));
541 host_list_size += LIST_GROW_STEP;
543 if (NULL != host_list[host_id])
545 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
546 return GNUNET_SYSERR;
548 host_list[host_id] = host;
554 * Adds a route to the route list
556 * @param route the route to add
559 route_list_add (struct Route *route)
561 if (route->dest >= route_list_size)
564 TESTBED_realloc (route_list,
565 sizeof (struct Route *) * route_list_size,
566 sizeof (struct Route *) *
567 (route_list_size + LIST_GROW_STEP));
568 route_list_size += LIST_GROW_STEP;
570 GNUNET_assert (NULL == route_list[route->dest]);
571 route_list[route->dest] = route;
576 * Adds a slave to the slave array
578 * @param slave the slave controller to add
581 slave_list_add (struct Slave *slave)
583 if (slave->host_id >= slave_list_size)
585 slave_list = TESTBED_realloc (slave_list,
586 sizeof (struct Slave *) *slave_list_size,
587 sizeof (struct Slave *) *
588 (slave_list_size + LIST_GROW_STEP));
589 slave_list_size += LIST_GROW_STEP;
591 GNUNET_assert (NULL == slave_list[slave->host_id]);
592 slave_list[slave->host_id] = slave;
597 * Adds a peer to the peer array
599 * @param peer the peer to add
602 peer_list_add (struct Peer *peer)
604 if (peer->id >= peer_list_size)
606 peer_list = TESTBED_realloc (peer_list,
607 sizeof (struct Peer *) * peer_list_size,
608 sizeof (struct Peer *) *
609 (peer_list_size + LIST_GROW_STEP));
610 peer_list_size += LIST_GROW_STEP;
612 GNUNET_assert (NULL == peer_list[peer->id]);
613 peer_list[peer->id] = peer;
618 * Routes message to a host given its host_id
620 * @param host_id the id of the destination host
621 * @param msg the message to be routed
624 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
631 * The Link Controller forwarding task
633 * @param cls the LCFContext
634 * @param tc the Task context from scheduler
637 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
641 * Completion callback for host registrations while forwarding Link Controller messages
643 * @param cls the LCFContext
644 * @param emsg the error message; NULL if host registration is successful
647 lcf_proc_cc (void *cls, const char *emsg)
649 struct LCFContext *lcf = cls;
652 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
658 LOG (GNUNET_ERROR_TYPE_WARNING,
659 "Host registration failed with message: %s\n", emsg);
660 lcf->state = FINISHED;
661 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
664 lcf->state = DELEGATED_HOST_REGISTERED;
665 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
668 GNUNET_assert (0); /* Shouldn't reach here */
674 * The Link Controller forwarding task
676 * @param cls the LCFContext
677 * @param tc the Task context from scheduler
680 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
682 struct LCFContext *lcf = cls;
683 struct LCFContextQueue *lcfq;
685 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
690 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
691 lcf->gateway->controller))
694 GNUNET_TESTBED_register_host (lcf->gateway->controller,
695 host_list[lcf->delegated_host_id],
700 lcf->state = DELEGATED_HOST_REGISTERED;
701 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
704 case DELEGATED_HOST_REGISTERED:
705 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
706 host_list[lcf->delegated_host_id],
707 host_list[lcf->gateway->host_id],
708 lcf->sxcfg, lcf->sxcfg_size,
710 lcf->is_subordinate);
711 lcf->state = FINISHED;
714 GNUNET_assert (lcfq->lcf == lcf);
715 GNUNET_free (lcf->sxcfg);
717 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
719 if (NULL != lcfq_head)
721 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
727 * Callback for event from slave controllers
729 * @param cls struct Slave *
730 * @param event information about the event
733 slave_event_callback(void *cls,
734 const struct GNUNET_TESTBED_EventInformation *event)
741 * Callback to signal successfull startup of the controller process
743 * @param cls the closure from GNUNET_TESTBED_controller_start()
744 * @param cfg the configuration with which the controller has been started;
745 * NULL if status is not GNUNET_OK
746 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
747 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
750 slave_status_callback (void *cls,
751 const struct GNUNET_CONFIGURATION_Handle *cfg,
754 struct Slave *slave = cls;
756 if (GNUNET_SYSERR == status)
758 slave->controller_proc = NULL;
759 LOG (GNUNET_ERROR_TYPE_WARNING,
760 "Unexpected slave shutdown\n");
761 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
765 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
766 master_context->event_mask,
767 &slave_event_callback, slave);
772 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
775 * @param client identification of the client
776 * @param message the actual message
779 handle_init (void *cls,
780 struct GNUNET_SERVER_Client *client,
781 const struct GNUNET_MessageHeader *message)
783 const struct GNUNET_TESTBED_InitMessage *msg;
784 struct GNUNET_TESTBED_Host *host;
788 if (NULL != master_context)
791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
794 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
795 master_context = GNUNET_malloc (sizeof (struct Context));
796 master_context->client = client;
797 master_context->host_id = ntohl (msg->host_id);
798 GNUNET_assert (GNUNET_OK ==
799 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
800 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
801 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
802 NULL, 0, NI_NUMERICHOST))
804 LOG (GNUNET_ERROR_TYPE_WARNING,
805 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
807 GNUNET_free (master_context->master_ip);
811 if (0 == strcasecmp (master_context->master_ip, "localhost"))
812 { /* Hack for connections via unix sockets */
813 LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
814 GNUNET_free (master_context->master_ip);
815 master_context->master_ip = strdup ("127.0.0.1");
817 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
818 master_context->system =
819 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
820 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
822 host_list_add (host);
823 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
824 GNUNET_SERVER_client_keep (client);
825 LOG_DEBUG ("Created master context with host ID: %u\n",
826 master_context->host_id);
827 GNUNET_SERVER_receive_done (client, GNUNET_OK);
832 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
835 * @param client identification of the client
836 * @param message the actual message
839 handle_add_host (void *cls,
840 struct GNUNET_SERVER_Client *client,
841 const struct GNUNET_MessageHeader *message)
843 struct GNUNET_TESTBED_Host *host;
844 const struct GNUNET_TESTBED_AddHostMessage *msg;
845 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
850 uint16_t username_length;
851 uint16_t hostname_length;
854 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
855 username_length = ntohs (msg->user_name_length);
856 username_length = (0 == username_length) ? 0 : username_length + 1;
857 username = (char *) &(msg[1]);
858 hostname = username + username_length;
859 if (ntohs (message->size) <=
860 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
866 hostname_length = ntohs (message->size)
867 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
868 if (strlen (hostname) != hostname_length - 1)
871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
874 host_id = ntohl (msg->host_id);
875 LOG_DEBUG ("Received ADDHOST message\n");
876 LOG_DEBUG ("-------host id: %u\n", host_id);
877 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
878 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
879 else LOG_DEBUG ("-------username: NULL\n");
880 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
881 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
882 ntohs (msg->ssh_port));
883 GNUNET_SERVER_receive_done (client, GNUNET_OK);
884 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
885 if (GNUNET_OK != host_list_add (host))
887 /* We are unable to add a host */
888 emsg = "A host exists with given host-id";
889 LOG_DEBUG ("%s: %u", emsg, host_id);
890 GNUNET_TESTBED_host_destroy (host);
891 reply_size += strlen (emsg) + 1;
892 reply = GNUNET_malloc (reply_size);
893 memcpy (&reply[1], emsg, strlen (emsg) + 1);
896 reply = GNUNET_malloc (reply_size);
897 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
898 reply->header.size = htons (reply_size);
899 reply->host_id = htonl (host_id);
900 queue_message (client, (struct GNUNET_MessageHeader *) reply);
905 * Iterator over hash map entries.
908 * @param key current key code
909 * @param value value in the hash map
910 * @return GNUNET_YES if we should continue to
914 int ss_exists_iterator (void *cls,
915 const struct GNUNET_HashCode * key,
918 struct SharedService *queried_ss = cls;
919 struct SharedService *ss = value;
921 if (0 == strcmp (ss->name, queried_ss->name))
929 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
932 * @param client identification of the client
933 * @param message the actual message
936 handle_configure_shared_service (void *cls,
937 struct GNUNET_SERVER_Client *client,
938 const struct GNUNET_MessageHeader *message)
940 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
941 struct SharedService *ss;
943 struct GNUNET_HashCode hash;
945 uint16_t service_name_size;
947 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
948 msg_size = ntohs (message->size);
949 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
955 service_name_size = msg_size -
956 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
957 service_name = (char *) &msg[1];
958 if ('\0' != service_name[service_name_size - 1])
961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
964 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
965 service_name, ntohl (msg->num_peers));
966 if (ntohl (msg->host_id) != master_context->host_id)
968 route_message (ntohl (msg->host_id), message);
969 GNUNET_SERVER_receive_done (client, GNUNET_OK);
972 GNUNET_SERVER_receive_done (client, GNUNET_OK);
973 ss = GNUNET_malloc (sizeof (struct SharedService));
974 ss->name = strdup (service_name);
975 ss->num_shared = ntohl (msg->num_peers);
976 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
978 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
979 &ss_exists_iterator, ss))
981 LOG (GNUNET_ERROR_TYPE_WARNING,
982 "Service %s already configured as a shared service. "
983 "Ignoring service sharing request \n", ss->name);
984 GNUNET_free (ss->name);
988 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
989 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
994 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
997 * @param client identification of the client
998 * @param message the actual message
1001 handle_link_controllers (void *cls,
1002 struct GNUNET_SERVER_Client *client,
1003 const struct GNUNET_MessageHeader *message)
1005 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1006 struct GNUNET_CONFIGURATION_Handle *cfg;
1007 struct LCFContextQueue *lcfq;
1008 struct Route *route;
1009 struct Route *new_route;
1013 uint32_t delegated_host_id;
1014 uint32_t slave_host_id;
1017 if (NULL == master_context)
1020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1023 msize = ntohs (message->size);
1024 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1030 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1031 delegated_host_id = ntohl (msg->delegated_host_id);
1032 if (delegated_host_id == master_context->host_id)
1035 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1036 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1039 if ((delegated_host_id >= host_list_size) ||
1040 (NULL == host_list[delegated_host_id]))
1042 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1046 slave_host_id = ntohl (msg->slave_host_id);
1047 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1049 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1053 if (slave_host_id == delegated_host_id)
1055 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1059 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1060 config_size = ntohs (msg->config_size);
1062 if (slave_host_id == master_context->host_id) /* Link from us */
1064 struct Slave *slave;
1066 if ((delegated_host_id < slave_list_size) &&
1067 (NULL != slave_list[delegated_host_id])) /* We have already added */
1069 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1074 config = GNUNET_malloc (config_size);
1075 dest_size = (uLongf) config_size;
1076 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1077 (const Bytef *) &msg[1], (uLong) msize))
1079 GNUNET_break (0); /* Compression error */
1080 GNUNET_free (config);
1081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1084 if (config_size == dest_size)
1086 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1087 GNUNET_free (config);
1088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1090 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1091 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1094 GNUNET_break (0); /* Configuration parsing error */
1095 GNUNET_free (config);
1096 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1099 GNUNET_free (config);
1100 if ((delegated_host_id < slave_list_size) &&
1101 (NULL != slave_list[delegated_host_id]))
1103 GNUNET_break (0); /* Configuration parsing error */
1104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1107 slave = GNUNET_malloc (sizeof (struct Slave));
1108 slave->host_id = delegated_host_id;
1109 slave_list_add (slave);
1110 if (1 == msg->is_subordinate)
1112 slave->controller_proc =
1113 GNUNET_TESTBED_controller_start (master_context->master_ip,
1114 host_list[slave->host_id],
1115 cfg, &slave_status_callback,
1120 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1121 master_context->event_mask,
1122 &slave_event_callback, slave);
1124 GNUNET_CONFIGURATION_destroy (cfg);
1125 new_route = GNUNET_malloc (sizeof (struct Route));
1126 new_route->dest = delegated_host_id;
1127 new_route->thru = master_context->host_id;
1128 route_list_add (new_route);
1129 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1133 /* Route the request */
1134 if (slave_host_id >= route_list_size)
1136 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1140 while (NULL != (route = route_list[slave_host_id]))
1142 if (route->thru == master_context->host_id)
1144 slave_host_id = route->thru;
1146 GNUNET_assert (NULL != route); /* because we add routes carefully */
1147 GNUNET_assert (route->dest < slave_list_size);
1148 GNUNET_assert (NULL != slave_list[route->dest]);
1149 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1150 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1151 lcfq->lcf->delegated_host_id = delegated_host_id;
1152 lcfq->lcf->is_subordinate =
1153 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1154 lcfq->lcf->state = INIT;
1155 lcfq->lcf->gateway = slave_list[route->dest];
1156 lcfq->lcf->sxcfg_size = msize;
1157 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1158 lcfq->lcf->scfg_size = config_size;
1159 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1160 if (NULL == lcfq_head)
1162 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1163 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1164 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1167 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1168 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1169 new_route = GNUNET_malloc (sizeof (struct Route));
1170 new_route->dest = delegated_host_id;
1171 new_route->thru = route->dest;
1172 route_list_add (new_route);
1177 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1180 * @param client identification of the client
1181 * @param message the actual message
1184 handle_peer_create (void *cls,
1185 struct GNUNET_SERVER_Client *client,
1186 const struct GNUNET_MessageHeader *message)
1188 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1189 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1190 struct GNUNET_CONFIGURATION_Handle *cfg;
1194 uint32_t config_size;
1198 msize = ntohs (message->size);
1199 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1201 GNUNET_break (0); /* We need configuration */
1202 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1205 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1206 if (ntohl (msg->host_id) == master_context->host_id)
1211 /* We are responsible for this peer */
1212 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1213 config_size = ntohl (msg->config_size);
1214 config = GNUNET_malloc (config_size);
1215 dest_size = config_size;
1216 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1217 (const Bytef *) &msg[1], (uLong) msize)))
1219 GNUNET_break (0); /* uncompression error */
1220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1223 if (config_size != dest_size)
1225 GNUNET_break (0);/* Uncompressed config size mismatch */
1226 GNUNET_free (config);
1227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1230 cfg = GNUNET_CONFIGURATION_create ();
1231 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1234 GNUNET_break (0); /* Configuration parsing error */
1235 GNUNET_free (config);
1236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1239 GNUNET_free (config);
1240 peer = GNUNET_malloc (sizeof (struct Peer));
1242 peer->id = ntohl (msg->peer_id);
1243 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1244 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1248 if (NULL == peer->peer)
1250 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1256 peer_list_add (peer);
1257 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1258 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1259 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1260 reply->peer_id = msg->peer_id;
1261 reply->operation_id = msg->operation_id;
1262 queue_message (client, &reply->header);
1263 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1267 /* FIXME: Forward the peer to other host */
1269 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1274 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1277 * @param client identification of the client
1278 * @param message the actual message
1281 handle_peer_destroy (void *cls,
1282 struct GNUNET_SERVER_Client *client,
1283 const struct GNUNET_MessageHeader *message)
1285 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1286 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1289 uint16_t reply_size;
1291 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1292 peer_id = ntohl (msg->peer_id);
1293 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1294 peer_id, GNUNET_ntohll (msg->operation_id));
1295 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1298 /* FIXME: Reply with failure event message or forward to slave controller */
1299 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1302 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1303 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1304 GNUNET_free (peer_list[peer_id]);
1305 peer_list[peer_id] = NULL;
1306 for (id = 0; id < LIST_GROW_STEP; id++)
1308 if (((peer_id + id >= peer_list_size) ||
1309 (NULL != peer_list[peer_id])))
1312 if (LIST_GROW_STEP == id)
1314 peer_list_size -= LIST_GROW_STEP;
1315 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1318 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1319 reply = GNUNET_malloc (reply_size);
1320 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1321 reply->header.size = htons (reply_size);
1322 reply->operation_id = msg->operation_id;
1323 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1324 queue_message (client, &reply->header);
1325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1330 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1333 * @param client identification of the client
1334 * @param message the actual message
1337 handle_peer_start (void *cls,
1338 struct GNUNET_SERVER_Client *client,
1339 const struct GNUNET_MessageHeader *message)
1341 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1342 struct GNUNET_TESTBED_PeerEventMessage *reply;
1345 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1346 peer_id = ntohl (msg->peer_id);
1347 if ((peer_id >= peer_list_size)
1348 || (NULL == peer_list[peer_id]))
1351 /* FIXME: reply with failure message or forward to slave controller */
1352 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1355 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1357 /* FIXME: return FAILURE message */
1359 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1362 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1363 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1364 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1365 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1366 reply->host_id = htonl (master_context->host_id);
1367 reply->peer_id = msg->peer_id;
1368 reply->operation_id = msg->operation_id;
1369 queue_message (client, &reply->header);
1370 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1375 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1378 * @param client identification of the client
1379 * @param message the actual message
1382 handle_peer_stop (void *cls,
1383 struct GNUNET_SERVER_Client *client,
1384 const struct GNUNET_MessageHeader *message)
1386 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1387 struct GNUNET_TESTBED_PeerEventMessage *reply;
1390 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1391 peer_id = ntohl (msg->peer_id);
1392 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1394 GNUNET_break (0); /* FIXME: route to slave? */
1395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1398 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1400 /* FIXME: return FAILURE message */
1402 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1405 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1406 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1407 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1408 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1409 reply->host_id = htonl (master_context->host_id);
1410 reply->peer_id = msg->peer_id;
1411 reply->operation_id = msg->operation_id;
1412 queue_message (client, &reply->header);
1413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1418 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1421 * @param client identification of the client
1422 * @param message the actual message
1425 handle_peer_get_config (void *cls,
1426 struct GNUNET_SERVER_Client *client,
1427 const struct GNUNET_MessageHeader *message)
1429 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1430 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1438 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1439 peer_id = ntohl (msg->peer_id);
1440 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1442 /* FIXME: return FAILURE message */
1444 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1446 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1448 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1449 GNUNET_free (config);
1450 msize = xc_size + sizeof (struct
1451 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1452 reply = GNUNET_realloc (xconfig, msize);
1453 (void) memmove (&reply[1], reply, xc_size);
1454 reply->header.size = htons (msize);
1455 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1456 reply->peer_id = msg->peer_id;
1457 reply->operation_id = msg->operation_id;
1458 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1459 &reply->peer_identity);
1460 reply->config_size = htons ((uint16_t) c_size);
1461 queue_message (client, &reply->header);
1462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1468 * Function called whenever there is an update to the
1469 * HELLO of this peer.
1471 * @param cls closure
1472 * @param hello our updated HELLO
1475 hello_update_cb (void *cls,
1476 const struct GNUNET_MessageHeader *hello)
1483 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1486 * @param client identification of the client
1487 * @param message the actual message
1490 handle_overlay_connect (void *cls,
1491 struct GNUNET_SERVER_Client *client,
1492 const struct GNUNET_MessageHeader *message)
1494 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1495 struct OverlayConnectContext *occ;
1499 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1500 p1 = ntohl (msg->peer1);
1501 p2 = ntohl (msg->peer2);
1502 GNUNET_assert (p1 < peer_list_size);
1503 GNUNET_assert (NULL != peer_list[p1]);
1504 GNUNET_assert (p2 < peer_list_size);
1505 GNUNET_assert (NULL != peer_list[p2]);
1506 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1507 occ->peer1 = peer_list[p1];
1508 occ->peer2 = peer_list[p2];
1509 occ->peer1_transport = GNUNET_TRANSPORT_connect (occ->peer1->cfg, NULL, occ,
1511 occ->peer2_transport = GNUNET_TRANSPORT_connect (occ->peer2->cfg, NULL, occ,
1513 occ->peer1_ghh = GNUNET_TRANSPORT_get_hello (occ->peer1_transport, &hello_update_cb, occ);
1514 occ->peer2_ghh = GNUNET_TRANSPORT_get_hello (occ->peer2_transport, &hello_update_cb, occ);
1519 * Iterator over hash map entries.
1521 * @param cls closure
1522 * @param key current key code
1523 * @param value value in the hash map
1524 * @return GNUNET_YES if we should continue to
1529 ss_map_free_iterator (void *cls,
1530 const struct GNUNET_HashCode * key, void *value)
1532 struct SharedService *ss = value;
1534 GNUNET_assert (GNUNET_YES ==
1535 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1536 GNUNET_free (ss->name);
1543 * Task to clean up and shutdown nicely
1546 * @param tc the TaskContext from scheduler
1549 shutdown_task (void *cls,
1550 const struct GNUNET_SCHEDULER_TaskContext *tc)
1552 struct LCFContextQueue *lcfq;
1555 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1556 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1557 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1559 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1560 if (NULL != lcfq_head)
1562 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1564 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1565 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1567 if (NULL != lcfq_head->lcf->rhandle)
1568 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1570 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1571 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1573 GNUNET_free (lcfq->lcf->sxcfg);
1574 GNUNET_free (lcfq->lcf);
1575 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1578 /* Clear peer list */
1579 for (id = 0; id < peer_list_size; id++)
1580 if (NULL != peer_list[id])
1582 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1583 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1584 GNUNET_free (peer_list[id]);
1586 GNUNET_free_non_null (peer_list);
1587 /* Clear host list */
1588 for (id = 0; id < host_list_size; id++)
1589 if (NULL != host_list[id])
1590 GNUNET_TESTBED_host_destroy (host_list[id]);
1591 GNUNET_free_non_null (host_list);
1592 /* Clear route list */
1593 for (id = 0; id < route_list_size; id++)
1594 if (NULL != route_list[id])
1595 GNUNET_free (route_list[id]);
1596 GNUNET_free_non_null (route_list);
1597 /* Clear slave_list */
1598 for (id = 0; id < slave_list_size; id++)
1599 if (NULL != slave_list[id])
1601 GNUNET_assert (NULL != slave_list[id]->controller);
1602 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1603 if (NULL != slave_list[id]->controller_proc)
1604 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1606 if (NULL != master_context)
1608 GNUNET_free_non_null (master_context->master_ip);
1609 if (NULL != master_context->system)
1610 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1611 GNUNET_free (master_context);
1612 master_context = NULL;
1618 * Callback for client disconnect
1621 * @param client the client which has disconnected
1624 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1626 if (NULL == master_context)
1628 if (client == master_context->client)
1630 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1631 GNUNET_SERVER_client_drop (client);
1632 /* should not be needed as we're terminated by failure to read
1633 from stdin, but if stdin fails for some reason, this shouldn't
1634 hurt for now --- might need to revise this later if we ever
1635 decide that master connections might be temporarily down
1637 //GNUNET_SCHEDULER_shutdown ();
1645 * @param cls closure
1646 * @param server the initialized server
1647 * @param cfg configuration to use
1650 testbed_run (void *cls,
1651 struct GNUNET_SERVER_Handle *server,
1652 const struct GNUNET_CONFIGURATION_Handle *cfg)
1654 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1656 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1657 sizeof (struct GNUNET_TESTBED_InitMessage)},
1658 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1659 {&handle_configure_shared_service, NULL,
1660 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1661 {&handle_link_controllers, NULL,
1662 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1663 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1664 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1665 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1666 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1667 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1668 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1669 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1670 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
1671 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1672 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
1673 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
1677 GNUNET_SERVER_add_handlers (server,
1679 GNUNET_SERVER_disconnect_notify (server,
1680 &client_disconnect_cb,
1682 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1684 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1687 LOG_DEBUG ("Testbed startup complete\n");
1692 * The starting point of execution
1694 int main (int argc, char *const *argv)
1698 GNUNET_SERVICE_run (argc,
1701 GNUNET_SERVICE_OPTION_NONE,