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 * The network address of the master controller
65 * The TESTING system handle for starting peers locally
67 struct GNUNET_TESTING_System *system;
70 * Event mask of event to be responded in this context
75 * Our host id according to this context
82 * The message queue for sending messages to clients
87 * The message to be sent
89 struct GNUNET_MessageHeader *msg;
92 * The client to send the message to
94 struct GNUNET_SERVER_Client *client;
97 * next pointer for DLL
99 struct MessageQueue *next;
102 * prev pointer for DLL
104 struct MessageQueue *prev;
109 * The structure for identifying a shared service
114 * The name of the shared service
119 * Number of shared peers per instance of the shared service
124 * Number of peers currently sharing the service
126 uint32_t num_sharing;
141 * The host destination is reachable thru
148 * Structure representing a connected(directly-linked) controller
153 * The controller process handle if we had started the controller
155 struct GNUNET_TESTBED_ControllerProc *controller_proc;
158 * The controller handle
160 struct GNUNET_TESTBED_Controller *controller;
163 * The id of the host this controller is running on
170 * States of LCFContext
175 * The Context has been initialized; Nothing has been done on it
180 * Delegated host has been registered at the forwarding controller
182 DELEGATED_HOST_REGISTERED,
185 * The context has been finished (may have error)
193 * Link controllers request forwarding context
198 * The serialized and compressed configuration
203 * The gateway which will pass the link message to delegated host
205 struct Slave *gateway;
208 * The host registration handle while registered hosts in this context
210 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
213 * The size of the compressed serialized configuration
218 * The size of the uncompressed configuration
223 * Should the delegated host be started by the slave host?
228 * The state of this context
230 enum LCFContextState state;
235 uint32_t delegated_host_id;
242 * Structure of a queue entry in LCFContext request queue
244 struct LCFContextQueue
249 struct LCFContext *lcf;
254 struct LCFContextQueue *next;
259 struct LCFContextQueue *prev;
264 * A locally started peer
269 * The peer handle from testing API
271 struct GNUNET_TESTING_Peer *peer;
274 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
277 struct GNUNET_CONFIGURATION_Handle *cfg;
280 * Our local reference id for this peer
288 * The master context; generated with the first INIT message
290 static struct Context *master_context;
299 static struct GNUNET_DISK_FileHandle *fh;
302 * Current Transmit Handle; NULL if no notify transmit exists currently
304 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
311 * The head for the LCF queue
313 static struct LCFContextQueue *lcfq_head;
316 * The tail for the LCF queue
318 static struct LCFContextQueue *lcfq_tail;
321 * The message queue head
323 static struct MessageQueue *mq_head;
326 * The message queue tail
328 static struct MessageQueue *mq_tail;
333 static struct GNUNET_TESTBED_Host **host_list;
338 static struct Route **route_list;
341 * A list of directly linked neighbours
343 static struct Slave **slave_list;
346 * A list of peers we own locally
348 static struct Peer **peer_list;
351 * The hashmap of shared services
353 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
356 * The size of the host list
358 static uint32_t host_list_size;
361 * The size of the route list
363 static uint32_t route_list_size;
366 * The size of directly linked neighbours list
368 static uint32_t slave_list_size;
371 * The size of the peer list
373 static uint32_t peer_list_size;
380 * The lcf_task handle
382 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
385 * The shutdown task handle
387 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
394 * Our configuration; we also use this as template for starting other controllers
396 static struct GNUNET_CONFIGURATION_Handle *config;
400 * Function called to notify a client about the connection begin ready to queue
401 * more data. "buf" will be NULL and "size" zero if the connection was closed
402 * for writing in the meantime.
405 * @param size number of bytes available in buf
406 * @param buf where the callee should write the message
407 * @return number of bytes written to buf
410 transmit_ready_notify (void *cls, size_t size, void *buf)
412 struct MessageQueue *mq_entry;
414 transmit_handle = NULL;
416 GNUNET_assert (NULL != mq_entry);
419 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
420 size = ntohs (mq_entry->msg->size);
421 memcpy (buf, mq_entry->msg, size);
422 GNUNET_free (mq_entry->msg);
423 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
424 GNUNET_free (mq_entry);
426 if (NULL != mq_entry)
428 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
429 ntohs (mq_entry->msg->size),
430 GNUNET_TIME_UNIT_FOREVER_REL,
431 &transmit_ready_notify, NULL);
437 * Queues a message in send queue for sending to the service
439 * @param client the client to whom the queued message has to be sent
440 * @param msg the message to queue
443 queue_message (struct GNUNET_SERVER_Client *client,
444 struct GNUNET_MessageHeader *msg)
446 struct MessageQueue *mq_entry;
450 type = ntohs (msg->type);
451 size = ntohs (msg->size);
452 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
453 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
454 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
456 mq_entry->client = client;
457 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
459 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
460 if (NULL == transmit_handle)
462 GNUNET_SERVER_notify_transmit_ready (client, size,
463 GNUNET_TIME_UNIT_FOREVER_REL,
464 &transmit_ready_notify, NULL);
469 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
471 * @param ptr the memory block to realloc
472 * @param size the size of ptr
473 * @param new_size the size to which ptr has to be realloc'ed
474 * @return the newly reallocated memory block
477 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
479 ptr = GNUNET_realloc (ptr, new_size);
481 ptr = memset (ptr + size, 0, new_size - size);
487 * Function to add a host to the current list of known hosts
489 * @param host the host to add
490 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
494 host_list_add (struct GNUNET_TESTBED_Host *host)
498 host_id = GNUNET_TESTBED_host_get_id_ (host);
499 if (host_list_size <= host_id)
502 TESTBED_realloc (host_list,
503 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
504 sizeof (struct GNUNET_TESTBED_Host *) *
505 (host_list_size + LIST_GROW_STEP));
506 host_list_size += LIST_GROW_STEP;
508 if (NULL != host_list[host_id])
510 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
511 return GNUNET_SYSERR;
513 host_list[host_id] = host;
519 * Adds a route to the route list
521 * @param route the route to add
524 route_list_add (struct Route *route)
526 if (route->dest >= route_list_size)
529 TESTBED_realloc (route_list,
530 sizeof (struct Route *) * route_list_size,
531 sizeof (struct Route *) *
532 (route_list_size + LIST_GROW_STEP));
533 route_list_size += LIST_GROW_STEP;
535 GNUNET_assert (NULL == route_list[route->dest]);
536 route_list[route->dest] = route;
541 * Adds a slave to the slave array
543 * @param route the route to add
546 slave_list_add (struct Slave *slave)
548 if (slave->host_id >= slave_list_size)
550 slave_list = TESTBED_realloc (slave_list,
551 sizeof (struct Slave *) *slave_list_size,
552 sizeof (struct Slave *) *
553 (slave_list_size + LIST_GROW_STEP));
554 slave_list_size += LIST_GROW_STEP;
556 GNUNET_assert (NULL == slave_list[slave->host_id]);
557 slave_list[slave->host_id] = slave;
562 * Adds a peer to the peer array
564 * @param route the route to add
567 peer_list_add (struct Peer *peer)
569 if (peer->id >= peer_list_size)
571 peer_list = TESTBED_realloc (peer_list,
572 sizeof (struct Peer *) * peer_list_size,
573 sizeof (struct Peer *) *
574 (peer_list_size + LIST_GROW_STEP));
575 peer_list_size += LIST_GROW_STEP;
577 GNUNET_assert (NULL == peer_list[peer->id]);
578 peer_list[peer->id] = peer;
583 * Routes message to a host given its host_id
585 * @param host_id the id of the destination host
586 * @param msg the message to be routed
589 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
596 * The Link Controller forwarding task
598 * @param cls the LCFContext
599 * @param tc the Task context from scheduler
602 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
606 * Completion callback for host registrations while forwarding Link Controller messages
608 * @param cls the LCFContext
609 * @param emsg the error message; NULL if host registration is successful
612 lcf_proc_cc (void *cls, const char *emsg)
614 struct LCFContext *lcf = cls;
617 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
623 LOG (GNUNET_ERROR_TYPE_WARNING,
624 "Host registration failed with message: %s\n", emsg);
625 lcf->state = FINISHED;
626 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
629 lcf->state = DELEGATED_HOST_REGISTERED;
630 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
633 GNUNET_assert (0); /* Shouldn't reach here */
639 * The Link Controller forwarding task
641 * @param cls the LCFContext
642 * @param tc the Task context from scheduler
645 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
647 struct LCFContext *lcf = cls;
648 struct LCFContextQueue *lcfq;
650 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
655 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
656 lcf->gateway->controller))
659 GNUNET_TESTBED_register_host (lcf->gateway->controller,
660 host_list[lcf->delegated_host_id],
665 lcf->state = DELEGATED_HOST_REGISTERED;
666 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
669 case DELEGATED_HOST_REGISTERED:
670 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
671 host_list[lcf->delegated_host_id],
672 host_list[lcf->gateway->host_id],
673 lcf->sxcfg, lcf->sxcfg_size,
675 lcf->is_subordinate);
676 lcf->state = FINISHED;
679 GNUNET_assert (lcfq->lcf == lcf);
680 GNUNET_free (lcf->sxcfg);
682 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
684 if (NULL != lcfq_head)
686 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
692 * Callback for event from slave controllers
694 * @param cls struct Slave *
695 * @param event information about the event
698 slave_event_callback(void *cls,
699 const struct GNUNET_TESTBED_EventInformation *event)
706 * Callback for unexpected slave shutdowns
709 * @param emsg error message if available; can be NULL, which does NOT mean
710 * that there was no error
713 slave_shutdown_handler (void *cls, const char *emsg)
717 slave = (struct Slave *) cls;
718 slave->controller_proc = NULL;
719 LOG (GNUNET_ERROR_TYPE_WARNING,
720 "Unexpected slave shutdown\n");
722 LOG (GNUNET_ERROR_TYPE_WARNING, "Error: %s\n", emsg);
723 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
728 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
731 * @param client identification of the client
732 * @param message the actual message
735 handle_init (void *cls,
736 struct GNUNET_SERVER_Client *client,
737 const struct GNUNET_MessageHeader *message)
739 const struct GNUNET_TESTBED_InitMessage *msg;
740 struct GNUNET_TESTBED_Host *host;
744 if (NULL != master_context)
747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
750 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
751 master_context = GNUNET_malloc (sizeof (struct Context));
752 master_context->client = client;
753 master_context->host_id = ntohl (msg->host_id);
754 GNUNET_assert (GNUNET_OK ==
755 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
756 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
757 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
758 NULL, 0, NI_NUMERICHOST))
760 LOG (GNUNET_ERROR_TYPE_WARNING,
761 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
764 master_context->system =
765 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
766 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
768 host_list_add (host);
769 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
770 GNUNET_SERVER_client_keep (client);
771 LOG_DEBUG ("Created master context with host ID: %u\n",
772 master_context->host_id);
773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
778 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
781 * @param client identification of the client
782 * @param message the actual message
785 handle_add_host (void *cls,
786 struct GNUNET_SERVER_Client *client,
787 const struct GNUNET_MessageHeader *message)
789 struct GNUNET_TESTBED_Host *host;
790 const struct GNUNET_TESTBED_AddHostMessage *msg;
791 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
796 uint16_t username_length;
797 uint16_t hostname_length;
800 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
801 username_length = ntohs (msg->user_name_length);
802 username_length = (0 == username_length) ? 0 : username_length + 1;
803 username = (char *) &(msg[1]);
804 hostname = username + username_length;
805 if (ntohs (message->size) <=
806 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
812 hostname_length = ntohs (message->size)
813 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
814 if (strlen (hostname) != hostname_length - 1)
817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
820 host_id = ntohl (msg->host_id);
821 LOG_DEBUG ("Received ADDHOST message\n");
822 LOG_DEBUG ("-------host id: %u\n", host_id);
823 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
824 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
825 else LOG_DEBUG ("-------username: NULL\n");
826 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
827 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
828 ntohs (msg->ssh_port));
829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
830 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
831 if (GNUNET_OK != host_list_add (host))
833 /* We are unable to add a host */
834 emsg = "A host exists with given host-id";
835 LOG_DEBUG ("%s: %u", emsg, host_id);
836 GNUNET_TESTBED_host_destroy (host);
837 reply_size += strlen (emsg) + 1;
838 reply = GNUNET_malloc (reply_size);
839 memcpy (&reply[1], emsg, strlen (emsg) + 1);
842 reply = GNUNET_malloc (reply_size);
843 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
844 reply->header.size = htons (reply_size);
845 reply->host_id = htonl (host_id);
846 queue_message (client, (struct GNUNET_MessageHeader *) reply);
851 * Iterator over hash map entries.
854 * @param key current key code
855 * @param value value in the hash map
856 * @return GNUNET_YES if we should continue to
860 int ss_exists_iterator (void *cls,
861 const struct GNUNET_HashCode * key,
864 struct SharedService *queried_ss = cls;
865 struct SharedService *ss = value;
867 if (0 == strcmp (ss->name, queried_ss->name))
875 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
878 * @param client identification of the client
879 * @param message the actual message
882 handle_configure_shared_service (void *cls,
883 struct GNUNET_SERVER_Client *client,
884 const struct GNUNET_MessageHeader *message)
886 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
887 struct SharedService *ss;
889 struct GNUNET_HashCode hash;
891 uint16_t service_name_size;
893 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
894 msg_size = ntohs (message->size);
895 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
901 service_name_size = msg_size -
902 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
903 service_name = (char *) &msg[1];
904 if ('\0' != service_name[service_name_size - 1])
907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
910 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
911 service_name, ntohl (msg->num_peers));
912 if (ntohl (msg->host_id) != master_context->host_id)
914 route_message (ntohl (msg->host_id), message);
915 GNUNET_SERVER_receive_done (client, GNUNET_OK);
918 GNUNET_SERVER_receive_done (client, GNUNET_OK);
919 ss = GNUNET_malloc (sizeof (struct SharedService));
920 ss->name = strdup (service_name);
921 ss->num_shared = ntohl (msg->num_peers);
922 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
924 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
925 &ss_exists_iterator, ss))
927 LOG (GNUNET_ERROR_TYPE_WARNING,
928 "Service %s already configured as a shared service. "
929 "Ignoring service sharing request \n", ss->name);
930 GNUNET_free (ss->name);
934 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
935 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
940 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
943 * @param client identification of the client
944 * @param message the actual message
947 handle_link_controllers (void *cls,
948 struct GNUNET_SERVER_Client *client,
949 const struct GNUNET_MessageHeader *message)
951 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
952 struct GNUNET_CONFIGURATION_Handle *cfg;
953 struct LCFContextQueue *lcfq;
955 struct Route *new_route;
959 uint32_t delegated_host_id;
960 uint32_t slave_host_id;
963 if (NULL == master_context)
966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
969 msize = ntohs (message->size);
970 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
976 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
977 delegated_host_id = ntohl (msg->delegated_host_id);
978 if (delegated_host_id == master_context->host_id)
981 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
985 if ((delegated_host_id >= host_list_size) ||
986 (NULL == host_list[delegated_host_id]))
988 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
992 slave_host_id = ntohl (msg->slave_host_id);
993 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
995 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
999 if (slave_host_id == delegated_host_id)
1001 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1005 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1006 config_size = ntohs (msg->config_size);
1008 if (slave_host_id == master_context->host_id) /* Link from us */
1010 struct Slave *slave;
1012 if ((delegated_host_id < slave_list_size) &&
1013 (NULL != slave_list[delegated_host_id])) /* We have already added */
1015 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1020 config = GNUNET_malloc (config_size);
1021 dest_size = (uLongf) config_size;
1022 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1023 (const Bytef *) &msg[1], (uLong) msize))
1025 GNUNET_break (0); /* Compression error */
1026 GNUNET_free (config);
1027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1030 if (config_size == dest_size)
1032 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1033 GNUNET_free (config);
1034 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1036 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1037 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1040 GNUNET_break (0); /* Configuration parsing error */
1041 GNUNET_free (config);
1042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1045 GNUNET_free (config);
1046 if ((delegated_host_id < slave_list_size) &&
1047 (NULL != slave_list[delegated_host_id]))
1049 GNUNET_break (0); /* Configuration parsing error */
1050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1053 slave = GNUNET_malloc (sizeof (struct Slave));
1054 slave->host_id = delegated_host_id;
1055 slave_list_add (slave);
1056 if (1 == msg->is_subordinate)
1058 slave->controller_proc =
1059 GNUNET_TESTBED_controller_start (master_context->master_ip,
1060 host_list[delegated_host_id],
1061 cfg, &slave_shutdown_handler,
1065 GNUNET_TESTBED_controller_connect (cfg, host_list[delegated_host_id],
1066 master_context->event_mask,
1067 &slave_event_callback, slave);
1068 GNUNET_CONFIGURATION_destroy (cfg);
1069 new_route = GNUNET_malloc (sizeof (struct Route));
1070 new_route->dest = delegated_host_id;
1071 new_route->thru = master_context->host_id;
1072 route_list_add (new_route);
1073 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1077 /* Route the request */
1078 if (slave_host_id >= route_list_size)
1080 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1084 while (NULL != (route = route_list[slave_host_id]))
1086 if (route->thru == master_context->host_id)
1088 slave_host_id = route->thru;
1090 GNUNET_assert (NULL != route); /* because we add routes carefully */
1091 GNUNET_assert (route->dest < slave_list_size);
1092 GNUNET_assert (NULL != slave_list[route->dest]);
1093 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1094 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1095 lcfq->lcf->delegated_host_id = delegated_host_id;
1096 lcfq->lcf->is_subordinate =
1097 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1098 lcfq->lcf->state = INIT;
1099 lcfq->lcf->gateway = slave_list[route->dest];
1100 lcfq->lcf->sxcfg_size = msize;
1101 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1102 lcfq->lcf->scfg_size = config_size;
1103 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1104 if (NULL == lcfq_head)
1106 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1107 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1108 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1111 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1112 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1113 new_route = GNUNET_malloc (sizeof (struct Route));
1114 new_route->dest = delegated_host_id;
1115 new_route->thru = route->dest;
1116 route_list_add (new_route);
1121 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1124 * @param client identification of the client
1125 * @param message the actual message
1128 handle_peer_create (void *cls,
1129 struct GNUNET_SERVER_Client *client,
1130 const struct GNUNET_MessageHeader *message)
1132 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1133 struct GNUNET_CONFIGURATION_Handle *cfg;
1137 uint32_t config_size;
1141 msize = ntohs (message->size);
1142 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1144 GNUNET_break (0); /* We need configuration */
1145 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1148 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1149 if (ntohl (msg->host_id) == master_context->host_id)
1154 /* We are responsible for this peer */
1155 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1156 config_size = ntohl (msg->config_size);
1157 config = GNUNET_malloc (config_size);
1158 dest_size = config_size;
1159 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1160 (const Bytef *) &msg[1], (uLong) msize)))
1162 GNUNET_break (0); /* uncompression error */
1163 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1166 if (config_size != dest_size)
1168 GNUNET_break (0);/* Uncompressed config size mismatch */
1169 GNUNET_free (config);
1170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1173 cfg = GNUNET_CONFIGURATION_create ();
1174 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1177 GNUNET_break (0); /* Configuration parsing error */
1178 GNUNET_free (config);
1179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1182 GNUNET_free (config);
1183 peer = GNUNET_malloc (sizeof (struct Peer));
1185 peer->id = ntohl (msg->peer_id);
1186 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1187 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1191 if (NULL == peer->peer)
1193 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1196 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1199 peer_list_add (peer);
1200 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1204 /* Forward the peer to other host */
1206 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1211 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1214 * @param client identification of the client
1215 * @param message the actual message
1218 handle_peer_destroy (void *cls,
1219 struct GNUNET_SERVER_Client *client,
1220 const struct GNUNET_MessageHeader *message)
1222 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1223 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1226 uint16_t reply_size;
1228 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1229 peer_id = ntohl (msg->peer_id);
1230 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1231 peer_id, GNUNET_ntohll (msg->operation_id));
1232 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1235 /* Reply with failure event message */
1237 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1238 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1239 GNUNET_free (peer_list[peer_id]);
1240 peer_list[peer_id] = NULL;
1241 for (id = 0; id < LIST_GROW_STEP; id++)
1243 if (((peer_id + id >= peer_list_size) ||
1244 (NULL != peer_list[peer_id])))
1247 if (LIST_GROW_STEP == id)
1249 peer_list_size -= LIST_GROW_STEP;
1250 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1253 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1254 reply = GNUNET_malloc (reply_size);
1255 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1256 reply->header.size = htons (reply_size);
1257 reply->operation_id = msg->operation_id;
1258 queue_message (client, (struct GNUNET_MessageHeader *) reply);
1264 * Iterator over hash map entries.
1266 * @param cls closure
1267 * @param key current key code
1268 * @param value value in the hash map
1269 * @return GNUNET_YES if we should continue to
1274 ss_map_free_iterator (void *cls,
1275 const struct GNUNET_HashCode * key, void *value)
1277 struct SharedService *ss = value;
1279 GNUNET_assert (GNUNET_YES ==
1280 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1281 GNUNET_free (ss->name);
1288 * Task to clean up and shutdown nicely
1291 * @param tc the TaskContext from scheduler
1294 shutdown_task (void *cls,
1295 const struct GNUNET_SCHEDULER_TaskContext *tc)
1297 struct LCFContextQueue *lcfq;
1300 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1301 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1302 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1304 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1307 GNUNET_DISK_file_close (fh);
1310 if (NULL != lcfq_head)
1312 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1314 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1315 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1317 if (NULL != lcfq_head->lcf->rhandle)
1318 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1320 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1321 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1323 GNUNET_free (lcfq->lcf->sxcfg);
1324 GNUNET_free (lcfq->lcf);
1325 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1328 /* Clear peer list */
1329 for (id = 0; id < peer_list_size; id++)
1330 if (NULL != peer_list[id])
1332 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1333 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1334 GNUNET_free (peer_list[id]);
1336 GNUNET_free_non_null (peer_list);
1337 /* Clear host list */
1338 for (id = 0; id < host_list_size; id++)
1339 if (NULL != host_list[id])
1340 GNUNET_TESTBED_host_destroy (host_list[id]);
1341 GNUNET_free_non_null (host_list);
1342 /* Clear route list */
1343 for (id = 0; id < route_list_size; id++)
1344 if (NULL != route_list[id])
1345 GNUNET_free (route_list[id]);
1346 GNUNET_free_non_null (route_list);
1347 /* Clear slave_list */
1348 for (id = 0; id < slave_list_size; id++)
1349 if (NULL != slave_list[id])
1351 GNUNET_assert (NULL != slave_list[id]->controller);
1352 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1353 if (NULL != slave_list[id]->controller_proc)
1354 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1356 GNUNET_free_non_null (master_context->master_ip);
1357 if (NULL != master_context->system)
1358 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1359 GNUNET_free_non_null (master_context);
1364 * Debug shutdown task in case of stdin getting closed
1367 * @param tc the TaskContext from scheduler
1370 shutdown_task_ (void *cls,
1371 const struct GNUNET_SCHEDULER_TaskContext *tc)
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "STDIN closed ...\n");
1374 shutdown_task (cls, tc);
1379 * Callback for client disconnect
1382 * @param client the client which has disconnected
1385 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1387 if (NULL == master_context)
1389 if (client == master_context->client)
1391 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1392 GNUNET_SERVER_client_drop (client);
1393 /* should not be needed as we're terminated by failure to read
1394 from stdin, but if stdin fails for some reason, this shouldn't
1395 hurt for now --- might need to revise this later if we ever
1396 decide that master connections might be temporarily down
1398 GNUNET_SCHEDULER_shutdown ();
1406 * @param cls closure
1407 * @param server the initialized server
1408 * @param cfg configuration to use
1411 testbed_run (void *cls,
1412 struct GNUNET_SERVER_Handle *server,
1413 const struct GNUNET_CONFIGURATION_Handle *cfg)
1415 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1417 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1418 sizeof (struct GNUNET_TESTBED_InitMessage)},
1419 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1420 {&handle_configure_shared_service, NULL,
1421 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1422 {&handle_link_controllers, NULL,
1423 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1424 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1425 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1426 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1430 config = GNUNET_CONFIGURATION_dup (cfg);
1431 GNUNET_SERVER_add_handlers (server,
1433 GNUNET_SERVER_disconnect_notify (server,
1434 &client_disconnect_cb,
1436 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1437 fh = GNUNET_DISK_get_handle_from_native (stdin);
1440 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1445 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1449 LOG_DEBUG ("Testbed startup complete\n");
1454 * The starting point of execution
1456 int main (int argc, char *const *argv)
1460 GNUNET_SERVICE_run (argc,
1463 GNUNET_SERVICE_OPTION_NONE,