-helper service shutdown fixes; overlay connect message
[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 for host registration
132  */
133 struct GNUNET_TESTBED_HostRegistrationHandle
134 {
135   /**
136    * The host being registered
137    */
138   struct GNUNET_TESTBED_Host *host;
139
140   /**
141    * The controller at which this host is being registered
142    */
143   struct GNUNET_TESTBED_Controller *c;
144
145   /**
146    * The Registartion completion callback
147    */
148   GNUNET_TESTBED_HostRegistrationCompletion cc;
149
150   /**
151    * The closure for above callback
152    */
153   void *cc_cls;
154 };
155
156
157 /**
158  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
159  * controller (testbed service)
160  *
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
164  *           not
165  */
166 static int
167 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
168                        const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
169 {
170   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
171   char *emsg;
172   uint16_t msg_size;
173
174   rh = c->rh;
175   if (NULL == rh)
176   {  
177     return GNUNET_OK;    
178   }
179   if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
180   {
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));
183     return GNUNET_OK;
184   }
185   c->rh = NULL;
186   msg_size = ntohs (msg->header.size);
187   if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
188   {
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);
192     GNUNET_free (rh);
193     return GNUNET_OK;
194   } 
195   /* We have an error message */
196   emsg = (char *) &msg[1];
197   if ('\0' != emsg[msg_size - 
198                    sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
199   {
200     GNUNET_break (0);
201     GNUNET_free (rh);
202     return GNUNET_NO;
203   }  
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);
207   GNUNET_free (rh);
208   return GNUNET_OK;
209 }
210
211
212 /**
213  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
214  * controller (testbed service)
215  *
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
219  *           not
220  */
221 static int
222 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
223                   const struct
224                   GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
225 {
226   struct GNUNET_TESTBED_Operation *op;
227   struct GNUNET_TESTBED_EventInformation *event;
228   uint64_t op_id;
229   
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)
233   {
234     if (op->operation_id == op_id)
235       break;
236   }
237   if (NULL == op)
238   {
239     LOG_DEBUG ("Operation not found\n");
240     return GNUNET_YES;
241   }
242   event = NULL;
243   if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
244     event = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_EventInformation));
245   if (NULL != event)
246     event->type = GNUNET_TESTBED_ET_OPERATION_FINISHED; 
247   switch (op->type)
248   {
249   case OP_PEER_DESTROY:
250     {
251       struct PeerDestroyData *data;
252       
253       if (NULL != event)
254       {
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;
260       }
261       data = (struct PeerDestroyData *) op->data;
262       if (NULL != data->peer->details)
263       {
264         if (NULL != data->peer->details->cfg)
265           GNUNET_CONFIGURATION_destroy (data->peer->details->cfg);
266         //PEER_DETAILS
267       }
268       GNUNET_free (data->peer);
269       GNUNET_free (data);
270       op->data = NULL;
271       //PEERDESTROYDATA
272     }
273     break;
274   default:
275     GNUNET_assert (0);
276   }  
277   GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
278   if (NULL != event)
279   {
280     if (NULL != c->cc)
281       c->cc (c->cc_cls, event);
282     GNUNET_free (event);
283   }
284   return GNUNET_YES;  
285 }
286
287
288 /**
289  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
290  * controller (testbed service)
291  *
292  * @param c the controller handler
293  * @param msg message received
294  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
295  *           not
296  */
297 static int
298 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
299                             const struct
300                             GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
301 {
302   struct GNUNET_TESTBED_Operation *op;
303   struct PeerCreateData *data;
304   struct GNUNET_TESTBED_Peer *peer;
305   GNUNET_TESTBED_PeerCreateCallback cb;
306   void *cls;
307   uint64_t op_id;
308
309   GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage)
310                  == ntohs (msg->header.size));
311   op_id = GNUNET_ntohll (msg->operation_id);
312   for (op = c->op_head; NULL != op; op = op->next)
313   {
314     if (op->operation_id == op_id)
315       break;
316   }
317   if (NULL == op)
318   {
319     LOG_DEBUG ("Operation not found\n");
320     return GNUNET_YES;
321   }
322   GNUNET_assert (OP_PEER_CREATE == op->type);
323   GNUNET_assert (NULL != op->data);
324   data = op->data;
325   GNUNET_assert (NULL != data->peer);
326   peer = data->peer;
327   GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
328   peer->state = PS_CREATED;
329   cb = data->cb;
330   cls = data->cls;
331   GNUNET_free (data);
332   op->data = NULL;
333   GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
334   if (NULL != cb)
335     cb (cls, peer, NULL);
336   return GNUNET_YES;
337 }
338
339
340 /**
341  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
342  * controller (testbed service)
343  *
344  * @param c the controller handler
345  * @param msg message received
346  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
347  *           not
348  */
349 static int
350 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
351                    const struct GNUNET_TESTBED_PeerEventMessage *msg)
352 {
353   struct GNUNET_TESTBED_Operation *op;
354   struct GNUNET_TESTBED_Peer *peer;
355   struct GNUNET_TESTBED_EventInformation event;
356   uint64_t op_id;
357
358   GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage)
359                  == ntohs (msg->header.size));
360   op_id = GNUNET_ntohll (msg->operation_id);
361   for (op = c->op_head; NULL != op; op = op->next)
362   {
363     if (op->operation_id == op_id)
364       break;
365   }
366   if (NULL == op)
367   {
368     LOG_DEBUG ("Operation not found\n");
369     return GNUNET_YES;
370   }
371   GNUNET_assert ((OP_PEER_START == op->type) || (OP_PEER_STOP == op->type));
372   peer = op->data;
373   GNUNET_assert (NULL != peer);
374   event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
375   switch (event.type)
376   {
377   case GNUNET_TESTBED_ET_PEER_START:
378     peer->state = PS_STARTED;
379     event.details.peer_start.host = peer->host;
380     event.details.peer_start.peer = peer;
381     break;
382   case GNUNET_TESTBED_ET_PEER_STOP:
383     peer->state = PS_STOPPED;    
384     event.details.peer_stop.peer = peer;  
385     break;
386   default:
387     GNUNET_assert (0);          /* We should never reach this state */
388   }
389   GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
390   if (0 != ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP)
391             & c->event_mask))
392   {
393     if (NULL != c->cc)
394       c->cc (c->cc_cls, &event);
395   }
396   return GNUNET_YES;
397 }
398
399
400 /**
401  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
402  * controller (testbed service)
403  *
404  * @param c the controller handler
405  * @param msg message received
406  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
407  *           not
408  */
409 static int
410 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
411                     const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
412 {
413   struct GNUNET_TESTBED_Operation *op;
414   struct GNUNET_TESTBED_Peer *peer;
415   struct PeerInfoData *data;
416   struct PeerInfoData2 *response_data;
417   struct GNUNET_TESTBED_EventInformation info;
418   uint64_t op_id;
419   
420   op_id = GNUNET_ntohll (msg->operation_id);
421   for (op = c->op_head; NULL != op; op = op->next)
422   {
423     if (op->operation_id == op_id)
424       break;
425   }
426   if (NULL == op)
427   {
428     LOG_DEBUG ("Operation not found");
429     return GNUNET_YES;
430   }
431   data = op->data;
432   GNUNET_assert (NULL != data);
433   peer = data->peer;
434   GNUNET_assert (NULL != peer);
435   GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
436   if (0 == (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
437   {
438     LOG_DEBUG ("Skipping operation callback as flag not set\n");
439     GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
440     return GNUNET_YES;
441   }
442   response_data = GNUNET_malloc (sizeof (struct PeerInfoData2));
443   response_data->pit = data->pit;
444   GNUNET_free (data);
445   info.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
446   info.details.operation_finished.operation = op;
447   info.details.operation_finished.op_cls = NULL;
448   info.details.operation_finished.emsg = NULL;
449   info.details.operation_finished.pit = response_data->pit;
450   switch (response_data->pit)
451   {
452   case GNUNET_TESTBED_PIT_IDENTITY:
453     {
454       struct GNUNET_PeerIdentity *peer_identity;
455
456       peer_identity = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
457       (void) memcpy (peer_identity, &msg->peer_identity, 
458                      sizeof (struct GNUNET_PeerIdentity));
459       response_data->details.peer_identity = peer_identity;      
460       info.details.operation_finished.op_result.pid = peer_identity;
461     }
462     break;
463   case GNUNET_TESTBED_PIT_CONFIGURATION:
464     {
465       struct GNUNET_CONFIGURATION_Handle *cfg;
466       char *config;
467       uLong config_size;
468       int ret;
469       uint16_t msize;
470       
471       config_size = (uLong) ntohs (msg->config_size);
472       config = GNUNET_malloc (config_size);
473       msize = ntohs (msg->header.size);
474       msize -= sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
475       if (Z_OK != (ret = uncompress ((Bytef *) config, &config_size,
476                                      (const Bytef *) &msg[1], (uLong) msize)))
477         GNUNET_assert (0);
478       cfg = GNUNET_CONFIGURATION_create ();
479       GNUNET_assert (GNUNET_OK == 
480                      GNUNET_CONFIGURATION_deserialize (cfg, config,
481                                                        (size_t) config_size,
482                                                        GNUNET_NO));
483       GNUNET_free (config);
484       response_data->details.cfg = cfg;
485       info.details.operation_finished.op_result.cfg = cfg;
486     }
487     break;
488   case GNUNET_TESTBED_PIT_GENERIC:
489     GNUNET_assert (0);          /* never reach here */
490     break;
491   }
492   op->data = response_data;
493   GNUNET_CONTAINER_DLL_remove (c->op_head, c->op_tail, op);
494   c->cc (c->cc_cls, &info);
495   return GNUNET_YES;
496 }
497
498
499 /**
500  * Handler for messages from controller (testbed service)
501  *
502  * @param cls the controller handler
503  * @param msg message received, NULL on timeout or fatal error
504  */
505 static void 
506 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
507 {
508   struct GNUNET_TESTBED_Controller *c = cls;
509   int status;
510   uint16_t msize;
511
512   c->in_receive = GNUNET_NO;
513   /* FIXME: Add checks for message integrity */
514   if (NULL == msg)
515   {
516     LOG_DEBUG ("Receive timed out or connection to service dropped\n");
517     return;
518   }
519   status = GNUNET_OK;
520   msize = ntohs (msg->size);
521   switch (ntohs (msg->type))
522   {
523   case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
524     GNUNET_assert (msize >= sizeof (struct
525                                     GNUNET_TESTBED_HostConfirmedMessage));
526     status =
527       handle_addhostconfirm (c, (const struct GNUNET_TESTBED_HostConfirmedMessage *) msg);
528     break;
529   case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
530     GNUNET_assert 
531       (msize == sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage));
532     status =
533       handle_opsuccess (c, (const struct
534                             GNUNET_TESTBED_GenericOperationSuccessEventMessage
535                             *) msg);
536     break;
537   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
538     GNUNET_assert (msize == 
539                    sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
540     status =
541       handle_peer_create_success 
542       (c, (const struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *)msg);
543     break;
544   case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
545     GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
546     status =
547       handle_peer_event (c, (const struct GNUNET_TESTBED_PeerEventMessage *) msg);
548     
549     break;
550   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
551     GNUNET_assert (msize >= 
552                    sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage));
553     status = 
554       handle_peer_config 
555       (c, (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
556   msg);
557     break;
558   default:
559     GNUNET_break (0);
560   }
561   if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
562   {
563     c->in_receive = GNUNET_YES;
564     GNUNET_CLIENT_receive (c->client, &message_handler, c,
565                            GNUNET_TIME_UNIT_FOREVER_REL);    
566   }
567 }
568
569
570 /**
571  * Function called to notify a client about the connection begin ready to queue
572  * more data.  "buf" will be NULL and "size" zero if the connection was closed
573  * for writing in the meantime.
574  *
575  * @param cls closure
576  * @param size number of bytes available in buf
577  * @param buf where the callee should write the message
578  * @return number of bytes written to buf
579  */
580 static size_t
581 transmit_ready_notify (void *cls, size_t size, void *buf)
582 {
583   struct GNUNET_TESTBED_Controller *c = cls;
584   struct MessageQueue *mq_entry;
585
586   c->th = NULL;
587   mq_entry = c->mq_head;
588   GNUNET_assert (NULL != mq_entry);
589   if ((0 == size) && (NULL == buf)) /* Timeout */
590   {
591     LOG_DEBUG ("Message sending timed out -- retrying\n");
592     c->th =
593       GNUNET_CLIENT_notify_transmit_ready (c->client,
594                                            ntohs (mq_entry->msg->size),
595                                            TIMEOUT_REL,
596                                            GNUNET_YES, &transmit_ready_notify,
597                                            c);
598     return 0;
599   }
600   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
601   size = ntohs (mq_entry->msg->size);  
602   memcpy (buf, mq_entry->msg, size);
603   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
604              ntohs (mq_entry->msg->type), size);
605   GNUNET_free (mq_entry->msg);
606   GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
607   GNUNET_free (mq_entry);
608   mq_entry = c->mq_head;
609   if (NULL != mq_entry)
610     c->th = 
611       GNUNET_CLIENT_notify_transmit_ready (c->client,
612                                            ntohs (mq_entry->msg->size),
613                                            TIMEOUT_REL,
614                                            GNUNET_YES, &transmit_ready_notify,
615                                            c);
616   if (GNUNET_NO == c->in_receive)
617   {
618     c->in_receive = GNUNET_YES;
619     GNUNET_CLIENT_receive (c->client, &message_handler, c,
620                            GNUNET_TIME_UNIT_FOREVER_REL);
621   }
622   return size;
623 }
624
625
626 /**
627  * Queues a message in send queue for sending to the service
628  *
629  * @param controller the handle to the controller
630  * @param msg the message to queue
631  */
632 void
633 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
634                                struct GNUNET_MessageHeader *msg)
635 {
636   struct MessageQueue *mq_entry;
637   uint16_t type;
638   uint16_t size;
639
640   type = ntohs (msg->type);
641   size = ntohs (msg->size);
642   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
643                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
644   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
645   mq_entry->msg = msg;
646   LOG (GNUNET_ERROR_TYPE_DEBUG,
647        "Queueing message of type %u, size %u for sending\n", type,
648        ntohs (msg->size));
649   GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
650                                     mq_entry);
651   if (NULL == controller->th)
652     controller->th = 
653       GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
654                                            TIMEOUT_REL,
655                                            GNUNET_YES, &transmit_ready_notify,
656                                            controller);
657 }
658
659
660 /**
661  * Handle for controller process
662  */
663 struct GNUNET_TESTBED_ControllerProc
664 {
665   /**
666    * The process handle
667    */
668   struct GNUNET_HELPER_Handle *helper;
669
670   /**
671    * The host where the helper is run
672    */
673   struct GNUNET_TESTBED_Host *host;
674
675   /**
676    * The controller error callback
677    */
678   GNUNET_TESTBED_ControllerStatusCallback cb;
679
680   /**
681    * The closure for the above callback
682    */
683   void *cls;
684
685   /**
686    * The send handle for the helper
687    */
688   struct GNUNET_HELPER_SendHandle *shandle;
689
690   /**
691    * The message corresponding to send handle
692    */
693   struct GNUNET_MessageHeader *msg;
694
695   /**
696    * The port number for ssh; used for helpers starting ssh
697    */
698   char *port;
699
700   /**
701    * The ssh destination string; used for helpers starting ssh
702    */
703   char *dst;
704
705   /**
706    * The configuration of the running testbed service
707    */
708   struct GNUNET_CONFIGURATION_Handle *cfg;
709
710 };
711
712
713 /**
714  * Functions with this signature are called whenever a
715  * complete message is received by the tokenizer.
716  *
717  * Do not call GNUNET_SERVER_mst_destroy in callback
718  *
719  * @param cls closure
720  * @param client identification of the client
721  * @param message the actual message
722  *
723  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
724  */
725 static int helper_mst (void *cls, void *client,
726                        const struct GNUNET_MessageHeader *message)
727 {
728   struct GNUNET_TESTBED_ControllerProc *cp = cls;
729   const struct GNUNET_TESTBED_HelperReply *msg;
730   const char *hostname;
731   char *config;
732   uLongf config_size;
733   uLongf xconfig_size;
734     
735   msg = (const struct GNUNET_TESTBED_HelperReply *) message;
736   GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) 
737                  < ntohs (msg->header.size));
738   GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY 
739                  == ntohs (msg->header.type));
740   config_size = (uLongf) ntohs (msg->config_size);
741   xconfig_size = (uLongf) (ntohs (msg->header.size)
742                            - sizeof (struct GNUNET_TESTBED_HelperReply));
743   config = GNUNET_malloc (config_size);
744   GNUNET_assert (Z_OK == uncompress ((Bytef *) config, &config_size,
745                                      (const Bytef *) &msg[1], xconfig_size));
746   GNUNET_assert (NULL == cp->cfg);
747   cp->cfg = GNUNET_CONFIGURATION_create ();
748   GNUNET_assert (GNUNET_CONFIGURATION_deserialize (cp->cfg, config, 
749                                                    config_size, GNUNET_NO));
750   GNUNET_free (config);
751   if ((NULL == cp->host) || 
752       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
753     hostname = "localhost";
754   /* Change the hostname so that we can connect to it */
755   GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname", 
756                                          hostname);
757   cp->cb (cp->cls, cp->cfg, GNUNET_OK);
758   return GNUNET_OK;
759 }
760
761
762 /**
763  * Continuation function from GNUNET_HELPER_send()
764  * 
765  * @param cls closure
766  * @param result GNUNET_OK on success,
767  *               GNUNET_NO if helper process died
768  *               GNUNET_SYSERR during GNUNET_HELPER_stop
769  */
770 static void 
771 clear_msg (void *cls, int result)
772 {
773   struct GNUNET_TESTBED_ControllerProc *cp = cls;
774   
775   GNUNET_assert (NULL != cp->shandle);
776   cp->shandle = NULL;
777   GNUNET_free (cp->msg);
778 }
779
780
781 /**
782  * Callback that will be called when the helper process dies. This is not called
783  * when the helper process is stoped using GNUNET_HELPER_stop()
784  *
785  * @param cls the closure from GNUNET_HELPER_start()
786  */
787 static void 
788 helper_exp_cb (void *cls)
789 {
790   struct GNUNET_TESTBED_ControllerProc *cp = cls;
791   GNUNET_TESTBED_ControllerStatusCallback cb;
792   void *cb_cls;
793
794   cb = cp->cb;
795   cb_cls = cp->cls;
796   GNUNET_TESTBED_controller_stop (cp);
797   if (NULL != cb)
798     cb (cb_cls, NULL, GNUNET_SYSERR);
799 }
800
801
802 /**
803  * Starts a controller process at the host. FIXME: add controller start callback
804  * with the configuration with which the controller is started
805  *
806  * @param controller_ip the ip address of the controller. Will be set as TRUSTED
807  *          host when starting testbed controller at host
808  * @param host the host where the controller has to be started; NULL for
809  *          localhost
810  * @param cfg template configuration to use for the remote controller; the
811  *          remote controller will be started with a slightly modified
812  *          configuration (port numbers, unix domain sockets and service home
813  *          values are changed as per TESTING library on the remote host)
814  * @param cb function called when the controller is successfully started or
815  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
816  *          called if cb is called with GNUNET_SYSERR as status. Will never be
817  *          called in the same task as 'GNUNET_TESTBED_controller_start'
818  *          (synchronous errors will be signalled by returning NULL). This
819  *          parameter cannot be NULL.
820  * @param cls closure for above callbacks
821  * @return the controller process handle, NULL on errors
822  */
823 struct GNUNET_TESTBED_ControllerProc *
824 GNUNET_TESTBED_controller_start (const char *controller_ip,
825                                  struct GNUNET_TESTBED_Host *host,
826                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
827                                  GNUNET_TESTBED_ControllerStatusCallback cb,
828                                  void *cls)
829 {
830   struct GNUNET_TESTBED_ControllerProc *cp;
831   struct GNUNET_TESTBED_HelperInit *msg;
832   
833   cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
834   if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
835   {
836     char * const binary_argv[] = {
837       "gnunet-testbed-helper", NULL
838     };
839
840     cp->helper = GNUNET_HELPER_start ("gnunet-testbed-helper", binary_argv, 
841                                       &helper_mst, &helper_exp_cb, cp);
842   }
843   else
844   {
845     char *remote_args[6 + 1];
846     unsigned int argp;
847     const char *username;
848     const char *hostname;
849
850     username = GNUNET_TESTBED_host_get_username_ (host);
851     hostname = GNUNET_TESTBED_host_get_hostname_ (host);
852     GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
853     if (NULL == username)
854       GNUNET_asprintf (&cp->dst, "%s", hostname);
855     else 
856       GNUNET_asprintf (&cp->dst, "%s@%s", hostname, username);
857     argp = 0;
858     remote_args[argp++] = "ssh";
859     remote_args[argp++] = "-p";
860     remote_args[argp++] = cp->port;
861     remote_args[argp++] = "-q";
862     remote_args[argp++] = cp->dst;
863     remote_args[argp++] = "gnunet-testbed-helper";
864     remote_args[argp++] = NULL;
865     GNUNET_assert (argp == 6 + 1);
866     cp->helper = GNUNET_HELPER_start ("ssh", remote_args,
867                                       &helper_mst, &helper_exp_cb, cp);
868   }
869   if (NULL == cp->helper)
870   {
871     GNUNET_free_non_null (cp->port);
872     GNUNET_free_non_null (cp->dst);
873     GNUNET_free (cp);
874     return NULL;
875   }
876   cp->host = host;
877   cp->cb = cb;
878   cp->cls = cls;
879   msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
880   cp->msg = &msg->header;
881   cp->shandle = GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO,
882                                     &clear_msg, cp);
883   if (NULL == cp->shandle)
884   {
885     GNUNET_free (msg);
886     GNUNET_TESTBED_controller_stop (cp);
887     return NULL;
888   }
889   return cp;
890 }
891
892
893 /**
894  * Stop the controller process (also will terminate all peers and controllers
895  * dependent on this controller).  This function blocks until the testbed has
896  * been fully terminated (!).
897  *
898  * @param cproc the controller process handle
899  */
900 void
901 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
902 {
903   if (NULL != cproc->shandle)
904     GNUNET_HELPER_send_cancel (cproc->shandle);
905   GNUNET_HELPER_stop (cproc->helper);
906   if (NULL != cproc->cfg)
907     GNUNET_CONFIGURATION_destroy (cproc->cfg);
908   GNUNET_free_non_null (cproc->port);
909   GNUNET_free_non_null (cproc->dst);
910   GNUNET_free (cproc);
911 }
912
913
914 /**
915  * Start a controller process using the given configuration at the
916  * given host.
917  *
918  * @param cfg configuration to use
919  * @param host host to run the controller on; This should be the same host if
920  *          the controller was previously started with
921  *          GNUNET_TESTBED_controller_start; NULL for localhost
922  * @param event_mask bit mask with set of events to call 'cc' for;
923  *                   or-ed values of "1LL" shifted by the
924  *                   respective 'enum GNUNET_TESTBED_EventType'
925  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
926  * @param cc controller callback to invoke on events
927  * @param cc_cls closure for cc
928  * @return handle to the controller
929  */
930 struct GNUNET_TESTBED_Controller *
931 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
932                                    struct GNUNET_TESTBED_Host *host,
933                                    uint64_t event_mask,
934                                    GNUNET_TESTBED_ControllerCallback cc,
935                                    void *cc_cls)
936 {
937   struct GNUNET_TESTBED_Controller *controller;
938   struct GNUNET_TESTBED_InitMessage *msg;
939
940   controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
941   controller->cc = cc;
942   controller->cc_cls = cc_cls;
943   controller->event_mask = event_mask;
944   controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
945   controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);  
946   if (NULL == controller->client)
947   {
948     GNUNET_TESTBED_controller_disconnect (controller);
949     return NULL;
950   }
951   if (NULL == host)
952   {
953     host = GNUNET_TESTBED_host_create_by_id_ (0);
954     if (NULL == host)
955     {
956       LOG (GNUNET_ERROR_TYPE_WARNING,
957            "Treating NULL host as localhost. Multiple references to localhost. "
958            " May break when localhost freed before calling disconnect \n");
959       host = GNUNET_TESTBED_host_lookup_by_id_ (0);
960     }
961     else
962     {
963       controller->aux_host = GNUNET_YES;
964     }
965   }
966   GNUNET_assert (NULL != host);
967   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage));
968   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
969   msg->header.size = htons (sizeof (struct GNUNET_TESTBED_InitMessage));
970   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
971   msg->event_mask = GNUNET_htonll (controller->event_mask);
972   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
973   return controller;
974 }
975
976
977 /**
978  * Configure shared services at a controller.  Using this function,
979  * you can specify that certain services (such as "resolver")
980  * should not be run for each peer but instead be shared
981  * across N peers on the specified host.  This function
982  * must be called before any peers are created at the host.
983  * 
984  * @param controller controller to configure
985  * @param service_name name of the service to share
986  * @param num_peers number of peers that should share one instance
987  *        of the specified service (1 for no sharing is the default),
988  *        use 0 to disable the service
989  */
990 void
991 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller *controller,
992                                              const char *service_name,
993                                              uint32_t num_peers)
994 {
995   struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
996   uint16_t service_name_size;
997   uint16_t msg_size;
998   
999   service_name_size = strlen (service_name) + 1;
1000   msg_size = sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage)
1001     + service_name_size;
1002   msg = GNUNET_malloc (msg_size);
1003   msg->header.size = htons (msg_size);
1004   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1005   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1006   msg->num_peers = htonl (num_peers);
1007   memcpy (&msg[1], service_name, service_name_size);
1008   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1009 }
1010
1011
1012 /**
1013  * disconnects from the controller.
1014  *
1015  * @param controller handle to controller to stop
1016  */
1017 void
1018 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller *controller)
1019 {
1020   struct MessageQueue *mq_entry;
1021
1022   if (NULL != controller->th)
1023     GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1024  /* Clear the message queue */
1025   while (NULL != (mq_entry = controller->mq_head))
1026   {
1027     GNUNET_CONTAINER_DLL_remove (controller->mq_head,
1028                                  controller->mq_tail,
1029                                  mq_entry);
1030     GNUNET_free (mq_entry->msg);
1031     GNUNET_free (mq_entry);
1032   }
1033   if (NULL != controller->client)
1034     GNUNET_CLIENT_disconnect (controller->client);
1035   GNUNET_CONFIGURATION_destroy (controller->cfg);
1036   if (GNUNET_YES == controller->aux_host)
1037     GNUNET_TESTBED_host_destroy (controller->host);
1038   GNUNET_free (controller);
1039 }
1040
1041
1042 /**
1043  * Register a host with the controller
1044  *
1045  * @param controller the controller handle
1046  * @param host the host to register
1047  * @param cc the completion callback to call to inform the status of
1048  *          registration. After calling this callback the registration handle
1049  *          will be invalid. Cannot be NULL.
1050  * @param cc_cls the closure for the cc
1051  * @return handle to the host registration which can be used to cancel the
1052  *           registration 
1053  */
1054 struct GNUNET_TESTBED_HostRegistrationHandle *
1055 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1056                               struct GNUNET_TESTBED_Host *host,
1057                               GNUNET_TESTBED_HostRegistrationCompletion cc,
1058                               void *cc_cls)
1059 {
1060   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1061   struct GNUNET_TESTBED_AddHostMessage *msg;
1062   const char *username;
1063   const char *hostname;
1064   uint16_t msg_size;
1065   uint16_t user_name_length;
1066
1067   if (NULL != controller->rh)
1068     return NULL;
1069   hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1070   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1071   {
1072     LOG (GNUNET_ERROR_TYPE_WARNING,
1073          "Host hostname: %s already registered\n",
1074          (NULL == hostname) ? "localhost" : hostname);
1075     return NULL;
1076   }  
1077   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1078   rh->host = host;
1079   rh->c = controller;
1080   GNUNET_assert (NULL != cc);
1081   rh->cc = cc;
1082   rh->cc_cls = cc_cls;
1083   controller->rh = rh;
1084   username = GNUNET_TESTBED_host_get_username_ (host);
1085   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1086   user_name_length = 0;
1087   if (NULL != username)
1088   {
1089     user_name_length = strlen (username) + 1;
1090     msg_size += user_name_length;
1091   }
1092   /* FIXME: what happens when hostname is NULL? localhost */
1093   GNUNET_assert (NULL != hostname);
1094   msg_size += strlen (hostname) + 1;
1095   msg = GNUNET_malloc (msg_size);
1096   msg->header.size = htons (msg_size);
1097   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1098   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1099   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1100   msg->user_name_length = htons (user_name_length);
1101   if (NULL != username)
1102     memcpy (&msg[1], username, user_name_length);
1103   strcpy (((void *) &msg[1]) + user_name_length, hostname);
1104   GNUNET_TESTBED_queue_message_ (controller, (struct GNUNET_MessageHeader *) msg);
1105   return rh;
1106 }
1107
1108
1109 /**
1110  * Cancel the pending registration. Note that if the registration message is
1111  * already sent to the service the cancellation has only the effect that the
1112  * registration completion callback for the registration is never called.
1113  *
1114  * @param handle the registration handle to cancel
1115  */
1116 void
1117 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1118                                     *handle)
1119 {
1120   if (handle != handle->c->rh)
1121   {
1122     GNUNET_break (0);
1123     return;
1124   }
1125   handle->c->rh = NULL;
1126   GNUNET_free (handle);  
1127 }
1128
1129
1130 /**
1131  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1132  * serialized and compressed
1133  *
1134  * @param master handle to the master controller who creates the association
1135  * @param delegated_host requests to which host should be delegated; cannot be NULL
1136  * @param slave_host which host is used to run the slave controller; use NULL to
1137  *          make the master controller connect to the delegated host
1138  * @param sxcfg serialized and compressed configuration
1139  * @param sxcfg_size the size scfg
1140  * @param scfg_size the size of uncompressed serialized configuration
1141  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1142  *          be started by the master controller; GNUNET_NO if we are just
1143  *          allowed to use the slave via TCP/IP
1144  */
1145 void
1146 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1147                                   struct GNUNET_TESTBED_Host *delegated_host,
1148                                   struct GNUNET_TESTBED_Host *slave_host,
1149                                   const char *sxcfg,
1150                                   size_t sxcfg_size,
1151                                   size_t scfg_size,
1152                                   int is_subordinate)
1153 {
1154   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1155   uint16_t msg_size;
1156
1157   GNUNET_assert (GNUNET_YES == 
1158                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1159   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1160     GNUNET_assert (GNUNET_YES == 
1161                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1162   msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1163   msg = GNUNET_malloc (msg_size);
1164   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);  
1165   msg->header.size = htons (msg_size);
1166   msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1167   msg->slave_host_id = htonl (GNUNET_TESTBED_host_get_id_ 
1168                               ((NULL != slave_host) ? slave_host : master->host));
1169   msg->config_size = htons ((uint16_t) scfg_size);
1170   msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1171   memcpy (&msg[1], sxcfg, sxcfg_size);
1172   GNUNET_TESTBED_queue_message_ (master, (struct GNUNET_MessageHeader *) msg);
1173 }
1174
1175
1176 /**
1177  * Compresses given configuration using zlib compress
1178  *
1179  * @param config the serialized configuration
1180  * @param size the size of config
1181  * @param xconfig will be set to the compressed configuration (memory is fresly
1182  *          allocated) 
1183  * @return the size of the xconfig
1184  */
1185 size_t
1186 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1187                                  char **xconfig)
1188 {
1189   size_t xsize;
1190   
1191   xsize = compressBound ((uLong) size);
1192   *xconfig = GNUNET_malloc (xsize);
1193   GNUNET_assert (Z_OK ==
1194                  compress2 ((Bytef *)* xconfig, (uLongf *) &xsize,
1195                             (const Bytef *) config, (uLongf) size, 
1196                             Z_BEST_SPEED));
1197   return xsize;
1198 }
1199                                 
1200
1201 /**
1202  * Create a link from slave controller to delegated controller. Whenever the
1203  * master controller is asked to start a peer at the delegated controller the
1204  * request will be routed towards slave controller (if a route exists). The
1205  * slave controller will then route it to the delegated controller. The
1206  * configuration of the slave controller is given and to be used to either
1207  * create the slave controller or to connect to an existing slave controller
1208  * process.  'is_subordinate' specifies if the given slave controller should be
1209  * started and managed by the master controller, or if the slave already has a
1210  * master and this is just a secondary master that is also allowed to use the
1211  * existing slave.
1212  *
1213  * @param master handle to the master controller who creates the association
1214  * @param delegated_host requests to which host should be delegated
1215  * @param slave_host which host is used to run the slave controller 
1216  * @param slave_cfg configuration to use for the slave controller
1217  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1218  *                       by the master controller; GNUNET_NO if we are just
1219  *                       allowed to use the slave via TCP/IP
1220  */
1221 void
1222 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1223                                 struct GNUNET_TESTBED_Host *delegated_host,
1224                                 struct GNUNET_TESTBED_Host *slave_host,
1225                                 const struct GNUNET_CONFIGURATION_Handle *slave_cfg,
1226                                 int is_subordinate)
1227 {
1228   char *config;
1229   char *cconfig;
1230   size_t cc_size;
1231   size_t config_size;  
1232   
1233   GNUNET_assert (GNUNET_YES == 
1234                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1235   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1236     GNUNET_assert (GNUNET_YES == 
1237                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1238   config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1239   cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1240   GNUNET_free (config);
1241   GNUNET_assert ((UINT16_MAX -
1242                   sizeof (struct GNUNET_TESTBED_ControllerLinkMessage))
1243                   >= cc_size); /* Configuration doesn't fit in 1 message */
1244   GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1245                                     (const char *) cconfig,
1246                                     cc_size, config_size, is_subordinate);
1247   GNUNET_free (cconfig);
1248 }
1249
1250
1251 /**
1252  * Ask the testbed controller to write the current overlay topology to
1253  * a file.  Naturally, the file will only contain a snapshot as the
1254  * topology may evolve all the time.
1255  *
1256  * @param controller overlay controller to inspect
1257  * @param filename name of the file the topology should
1258  *        be written to.
1259  */
1260 void
1261 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller *controller,
1262                                                const char *filename)
1263 {
1264   GNUNET_break (0);
1265 }
1266
1267
1268 /**
1269  * Creates a helper initialization message. Only for testing.
1270  *
1271  * @param cname the ip address of the controlling host
1272  * @param cfg the configuration that has to used to start the testbed service
1273  *          thru helper
1274  * @return the initialization message
1275  */
1276 struct GNUNET_TESTBED_HelperInit *
1277 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1278                                          const struct GNUNET_CONFIGURATION_Handle *cfg)
1279 {
1280   struct GNUNET_TESTBED_HelperInit *msg;
1281   char *config;
1282   char *xconfig;
1283   size_t config_size;
1284   size_t xconfig_size;
1285   uint16_t cname_len;
1286   uint16_t msg_size;
1287
1288   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1289   GNUNET_assert (NULL != config);
1290   xconfig_size =
1291     GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1292   GNUNET_free (config);
1293   cname_len = strlen (cname);
1294   msg_size = xconfig_size + cname_len + 1 + 
1295     sizeof (struct GNUNET_TESTBED_HelperInit);
1296   msg = GNUNET_realloc (xconfig, msg_size);
1297   (void) memmove ( ((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1298   msg->header.size = htons (msg_size);
1299   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1300   msg->cname_size = htons (cname_len);
1301   msg->config_size = htons (config_size);
1302   (void) strcpy ((char *) &msg[1], cname);
1303   return msg;
1304 }
1305
1306
1307 /**
1308  * Cancel a pending operation.  Releases all resources
1309  * of the operation and will ensure that no event
1310  * is generated for the operation.  Does NOT guarantee
1311  * that the operation will be fully undone (or that
1312  * nothing ever happened).  
1313  * 
1314  * @param operation operation to cancel
1315  */
1316 void
1317 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1318 {
1319   GNUNET_CONTAINER_DLL_remove (operation->controller->op_head,
1320                                operation->controller->op_tail,
1321                                operation);
1322   GNUNET_TESTBED_operation_done (operation);
1323 }
1324
1325
1326 /**
1327  * Signal that the information from an operation has been fully
1328  * processed.  This function MUST be called for each event
1329  * of type 'operation_finished' to fully remove the operation
1330  * from the operation queue.  After calling this function, the
1331  * 'op_result' becomes invalid (!).
1332  * 
1333  * @param operation operation to signal completion for
1334  */
1335 void
1336 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1337 {
1338   switch (operation->type)
1339   {
1340   case OP_PEER_CREATE:
1341     GNUNET_free_non_null (operation->data);
1342     break;
1343   case OP_PEER_DESTROY:
1344     GNUNET_free_non_null (operation->data);
1345     break;
1346   case OP_PEER_START:
1347   case OP_PEER_STOP:
1348     break;
1349   case OP_PEER_INFO:
1350     {
1351       struct PeerInfoData2 *data;
1352       
1353       data = operation->data;
1354       switch (data->pit)
1355       {
1356       case GNUNET_TESTBED_PIT_IDENTITY:
1357         GNUNET_free (data->details.peer_identity);
1358         break;
1359       case GNUNET_TESTBED_PIT_CONFIGURATION:
1360         GNUNET_CONFIGURATION_destroy (data->details.cfg);
1361         break;
1362       case GNUNET_TESTBED_PIT_GENERIC:
1363         GNUNET_assert (0);              /* never reach here */
1364         break;
1365       }
1366     }
1367     GNUNET_free_non_null (operation->data);
1368     break;
1369   case OP_OVERLAY_CONNECT:
1370     GNUNET_free_non_null (operation->data);
1371     break;
1372   }
1373   GNUNET_free (operation);
1374 }
1375
1376
1377 /* end of testbed_api.c */