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 * Slave startup context
175 * The slave corresponding to this context
180 * The configuration used as a template while startup
182 struct GNUNET_CONFIGURATION_Handle *cfg;
187 * States of LCFContext
192 * The Context has been initialized; Nothing has been done on it
197 * Delegated host has been registered at the forwarding controller
199 DELEGATED_HOST_REGISTERED,
202 * The context has been finished (may have error)
210 * Link controllers request forwarding context
215 * The serialized and compressed configuration
220 * The gateway which will pass the link message to delegated host
222 struct Slave *gateway;
225 * The host registration handle while registered hosts in this context
227 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
230 * The size of the compressed serialized configuration
235 * The size of the uncompressed configuration
240 * Should the delegated host be started by the slave host?
245 * The state of this context
247 enum LCFContextState state;
252 uint32_t delegated_host_id;
259 * Structure of a queue entry in LCFContext request queue
261 struct LCFContextQueue
266 struct LCFContext *lcf;
271 struct LCFContextQueue *next;
276 struct LCFContextQueue *prev;
281 * A locally started peer
286 * The peer handle from testing API
288 struct GNUNET_TESTING_Peer *peer;
291 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
294 struct GNUNET_CONFIGURATION_Handle *cfg;
297 * Our local reference id for this peer
305 * The master context; generated with the first INIT message
307 static struct Context *master_context;
316 static struct GNUNET_DISK_FileHandle *fh;
319 * Current Transmit Handle; NULL if no notify transmit exists currently
321 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
328 * The head for the LCF queue
330 static struct LCFContextQueue *lcfq_head;
333 * The tail for the LCF queue
335 static struct LCFContextQueue *lcfq_tail;
338 * The message queue head
340 static struct MessageQueue *mq_head;
343 * The message queue tail
345 static struct MessageQueue *mq_tail;
350 static struct GNUNET_TESTBED_Host **host_list;
355 static struct Route **route_list;
358 * A list of directly linked neighbours
360 static struct Slave **slave_list;
363 * A list of peers we own locally
365 static struct Peer **peer_list;
368 * The hashmap of shared services
370 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
373 * The size of the host list
375 static uint32_t host_list_size;
378 * The size of the route list
380 static uint32_t route_list_size;
383 * The size of directly linked neighbours list
385 static uint32_t slave_list_size;
388 * The size of the peer list
390 static uint32_t peer_list_size;
397 * The lcf_task handle
399 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
402 * The shutdown task handle
404 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
411 * Our configuration; we also use this as template for starting other controllers
413 static struct GNUNET_CONFIGURATION_Handle *config;
417 * Function called to notify a client about the connection begin ready to queue
418 * more data. "buf" will be NULL and "size" zero if the connection was closed
419 * for writing in the meantime.
422 * @param size number of bytes available in buf
423 * @param buf where the callee should write the message
424 * @return number of bytes written to buf
427 transmit_ready_notify (void *cls, size_t size, void *buf)
429 struct MessageQueue *mq_entry;
431 transmit_handle = NULL;
433 GNUNET_assert (NULL != mq_entry);
436 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
437 size = ntohs (mq_entry->msg->size);
438 memcpy (buf, mq_entry->msg, size);
439 GNUNET_free (mq_entry->msg);
440 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
441 GNUNET_free (mq_entry);
443 if (NULL != mq_entry)
445 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
446 ntohs (mq_entry->msg->size),
447 GNUNET_TIME_UNIT_FOREVER_REL,
448 &transmit_ready_notify, NULL);
454 * Queues a message in send queue for sending to the service
456 * @param client the client to whom the queued message has to be sent
457 * @param msg the message to queue
460 queue_message (struct GNUNET_SERVER_Client *client,
461 struct GNUNET_MessageHeader *msg)
463 struct MessageQueue *mq_entry;
467 type = ntohs (msg->type);
468 size = ntohs (msg->size);
469 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
470 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
471 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
473 mq_entry->client = client;
474 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
476 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
477 if (NULL == transmit_handle)
479 GNUNET_SERVER_notify_transmit_ready (client, size,
480 GNUNET_TIME_UNIT_FOREVER_REL,
481 &transmit_ready_notify, NULL);
486 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
488 * @param ptr the memory block to realloc
489 * @param size the size of ptr
490 * @param new_size the size to which ptr has to be realloc'ed
491 * @return the newly reallocated memory block
494 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
496 ptr = GNUNET_realloc (ptr, new_size);
498 ptr = memset (ptr + size, 0, new_size - size);
504 * Function to add a host to the current list of known hosts
506 * @param host the host to add
507 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
511 host_list_add (struct GNUNET_TESTBED_Host *host)
515 host_id = GNUNET_TESTBED_host_get_id_ (host);
516 if (host_list_size <= host_id)
519 TESTBED_realloc (host_list,
520 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
521 sizeof (struct GNUNET_TESTBED_Host *) *
522 (host_list_size + LIST_GROW_STEP));
523 host_list_size += LIST_GROW_STEP;
525 if (NULL != host_list[host_id])
527 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
528 return GNUNET_SYSERR;
530 host_list[host_id] = host;
536 * Adds a route to the route list
538 * @param route the route to add
541 route_list_add (struct Route *route)
543 if (route->dest >= route_list_size)
546 TESTBED_realloc (route_list,
547 sizeof (struct Route *) * route_list_size,
548 sizeof (struct Route *) *
549 (route_list_size + LIST_GROW_STEP));
550 route_list_size += LIST_GROW_STEP;
552 GNUNET_assert (NULL == route_list[route->dest]);
553 route_list[route->dest] = route;
558 * Adds a slave to the slave array
560 * @param route the route to add
563 slave_list_add (struct Slave *slave)
565 if (slave->host_id >= slave_list_size)
567 slave_list = TESTBED_realloc (slave_list,
568 sizeof (struct Slave *) *slave_list_size,
569 sizeof (struct Slave *) *
570 (slave_list_size + LIST_GROW_STEP));
571 slave_list_size += LIST_GROW_STEP;
573 GNUNET_assert (NULL == slave_list[slave->host_id]);
574 slave_list[slave->host_id] = slave;
579 * Adds a peer to the peer array
581 * @param route the route to add
584 peer_list_add (struct Peer *peer)
586 if (peer->id >= peer_list_size)
588 peer_list = TESTBED_realloc (peer_list,
589 sizeof (struct Peer *) * peer_list_size,
590 sizeof (struct Peer *) *
591 (peer_list_size + LIST_GROW_STEP));
592 peer_list_size += LIST_GROW_STEP;
594 GNUNET_assert (NULL == peer_list[peer->id]);
595 peer_list[peer->id] = peer;
600 * Routes message to a host given its host_id
602 * @param host_id the id of the destination host
603 * @param msg the message to be routed
606 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
613 * The Link Controller forwarding task
615 * @param cls the LCFContext
616 * @param tc the Task context from scheduler
619 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
623 * Completion callback for host registrations while forwarding Link Controller messages
625 * @param cls the LCFContext
626 * @param emsg the error message; NULL if host registration is successful
629 lcf_proc_cc (void *cls, const char *emsg)
631 struct LCFContext *lcf = cls;
634 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
640 LOG (GNUNET_ERROR_TYPE_WARNING,
641 "Host registration failed with message: %s\n", emsg);
642 lcf->state = FINISHED;
643 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
646 lcf->state = DELEGATED_HOST_REGISTERED;
647 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
650 GNUNET_assert (0); /* Shouldn't reach here */
656 * The Link Controller forwarding task
658 * @param cls the LCFContext
659 * @param tc the Task context from scheduler
662 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
664 struct LCFContext *lcf = cls;
665 struct LCFContextQueue *lcfq;
667 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
672 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
673 lcf->gateway->controller))
676 GNUNET_TESTBED_register_host (lcf->gateway->controller,
677 host_list[lcf->delegated_host_id],
682 lcf->state = DELEGATED_HOST_REGISTERED;
683 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
686 case DELEGATED_HOST_REGISTERED:
687 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
688 host_list[lcf->delegated_host_id],
689 host_list[lcf->gateway->host_id],
690 lcf->sxcfg, lcf->sxcfg_size,
692 lcf->is_subordinate);
693 lcf->state = FINISHED;
696 GNUNET_assert (lcfq->lcf == lcf);
697 GNUNET_free (lcf->sxcfg);
699 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
701 if (NULL != lcfq_head)
703 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
709 * Callback for event from slave controllers
711 * @param cls struct Slave *
712 * @param event information about the event
715 slave_event_callback(void *cls,
716 const struct GNUNET_TESTBED_EventInformation *event)
723 * Callback to signal successfull startup of the controller process
725 * @param cls the closure from GNUNET_TESTBED_controller_start()
726 * @param cfg the configuration with which the controller has been started;
727 * NULL if status is not GNUNET_OK
728 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
729 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
732 slave_status_callback (void *cls,
733 const struct GNUNET_CONFIGURATION_Handle *cfg,
736 struct SlaveContext *sc = cls;
738 if (GNUNET_SYSERR == status)
740 sc->slave->controller_proc = NULL;
741 LOG (GNUNET_ERROR_TYPE_WARNING,
742 "Unexpected slave shutdown\n");
743 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
746 GNUNET_CONFIGURATION_destroy (sc->cfg);
747 sc->slave->controller =
748 GNUNET_TESTBED_controller_connect (cfg, host_list[sc->slave->host_id],
749 master_context->event_mask,
750 &slave_event_callback, sc->slave);
756 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
759 * @param client identification of the client
760 * @param message the actual message
763 handle_init (void *cls,
764 struct GNUNET_SERVER_Client *client,
765 const struct GNUNET_MessageHeader *message)
767 const struct GNUNET_TESTBED_InitMessage *msg;
768 struct GNUNET_TESTBED_Host *host;
772 if (NULL != master_context)
775 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
778 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
779 master_context = GNUNET_malloc (sizeof (struct Context));
780 master_context->client = client;
781 master_context->host_id = ntohl (msg->host_id);
782 GNUNET_assert (GNUNET_OK ==
783 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
784 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
785 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
786 NULL, 0, NI_NUMERICHOST))
788 LOG (GNUNET_ERROR_TYPE_WARNING,
789 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
792 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
793 master_context->system =
794 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
795 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
797 host_list_add (host);
798 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
799 GNUNET_SERVER_client_keep (client);
800 LOG_DEBUG ("Created master context with host ID: %u\n",
801 master_context->host_id);
802 GNUNET_SERVER_receive_done (client, GNUNET_OK);
807 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
810 * @param client identification of the client
811 * @param message the actual message
814 handle_add_host (void *cls,
815 struct GNUNET_SERVER_Client *client,
816 const struct GNUNET_MessageHeader *message)
818 struct GNUNET_TESTBED_Host *host;
819 const struct GNUNET_TESTBED_AddHostMessage *msg;
820 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
825 uint16_t username_length;
826 uint16_t hostname_length;
829 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
830 username_length = ntohs (msg->user_name_length);
831 username_length = (0 == username_length) ? 0 : username_length + 1;
832 username = (char *) &(msg[1]);
833 hostname = username + username_length;
834 if (ntohs (message->size) <=
835 (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
841 hostname_length = ntohs (message->size)
842 - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
843 if (strlen (hostname) != hostname_length - 1)
846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
849 host_id = ntohl (msg->host_id);
850 LOG_DEBUG ("Received ADDHOST message\n");
851 LOG_DEBUG ("-------host id: %u\n", host_id);
852 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
853 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
854 else LOG_DEBUG ("-------username: NULL\n");
855 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
856 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
857 ntohs (msg->ssh_port));
858 GNUNET_SERVER_receive_done (client, GNUNET_OK);
859 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
860 if (GNUNET_OK != host_list_add (host))
862 /* We are unable to add a host */
863 emsg = "A host exists with given host-id";
864 LOG_DEBUG ("%s: %u", emsg, host_id);
865 GNUNET_TESTBED_host_destroy (host);
866 reply_size += strlen (emsg) + 1;
867 reply = GNUNET_malloc (reply_size);
868 memcpy (&reply[1], emsg, strlen (emsg) + 1);
871 reply = GNUNET_malloc (reply_size);
872 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
873 reply->header.size = htons (reply_size);
874 reply->host_id = htonl (host_id);
875 queue_message (client, (struct GNUNET_MessageHeader *) reply);
880 * Iterator over hash map entries.
883 * @param key current key code
884 * @param value value in the hash map
885 * @return GNUNET_YES if we should continue to
889 int ss_exists_iterator (void *cls,
890 const struct GNUNET_HashCode * key,
893 struct SharedService *queried_ss = cls;
894 struct SharedService *ss = value;
896 if (0 == strcmp (ss->name, queried_ss->name))
904 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
907 * @param client identification of the client
908 * @param message the actual message
911 handle_configure_shared_service (void *cls,
912 struct GNUNET_SERVER_Client *client,
913 const struct GNUNET_MessageHeader *message)
915 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
916 struct SharedService *ss;
918 struct GNUNET_HashCode hash;
920 uint16_t service_name_size;
922 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
923 msg_size = ntohs (message->size);
924 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
930 service_name_size = msg_size -
931 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
932 service_name = (char *) &msg[1];
933 if ('\0' != service_name[service_name_size - 1])
936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
939 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
940 service_name, ntohl (msg->num_peers));
941 if (ntohl (msg->host_id) != master_context->host_id)
943 route_message (ntohl (msg->host_id), message);
944 GNUNET_SERVER_receive_done (client, GNUNET_OK);
947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
948 ss = GNUNET_malloc (sizeof (struct SharedService));
949 ss->name = strdup (service_name);
950 ss->num_shared = ntohl (msg->num_peers);
951 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
953 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
954 &ss_exists_iterator, ss))
956 LOG (GNUNET_ERROR_TYPE_WARNING,
957 "Service %s already configured as a shared service. "
958 "Ignoring service sharing request \n", ss->name);
959 GNUNET_free (ss->name);
963 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
969 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
972 * @param client identification of the client
973 * @param message the actual message
976 handle_link_controllers (void *cls,
977 struct GNUNET_SERVER_Client *client,
978 const struct GNUNET_MessageHeader *message)
980 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
981 struct GNUNET_CONFIGURATION_Handle *cfg;
982 struct SlaveContext *sc;
983 struct LCFContextQueue *lcfq;
985 struct Route *new_route;
989 uint32_t delegated_host_id;
990 uint32_t slave_host_id;
993 if (NULL == master_context)
996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
999 msize = ntohs (message->size);
1000 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1006 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1007 delegated_host_id = ntohl (msg->delegated_host_id);
1008 if (delegated_host_id == master_context->host_id)
1011 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1015 if ((delegated_host_id >= host_list_size) ||
1016 (NULL == host_list[delegated_host_id]))
1018 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1022 slave_host_id = ntohl (msg->slave_host_id);
1023 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1025 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1026 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1029 if (slave_host_id == delegated_host_id)
1031 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1035 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1036 config_size = ntohs (msg->config_size);
1038 if (slave_host_id == master_context->host_id) /* Link from us */
1040 struct Slave *slave;
1042 if ((delegated_host_id < slave_list_size) &&
1043 (NULL != slave_list[delegated_host_id])) /* We have already added */
1045 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1047 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1050 config = GNUNET_malloc (config_size);
1051 dest_size = (uLongf) config_size;
1052 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1053 (const Bytef *) &msg[1], (uLong) msize))
1055 GNUNET_break (0); /* Compression error */
1056 GNUNET_free (config);
1057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1060 if (config_size == dest_size)
1062 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1063 GNUNET_free (config);
1064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1066 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1067 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1070 GNUNET_break (0); /* Configuration parsing error */
1071 GNUNET_free (config);
1072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1075 GNUNET_free (config);
1076 if ((delegated_host_id < slave_list_size) &&
1077 (NULL != slave_list[delegated_host_id]))
1079 GNUNET_break (0); /* Configuration parsing error */
1080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1083 slave = GNUNET_malloc (sizeof (struct Slave));
1084 slave->host_id = delegated_host_id;
1085 slave_list_add (slave);
1086 sc = GNUNET_malloc (sizeof (struct SlaveContext));
1089 if (1 == msg->is_subordinate)
1091 slave->controller_proc =
1092 GNUNET_TESTBED_controller_start (master_context->master_ip,
1093 host_list[slave->host_id],
1094 cfg, &slave_status_callback,
1097 new_route = GNUNET_malloc (sizeof (struct Route));
1098 new_route->dest = delegated_host_id;
1099 new_route->thru = master_context->host_id;
1100 route_list_add (new_route);
1101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1105 /* Route the request */
1106 if (slave_host_id >= route_list_size)
1108 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1112 while (NULL != (route = route_list[slave_host_id]))
1114 if (route->thru == master_context->host_id)
1116 slave_host_id = route->thru;
1118 GNUNET_assert (NULL != route); /* because we add routes carefully */
1119 GNUNET_assert (route->dest < slave_list_size);
1120 GNUNET_assert (NULL != slave_list[route->dest]);
1121 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1122 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1123 lcfq->lcf->delegated_host_id = delegated_host_id;
1124 lcfq->lcf->is_subordinate =
1125 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1126 lcfq->lcf->state = INIT;
1127 lcfq->lcf->gateway = slave_list[route->dest];
1128 lcfq->lcf->sxcfg_size = msize;
1129 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1130 lcfq->lcf->scfg_size = config_size;
1131 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1132 if (NULL == lcfq_head)
1134 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1135 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1136 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1139 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1140 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1141 new_route = GNUNET_malloc (sizeof (struct Route));
1142 new_route->dest = delegated_host_id;
1143 new_route->thru = route->dest;
1144 route_list_add (new_route);
1149 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1152 * @param client identification of the client
1153 * @param message the actual message
1156 handle_peer_create (void *cls,
1157 struct GNUNET_SERVER_Client *client,
1158 const struct GNUNET_MessageHeader *message)
1160 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1161 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1162 struct GNUNET_CONFIGURATION_Handle *cfg;
1166 uint32_t config_size;
1170 msize = ntohs (message->size);
1171 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1173 GNUNET_break (0); /* We need configuration */
1174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1177 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1178 if (ntohl (msg->host_id) == master_context->host_id)
1183 /* We are responsible for this peer */
1184 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1185 config_size = ntohl (msg->config_size);
1186 config = GNUNET_malloc (config_size);
1187 dest_size = config_size;
1188 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1189 (const Bytef *) &msg[1], (uLong) msize)))
1191 GNUNET_break (0); /* uncompression error */
1192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1195 if (config_size != dest_size)
1197 GNUNET_break (0);/* Uncompressed config size mismatch */
1198 GNUNET_free (config);
1199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1202 cfg = GNUNET_CONFIGURATION_create ();
1203 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1206 GNUNET_break (0); /* Configuration parsing error */
1207 GNUNET_free (config);
1208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1211 GNUNET_free (config);
1212 peer = GNUNET_malloc (sizeof (struct Peer));
1214 peer->id = ntohl (msg->peer_id);
1215 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1216 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1220 if (NULL == peer->peer)
1222 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1225 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1228 peer_list_add (peer);
1229 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1230 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1231 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1232 reply->peer_id = msg->peer_id;
1233 reply->operation_id = msg->operation_id;
1234 queue_message (client, &reply->header);
1235 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1239 /* FIXME: Forward the peer to other host */
1241 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1246 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1249 * @param client identification of the client
1250 * @param message the actual message
1253 handle_peer_destroy (void *cls,
1254 struct GNUNET_SERVER_Client *client,
1255 const struct GNUNET_MessageHeader *message)
1257 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1258 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1261 uint16_t reply_size;
1263 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1264 peer_id = ntohl (msg->peer_id);
1265 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1266 peer_id, GNUNET_ntohll (msg->operation_id));
1267 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1270 /* FIXME: Reply with failure event message or forward to slave controller */
1271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1274 GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1275 GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1276 GNUNET_free (peer_list[peer_id]);
1277 peer_list[peer_id] = NULL;
1278 for (id = 0; id < LIST_GROW_STEP; id++)
1280 if (((peer_id + id >= peer_list_size) ||
1281 (NULL != peer_list[peer_id])))
1284 if (LIST_GROW_STEP == id)
1286 peer_list_size -= LIST_GROW_STEP;
1287 peer_list = GNUNET_realloc (peer_list, peer_list_size);
1290 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1291 reply = GNUNET_malloc (reply_size);
1292 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1293 reply->header.size = htons (reply_size);
1294 reply->operation_id = msg->operation_id;
1295 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1296 queue_message (client, &reply->header);
1297 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1302 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1305 * @param client identification of the client
1306 * @param message the actual message
1309 handle_peer_start (void *cls,
1310 struct GNUNET_SERVER_Client *client,
1311 const struct GNUNET_MessageHeader *message)
1313 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1314 struct GNUNET_TESTBED_PeerEventMessage *reply;
1317 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1318 peer_id = ntohl (msg->peer_id);
1319 if ((peer_id >= peer_list_size)
1320 || (NULL == peer_list[peer_id]))
1323 /* FIXME: reply with failure message or forward to slave controller */
1324 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1327 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1329 /* FIXME: return FAILURE message */
1331 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1334 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1335 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1336 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1337 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1338 reply->host_id = htonl (master_context->host_id);
1339 reply->peer_id = msg->peer_id;
1340 reply->operation_id = msg->operation_id;
1341 queue_message (client, &reply->header);
1342 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1347 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1350 * @param client identification of the client
1351 * @param message the actual message
1354 handle_peer_stop (void *cls,
1355 struct GNUNET_SERVER_Client *client,
1356 const struct GNUNET_MessageHeader *message)
1358 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1359 struct GNUNET_TESTBED_PeerEventMessage *reply;
1362 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1363 peer_id = ntohl (msg->peer_id);
1364 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1366 GNUNET_break (0); /* FIXME: route to slave? */
1367 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1370 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1372 /* FIXME: return FAILURE message */
1374 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1377 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1378 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1379 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1380 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1381 reply->host_id = htonl (master_context->host_id);
1382 reply->peer_id = msg->peer_id;
1383 reply->operation_id = msg->operation_id;
1384 queue_message (client, &reply->header);
1385 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1390 * Iterator over hash map entries.
1392 * @param cls closure
1393 * @param key current key code
1394 * @param value value in the hash map
1395 * @return GNUNET_YES if we should continue to
1400 ss_map_free_iterator (void *cls,
1401 const struct GNUNET_HashCode * key, void *value)
1403 struct SharedService *ss = value;
1405 GNUNET_assert (GNUNET_YES ==
1406 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1407 GNUNET_free (ss->name);
1414 * Task to clean up and shutdown nicely
1417 * @param tc the TaskContext from scheduler
1420 shutdown_task (void *cls,
1421 const struct GNUNET_SCHEDULER_TaskContext *tc)
1423 struct LCFContextQueue *lcfq;
1426 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1427 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1428 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1430 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1433 GNUNET_DISK_file_close (fh);
1436 if (NULL != lcfq_head)
1438 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1440 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1441 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1443 if (NULL != lcfq_head->lcf->rhandle)
1444 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1446 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1447 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1449 GNUNET_free (lcfq->lcf->sxcfg);
1450 GNUNET_free (lcfq->lcf);
1451 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1454 /* Clear peer list */
1455 for (id = 0; id < peer_list_size; id++)
1456 if (NULL != peer_list[id])
1458 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1459 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1460 GNUNET_free (peer_list[id]);
1462 GNUNET_free_non_null (peer_list);
1463 /* Clear host list */
1464 for (id = 0; id < host_list_size; id++)
1465 if (NULL != host_list[id])
1466 GNUNET_TESTBED_host_destroy (host_list[id]);
1467 GNUNET_free_non_null (host_list);
1468 /* Clear route list */
1469 for (id = 0; id < route_list_size; id++)
1470 if (NULL != route_list[id])
1471 GNUNET_free (route_list[id]);
1472 GNUNET_free_non_null (route_list);
1473 /* Clear slave_list */
1474 for (id = 0; id < slave_list_size; id++)
1475 if (NULL != slave_list[id])
1477 GNUNET_assert (NULL != slave_list[id]->controller);
1478 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1479 if (NULL != slave_list[id]->controller_proc)
1480 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1482 GNUNET_free_non_null (master_context->master_ip);
1483 if (NULL != master_context->system)
1484 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1485 GNUNET_free_non_null (master_context);
1490 * Debug shutdown task in case of stdin getting closed
1493 * @param tc the TaskContext from scheduler
1496 shutdown_task_ (void *cls,
1497 const struct GNUNET_SCHEDULER_TaskContext *tc)
1499 LOG (GNUNET_ERROR_TYPE_DEBUG, "STDIN closed ...\n");
1500 shutdown_task (cls, tc);
1505 * Callback for client disconnect
1508 * @param client the client which has disconnected
1511 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1513 if (NULL == master_context)
1515 if (client == master_context->client)
1517 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1518 GNUNET_SERVER_client_drop (client);
1519 /* should not be needed as we're terminated by failure to read
1520 from stdin, but if stdin fails for some reason, this shouldn't
1521 hurt for now --- might need to revise this later if we ever
1522 decide that master connections might be temporarily down
1524 GNUNET_SCHEDULER_shutdown ();
1532 * @param cls closure
1533 * @param server the initialized server
1534 * @param cfg configuration to use
1537 testbed_run (void *cls,
1538 struct GNUNET_SERVER_Handle *server,
1539 const struct GNUNET_CONFIGURATION_Handle *cfg)
1541 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1543 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1544 sizeof (struct GNUNET_TESTBED_InitMessage)},
1545 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1546 {&handle_configure_shared_service, NULL,
1547 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1548 {&handle_link_controllers, NULL,
1549 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1550 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1551 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1552 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1553 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1554 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1555 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1556 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
1560 config = GNUNET_CONFIGURATION_dup (cfg);
1561 GNUNET_SERVER_add_handlers (server,
1563 GNUNET_SERVER_disconnect_notify (server,
1564 &client_disconnect_cb,
1566 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1567 fh = GNUNET_DISK_get_handle_from_native (stdin);
1570 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1575 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1579 LOG_DEBUG ("Testbed startup complete\n");
1584 * The starting point of execution
1586 int main (int argc, char *const *argv)
1590 GNUNET_SERVICE_run (argc,
1593 GNUNET_SERVICE_OPTION_NONE,