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 * The master context; generated with the first INIT message
411 static struct Context *master_context;
418 * Current Transmit Handle; NULL if no notify transmit exists currently
420 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
427 * The head for the LCF queue
429 static struct LCFContextQueue *lcfq_head;
432 * The tail for the LCF queue
434 static struct LCFContextQueue *lcfq_tail;
437 * The message queue head
439 static struct MessageQueue *mq_head;
442 * The message queue tail
444 static struct MessageQueue *mq_tail;
449 static struct GNUNET_TESTBED_Host **host_list;
454 static struct Route **route_list;
457 * A list of directly linked neighbours
459 static struct Slave **slave_list;
462 * A list of peers we own locally
464 static struct Peer **peer_list;
467 * The hashmap of shared services
469 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
472 * The size of the host list
474 static uint32_t host_list_size;
477 * The size of the route list
479 static uint32_t route_list_size;
482 * The size of directly linked neighbours list
484 static uint32_t slave_list_size;
487 * The size of the peer list
489 static uint32_t peer_list_size;
496 * The lcf_task handle
498 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
501 * The shutdown task handle
503 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
507 * Function called to notify a client about the connection begin ready to queue
508 * more data. "buf" will be NULL and "size" zero if the connection was closed
509 * for writing in the meantime.
512 * @param size number of bytes available in buf
513 * @param buf where the callee should write the message
514 * @return number of bytes written to buf
517 transmit_ready_notify (void *cls, size_t size, void *buf)
519 struct MessageQueue *mq_entry;
521 transmit_handle = NULL;
523 GNUNET_assert (NULL != mq_entry);
526 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
527 size = ntohs (mq_entry->msg->size);
528 memcpy (buf, mq_entry->msg, size);
529 GNUNET_free (mq_entry->msg);
530 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
531 GNUNET_free (mq_entry);
533 if (NULL != mq_entry)
535 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
536 ntohs (mq_entry->msg->size),
537 GNUNET_TIME_UNIT_FOREVER_REL,
538 &transmit_ready_notify, NULL);
544 * Queues a message in send queue for sending to the service
546 * @param client the client to whom the queued message has to be sent
547 * @param msg the message to queue
550 queue_message (struct GNUNET_SERVER_Client *client,
551 struct GNUNET_MessageHeader *msg)
553 struct MessageQueue *mq_entry;
557 type = ntohs (msg->type);
558 size = ntohs (msg->size);
559 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
560 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
561 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
563 mq_entry->client = client;
564 LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
566 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
567 if (NULL == transmit_handle)
569 GNUNET_SERVER_notify_transmit_ready (client, size,
570 GNUNET_TIME_UNIT_FOREVER_REL,
571 &transmit_ready_notify, NULL);
576 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
578 * @param ptr the memory block to realloc
579 * @param size the size of ptr
580 * @param new_size the size to which ptr has to be realloc'ed
581 * @return the newly reallocated memory block
584 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
586 ptr = GNUNET_realloc (ptr, new_size);
588 ptr = memset (ptr + size, 0, new_size - size);
594 * Function to add a host to the current list of known hosts
596 * @param host the host to add
597 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
601 host_list_add (struct GNUNET_TESTBED_Host *host)
605 host_id = GNUNET_TESTBED_host_get_id_ (host);
606 if (host_list_size <= host_id)
609 TESTBED_realloc (host_list,
610 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
611 sizeof (struct GNUNET_TESTBED_Host *) *
612 (host_list_size + LIST_GROW_STEP));
613 host_list_size += LIST_GROW_STEP;
615 if (NULL != host_list[host_id])
617 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
618 return GNUNET_SYSERR;
620 host_list[host_id] = host;
626 * Adds a route to the route list
628 * @param route the route to add
631 route_list_add (struct Route *route)
633 if (route->dest >= route_list_size)
636 TESTBED_realloc (route_list,
637 sizeof (struct Route *) * route_list_size,
638 sizeof (struct Route *) *
639 (route_list_size + LIST_GROW_STEP));
640 route_list_size += LIST_GROW_STEP;
642 GNUNET_assert (NULL == route_list[route->dest]);
643 route_list[route->dest] = route;
648 * Adds a slave to the slave array
650 * @param slave the slave controller to add
653 slave_list_add (struct Slave *slave)
655 if (slave->host_id >= slave_list_size)
657 slave_list = TESTBED_realloc (slave_list,
658 sizeof (struct Slave *) *slave_list_size,
659 sizeof (struct Slave *) *
660 (slave_list_size + LIST_GROW_STEP));
661 slave_list_size += LIST_GROW_STEP;
663 GNUNET_assert (NULL == slave_list[slave->host_id]);
664 slave_list[slave->host_id] = slave;
669 * Adds a peer to the peer array
671 * @param peer the peer to add
674 peer_list_add (struct Peer *peer)
676 if (peer->id >= peer_list_size)
678 peer_list = TESTBED_realloc (peer_list,
679 sizeof (struct Peer *) * peer_list_size,
680 sizeof (struct Peer *) *
681 (peer_list_size + LIST_GROW_STEP));
682 peer_list_size += LIST_GROW_STEP;
684 GNUNET_assert (NULL == peer_list[peer->id]);
685 peer_list[peer->id] = peer;
690 * Removes a the give peer from the peer array
692 * @param peer the peer to be removed
695 peer_list_remove (struct Peer *peer)
699 peer_list[peer->id] = NULL;
700 while (peer_list_size >= LIST_GROW_STEP)
702 for (id = peer_list_size - 1;
703 id > peer_list_size - LIST_GROW_STEP; id--)
704 if (NULL != peer_list[id])
706 if (id != peer_list_size - LIST_GROW_STEP)
708 peer_list_size -= LIST_GROW_STEP;
710 peer_list = GNUNET_realloc (peer_list, sizeof (struct GNUNET_TESTBED_Peer*)
716 * Routes message to a host given its host_id
718 * @param host_id the id of the destination host
719 * @param msg the message to be routed
722 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
729 * Send operation failure message to client
731 * @param client the client to which the failure message has to be sent to
732 * @param operation_id the id of the failed operation
733 * @param emsg the error message; can be NULL
736 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
737 uint64_t operation_id,
740 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
744 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
745 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
747 msg = GNUNET_malloc (msize);
748 msg->header.size = htons (msize);
749 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT);
750 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
751 msg->operation_id = GNUNET_htonll (operation_id);
753 memcpy (&msg[1], emsg, emsg_len);
754 queue_message (client, &msg->header);
759 * The Link Controller forwarding task
761 * @param cls the LCFContext
762 * @param tc the Task context from scheduler
765 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
769 * Completion callback for host registrations while forwarding Link Controller messages
771 * @param cls the LCFContext
772 * @param emsg the error message; NULL if host registration is successful
775 lcf_proc_cc (void *cls, const char *emsg)
777 struct LCFContext *lcf = cls;
780 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
785 goto registration_error;
786 lcf->state = DELEGATED_HOST_REGISTERED;
787 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
789 case DELEGATED_HOST_REGISTERED:
791 goto registration_error;
792 lcf->state = SLAVE_HOST_REGISTERED;
793 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
796 GNUNET_assert (0); /* Shouldn't reach here */
801 LOG (GNUNET_ERROR_TYPE_WARNING,
802 "Host registration failed with message: %s\n", emsg);
803 lcf->state = FINISHED;
804 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
809 * The Link Controller forwarding task
811 * @param cls the LCFContext
812 * @param tc the Task context from scheduler
815 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
817 struct LCFContext *lcf = cls;
818 struct LCFContextQueue *lcfq;
820 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
825 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
826 lcf->gateway->controller))
829 GNUNET_TESTBED_register_host (lcf->gateway->controller,
830 host_list[lcf->delegated_host_id],
835 lcf->state = DELEGATED_HOST_REGISTERED;
836 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
839 case DELEGATED_HOST_REGISTERED:
841 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
842 lcf->gateway->controller))
845 GNUNET_TESTBED_register_host (lcf->gateway->controller,
846 host_list[lcf->slave_host_id],
851 lcf->state = SLAVE_HOST_REGISTERED;
852 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
855 case SLAVE_HOST_REGISTERED:
856 GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
857 host_list[lcf->delegated_host_id],
858 host_list[lcf->slave_host_id],
859 lcf->sxcfg, lcf->sxcfg_size,
861 lcf->is_subordinate);
862 lcf->state = FINISHED;
863 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
867 GNUNET_assert (lcfq->lcf == lcf);
868 GNUNET_free (lcf->sxcfg);
870 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
872 if (NULL != lcfq_head)
874 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
880 * Callback for event from slave controllers
882 * @param cls struct Slave *
883 * @param event information about the event
886 slave_event_callback(void *cls,
887 const struct GNUNET_TESTBED_EventInformation *event)
894 * Callback to signal successfull startup of the controller process
896 * @param cls the closure from GNUNET_TESTBED_controller_start()
897 * @param cfg the configuration with which the controller has been started;
898 * NULL if status is not GNUNET_OK
899 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
900 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
903 slave_status_callback (void *cls,
904 const struct GNUNET_CONFIGURATION_Handle *cfg,
907 struct Slave *slave = cls;
909 if (GNUNET_SYSERR == status)
911 slave->controller_proc = NULL;
912 LOG (GNUNET_ERROR_TYPE_WARNING,
913 "Unexpected slave shutdown\n");
914 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
918 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
919 master_context->event_mask,
920 &slave_event_callback, slave);
925 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
928 * @param client identification of the client
929 * @param message the actual message
932 handle_init (void *cls,
933 struct GNUNET_SERVER_Client *client,
934 const struct GNUNET_MessageHeader *message)
936 const struct GNUNET_TESTBED_InitMessage *msg;
937 struct GNUNET_TESTBED_Host *host;
938 const char *controller_hostname;
941 if (NULL != master_context)
944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
947 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
948 msize = ntohs (message->size);
949 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
955 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
956 controller_hostname = (const char *) &msg[1];
957 if ('\0' != controller_hostname[msize - 1])
960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
963 master_context = GNUNET_malloc (sizeof (struct Context));
964 master_context->client = client;
965 master_context->host_id = ntohl (msg->host_id);
966 master_context->master_ip = GNUNET_strdup (controller_hostname);
967 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
968 master_context->system =
969 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
970 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
972 host_list_add (host);
973 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
974 GNUNET_SERVER_client_keep (client);
975 LOG_DEBUG ("Created master context with host ID: %u\n",
976 master_context->host_id);
977 GNUNET_SERVER_receive_done (client, GNUNET_OK);
982 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
985 * @param client identification of the client
986 * @param message the actual message
989 handle_add_host (void *cls,
990 struct GNUNET_SERVER_Client *client,
991 const struct GNUNET_MessageHeader *message)
993 struct GNUNET_TESTBED_Host *host;
994 const struct GNUNET_TESTBED_AddHostMessage *msg;
995 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1000 uint16_t username_length;
1001 uint16_t hostname_length;
1002 uint16_t reply_size;
1005 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1006 msize = ntohs (msg->header.size);
1007 username = (char *) &(msg[1]);
1008 username_length = ntohs (msg->user_name_length);
1009 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1010 + username_length + 1)); /* msg must contain hostname */
1011 if (0 != username_length)
1012 GNUNET_assert ('\0' == username[username_length]);
1013 username_length = (0 == username_length) ? 0 : username_length + 1;
1014 hostname = username + username_length;
1015 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1017 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1018 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1019 host_id = ntohl (msg->host_id);
1020 LOG_DEBUG ("Received ADDHOST message\n");
1021 LOG_DEBUG ("-------host id: %u\n", host_id);
1022 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
1023 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
1024 else LOG_DEBUG ("-------username: NULL\n");
1025 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1026 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1027 ntohs (msg->ssh_port));
1028 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1029 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1030 if (GNUNET_OK != host_list_add (host))
1032 /* We are unable to add a host */
1033 emsg = "A host exists with given host-id";
1034 LOG_DEBUG ("%s: %u", emsg, host_id);
1035 GNUNET_TESTBED_host_destroy (host);
1036 reply_size += strlen (emsg) + 1;
1037 reply = GNUNET_malloc (reply_size);
1038 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1041 reply = GNUNET_malloc (reply_size);
1042 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1043 reply->header.size = htons (reply_size);
1044 reply->host_id = htonl (host_id);
1045 queue_message (client, &reply->header);
1050 * Iterator over hash map entries.
1052 * @param cls closure
1053 * @param key current key code
1054 * @param value value in the hash map
1055 * @return GNUNET_YES if we should continue to
1059 int ss_exists_iterator (void *cls,
1060 const struct GNUNET_HashCode * key,
1063 struct SharedService *queried_ss = cls;
1064 struct SharedService *ss = value;
1066 if (0 == strcmp (ss->name, queried_ss->name))
1074 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1077 * @param client identification of the client
1078 * @param message the actual message
1081 handle_configure_shared_service (void *cls,
1082 struct GNUNET_SERVER_Client *client,
1083 const struct GNUNET_MessageHeader *message)
1085 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1086 struct SharedService *ss;
1088 struct GNUNET_HashCode hash;
1090 uint16_t service_name_size;
1092 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1093 msg_size = ntohs (message->size);
1094 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1100 service_name_size = msg_size -
1101 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1102 service_name = (char *) &msg[1];
1103 if ('\0' != service_name[service_name_size - 1])
1106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1109 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1110 service_name, ntohl (msg->num_peers));
1111 if (ntohl (msg->host_id) != master_context->host_id)
1113 route_message (ntohl (msg->host_id), message);
1114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1117 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1118 ss = GNUNET_malloc (sizeof (struct SharedService));
1119 ss->name = strdup (service_name);
1120 ss->num_shared = ntohl (msg->num_peers);
1121 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1122 if (GNUNET_SYSERR ==
1123 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1124 &ss_exists_iterator, ss))
1126 LOG (GNUNET_ERROR_TYPE_WARNING,
1127 "Service %s already configured as a shared service. "
1128 "Ignoring service sharing request \n", ss->name);
1129 GNUNET_free (ss->name);
1133 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1134 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1139 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1142 * @param client identification of the client
1143 * @param message the actual message
1146 handle_link_controllers (void *cls,
1147 struct GNUNET_SERVER_Client *client,
1148 const struct GNUNET_MessageHeader *message)
1150 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1151 struct GNUNET_CONFIGURATION_Handle *cfg;
1152 struct LCFContextQueue *lcfq;
1153 struct Route *route;
1154 struct Route *new_route;
1158 uint32_t delegated_host_id;
1159 uint32_t slave_host_id;
1162 if (NULL == master_context)
1165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1168 msize = ntohs (message->size);
1169 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1175 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1176 delegated_host_id = ntohl (msg->delegated_host_id);
1177 if (delegated_host_id == master_context->host_id)
1180 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1184 if ((delegated_host_id >= host_list_size) ||
1185 (NULL == host_list[delegated_host_id]))
1187 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1191 slave_host_id = ntohl (msg->slave_host_id);
1192 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1194 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1198 if (slave_host_id == delegated_host_id)
1200 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1204 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1205 config_size = ntohs (msg->config_size);
1207 if (slave_host_id == master_context->host_id) /* Link from us */
1209 struct Slave *slave;
1211 if ((delegated_host_id < slave_list_size) &&
1212 (NULL != slave_list[delegated_host_id])) /* We have already added */
1214 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1219 config = GNUNET_malloc (config_size);
1220 dest_size = (uLongf) config_size;
1221 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1222 (const Bytef *) &msg[1], (uLong) msize))
1224 GNUNET_break (0); /* Compression error */
1225 GNUNET_free (config);
1226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1229 if (config_size == dest_size)
1231 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1232 GNUNET_free (config);
1233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1236 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1237 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1240 GNUNET_break (0); /* Configuration parsing error */
1241 GNUNET_free (config);
1242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1245 GNUNET_free (config);
1246 if ((delegated_host_id < slave_list_size) &&
1247 (NULL != slave_list[delegated_host_id]))
1249 GNUNET_break (0); /* Configuration parsing error */
1250 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1253 slave = GNUNET_malloc (sizeof (struct Slave));
1254 slave->host_id = delegated_host_id;
1255 slave_list_add (slave);
1256 if (1 == msg->is_subordinate)
1258 slave->controller_proc =
1259 GNUNET_TESTBED_controller_start (master_context->master_ip,
1260 host_list[slave->host_id],
1261 cfg, &slave_status_callback,
1266 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1267 master_context->event_mask,
1268 &slave_event_callback, slave);
1270 GNUNET_CONFIGURATION_destroy (cfg);
1271 new_route = GNUNET_malloc (sizeof (struct Route));
1272 new_route->dest = delegated_host_id;
1273 new_route->thru = master_context->host_id;
1274 route_list_add (new_route);
1275 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1279 /* Route the request */
1280 if (slave_host_id >= route_list_size)
1282 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1286 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1287 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1288 lcfq->lcf->delegated_host_id = delegated_host_id;
1289 lcfq->lcf->slave_host_id = slave_host_id;
1290 while (NULL != (route = route_list[slave_host_id]))
1292 if (route->thru == master_context->host_id)
1294 slave_host_id = route->thru;
1296 GNUNET_assert (NULL != route); /* because we add routes carefully */
1297 GNUNET_assert (route->dest < slave_list_size);
1298 GNUNET_assert (NULL != slave_list[route->dest]);
1299 lcfq->lcf->is_subordinate =
1300 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1301 lcfq->lcf->state = INIT;
1302 lcfq->lcf->gateway = slave_list[route->dest];
1303 lcfq->lcf->sxcfg_size = msize;
1304 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1305 lcfq->lcf->scfg_size = config_size;
1306 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1307 if (NULL == lcfq_head)
1309 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1310 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1311 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1314 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1315 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1316 new_route = GNUNET_malloc (sizeof (struct Route));
1317 new_route->dest = delegated_host_id;
1318 new_route->thru = route->dest;
1319 route_list_add (new_route);
1324 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1327 * @param client identification of the client
1328 * @param message the actual message
1331 handle_peer_create (void *cls,
1332 struct GNUNET_SERVER_Client *client,
1333 const struct GNUNET_MessageHeader *message)
1335 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1336 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1337 struct GNUNET_CONFIGURATION_Handle *cfg;
1341 uint32_t config_size;
1345 msize = ntohs (message->size);
1346 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1348 GNUNET_break (0); /* We need configuration */
1349 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1352 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1353 if (ntohl (msg->host_id) == master_context->host_id)
1358 /* We are responsible for this peer */
1359 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1360 config_size = ntohl (msg->config_size);
1361 config = GNUNET_malloc (config_size);
1362 dest_size = config_size;
1363 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1364 (const Bytef *) &msg[1], (uLong) msize)))
1366 GNUNET_break (0); /* uncompression error */
1367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1370 if (config_size != dest_size)
1372 GNUNET_break (0);/* Uncompressed config size mismatch */
1373 GNUNET_free (config);
1374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1377 cfg = GNUNET_CONFIGURATION_create ();
1378 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1381 GNUNET_break (0); /* Configuration parsing error */
1382 GNUNET_free (config);
1383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1386 GNUNET_free (config);
1387 peer = GNUNET_malloc (sizeof (struct Peer));
1389 peer->id = ntohl (msg->peer_id);
1390 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1391 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1395 if (NULL == peer->peer)
1397 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1404 peer_list_add (peer);
1405 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1406 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1407 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1408 reply->peer_id = msg->peer_id;
1409 reply->operation_id = msg->operation_id;
1410 queue_message (client, &reply->header);
1411 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1415 /* FIXME: Forward the peer to other host */
1417 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1422 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1425 * @param client identification of the client
1426 * @param message the actual message
1429 handle_peer_destroy (void *cls,
1430 struct GNUNET_SERVER_Client *client,
1431 const struct GNUNET_MessageHeader *message)
1433 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1434 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1437 uint16_t reply_size;
1439 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1440 peer_id = ntohl (msg->peer_id);
1441 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1442 peer_id, GNUNET_ntohll (msg->operation_id));
1443 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1446 /* FIXME: Reply with failure event message or forward to slave controller */
1447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1450 peer = peer_list[peer_id];
1451 GNUNET_TESTING_peer_destroy (peer->peer);
1452 GNUNET_CONFIGURATION_destroy (peer->cfg);
1453 peer_list_remove (peer);
1456 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1457 reply = GNUNET_malloc (reply_size);
1458 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1459 reply->header.size = htons (reply_size);
1460 reply->operation_id = msg->operation_id;
1461 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1462 queue_message (client, &reply->header);
1463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1468 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1471 * @param client identification of the client
1472 * @param message the actual message
1475 handle_peer_start (void *cls,
1476 struct GNUNET_SERVER_Client *client,
1477 const struct GNUNET_MessageHeader *message)
1479 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1480 struct GNUNET_TESTBED_PeerEventMessage *reply;
1483 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1484 peer_id = ntohl (msg->peer_id);
1485 if ((peer_id >= peer_list_size)
1486 || (NULL == peer_list[peer_id]))
1489 /* FIXME: reply with failure message or forward to slave controller */
1490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1493 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1495 /* FIXME: return FAILURE message */
1497 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1500 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1501 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1502 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1503 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1504 reply->host_id = htonl (master_context->host_id);
1505 reply->peer_id = msg->peer_id;
1506 reply->operation_id = msg->operation_id;
1507 queue_message (client, &reply->header);
1508 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1513 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1516 * @param client identification of the client
1517 * @param message the actual message
1520 handle_peer_stop (void *cls,
1521 struct GNUNET_SERVER_Client *client,
1522 const struct GNUNET_MessageHeader *message)
1524 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1525 struct GNUNET_TESTBED_PeerEventMessage *reply;
1528 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1529 peer_id = ntohl (msg->peer_id);
1530 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1532 GNUNET_break (0); /* FIXME: route to slave? */
1533 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1536 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1538 /* FIXME: return FAILURE message */
1540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1543 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1544 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1545 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1546 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1547 reply->host_id = htonl (master_context->host_id);
1548 reply->peer_id = msg->peer_id;
1549 reply->operation_id = msg->operation_id;
1550 queue_message (client, &reply->header);
1551 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1556 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1559 * @param client identification of the client
1560 * @param message the actual message
1563 handle_peer_get_config (void *cls,
1564 struct GNUNET_SERVER_Client *client,
1565 const struct GNUNET_MessageHeader *message)
1567 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1568 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1576 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1577 peer_id = ntohl (msg->peer_id);
1578 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1580 /* FIXME: return FAILURE message */
1582 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1584 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1586 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1587 GNUNET_free (config);
1588 msize = xc_size + sizeof (struct
1589 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1590 reply = GNUNET_realloc (xconfig, msize);
1591 (void) memmove (&reply[1], reply, xc_size);
1592 reply->header.size = htons (msize);
1593 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1594 reply->peer_id = msg->peer_id;
1595 reply->operation_id = msg->operation_id;
1596 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1597 &reply->peer_identity);
1598 reply->config_size = htons ((uint16_t) c_size);
1599 queue_message (client, &reply->header);
1600 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1605 * Task for cleaing up overlay connect context structure
1607 * @param cls the overlay connect context
1608 * @param tc the task context
1611 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1613 struct OverlayConnectContext *occ = cls;
1615 GNUNET_free_non_null (occ->emsg);
1616 GNUNET_free_non_null (occ->hello);
1617 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1618 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1619 if (NULL != occ->ch)
1620 GNUNET_CORE_disconnect (occ->ch);
1621 if (NULL != occ->ghh)
1622 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1623 if (NULL != occ->p1th)
1624 GNUNET_TRANSPORT_disconnect (occ->p1th);
1625 if (NULL != occ->p2th)
1626 GNUNET_TRANSPORT_disconnect (occ->p2th);
1632 * Task which will be run when overlay connect request has been timed out
1638 timeout_overlay_connect (void *cls,
1639 const struct GNUNET_SCHEDULER_TaskContext *tc)
1641 struct OverlayConnectContext *occ = cls;
1643 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1644 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1645 GNUNET_SERVER_client_drop (occ->client);
1646 occ_cleanup (occ, tc);
1652 * Function called to notify transport users that another
1653 * peer connected to us.
1655 * @param cls closure
1656 * @param new_peer the peer that connected
1657 * @param ats performance data
1658 * @param ats_count number of entries in ats (excluding 0-termination)
1661 overlay_connect_notify (void *cls,
1662 const struct GNUNET_PeerIdentity * new_peer,
1663 const struct GNUNET_ATS_Information * ats,
1664 unsigned int ats_count)
1666 struct OverlayConnectContext *occ = cls;
1667 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1669 char *other_peer_str;
1671 LOG_DEBUG ("Overlay connect notify\n");
1672 if (0 == memcmp (new_peer, &occ->peer_identity,
1673 sizeof (struct GNUNET_PeerIdentity)))
1675 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1676 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1677 if (0 != memcmp (new_peer, &occ->other_peer_identity,
1678 sizeof (struct GNUNET_PeerIdentity)))
1680 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1681 new_peer_str, other_peer_str);
1682 GNUNET_free (new_peer_str);
1683 GNUNET_free (other_peer_str);
1686 LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1687 GNUNET_free (new_peer_str);
1688 GNUNET_free (other_peer_str);
1689 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1691 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1692 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1694 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
1695 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1696 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1697 GNUNET_free_non_null (occ->emsg);
1699 GNUNET_TRANSPORT_disconnect (occ->p1th);
1701 /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
1702 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1703 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1704 msg->header.size = htons (sizeof (struct
1705 GNUNET_TESTBED_ConnectionEventMessage));
1706 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1707 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1708 msg->peer1 = htonl (occ->peer->id);
1709 msg->peer2 = htonl (occ->other_peer->id);
1710 msg->operation_id = GNUNET_htonll (occ->op_id);
1711 queue_message (occ->client, &msg->header);
1712 GNUNET_SERVER_client_drop (occ->client);
1713 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
1718 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1720 struct OverlayConnectContext *occ = cls;
1721 char *other_peer_str;
1723 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1724 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1726 GNUNET_assert (NULL != occ->hello);
1727 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1728 LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
1729 GNUNET_free (other_peer_str);
1730 GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
1731 GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
1732 occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1737 * Test for checking whether HELLO message is empty
1739 * @param cls empty flag to set
1740 * @param address the HELLO
1741 * @param expiration expiration of the HELLO
1745 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
1746 struct GNUNET_TIME_Absolute expiration)
1756 * Function called whenever there is an update to the
1757 * HELLO of peers in the OverlayConnectClosure
1759 * @param cls closure
1760 * @param hello our updated HELLO
1763 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1765 struct OverlayConnectContext *occ = cls;
1769 msize = ntohs (hello->size);
1772 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1773 "HELLO message of peer %s is of size 0\n",
1774 &occ->other_peer_identity);
1779 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
1780 GNUNET_NO, &test_address, &empty);
1781 if (GNUNET_YES == empty)
1783 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
1786 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
1787 occ->hello = GNUNET_malloc (msize);
1788 memcpy (occ->hello, hello, msize);
1789 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1791 GNUNET_TRANSPORT_disconnect (occ->p2th);
1793 GNUNET_free_non_null (occ->emsg);
1794 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
1795 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1800 * Function called after GNUNET_CORE_connect has succeeded (or failed
1801 * for good). Note that the private key of the peer is intentionally
1802 * not exposed here; if you need it, your process should try to read
1803 * the private key file directly (which should work if you are
1806 * @param cls closure
1807 * @param server handle to the server, NULL if we failed
1808 * @param my_identity ID of this peer, NULL if we failed
1811 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
1812 const struct GNUNET_PeerIdentity *my_identity)
1814 struct OverlayConnectContext *occ = cls;
1816 GNUNET_free_non_null (occ->emsg);
1818 memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
1820 GNUNET_TRANSPORT_connect (occ->peer->cfg, &occ->peer_identity, NULL, NULL,
1822 /* Connect to the transport of 2nd peer and get its HELLO message */
1823 GNUNET_TESTING_peer_get_identity (occ->other_peer->peer,
1824 &occ->other_peer_identity);
1826 GNUNET_TRANSPORT_connect (occ->other_peer->cfg, &occ->other_peer_identity,
1827 NULL, NULL, NULL, NULL);
1828 if ((NULL == occ->p1th) || (NULL == occ->p2th))
1830 occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
1833 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s
1834 (&occ->other_peer_identity));
1835 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
1836 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
1840 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1841 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1846 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1849 * @param client identification of the client
1850 * @param message the actual message
1853 handle_overlay_connect (void *cls,
1854 struct GNUNET_SERVER_Client *client,
1855 const struct GNUNET_MessageHeader *message)
1857 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1858 struct OverlayConnectContext *occ;
1859 struct GNUNET_CORE_MessageHandler no_handlers[] = {
1865 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1866 p1 = ntohl (msg->peer1);
1867 p2 = ntohl (msg->peer2);
1868 GNUNET_assert (p1 < peer_list_size);
1869 GNUNET_assert (NULL != peer_list[p1]);
1870 GNUNET_assert (p2 < peer_list_size);
1871 GNUNET_assert (NULL != peer_list[p2]);
1872 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1873 GNUNET_SERVER_client_keep (client);
1874 occ->client = client;
1875 occ->state = OCC_STATE_INIT;
1876 occ->peer = peer_list[p1];
1877 occ->other_peer = peer_list[p2];
1878 occ->op_id = GNUNET_ntohll (msg->operation_id);
1880 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1881 (GNUNET_TIME_UNIT_SECONDS, 30),
1882 &timeout_overlay_connect, occ);
1883 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
1884 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
1886 GNUNET_CORE_connect (occ->peer->cfg, occ, &core_startup_cb,
1887 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
1888 GNUNET_NO, no_handlers);
1889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1894 * Iterator over hash map entries.
1896 * @param cls closure
1897 * @param key current key code
1898 * @param value value in the hash map
1899 * @return GNUNET_YES if we should continue to
1904 ss_map_free_iterator (void *cls,
1905 const struct GNUNET_HashCode * key, void *value)
1907 struct SharedService *ss = value;
1909 GNUNET_assert (GNUNET_YES ==
1910 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1911 GNUNET_free (ss->name);
1918 * Task to clean up and shutdown nicely
1921 * @param tc the TaskContext from scheduler
1924 shutdown_task (void *cls,
1925 const struct GNUNET_SCHEDULER_TaskContext *tc)
1927 struct LCFContextQueue *lcfq;
1930 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1931 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1932 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1934 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1935 if (NULL != lcfq_head)
1937 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1939 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1940 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1942 if (NULL != lcfq_head->lcf->rhandle)
1943 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1945 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1946 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1948 GNUNET_free (lcfq->lcf->sxcfg);
1949 GNUNET_free (lcfq->lcf);
1950 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1953 /* Clear peer list */
1954 for (id = 0; id < peer_list_size; id++)
1955 if (NULL != peer_list[id])
1957 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1958 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1959 GNUNET_free (peer_list[id]);
1961 GNUNET_free_non_null (peer_list);
1962 /* Clear host list */
1963 for (id = 0; id < host_list_size; id++)
1964 if (NULL != host_list[id])
1965 GNUNET_TESTBED_host_destroy (host_list[id]);
1966 GNUNET_free_non_null (host_list);
1967 /* Clear route list */
1968 for (id = 0; id < route_list_size; id++)
1969 if (NULL != route_list[id])
1970 GNUNET_free (route_list[id]);
1971 GNUNET_free_non_null (route_list);
1972 /* Clear slave_list */
1973 for (id = 0; id < slave_list_size; id++)
1974 if (NULL != slave_list[id])
1976 GNUNET_assert (NULL != slave_list[id]->controller);
1977 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1978 if (NULL != slave_list[id]->controller_proc)
1979 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1981 if (NULL != master_context)
1983 GNUNET_free_non_null (master_context->master_ip);
1984 if (NULL != master_context->system)
1985 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1986 GNUNET_free (master_context);
1987 master_context = NULL;
1993 * Callback for client disconnect
1996 * @param client the client which has disconnected
1999 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2001 if (NULL == master_context)
2003 if (client == master_context->client)
2005 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2006 GNUNET_SERVER_client_drop (client);
2007 /* should not be needed as we're terminated by failure to read
2008 from stdin, but if stdin fails for some reason, this shouldn't
2009 hurt for now --- might need to revise this later if we ever
2010 decide that master connections might be temporarily down
2012 //GNUNET_SCHEDULER_shutdown ();
2020 * @param cls closure
2021 * @param server the initialized server
2022 * @param cfg configuration to use
2025 testbed_run (void *cls,
2026 struct GNUNET_SERVER_Handle *server,
2027 const struct GNUNET_CONFIGURATION_Handle *cfg)
2029 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
2031 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2032 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2033 {&handle_configure_shared_service, NULL,
2034 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2035 {&handle_link_controllers, NULL,
2036 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2037 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2038 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2039 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2040 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2041 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2042 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2043 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2044 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2045 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2046 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2047 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2051 GNUNET_SERVER_add_handlers (server,
2053 GNUNET_SERVER_disconnect_notify (server,
2054 &client_disconnect_cb,
2056 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2058 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2061 LOG_DEBUG ("Testbed startup complete\n");
2066 * The starting point of execution
2068 int main (int argc, char *const *argv)
2070 //sleep (30); /* Debugging */
2073 GNUNET_SERVICE_run (argc,
2076 GNUNET_SERVICE_OPTION_NONE,