2 This file is part of GNUnet
3 (C) 2008--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 3, 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/testbed_api.c
23 * @brief API for accessing the GNUnet testing service.
24 * This library is supposed to make it easier to write
25 * testcases and script large-scale benchmarks.
26 * @author Christian Grothoff
27 * @author Sree Harsha Totakura
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_core_service.h"
34 #include "gnunet_constants.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_hello_lib.h"
40 #include "testbed_api.h"
41 #include "testbed_api_hosts.h"
42 #include "testbed_api_peers.h"
45 * Generic logging shorthand
47 #define LOG(kind, ...) \
48 GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
53 #define LOG_DEBUG(...) \
54 LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
57 * Relative time seconds shorthand
59 #define TIME_REL_SECS(sec) \
60 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
64 * Default server message sending retry timeout
66 #define TIMEOUT_REL TIME_REL_SECS(1)
70 * The message queue for sending messages to the controller service
75 * The message to be sent
77 struct GNUNET_MessageHeader *msg;
80 * next pointer for DLL
82 struct MessageQueue *next;
85 * prev pointer for DLL
87 struct MessageQueue *prev;
92 * Structure for a controller link
97 * The next ptr for DLL
99 struct ControllerLink *next;
102 * The prev ptr for DLL
104 struct ControllerLink *prev;
107 * The host which will be referred in the peer start request. This is the
108 * host where the peer should be started
110 struct GNUNET_TESTBED_Host *delegated_host;
113 * The host which will contacted to delegate the peer start request
115 struct GNUNET_TESTBED_Host *slave_host;
118 * The configuration to be used to connect to slave host
120 const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
123 * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
124 * if we are just allowed to use the slave via TCP/IP
131 * handle for host registration
133 struct GNUNET_TESTBED_HostRegistrationHandle
136 * The host being registered
138 struct GNUNET_TESTBED_Host *host;
141 * The controller at which this host is being registered
143 struct GNUNET_TESTBED_Controller *c;
146 * The Registartion completion callback
148 GNUNET_TESTBED_HostRegistrationCompletion cc;
151 * The closure for above callback
158 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
159 * controller (testbed service)
161 * @param c the controller handler
162 * @param msg message received
163 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
167 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
168 const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
170 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
179 if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
181 LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
182 GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
186 msg_size = ntohs (msg->header.size);
187 if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
189 LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
190 GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
191 rh->cc (rh->cc_cls, NULL);
195 /* We have an error message */
196 emsg = (char *) &msg[1];
197 if ('\0' != emsg[msg_size -
198 sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
204 LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
205 ntohl (msg->host_id), emsg);
206 rh->cc (rh->cc_cls, emsg);
213 * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
214 * controller (testbed service)
216 * @param c the controller handler
217 * @param msg message received
218 * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
222 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
224 GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
226 struct GNUNET_TESTBED_Operation *op;
227 struct GNUNET_TESTBED_EventInformation *event;
230 op_id = GNUNET_ntohll (msg->operation_id);
231 LOG_DEBUG ("Operation %ul successful\n", op_id);
232 for (op = c->op_head; NULL != op; op = op->next)
234 if (op->operation_id == op_id)
239 LOG_DEBUG ("Operation not found\n");
243 if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
244 event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
246 event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
249 case OP_PEER_DESTROY:
251 struct PeerDestroyData *data;
255 event->details.operation_finished.operation = op;
256 event->details.operation_finished.op_cls = NULL;
257 event->details.operation_finished.emsg = NULL;
258 event->details.operation_finished.pit = GNUNET_TESTBED_PIT_GENERIC;
259 event->details.operation_finished.op_result.generic = NULL;
261 data = (struct PeerDestroyData *) op->data;
262 if (NULL != data->peer->details)
264 if (NULL != data->peer->details->cfg)
265 GNUNET_CONFIGURATION_destroy (data->peer->details->cfg);
268 GNUNET_free (data->peer);
279 c->cc (c->cc_cls, event);
282 GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
289 * Handler for messages from controller (testbed service)
291 * @param cls the controller handler
292 * @param msg message received, NULL on timeout or fatal error
295 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
297 struct GNUNET_TESTBED_Controller *c = cls;
300 c->in_receive = GNUNET_NO;
301 /* FIXME: Add checks for message integrity */
304 LOG_DEBUG ("Receive timed out or connection to service dropped\n");
308 switch (ntohs (msg->type))
310 case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
312 handle_addhostconfirm (c, (const struct
313 GNUNET_TESTBED_HostConfirmedMessage *) msg);
315 case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
317 handle_opsuccess (c, (const struct
318 GNUNET_TESTBED_GenericOperationSuccessEventMessage
324 if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
326 c->in_receive = GNUNET_YES;
327 GNUNET_CLIENT_receive (c->client, &message_handler, c,
328 GNUNET_TIME_UNIT_FOREVER_REL);
334 * Function called to notify a client about the connection begin ready to queue
335 * more data. "buf" will be NULL and "size" zero if the connection was closed
336 * for writing in the meantime.
339 * @param size number of bytes available in buf
340 * @param buf where the callee should write the message
341 * @return number of bytes written to buf
344 transmit_ready_notify (void *cls, size_t size, void *buf)
346 struct GNUNET_TESTBED_Controller *c = cls;
347 struct MessageQueue *mq_entry;
350 mq_entry = c->mq_head;
351 GNUNET_assert (NULL != mq_entry);
352 if ((0 == size) && (NULL == buf)) /* Timeout */
354 LOG_DEBUG ("Message sending timed out -- retrying\n");
356 GNUNET_CLIENT_notify_transmit_ready (c->client,
357 ntohs (mq_entry->msg->size),
359 GNUNET_YES, &transmit_ready_notify,
363 GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
364 size = ntohs (mq_entry->msg->size);
365 memcpy (buf, mq_entry->msg, size);
366 LOG_DEBUG ("Message of type: %u and size: %u sent\n",
367 ntohs (mq_entry->msg->type), size);
368 GNUNET_free (mq_entry->msg);
369 GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
370 GNUNET_free (mq_entry);
371 mq_entry = c->mq_head;
372 if (NULL != mq_entry)
374 GNUNET_CLIENT_notify_transmit_ready (c->client,
375 ntohs (mq_entry->msg->size),
377 GNUNET_YES, &transmit_ready_notify,
379 if (GNUNET_NO == c->in_receive)
381 c->in_receive = GNUNET_YES;
382 GNUNET_CLIENT_receive (c->client, &message_handler, c,
383 GNUNET_TIME_UNIT_FOREVER_REL);
390 * Queues a message in send queue for sending to the service
392 * @param controller the handle to the controller
393 * @param msg the message to queue
396 GNUNET_TESTBED_queue_message (struct GNUNET_TESTBED_Controller *controller,
397 struct GNUNET_MessageHeader *msg)
399 struct MessageQueue *mq_entry;
403 type = ntohs (msg->type);
404 size = ntohs (msg->size);
405 GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
406 (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
407 mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
409 LOG (GNUNET_ERROR_TYPE_DEBUG,
410 "Queueing message of type %u, size %u for sending\n", type,
412 GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
414 if (NULL == controller->th)
416 GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
418 GNUNET_YES, &transmit_ready_notify,
424 * Handle for controller process
426 struct GNUNET_TESTBED_ControllerProc
431 struct GNUNET_HELPER_Handle *helper;
434 * The host where the helper is run
436 struct GNUNET_TESTBED_Host *host;
439 * The controller error callback
441 GNUNET_TESTBED_ControllerStatusCallback cb;
444 * The closure for the above callback
448 * The send handle for the helper
450 struct GNUNET_HELPER_SendHandle *shandle;
453 * The port number for ssh; used for helpers starting ssh
458 * The ssh destination string; used for helpers starting ssh
463 * The configuration of the running testbed service
465 struct GNUNET_CONFIGURATION_Handle *cfg;
471 * Functions with this signature are called whenever a
472 * complete message is received by the tokenizer.
474 * Do not call GNUNET_SERVER_mst_destroy in callback
477 * @param client identification of the client
478 * @param message the actual message
480 * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
482 static int helper_mst (void *cls, void *client,
483 const struct GNUNET_MessageHeader *message)
485 struct GNUNET_TESTBED_ControllerProc *cp = cls;
486 const struct GNUNET_TESTBED_HelperReply *msg;
491 msg = (const struct GNUNET_TESTBED_HelperReply *) message;
492 GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply)
493 < ntohs (msg->header.size));
494 GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY
495 == ntohs (msg->header.type));
496 config_size = (uLongf) ntohs (msg->config_size);
497 xconfig_size = (uLongf) (ntohs (msg->header.size)
498 - sizeof (struct GNUNET_TESTBED_HelperReply));
499 config = GNUNET_malloc (config_size);
500 GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
501 (const Bytef *) &msg[1], xconfig_size));
502 GNUNET_assert (NULL == cp->cfg);
503 cp->cfg = GNUNET_CONFIGURATION_create ();
504 GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config,
505 config_size, GNUNET_NO));
506 /* Change the hostname so that we can connect to it */
507 GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
508 (NULL == cp->cfg) ? "localhost" :
509 GNUNET_TESTBED_host_get_hostname_ (cp->host));
510 cp->cb (cp->cls, cp->cfg, GNUNET_OK);
516 * Continuation function from GNUNET_HELPER_send()
519 * @param result GNUNET_OK on success,
520 * GNUNET_NO if helper process died
521 * GNUNET_SYSERR during GNUNET_HELPER_stop
524 clear_msg (void *cls, int result)
531 * Callback that will be called when the helper process dies. This is not called
532 * when the helper process is stoped using GNUNET_HELPER_stop()
534 * @param cls the closure from GNUNET_HELPER_start()
537 helper_exp_cb (void *cls)
539 struct GNUNET_TESTBED_ControllerProc *cp = cls;
540 GNUNET_TESTBED_ControllerStatusCallback cb;
545 GNUNET_TESTBED_controller_stop (cp);
547 cb (cb_cls, NULL, GNUNET_SYSERR);
552 * Starts a controller process at the host. FIXME: add controller start callback
553 * with the configuration with which the controller is started
555 * @param controller_ip the ip address of the controller. Will be set as TRUSTED
556 * host when starting testbed controller at host
557 * @param host the host where the controller has to be started; NULL for
559 * @param cfg template configuration to use for the remote controller; the
560 * remote controller will be started with a slightly modified
561 * configuration (port numbers, unix domain sockets and service home
562 * values are changed as per TESTING library on the remote host)
563 * @param cb function called when the controller is successfully started or
564 * dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
565 * called if cb is called with GNUNET_SYSERR as status. Will never be
566 * called in the same task as 'GNUNET_TESTBED_controller_start'
567 * (synchronous errors will be signalled by returning NULL)
568 * @param cls closure for above callbacks
569 * @return the controller process handle, NULL on errors
571 struct GNUNET_TESTBED_ControllerProc *
572 GNUNET_TESTBED_controller_start (const char *controller_ip,
573 struct GNUNET_TESTBED_Host *host,
574 const struct GNUNET_CONFIGURATION_Handle *cfg,
575 GNUNET_TESTBED_ControllerStatusCallback cb,
578 struct GNUNET_TESTBED_ControllerProc *cp;
579 struct GNUNET_TESTBED_HelperInit *msg;
581 cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
582 if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
584 char * const binary_argv[] = {
585 "gnunet-testbed-helper", NULL
588 cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv,
589 &helper_mst, &helper_exp_cb, cp);
593 char *remote_args[6 + 1];
595 const char *username;
596 const char *hostname;
598 username = GNUNET_TESTBED_host_get_username_ (host);
599 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
600 GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
601 if (NULL == username)
602 GNUNET_asprintf (&cp->dst, "%s", hostname);
604 GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
606 remote_args[argp++] = "ssh";
607 remote_args[argp++] = "-p";
608 remote_args[argp++] = cp->port;
609 remote_args[argp++] = "-q";
610 remote_args[argp++] = cp->dst;
611 remote_args[argp++] = "gnunet-testbed-helper";
612 remote_args[argp++] = NULL;
613 GNUNET_assert (argp == 6 + 1);
614 cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
615 &helper_mst, &helper_exp_cb, cp);
617 if (NULL == cp->helper)
619 GNUNET_free_non_null (cp->port);
620 GNUNET_free_non_null (cp->dst);
627 msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
628 cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
630 if (NULL == cp->shandle)
633 GNUNET_TESTBED_controller_stop (cp);
641 * Stop the controller process (also will terminate all peers and controllers
642 * dependent on this controller). This function blocks until the testbed has
643 * been fully terminated (!).
645 * @param cproc the controller process handle
648 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cp)
650 if (NULL != cp->shandle)
651 GNUNET_HELPER_send_cancel (cp->shandle);
652 GNUNET_HELPER_stop (cp->helper);
653 GNUNET_free_non_null (cp->port);
654 GNUNET_free_non_null (cp->dst);
660 * Start a controller process using the given configuration at the
663 * @param cfg configuration to use
664 * @param host host to run the controller on; This should be the same host if
665 * the controller was previously started with
666 * GNUNET_TESTBED_controller_start; NULL for localhost
667 * @param event_mask bit mask with set of events to call 'cc' for;
668 * or-ed values of "1LL" shifted by the
669 * respective 'enum GNUNET_TESTBED_EventType'
670 * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
671 * @param cc controller callback to invoke on events
672 * @param cc_cls closure for cc
673 * @return handle to the controller
675 struct GNUNET_TESTBED_Controller *
676 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
677 struct GNUNET_TESTBED_Host *host,
679 GNUNET_TESTBED_ControllerCallback cc,
682 struct GNUNET_TESTBED_Controller *controller;
683 struct GNUNET_TESTBED_InitMessage *msg;
685 controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
687 controller->cc_cls = cc_cls;
688 controller->event_mask = event_mask;
689 controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
690 controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
691 if (NULL == controller->client)
693 GNUNET_TESTBED_controller_disconnect (controller);
698 host = GNUNET_TESTBED_host_create_by_id_ (0);
701 LOG (GNUNET_ERROR_TYPE_WARNING,
702 "Treating NULL host as localhost. Multiple references to localhost. "
703 " May break when localhost freed before calling disconnect \n");
704 host = GNUNET_TESTBED_host_lookup_by_id_ (0);
708 controller->aux_host = GNUNET_YES;
711 GNUNET_assert (NULL != host);
712 msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
713 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
714 msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
715 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
716 msg->event_mask = GNUNET_htonll (controller->event_mask);
717 GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
723 * Configure shared services at a controller. Using this function,
724 * you can specify that certain services (such as "resolver")
725 * should not be run for each peer but instead be shared
726 * across N peers on the specified host. This function
727 * must be called before any peers are created at the host.
729 * @param controller controller to configure
730 * @param service_name name of the service to share
731 * @param num_peers number of peers that should share one instance
732 * of the specified service (1 for no sharing is the default),
733 * use 0 to disable the service
736 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
737 const char *service_name,
740 struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
741 uint16_t service_name_size;
744 service_name_size = strlen (service_name) + 1;
745 msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
747 msg = GNUNET_malloc (msg_size);
748 msg->header.size = htons (msg_size);
749 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
750 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
751 msg->num_peers = htonl (num_peers);
752 memcpy (&msg[1], service_name, service_name_size);
753 GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
758 * disconnects from the controller.
760 * @param controller handle to controller to stop
763 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
765 struct MessageQueue *mq_entry;
767 if (NULL != controller->th)
768 GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
769 /* Clear the message queue */
770 while (NULL != (mq_entry = controller->mq_head))
772 GNUNET_CONTAINER_DLL_remove (controller->mq_head,
775 GNUNET_free (mq_entry->msg);
776 GNUNET_free (mq_entry);
778 if (NULL != controller->client)
779 GNUNET_CLIENT_disconnect (controller->client);
780 GNUNET_CONFIGURATION_destroy (controller->cfg);
781 if (GNUNET_YES == controller->aux_host)
782 GNUNET_TESTBED_host_destroy (controller->host);
783 GNUNET_free (controller);
788 * Register a host with the controller
790 * @param controller the controller handle
791 * @param host the host to register
792 * @param cc the completion callback to call to inform the status of
793 * registration. After calling this callback the registration handle
794 * will be invalid. Cannot be NULL.
795 * @param cc_cls the closure for the cc
796 * @return handle to the host registration which can be used to cancel the
799 struct GNUNET_TESTBED_HostRegistrationHandle *
800 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
801 struct GNUNET_TESTBED_Host *host,
802 GNUNET_TESTBED_HostRegistrationCompletion cc,
805 struct GNUNET_TESTBED_HostRegistrationHandle *rh;
806 struct GNUNET_TESTBED_AddHostMessage *msg;
807 const char *username;
808 const char *hostname;
810 uint16_t user_name_length;
812 if (NULL != controller->rh)
814 hostname = GNUNET_TESTBED_host_get_hostname_ (host);
815 if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
817 LOG (GNUNET_ERROR_TYPE_WARNING,
818 "Host hostname: %s already registered\n",
819 (NULL == hostname) ? "localhost" : hostname);
822 rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
825 GNUNET_assert (NULL != cc);
829 username = GNUNET_TESTBED_host_get_username_ (host);
830 msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
831 user_name_length = 0;
832 if (NULL != username)
834 user_name_length = strlen (username) + 1;
835 msg_size += user_name_length;
837 /* FIXME: what happens when hostname is NULL? localhost */
838 GNUNET_assert (NULL != hostname);
839 msg_size += strlen (hostname) + 1;
840 msg = GNUNET_malloc (msg_size);
841 msg->header.size = htons (msg_size);
842 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
843 msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
844 msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
845 msg->user_name_length = htons (user_name_length);
846 if (NULL != username)
847 memcpy (&msg[1], username, user_name_length);
848 strcpy (((void *) &msg[1]) + user_name_length, hostname);
849 GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
855 * Cancel the pending registration. Note that if the registration message is
856 * already sent to the service the cancellation has only the effect that the
857 * registration completion callback for the registration is never called.
859 * @param handle the registration handle to cancel
862 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
865 if (handle != handle->c->rh)
870 handle->c->rh = NULL;
871 GNUNET_free (handle);
876 * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
877 * serialized and compressed
879 * @param master handle to the master controller who creates the association
880 * @param delegated_host requests to which host should be delegated; cannot be NULL
881 * @param slave_host which host is used to run the slave controller; use NULL to
882 * make the master controller connect to the delegated host
883 * @param sxcfg serialized and compressed configuration
884 * @param sxcfg_size the size scfg
885 * @param scfg_size the size of uncompressed serialized configuration
886 * @param is_subordinate GNUNET_YES if the controller at delegated_host should
887 * be started by the master controller; GNUNET_NO if we are just
888 * allowed to use the slave via TCP/IP
891 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
892 struct GNUNET_TESTBED_Host *delegated_host,
893 struct GNUNET_TESTBED_Host *slave_host,
899 struct GNUNET_TESTBED_ControllerLinkMessage *msg;
902 GNUNET_assert (GNUNET_YES ==
903 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
904 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
907 msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
908 msg = GNUNET_malloc (msg_size);
909 msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
910 msg->header.size = htons (msg_size);
911 msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
912 msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_
913 ((NULL != slave_host) ? slave_host : master->host));
914 msg->config_size = htons ((uint16_t) scfg_size);
915 msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
916 memcpy (&msg[1], sxcfg, sxcfg_size);
917 GNUNET_TESTBED_queue_message (master, (struct GNUNET_MessageHeader *) msg);
922 * Compresses given configuration using zlib compress
924 * @param config the serialized configuration
925 * @param size the size of config
926 * @param xconfig will be set to the compressed configuration (memory is fresly
928 * @return the size of the xconfig
931 GNUNET_TESTBED_compress_config (const char *config, size_t size,
936 xsize = compressBound ((uLong) size);
937 *xconfig = GNUNET_malloc (xsize);
938 GNUNET_assert (Z_OK ==
939 compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
940 (const Bytef *) config, (uLongf) size,
947 * Create a link from slave controller to delegated controller. Whenever the
948 * master controller is asked to start a peer at the delegated controller the
949 * request will be routed towards slave controller (if a route exists). The
950 * slave controller will then route it to the delegated controller. The
951 * configuration of the slave controller is given and to be used to either
952 * create the slave controller or to connect to an existing slave controller
953 * process. 'is_subordinate' specifies if the given slave controller should be
954 * started and managed by the master controller, or if the slave already has a
955 * master and this is just a secondary master that is also allowed to use the
958 * @param master handle to the master controller who creates the association
959 * @param delegated_host requests to which host should be delegated
960 * @param slave_host which host is used to run the slave controller
961 * @param slave_cfg configuration to use for the slave controller
962 * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
963 * by the master controller; GNUNET_NO if we are just
964 * allowed to use the slave via TCP/IP
967 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
968 struct GNUNET_TESTBED_Host *delegated_host,
969 struct GNUNET_TESTBED_Host *slave_host,
970 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
978 GNUNET_assert (GNUNET_YES ==
979 GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
980 if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
981 GNUNET_assert (GNUNET_YES ==
982 GNUNET_TESTBED_is_host_registered_ (slave_host, master));
983 config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
984 cc_size = GNUNET_TESTBED_compress_config (config, config_size, &cconfig);
985 GNUNET_free (config);
986 GNUNET_assert ((UINT16_MAX -
987 sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
988 >= cc_size); /* Configuration doesn't fit in 1 message */
989 GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
990 (const char *) cconfig,
991 cc_size, config_size, is_subordinate);
992 GNUNET_free (cconfig);
997 * Ask the testbed controller to write the current overlay topology to
998 * a file. Naturally, the file will only contain a snapshot as the
999 * topology may evolve all the time.
1001 * @param controller overlay controller to inspect
1002 * @param filename name of the file the topology should
1006 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1007 const char *filename)
1014 /* end of testbed_api.c */