2 This file is part of GNUnet.
3 (C) 2008--2013 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
27 #include "gnunet-service-testbed.h"
37 struct GNUNET_CONFIGURATION_Handle *our_config;
40 * The master context; generated with the first INIT message
42 struct Context *GST_context;
47 struct GNUNET_TESTBED_Host **GST_host_list;
50 * DLL head for forwarded operation contexts
52 struct ForwardedOperationContext *fopcq_head;
55 * DLL tail for forwarded operation contexts
57 struct ForwardedOperationContext *fopcq_tail;
60 * Operation queue for open file descriptors
62 struct OperationQueue *GST_opq_openfds;
65 * Timeout for operations which may take some time
67 const struct GNUNET_TIME_Relative GST_timeout;
70 * The size of the host list
72 unsigned int GST_host_list_size;
75 * The size of the peer list
77 unsigned int GST_peer_list_size;
80 /***********************************/
81 /* Local definitions and variables */
82 /***********************************/
85 * The message queue for sending messages to clients
90 * The message to be sent
92 struct GNUNET_MessageHeader *msg;
95 * The client to send the message to
97 struct GNUNET_SERVER_Client *client;
100 * next pointer for DLL
102 struct MessageQueue *next;
105 * prev pointer for DLL
107 struct MessageQueue *prev;
111 * Our hostname; we give this to all the peers we start
113 static char *hostname;
116 * Current Transmit Handle; NULL if no notify transmit exists currently
118 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
121 * The message queue head
123 static struct MessageQueue *mq_head;
126 * The message queue tail
128 static struct MessageQueue *mq_tail;
132 * The shutdown task handle
134 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
138 * Function called to notify a client about the connection begin ready to queue
139 * more data. "buf" will be NULL and "size" zero if the connection was closed
140 * for writing in the meantime.
143 * @param size number of bytes available in buf
144 * @param buf where the callee should write the message
145 * @return number of bytes written to buf
148 transmit_ready_notify (void *cls, size_t size, void *buf)
150 struct MessageQueue *mq_entry;
152 transmit_handle = NULL;
154 GNUNET_assert (NULL != mq_entry);
157 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
158 size = ntohs (mq_entry->msg->size);
159 memcpy (buf, mq_entry->msg, size);
160 GNUNET_free (mq_entry->msg);
161 GNUNET_SERVER_client_drop (mq_entry->client);
162 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
163 GNUNET_free (mq_entry);
165 if (NULL != mq_entry)
167 GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
168 ntohs (mq_entry->msg->size),
169 GNUNET_TIME_UNIT_FOREVER_REL,
170 &transmit_ready_notify, NULL);
176 * Queues a message in send queue for sending to the service
178 * @param client the client to whom the queued message has to be sent
179 * @param msg the message to queue
182 GST_queue_message (struct GNUNET_SERVER_Client *client,
183 struct GNUNET_MessageHeader *msg)
185 struct MessageQueue *mq_entry;
189 type = ntohs (msg->type);
190 size = ntohs (msg->size);
191 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
192 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
193 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
195 mq_entry->client = client;
196 GNUNET_SERVER_client_keep (client);
197 LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
199 GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
200 if (NULL == transmit_handle)
202 GNUNET_SERVER_notify_transmit_ready (client, size,
203 GNUNET_TIME_UNIT_FOREVER_REL,
204 &transmit_ready_notify, NULL);
209 * Function to add a host to the current list of known hosts
211 * @param host the host to add
212 * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
216 host_list_add (struct GNUNET_TESTBED_Host *host)
220 host_id = GNUNET_TESTBED_host_get_id_ (host);
221 if (GST_host_list_size <= host_id)
222 GST_array_grow_large_enough (GST_host_list, GST_host_list_size, host_id);
223 if (NULL != GST_host_list[host_id])
225 LOG_DEBUG ("A host with id: %u already exists\n", host_id);
226 return GNUNET_SYSERR;
228 GST_host_list[host_id] = host;
234 * Send operation failure message to client
236 * @param client the client to which the failure message has to be sent to
237 * @param operation_id the id of the failed operation
238 * @param emsg the error message; can be NULL
241 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
242 uint64_t operation_id, const char *emsg)
244 struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
248 msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
249 emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
251 msg = GNUNET_malloc (msize);
252 msg->header.size = htons (msize);
253 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT);
254 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
255 msg->operation_id = GNUNET_htonll (operation_id);
257 memcpy (&msg[1], emsg, emsg_len);
258 GST_queue_message (client, &msg->header);
263 * Function to send generic operation success message to given client
265 * @param client the client to send the message to
266 * @param operation_id the id of the operation which was successful
269 GST_send_operation_success_msg (struct GNUNET_SERVER_Client *client,
270 uint64_t operation_id)
272 struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
275 msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
276 msg = GNUNET_malloc (msize);
277 msg->header.size = htons (msize);
279 htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS);
280 msg->operation_id = GNUNET_htonll (operation_id);
281 msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
282 GST_queue_message (client, &msg->header);
286 * Callback which will be called after a host registration succeeded or failed
288 * @param cls the handle to the slave at which the registration is completed
289 * @param emsg the error message; NULL if host registration is successful
292 hr_completion (void *cls, const char *emsg);
296 * Attempts to register the next host in the host registration queue
298 * @param slave the slave controller whose host registration queue is checked
299 * for host registrations
302 register_next_host (struct Slave *slave)
304 struct HostRegistration *hr;
306 hr = slave->hr_dll_head;
307 GNUNET_assert (NULL != hr);
308 GNUNET_assert (NULL == slave->rhandle);
309 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u\n",
310 GNUNET_TESTBED_host_get_id_ (hr->host),
311 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
313 GNUNET_TESTBED_register_host (slave->controller, hr->host, hr_completion,
319 * Callback which will be called to after a host registration succeeded or failed
321 * @param cls the handle to the slave at which the registration is completed
322 * @param emsg the error message; NULL if host registration is successful
325 hr_completion (void *cls, const char *emsg)
327 struct Slave *slave = cls;
328 struct HostRegistration *hr;
330 slave->rhandle = NULL;
331 hr = slave->hr_dll_head;
332 GNUNET_assert (NULL != hr);
333 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u successful\n",
334 GNUNET_TESTBED_host_get_id_ (hr->host),
335 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
336 GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head, slave->hr_dll_tail, hr);
338 hr->cb (hr->cb_cls, emsg);
340 if (NULL != slave->hr_dll_head)
341 register_next_host (slave);
346 * Adds a host registration's request to a slave's registration queue
348 * @param slave the slave controller at which the given host has to be
350 * @param cb the host registration completion callback
351 * @param cb_cls the closure for the host registration completion callback
352 * @param host the host which has to be registered
355 GST_queue_host_registration (struct Slave *slave,
356 GNUNET_TESTBED_HostRegistrationCompletion cb,
357 void *cb_cls, struct GNUNET_TESTBED_Host *host)
359 struct HostRegistration *hr;
362 LOG (GNUNET_ERROR_TYPE_DEBUG,
363 "Queueing host registration for host %u at %u\n",
364 GNUNET_TESTBED_host_get_id_ (host),
365 GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
366 hr = GNUNET_malloc (sizeof (struct HostRegistration));
370 call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
371 GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head, slave->hr_dll_tail, hr);
372 if (GNUNET_YES == call_register)
373 register_next_host (slave);
378 * Callback to relay the reply msg of a forwarded operation back to the client
380 * @param cls ForwardedOperationContext
381 * @param msg the message to relay
384 GST_forwarded_operation_reply_relay (void *cls,
385 const struct GNUNET_MessageHeader *msg)
387 struct ForwardedOperationContext *fopc = cls;
388 struct GNUNET_MessageHeader *dup_msg;
391 msize = ntohs (msg->size);
392 LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
394 dup_msg = GNUNET_copy_message (msg);
395 GST_queue_message (fopc->client, dup_msg);
396 GNUNET_SERVER_client_drop (fopc->client);
397 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
398 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
404 * Task to free resources when forwarded operation has been timedout
406 * @param cls the ForwardedOperationContext
407 * @param tc the task context from scheduler
410 GST_forwarded_operation_timeout (void *cls,
411 const struct GNUNET_SCHEDULER_TaskContext *tc)
413 struct ForwardedOperationContext *fopc = cls;
415 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
416 LOG (GNUNET_ERROR_TYPE_DEBUG, "A forwarded operation has timed out\n");
417 GST_send_operation_fail_msg (fopc->client, fopc->operation_id,
418 "A forwarded operation has timed out");
419 GNUNET_SERVER_client_drop (fopc->client);
420 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
426 * Parse service sharing specification line.
427 * Format is "[<service:share>] [<service:share>] ..."
429 * @param ss_str the spec string to be parsed
430 * @param cfg the configuration to use for shared services
431 * @return an array suitable to pass to GNUNET_TESTING_system_create(). NULL
432 * upon empty service sharing specification.
434 static struct GNUNET_TESTING_SharedService *
435 parse_shared_services (char *ss_str, struct GNUNET_CONFIGURATION_Handle *cfg)
437 struct GNUNET_TESTING_SharedService ss;
438 struct GNUNET_TESTING_SharedService *slist;
444 GNUNET_array_grow (slist, n, n+1); \
445 (void) memcpy (&slist[n - 1], &ss, \
446 sizeof (struct GNUNET_TESTING_SharedService)); \
452 for (; NULL != (arg = strtok (ss_str, " ")); ss_str = NULL)
456 if (2 != sscanf (arg, "%255[^:]:%u", service, &ss.share))
458 LOG (GNUNET_ERROR_TYPE_WARNING, "Ignoring shared service spec: %s", arg);
461 LOG_DEBUG ("Will be sharing %s service among %u peers\n", service, ss.share);
462 ss.service = GNUNET_strdup (service);
467 /* Add trailing NULL block */
468 (void) memset (&ss, 0, sizeof (struct GNUNET_TESTING_SharedService));
477 * Callback function invoked for each interface found.
480 * @param name name of the interface (can be NULL for unknown)
481 * @param isDefault is this presumably the default interface
482 * @param addr address of this interface (can be NULL for unknown or unassigned)
483 * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
484 * @param netmask the network mask (can be NULL for unknown or unassigned))
485 * @param addrlen length of the address
486 * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort
489 addr_proc (void *cls, const char *name, int isDefault,
490 const struct sockaddr *addr,
491 const struct sockaddr *broadcast_addr,
492 const struct sockaddr *netmask, socklen_t addrlen)
494 struct Context *ctx = cls;
495 const struct sockaddr_in *in_addr;
499 if (sizeof (struct sockaddr_in) != addrlen)
501 in_addr = (const struct sockaddr_in *) addr;
502 if (NULL == (ipaddr = inet_ntoa (in_addr->sin_addr)))
504 if (NULL == ctx->master_ips)
506 ctx->master_ips = GNUNET_strdup (ipaddr);
510 (void) GNUNET_asprintf (&tmp, "%s; %s", ctx->master_ips, ipaddr);
511 GNUNET_free (ctx->master_ips);
512 ctx->master_ips = tmp;
519 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
522 * @param client identification of the client
523 * @param message the actual message
526 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
527 const struct GNUNET_MessageHeader *message)
529 const struct GNUNET_TESTBED_InitMessage *msg;
530 struct GNUNET_TESTBED_Host *host;
532 struct GNUNET_TESTING_SharedService *ss;
537 if (NULL != GST_context)
539 LOG_DEBUG ("We are being connected to laterally\n");
540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
543 msg = (const struct GNUNET_TESTBED_InitMessage *) message;
544 len = GNUNET_OS_get_hostname_max_length ();
545 hostname = GNUNET_malloc (len);
546 if (0 != gethostname (hostname, len))
548 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "gethostname");
549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
554 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (our_config, "TESTBED",
558 ss = parse_shared_services (ss_str, our_config);
559 GNUNET_free (ss_str);
562 GST_context = GNUNET_malloc (sizeof (struct Context));
563 GNUNET_SERVER_client_keep (client);
564 GST_context->client = client;
565 GST_context->host_id = ntohl (msg->host_id);
566 GNUNET_OS_network_interfaces_list (&addr_proc, GST_context);
567 if (NULL == GST_context->master_ips)
569 LOG (GNUNET_ERROR_TYPE_ERROR,
570 "Testbed needs networking, but no network interfaces are found on this host. Exiting\n");
571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
572 GNUNET_SCHEDULER_shutdown ();
575 LOG_DEBUG ("Our IP addresses: %s\n", GST_context->master_ips);
576 GST_context->system =
577 GNUNET_TESTING_system_create ("testbed", GST_context->master_ips,
581 for (cnt = 0; NULL != ss[cnt].service; cnt++)
583 ss_str = (char *) ss[cnt].service;
584 GNUNET_free (ss_str);
590 host = GNUNET_TESTBED_host_create_with_id (GST_context->host_id, hostname,
591 NULL, our_config, 0);
592 host_list_add (host);
593 LOG_DEBUG ("Created master context with host ID: %u\n", GST_context->host_id);
594 GNUNET_SERVER_receive_done (client, GNUNET_OK);
599 * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
602 * @param client identification of the client
603 * @param message the actual message
606 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
607 const struct GNUNET_MessageHeader *message)
609 struct GNUNET_TESTBED_Host *host;
610 const struct GNUNET_TESTBED_AddHostMessage *msg;
611 struct GNUNET_TESTBED_HostConfirmedMessage *reply;
612 struct GNUNET_CONFIGURATION_Handle *host_cfg;
618 uint16_t username_length;
619 uint16_t hostname_length;
623 msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
624 msize = ntohs (msg->header.size);
625 if (msize <= sizeof (struct GNUNET_TESTBED_AddHostMessage))
628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
631 username_length = ntohs (msg->username_length);
632 hostname_length = ntohs (msg->hostname_length);
633 /* msg must contain hostname */
634 if ((msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
636 || (0 == hostname_length))
639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
642 /* msg must contain configuration */
643 if (msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
644 username_length + hostname_length))
647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
653 if (0 != username_length)
655 username = GNUNET_malloc (username_length + 1);
656 strncpy (username, ptr, username_length);
657 ptr += username_length;
659 hostname = GNUNET_malloc (hostname_length + 1);
660 strncpy (hostname, ptr, hostname_length);
661 ptr += hostname_length;
662 if (NULL == (host_cfg = GNUNET_TESTBED_extract_config_ (message)))
664 GNUNET_free_non_null (username);
665 GNUNET_free_non_null (hostname);
667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
670 host_id = ntohl (msg->host_id);
671 LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
672 LOG_DEBUG ("-------host id: %u\n", host_id);
673 LOG_DEBUG ("-------hostname: %s\n", hostname);
674 if (NULL != username)
675 LOG_DEBUG ("-------username: %s\n", username);
677 LOG_DEBUG ("-------username: <not given>\n");
678 LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
680 GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
681 host_cfg, ntohs (msg->ssh_port));
682 GNUNET_free_non_null (username);
683 GNUNET_free (hostname);
684 GNUNET_CONFIGURATION_destroy (host_cfg);
688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
691 reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
692 if (GNUNET_OK != host_list_add (host))
694 /* We are unable to add a host */
695 emsg = "A host exists with given host-id";
696 LOG_DEBUG ("%s: %u", emsg, host_id);
697 GNUNET_TESTBED_host_destroy (host);
698 reply_size += strlen (emsg) + 1;
699 reply = GNUNET_malloc (reply_size);
700 memcpy (&reply[1], emsg, strlen (emsg) + 1);
704 LOG_DEBUG ("Added host %u at %u\n", host_id, GST_context->host_id);
705 reply = GNUNET_malloc (reply_size);
707 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST_SUCCESS);
708 reply->header.size = htons (reply_size);
709 reply->host_id = htonl (host_id);
710 GST_queue_message (client, &reply->header);
711 GNUNET_SERVER_receive_done (client, GNUNET_OK);
716 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
719 * @param client identification of the client
720 * @param message the actual message
723 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
724 const struct GNUNET_MessageHeader *message)
726 struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
728 struct GNUNET_TESTBED_SlaveConfiguration *reply;
729 const struct GNUNET_CONFIGURATION_Handle *cfg;
738 msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
739 slave_id = ntohl (msg->slave_id);
740 op_id = GNUNET_ntohll (msg->operation_id);
741 if ((GST_slave_list_size <= slave_id) || (NULL == GST_slave_list[slave_id]))
743 /* FIXME: Add forwardings for this type of message here.. */
744 GST_send_operation_fail_msg (client, op_id, "Slave not found");
745 GNUNET_SERVER_receive_done (client, GNUNET_OK);
748 slave = GST_slave_list[slave_id];
749 GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (GST_host_list[slave->host_id])));
750 config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
752 GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
753 GNUNET_free (config);
754 reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
755 GNUNET_break (reply_size <= UINT16_MAX);
756 GNUNET_break (config_size <= UINT16_MAX);
757 reply = GNUNET_realloc (xconfig, reply_size);
758 (void) memmove (&reply[1], reply, xconfig_size);
759 reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION);
760 reply->header.size = htons ((uint16_t) reply_size);
761 reply->slave_id = msg->slave_id;
762 reply->operation_id = msg->operation_id;
763 reply->config_size = htons ((uint16_t) config_size);
764 GST_queue_message (client, &reply->header);
765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
770 * Clears the forwarded operations queue
775 struct ForwardedOperationContext *fopc;
777 while (NULL != (fopc = fopcq_head))
779 GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
780 GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
781 if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
782 GNUNET_SCHEDULER_cancel (fopc->timeout_task);
783 GNUNET_SERVER_client_drop (fopc->client);
787 GNUNET_free (fopc->cls);
789 case OP_SHUTDOWN_PEERS:
791 struct HandlerContext_ShutdownPeers *hc = fopc->cls;
793 GNUNET_assert (0 < hc->nslaves);
795 if (0 == hc->nslaves)
801 case OP_PEER_DESTROY:
803 case OP_OVERLAY_CONNECT:
804 case OP_LINK_CONTROLLERS:
805 case OP_GET_SLAVE_CONFIG:
806 case OP_MANAGE_SERVICE:
807 case OP_PEER_RECONFIGURE:
818 * Task to clean up and shutdown nicely
821 * @param tc the TaskContext from scheduler
824 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
826 struct MessageQueue *mq_entry;
829 shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
830 LOG_DEBUG ("Shutting down testbed service\n");
831 /* cleanup any remaining forwarded operations */
838 GST_neighbour_list_clean();
840 /* Clear peer list */
841 GST_destroy_peers ();
842 /* Clear route list */
843 GST_route_list_clear ();
844 /* Clear GST_slave_list */
845 GST_slave_list_clear ();
846 /* Clear host list */
847 for (id = 0; id < GST_host_list_size; id++)
848 if (NULL != GST_host_list[id])
849 GNUNET_TESTBED_host_destroy (GST_host_list[id]);
850 GNUNET_free_non_null (GST_host_list);
851 if (NULL != GST_context)
853 GNUNET_free_non_null (GST_context->master_ips);
854 if (NULL != GST_context->system)
855 GNUNET_TESTING_system_destroy (GST_context->system, GNUNET_YES);
856 GNUNET_SERVER_client_drop (GST_context->client);
857 GNUNET_free (GST_context);
860 if (NULL != transmit_handle)
861 GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
862 while (NULL != (mq_entry = mq_head))
864 GNUNET_free (mq_entry->msg);
865 GNUNET_SERVER_client_drop (mq_entry->client);
866 GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
867 GNUNET_free (mq_entry);
869 GNUNET_free_non_null (hostname);
870 GNUNET_CONFIGURATION_destroy (our_config);
871 /* Free hello cache */
873 GNUNET_TESTBED_operation_queue_destroy_ (GST_opq_openfds);
874 GST_opq_openfds = NULL;
875 GST_stats_destroy ();
880 * Callback for client disconnect
883 * @param client the client which has disconnected
886 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
888 if (NULL == GST_context)
890 if (client == GST_context->client)
892 LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
893 /* should not be needed as we're terminated by failure to read
894 * from stdin, but if stdin fails for some reason, this shouldn't
895 * hurt for now --- might need to revise this later if we ever
896 * decide that master connections might be temporarily down
898 //GNUNET_SCHEDULER_shutdown ();
907 * @param server the initialized server
908 * @param cfg configuration to use
911 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
912 const struct GNUNET_CONFIGURATION_Handle *cfg)
914 static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
915 {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
916 sizeof (struct GNUNET_TESTBED_InitMessage)},
917 {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST, 0},
918 {&GST_handle_link_controllers, NULL,
919 GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS,
920 sizeof (struct GNUNET_TESTBED_ControllerLinkRequest)},
921 {&GST_handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER, 0},
922 {&GST_handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER,
923 sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
924 {&GST_handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_START_PEER,
925 sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
926 {&GST_handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER,
927 sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
928 {&GST_handle_peer_get_config, NULL,
929 GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION,
930 sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
931 {&GST_handle_overlay_connect, NULL,
932 GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT,
933 sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
934 {&GST_handle_remote_overlay_connect, NULL,
935 GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT, 0},
936 {&GST_handle_manage_peer_service, NULL,
937 GNUNET_MESSAGE_TYPE_TESTBED_MANAGE_PEER_SERVICE, 0},
938 {&handle_slave_get_config, NULL,
939 GNUNET_MESSAGE_TYPE_TESTBED_GET_SLAVE_CONFIGURATION,
940 sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
941 {&GST_handle_shutdown_peers, NULL, GNUNET_MESSAGE_TYPE_TESTBED_SHUTDOWN_PEERS,
942 sizeof (struct GNUNET_TESTBED_ShutdownPeersMessage)},
943 {&GST_handle_peer_reconfigure, NULL,
944 GNUNET_MESSAGE_TYPE_TESTBED_RECONFIGURE_PEER, 0},
948 unsigned long long num;
950 LOG_DEBUG ("Starting testbed\n");
952 GNUNET_CONFIGURATION_get_value_filename (cfg, "TESTBED", "LOG_FILE",
955 GNUNET_break (GNUNET_OK == GNUNET_log_setup ("testbed", "DEBUG", logfile));
956 GNUNET_free (logfile);
958 GNUNET_assert (GNUNET_OK ==
959 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
960 "CACHE_SIZE", &num));
961 GST_cache_init ((unsigned int) num);
962 GNUNET_assert (GNUNET_OK ==
963 GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
964 "MAX_OPEN_FDS", &num));
965 GST_opq_openfds = GNUNET_TESTBED_operation_queue_create_ ((unsigned int) num);
966 GNUNET_assert (GNUNET_OK ==
967 GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
970 GNUNET_TIME_Relative *)
972 GNUNET_assert (GNUNET_OK ==
973 GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
974 "HOSTNAME", &hostname));
975 our_config = GNUNET_CONFIGURATION_dup (cfg);
976 GNUNET_SERVER_add_handlers (server, message_handlers);
977 GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
979 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
980 GNUNET_SCHEDULER_PRIORITY_IDLE,
981 &shutdown_task, NULL);
982 LOG_DEBUG ("Testbed startup complete\n");
983 GST_stats_init (our_config);
988 * The starting point of execution
991 main (int argc, char *const *argv)
993 //sleep (15); /* Debugging */
995 GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
996 &testbed_run, NULL)) ? 0 : 1;
999 /* end of gnunet-service-testbed.c */