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