operation and peer_destroy
[oweals/gnunet.git] / src / testbed / testbed_api.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
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
28  */
29
30
31 #include "platform.h"
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"
37 #include <zlib.h>
38
39 #include "testbed.h"
40 #include "testbed_api.h"
41 #include "testbed_api_hosts.h"
42
43 /**
44  * Generic logging shorthand
45  */
46 #define LOG(kind, ...)                          \
47   GNUNET_log_from (kind, "testbed-api", __VA_ARGS__);
48
49 /**
50  * Debug logging
51  */
52 #define LOG_DEBUG(...)                          \
53   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__);
54
55 /**
56  * Relative time seconds shorthand
57  */
58 #define TIME_REL_SECS(sec) \
59   GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
60
61
62 /**
63  * Default server message sending retry timeout
64  */
65 #define TIMEOUT_REL TIME_REL_SECS(1)
66
67
68 /**
69  * The message queue for sending messages to the controller service
70  */
71 struct MessageQueue
72 {
73   /**
74    * The message to be sent
75    */
76   struct GNUNET_MessageHeader *msg;
77
78   /**
79    * next pointer for DLL
80    */
81   struct MessageQueue *next;
82   
83   /**
84    * prev pointer for DLL
85    */
86   struct MessageQueue *prev;
87 };
88
89
90 /**
91  * Structure for a controller link
92  */
93 struct ControllerLink
94 {
95   /**
96    * The next ptr for DLL
97    */
98   struct ControllerLink *next;
99
100   /**
101    * The prev ptr for DLL
102    */
103   struct ControllerLink *prev;
104
105   /**
106    * The host which will be referred in the peer start request. This is the
107    * host where the peer should be started
108    */
109   struct GNUNET_TESTBED_Host *delegated_host;
110
111   /**
112    * The host which will contacted to delegate the peer start request
113    */
114   struct GNUNET_TESTBED_Host *slave_host;
115
116   /**
117    * The configuration to be used to connect to slave host
118    */
119   const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
120
121   /**
122    * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
123    * if we are just allowed to use the slave via TCP/IP
124    */
125   int is_subordinate;
126 };
127
128
129 /**
130  * Handle to interact with a GNUnet testbed controller.  Each
131  * controller has at least one master handle which is created when the
132  * controller is created; this master handle interacts with the
133  * controller process, destroying it destroys the controller (by
134  * closing stdin of the controller process).  Additionally,
135  * controllers can interact with each other (in a P2P fashion); those
136  * links are established via TCP/IP on the controller's service port.
137  */
138 struct GNUNET_TESTBED_Controller
139 {
140
141   /**
142    * The host where the controller is running
143    */
144   struct GNUNET_TESTBED_Host *host;
145
146   /**
147    * The controller callback
148    */
149   GNUNET_TESTBED_ControllerCallback cc;
150
151   /**
152    * The closure for controller callback
153    */
154   void *cc_cls;
155
156   /**
157    * The configuration to use while connecting to controller
158    */
159   struct GNUNET_CONFIGURATION_Handle *cfg;
160
161   /**
162    * The client connection handle to the controller service
163    */
164   struct GNUNET_CLIENT_Connection *client;
165   
166   /**
167    * The head of the message queue
168    */
169   struct MessageQueue *mq_head;
170
171   /**
172    * The tail of the message queue
173    */
174   struct MessageQueue *mq_tail;
175
176   /**
177    * The head of the ControllerLink list
178    */
179   struct ControllerLink *cl_head;
180
181   /**
182    * The tail of the ControllerLink list
183    */
184   struct ControllerLink *cl_tail;
185
186   /**
187    * The client transmit handle
188    */
189   struct GNUNET_CLIENT_TransmitHandle *th;
190
191   /**
192    * The host registration handle; NULL if no current registration requests are
193    * present 
194    */
195   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
196
197   /**
198    * The controller event mask
199    */
200   uint64_t event_mask;
201
202   /**
203    * Did we start the receive loop yet?
204    */
205   int in_receive;
206
207   /**
208    * Did we create the host for this?
209    */
210   int aux_host;
211 };
212
213
214 /**
215  * handle for host registration
216  */
217 struct GNUNET_TESTBED_HostRegistrationHandle
218 {
219   /**
220    * The host being registered
221    */
222   struct GNUNET_TESTBED_Host *host;
223
224   /**
225    * The controller at which this host is being registered
226    */
227   struct GNUNET_TESTBED_Controller *c;
228
229   /**
230    * The Registartion completion callback
231    */
232   GNUNET_TESTBED_HostRegistrationCompletion cc;
233
234   /**
235    * The closure for above callback
236    */
237   void *cc_cls;
238 };
239
240
241 /**
242  * The global operation id counter
243  */
244 uint64_t GNUNET_TESTBED_operation_id;
245
246 /**
247  * The head of the operation queue
248  */
249 struct GNUNET_TESTBED_Operation *op_head;
250
251 /**
252  * The tail of the operation queue
253  */
254 struct GNUNET_TESTBED_Operation *op_tail;
255
256
257 /**
258  * Adds an operation to the queue of operations
259  *
260  * @param op the operation to add
261  */
262 void
263 GNUNET_TESTBED_operation_add (struct GNUNET_TESTBED_Operation *op)
264 {
265   GNUNET_CONTAINER_DLL_insert_tail (op_head, op_tail, op);
266 }
267
268
269 /**
270  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
271  * controller (testbed service)
272  *
273  * @param c the controller handler
274  * @param msg message received
275  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
276  *           not
277  */
278 static int
279 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
280                        const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
281 {
282   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
283   char *emsg;
284   uint16_t msg_size;
285
286   rh = c->rh;
287   if (NULL == rh)
288   {  
289     return GNUNET_OK;    
290   }
291   if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
292   {
293     LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
294                GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
295     return GNUNET_OK;
296   }
297   c->rh = NULL;
298   msg_size = ntohs (msg->header.size);
299   if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
300   {
301     LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
302     GNUNET_TESTBED_mark_host_registered_at_  (rh->host, c);
303     rh->cc (rh->cc_cls, NULL);
304     GNUNET_free (rh);
305     return GNUNET_OK;
306   } 
307   /* We have an error message */
308   emsg = (char *) &msg[1];
309   if ('\0' != emsg[msg_size - 
310                    sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
311   {
312     GNUNET_break (0);
313     GNUNET_free (rh);
314     return GNUNET_NO;
315   }  
316   LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
317        ntohl (msg->host_id), emsg);
318   rh->cc (rh->cc_cls, emsg);
319   GNUNET_free (rh);
320   return GNUNET_OK;
321 }
322
323
324 /**
325  * Handler for messages from controller (testbed service)
326  *
327  * @param cls the controller handler
328  * @param msg message received, NULL on timeout or fatal error
329  */
330 static void 
331 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
332 {
333   struct GNUNET_TESTBED_Controller *c = cls;  
334   int status;
335
336   c->in_receive = GNUNET_NO;
337   /* FIXME: Add checks for message integrity */
338   if (NULL == msg)
339   {
340     LOG_DEBUG ("Receive timed out or connection to service dropped\n");
341     return;
342   }
343   status = GNUNET_OK;
344   switch (ntohs (msg->type))
345   {
346   case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
347     status =
348       handle_addhostconfirm (c, (const struct
349                                  GNUNET_TESTBED_HostConfirmedMessage *) msg);   
350     break;
351   default:
352     GNUNET_break (0);
353   }
354   if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
355   {
356     c->in_receive = GNUNET_YES;
357     GNUNET_CLIENT_receive (c->client, &message_handler, c,
358                            GNUNET_TIME_UNIT_FOREVER_REL);    
359   }
360 }
361
362 /**
363  * Function called to notify a client about the connection begin ready to queue
364  * more data.  "buf" will be NULL and "size" zero if the connection was closed
365  * for writing in the meantime.
366  *
367  * @param cls closure
368  * @param size number of bytes available in buf
369  * @param buf where the callee should write the message
370  * @return number of bytes written to buf
371  */
372 static size_t
373 transmit_ready_notify (void *cls, size_t size, void *buf)
374 {
375   struct GNUNET_TESTBED_Controller *c = cls;
376   struct MessageQueue *mq_entry;
377
378   c->th = NULL;
379   mq_entry = c->mq_head;
380   GNUNET_assert (NULL != mq_entry);
381   if ((0 == size) && (NULL == buf)) /* Timeout */
382   {
383     LOG_DEBUG ("Message sending timed out -- retrying\n");
384     c->th =
385       GNUNET_CLIENT_notify_transmit_ready (c->client,
386                                            ntohs (mq_entry->msg->size),
387                                            TIMEOUT_REL,
388                                            GNUNET_YES, &transmit_ready_notify,
389                                            c);
390     return 0;
391   }
392   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
393   size = ntohs (mq_entry->msg->size);  
394   memcpy (buf, mq_entry->msg, size);
395   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
396              ntohs (mq_entry->msg->type), size);
397   GNUNET_free (mq_entry->msg);
398   GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
399   GNUNET_free (mq_entry);
400   mq_entry = c->mq_head;
401   if (NULL != mq_entry)
402     c->th = 
403       GNUNET_CLIENT_notify_transmit_ready (c->client,
404                                            ntohs (mq_entry->msg->size),
405                                            TIMEOUT_REL,
406                                            GNUNET_YES, &transmit_ready_notify,
407                                            c);
408   if (GNUNET_NO == c->in_receive)
409   {
410     c->in_receive = GNUNET_YES;
411     GNUNET_CLIENT_receive (c->client, &message_handler, c,
412                            GNUNET_TIME_UNIT_FOREVER_REL);
413   }
414   return size;
415 }
416
417
418 /**
419  * Queues a message in send queue for sending to the service
420  *
421  * @param controller the handle to the controller
422  * @param msg the message to queue
423  */
424 void
425 GNUNET_TESTBED_queue_message (struct GNUNET_TESTBED_Controller *controller,
426                               struct GNUNET_MessageHeader *msg)
427 {
428   struct MessageQueue *mq_entry;
429   uint16_t type;
430   uint16_t size;
431
432   type = ntohs (msg->type);
433   size = ntohs (msg->size);
434   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
435                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
436   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
437   mq_entry->msg = msg;
438   LOG (GNUNET_ERROR_TYPE_DEBUG,
439        "Queueing message of type %u, size %u for sending\n", type,
440        ntohs (msg->size));
441   GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
442                                     mq_entry);
443   if (NULL == controller->th)
444     controller->th = 
445       GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
446                                            TIMEOUT_REL,
447                                            GNUNET_YES, &transmit_ready_notify,
448                                            controller);
449 }
450
451
452 /**
453  * Handle for controller process
454  */
455 struct GNUNET_TESTBED_ControllerProc
456 {
457   /**
458    * The helper handle
459    */
460   struct GNUNET_TESTBED_HelperHandle *helper;
461
462 };
463
464
465 /**
466  * Starts a controller process at the host
467  *
468  * @param host the host where the controller has to be started; NULL for localhost
469  * @return the controller process handle
470  */
471 struct GNUNET_TESTBED_ControllerProc *
472 GNUNET_TESTBED_controller_start (struct GNUNET_TESTBED_Host *host)
473 {
474   struct GNUNET_TESTBED_ControllerProc *cproc;
475   char * const binary_argv[] = {
476     "gnunet-service-testbed",
477     "gnunet-service-testbed",
478     NULL
479   };
480
481   cproc = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
482   cproc->helper = GNUNET_TESTBED_host_run_ (host, binary_argv);
483   if (NULL == cproc->helper)
484   {
485     GNUNET_free (cproc);
486     return NULL;
487   }
488   return cproc;
489 }
490
491
492 /**
493  * Stop the controller process (also will terminate all peers and controllers
494  * dependent on this controller).  This function blocks until the testbed has
495  * been fully terminated (!).
496  *
497  * @param cproc the controller process handle
498  */
499 void
500 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
501 {
502   GNUNET_TESTBED_host_stop_ (cproc->helper);
503   GNUNET_free (cproc);
504 }
505
506
507 /**
508  * Start a controller process using the given configuration at the
509  * given host.
510  *
511  * @param cfg configuration to use
512  * @param host host to run the controller on; This should be the same host if
513  *          the controller was previously started with
514  *          GNUNET_TESTBED_controller_start; NULL for localhost
515  * @param event_mask bit mask with set of events to call 'cc' for;
516  *                   or-ed values of "1LL" shifted by the
517  *                   respective 'enum GNUNET_TESTBED_EventType'
518  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
519  * @param cc controller callback to invoke on events
520  * @param cc_cls closure for cc
521  * @return handle to the controller
522  */
523 struct GNUNET_TESTBED_Controller *
524 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
525                                    struct GNUNET_TESTBED_Host *host,
526                                    uint64_t event_mask,
527                                    GNUNET_TESTBED_ControllerCallback cc,
528                                    void *cc_cls)
529 {
530   struct GNUNET_TESTBED_Controller *controller;
531   struct GNUNET_TESTBED_InitMessage *msg;
532
533   controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
534   controller->cc = cc;
535   controller->cc_cls = cc_cls;
536   controller->event_mask = event_mask;
537   controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
538   controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);  
539   if (NULL == controller->client)
540   {
541     GNUNET_TESTBED_controller_disconnect (controller);
542     return NULL;
543   }
544   if (NULL == host)
545   {
546     host = GNUNET_TESTBED_host_create_by_id_ (0);
547     if (NULL == host)
548     {
549       LOG (GNUNET_ERROR_TYPE_WARNING,
550            "Treating NULL host as localhost. Multiple references to localhost. "
551            " May break when localhost freed before calling disconnect \n");
552       host = GNUNET_TESTBED_host_lookup_by_id_ (0);
553     }
554     else
555     {
556       controller->aux_host = GNUNET_YES;
557     }
558   }
559   GNUNET_assert (NULL != host);
560   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
561   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
562   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
563   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
564   msg->event_mask = GNUNET_htonll (controller->event_mask);
565   GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
566   return controller;
567 }
568
569
570 /**
571  * Configure shared services at a controller.  Using this function,
572  * you can specify that certain services (such as "resolver")
573  * should not be run for each peer but instead be shared
574  * across N peers on the specified host.  This function
575  * must be called before any peers are created at the host.
576  * 
577  * @param controller controller to configure
578  * @param service_name name of the service to share
579  * @param num_peers number of peers that should share one instance
580  *        of the specified service (1 for no sharing is the default),
581  *        use 0 to disable the service
582  */
583 void
584 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
585                                              const char *service_name,
586                                              uint32_t num_peers)
587 {
588   struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
589   uint16_t service_name_size;
590   uint16_t msg_size;
591   
592   service_name_size = strlen (service_name) + 1;
593   msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
594     + service_name_size;
595   msg = GNUNET_malloc (msg_size);
596   msg->header.size = htons (msg_size);
597   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
598   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
599   msg->num_peers = htonl (num_peers);
600   memcpy (&msg[1], service_name, service_name_size);
601   GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
602 }
603
604
605 /**
606  * disconnects from the controller.
607  *
608  * @param controller handle to controller to stop
609  */
610 void
611 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
612 {
613   struct MessageQueue *mq_entry;
614
615   if (NULL != controller->th)
616     GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
617  /* Clear the message queue */
618   while (NULL != (mq_entry = controller->mq_head))
619   {
620     GNUNET_CONTAINER_DLL_remove (controller->mq_head,
621                                  controller->mq_tail,
622                                  mq_entry);
623     GNUNET_free (mq_entry->msg);
624     GNUNET_free (mq_entry);
625   }
626   if (NULL != controller->client)
627     GNUNET_CLIENT_disconnect (controller->client);
628   GNUNET_CONFIGURATION_destroy (controller->cfg);
629   if (GNUNET_YES == controller->aux_host)
630     GNUNET_TESTBED_host_destroy (controller->host);
631   GNUNET_free (controller);
632 }
633
634
635 /**
636  * Register a host with the controller
637  *
638  * @param controller the controller handle
639  * @param host the host to register
640  * @param cc the completion callback to call to inform the status of
641  *          registration. After calling this callback the registration handle
642  *          will be invalid. Cannot be NULL.
643  * @param cc_cls the closure for the cc
644  * @return handle to the host registration which can be used to cancel the
645  *           registration 
646  */
647 struct GNUNET_TESTBED_HostRegistrationHandle *
648 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
649                               struct GNUNET_TESTBED_Host *host,
650                               GNUNET_TESTBED_HostRegistrationCompletion cc,
651                               void *cc_cls)
652 {
653   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
654   struct GNUNET_TESTBED_AddHostMessage *msg;
655   const char *username;
656   const char *hostname;
657   uint16_t msg_size;
658   uint16_t user_name_length;
659
660   if (NULL != controller->rh)
661     return NULL;
662   hostname = GNUNET_TESTBED_host_get_hostname_ (host);
663   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
664   {
665     LOG (GNUNET_ERROR_TYPE_WARNING,
666          "Host hostname: %s already registered\n",
667          (NULL == hostname) ? "localhost" : hostname);
668     return NULL;
669   }  
670   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
671   rh->host = host;
672   rh->c = controller;
673   GNUNET_assert (NULL != cc);
674   rh->cc = cc;
675   rh->cc_cls = cc_cls;
676   controller->rh = rh;
677   username = GNUNET_TESTBED_host_get_username_ (host);
678   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
679   user_name_length = 0;
680   if (NULL != username)
681   {
682     user_name_length = strlen (username) + 1;
683     msg_size += user_name_length;
684   }
685   /* FIXME: what happens when hostname is NULL? localhost */
686   GNUNET_assert (NULL != hostname);
687   msg_size += strlen (hostname) + 1;
688   msg = GNUNET_malloc (msg_size);
689   msg->header.size = htons (msg_size);
690   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
691   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
692   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
693   msg->user_name_length = htons (user_name_length);
694   if (NULL != username)
695     memcpy (&msg[1], username, user_name_length);
696   strcpy (((void *) &msg[1]) + user_name_length, hostname);
697   GNUNET_TESTBED_queue_message (controller, (struct GNUNET_MessageHeader *) msg);
698   return rh;
699 }
700
701
702 /**
703  * Cancel the pending registration. Note that if the registration message is
704  * already sent to the service the cancellation has only the effect that the
705  * registration completion callback for the registration is never called.
706  *
707  * @param handle the registration handle to cancel
708  */
709 void
710 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
711                                     *handle)
712 {
713   if (handle != handle->c->rh)
714   {
715     GNUNET_break (0);
716     return;
717   }
718   handle->c->rh = NULL;
719   GNUNET_free (handle);  
720 }
721
722
723 /**
724  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
725  * serialized and compressed
726  *
727  * @param master handle to the master controller who creates the association
728  * @param delegated_host requests to which host should be delegated; cannot be NULL
729  * @param slave_host which host is used to run the slave controller; use NULL to
730  *          make the master controller connect to the delegated host
731  * @param sxcfg serialized and compressed configuration
732  * @param sxcfg_size the size scfg
733  * @param scfg_size the size of uncompressed serialized configuration
734  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
735  *          be started by the master controller; GNUNET_NO if we are just
736  *          allowed to use the slave via TCP/IP
737  */
738 void
739 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
740                                   struct GNUNET_TESTBED_Host *delegated_host,
741                                   struct GNUNET_TESTBED_Host *slave_host,
742                                   const char *sxcfg,
743                                   size_t sxcfg_size,
744                                   size_t scfg_size,
745                                   int is_subordinate)
746 {
747   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
748   uint16_t msg_size;
749
750   GNUNET_assert (GNUNET_YES == 
751                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
752   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
753     GNUNET_assert (GNUNET_YES == 
754                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
755   msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
756   msg = GNUNET_malloc (msg_size);
757   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);  
758   msg->header.size = htons (msg_size);
759   msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
760   msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_ 
761                               ((NULL != slave_host) ? slave_host : master->host));
762   msg->config_size = htons ((uint16_t) scfg_size);
763   msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
764   memcpy (&msg[1], sxcfg, sxcfg_size);
765   GNUNET_TESTBED_queue_message (master, (struct GNUNET_MessageHeader *) msg);
766 }
767
768
769 /**
770  * Compresses given configuration using zlib compress
771  *
772  * @param config the serialized configuration
773  * @param size the size of config
774  * @param xconfig will be set to the compressed configuration (memory is fresly
775  *          allocated) 
776  * @return the size of the xconfig
777  */
778 size_t
779 GNUNET_TESTBED_compress_config (const char *config, size_t size,
780                                 char **xconfig)
781 {
782   size_t xsize;
783   
784   xsize = compressBound ((uLong) size);
785   *xconfig = GNUNET_malloc (xsize);
786   GNUNET_assert (Z_OK ==
787                  compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
788                             (const Bytef *) config, (uLongf) size, 
789                             Z_BEST_SPEED));
790   return xsize;
791 }
792                                 
793
794 /**
795  * Create a link from slave controller to delegated controller. Whenever the
796  * master controller is asked to start a peer at the delegated controller the
797  * request will be routed towards slave controller (if a route exists). The
798  * slave controller will then route it to the delegated controller. The
799  * configuration of the slave controller is given and to be used to either
800  * create the slave controller or to connect to an existing slave controller
801  * process.  'is_subordinate' specifies if the given slave controller should be
802  * started and managed by the master controller, or if the slave already has a
803  * master and this is just a secondary master that is also allowed to use the
804  * existing slave.
805  *
806  * @param master handle to the master controller who creates the association
807  * @param delegated_host requests to which host should be delegated
808  * @param slave_host which host is used to run the slave controller 
809  * @param slave_cfg configuration to use for the slave controller
810  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
811  *                       by the master controller; GNUNET_NO if we are just
812  *                       allowed to use the slave via TCP/IP
813  */
814 void
815 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
816                                 struct GNUNET_TESTBED_Host *delegated_host,
817                                 struct GNUNET_TESTBED_Host *slave_host,
818                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
819                                 int is_subordinate)
820 {
821   char *config;
822   char *cconfig;
823   size_t cc_size;
824   size_t config_size;  
825   
826   GNUNET_assert (GNUNET_YES == 
827                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
828   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
829     GNUNET_assert (GNUNET_YES == 
830                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
831   config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
832   cc_size = GNUNET_TESTBED_compress_config (config, config_size, &cconfig);
833   GNUNET_free (config);
834   GNUNET_assert ((UINT16_MAX -
835                   sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
836                   >= cc_size); /* Configuration doesn't fit in 1 message */
837   GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
838                                     (const char *) cconfig,
839                                     cc_size, config_size, is_subordinate);
840   GNUNET_free (cconfig);
841 }
842
843
844 /**
845  * Ask the testbed controller to write the current overlay topology to
846  * a file.  Naturally, the file will only contain a snapshot as the
847  * topology may evolve all the time.
848  *
849  * @param controller overlay controller to inspect
850  * @param filename name of the file the topology should
851  *        be written to.
852  */
853 void
854 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
855                                                const char *filename)
856 {
857   GNUNET_break (0);
858 }
859
860
861
862 /* end of testbed_api.c */