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;
941 if (NULL != master_context)
944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
947 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
948 master_context = GNUNET_malloc (sizeof (struct Context));
949 master_context->client = client;
950 master_context->host_id = ntohl (msg->host_id);
951 GNUNET_assert (GNUNET_OK ==
952 GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
953 master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
954 if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
955 NULL, 0, NI_NUMERICHOST))
957 LOG (GNUNET_ERROR_TYPE_WARNING,
958 "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
960 GNUNET_free (master_context->master_ip);
964 if (0 == strcasecmp (master_context->master_ip, "localhost"))
965 { /* Hack for connections via unix sockets */
966 LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
967 GNUNET_free (master_context->master_ip);
968 master_context->master_ip = strdup ("127.0.0.1");
970 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
971 master_context->system =
972 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
973 host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
975 host_list_add (host);
976 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
977 GNUNET_SERVER_client_keep (client);
978 LOG_DEBUG ("Created master context with host ID: %u\n",
979 master_context->host_id);
980 GNUNET_SERVER_receive_done (client, GNUNET_OK);
985 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
988 * @param client identification of the client
989 * @param message the actual message
992 handle_add_host (void *cls,
993 struct GNUNET_SERVER_Client *client,
994 const struct GNUNET_MessageHeader *message)
996 struct GNUNET_TESTBED_Host *host;
997 const struct GNUNET_TESTBED_AddHostMessage *msg;
998 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1003 uint16_t username_length;
1004 uint16_t hostname_length;
1005 uint16_t reply_size;
1008 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1009 msize = ntohs (msg->header.size);
1010 username = (char *) &(msg[1]);
1011 username_length = ntohs (msg->user_name_length);
1012 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1013 + username_length + 1)); /* msg must contain hostname */
1014 if (0 != username_length)
1015 GNUNET_assert ('\0' == username[username_length]);
1016 username_length = (0 == username_length) ? 0 : username_length + 1;
1017 hostname = username + username_length;
1018 hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1020 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1021 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1022 host_id = ntohl (msg->host_id);
1023 LOG_DEBUG ("Received ADDHOST message\n");
1024 LOG_DEBUG ("-------host id: %u\n", host_id);
1025 if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
1026 if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
1027 else LOG_DEBUG ("-------username: NULL\n");
1028 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1029 host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1030 ntohs (msg->ssh_port));
1031 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1032 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1033 if (GNUNET_OK != host_list_add (host))
1035 /* We are unable to add a host */
1036 emsg = "A host exists with given host-id";
1037 LOG_DEBUG ("%s: %u", emsg, host_id);
1038 GNUNET_TESTBED_host_destroy (host);
1039 reply_size += strlen (emsg) + 1;
1040 reply = GNUNET_malloc (reply_size);
1041 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1044 reply = GNUNET_malloc (reply_size);
1045 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1046 reply->header.size = htons (reply_size);
1047 reply->host_id = htonl (host_id);
1048 queue_message (client, &reply->header);
1053 * Iterator over hash map entries.
1055 * @param cls closure
1056 * @param key current key code
1057 * @param value value in the hash map
1058 * @return GNUNET_YES if we should continue to
1062 int ss_exists_iterator (void *cls,
1063 const struct GNUNET_HashCode * key,
1066 struct SharedService *queried_ss = cls;
1067 struct SharedService *ss = value;
1069 if (0 == strcmp (ss->name, queried_ss->name))
1077 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1080 * @param client identification of the client
1081 * @param message the actual message
1084 handle_configure_shared_service (void *cls,
1085 struct GNUNET_SERVER_Client *client,
1086 const struct GNUNET_MessageHeader *message)
1088 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1089 struct SharedService *ss;
1091 struct GNUNET_HashCode hash;
1093 uint16_t service_name_size;
1095 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1096 msg_size = ntohs (message->size);
1097 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1103 service_name_size = msg_size -
1104 sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1105 service_name = (char *) &msg[1];
1106 if ('\0' != service_name[service_name_size - 1])
1109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1112 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1113 service_name, ntohl (msg->num_peers));
1114 if (ntohl (msg->host_id) != master_context->host_id)
1116 route_message (ntohl (msg->host_id), message);
1117 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1120 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1121 ss = GNUNET_malloc (sizeof (struct SharedService));
1122 ss->name = strdup (service_name);
1123 ss->num_shared = ntohl (msg->num_peers);
1124 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1125 if (GNUNET_SYSERR ==
1126 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1127 &ss_exists_iterator, ss))
1129 LOG (GNUNET_ERROR_TYPE_WARNING,
1130 "Service %s already configured as a shared service. "
1131 "Ignoring service sharing request \n", ss->name);
1132 GNUNET_free (ss->name);
1136 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1137 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1142 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1145 * @param client identification of the client
1146 * @param message the actual message
1149 handle_link_controllers (void *cls,
1150 struct GNUNET_SERVER_Client *client,
1151 const struct GNUNET_MessageHeader *message)
1153 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1154 struct GNUNET_CONFIGURATION_Handle *cfg;
1155 struct LCFContextQueue *lcfq;
1156 struct Route *route;
1157 struct Route *new_route;
1161 uint32_t delegated_host_id;
1162 uint32_t slave_host_id;
1165 if (NULL == master_context)
1168 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1171 msize = ntohs (message->size);
1172 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1178 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1179 delegated_host_id = ntohl (msg->delegated_host_id);
1180 if (delegated_host_id == master_context->host_id)
1183 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1184 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1187 if ((delegated_host_id >= host_list_size) ||
1188 (NULL == host_list[delegated_host_id]))
1190 LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1194 slave_host_id = ntohl (msg->slave_host_id);
1195 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1197 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1201 if (slave_host_id == delegated_host_id)
1203 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1207 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1208 config_size = ntohs (msg->config_size);
1210 if (slave_host_id == master_context->host_id) /* Link from us */
1212 struct Slave *slave;
1214 if ((delegated_host_id < slave_list_size) &&
1215 (NULL != slave_list[delegated_host_id])) /* We have already added */
1217 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1222 config = GNUNET_malloc (config_size);
1223 dest_size = (uLongf) config_size;
1224 if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1225 (const Bytef *) &msg[1], (uLong) msize))
1227 GNUNET_break (0); /* Compression error */
1228 GNUNET_free (config);
1229 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1232 if (config_size == dest_size)
1234 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1235 GNUNET_free (config);
1236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1239 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1240 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1243 GNUNET_break (0); /* Configuration parsing error */
1244 GNUNET_free (config);
1245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1248 GNUNET_free (config);
1249 if ((delegated_host_id < slave_list_size) &&
1250 (NULL != slave_list[delegated_host_id]))
1252 GNUNET_break (0); /* Configuration parsing error */
1253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1256 slave = GNUNET_malloc (sizeof (struct Slave));
1257 slave->host_id = delegated_host_id;
1258 slave_list_add (slave);
1259 if (1 == msg->is_subordinate)
1261 slave->controller_proc =
1262 GNUNET_TESTBED_controller_start (master_context->master_ip,
1263 host_list[slave->host_id],
1264 cfg, &slave_status_callback,
1269 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1270 master_context->event_mask,
1271 &slave_event_callback, slave);
1273 GNUNET_CONFIGURATION_destroy (cfg);
1274 new_route = GNUNET_malloc (sizeof (struct Route));
1275 new_route->dest = delegated_host_id;
1276 new_route->thru = master_context->host_id;
1277 route_list_add (new_route);
1278 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1282 /* Route the request */
1283 if (slave_host_id >= route_list_size)
1285 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1286 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1289 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1290 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1291 lcfq->lcf->delegated_host_id = delegated_host_id;
1292 lcfq->lcf->slave_host_id = slave_host_id;
1293 while (NULL != (route = route_list[slave_host_id]))
1295 if (route->thru == master_context->host_id)
1297 slave_host_id = route->thru;
1299 GNUNET_assert (NULL != route); /* because we add routes carefully */
1300 GNUNET_assert (route->dest < slave_list_size);
1301 GNUNET_assert (NULL != slave_list[route->dest]);
1302 lcfq->lcf->is_subordinate =
1303 (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1304 lcfq->lcf->state = INIT;
1305 lcfq->lcf->gateway = slave_list[route->dest];
1306 lcfq->lcf->sxcfg_size = msize;
1307 lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1308 lcfq->lcf->scfg_size = config_size;
1309 (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1310 if (NULL == lcfq_head)
1312 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1313 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1314 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1317 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1319 new_route = GNUNET_malloc (sizeof (struct Route));
1320 new_route->dest = delegated_host_id;
1321 new_route->thru = route->dest;
1322 route_list_add (new_route);
1327 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1330 * @param client identification of the client
1331 * @param message the actual message
1334 handle_peer_create (void *cls,
1335 struct GNUNET_SERVER_Client *client,
1336 const struct GNUNET_MessageHeader *message)
1338 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1339 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1340 struct GNUNET_CONFIGURATION_Handle *cfg;
1344 uint32_t config_size;
1348 msize = ntohs (message->size);
1349 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1351 GNUNET_break (0); /* We need configuration */
1352 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1355 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1356 if (ntohl (msg->host_id) == master_context->host_id)
1361 /* We are responsible for this peer */
1362 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1363 config_size = ntohl (msg->config_size);
1364 config = GNUNET_malloc (config_size);
1365 dest_size = config_size;
1366 if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1367 (const Bytef *) &msg[1], (uLong) msize)))
1369 GNUNET_break (0); /* uncompression error */
1370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1373 if (config_size != dest_size)
1375 GNUNET_break (0);/* Uncompressed config size mismatch */
1376 GNUNET_free (config);
1377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1380 cfg = GNUNET_CONFIGURATION_create ();
1381 if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1384 GNUNET_break (0); /* Configuration parsing error */
1385 GNUNET_free (config);
1386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1389 GNUNET_free (config);
1390 peer = GNUNET_malloc (sizeof (struct Peer));
1392 peer->id = ntohl (msg->peer_id);
1393 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1394 peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1398 if (NULL == peer->peer)
1400 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1407 peer_list_add (peer);
1408 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1409 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1410 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1411 reply->peer_id = msg->peer_id;
1412 reply->operation_id = msg->operation_id;
1413 queue_message (client, &reply->header);
1414 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1418 /* FIXME: Forward the peer to other host */
1420 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1425 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1428 * @param client identification of the client
1429 * @param message the actual message
1432 handle_peer_destroy (void *cls,
1433 struct GNUNET_SERVER_Client *client,
1434 const struct GNUNET_MessageHeader *message)
1436 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1437 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1440 uint16_t reply_size;
1442 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1443 peer_id = ntohl (msg->peer_id);
1444 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1445 peer_id, GNUNET_ntohll (msg->operation_id));
1446 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1449 /* FIXME: Reply with failure event message or forward to slave controller */
1450 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1453 peer = peer_list[peer_id];
1454 GNUNET_TESTING_peer_destroy (peer->peer);
1455 GNUNET_CONFIGURATION_destroy (peer->cfg);
1456 peer_list_remove (peer);
1459 sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1460 reply = GNUNET_malloc (reply_size);
1461 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1462 reply->header.size = htons (reply_size);
1463 reply->operation_id = msg->operation_id;
1464 reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1465 queue_message (client, &reply->header);
1466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1471 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1474 * @param client identification of the client
1475 * @param message the actual message
1478 handle_peer_start (void *cls,
1479 struct GNUNET_SERVER_Client *client,
1480 const struct GNUNET_MessageHeader *message)
1482 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1483 struct GNUNET_TESTBED_PeerEventMessage *reply;
1486 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1487 peer_id = ntohl (msg->peer_id);
1488 if ((peer_id >= peer_list_size)
1489 || (NULL == peer_list[peer_id]))
1492 /* FIXME: reply with failure message or forward to slave controller */
1493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1496 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1498 /* FIXME: return FAILURE message */
1500 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1503 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1504 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1505 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1506 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1507 reply->host_id = htonl (master_context->host_id);
1508 reply->peer_id = msg->peer_id;
1509 reply->operation_id = msg->operation_id;
1510 queue_message (client, &reply->header);
1511 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1516 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1519 * @param client identification of the client
1520 * @param message the actual message
1523 handle_peer_stop (void *cls,
1524 struct GNUNET_SERVER_Client *client,
1525 const struct GNUNET_MessageHeader *message)
1527 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1528 struct GNUNET_TESTBED_PeerEventMessage *reply;
1531 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1532 peer_id = ntohl (msg->peer_id);
1533 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1535 GNUNET_break (0); /* FIXME: route to slave? */
1536 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1539 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1541 /* FIXME: return FAILURE message */
1543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1546 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1547 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1548 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1549 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1550 reply->host_id = htonl (master_context->host_id);
1551 reply->peer_id = msg->peer_id;
1552 reply->operation_id = msg->operation_id;
1553 queue_message (client, &reply->header);
1554 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1559 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1562 * @param client identification of the client
1563 * @param message the actual message
1566 handle_peer_get_config (void *cls,
1567 struct GNUNET_SERVER_Client *client,
1568 const struct GNUNET_MessageHeader *message)
1570 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1571 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1579 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1580 peer_id = ntohl (msg->peer_id);
1581 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1583 /* FIXME: return FAILURE message */
1585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1587 config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1589 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1590 GNUNET_free (config);
1591 msize = xc_size + sizeof (struct
1592 GNUNET_TESTBED_PeerConfigurationInformationMessage);
1593 reply = GNUNET_realloc (xconfig, msize);
1594 (void) memmove (&reply[1], reply, xc_size);
1595 reply->header.size = htons (msize);
1596 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1597 reply->peer_id = msg->peer_id;
1598 reply->operation_id = msg->operation_id;
1599 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1600 &reply->peer_identity);
1601 reply->config_size = htons ((uint16_t) c_size);
1602 queue_message (client, &reply->header);
1603 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1608 * Task for cleaing up overlay connect context structure
1610 * @param cls the overlay connect context
1611 * @param tc the task context
1614 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1616 struct OverlayConnectContext *occ = cls;
1618 GNUNET_free_non_null (occ->emsg);
1619 GNUNET_free_non_null (occ->hello);
1620 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1621 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1622 if (NULL != occ->ch)
1623 GNUNET_CORE_disconnect (occ->ch);
1624 if (NULL != occ->ghh)
1625 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1626 if (NULL != occ->p1th)
1627 GNUNET_TRANSPORT_disconnect (occ->p1th);
1628 if (NULL != occ->p2th)
1629 GNUNET_TRANSPORT_disconnect (occ->p2th);
1635 * Task which will be run when overlay connect request has been timed out
1641 timeout_overlay_connect (void *cls,
1642 const struct GNUNET_SCHEDULER_TaskContext *tc)
1644 struct OverlayConnectContext *occ = cls;
1646 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1647 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1648 GNUNET_SERVER_client_drop (occ->client);
1649 occ_cleanup (occ, tc);
1655 * Function called to notify transport users that another
1656 * peer connected to us.
1658 * @param cls closure
1659 * @param new_peer the peer that connected
1660 * @param ats performance data
1661 * @param ats_count number of entries in ats (excluding 0-termination)
1664 overlay_connect_notify (void *cls,
1665 const struct GNUNET_PeerIdentity * new_peer,
1666 const struct GNUNET_ATS_Information * ats,
1667 unsigned int ats_count)
1669 struct OverlayConnectContext *occ = cls;
1670 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1672 char *other_peer_str;
1674 LOG_DEBUG ("Overlay connect notify\n");
1675 if (0 == memcmp (new_peer, &occ->peer_identity,
1676 sizeof (struct GNUNET_PeerIdentity)))
1678 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1679 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1680 if (0 != memcmp (new_peer, &occ->other_peer_identity,
1681 sizeof (struct GNUNET_PeerIdentity)))
1683 LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1684 new_peer_str, other_peer_str);
1685 GNUNET_free (new_peer_str);
1686 GNUNET_free (other_peer_str);
1689 LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1690 GNUNET_free (new_peer_str);
1691 GNUNET_free (other_peer_str);
1692 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1694 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1695 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1697 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
1698 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1699 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1700 GNUNET_free_non_null (occ->emsg);
1702 GNUNET_TRANSPORT_disconnect (occ->p1th);
1704 /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
1705 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1706 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1707 msg->header.size = htons (sizeof (struct
1708 GNUNET_TESTBED_ConnectionEventMessage));
1709 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1710 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1711 msg->peer1 = htonl (occ->peer->id);
1712 msg->peer2 = htonl (occ->other_peer->id);
1713 msg->operation_id = GNUNET_htonll (occ->op_id);
1714 queue_message (occ->client, &msg->header);
1715 GNUNET_SERVER_client_drop (occ->client);
1716 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
1721 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1723 struct OverlayConnectContext *occ = cls;
1724 char *other_peer_str;
1726 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1727 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1729 GNUNET_assert (NULL != occ->hello);
1730 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1731 LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
1732 GNUNET_free (other_peer_str);
1733 GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
1734 GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
1735 occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1740 * Test for checking whether HELLO message is empty
1742 * @param cls empty flag to set
1743 * @param address the HELLO
1744 * @param expiration expiration of the HELLO
1748 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
1749 struct GNUNET_TIME_Absolute expiration)
1759 * Function called whenever there is an update to the
1760 * HELLO of peers in the OverlayConnectClosure
1762 * @param cls closure
1763 * @param hello our updated HELLO
1766 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1768 struct OverlayConnectContext *occ = cls;
1772 msize = ntohs (hello->size);
1775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1776 "HELLO message of peer %s is of size 0\n",
1777 &occ->other_peer_identity);
1782 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
1783 GNUNET_NO, &test_address, &empty);
1784 if (GNUNET_YES == empty)
1786 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
1789 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
1790 occ->hello = GNUNET_malloc (msize);
1791 memcpy (occ->hello, hello, msize);
1792 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1794 GNUNET_TRANSPORT_disconnect (occ->p2th);
1796 GNUNET_free_non_null (occ->emsg);
1797 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
1798 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1803 * Function called after GNUNET_CORE_connect has succeeded (or failed
1804 * for good). Note that the private key of the peer is intentionally
1805 * not exposed here; if you need it, your process should try to read
1806 * the private key file directly (which should work if you are
1809 * @param cls closure
1810 * @param server handle to the server, NULL if we failed
1811 * @param my_identity ID of this peer, NULL if we failed
1814 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
1815 const struct GNUNET_PeerIdentity *my_identity)
1817 struct OverlayConnectContext *occ = cls;
1819 GNUNET_free_non_null (occ->emsg);
1821 memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
1823 GNUNET_TRANSPORT_connect (occ->peer->cfg, &occ->peer_identity, NULL, NULL,
1825 /* Connect to the transport of 2nd peer and get its HELLO message */
1826 GNUNET_TESTING_peer_get_identity (occ->other_peer->peer,
1827 &occ->other_peer_identity);
1829 GNUNET_TRANSPORT_connect (occ->other_peer->cfg, &occ->other_peer_identity,
1830 NULL, NULL, NULL, NULL);
1831 if ((NULL == occ->p1th) || (NULL == occ->p2th))
1833 occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
1836 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s
1837 (&occ->other_peer_identity));
1838 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
1839 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
1843 GNUNET_SCHEDULER_cancel (occ->timeout_task);
1844 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
1849 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1852 * @param client identification of the client
1853 * @param message the actual message
1856 handle_overlay_connect (void *cls,
1857 struct GNUNET_SERVER_Client *client,
1858 const struct GNUNET_MessageHeader *message)
1860 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1861 struct OverlayConnectContext *occ;
1862 struct GNUNET_CORE_MessageHandler no_handlers[] = {
1868 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1869 p1 = ntohl (msg->peer1);
1870 p2 = ntohl (msg->peer2);
1871 GNUNET_assert (p1 < peer_list_size);
1872 GNUNET_assert (NULL != peer_list[p1]);
1873 GNUNET_assert (p2 < peer_list_size);
1874 GNUNET_assert (NULL != peer_list[p2]);
1875 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1876 GNUNET_SERVER_client_keep (client);
1877 occ->client = client;
1878 occ->state = OCC_STATE_INIT;
1879 occ->peer = peer_list[p1];
1880 occ->other_peer = peer_list[p2];
1881 occ->op_id = GNUNET_ntohll (msg->operation_id);
1883 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1884 (GNUNET_TIME_UNIT_SECONDS, 30),
1885 &timeout_overlay_connect, occ);
1886 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
1887 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
1889 GNUNET_CORE_connect (occ->peer->cfg, occ, &core_startup_cb,
1890 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
1891 GNUNET_NO, no_handlers);
1892 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1897 * Iterator over hash map entries.
1899 * @param cls closure
1900 * @param key current key code
1901 * @param value value in the hash map
1902 * @return GNUNET_YES if we should continue to
1907 ss_map_free_iterator (void *cls,
1908 const struct GNUNET_HashCode * key, void *value)
1910 struct SharedService *ss = value;
1912 GNUNET_assert (GNUNET_YES ==
1913 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1914 GNUNET_free (ss->name);
1921 * Task to clean up and shutdown nicely
1924 * @param tc the TaskContext from scheduler
1927 shutdown_task (void *cls,
1928 const struct GNUNET_SCHEDULER_TaskContext *tc)
1930 struct LCFContextQueue *lcfq;
1933 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1934 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1935 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1937 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1938 if (NULL != lcfq_head)
1940 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1942 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1943 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1945 if (NULL != lcfq_head->lcf->rhandle)
1946 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1948 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1949 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1951 GNUNET_free (lcfq->lcf->sxcfg);
1952 GNUNET_free (lcfq->lcf);
1953 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1956 /* Clear peer list */
1957 for (id = 0; id < peer_list_size; id++)
1958 if (NULL != peer_list[id])
1960 GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1961 GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1962 GNUNET_free (peer_list[id]);
1964 GNUNET_free_non_null (peer_list);
1965 /* Clear host list */
1966 for (id = 0; id < host_list_size; id++)
1967 if (NULL != host_list[id])
1968 GNUNET_TESTBED_host_destroy (host_list[id]);
1969 GNUNET_free_non_null (host_list);
1970 /* Clear route list */
1971 for (id = 0; id < route_list_size; id++)
1972 if (NULL != route_list[id])
1973 GNUNET_free (route_list[id]);
1974 GNUNET_free_non_null (route_list);
1975 /* Clear slave_list */
1976 for (id = 0; id < slave_list_size; id++)
1977 if (NULL != slave_list[id])
1979 GNUNET_assert (NULL != slave_list[id]->controller);
1980 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1981 if (NULL != slave_list[id]->controller_proc)
1982 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1984 if (NULL != master_context)
1986 GNUNET_free_non_null (master_context->master_ip);
1987 if (NULL != master_context->system)
1988 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1989 GNUNET_free (master_context);
1990 master_context = NULL;
1996 * Callback for client disconnect
1999 * @param client the client which has disconnected
2002 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2004 if (NULL == master_context)
2006 if (client == master_context->client)
2008 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2009 GNUNET_SERVER_client_drop (client);
2010 /* should not be needed as we're terminated by failure to read
2011 from stdin, but if stdin fails for some reason, this shouldn't
2012 hurt for now --- might need to revise this later if we ever
2013 decide that master connections might be temporarily down
2015 //GNUNET_SCHEDULER_shutdown ();
2023 * @param cls closure
2024 * @param server the initialized server
2025 * @param cfg configuration to use
2028 testbed_run (void *cls,
2029 struct GNUNET_SERVER_Handle *server,
2030 const struct GNUNET_CONFIGURATION_Handle *cfg)
2032 static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
2034 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
2035 sizeof (struct GNUNET_TESTBED_InitMessage)},
2036 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2037 {&handle_configure_shared_service, NULL,
2038 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2039 {&handle_link_controllers, NULL,
2040 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2041 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2042 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2043 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2044 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2045 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2046 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2047 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2048 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2049 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2050 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2051 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2055 GNUNET_SERVER_add_handlers (server,
2057 GNUNET_SERVER_disconnect_notify (server,
2058 &client_disconnect_cb,
2060 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2062 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2065 LOG_DEBUG ("Testbed startup complete\n");
2070 * The starting point of execution
2072 int main (int argc, char *const *argv)
2074 //sleep (30); /* Debugging */
2077 GNUNET_SERVICE_run (argc,
2080 GNUNET_SERVICE_OPTION_NONE,