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