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