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