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"
31 #include "gnunet_core_service.h"
32 #include "gnunet_hello_lib.h"
35 #include "gnunet_testbed_service.h"
37 #include "testbed_api.h"
38 #include "testbed_api_hosts.h"
39 #include "gnunet_testing_lib-new.h"
44 #define LOG(kind,...) \
45 GNUNET_log (kind, __VA_ARGS__)
50 #define LOG_DEBUG(...) \
51 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
54 #define LIST_GROW_STEP 10
59 * The client handle associated with this context
61 struct GNUNET_SERVER_Client *client;
64 * The network address of the master controller
69 * The TESTING system handle for starting peers locally
71 struct GNUNET_TESTING_System *system;
74 * Event mask of event to be responded in this context
79 * Our host id according to this context
86 * The message queue for sending messages to clients
91 * The message to be sent
93 struct GNUNET_MessageHeader *msg;
96 * The client to send the message to
98 struct GNUNET_SERVER_Client *client;
101 * next pointer for DLL
103 struct MessageQueue *next;
106 * prev pointer for DLL
108 struct MessageQueue *prev;
113 * The structure for identifying a shared service
118 * The name of the shared service
123 * Number of shared peers per instance of the shared service
128 * Number of peers currently sharing the service
130 uint32_t num_sharing;
145 * The host destination is reachable thru
152 * Structure representing a connected(directly-linked) controller
157 * The controller process handle if we had started the controller
159 struct GNUNET_TESTBED_ControllerProc *controller_proc;
162 * The controller handle
164 struct GNUNET_TESTBED_Controller *controller;
167 * The id of the host this controller is running on
174 * States of LCFContext
179 * The Context has been initialized; Nothing has been done on it
184 * Delegated host has been registered at the forwarding controller
186 DELEGATED_HOST_REGISTERED,
189 * The slave host has been registred at the forwarding controller
191 SLAVE_HOST_REGISTERED,
194 * The context has been finished (may have error)
202 * Link controllers request forwarding context
207 * The serialized and compressed configuration
212 * The gateway which will pass the link message to delegated host
214 struct Slave *gateway;
217 * The host registration handle while registered hosts in this context
219 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
222 * The size of the compressed serialized configuration
227 * The size of the uncompressed configuration
232 * Should the delegated host be started by the slave host?
237 * The state of this context
239 enum LCFContextState state;
244 uint32_t delegated_host_id;
249 uint32_t slave_host_id;
255 * Structure of a queue entry in LCFContext request queue
257 struct LCFContextQueue
262 struct LCFContext *lcf;
267 struct LCFContextQueue *next;
272 struct LCFContextQueue *prev;
277 * A locally started peer
282 * The peer handle from testing API
284 struct GNUNET_TESTING_Peer *peer;
287 * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
290 struct GNUNET_CONFIGURATION_Handle *cfg;
293 * Our local reference id for this peer
301 * State information for overlay connect context
311 * Peer 1 has connected to peer0
313 OCC_STATE_PEER0_SUCCESS,
316 * Peer 2 has connected to peer1
318 OCC_STATE_PEER1_SUCCESS,
324 * Context information for connecting 2 peers in overlay
326 struct OverlayConnectContext
329 * The client which has requested for overlay connection
331 struct GNUNET_SERVER_Client *client;
334 * the peer which has to connect to the other peer
341 struct Peer *other_peer;
344 * Transport handle of the first peer to offer second peer's HELLO
346 struct GNUNET_TRANSPORT_Handle *p1th;
349 * Transport handle of other peer to get its HELLO
351 struct GNUNET_TRANSPORT_Handle *p2th;
354 * Core handles of the peer which has to connect to other peer
356 struct GNUNET_CORE_Handle *ch;
359 * HELLO of the other peer
361 struct GNUNET_MessageHeader *hello;
364 * Get hello handle for the other peer
366 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
369 * The error message we send if this overlay connect operation has timed out
374 * The peer identity of the first peer
376 struct GNUNET_PeerIdentity peer_identity;
379 * The peer identity of the other peer
381 struct GNUNET_PeerIdentity other_peer_identity;
384 * The id of the operation responsible for creating this context
389 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
392 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
395 * The id of the overlay connect timeout task
397 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
400 * State information for determining whose HELLOs have been successfully
409 * Context data to be used when forwarding peer create messages
411 struct PeerCreateContext
414 * The operation handle to peer create
416 struct GNUNET_TESTBED_operation *operation;
419 * The ID of the operation which created this context
421 uint64_t operation_id;
424 * The peer create timeout task id
426 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
432 * The master context; generated with the first INIT message
434 static struct Context *master_context;
441 * Current Transmit Handle; NULL if no notify transmit exists currently
443 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
450 * The head for the LCF queue
452 static struct LCFContextQueue *lcfq_head;
455 * The tail for the LCF queue
457 static struct LCFContextQueue *lcfq_tail;
460 * The message queue head
462 static struct MessageQueue *mq_head;
465 * The message queue tail
467 static struct MessageQueue *mq_tail;
472 static struct GNUNET_TESTBED_Host **host_list;
477 static struct Route **route_list;
480 * A list of directly linked neighbours
482 static struct Slave **slave_list;
485 * A list of peers we own locally
487 static struct Peer **peer_list;
490 * The hashmap of shared services
492 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
495 * The size of the host list
497 static uint32_t host_list_size;
500 * The size of the route list
502 static uint32_t route_list_size;
505 * The size of directly linked neighbours list
507 static uint32_t slave_list_size;
510 * The size of the peer list
512 static uint32_t peer_list_size;
519 * The lcf_task handle
521 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
524 * The shutdown task handle
526 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
530 * Function called to notify a client about the connection begin ready to queue
531 * more data. "buf" will be NULL and "size" zero if the connection was closed
532 * for writing in the meantime.
535 * @param size number of bytes available in buf
536 * @param buf where the callee should write the message
537 * @return number of bytes written to buf
540 transmit_ready_notify (void *cls, size_t size, void *buf)
542 struct MessageQueue *mq_entry;
544 transmit_handle = NULL;
546 GNUNET_assert (NULL != mq_entry);
549 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
550 size = ntohs (mq_entry->msg->size);
551 memcpy (buf, mq_entry->msg, size);
552 GNUNET_free (mq_entry->msg);
553 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
554 GNUNET_free (mq_entry);
556 if (NULL != mq_entry)
558 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
559 ntohs (mq_entry->msg->size),
560 GNUNET_TIME_UNIT_FOREVER_REL,
561 &transmit_ready_notify, NULL);
567 * Queues a message in send queue for sending to the service
569 * @param client the client to whom the queued message has to be sent
570 * @param msg the message to queue
573 queue_message (struct GNUNET_SERVER_Client *client,
574 struct GNUNET_MessageHeader *msg)
576 struct MessageQueue *mq_entry;
580 type = ntohs (msg->type);
581 size = ntohs (msg->size);
582 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
583 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
584 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
586 mq_entry->client = client;
587 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
589 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
590 if (NULL == transmit_handle)
592 GNUNET_SERVER_notify_transmit_ready (client, size,
593 GNUNET_TIME_UNIT_FOREVER_REL,
594 &transmit_ready_notify, NULL);
599 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
601 * @param ptr the memory block to realloc
602 * @param size the size of ptr
603 * @param new_size the size to which ptr has to be realloc'ed
604 * @return the newly reallocated memory block
607 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
609 ptr = GNUNET_realloc (ptr, new_size);
611 ptr = memset (ptr + size, 0, new_size - size);
617 * Function to add a host to the current list of known hosts
619 * @param host the host to add
620 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
624 host_list_add (struct GNUNET_TESTBED_Host *host)
628 host_id = GNUNET_TESTBED_host_get_id_ (host);
629 if (host_list_size <= host_id)
632 TESTBED_realloc (host_list,
633 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
634 sizeof (struct GNUNET_TESTBED_Host *) *
635 (host_list_size + LIST_GROW_STEP));
636 host_list_size += LIST_GROW_STEP;
638 if (NULL != host_list[host_id])
640 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
641 return GNUNET_SYSERR;
643 host_list[host_id] = host;
649 * Adds a route to the route list
651 * @param route the route to add
654 route_list_add (struct Route *route)
656 if (route->dest >= route_list_size)
659 TESTBED_realloc (route_list,
660 sizeof (struct Route *) * route_list_size,
661 sizeof (struct Route *) *
662 (route_list_size + LIST_GROW_STEP));
663 route_list_size += LIST_GROW_STEP;
665 GNUNET_assert (NULL == route_list[route->dest]);
666 route_list[route->dest] = route;
671 * Adds a slave to the slave array
673 * @param slave the slave controller to add
676 slave_list_add (struct Slave *slave)
678 if (slave->host_id >= slave_list_size)
680 slave_list = TESTBED_realloc (slave_list,
681 sizeof (struct Slave *) *slave_list_size,
682 sizeof (struct Slave *) *
683 (slave_list_size + LIST_GROW_STEP));
684 slave_list_size += LIST_GROW_STEP;
686 GNUNET_assert (NULL == slave_list[slave->host_id]);
687 slave_list[slave->host_id] = slave;
692 * Adds a peer to the peer array
694 * @param peer the peer to add
697 peer_list_add (struct Peer *peer)
699 if (peer->id >= peer_list_size)
701 peer_list = TESTBED_realloc (peer_list,
702 sizeof (struct Peer *) * peer_list_size,
703 sizeof (struct Peer *) *
704 (peer_list_size + LIST_GROW_STEP));
705 peer_list_size += LIST_GROW_STEP;
707 GNUNET_assert (NULL == peer_list[peer->id]);
708 peer_list[peer->id] = peer;
713 * Removes a the give peer from the peer array
715 * @param peer the peer to be removed
718 peer_list_remove (struct Peer *peer)
722 peer_list[peer->id] = NULL;
723 while (peer_list_size >= LIST_GROW_STEP)
725 for (id = peer_list_size - 1;
726 id > peer_list_size - LIST_GROW_STEP; id--)
727 if (NULL != peer_list[id])
729 if (id != peer_list_size - LIST_GROW_STEP)
731 peer_list_size -= LIST_GROW_STEP;
733 peer_list = GNUNET_realloc (peer_list, sizeof (struct GNUNET_TESTBED_Peer*)
739 * Finds the route with directly connected host as destination through which
740 * the destination host can be reached
742 * @param host_id the id of the destination host
743 * @return the route with directly connected destination host; NULL if no route
746 static struct Route *
747 find_dest_route (uint32_t host_id)
751 while(NULL != (route = route_list[host_id]))
753 if (route->thru == master_context->host_id)
755 host_id = route->thru;
762 * Routes message to a host given its host_id
764 * @param host_id the id of the destination host
765 * @param msg the message to be routed
768 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
775 * Send operation failure message to client
777 * @param client the client to which the failure message has to be sent to
778 * @param operation_id the id of the failed operation
779 * @param emsg the error message; can be NULL
782 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
783 uint64_t operation_id,
786 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
790 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
791 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
793 msg = GNUNET_malloc (msize);
794 msg->header.size = htons (msize);
795 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT);
796 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
797 msg->operation_id = GNUNET_htonll (operation_id);
799 memcpy (&msg[1], emsg, emsg_len);
800 queue_message (client, &msg->header);
805 * The Link Controller forwarding task
807 * @param cls the LCFContext
808 * @param tc the Task context from scheduler
811 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
815 * Completion callback for host registrations while forwarding Link Controller messages
817 * @param cls the LCFContext
818 * @param emsg the error message; NULL if host registration is successful
821 lcf_proc_cc (void *cls, const char *emsg)
823 struct LCFContext *lcf = cls;
826 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
831 goto registration_error;
832 lcf->state = DELEGATED_HOST_REGISTERED;
833 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
835 case DELEGATED_HOST_REGISTERED:
837 goto registration_error;
838 lcf->state = SLAVE_HOST_REGISTERED;
839 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
842 GNUNET_assert (0); /* Shouldn't reach here */
847 LOG (GNUNET_ERROR_TYPE_WARNING,
848 "Host registration failed with message: %s\n", emsg);
849 lcf->state = FINISHED;
850 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
855 * The Link Controller forwarding task
857 * @param cls the LCFContext
858 * @param tc the Task context from scheduler
861 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
863 struct LCFContext *lcf = cls;
864 struct LCFContextQueue *lcfq;
866 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
871 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
872 lcf->gateway->controller))
875 GNUNET_TESTBED_register_host (lcf->gateway->controller,
876 host_list[lcf->delegated_host_id],
881 lcf->state = DELEGATED_HOST_REGISTERED;
882 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
885 case DELEGATED_HOST_REGISTERED:
887 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
888 lcf->gateway->controller))
891 GNUNET_TESTBED_register_host (lcf->gateway->controller,
892 host_list[lcf->slave_host_id],
897 lcf->state = SLAVE_HOST_REGISTERED;
898 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
901 case SLAVE_HOST_REGISTERED:
902 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
903 host_list[lcf->delegated_host_id],
904 host_list[lcf->slave_host_id],
905 lcf->sxcfg, lcf->sxcfg_size,
907 lcf->is_subordinate);
908 lcf->state = FINISHED;
909 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
913 GNUNET_assert (lcfq->lcf == lcf);
914 GNUNET_free (lcf->sxcfg);
916 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
918 if (NULL != lcfq_head)
920 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
926 * Callback for event from slave controllers
928 * @param cls struct Slave *
929 * @param event information about the event
932 slave_event_callback(void *cls,
933 const struct GNUNET_TESTBED_EventInformation *event)
940 * Callback to signal successfull startup of the controller process
942 * @param cls the closure from GNUNET_TESTBED_controller_start()
943 * @param cfg the configuration with which the controller has been started;
944 * NULL if status is not GNUNET_OK
945 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
946 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
949 slave_status_callback (void *cls,
950 const struct GNUNET_CONFIGURATION_Handle *cfg,
953 struct Slave *slave = cls;
955 if (GNUNET_SYSERR == status)
957 slave->controller_proc = NULL;
958 LOG (GNUNET_ERROR_TYPE_WARNING,
959 "Unexpected slave shutdown\n");
960 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
964 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
965 master_context->event_mask,
966 &slave_event_callback, slave);
971 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
974 * @param client identification of the client
975 * @param message the actual message
978 handle_init (void *cls,
979 struct GNUNET_SERVER_Client *client,
980 const struct GNUNET_MessageHeader *message)
982 const struct GNUNET_TESTBED_InitMessage *msg;
983 struct GNUNET_TESTBED_Host *host;
984 const char *controller_hostname;
987 if (NULL != master_context)
990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
993 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
994 msize = ntohs (message->size);
995 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1001 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1002 controller_hostname = (const char *) &msg[1];
1003 if ('\0' != controller_hostname[msize - 1])
1006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1009 master_context = GNUNET_malloc (sizeof (struct Context));
1010 master_context->client = client;
1011 master_context->host_id = ntohl (msg->host_id);
1012 master_context->master_ip = GNUNET_strdup (controller_hostname);
1013 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1014 master_context->system =
1015 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1016 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
1018 host_list_add (host);
1019 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1020 GNUNET_SERVER_client_keep (client);
1021 LOG_DEBUG ("Created master context with host ID: %u\n",
1022 master_context->host_id);
1023 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1028 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1031 * @param client identification of the client
1032 * @param message the actual message
1035 handle_add_host (void *cls,
1036 struct GNUNET_SERVER_Client *client,
1037 const struct GNUNET_MessageHeader *message)
1039 struct GNUNET_TESTBED_Host *host;
1040 const struct GNUNET_TESTBED_AddHostMessage *msg;
1041 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1046 uint16_t username_length;
1047 uint16_t hostname_length;
1048 uint16_t reply_size;
1051 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1052 msize = ntohs (msg->header.size);
1053 username = (char *) &(msg[1]);
1054 username_length = ntohs (msg->user_name_length);
1055 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1056 + username_length + 1)); /* msg must contain hostname */
1057 if (0 != username_length)
1058 GNUNET_assert ('\0' == username[username_length]);
1059 username_length = (0 == username_length) ? 0 : username_length + 1;
1060 hostname = username + username_length;
1061 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1063 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1064 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1065 host_id = ntohl (msg->host_id);
1066 LOG_DEBUG ("Received ADDHOST message\n");
1067 LOG_DEBUG ("-------host id: %u\n", host_id);
1068 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
1069 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
1070 else LOG_DEBUG ("-------username: NULL\n");
1071 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1072 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1073 ntohs (msg->ssh_port));
1074 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1075 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1076 if (GNUNET_OK != host_list_add (host))
1078 /* We are unable to add a host */
1079 emsg = "A host exists with given host-id";
1080 LOG_DEBUG ("%s: %u", emsg, host_id);
1081 GNUNET_TESTBED_host_destroy (host);
1082 reply_size += strlen (emsg) + 1;
1083 reply = GNUNET_malloc (reply_size);
1084 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1087 reply = GNUNET_malloc (reply_size);
1088 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1089 reply->header.size = htons (reply_size);
1090 reply->host_id = htonl (host_id);
1091 queue_message (client, &reply->header);
1096 * Iterator over hash map entries.
1098 * @param cls closure
1099 * @param key current key code
1100 * @param value value in the hash map
1101 * @return GNUNET_YES if we should continue to
1105 int ss_exists_iterator (void *cls,
1106 const struct GNUNET_HashCode * key,
1109 struct SharedService *queried_ss = cls;
1110 struct SharedService *ss = value;
1112 if (0 == strcmp (ss->name, queried_ss->name))
1120 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1123 * @param client identification of the client
1124 * @param message the actual message
1127 handle_configure_shared_service (void *cls,
1128 struct GNUNET_SERVER_Client *client,
1129 const struct GNUNET_MessageHeader *message)
1131 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1132 struct SharedService *ss;
1134 struct GNUNET_HashCode hash;
1136 uint16_t service_name_size;
1138 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1139 msg_size = ntohs (message->size);
1140 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1146 service_name_size = msg_size -
1147 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1148 service_name = (char *) &msg[1];
1149 if ('\0' != service_name[service_name_size - 1])
1152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1155 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1156 service_name, ntohl (msg->num_peers));
1157 if (ntohl (msg->host_id) != master_context->host_id)
1159 route_message (ntohl (msg->host_id), message);
1160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1163 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1164 ss = GNUNET_malloc (sizeof (struct SharedService));
1165 ss->name = strdup (service_name);
1166 ss->num_shared = ntohl (msg->num_peers);
1167 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1168 if (GNUNET_SYSERR ==
1169 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1170 &ss_exists_iterator, ss))
1172 LOG (GNUNET_ERROR_TYPE_WARNING,
1173 "Service %s already configured as a shared service. "
1174 "Ignoring service sharing request \n", ss->name);
1175 GNUNET_free (ss->name);
1179 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1185 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1188 * @param client identification of the client
1189 * @param message the actual message
1192 handle_link_controllers (void *cls,
1193 struct GNUNET_SERVER_Client *client,
1194 const struct GNUNET_MessageHeader *message)
1196 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1197 struct GNUNET_CONFIGURATION_Handle *cfg;
1198 struct LCFContextQueue *lcfq;
1199 struct Route *route;
1200 struct Route *new_route;
1204 uint32_t delegated_host_id;
1205 uint32_t slave_host_id;
1208 if (NULL == master_context)
1211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1214 msize = ntohs (message->size);
1215 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1221 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1222 delegated_host_id = ntohl (msg->delegated_host_id);
1223 if (delegated_host_id == master_context->host_id)
1226 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1230 if ((delegated_host_id >= host_list_size) ||
1231 (NULL == host_list[delegated_host_id]))
1233 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1237 slave_host_id = ntohl (msg->slave_host_id);
1238 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1240 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1244 if (slave_host_id == delegated_host_id)
1246 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1247 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1250 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1251 config_size = ntohs (msg->config_size);
1253 if (slave_host_id == master_context->host_id) /* Link from us */
1255 struct Slave *slave;
1257 if ((delegated_host_id < slave_list_size) &&
1258 (NULL != slave_list[delegated_host_id])) /* We have already added */
1260 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1265 config = GNUNET_malloc (config_size);
1266 dest_size = (uLongf) config_size;
1267 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1268 (const Bytef *) &msg[1], (uLong) msize))
1270 GNUNET_break (0); /* Compression error */
1271 GNUNET_free (config);
1272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1275 if (config_size == dest_size)
1277 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1278 GNUNET_free (config);
1279 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1282 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1283 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1286 GNUNET_break (0); /* Configuration parsing error */
1287 GNUNET_free (config);
1288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1291 GNUNET_free (config);
1292 if ((delegated_host_id < slave_list_size) &&
1293 (NULL != slave_list[delegated_host_id]))
1295 GNUNET_break (0); /* Configuration parsing error */
1296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1299 slave = GNUNET_malloc (sizeof (struct Slave));
1300 slave->host_id = delegated_host_id;
1301 slave_list_add (slave);
1302 if (1 == msg->is_subordinate)
1304 slave->controller_proc =
1305 GNUNET_TESTBED_controller_start (master_context->master_ip,
1306 host_list[slave->host_id],
1307 cfg, &slave_status_callback,
1312 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1313 master_context->event_mask,
1314 &slave_event_callback, slave);
1316 GNUNET_CONFIGURATION_destroy (cfg);
1317 new_route = GNUNET_malloc (sizeof (struct Route));
1318 new_route->dest = delegated_host_id;
1319 new_route->thru = master_context->host_id;
1320 route_list_add (new_route);
1321 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1325 /* Route the request */
1326 if (slave_host_id >= route_list_size)
1328 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1332 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1333 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1334 lcfq->lcf->delegated_host_id = delegated_host_id;
1335 lcfq->lcf->slave_host_id = slave_host_id;
1336 route = find_dest_route (slave_host_id);
1337 GNUNET_assert (NULL != route); /* because we add routes carefully */
1338 GNUNET_assert (route->dest < slave_list_size);
1339 GNUNET_assert (NULL != slave_list[route->dest]);
1340 lcfq->lcf->is_subordinate =
1341 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1342 lcfq->lcf->state = INIT;
1343 lcfq->lcf->gateway = slave_list[route->dest];
1344 lcfq->lcf->sxcfg_size = msize;
1345 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1346 lcfq->lcf->scfg_size = config_size;
1347 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1348 if (NULL == lcfq_head)
1350 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1351 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1352 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1355 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1356 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1357 new_route = GNUNET_malloc (sizeof (struct Route));
1358 new_route->dest = delegated_host_id;
1359 new_route->thru = route->dest;
1360 route_list_add (new_route);
1365 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1368 * @param client identification of the client
1369 * @param message the actual message
1372 handle_peer_create (void *cls,
1373 struct GNUNET_SERVER_Client *client,
1374 const struct GNUNET_MessageHeader *message)
1376 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1377 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1378 struct GNUNET_CONFIGURATION_Handle *cfg;
1379 struct PeerCreateContext *pc_ctxt;
1380 struct Route *route;
1384 uint32_t config_size;
1389 msize = ntohs (message->size);
1390 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1392 GNUNET_break (0); /* We need configuration */
1393 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1396 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1397 host_id = ntohl (msg->host_id);
1398 if (host_id == master_context->host_id)
1403 /* We are responsible for this peer */
1404 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1405 config_size = ntohl (msg->config_size);
1406 config = GNUNET_malloc (config_size);
1407 dest_size = config_size;
1408 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1409 (const Bytef *) &msg[1], (uLong) msize)))
1411 GNUNET_break (0); /* uncompression error */
1412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1415 if (config_size != dest_size)
1417 GNUNET_break (0);/* Uncompressed config size mismatch */
1418 GNUNET_free (config);
1419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1422 cfg = GNUNET_CONFIGURATION_create ();
1423 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1426 GNUNET_break (0); /* Configuration parsing error */
1427 GNUNET_free (config);
1428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1431 GNUNET_free (config);
1432 peer = GNUNET_malloc (sizeof (struct Peer));
1434 peer->id = ntohl (msg->peer_id);
1435 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1436 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1440 if (NULL == peer->peer)
1442 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1449 peer_list_add (peer);
1450 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1451 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1452 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1453 reply->peer_id = msg->peer_id;
1454 reply->operation_id = msg->operation_id;
1455 queue_message (client, &reply->header);
1456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1460 /* Forward peer create request */
1461 route = find_dest_route (host_id);
1465 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1468 pc_ctxt = GNUNET_malloc (sizeof (struct PeerCreateContext));
1469 pc_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1470 /* To be continued .. :) */
1475 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1478 * @param client identification of the client
1479 * @param message the actual message
1482 handle_peer_destroy (void *cls,
1483 struct GNUNET_SERVER_Client *client,
1484 const struct GNUNET_MessageHeader *message)
1486 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1487 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1490 uint16_t reply_size;
1492 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1493 peer_id = ntohl (msg->peer_id);
1494 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1495 peer_id, GNUNET_ntohll (msg->operation_id));
1496 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1499 /* FIXME: Reply with failure event message or forward to slave controller */
1500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1503 peer = peer_list[peer_id];
1504 GNUNET_TESTING_peer_destroy (peer->peer);
1505 GNUNET_CONFIGURATION_destroy (peer->cfg);
1506 peer_list_remove (peer);
1509 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1510 reply = GNUNET_malloc (reply_size);
1511 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1512 reply->header.size = htons (reply_size);
1513 reply->operation_id = msg->operation_id;
1514 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1515 queue_message (client, &reply->header);
1516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1521 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1524 * @param client identification of the client
1525 * @param message the actual message
1528 handle_peer_start (void *cls,
1529 struct GNUNET_SERVER_Client *client,
1530 const struct GNUNET_MessageHeader *message)
1532 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1533 struct GNUNET_TESTBED_PeerEventMessage *reply;
1536 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1537 peer_id = ntohl (msg->peer_id);
1538 if ((peer_id >= peer_list_size)
1539 || (NULL == peer_list[peer_id]))
1542 /* FIXME: reply with failure message or forward to slave controller */
1543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1546 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1548 /* FIXME: return FAILURE message */
1550 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1553 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1554 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1555 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1556 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1557 reply->host_id = htonl (master_context->host_id);
1558 reply->peer_id = msg->peer_id;
1559 reply->operation_id = msg->operation_id;
1560 queue_message (client, &reply->header);
1561 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1566 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1569 * @param client identification of the client
1570 * @param message the actual message
1573 handle_peer_stop (void *cls,
1574 struct GNUNET_SERVER_Client *client,
1575 const struct GNUNET_MessageHeader *message)
1577 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1578 struct GNUNET_TESTBED_PeerEventMessage *reply;
1581 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1582 peer_id = ntohl (msg->peer_id);
1583 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1585 GNUNET_break (0); /* FIXME: route to slave? */
1586 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1589 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1591 /* FIXME: return FAILURE message */
1593 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1596 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1597 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1598 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1599 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1600 reply->host_id = htonl (master_context->host_id);
1601 reply->peer_id = msg->peer_id;
1602 reply->operation_id = msg->operation_id;
1603 queue_message (client, &reply->header);
1604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1609 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1612 * @param client identification of the client
1613 * @param message the actual message
1616 handle_peer_get_config (void *cls,
1617 struct GNUNET_SERVER_Client *client,
1618 const struct GNUNET_MessageHeader *message)
1620 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1621 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1629 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1630 peer_id = ntohl (msg->peer_id);
1631 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1633 /* FIXME: return FAILURE message */
1635 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1637 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1639 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1640 GNUNET_free (config);
1641 msize = xc_size + sizeof (struct
1642 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1643 reply = GNUNET_realloc (xconfig, msize);
1644 (void) memmove (&reply[1], reply, xc_size);
1645 reply->header.size = htons (msize);
1646 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1647 reply->peer_id = msg->peer_id;
1648 reply->operation_id = msg->operation_id;
1649 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1650 &reply->peer_identity);
1651 reply->config_size = htons ((uint16_t) c_size);
1652 queue_message (client, &reply->header);
1653 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1658 * Task for cleaing up overlay connect context structure
1660 * @param cls the overlay connect context
1661 * @param tc the task context
1664 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1666 struct OverlayConnectContext *occ = cls;
1668 GNUNET_free_non_null (occ->emsg);
1669 GNUNET_free_non_null (occ->hello);
1670 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1671 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1672 if (NULL != occ->ch)
1673 GNUNET_CORE_disconnect (occ->ch);
1674 if (NULL != occ->ghh)
1675 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1676 if (NULL != occ->p1th)
1677 GNUNET_TRANSPORT_disconnect (occ->p1th);
1678 if (NULL != occ->p2th)
1679 GNUNET_TRANSPORT_disconnect (occ->p2th);
1685 * Task which will be run when overlay connect request has been timed out
1691 timeout_overlay_connect (void *cls,
1692 const struct GNUNET_SCHEDULER_TaskContext *tc)
1694 struct OverlayConnectContext *occ = cls;
1696 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1697 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1698 GNUNET_SERVER_client_drop (occ->client);
1699 occ_cleanup (occ, tc);
1705 * Function called to notify transport users that another
1706 * peer connected to us.
1708 * @param cls closure
1709 * @param new_peer the peer that connected
1710 * @param ats performance data
1711 * @param ats_count number of entries in ats (excluding 0-termination)
1714 overlay_connect_notify (void *cls,
1715 const struct GNUNET_PeerIdentity * new_peer,
1716 const struct GNUNET_ATS_Information * ats,
1717 unsigned int ats_count)
1719 struct OverlayConnectContext *occ = cls;
1720 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1722 char *other_peer_str;
1724 LOG_DEBUG ("Overlay connect notify\n");
1725 if (0 == memcmp (new_peer, &occ->peer_identity,
1726 sizeof (struct GNUNET_PeerIdentity)))
1728 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1729 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1730 if (0 != memcmp (new_peer, &occ->other_peer_identity,
1731 sizeof (struct GNUNET_PeerIdentity)))
1733 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1734 new_peer_str, other_peer_str);
1735 GNUNET_free (new_peer_str);
1736 GNUNET_free (other_peer_str);
1739 LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1740 GNUNET_free (new_peer_str);
1741 GNUNET_free (other_peer_str);
1742 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1744 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1745 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1747 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
1748 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1749 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1750 GNUNET_free_non_null (occ->emsg);
1752 GNUNET_TRANSPORT_disconnect (occ->p1th);
1754 /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
1755 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1756 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1757 msg->header.size = htons (sizeof (struct
1758 GNUNET_TESTBED_ConnectionEventMessage));
1759 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1760 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1761 msg->peer1 = htonl (occ->peer->id);
1762 msg->peer2 = htonl (occ->other_peer->id);
1763 msg->operation_id = GNUNET_htonll (occ->op_id);
1764 queue_message (occ->client, &msg->header);
1765 GNUNET_SERVER_client_drop (occ->client);
1766 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
1771 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1773 struct OverlayConnectContext *occ = cls;
1774 char *other_peer_str;
1776 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1777 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1779 GNUNET_assert (NULL != occ->hello);
1780 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1781 LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
1782 GNUNET_free (other_peer_str);
1783 GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
1784 GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
1785 occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1790 * Test for checking whether HELLO message is empty
1792 * @param cls empty flag to set
1793 * @param address the HELLO
1794 * @param expiration expiration of the HELLO
1798 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
1799 struct GNUNET_TIME_Absolute expiration)
1809 * Function called whenever there is an update to the
1810 * HELLO of peers in the OverlayConnectClosure
1812 * @param cls closure
1813 * @param hello our updated HELLO
1816 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1818 struct OverlayConnectContext *occ = cls;
1822 msize = ntohs (hello->size);
1825 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1826 "HELLO message of peer %s is of size 0\n",
1827 &occ->other_peer_identity);
1832 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
1833 GNUNET_NO, &test_address, &empty);
1834 if (GNUNET_YES == empty)
1836 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
1839 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
1840 occ->hello = GNUNET_malloc (msize);
1841 memcpy (occ->hello, hello, msize);
1842 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1844 GNUNET_TRANSPORT_disconnect (occ->p2th);
1846 GNUNET_free_non_null (occ->emsg);
1847 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
1848 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1853 * Function called after GNUNET_CORE_connect has succeeded (or failed
1854 * for good). Note that the private key of the peer is intentionally
1855 * not exposed here; if you need it, your process should try to read
1856 * the private key file directly (which should work if you are
1859 * @param cls closure
1860 * @param server handle to the server, NULL if we failed
1861 * @param my_identity ID of this peer, NULL if we failed
1864 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
1865 const struct GNUNET_PeerIdentity *my_identity)
1867 struct OverlayConnectContext *occ = cls;
1869 GNUNET_free_non_null (occ->emsg);
1871 memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
1873 GNUNET_TRANSPORT_connect (occ->peer->cfg, &occ->peer_identity, NULL, NULL,
1875 /* Connect to the transport of 2nd peer and get its HELLO message */
1876 GNUNET_TESTING_peer_get_identity (occ->other_peer->peer,
1877 &occ->other_peer_identity);
1879 GNUNET_TRANSPORT_connect (occ->other_peer->cfg, &occ->other_peer_identity,
1880 NULL, NULL, NULL, NULL);
1881 if ((NULL == occ->p1th) || (NULL == occ->p2th))
1883 occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
1886 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s
1887 (&occ->other_peer_identity));
1888 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
1889 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
1893 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1894 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1899 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1902 * @param client identification of the client
1903 * @param message the actual message
1906 handle_overlay_connect (void *cls,
1907 struct GNUNET_SERVER_Client *client,
1908 const struct GNUNET_MessageHeader *message)
1910 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1911 struct OverlayConnectContext *occ;
1912 struct GNUNET_CORE_MessageHandler no_handlers[] = {
1918 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1919 p1 = ntohl (msg->peer1);
1920 p2 = ntohl (msg->peer2);
1921 GNUNET_assert (p1 < peer_list_size);
1922 GNUNET_assert (NULL != peer_list[p1]);
1923 GNUNET_assert (p2 < peer_list_size);
1924 GNUNET_assert (NULL != peer_list[p2]);
1925 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1926 GNUNET_SERVER_client_keep (client);
1927 occ->client = client;
1928 occ->state = OCC_STATE_INIT;
1929 occ->peer = peer_list[p1];
1930 occ->other_peer = peer_list[p2];
1931 occ->op_id = GNUNET_ntohll (msg->operation_id);
1933 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1934 (GNUNET_TIME_UNIT_SECONDS, 30),
1935 &timeout_overlay_connect, occ);
1936 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
1937 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
1939 GNUNET_CORE_connect (occ->peer->cfg, occ, &core_startup_cb,
1940 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
1941 GNUNET_NO, no_handlers);
1942 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1947 * Iterator over hash map entries.
1949 * @param cls closure
1950 * @param key current key code
1951 * @param value value in the hash map
1952 * @return GNUNET_YES if we should continue to
1957 ss_map_free_iterator (void *cls,
1958 const struct GNUNET_HashCode * key, void *value)
1960 struct SharedService *ss = value;
1962 GNUNET_assert (GNUNET_YES ==
1963 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1964 GNUNET_free (ss->name);
1971 * Task to clean up and shutdown nicely
1974 * @param tc the TaskContext from scheduler
1977 shutdown_task (void *cls,
1978 const struct GNUNET_SCHEDULER_TaskContext *tc)
1980 struct LCFContextQueue *lcfq;
1983 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1984 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1985 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1987 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1988 if (NULL != lcfq_head)
1990 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1992 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1993 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1995 if (NULL != lcfq_head->lcf->rhandle)
1996 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1998 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1999 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2001 GNUNET_free (lcfq->lcf->sxcfg);
2002 GNUNET_free (lcfq->lcf);
2003 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2006 /* Clear peer list */
2007 for (id = 0; id < peer_list_size; id++)
2008 if (NULL != peer_list[id])
2010 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
2011 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
2012 GNUNET_free (peer_list[id]);
2014 GNUNET_free_non_null (peer_list);
2015 /* Clear host list */
2016 for (id = 0; id < host_list_size; id++)
2017 if (NULL != host_list[id])
2018 GNUNET_TESTBED_host_destroy (host_list[id]);
2019 GNUNET_free_non_null (host_list);
2020 /* Clear route list */
2021 for (id = 0; id < route_list_size; id++)
2022 if (NULL != route_list[id])
2023 GNUNET_free (route_list[id]);
2024 GNUNET_free_non_null (route_list);
2025 /* Clear slave_list */
2026 for (id = 0; id < slave_list_size; id++)
2027 if (NULL != slave_list[id])
2029 GNUNET_assert (NULL != slave_list[id]->controller);
2030 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2031 if (NULL != slave_list[id]->controller_proc)
2032 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2034 if (NULL != master_context)
2036 GNUNET_free_non_null (master_context->master_ip);
2037 if (NULL != master_context->system)
2038 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2039 GNUNET_free (master_context);
2040 master_context = NULL;
2046 * Callback for client disconnect
2049 * @param client the client which has disconnected
2052 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2054 if (NULL == master_context)
2056 if (client == master_context->client)
2058 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2059 GNUNET_SERVER_client_drop (client);
2060 /* should not be needed as we're terminated by failure to read
2061 from stdin, but if stdin fails for some reason, this shouldn't
2062 hurt for now --- might need to revise this later if we ever
2063 decide that master connections might be temporarily down
2065 //GNUNET_SCHEDULER_shutdown ();
2073 * @param cls closure
2074 * @param server the initialized server
2075 * @param cfg configuration to use
2078 testbed_run (void *cls,
2079 struct GNUNET_SERVER_Handle *server,
2080 const struct GNUNET_CONFIGURATION_Handle *cfg)
2082 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
2084 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2085 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2086 {&handle_configure_shared_service, NULL,
2087 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2088 {&handle_link_controllers, NULL,
2089 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2090 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2091 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2092 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2093 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2094 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2095 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2096 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2097 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2098 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2099 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2100 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2104 GNUNET_SERVER_add_handlers (server,
2106 GNUNET_SERVER_disconnect_notify (server,
2107 &client_disconnect_cb,
2109 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2111 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2114 LOG_DEBUG ("Testbed startup complete\n");
2119 * The starting point of execution
2121 int main (int argc, char *const *argv)
2123 //sleep (30); /* Debugging */
2126 GNUNET_SERVICE_run (argc,
2129 GNUNET_SERVICE_OPTION_NONE,