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 * By how much should the arrays lists grow
56 #define LIST_GROW_STEP 10
59 * Default timeout for operations which may take some time
61 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30)
64 * Timeout of Transport try_connect requests
66 #define TRANSPORT_TRY_CONNECT_TIMEOUT \
67 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100)
70 * The main context information associated with the client which started us
75 * The client handle associated with this context
77 struct GNUNET_SERVER_Client *client;
80 * The network address of the master controller
85 * The TESTING system handle for starting peers locally
87 struct GNUNET_TESTING_System *system;
90 * Event mask of event to be responded in this context
95 * Our host id according to this context
102 * The message queue for sending messages to clients
107 * The message to be sent
109 struct GNUNET_MessageHeader *msg;
112 * The client to send the message to
114 struct GNUNET_SERVER_Client *client;
117 * next pointer for DLL
119 struct MessageQueue *next;
122 * prev pointer for DLL
124 struct MessageQueue *prev;
129 * The structure for identifying a shared service
134 * The name of the shared service
139 * Number of shared peers per instance of the shared service
144 * Number of peers currently sharing the service
146 uint32_t num_sharing;
161 * The destination host is reachable thru
168 * Context information used while linking controllers
170 struct LinkControllersContext;
174 * Structure representing a connected(directly-linked) controller
179 * The controller process handle if we had started the controller
181 struct GNUNET_TESTBED_ControllerProc *controller_proc;
184 * The controller handle
186 struct GNUNET_TESTBED_Controller *controller;
189 * The configuration of the slave. Will be NULL for slave which we didn't
192 struct GNUNET_CONFIGURATION_Handle *cfg;
195 * handle to lcc which is associated with this slave startup. Should be set to
196 * NULL when the slave has successfully started up
198 struct LinkControllersContext *lcc;
201 * The id of the host this controller is running on
208 * States of LCFContext
213 * The Context has been initialized; Nothing has been done on it
218 * Delegated host has been registered at the forwarding controller
220 DELEGATED_HOST_REGISTERED,
223 * The slave host has been registred at the forwarding controller
225 SLAVE_HOST_REGISTERED,
228 * The context has been finished (may have error)
235 * Link controllers request forwarding context
240 * The gateway which will pass the link message to delegated host
242 struct Slave *gateway;
245 * The controller link message that has to be forwarded to
247 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
250 * The client which has asked to perform this operation
252 struct GNUNET_SERVER_Client *client;
255 * The host registration handle while registered hosts in this context
257 struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
260 * The id of the operation which created this context
262 uint64_t operation_id;
265 * The state of this context
267 enum LCFContextState state;
272 uint32_t delegated_host_id;
277 uint32_t slave_host_id;
283 * Structure of a queue entry in LCFContext request queue
285 struct LCFContextQueue
290 struct LCFContext *lcf;
295 struct LCFContextQueue *next;
300 struct LCFContextQueue *prev;
305 * A locally started peer
314 * The peer handle from testing API
316 struct GNUNET_TESTING_Peer *peer;
319 * The modified (by GNUNET_TESTING_peer_configure) configuration this
320 * peer is configured with
322 struct GNUNET_CONFIGURATION_Handle *cfg;
325 * Is the peer running
334 * The controller this peer is started through
336 struct GNUNET_TESTBED_Controller *controller;
343 * Is this peer locally created?
348 * Our local reference id for this peer
356 * Context information for connecting 2 peers in overlay
358 struct OverlayConnectContext
361 * The client which has requested for overlay connection
363 struct GNUNET_SERVER_Client *client;
366 * the peer which has to connect to the other peer
371 * Transport handle of the first peer to get its HELLO
373 struct GNUNET_TRANSPORT_Handle *p1th;
376 * Transport handle of other peer to offer first peer's HELLO
378 struct GNUNET_TRANSPORT_Handle *p2th;
381 * Core handles of the first peer; used to notify when second peer connects to it
383 struct GNUNET_CORE_Handle *ch;
386 * HELLO of the other peer
388 struct GNUNET_MessageHeader *hello;
391 * Get hello handle to acquire HELLO of first peer
393 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
396 * The error message we send if this overlay connect operation has timed out
401 * Operation context for suboperations
403 struct OperationContext *opc;
406 * Controller of peer 2; NULL if the peer is local
408 struct GNUNET_TESTBED_Controller *peer2_controller;
411 * The peer identity of the first peer
413 struct GNUNET_PeerIdentity peer_identity;
416 * The peer identity of the other peer
418 struct GNUNET_PeerIdentity other_peer_identity;
421 * The id of the operation responsible for creating this context
426 * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
429 GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
432 * The id of the overlay connect timeout task
434 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
444 uint32_t other_peer_id;
449 * Context information for RequestOverlayConnect
450 * operations. RequestOverlayConnect is used when peers A, B reside on different
451 * hosts and the host controller for peer B is asked by the host controller of
452 * peer A to make peer B connect to peer A
454 struct RequestOverlayConnectContext
457 * The transport handle of peer B
459 struct GNUNET_TRANSPORT_Handle *th;
464 struct GNUNET_MessageHeader *hello;
467 * The peer identity of peer A
469 struct GNUNET_PeerIdentity a_id;
472 * Task for offering HELLO of A to B and doing try_connect
474 GNUNET_SCHEDULER_TaskIdentifier attempt_connect_task_id;
477 * Task to timeout RequestOverlayConnect
479 GNUNET_SCHEDULER_TaskIdentifier timeout_rocc_task_id;
485 * Context information for operations forwarded to subcontrollers
487 struct ForwardedOperationContext
490 * The generated operation context
492 struct OperationContext *opc;
495 * The client to which we have to reply
497 struct GNUNET_SERVER_Client *client;
505 * Task ID for the timeout task
507 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
510 * The id of the operation that has been forwarded
512 uint64_t operation_id;
518 * Context information used while linking controllers
520 struct LinkControllersContext
523 * The client which initiated the link controller operation
525 struct GNUNET_SERVER_Client *client;
528 * The ID of the operation
530 uint64_t operation_id;
537 * The master context; generated with the first INIT message
539 static struct Context *master_context;
546 * Current Transmit Handle; NULL if no notify transmit exists currently
548 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
555 * The head for the LCF queue
557 static struct LCFContextQueue *lcfq_head;
560 * The tail for the LCF queue
562 static struct LCFContextQueue *lcfq_tail;
565 * The message queue head
567 static struct MessageQueue *mq_head;
570 * The message queue tail
572 static struct MessageQueue *mq_tail;
577 static struct GNUNET_TESTBED_Host **host_list;
582 static struct Route **route_list;
585 * A list of directly linked neighbours
587 static struct Slave **slave_list;
590 * A list of peers we own locally
592 static struct Peer **peer_list;
595 * The hashmap of shared services
597 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
600 * The size of the host list
602 static uint32_t host_list_size;
605 * The size of the route list
607 static uint32_t route_list_size;
610 * The size of directly linked neighbours list
612 static uint32_t slave_list_size;
615 * The size of the peer list
617 static uint32_t peer_list_size;
624 * The lcf_task handle
626 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
629 * The shutdown task handle
631 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
635 * Function called to notify a client about the connection begin ready to queue
636 * more data. "buf" will be NULL and "size" zero if the connection was closed
637 * for writing in the meantime.
640 * @param size number of bytes available in buf
641 * @param buf where the callee should write the message
642 * @return number of bytes written to buf
645 transmit_ready_notify (void *cls, size_t size, void *buf)
647 struct MessageQueue *mq_entry;
649 transmit_handle = NULL;
651 GNUNET_assert (NULL != mq_entry);
654 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
655 size = ntohs (mq_entry->msg->size);
656 memcpy (buf, mq_entry->msg, size);
657 GNUNET_free (mq_entry->msg);
658 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
659 GNUNET_free (mq_entry);
661 if (NULL != mq_entry)
663 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
664 ntohs (mq_entry->msg->size),
665 GNUNET_TIME_UNIT_FOREVER_REL,
666 &transmit_ready_notify, NULL);
672 * Queues a message in send queue for sending to the service
674 * @param client the client to whom the queued message has to be sent
675 * @param msg the message to queue
678 queue_message (struct GNUNET_SERVER_Client *client,
679 struct GNUNET_MessageHeader *msg)
681 struct MessageQueue *mq_entry;
685 type = ntohs (msg->type);
686 size = ntohs (msg->size);
687 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
688 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
689 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
691 mq_entry->client = client;
692 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
694 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
695 if (NULL == transmit_handle)
697 GNUNET_SERVER_notify_transmit_ready (client, size,
698 GNUNET_TIME_UNIT_FOREVER_REL,
699 &transmit_ready_notify, NULL);
704 * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
706 * @param ptr the memory block to realloc
707 * @param size the size of ptr
708 * @param new_size the size to which ptr has to be realloc'ed
709 * @return the newly reallocated memory block
712 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
714 ptr = GNUNET_realloc (ptr, new_size);
716 (void) memset (ptr + size, 0, new_size - size);
722 * Function to add a host to the current list of known hosts
724 * @param host the host to add
725 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
729 host_list_add (struct GNUNET_TESTBED_Host *host)
733 host_id = GNUNET_TESTBED_host_get_id_ (host);
734 if (host_list_size <= host_id)
737 TESTBED_realloc (host_list,
738 sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
739 sizeof (struct GNUNET_TESTBED_Host *) *
740 (host_list_size + LIST_GROW_STEP));
741 host_list_size += LIST_GROW_STEP;
743 if (NULL != host_list[host_id])
745 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
746 return GNUNET_SYSERR;
748 host_list[host_id] = host;
754 * Adds a route to the route list
756 * @param route the route to add
759 route_list_add (struct Route *route)
761 if (route->dest >= route_list_size)
764 TESTBED_realloc (route_list, sizeof (struct Route *) * route_list_size,
765 sizeof (struct Route *) * (route_list_size +
767 route_list_size += LIST_GROW_STEP;
769 GNUNET_assert (NULL == route_list[route->dest]);
770 route_list[route->dest] = route;
775 * Adds a slave to the slave array
777 * @param slave the slave controller to add
780 slave_list_add (struct Slave *slave)
782 if (slave->host_id >= slave_list_size)
785 TESTBED_realloc (slave_list, sizeof (struct Slave *) * slave_list_size,
786 sizeof (struct Slave *) * (slave_list_size +
788 slave_list_size += LIST_GROW_STEP;
790 GNUNET_assert (NULL == slave_list[slave->host_id]);
791 slave_list[slave->host_id] = slave;
796 * Adds a peer to the peer array
798 * @param peer the peer to add
801 peer_list_add (struct Peer *peer)
805 orig_size = peer_list_size;
806 if (peer->id >= peer_list_size)
808 while (peer->id >= peer_list_size)
809 peer_list_size += LIST_GROW_STEP;
811 TESTBED_realloc (peer_list, sizeof (struct Peer *) * orig_size,
812 sizeof (struct Peer *) * peer_list_size);
814 GNUNET_assert (NULL == peer_list[peer->id]);
815 peer_list[peer->id] = peer;
820 * Removes a the give peer from the peer array
822 * @param peer the peer to be removed
825 peer_list_remove (struct Peer *peer)
830 peer_list[peer->id] = NULL;
831 orig_size = peer_list_size;
832 while (peer_list_size >= LIST_GROW_STEP)
834 for (id = peer_list_size - 1;
835 (id >= peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX); id--)
836 if (NULL != peer_list[id])
838 if (id != ((peer_list_size - LIST_GROW_STEP) - 1))
840 peer_list_size -= LIST_GROW_STEP;
842 if (orig_size == peer_list_size)
845 GNUNET_realloc (peer_list, sizeof (struct Peer *) * peer_list_size);
850 * Finds the route with directly connected host as destination through which
851 * the destination host can be reached
853 * @param host_id the id of the destination host
854 * @return the route with directly connected destination host; NULL if no route
857 static struct Route *
858 find_dest_route (uint32_t host_id)
862 while (NULL != (route = route_list[host_id]))
864 if (route->thru == master_context->host_id)
866 host_id = route->thru;
873 * Routes message to a host given its host_id
875 * @param host_id the id of the destination host
876 * @param msg the message to be routed
879 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
886 * Send operation failure message to client
888 * @param client the client to which the failure message has to be sent to
889 * @param operation_id the id of the failed operation
890 * @param emsg the error message; can be NULL
893 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
894 uint64_t operation_id, const char *emsg)
896 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
900 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
901 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
903 msg = GNUNET_malloc (msize);
904 msg->header.size = htons (msize);
905 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT);
906 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
907 msg->operation_id = GNUNET_htonll (operation_id);
909 memcpy (&msg[1], emsg, emsg_len);
910 queue_message (client, &msg->header);
915 * Function to send generic operation success message to given client
917 * @param client the client to send the message to
918 * @param operation_id the id of the operation which was successful
921 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
922 uint64_t operation_id)
924 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
927 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
928 msg = GNUNET_malloc (msize);
929 msg->header.size = htons (msize);
930 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
931 msg->operation_id = GNUNET_htonll (operation_id);
932 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
933 queue_message (client, &msg->header);
938 * The Link Controller forwarding task
940 * @param cls the LCFContext
941 * @param tc the Task context from scheduler
944 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
948 * Completion callback for host registrations while forwarding Link Controller messages
950 * @param cls the LCFContext
951 * @param emsg the error message; NULL if host registration is successful
954 lcf_proc_cc (void *cls, const char *emsg)
956 struct LCFContext *lcf = cls;
959 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
964 goto registration_error;
965 lcf->state = DELEGATED_HOST_REGISTERED;
966 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
968 case DELEGATED_HOST_REGISTERED:
970 goto registration_error;
971 lcf->state = SLAVE_HOST_REGISTERED;
972 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
975 GNUNET_assert (0); /* Shouldn't reach here */
980 LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
982 lcf->state = FINISHED;
983 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
988 * Callback to be called when forwarded link controllers operation is
989 * successfull. We have to relay the reply msg back to the client
991 * @param cls ForwardedOperationContext
992 * @param msg the peer create success message
995 forwarded_operation_reply_relay (void *cls,
996 const struct GNUNET_MessageHeader *msg)
998 struct ForwardedOperationContext *fopc = cls;
999 struct GNUNET_MessageHeader *dup_msg;
1002 msize = ntohs (msg->size);
1003 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
1005 dup_msg = GNUNET_malloc (msize);
1006 (void) memcpy (dup_msg, msg, msize);
1007 queue_message (fopc->client, dup_msg);
1008 GNUNET_SERVER_client_drop (fopc->client);
1009 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
1015 * Task to free resources when forwarded link controllers has been timedout
1017 * @param cls the ForwardedOperationContext
1018 * @param tc the task context from scheduler
1021 forwarded_operation_timeout (void *cls,
1022 const struct GNUNET_SCHEDULER_TaskContext *tc)
1024 struct ForwardedOperationContext *fopc = cls;
1026 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
1027 send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
1028 GNUNET_SERVER_client_drop (fopc->client);
1034 * The Link Controller forwarding task
1036 * @param cls the LCFContext
1037 * @param tc the Task context from scheduler
1040 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1042 struct LCFContext *lcf = cls;
1043 struct LCFContextQueue *lcfq;
1044 struct ForwardedOperationContext *fopc;
1046 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1051 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
1052 lcf->gateway->controller))
1055 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1056 host_list[lcf->delegated_host_id],
1061 lcf->state = DELEGATED_HOST_REGISTERED;
1062 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1065 case DELEGATED_HOST_REGISTERED:
1067 GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
1068 lcf->gateway->controller))
1071 GNUNET_TESTBED_register_host (lcf->gateway->controller,
1072 host_list[lcf->slave_host_id],
1077 lcf->state = SLAVE_HOST_REGISTERED;
1078 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1081 case SLAVE_HOST_REGISTERED:
1082 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1083 fopc->client = lcf->client;
1084 fopc->operation_id = lcf->operation_id;
1086 GNUNET_TESTBED_forward_operation_msg_ (lcf->gateway->controller,
1089 &forwarded_operation_reply_relay,
1091 fopc->timeout_task =
1092 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1094 lcf->state = FINISHED;
1095 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
1099 GNUNET_assert (lcfq->lcf == lcf);
1100 GNUNET_free (lcf->msg);
1102 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1104 if (NULL != lcfq_head)
1106 GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
1112 * Callback for event from slave controllers
1114 * @param cls struct Slave *
1115 * @param event information about the event
1118 slave_event_callback (void *cls,
1119 const struct GNUNET_TESTBED_EventInformation *event)
1126 * Callback to signal successfull startup of the controller process
1128 * @param cls the handle to the slave whose status is to be found here
1129 * @param cfg the configuration with which the controller has been started;
1130 * NULL if status is not GNUNET_OK
1131 * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
1132 * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
1135 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
1138 struct Slave *slave = cls;
1139 struct LinkControllersContext *lcc;
1142 if (GNUNET_SYSERR == status)
1144 slave->controller_proc = NULL;
1145 slave_list[slave->host_id] = NULL;
1146 if (NULL != slave->cfg)
1147 GNUNET_CONFIGURATION_destroy (slave->cfg);
1148 GNUNET_free (slave);
1150 LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
1151 GNUNET_SCHEDULER_shutdown (); /* We too shutdown */
1155 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1156 master_context->event_mask,
1157 &slave_event_callback, slave);
1158 if (NULL != slave->controller)
1160 send_operation_success_msg (lcc->client, lcc->operation_id);
1161 slave->cfg = GNUNET_CONFIGURATION_dup (cfg);
1165 send_operation_fail_msg (lcc->client, lcc->operation_id,
1166 "Could not connect to delegated controller");
1167 GNUNET_TESTBED_controller_stop (slave->controller_proc);
1168 slave_list[slave->host_id] = NULL;
1169 GNUNET_free (slave);
1176 if (NULL != lcc->client)
1178 GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
1179 GNUNET_SERVER_client_drop (lcc->client);
1190 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
1193 * @param client identification of the client
1194 * @param message the actual message
1197 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
1198 const struct GNUNET_MessageHeader *message)
1200 const struct GNUNET_TESTBED_InitMessage *msg;
1201 struct GNUNET_TESTBED_Host *host;
1202 const char *controller_hostname;
1205 if (NULL != master_context)
1208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1211 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
1212 msize = ntohs (message->size);
1213 if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
1216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1219 msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
1220 controller_hostname = (const char *) &msg[1];
1221 if ('\0' != controller_hostname[msize - 1])
1224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1227 master_context = GNUNET_malloc (sizeof (struct Context));
1228 master_context->client = client;
1229 master_context->host_id = ntohl (msg->host_id);
1230 master_context->master_ip = GNUNET_strdup (controller_hostname);
1231 LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
1232 master_context->system =
1233 GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
1235 GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
1237 host_list_add (host);
1238 master_context->event_mask = GNUNET_ntohll (msg->event_mask);
1239 GNUNET_SERVER_client_keep (client);
1240 LOG_DEBUG ("Created master context with host ID: %u\n",
1241 master_context->host_id);
1242 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1247 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1250 * @param client identification of the client
1251 * @param message the actual message
1254 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1255 const struct GNUNET_MessageHeader *message)
1257 struct GNUNET_TESTBED_Host *host;
1258 const struct GNUNET_TESTBED_AddHostMessage *msg;
1259 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1264 uint16_t username_length;
1265 uint16_t hostname_length;
1266 uint16_t reply_size;
1269 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1270 msize = ntohs (msg->header.size);
1271 username = (char *) &(msg[1]);
1272 username_length = ntohs (msg->user_name_length);
1273 GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1)); /* msg must contain hostname */
1274 if (0 != username_length)
1275 GNUNET_assert ('\0' == username[username_length]);
1276 username_length = (0 == username_length) ? 0 : username_length + 1;
1277 hostname = username + username_length;
1279 msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
1280 GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1281 GNUNET_assert (strlen (hostname) == hostname_length - 1);
1282 host_id = ntohl (msg->host_id);
1283 LOG_DEBUG ("Received ADDHOST message\n");
1284 LOG_DEBUG ("-------host id: %u\n", host_id);
1285 LOG_DEBUG ("-------hostname: %s\n", hostname);
1286 if (0 != username_length)
1287 LOG_DEBUG ("-------username: %s\n", username);
1290 LOG_DEBUG ("-------username: NULL\n");
1293 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1295 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1296 ntohs (msg->ssh_port));
1297 GNUNET_assert (NULL != host);
1298 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1299 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1300 if (GNUNET_OK != host_list_add (host))
1302 /* We are unable to add a host */
1303 emsg = "A host exists with given host-id";
1304 LOG_DEBUG ("%s: %u", emsg, host_id);
1305 GNUNET_TESTBED_host_destroy (host);
1306 reply_size += strlen (emsg) + 1;
1307 reply = GNUNET_malloc (reply_size);
1308 memcpy (&reply[1], emsg, strlen (emsg) + 1);
1311 reply = GNUNET_malloc (reply_size);
1312 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1313 reply->header.size = htons (reply_size);
1314 reply->host_id = htonl (host_id);
1315 queue_message (client, &reply->header);
1320 * Iterator over hash map entries.
1322 * @param cls closure
1323 * @param key current key code
1324 * @param value value in the hash map
1325 * @return GNUNET_YES if we should continue to
1330 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1332 struct SharedService *queried_ss = cls;
1333 struct SharedService *ss = value;
1335 if (0 == strcmp (ss->name, queried_ss->name))
1343 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1346 * @param client identification of the client
1347 * @param message the actual message
1350 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1351 const struct GNUNET_MessageHeader *message)
1353 const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1354 struct SharedService *ss;
1356 struct GNUNET_HashCode hash;
1358 uint16_t service_name_size;
1360 msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1361 msg_size = ntohs (message->size);
1362 if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1369 msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1370 service_name = (char *) &msg[1];
1371 if ('\0' != service_name[service_name_size - 1])
1374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1377 LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1378 service_name, ntohl (msg->num_peers));
1379 if (ntohl (msg->host_id) != master_context->host_id)
1381 route_message (ntohl (msg->host_id), message);
1382 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1385 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1386 ss = GNUNET_malloc (sizeof (struct SharedService));
1387 ss->name = strdup (service_name);
1388 ss->num_shared = ntohl (msg->num_peers);
1389 GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1390 if (GNUNET_SYSERR ==
1391 GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1392 &ss_exists_iterator, ss))
1394 LOG (GNUNET_ERROR_TYPE_WARNING,
1395 "Service %s already configured as a shared service. "
1396 "Ignoring service sharing request \n", ss->name);
1397 GNUNET_free (ss->name);
1401 GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1402 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1407 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1410 * @param client identification of the client
1411 * @param message the actual message
1414 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1415 const struct GNUNET_MessageHeader *message)
1417 const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1418 struct GNUNET_CONFIGURATION_Handle *cfg;
1419 struct LCFContextQueue *lcfq;
1420 struct Route *route;
1421 struct Route *new_route;
1425 uint32_t delegated_host_id;
1426 uint32_t slave_host_id;
1429 if (NULL == master_context)
1432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1435 msize = ntohs (message->size);
1436 if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1442 msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1443 delegated_host_id = ntohl (msg->delegated_host_id);
1444 if (delegated_host_id == master_context->host_id)
1447 LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1451 if ((delegated_host_id >= host_list_size) ||
1452 (NULL == host_list[delegated_host_id]))
1454 LOG (GNUNET_ERROR_TYPE_WARNING,
1455 "Delegated host %u not registered with us\n", delegated_host_id);
1456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1459 slave_host_id = ntohl (msg->slave_host_id);
1460 if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1462 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1466 if (slave_host_id == delegated_host_id)
1468 LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1469 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1473 if (slave_host_id == master_context->host_id) /* Link from us */
1475 struct Slave *slave;
1476 struct LinkControllersContext *lcc;
1478 msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1479 config_size = ntohs (msg->config_size);
1480 if ((delegated_host_id < slave_list_size) && (NULL != slave_list[delegated_host_id])) /* We have already added */
1482 LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1487 config = GNUNET_malloc (config_size);
1488 dest_size = (uLongf) config_size;
1490 uncompress ((Bytef *) config, &dest_size, (const Bytef *) &msg[1],
1493 GNUNET_break (0); /* Compression error */
1494 GNUNET_free (config);
1495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1498 if (config_size != dest_size)
1500 LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1501 GNUNET_free (config);
1502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1505 cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1507 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1509 GNUNET_break (0); /* Configuration parsing error */
1510 GNUNET_free (config);
1511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1514 GNUNET_free (config);
1515 if ((delegated_host_id < slave_list_size) &&
1516 (NULL != slave_list[delegated_host_id]))
1518 GNUNET_break (0); /* Configuration parsing error */
1519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1522 slave = GNUNET_malloc (sizeof (struct Slave));
1523 slave->host_id = delegated_host_id;
1524 slave_list_add (slave);
1525 if (1 != msg->is_subordinate)
1528 GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1529 master_context->event_mask,
1530 &slave_event_callback, slave);
1531 GNUNET_CONFIGURATION_destroy (cfg);
1532 if (NULL != slave->controller)
1533 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1535 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1536 "Could not connect to delegated controller");
1537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1540 lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1541 lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1542 GNUNET_SERVER_client_keep (client);
1543 lcc->client = client;
1545 slave->controller_proc =
1546 GNUNET_TESTBED_controller_start (master_context->master_ip,
1547 host_list[slave->host_id], cfg,
1548 &slave_status_callback, slave);
1549 GNUNET_CONFIGURATION_destroy (cfg);
1550 new_route = GNUNET_malloc (sizeof (struct Route));
1551 new_route->dest = delegated_host_id;
1552 new_route->thru = master_context->host_id;
1553 route_list_add (new_route);
1557 /* Route the request */
1558 if (slave_host_id >= route_list_size)
1560 LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1564 lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1565 lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1566 lcfq->lcf->delegated_host_id = delegated_host_id;
1567 lcfq->lcf->slave_host_id = slave_host_id;
1568 route = find_dest_route (slave_host_id);
1569 GNUNET_assert (NULL != route); /* because we add routes carefully */
1570 GNUNET_assert (route->dest < slave_list_size);
1571 GNUNET_assert (NULL != slave_list[route->dest]);
1572 lcfq->lcf->state = INIT;
1573 lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1574 lcfq->lcf->gateway = slave_list[route->dest];
1575 lcfq->lcf->msg = GNUNET_malloc (msize);
1576 (void) memcpy (lcfq->lcf->msg, msg, msize);
1577 GNUNET_SERVER_client_keep (client);
1578 lcfq->lcf->client = client;
1579 if (NULL == lcfq_head)
1581 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1582 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1583 lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1586 GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1587 /* FIXME: Adding a new route should happen after the controllers are linked
1589 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1590 new_route = GNUNET_malloc (sizeof (struct Route));
1591 new_route->dest = delegated_host_id;
1592 new_route->thru = route->dest;
1593 route_list_add (new_route);
1598 * The task to be executed if the forwarded peer create operation has been
1601 * @param cls the FowardedOperationContext
1602 * @param tc the TaskContext from the scheduler
1605 peer_create_forward_timeout (void *cls,
1606 const struct GNUNET_SCHEDULER_TaskContext *tc)
1608 struct ForwardedOperationContext *fo_ctxt = cls;
1610 /* send error msg to client */
1611 send_operation_fail_msg (fo_ctxt->client, fo_ctxt->operation_id, "Timedout");
1612 GNUNET_SERVER_client_drop (fo_ctxt->client);
1613 GNUNET_TESTBED_forward_operation_msg_cancel_ (fo_ctxt->opc);
1614 GNUNET_free (fo_ctxt);
1619 * Callback to be called when forwarded peer create operation is
1620 * successfull. We have to relay the reply msg back to the client
1622 * @param cls ForwardedOperationContext
1623 * @param msg the peer create success message
1626 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1628 struct ForwardedOperationContext *fo_ctxt = cls;
1629 const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *success_msg;
1630 struct GNUNET_MessageHeader *dup_msg;
1634 GNUNET_SCHEDULER_cancel (fo_ctxt->timeout_task);
1635 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS)
1638 (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *) msg;
1639 peer = GNUNET_malloc (sizeof (struct Peer));
1640 peer->is_remote = GNUNET_YES;
1641 peer->id = ntohl (success_msg->peer_id);
1642 GNUNET_assert (NULL != fo_ctxt->cls);
1643 peer->details.remote.controller = fo_ctxt->cls;
1644 peer_list_add (peer);
1646 msize = ntohs (msg->size);
1647 dup_msg = GNUNET_malloc (msize);
1648 (void) memcpy (dup_msg, msg, msize);
1649 queue_message (fo_ctxt->client, dup_msg);
1650 GNUNET_SERVER_client_drop (fo_ctxt->client);
1651 GNUNET_free (fo_ctxt);
1657 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1660 * @param client identification of the client
1661 * @param message the actual message
1664 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1665 const struct GNUNET_MessageHeader *message)
1667 const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1668 struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1669 struct GNUNET_CONFIGURATION_Handle *cfg;
1670 struct ForwardedOperationContext *fo_ctxt;
1671 struct Route *route;
1676 uint32_t config_size;
1682 msize = ntohs (message->size);
1683 if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1685 GNUNET_break (0); /* We need configuration */
1686 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1689 msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1690 host_id = ntohl (msg->host_id);
1691 peer_id = ntohl (msg->peer_id);
1692 if (UINT32_MAX == peer_id)
1694 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1695 "Cannot create peer with given ID");
1696 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1699 if (host_id == master_context->host_id)
1703 /* We are responsible for this peer */
1704 msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1705 config_size = ntohl (msg->config_size);
1706 config = GNUNET_malloc (config_size);
1707 dest_size = config_size;
1710 uncompress ((Bytef *) config, (uLongf *) & dest_size,
1711 (const Bytef *) &msg[1], (uLong) msize)))
1713 GNUNET_break (0); /* uncompression error */
1714 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1717 if (config_size != dest_size)
1719 GNUNET_break (0); /* Uncompressed config size mismatch */
1720 GNUNET_free (config);
1721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1724 cfg = GNUNET_CONFIGURATION_create ();
1726 GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1728 GNUNET_break (0); /* Configuration parsing error */
1729 GNUNET_free (config);
1730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1733 GNUNET_free (config);
1734 peer = GNUNET_malloc (sizeof (struct Peer));
1735 peer->is_remote = GNUNET_NO;
1736 peer->details.local.cfg = cfg;
1738 LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1739 peer->details.local.peer =
1740 GNUNET_TESTING_peer_configure (master_context->system,
1741 peer->details.local.cfg, peer->id,
1742 NULL /* Peer id */ ,
1744 if (NULL == peer->details.local.peer)
1746 LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1753 peer->details.local.is_running = GNUNET_NO;
1754 peer_list_add (peer);
1756 GNUNET_malloc (sizeof
1757 (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1758 reply->header.size =
1759 htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1760 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1761 reply->peer_id = msg->peer_id;
1762 reply->operation_id = msg->operation_id;
1763 queue_message (client, &reply->header);
1764 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1768 /* Forward peer create request */
1769 route = find_dest_route (host_id);
1773 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1776 fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1777 GNUNET_SERVER_client_keep (client);
1778 fo_ctxt->client = client;
1779 fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1780 fo_ctxt->cls = slave_list[route->dest]->controller;
1782 GNUNET_TESTBED_forward_operation_msg_ (slave_list
1783 [route->dest]->controller,
1784 fo_ctxt->operation_id,
1786 peer_create_success_cb, fo_ctxt);
1787 fo_ctxt->timeout_task =
1788 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &peer_create_forward_timeout,
1791 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1796 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1799 * @param client identification of the client
1800 * @param message the actual message
1803 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1804 const struct GNUNET_MessageHeader *message)
1806 const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1807 struct ForwardedOperationContext *fopc;
1811 msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1812 peer_id = ntohl (msg->peer_id);
1813 LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1814 peer_id, GNUNET_ntohll (msg->operation_id));
1815 if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1817 LOG (GNUNET_ERROR_TYPE_ERROR,
1818 "Asked to destroy a non existent peer with id: %u\n", peer_id);
1819 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1820 "Peer doesn't exist");
1821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1824 peer = peer_list[peer_id];
1825 if (GNUNET_YES == peer->is_remote)
1827 /* Forward the destory message to sub controller */
1828 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1829 GNUNET_SERVER_client_keep (client);
1830 fopc->client = client;
1831 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1833 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1834 fopc->operation_id, &msg->header,
1835 &forwarded_operation_reply_relay,
1837 fopc->timeout_task =
1838 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1840 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1843 GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1844 GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1845 peer_list_remove (peer);
1847 send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1848 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1853 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1856 * @param client identification of the client
1857 * @param message the actual message
1860 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1861 const struct GNUNET_MessageHeader *message)
1863 const struct GNUNET_TESTBED_PeerStartMessage *msg;
1864 struct GNUNET_TESTBED_PeerEventMessage *reply;
1865 struct ForwardedOperationContext *fopc;
1869 msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1870 peer_id = ntohl (msg->peer_id);
1871 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1874 LOG (GNUNET_ERROR_TYPE_ERROR,
1875 "Asked to start a non existent peer with id: %u\n", peer_id);
1876 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1879 peer = peer_list[peer_id];
1880 if (GNUNET_YES == peer->is_remote)
1882 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1883 GNUNET_SERVER_client_keep (client);
1884 fopc->client = client;
1885 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1887 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1888 fopc->operation_id, &msg->header,
1889 &forwarded_operation_reply_relay,
1891 fopc->timeout_task =
1892 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1897 if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1899 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1901 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1904 peer->details.local.is_running = GNUNET_YES;
1905 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1906 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1907 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1908 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1909 reply->host_id = htonl (master_context->host_id);
1910 reply->peer_id = msg->peer_id;
1911 reply->operation_id = msg->operation_id;
1912 queue_message (client, &reply->header);
1913 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1918 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1921 * @param client identification of the client
1922 * @param message the actual message
1925 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1926 const struct GNUNET_MessageHeader *message)
1928 const struct GNUNET_TESTBED_PeerStopMessage *msg;
1929 struct GNUNET_TESTBED_PeerEventMessage *reply;
1930 struct ForwardedOperationContext *fopc;
1934 msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1935 peer_id = ntohl (msg->peer_id);
1936 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1938 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1940 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1943 peer = peer_list[peer_id];
1944 if (GNUNET_YES == peer->is_remote)
1946 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1947 GNUNET_SERVER_client_keep (client);
1948 fopc->client = client;
1949 fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1951 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
1952 fopc->operation_id, &msg->header,
1953 &forwarded_operation_reply_relay,
1955 fopc->timeout_task =
1956 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
1958 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1961 if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer->details.local.peer))
1963 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1964 "Peer not running");
1965 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1968 peer->details.local.is_running = GNUNET_NO;
1969 reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1970 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1971 reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1972 reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1973 reply->host_id = htonl (master_context->host_id);
1974 reply->peer_id = msg->peer_id;
1975 reply->operation_id = msg->operation_id;
1976 queue_message (client, &reply->header);
1977 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1982 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1985 * @param client identification of the client
1986 * @param message the actual message
1989 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1990 const struct GNUNET_MessageHeader *message)
1992 const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1993 struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
2002 msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
2003 peer_id = ntohl (msg->peer_id);
2004 if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
2006 send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
2008 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2011 peer = peer_list[peer_id];
2012 if (GNUNET_YES == peer->is_remote)
2014 /* FIXME: forward to sub controller */
2016 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2020 GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->details.local.cfg,
2022 xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
2023 GNUNET_free (config);
2026 sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
2027 reply = GNUNET_realloc (xconfig, msize);
2028 (void) memmove (&reply[1], reply, xc_size);
2029 reply->header.size = htons (msize);
2030 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
2031 reply->peer_id = msg->peer_id;
2032 reply->operation_id = msg->operation_id;
2033 GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->details.local.peer,
2034 &reply->peer_identity);
2035 reply->config_size = htons ((uint16_t) c_size);
2036 queue_message (client, &reply->header);
2037 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2042 * Task for cleaing up overlay connect context structure
2044 * @param cls the overlay connect context
2045 * @param tc the task context
2048 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2050 struct OverlayConnectContext *occ = cls;
2052 LOG_DEBUG ("Cleaning up occ\n");
2053 GNUNET_free_non_null (occ->emsg);
2054 GNUNET_free_non_null (occ->hello);
2055 GNUNET_SERVER_client_drop (occ->client);
2056 if (NULL != occ->opc)
2057 GNUNET_TESTBED_forward_operation_msg_cancel_ (occ->opc);
2058 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2059 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2060 if (NULL != occ->ch)
2061 GNUNET_CORE_disconnect (occ->ch);
2062 if (NULL != occ->ghh)
2063 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2064 if (NULL != occ->p1th)
2065 GNUNET_TRANSPORT_disconnect (occ->p1th);
2066 if (NULL != occ->p2th)
2067 GNUNET_TRANSPORT_disconnect (occ->p2th);
2073 * Task which will be run when overlay connect request has been timed out
2075 * @param cls the OverlayConnectContext
2076 * @param tc the TaskContext
2079 timeout_overlay_connect (void *cls,
2080 const struct GNUNET_SCHEDULER_TaskContext *tc)
2082 struct OverlayConnectContext *occ = cls;
2084 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2085 send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
2086 occ_cleanup (occ, tc);
2092 * Function called to notify transport users that another
2093 * peer connected to us.
2095 * @param cls closure
2096 * @param new_peer the peer that connected
2097 * @param ats performance data
2098 * @param ats_count number of entries in ats (excluding 0-termination)
2101 overlay_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2102 const struct GNUNET_ATS_Information *ats,
2103 unsigned int ats_count)
2105 struct OverlayConnectContext *occ = cls;
2106 struct GNUNET_TESTBED_ConnectionEventMessage *msg;
2108 char *other_peer_str;
2110 LOG_DEBUG ("Overlay connect notify\n");
2112 memcmp (new_peer, &occ->peer_identity,
2113 sizeof (struct GNUNET_PeerIdentity)))
2115 new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
2116 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2118 memcmp (new_peer, &occ->other_peer_identity,
2119 sizeof (struct GNUNET_PeerIdentity)))
2121 LOG_DEBUG ("Unexpected peer %4s connected when expecting peer %4s\n",
2122 new_peer_str, other_peer_str);
2123 GNUNET_free (new_peer_str);
2124 GNUNET_free (other_peer_str);
2127 GNUNET_free (new_peer_str);
2128 LOG_DEBUG ("Peer %4s connected to peer %4s\n", other_peer_str,
2129 GNUNET_i2s (&occ->peer_identity));
2130 GNUNET_free (other_peer_str);
2131 if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
2133 GNUNET_SCHEDULER_cancel (occ->send_hello_task);
2134 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2136 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
2137 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2138 occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2139 GNUNET_free_non_null (occ->emsg);
2141 if (NULL != occ->p2th)
2142 GNUNET_TRANSPORT_disconnect (occ->p2th);
2144 LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
2145 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2147 htons (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
2148 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
2149 msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
2150 msg->peer1 = htonl (occ->peer_id);
2151 msg->peer2 = htonl (occ->other_peer_id);
2152 msg->operation_id = GNUNET_htonll (occ->op_id);
2153 queue_message (occ->client, &msg->header);
2154 GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
2159 * Task to offer HELLO of peer 1 to peer 2 and try to make peer 2 to connect to
2162 * @param cls the OverlayConnectContext
2163 * @param tc the TaskContext from scheduler
2166 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2168 struct OverlayConnectContext *occ = cls;
2169 char *other_peer_str;
2171 occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
2172 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2174 GNUNET_assert (NULL != occ->hello);
2175 other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
2176 if (NULL != occ->peer2_controller)
2178 struct GNUNET_TESTBED_RequestConnectMessage *msg;
2180 uint16_t hello_size;
2182 LOG_DEBUG ("Offering HELLO of %s to %s via Remote Overlay Request\n",
2183 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2184 hello_size = ntohs (occ->hello->size);
2185 msize = sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hello_size;
2186 msg = GNUNET_malloc (msize);
2187 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT);
2188 msg->header.size = htons (msize);
2189 msg->peer = htonl (occ->other_peer_id);
2190 msg->operation_id = GNUNET_htonll (occ->op_id);
2191 (void) memcpy (&msg->peer_identity, &occ->peer_identity,
2192 sizeof (struct GNUNET_PeerIdentity));
2193 memcpy (msg->hello, occ->hello, hello_size);
2194 GNUNET_TESTBED_queue_message_ (occ->peer2_controller, &msg->header);
2198 LOG_DEBUG ("Offering HELLO of %s to %s\n",
2199 GNUNET_i2s (&occ->peer_identity), other_peer_str);
2200 GNUNET_TRANSPORT_offer_hello (occ->p2th, occ->hello, NULL, NULL);
2201 GNUNET_TRANSPORT_try_connect (occ->p2th, &occ->peer_identity);
2202 occ->send_hello_task =
2203 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2206 GNUNET_free (other_peer_str);
2210 * Test for checking whether HELLO message is empty
2212 * @param cls empty flag to set
2213 * @param address the HELLO
2214 * @param expiration expiration of the HELLO
2218 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
2219 struct GNUNET_TIME_Absolute expiration)
2229 * Function called whenever there is an update to the HELLO of peers in the
2230 * OverlayConnectClosure. If we have a valid HELLO, we connect to the peer 2's
2231 * transport and offer peer 1's HELLO and ask peer 2 to connect to peer 1
2233 * @param cls closure
2234 * @param hello our updated HELLO
2237 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
2239 struct OverlayConnectContext *occ = cls;
2243 msize = ntohs (hello->size);
2245 (void) GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *)
2246 hello, GNUNET_NO, &test_address,
2248 if (GNUNET_YES == empty)
2250 LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->peer_identity));
2253 LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->peer_identity));
2254 occ->hello = GNUNET_malloc (msize);
2255 memcpy (occ->hello, hello, msize);
2256 GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
2258 GNUNET_TRANSPORT_disconnect (occ->p1th);
2260 GNUNET_free_non_null (occ->emsg);
2261 if (NULL == occ->peer2_controller)
2264 GNUNET_TRANSPORT_connect (peer_list[occ->other_peer_id]->details.local.cfg,
2265 &occ->other_peer_identity, NULL, NULL, NULL,
2267 if (NULL == occ->p2th)
2269 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of %s\n",
2270 GNUNET_i2s (&occ->other_peer_identity));
2271 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2272 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2276 occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
2277 occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
2282 * Function called after GNUNET_CORE_connect has succeeded (or failed
2283 * for good). Note that the private key of the peer is intentionally
2284 * not exposed here; if you need it, your process should try to read
2285 * the private key file directly (which should work if you are
2288 * @param cls closure
2289 * @param server handle to the server, NULL if we failed
2290 * @param my_identity ID of this peer, NULL if we failed
2293 core_startup_cb (void *cls, struct GNUNET_CORE_Handle *server,
2294 const struct GNUNET_PeerIdentity *my_identity)
2296 struct OverlayConnectContext *occ = cls;
2298 GNUNET_free_non_null (occ->emsg);
2299 occ->emsg = GNUNET_strdup ("Failed to connect to CORE\n");
2300 if ((NULL == server) || (NULL == my_identity))
2302 GNUNET_free (occ->emsg);
2305 memcpy (&occ->peer_identity, my_identity,
2306 sizeof (struct GNUNET_PeerIdentity));
2308 GNUNET_TRANSPORT_connect (occ->peer->details.local.cfg,
2309 &occ->peer_identity, NULL, NULL, NULL, NULL);
2310 if (NULL == occ->p1th)
2312 GNUNET_asprintf (&occ->emsg, "Cannot connect to TRANSPORT of peers %4s",
2313 GNUNET_i2s (&occ->peer_identity));
2316 LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s (&occ->peer_identity));
2317 occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
2318 occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p1th, &hello_update_cb, occ);
2322 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2323 occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2329 * Callback to be called when forwarded get peer config operation as part of
2330 * overlay connect is successfull. Connection to Peer 1's core is made and is
2331 * checked for new connection from peer 2
2333 * @param cls ForwardedOperationContext
2334 * @param msg the peer create success message
2337 overlay_connect_get_config (void *cls, const struct GNUNET_MessageHeader *msg)
2339 struct OverlayConnectContext *occ = cls;
2340 const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *cmsg;
2341 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2346 if (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG != ntohs (msg->type))
2348 cmsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
2350 memcpy (&occ->other_peer_identity, &cmsg->peer_identity,
2351 sizeof (struct GNUNET_PeerIdentity));
2352 GNUNET_free_non_null (occ->emsg);
2353 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2355 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2356 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2357 GNUNET_NO, no_handlers);
2358 if (NULL == occ->ch)
2363 GNUNET_SCHEDULER_cancel (occ->timeout_task);
2365 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2370 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
2373 * @param client identification of the client
2374 * @param message the actual message
2377 handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
2378 const struct GNUNET_MessageHeader *message)
2380 const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
2381 struct OverlayConnectContext *occ;
2382 const struct GNUNET_CORE_MessageHandler no_handlers[] = {
2386 uint64_t operation_id;
2390 msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
2391 p1 = ntohl (msg->peer1);
2392 p2 = ntohl (msg->peer2);
2393 GNUNET_assert (p1 < peer_list_size);
2394 GNUNET_assert (NULL != peer_list[p1]);
2395 peer = peer_list[p1];
2396 operation_id = GNUNET_ntohll (msg->operation_id);
2397 if (GNUNET_YES == peer->is_remote)
2399 struct ForwardedOperationContext *fopc;
2401 fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
2402 GNUNET_SERVER_client_keep (client);
2403 fopc->client = client;
2404 fopc->operation_id = operation_id;
2406 GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.controller,
2407 operation_id, message,
2408 &forwarded_operation_reply_relay,
2410 fopc->timeout_task =
2411 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &forwarded_operation_timeout,
2413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2416 occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
2417 GNUNET_SERVER_client_keep (client);
2418 occ->client = client;
2420 occ->other_peer_id = p2;
2421 occ->peer = peer_list[p1];
2422 occ->op_id = GNUNET_ntohll (msg->operation_id);
2423 if ((p2 >= peer_list_size) || (NULL == peer_list[p2]))
2425 uint32_t peer2_host_id;
2427 peer2_host_id = ntohl (msg->peer2_host_id);
2428 if ((peer2_host_id >= slave_list_size)
2429 || (NULL ==slave_list[peer2_host_id]))
2432 GNUNET_SERVER_client_drop (client);
2434 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2437 occ->peer2_controller = slave_list[peer2_host_id]->controller;
2438 if (NULL == occ->peer2_controller)
2441 GNUNET_SERVER_client_drop (client);
2443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2449 if (GNUNET_YES == peer_list[occ->other_peer_id]->is_remote)
2450 occ->peer2_controller = peer_list[occ->other_peer_id]->details.remote.controller;
2452 /* Get the identity of the second peer */
2453 if (NULL != occ->peer2_controller)
2455 struct GNUNET_TESTBED_PeerGetConfigurationMessage cmsg;
2458 htons (sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage));
2459 cmsg.header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG);
2460 cmsg.peer_id = msg->peer2;
2461 cmsg.operation_id = msg->operation_id;
2463 GNUNET_TESTBED_forward_operation_msg_ (occ->peer2_controller,
2464 occ->op_id, &cmsg.header,
2465 &overlay_connect_get_config,
2468 GNUNET_strdup ("Timeout while getting peer identity of peer B\n");
2470 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2471 (GNUNET_TIME_UNIT_SECONDS, 30),
2472 &timeout_overlay_connect, occ);
2473 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2476 GNUNET_TESTING_peer_get_identity (peer_list[occ->other_peer_id]->details.local.peer,
2477 &occ->other_peer_identity);
2478 /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
2479 occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
2481 GNUNET_CORE_connect (occ->peer->details.local.cfg, occ, &core_startup_cb,
2482 &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
2483 GNUNET_NO, no_handlers);
2484 if (NULL == occ->ch)
2486 GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);
2489 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
2490 (GNUNET_TIME_UNIT_SECONDS, 30),
2491 &timeout_overlay_connect, occ);
2492 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2497 * Function to cleanup RequestOverlayConnectContext and any associated tasks
2500 * @param rocc the RequestOverlayConnectContext
2503 cleanup_rocc (struct RequestOverlayConnectContext *rocc)
2505 if (GNUNET_SCHEDULER_NO_TASK != rocc->attempt_connect_task_id)
2506 GNUNET_SCHEDULER_cancel (rocc->attempt_connect_task_id);
2507 if (GNUNET_SCHEDULER_NO_TASK != rocc->timeout_rocc_task_id)
2508 GNUNET_SCHEDULER_cancel (rocc->timeout_rocc_task_id);
2509 GNUNET_TRANSPORT_disconnect (rocc->th);
2510 GNUNET_free_non_null (rocc->hello);
2516 * Task to timeout rocc and cleanit up
2518 * @param cls the RequestOverlayConnectContext
2519 * @param tc the TaskContext from scheduler
2522 timeout_rocc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2524 struct RequestOverlayConnectContext *rocc = cls;
2526 rocc->timeout_rocc_task_id = GNUNET_SCHEDULER_NO_TASK;
2527 cleanup_rocc (rocc);
2532 * Function called to notify transport users that another
2533 * peer connected to us.
2535 * @param cls closure
2536 * @param new_peer the peer that connected
2537 * @param ats performance data
2538 * @param ats_count number of entries in ats (excluding 0-termination)
2541 transport_connect_notify (void *cls, const struct GNUNET_PeerIdentity *new_peer,
2542 const struct GNUNET_ATS_Information * ats,
2545 struct RequestOverlayConnectContext *rocc = cls;
2547 LOG_DEBUG ("Request Overlay connect notify\n");
2548 if (0 != memcmp (new_peer, &rocc->a_id, sizeof (struct GNUNET_PeerIdentity)))
2550 LOG_DEBUG ("Peer %4s connected\n", GNUNET_i2s (&rocc->a_id));
2551 cleanup_rocc (rocc);
2556 * Task to offer the HELLO message to the peer and ask it to connect to the peer
2557 * whose identity is in RequestOverlayConnectContext
2559 * @param cls the RequestOverlayConnectContext
2560 * @param tc the TaskContext from scheduler
2563 attempt_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2565 struct RequestOverlayConnectContext *rocc = cls;
2567 rocc->attempt_connect_task_id = GNUNET_SCHEDULER_NO_TASK;
2568 GNUNET_TRANSPORT_offer_hello (rocc->th, rocc->hello, NULL, NULL);
2569 GNUNET_TRANSPORT_try_connect (rocc->th, &rocc->a_id);
2570 rocc->attempt_connect_task_id =
2571 GNUNET_SCHEDULER_add_delayed (TRANSPORT_TRY_CONNECT_TIMEOUT,
2572 &attempt_connect_task, rocc);
2577 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT messages
2580 * @param client identification of the client
2581 * @param message the actual message
2584 handle_overlay_request_connect (void *cls, struct GNUNET_SERVER_Client *client,
2585 const struct GNUNET_MessageHeader *message)
2587 const struct GNUNET_TESTBED_RequestConnectMessage *msg;
2588 struct RequestOverlayConnectContext *rocc;
2594 msize = ntohs (message->size);
2595 if (sizeof (struct GNUNET_TESTBED_RequestConnectMessage) >= msize)
2598 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2601 msg = (const struct GNUNET_TESTBED_RequestConnectMessage *) message;
2602 if ((NULL == msg->hello) ||
2603 (GNUNET_MESSAGE_TYPE_HELLO != ntohs (msg->hello->type)))
2606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2609 hsize = ntohs (msg->hello->size);
2610 if ((sizeof (struct GNUNET_TESTBED_RequestConnectMessage) + hsize) != msize)
2613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2616 peer_id = ntohl (msg->peer);
2617 if ((peer_id >= peer_list_size) || (NULL == (peer = peer_list[peer_id])))
2619 GNUNET_break_op (0);
2620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2623 if (GNUNET_NO != peer->is_remote)
2626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2629 rocc = GNUNET_malloc (sizeof (struct RequestOverlayConnectContext));
2630 rocc->th = GNUNET_TRANSPORT_connect (peer->details.local.cfg, NULL, rocc,
2631 NULL, &transport_connect_notify, NULL);
2632 if (NULL == rocc->th)
2636 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
2639 memcpy (&rocc->a_id, &msg->peer_identity,
2640 sizeof (struct GNUNET_PeerIdentity));
2641 rocc->hello = GNUNET_malloc (hsize);
2642 memcpy (rocc->hello, msg->hello, hsize);
2643 /* GNUNET_TRANSPORT_offer_hello (th, msg->hello, NULL, NULL); */
2644 /* GNUNET_TRANSPORT_try_connect (th, &msg->peer_identity); */
2645 rocc->attempt_connect_task_id =
2646 GNUNET_SCHEDULER_add_now (&attempt_connect_task, rocc);
2647 rocc->timeout_rocc_task_id =
2648 GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_rocc_task, rocc);
2649 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2654 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
2657 * @param client identification of the client
2658 * @param message the actual message
2661 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
2662 const struct GNUNET_MessageHeader *message)
2664 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
2668 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
2669 slave_id = ntohl (msg->slave_id);
2670 op_id = GNUNET_ntohll (msg->operation_id);
2671 if ((slave_list_size <= slave_id) || (NULL == slave_list[slave_id]))
2673 send_operation_fail_msg (client, op_id, "Slave not found");
2674 GNUNET_SERVER_receive_done (client, GNUNET_OK);
2682 * Iterator over hash map entries.
2684 * @param cls closure
2685 * @param key current key code
2686 * @param value value in the hash map
2687 * @return GNUNET_YES if we should continue to
2692 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
2694 struct SharedService *ss = value;
2696 GNUNET_assert (GNUNET_YES ==
2697 GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
2698 GNUNET_free (ss->name);
2705 * Task to clean up and shutdown nicely
2708 * @param tc the TaskContext from scheduler
2711 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2713 struct LCFContextQueue *lcfq;
2716 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2717 LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2718 (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2720 GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2721 if (NULL != lcfq_head)
2723 if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2725 GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2726 lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2728 if (NULL != lcfq_head->lcf->rhandle)
2729 GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
2731 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2732 for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2734 GNUNET_free (lcfq->lcf->msg);
2735 GNUNET_free (lcfq->lcf);
2736 GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2739 /* Clear peer list */
2740 for (id = 0; id < peer_list_size; id++)
2741 if (NULL != peer_list[id])
2743 if (GNUNET_NO == peer_list[id]->is_remote)
2745 if (GNUNET_YES == peer_list[id]->details.local.is_running)
2746 GNUNET_TESTING_peer_stop (peer_list[id]->details.local.peer);
2747 GNUNET_TESTING_peer_destroy (peer_list[id]->details.local.peer);
2748 GNUNET_CONFIGURATION_destroy (peer_list[id]->details.local.cfg);
2750 GNUNET_free (peer_list[id]);
2752 GNUNET_free_non_null (peer_list);
2753 /* Clear host list */
2754 for (id = 0; id < host_list_size; id++)
2755 if (NULL != host_list[id])
2756 GNUNET_TESTBED_host_destroy (host_list[id]);
2757 GNUNET_free_non_null (host_list);
2758 /* Clear route list */
2759 for (id = 0; id < route_list_size; id++)
2760 if (NULL != route_list[id])
2761 GNUNET_free (route_list[id]);
2762 GNUNET_free_non_null (route_list);
2763 /* Clear slave_list */
2764 for (id = 0; id < slave_list_size; id++)
2765 if (NULL != slave_list[id])
2767 if (NULL != slave_list[id]->cfg)
2768 GNUNET_CONFIGURATION_destroy (slave_list[id]->cfg);
2769 if (NULL != slave_list[id]->controller)
2770 GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
2771 if (NULL != slave_list[id]->controller_proc)
2772 GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
2773 GNUNET_free (slave_list[id]);
2775 GNUNET_free_non_null (slave_list);
2776 if (NULL != master_context)
2778 GNUNET_free_non_null (master_context->master_ip);
2779 if (NULL != master_context->system)
2780 GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
2781 GNUNET_free (master_context);
2782 master_context = NULL;
2788 * Callback for client disconnect
2791 * @param client the client which has disconnected
2794 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2796 if (NULL == master_context)
2798 if (client == master_context->client)
2800 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2801 GNUNET_SERVER_client_drop (client);
2802 /* should not be needed as we're terminated by failure to read
2803 * from stdin, but if stdin fails for some reason, this shouldn't
2804 * hurt for now --- might need to revise this later if we ever
2805 * decide that master connections might be temporarily down
2806 * for some reason */
2807 //GNUNET_SCHEDULER_shutdown ();
2815 * @param cls closure
2816 * @param server the initialized server
2817 * @param cfg configuration to use
2820 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2821 const struct GNUNET_CONFIGURATION_Handle *cfg)
2823 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2824 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2825 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2826 {&handle_configure_shared_service, NULL,
2827 GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2828 {&handle_link_controllers, NULL,
2829 GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2830 {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2831 {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2832 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2833 {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2834 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2835 {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2836 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2837 {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2838 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2839 {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2840 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2841 {&handle_overlay_request_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_REQUESTCONNECT,
2843 {handle_slave_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG,
2844 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
2848 GNUNET_SERVER_add_handlers (server, message_handlers);
2849 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2850 ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2852 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2853 &shutdown_task, NULL);
2854 LOG_DEBUG ("Testbed startup complete\n");
2859 * The starting point of execution
2862 main (int argc, char *const *argv)
2864 //sleep (15); /* Debugging */
2865 return (GNUNET_OK ==
2866 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2867 &testbed_run, NULL)) ? 0 : 1;
2870 /* end of gnunet-service-testbed.c */