test case for lateral connections between controllers
[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  * Testbed Helper binary name
72  */
73 #define HELPER_TESTBED_BINARY "gnunet-helper-testbed"
74 #define HELPER_TESTBED_BINARY_SSH ". ~/.bashrc; gnunet-helper-testbed"
75
76
77 /**
78  * The message queue for sending messages to the controller service
79  */
80 struct MessageQueue
81 {
82   /**
83    * The message to be sent
84    */
85   struct GNUNET_MessageHeader *msg;
86
87   /**
88    * next pointer for DLL
89    */
90   struct MessageQueue *next;
91
92   /**
93    * prev pointer for DLL
94    */
95   struct MessageQueue *prev;
96 };
97
98
99 /**
100  * Structure for a controller link
101  */
102 struct ControllerLink
103 {
104   /**
105    * The next ptr for DLL
106    */
107   struct ControllerLink *next;
108
109   /**
110    * The prev ptr for DLL
111    */
112   struct ControllerLink *prev;
113
114   /**
115    * The host which will be referred in the peer start request. This is the
116    * host where the peer should be started
117    */
118   struct GNUNET_TESTBED_Host *delegated_host;
119
120   /**
121    * The host which will contacted to delegate the peer start request
122    */
123   struct GNUNET_TESTBED_Host *slave_host;
124
125   /**
126    * The configuration to be used to connect to slave host
127    */
128   const struct GNUNET_CONFIGURATION_Handle *slave_cfg;
129
130   /**
131    * GNUNET_YES if the slave should be started (and stopped) by us; GNUNET_NO
132    * if we are just allowed to use the slave via TCP/IP
133    */
134   int is_subordinate;
135 };
136
137
138 /**
139  * handle for host registration
140  */
141 struct GNUNET_TESTBED_HostRegistrationHandle
142 {
143   /**
144    * The host being registered
145    */
146   struct GNUNET_TESTBED_Host *host;
147
148   /**
149    * The controller at which this host is being registered
150    */
151   struct GNUNET_TESTBED_Controller *c;
152
153   /**
154    * The Registartion completion callback
155    */
156   GNUNET_TESTBED_HostRegistrationCompletion cc;
157
158   /**
159    * The closure for above callback
160    */
161   void *cc_cls;
162 };
163
164
165 /**
166  * Context data for forwarded Operation
167  */
168 struct ForwardedOperationData
169 {
170
171   /**
172    * The callback to call when reply is available
173    */
174   GNUNET_CLIENT_MessageHandler cc;
175
176   /**
177    * The closure for the above callback
178    */
179   void *cc_cls;
180
181 };
182
183
184 /**
185  * Context data for get slave config operations
186  */
187 struct GetSlaveConfigData
188 {
189   /**
190    * The operation closure
191    */
192   void *op_cls;
193
194   /**
195    * The id of the slave controller
196    */
197   uint32_t slave_id;
198
199 };
200
201
202 /**
203  * Returns the operation context with the given id if found in the Operation
204  * context queues of the controller
205  *
206  * @param c the controller whose queues are searched
207  * @param id the id which has to be checked
208  * @return the matching operation context; NULL if no match found
209  */
210 static struct OperationContext *
211 find_opc (const struct GNUNET_TESTBED_Controller *c, const uint64_t id)
212 {
213   struct OperationContext *opc;
214
215   for (opc = c->ocq_head; NULL != opc; opc = opc->next)
216   {
217     if (id == opc->id)
218       return opc;
219   }
220   return NULL;
221 }
222
223
224 /**
225  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
226  * controller (testbed service)
227  *
228  * @param c the controller handler
229  * @param msg message received
230  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
231  *           not
232  */
233 static int
234 handle_addhostconfirm (struct GNUNET_TESTBED_Controller *c,
235                        const struct GNUNET_TESTBED_HostConfirmedMessage *msg)
236 {
237   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
238   char *emsg;
239   uint16_t msg_size;
240
241   rh = c->rh;
242   if (NULL == rh)
243   {
244     return GNUNET_OK;
245   }
246   if (GNUNET_TESTBED_host_get_id_ (rh->host) != ntohl (msg->host_id))
247   {
248     LOG_DEBUG ("Mismatch in host id's %u, %u of host confirm msg\n",
249                GNUNET_TESTBED_host_get_id_ (rh->host), ntohl (msg->host_id));
250     return GNUNET_OK;
251   }
252   c->rh = NULL;
253   msg_size = ntohs (msg->header.size);
254   if (sizeof (struct GNUNET_TESTBED_HostConfirmedMessage) == msg_size)
255   {
256     LOG_DEBUG ("Host %u successfully registered\n", ntohl (msg->host_id));
257     GNUNET_TESTBED_mark_host_registered_at_ (rh->host, c);
258     rh->cc (rh->cc_cls, NULL);
259     GNUNET_free (rh);
260     return GNUNET_OK;
261   }
262   /* We have an error message */
263   emsg = (char *) &msg[1];
264   if ('\0' !=
265       emsg[msg_size - sizeof (struct GNUNET_TESTBED_HostConfirmedMessage)])
266   {
267     GNUNET_break (0);
268     GNUNET_free (rh);
269     return GNUNET_NO;
270   }
271   LOG (GNUNET_ERROR_TYPE_ERROR, _("Adding host %u failed with error: %s\n"),
272        ntohl (msg->host_id), emsg);
273   rh->cc (rh->cc_cls, emsg);
274   GNUNET_free (rh);
275   return GNUNET_OK;
276 }
277
278
279 /**
280  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM message from
281  * controller (testbed service)
282  *
283  * @param c the controller handler
284  * @param msg message received
285  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
286  *           not
287  */
288 static int
289 handle_opsuccess (struct GNUNET_TESTBED_Controller *c,
290                   const struct
291                   GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg)
292 {
293   struct OperationContext *opc;
294   struct GNUNET_TESTBED_EventInformation event;
295   uint64_t op_id;
296
297   op_id = GNUNET_ntohll (msg->operation_id);
298   LOG_DEBUG ("Operation %ul successful\n", op_id);
299   if (NULL == (opc = find_opc (c, op_id)))
300   {
301     LOG_DEBUG ("Operation not found\n");
302     return GNUNET_YES;
303   }
304   switch (opc->type)
305   {
306   case OP_FORWARDED:
307     {
308       struct ForwardedOperationData *fo_data;
309       
310       fo_data = opc->data;
311       if (NULL != fo_data->cc)
312         fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
313       GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
314       GNUNET_free (fo_data);
315       GNUNET_free (opc);
316       return GNUNET_YES;
317     }
318     break;
319   case OP_PEER_DESTROY:
320     {
321       struct GNUNET_TESTBED_Peer *peer;
322       
323       peer = opc->data;
324       GNUNET_free (peer);
325       opc->data = NULL;
326       //PEERDESTROYDATA
327     }
328     break;
329   case OP_LINK_CONTROLLERS:
330     break;
331   default:
332     GNUNET_assert (0);
333   }
334   event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
335   event.details.operation_finished.operation = opc->op;
336   event.details.operation_finished.op_cls = NULL;
337   event.details.operation_finished.emsg = NULL;
338   event.details.operation_finished.generic = NULL;
339   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
340   opc->state = OPC_STATE_FINISHED;
341   if (0 != (c->event_mask & (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED)))
342   {
343     if (NULL != c->cc)
344       c->cc (c->cc_cls, &event);
345   }
346   return GNUNET_YES;
347 }
348
349
350 /**
351  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS message from
352  * controller (testbed service)
353  *
354  * @param c the controller handler
355  * @param msg message received
356  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
357  *           not
358  */
359 static int
360 handle_peer_create_success (struct GNUNET_TESTBED_Controller *c,
361                             const struct
362                             GNUNET_TESTBED_PeerCreateSuccessEventMessage *msg)
363 {
364   struct OperationContext *opc;
365   struct PeerCreateData *data;
366   struct GNUNET_TESTBED_Peer *peer;
367   GNUNET_TESTBED_PeerCreateCallback cb;
368   void *cls;
369   uint64_t op_id;
370
371   GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage) ==
372                  ntohs (msg->header.size));
373   op_id = GNUNET_ntohll (msg->operation_id);
374   if (NULL == (opc = find_opc (c, op_id)))
375   {
376     LOG_DEBUG ("Operation context for PeerCreateSuccessEvent not found\n");
377     return GNUNET_YES;
378   }
379   if (OP_FORWARDED == opc->type)
380   {
381     struct ForwardedOperationData *fo_data;
382
383     fo_data = opc->data;
384     if (NULL != fo_data->cc)
385       fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
386     GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
387     GNUNET_free (fo_data);
388     GNUNET_free (opc);
389     return GNUNET_YES;
390   }
391   GNUNET_assert (OP_PEER_CREATE == opc->type);
392   GNUNET_assert (NULL != opc->data);
393   data = opc->data;
394   GNUNET_assert (NULL != data->peer);
395   peer = data->peer;
396   GNUNET_assert (peer->unique_id == ntohl (msg->peer_id));
397   peer->state = PS_CREATED;
398   cb = data->cb;
399   cls = data->cls;
400   GNUNET_free (opc->data);
401   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
402   opc->state = OPC_STATE_FINISHED;
403   if (NULL != cb)
404     cb (cls, peer, NULL);
405   return GNUNET_YES;
406 }
407
408
409 /**
410  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT message from
411  * controller (testbed service)
412  *
413  * @param c the controller handler
414  * @param msg message received
415  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
416  *           not
417  */
418 static int
419 handle_peer_event (struct GNUNET_TESTBED_Controller *c,
420                    const struct GNUNET_TESTBED_PeerEventMessage *msg)
421 {
422   struct OperationContext *opc;
423   struct GNUNET_TESTBED_Peer *peer;
424   struct PeerEventData *data;
425   GNUNET_TESTBED_PeerChurnCallback pcc;
426   void *pcc_cls;
427   struct GNUNET_TESTBED_EventInformation event;
428   uint64_t op_id;
429
430   GNUNET_assert (sizeof (struct GNUNET_TESTBED_PeerEventMessage) ==
431                  ntohs (msg->header.size));
432   op_id = GNUNET_ntohll (msg->operation_id);
433   if (NULL == (opc = find_opc (c, op_id)))
434   {
435     LOG_DEBUG ("Operation not found\n");
436     return GNUNET_YES;
437   }
438   if (OP_FORWARDED == opc->type)
439   {
440     struct ForwardedOperationData *fo_data;
441
442     fo_data = opc->data;
443     if (NULL != fo_data->cc)
444       fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
445     GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
446     GNUNET_free (fo_data);
447     GNUNET_free (opc);
448     return GNUNET_YES;
449   }
450   GNUNET_assert ((OP_PEER_START == opc->type) || (OP_PEER_STOP == opc->type));
451   data = opc->data;
452   GNUNET_assert (NULL != data);
453   peer = data->peer;
454   GNUNET_assert (NULL != peer);
455   event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
456   switch (event.type)
457   {
458   case GNUNET_TESTBED_ET_PEER_START:
459     peer->state = PS_STARTED;
460     event.details.peer_start.host = peer->host;
461     event.details.peer_start.peer = peer;
462     break;
463   case GNUNET_TESTBED_ET_PEER_STOP:
464     peer->state = PS_STOPPED;
465     event.details.peer_stop.peer = peer;
466     break;
467   default:
468     GNUNET_assert (0);          /* We should never reach this state */
469   }
470   pcc = data->pcc;
471   pcc_cls = data->pcc_cls;
472   GNUNET_free (data);
473   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
474   opc->state = OPC_STATE_FINISHED;
475   if (0 !=
476       ((GNUNET_TESTBED_ET_PEER_START | GNUNET_TESTBED_ET_PEER_STOP) &
477        c->event_mask))
478   {
479     if (NULL != c->cc)
480       c->cc (c->cc_cls, &event);
481   }
482   if (NULL != pcc)
483     pcc (pcc_cls, NULL);
484   return GNUNET_YES;
485 }
486
487
488 /**
489  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT message from
490  * controller (testbed service)
491  *
492  * @param c the controller handler
493  * @param msg message received
494  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
495  *           not
496  */
497 static int
498 handle_peer_conevent (struct GNUNET_TESTBED_Controller *c,
499                       const struct GNUNET_TESTBED_ConnectionEventMessage *msg)
500 {
501   struct OperationContext *opc;
502   struct OverlayConnectData *data;
503   GNUNET_TESTBED_OperationCompletionCallback cb;
504   void *cb_cls;
505   struct GNUNET_TESTBED_EventInformation event;
506   uint64_t op_id;
507
508   op_id = GNUNET_ntohll (msg->operation_id);
509   if (NULL == (opc = find_opc (c, op_id)))
510   {
511     LOG_DEBUG ("Operation not found\n");
512     return GNUNET_YES;
513   }
514   data = opc->data;
515   GNUNET_assert (NULL != data);
516   GNUNET_assert ((ntohl (msg->peer1) == data->p1->unique_id) &&
517                  (ntohl (msg->peer2) == data->p2->unique_id));
518   event.type = (enum GNUNET_TESTBED_EventType) ntohl (msg->event_type);
519   switch (event.type)
520   {
521   case GNUNET_TESTBED_ET_CONNECT:
522     event.details.peer_connect.peer1 = data->p1;
523     event.details.peer_connect.peer2 = data->p2;
524     break;
525   case GNUNET_TESTBED_ET_DISCONNECT:
526     GNUNET_assert (0);          /* FIXME: implement */
527     break;
528   default:
529     GNUNET_assert (0);          /* Should never reach here */
530     break;
531   }
532   cb = data->cb;
533   cb_cls = data->cb_cls;
534   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
535   opc->state = OPC_STATE_FINISHED;
536   GNUNET_free (data);
537   if (0 !=
538       ((GNUNET_TESTBED_ET_CONNECT | GNUNET_TESTBED_ET_DISCONNECT) &
539        c->event_mask))
540   {
541     if (NULL != c->cc)
542       c->cc (c->cc_cls, &event);
543   }
544   if (NULL != cb)
545     cb (cb_cls, opc->op, NULL);
546   return GNUNET_YES;
547 }
548
549
550 /**
551  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG message from
552  * controller (testbed service)
553  *
554  * @param c the controller handler
555  * @param msg message received
556  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
557  *           not
558  */
559 static int
560 handle_peer_config (struct GNUNET_TESTBED_Controller *c,
561                     const struct
562                     GNUNET_TESTBED_PeerConfigurationInformationMessage *msg)
563 {
564   struct OperationContext *opc;
565   struct GNUNET_TESTBED_Peer *peer;
566   struct PeerInfoData *data;
567   struct GNUNET_TESTBED_PeerInformation *pinfo;
568   GNUNET_TESTBED_PeerInfoCallback cb;
569   void *cb_cls;
570   uint64_t op_id;
571
572   op_id = GNUNET_ntohll (msg->operation_id);
573   if (NULL == (opc = find_opc (c, op_id)))
574   {
575     LOG_DEBUG ("Operation not found\n");
576     return GNUNET_YES;
577   }
578   if (OP_FORWARDED == opc->type)
579   {
580     struct ForwardedOperationData *fo_data;
581
582     fo_data = opc->data;
583     if (NULL != fo_data->cc)
584       fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
585     GNUNET_CONTAINER_DLL_remove (c->ocq_head, c->ocq_tail, opc);
586     GNUNET_free (fo_data);
587     GNUNET_free (opc);
588     return GNUNET_YES;
589   }
590   data = opc->data;
591   GNUNET_assert (NULL != data);
592   peer = data->peer;
593   GNUNET_assert (NULL != peer);
594   GNUNET_assert (ntohl (msg->peer_id) == peer->unique_id);
595   pinfo = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerInformation));
596   pinfo->pit = data->pit;
597   cb = data->cb;
598   cb_cls = data->cb_cls;
599   GNUNET_free (data);
600   opc->data = NULL;
601   switch (pinfo->pit)
602   {
603   case GNUNET_TESTBED_PIT_IDENTITY:
604     pinfo->result.id = GNUNET_malloc (sizeof (struct GNUNET_PeerIdentity));
605     (void) memcpy (pinfo->result.id, &msg->peer_identity,
606                    sizeof (struct GNUNET_PeerIdentity));
607     break;
608   case GNUNET_TESTBED_PIT_CONFIGURATION:
609     pinfo->result.cfg =        /* Freed in oprelease_peer_getinfo */
610         GNUNET_TESTBED_extract_config_ (&msg->header);
611     break;
612   case GNUNET_TESTBED_PIT_GENERIC:
613     GNUNET_assert (0);          /* never reach here */
614     break;
615   }
616   opc->data = pinfo;
617   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
618   opc->state = OPC_STATE_FINISHED;
619   if (NULL != cb)
620     cb (cb_cls, opc->op, pinfo, NULL);
621   return GNUNET_YES;
622 }
623
624
625 /**
626  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT message from
627  * controller (testbed service)
628  *
629  * @param c the controller handler
630  * @param msg message received
631  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
632  *           not
633  */
634 static int
635 handle_op_fail_event (struct GNUNET_TESTBED_Controller *c,
636                       const struct GNUNET_TESTBED_OperationFailureEventMessage
637                       *msg)
638 {
639   struct OperationContext *opc;
640   const char *emsg;
641   uint64_t op_id;
642   struct GNUNET_TESTBED_EventInformation event;
643
644   op_id = GNUNET_ntohll (msg->operation_id);
645   if (NULL == (opc = find_opc (c, op_id)))
646   {
647     LOG_DEBUG ("Operation not found\n");
648     return GNUNET_YES;
649   }
650   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
651   if (OP_FORWARDED == opc->type)
652   {
653     struct ForwardedOperationData *fo_data;
654
655     fo_data = opc->data;
656     if (NULL != fo_data->cc)
657       fo_data->cc (fo_data->cc_cls, (const struct GNUNET_MessageHeader *) msg);
658     GNUNET_free (fo_data);
659     GNUNET_free (opc);
660     return GNUNET_YES;
661   }
662   opc->state = OPC_STATE_FINISHED;
663   emsg = GNUNET_TESTBED_parse_error_string_ (msg);
664   if (NULL == emsg)
665     emsg = "Unknown error";
666   if (OP_PEER_INFO == opc->type)
667   {
668     struct PeerInfoData *data;
669     data = opc->data;
670     if (NULL != data->cb)
671       data->cb (data->cb_cls, opc->op, NULL, emsg);
672     GNUNET_free (data);
673     return GNUNET_YES;  /* We do not call controller callback for peer info */
674   }
675   if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
676       (NULL != c->cc))
677   {
678     event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;
679     event.details.operation_finished.operation = opc->op;
680     event.details.operation_finished.op_cls = NULL;
681     event.details.operation_finished.emsg = emsg;
682     event.details.operation_finished.generic = NULL;
683     c->cc (c->cc_cls, &event);
684   }
685   switch (opc->type)
686   {
687   case OP_PEER_CREATE:
688     {
689       struct PeerCreateData *data;      
690       data = opc->data;
691       GNUNET_free (data->peer);
692       if (NULL != data->cb)
693         data->cb (data->cls, NULL, emsg);
694       GNUNET_free (data);      
695     }
696     break;
697   case OP_PEER_START:
698   case OP_PEER_STOP:
699     {
700       struct PeerEventData *data;
701       data = opc->data;
702       if (NULL != data->pcc)
703         data->pcc (data->pcc_cls, emsg);
704       GNUNET_free (data);
705     }
706     break;
707   case OP_PEER_DESTROY:
708     break;
709   case OP_PEER_INFO:
710     GNUNET_assert (0);
711   case OP_OVERLAY_CONNECT:
712     {
713       struct OverlayConnectData *data;
714       data = opc->data;
715       if (NULL != data->cb)
716         data->cb (data->cb_cls, opc->op, emsg);
717       GNUNET_free (data);
718     }
719     break;
720   case OP_FORWARDED:
721     GNUNET_assert (0);
722   case OP_LINK_CONTROLLERS:     /* No secondary callback */
723     break;
724   default:
725     GNUNET_break (0);
726   }  
727   return GNUNET_YES;
728 }
729
730
731 /**
732  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG message from controller
733  * (testbed service)
734  *
735  * @param c the controller handler
736  * @param msg message received
737  * @return GNUNET_YES if we can continue receiving from service; GNUNET_NO if
738  *           not
739  */
740 static int
741 handle_slave_config (struct GNUNET_TESTBED_Controller *c,
742                      const struct GNUNET_TESTBED_SlaveConfiguration * msg)
743 {
744   struct OperationContext *opc;
745   void *op_cls;
746   uint64_t op_id;
747   struct GNUNET_TESTBED_EventInformation event;  
748
749   op_id = GNUNET_ntohll (msg->operation_id);
750   if (NULL == (opc = find_opc (c, op_id)))
751   {
752     LOG_DEBUG ("Operation not found\n");
753     return GNUNET_YES;
754   }
755   if (OP_GET_SLAVE_CONFIG != opc->type)
756   {
757     GNUNET_break (0);
758     return GNUNET_YES;
759   }  
760   op_cls = ((struct GetSlaveConfigData *) opc->data)->op_cls;
761   GNUNET_free (opc->data);
762   opc->data = NULL;
763   opc->state = OPC_STATE_FINISHED;
764   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
765   if ((0 != (GNUNET_TESTBED_ET_OPERATION_FINISHED & c->event_mask)) &&
766       (NULL != c->cc))
767   {
768     opc->data = GNUNET_TESTBED_extract_config_ (&msg->header);
769     event.type = GNUNET_TESTBED_ET_OPERATION_FINISHED;   
770     event.details.operation_finished.generic = opc->data;
771     event.details.operation_finished.operation = opc->op;
772     event.details.operation_finished.op_cls = op_cls;
773     event.details.operation_finished.emsg = NULL;
774     c->cc (c->cc_cls, &event);
775   }
776   return GNUNET_YES;
777 }
778
779
780 /**
781  * Handler for messages from controller (testbed service)
782  *
783  * @param cls the controller handler
784  * @param msg message received, NULL on timeout or fatal error
785  */
786 static void
787 message_handler (void *cls, const struct GNUNET_MessageHeader *msg)
788 {
789   struct GNUNET_TESTBED_Controller *c = cls;
790   int status;
791   uint16_t msize;
792
793   c->in_receive = GNUNET_NO;
794   /* FIXME: Add checks for message integrity */
795   if (NULL == msg)
796   {
797     LOG_DEBUG ("Receive timed out or connection to service dropped\n");
798     return;
799   }
800   status = GNUNET_OK;
801   msize = ntohs (msg->size);
802   switch (ntohs (msg->type))
803   {
804   case GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM:
805     GNUNET_assert (msize >=
806                    sizeof (struct GNUNET_TESTBED_HostConfirmedMessage));
807     status =
808         handle_addhostconfirm (c,
809                                (const struct GNUNET_TESTBED_HostConfirmedMessage
810                                 *) msg);
811     break;
812   case GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS:
813     GNUNET_assert (msize ==
814                    sizeof (struct
815                            GNUNET_TESTBED_GenericOperationSuccessEventMessage));
816     status =
817         handle_opsuccess (c,
818                           (const struct
819                            GNUNET_TESTBED_GenericOperationSuccessEventMessage *)
820                           msg);
821     break;
822   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS:
823     GNUNET_assert (msize ==
824                    sizeof (struct
825                            GNUNET_TESTBED_PeerCreateSuccessEventMessage));
826     status =
827         handle_peer_create_success (c,
828                                     (const struct
829                                      GNUNET_TESTBED_PeerCreateSuccessEventMessage
830                                      *) msg);
831     break;
832   case GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT:
833     GNUNET_assert (msize == sizeof (struct GNUNET_TESTBED_PeerEventMessage));
834     status =
835         handle_peer_event (c,
836                            (const struct GNUNET_TESTBED_PeerEventMessage *)
837                            msg);
838
839     break;
840   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
841     GNUNET_assert (msize >=
842                    sizeof (struct
843                            GNUNET_TESTBED_PeerConfigurationInformationMessage));
844     status =
845         handle_peer_config (c,
846                             (const struct
847                              GNUNET_TESTBED_PeerConfigurationInformationMessage
848                              *) msg);
849     break;
850   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT:
851     GNUNET_assert (msize ==
852                    sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
853     status =
854         handle_peer_conevent (c,
855                               (const struct
856                                GNUNET_TESTBED_ConnectionEventMessage *) msg);
857     break;
858   case GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONFAILEVENT:
859     GNUNET_assert (msize >=
860                    sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage));
861     status =
862         handle_op_fail_event (c,
863                               (const struct
864                                GNUNET_TESTBED_OperationFailureEventMessage *)
865                               msg);
866     break;
867   case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
868     GNUNET_assert (msize >
869                    sizeof (struct GNUNET_TESTBED_SlaveConfiguration));
870     status = 
871         handle_slave_config (c, (const struct 
872                                  GNUNET_TESTBED_SlaveConfiguration *) msg);
873     break;
874   default:
875     GNUNET_assert (0);
876   }
877   if ((GNUNET_OK == status) && (GNUNET_NO == c->in_receive))
878   {
879     c->in_receive = GNUNET_YES;
880     GNUNET_CLIENT_receive (c->client, &message_handler, c,
881                            GNUNET_TIME_UNIT_FOREVER_REL);
882   }
883 }
884
885
886 /**
887  * Function called to notify a client about the connection begin ready to queue
888  * more data.  "buf" will be NULL and "size" zero if the connection was closed
889  * for writing in the meantime.
890  *
891  * @param cls closure
892  * @param size number of bytes available in buf
893  * @param buf where the callee should write the message
894  * @return number of bytes written to buf
895  */
896 static size_t
897 transmit_ready_notify (void *cls, size_t size, void *buf)
898 {
899   struct GNUNET_TESTBED_Controller *c = cls;
900   struct MessageQueue *mq_entry;
901
902   c->th = NULL;
903   mq_entry = c->mq_head;
904   GNUNET_assert (NULL != mq_entry);
905   if ((0 == size) && (NULL == buf))     /* Timeout */
906   {
907     LOG_DEBUG ("Message sending timed out -- retrying\n");
908     c->th =
909         GNUNET_CLIENT_notify_transmit_ready (c->client,
910                                              ntohs (mq_entry->msg->size),
911                                              TIMEOUT_REL, GNUNET_YES,
912                                              &transmit_ready_notify, c);
913     return 0;
914   }
915   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
916   size = ntohs (mq_entry->msg->size);
917   memcpy (buf, mq_entry->msg, size);
918   LOG_DEBUG ("Message of type: %u and size: %u sent\n",
919              ntohs (mq_entry->msg->type), size);
920   GNUNET_free (mq_entry->msg);
921   GNUNET_CONTAINER_DLL_remove (c->mq_head, c->mq_tail, mq_entry);
922   GNUNET_free (mq_entry);
923   mq_entry = c->mq_head;
924   if (NULL != mq_entry)
925     c->th =
926         GNUNET_CLIENT_notify_transmit_ready (c->client,
927                                              ntohs (mq_entry->msg->size),
928                                              TIMEOUT_REL, GNUNET_YES,
929                                              &transmit_ready_notify, c);
930   if (GNUNET_NO == c->in_receive)
931   {
932     c->in_receive = GNUNET_YES;
933     GNUNET_CLIENT_receive (c->client, &message_handler, c,
934                            GNUNET_TIME_UNIT_FOREVER_REL);
935   }
936   return size;
937 }
938
939
940 /**
941  * Queues a message in send queue for sending to the service
942  *
943  * @param controller the handle to the controller
944  * @param msg the message to queue
945  */
946 void
947 GNUNET_TESTBED_queue_message_ (struct GNUNET_TESTBED_Controller *controller,
948                                struct GNUNET_MessageHeader *msg)
949 {
950   struct MessageQueue *mq_entry;
951   uint16_t type;
952   uint16_t size;
953
954   type = ntohs (msg->type);
955   size = ntohs (msg->size);
956   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
957                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
958   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
959   mq_entry->msg = msg;
960   LOG (GNUNET_ERROR_TYPE_DEBUG,
961        "Queueing message of type %u, size %u for sending\n", type,
962        ntohs (msg->size));
963   GNUNET_CONTAINER_DLL_insert_tail (controller->mq_head, controller->mq_tail,
964                                     mq_entry);
965   if (NULL == controller->th)
966     controller->th =
967         GNUNET_CLIENT_notify_transmit_ready (controller->client, size,
968                                              TIMEOUT_REL, GNUNET_YES,
969                                              &transmit_ready_notify,
970                                              controller);
971 }
972
973
974 /**
975  * Sends the given message as an operation. The given callback is called when a
976  * reply for the operation is available.  Call
977  * GNUNET_TESTBED_forward_operation_msg_cancel_() to cleanup the returned
978  * operation context if the cc hasn't been called
979  *
980  * @param controller the controller to which the message has to be sent
981  * @param operation_id the operation id of the message
982  * @param msg the message to send
983  * @param cc the callback to call when reply is available
984  * @param cc_cls the closure for the above callback
985  * @return the operation context which can be used to cancel the forwarded
986  *           operation
987  */
988 struct OperationContext *
989 GNUNET_TESTBED_forward_operation_msg_ (struct GNUNET_TESTBED_Controller
990                                        *controller, uint64_t operation_id,
991                                        const struct GNUNET_MessageHeader *msg,
992                                        GNUNET_CLIENT_MessageHandler cc,
993                                        void *cc_cls)
994 {
995   struct OperationContext *opc;
996   struct ForwardedOperationData *data;
997   struct GNUNET_MessageHeader *dup_msg;
998   uint16_t msize;
999
1000   data = GNUNET_malloc (sizeof (struct ForwardedOperationData));
1001   data->cc = cc;
1002   data->cc_cls = cc_cls;
1003   opc = GNUNET_malloc (sizeof (struct OperationContext));
1004   opc->c = controller;
1005   opc->type = OP_FORWARDED;
1006   opc->data = data;
1007   opc->id = operation_id;
1008   msize = ntohs (msg->size);
1009   dup_msg = GNUNET_malloc (msize);
1010   (void) memcpy (dup_msg, msg, msize);
1011   GNUNET_TESTBED_queue_message_ (opc->c, dup_msg);
1012   GNUNET_CONTAINER_DLL_insert_tail (controller->ocq_head, controller->ocq_tail,
1013                                     opc);
1014   return opc;
1015 }
1016
1017
1018 /**
1019  * Function to cancel an operation created by simply forwarding an operation
1020  * message.
1021  *
1022  * @param opc the operation context from GNUNET_TESTBED_forward_operation_msg_()
1023  */
1024 void
1025 GNUNET_TESTBED_forward_operation_msg_cancel_ (struct OperationContext *opc)
1026 {
1027   GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1028   GNUNET_free (opc->data);
1029   GNUNET_free (opc);
1030 }
1031
1032
1033 /**
1034  * Handle for controller process
1035  */
1036 struct GNUNET_TESTBED_ControllerProc
1037 {
1038   /**
1039    * The process handle
1040    */
1041   struct GNUNET_HELPER_Handle *helper;
1042
1043   /**
1044    * The host where the helper is run
1045    */
1046   struct GNUNET_TESTBED_Host *host;
1047
1048   /**
1049    * The controller error callback
1050    */
1051   GNUNET_TESTBED_ControllerStatusCallback cb;
1052
1053   /**
1054    * The closure for the above callback
1055    */
1056   void *cls;
1057
1058   /**
1059    * The send handle for the helper
1060    */
1061   struct GNUNET_HELPER_SendHandle *shandle;
1062
1063   /**
1064    * The message corresponding to send handle
1065    */
1066   struct GNUNET_MessageHeader *msg;
1067
1068   /**
1069    * The port number for ssh; used for helpers starting ssh
1070    */
1071   char *port;
1072
1073   /**
1074    * The ssh destination string; used for helpers starting ssh
1075    */
1076   char *dst;
1077
1078   /**
1079    * The configuration of the running testbed service
1080    */
1081   struct GNUNET_CONFIGURATION_Handle *cfg;
1082
1083 };
1084
1085
1086 /**
1087  * Functions with this signature are called whenever a
1088  * complete message is received by the tokenizer.
1089  *
1090  * Do not call GNUNET_SERVER_mst_destroy in callback
1091  *
1092  * @param cls closure
1093  * @param client identification of the client
1094  * @param message the actual message
1095  *
1096  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1097  */
1098 static int
1099 helper_mst (void *cls, void *client, const struct GNUNET_MessageHeader *message)
1100 {
1101   struct GNUNET_TESTBED_ControllerProc *cp = cls;
1102   const struct GNUNET_TESTBED_HelperReply *msg;
1103   const char *hostname;
1104   char *config;
1105   uLongf config_size;
1106   uLongf xconfig_size;
1107
1108   msg = (const struct GNUNET_TESTBED_HelperReply *) message;
1109   GNUNET_assert (sizeof (struct GNUNET_TESTBED_HelperReply) <
1110                  ntohs (msg->header.size));
1111   GNUNET_assert (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_REPLY ==
1112                  ntohs (msg->header.type));
1113   config_size = (uLongf) ntohs (msg->config_size);
1114   xconfig_size =
1115       (uLongf) (ntohs (msg->header.size) -
1116                 sizeof (struct GNUNET_TESTBED_HelperReply));
1117   config = GNUNET_malloc (config_size);
1118   GNUNET_assert (Z_OK ==
1119                  uncompress ((Bytef *) config, &config_size,
1120                              (const Bytef *) &msg[1], xconfig_size));
1121   GNUNET_assert (NULL == cp->cfg);
1122   cp->cfg = GNUNET_CONFIGURATION_create ();
1123   GNUNET_assert (GNUNET_CONFIGURATION_deserialize
1124                  (cp->cfg, config, config_size, GNUNET_NO));
1125   GNUNET_free (config);
1126   if ((NULL == cp->host) ||
1127       (NULL == (hostname = GNUNET_TESTBED_host_get_hostname_ (cp->host))))
1128     hostname = "localhost";
1129   /* Change the hostname so that we can connect to it */
1130   GNUNET_CONFIGURATION_set_value_string (cp->cfg, "testbed", "hostname",
1131                                          hostname);
1132   cp->cb (cp->cls, cp->cfg, GNUNET_OK);
1133   return GNUNET_OK;
1134 }
1135
1136
1137 /**
1138  * Continuation function from GNUNET_HELPER_send()
1139  *
1140  * @param cls closure
1141  * @param result GNUNET_OK on success,
1142  *               GNUNET_NO if helper process died
1143  *               GNUNET_SYSERR during GNUNET_HELPER_stop
1144  */
1145 static void
1146 clear_msg (void *cls, int result)
1147 {
1148   struct GNUNET_TESTBED_ControllerProc *cp = cls;
1149
1150   GNUNET_assert (NULL != cp->shandle);
1151   cp->shandle = NULL;
1152   GNUNET_free (cp->msg);
1153 }
1154
1155
1156 /**
1157  * Callback that will be called when the helper process dies. This is not called
1158  * when the helper process is stoped using GNUNET_HELPER_stop()
1159  *
1160  * @param cls the closure from GNUNET_HELPER_start()
1161  */
1162 static void
1163 helper_exp_cb (void *cls)
1164 {
1165   struct GNUNET_TESTBED_ControllerProc *cp = cls;
1166   GNUNET_TESTBED_ControllerStatusCallback cb;
1167   void *cb_cls;
1168
1169   cb = cp->cb;
1170   cb_cls = cp->cls;
1171   cp->helper = NULL;
1172   GNUNET_TESTBED_controller_stop (cp);
1173   if (NULL != cb)
1174     cb (cb_cls, NULL, GNUNET_SYSERR);
1175 }
1176
1177
1178 /**
1179  * Function to call to start a link-controllers type operation once all queues
1180  * the operation is part of declare that the operation can be activated.
1181  *
1182  * @param cls the closure from GNUNET_TESTBED_operation_create_()
1183  */
1184 static void
1185 opstart_link_controllers (void *cls)
1186 {
1187   struct OperationContext *opc = cls;
1188   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1189
1190   GNUNET_assert (NULL != opc->data);
1191   msg = opc->data;
1192   opc->data = NULL;
1193   opc->state = OPC_STATE_STARTED;
1194   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1195   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1196 }
1197
1198
1199 /**
1200  * Callback which will be called when link-controllers type operation is released
1201  *
1202  * @param cls the closure from GNUNET_TESTBED_operation_create_()
1203  */
1204 static void
1205 oprelease_link_controllers (void *cls)
1206 {
1207   struct OperationContext *opc = cls;
1208
1209   if (OPC_STATE_INIT == opc->state)
1210     GNUNET_free (opc->data);
1211   if (OPC_STATE_STARTED == opc->state)
1212     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1213   GNUNET_free (opc);
1214 }
1215
1216
1217 /**
1218  * Function to be called when get slave config operation is ready
1219  *
1220  * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1221  */
1222 static void
1223 opstart_get_slave_config (void *cls)
1224 {
1225   struct OperationContext *opc = cls;
1226   struct GetSlaveConfigData *data;
1227   struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1228   uint16_t msize;
1229   
1230   data = opc->data;
1231   msize = sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage);
1232   msg = GNUNET_malloc (msize);
1233   msg->header.size = htons (msize);
1234   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG);
1235   msg->operation_id = GNUNET_htonll (opc->id);
1236   msg->slave_id = htonl (data->slave_id);
1237   GNUNET_CONTAINER_DLL_insert_tail (opc->c->ocq_head, opc->c->ocq_tail, opc);
1238   GNUNET_TESTBED_queue_message_ (opc->c, &msg->header);
1239   opc->state = OPC_STATE_STARTED;
1240 }
1241
1242
1243 /**
1244  * Function to be called when get slave config operation is cancelled or finished
1245  *
1246  * @param cls the OperationContext of type OP_GET_SLAVE_CONFIG
1247  */
1248 static void
1249 oprelease_get_slave_config (void *cls)
1250 {
1251   struct OperationContext *opc = cls;
1252
1253   switch (opc->state)
1254   {
1255   case OPC_STATE_INIT:
1256     GNUNET_free (opc->data);
1257     break;
1258   case OPC_STATE_STARTED:
1259     GNUNET_free (opc->data);
1260     GNUNET_CONTAINER_DLL_remove (opc->c->ocq_head, opc->c->ocq_tail, opc);
1261     break;
1262   case OPC_STATE_FINISHED:
1263     if (NULL != opc->data)
1264       GNUNET_CONFIGURATION_destroy (opc->data);
1265     break;
1266   }
1267   GNUNET_free (opc);
1268 }
1269
1270
1271 /**
1272  * Starts a controller process at the host. FIXME: add controller start callback
1273  * with the configuration with which the controller is started
1274  *
1275  * @param controller_ip the ip address of the controller. Will be set as TRUSTED
1276  *          host when starting testbed controller at host
1277  * @param host the host where the controller has to be started; NULL for
1278  *          localhost
1279  * @param cfg template configuration to use for the remote controller; the
1280  *          remote controller will be started with a slightly modified
1281  *          configuration (port numbers, unix domain sockets and service home
1282  *          values are changed as per TESTING library on the remote host)
1283  * @param cb function called when the controller is successfully started or
1284  *          dies unexpectedly; GNUNET_TESTBED_controller_stop shouldn't be
1285  *          called if cb is called with GNUNET_SYSERR as status. Will never be
1286  *          called in the same task as 'GNUNET_TESTBED_controller_start'
1287  *          (synchronous errors will be signalled by returning NULL). This
1288  *          parameter cannot be NULL.
1289  * @param cls closure for above callbacks
1290  * @return the controller process handle, NULL on errors
1291  */
1292 struct GNUNET_TESTBED_ControllerProc *
1293 GNUNET_TESTBED_controller_start (const char *controller_ip,
1294                                  struct GNUNET_TESTBED_Host *host,
1295                                  const struct GNUNET_CONFIGURATION_Handle *cfg,
1296                                  GNUNET_TESTBED_ControllerStatusCallback cb,
1297                                  void *cls)
1298 {
1299   struct GNUNET_TESTBED_ControllerProc *cp;
1300   struct GNUNET_TESTBED_HelperInit *msg;
1301   static char *const binary_argv[] = {
1302     HELPER_TESTBED_BINARY, NULL
1303   };
1304   
1305   cp = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ControllerProc));
1306   if ((NULL == host) || (0 == GNUNET_TESTBED_host_get_id_ (host)))
1307     cp->helper =
1308         GNUNET_HELPER_start (GNUNET_YES, HELPER_TESTBED_BINARY, binary_argv,
1309                              &helper_mst, &helper_exp_cb, cp);
1310   else
1311   {
1312     char *remote_args[8];
1313     unsigned int argp;
1314     const char *username;
1315     const char *hostname;
1316
1317     username = GNUNET_TESTBED_host_get_username_ (host);
1318     hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1319     GNUNET_asprintf (&cp->port, "%u", GNUNET_TESTBED_host_get_ssh_port_ (host));
1320     if (NULL == username)
1321       GNUNET_asprintf (&cp->dst, "%s", hostname);
1322     else
1323       GNUNET_asprintf (&cp->dst, "%s@%s", username, hostname);
1324     LOG_DEBUG ("Starting SSH to destination %s\n", cp->dst);
1325     argp = 0;
1326     remote_args[argp++] = "ssh";
1327     remote_args[argp++] = "-p";
1328     remote_args[argp++] = cp->port;
1329     remote_args[argp++] = "-o";
1330     remote_args[argp++] = "BatchMode=yes";
1331     remote_args[argp++] = cp->dst;
1332     remote_args[argp++] = HELPER_TESTBED_BINARY_SSH;
1333     remote_args[argp++] = NULL;
1334     GNUNET_assert (argp == 8);
1335     cp->helper =
1336         GNUNET_HELPER_start (GNUNET_NO, "ssh", remote_args, &helper_mst,
1337                              &helper_exp_cb, cp);
1338   }
1339   if (NULL == cp->helper)
1340   {
1341     GNUNET_free_non_null (cp->port);
1342     GNUNET_free_non_null (cp->dst);
1343     GNUNET_free (cp);
1344     return NULL;
1345   }
1346   cp->host = host;
1347   cp->cb = cb;
1348   cp->cls = cls;
1349   msg = GNUNET_TESTBED_create_helper_init_msg_ (controller_ip, cfg);
1350   cp->msg = &msg->header;
1351   cp->shandle =
1352       GNUNET_HELPER_send (cp->helper, &msg->header, GNUNET_NO, &clear_msg, cp);
1353   if (NULL == cp->shandle)
1354   {
1355     GNUNET_free (msg);
1356     GNUNET_TESTBED_controller_stop (cp);
1357     return NULL;
1358   }
1359   return cp;
1360 }
1361
1362
1363 /**
1364  * Stop the controller process (also will terminate all peers and controllers
1365  * dependent on this controller).  This function blocks until the testbed has
1366  * been fully terminated (!). The controller status cb from
1367  * GNUNET_TESTBED_controller_start() will not be called.
1368  *
1369  * @param cproc the controller process handle
1370  */
1371 void
1372 GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
1373 {
1374   if (NULL != cproc->shandle)
1375     GNUNET_HELPER_send_cancel (cproc->shandle);
1376   if (NULL != cproc->helper)
1377     GNUNET_HELPER_stop (cproc->helper);
1378   if (NULL != cproc->cfg)
1379     GNUNET_CONFIGURATION_destroy (cproc->cfg);
1380   GNUNET_free_non_null (cproc->port);
1381   GNUNET_free_non_null (cproc->dst);
1382   GNUNET_free (cproc);
1383 }
1384
1385
1386 /**
1387  * Start a controller process using the given configuration at the
1388  * given host.
1389  *
1390  * @param cfg configuration to use
1391  * @param host host to run the controller on; This should be the same host if
1392  *          the controller was previously started with
1393  *          GNUNET_TESTBED_controller_start; NULL for localhost
1394  * @param event_mask bit mask with set of events to call 'cc' for;
1395  *                   or-ed values of "1LL" shifted by the
1396  *                   respective 'enum GNUNET_TESTBED_EventType'
1397  *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) | ...")
1398  * @param cc controller callback to invoke on events
1399  * @param cc_cls closure for cc
1400  * @return handle to the controller
1401  */
1402 struct GNUNET_TESTBED_Controller *
1403 GNUNET_TESTBED_controller_connect (const struct GNUNET_CONFIGURATION_Handle
1404                                    *cfg, struct GNUNET_TESTBED_Host *host,
1405                                    uint64_t event_mask,
1406                                    GNUNET_TESTBED_ControllerCallback cc,
1407                                    void *cc_cls)
1408 {
1409   struct GNUNET_TESTBED_Controller *controller;
1410   struct GNUNET_TESTBED_InitMessage *msg;
1411   const char *controller_hostname;
1412   unsigned long long max_parallel_operations;
1413   unsigned long long max_parallel_service_connections;
1414   unsigned long long max_parallel_topology_config_operations;
1415
1416   if (GNUNET_OK !=
1417       GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1418                                              "MAX_PARALLEL_OPERATIONS",
1419                                              &max_parallel_operations))
1420   {
1421     GNUNET_break (0);
1422     return NULL;
1423   }
1424   if (GNUNET_OK !=
1425       GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1426                                              "MAX_PARALLEL_SERVICE_CONNECTIONS",
1427                                              &max_parallel_service_connections))
1428   {
1429     GNUNET_break (0);
1430     return NULL;
1431   }
1432   if (GNUNET_OK !=
1433       GNUNET_CONFIGURATION_get_value_number (cfg, "testbed",
1434                                              "MAX_PARALLEL_TOPOLOGY_CONFIG_OPERATIONS",
1435                                              &max_parallel_topology_config_operations))
1436   {
1437     GNUNET_break (0);
1438     return NULL;
1439   }
1440   controller = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Controller));
1441   controller->cc = cc;
1442   controller->cc_cls = cc_cls;
1443   controller->event_mask = event_mask;
1444   controller->cfg = GNUNET_CONFIGURATION_dup (cfg);
1445   controller->client = GNUNET_CLIENT_connect ("testbed", controller->cfg);
1446   if (NULL == controller->client)
1447   {
1448     GNUNET_TESTBED_controller_disconnect (controller);
1449     return NULL;
1450   }
1451   if (NULL == host)
1452   {
1453     host = GNUNET_TESTBED_host_create_by_id_ (0);
1454     if (NULL == host)           /* If the above host create fails */
1455     {
1456       LOG (GNUNET_ERROR_TYPE_WARNING,
1457            "Treating NULL host as localhost. Multiple references to localhost "
1458            "may break when localhost freed before calling disconnect \n");
1459       host = GNUNET_TESTBED_host_lookup_by_id_ (0);
1460     }
1461     else
1462     {
1463       controller->aux_host = GNUNET_YES;
1464     }
1465   }
1466   GNUNET_assert (NULL != host);
1467   GNUNET_TESTBED_mark_host_registered_at_ (host, controller);
1468   controller->host = host;
1469   controller->opq_parallel_operations =
1470       GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1471                                               max_parallel_operations);
1472   controller->opq_parallel_service_connections =
1473       GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1474                                               max_parallel_service_connections);
1475   controller->opq_parallel_topology_config_operations=
1476       GNUNET_TESTBED_operation_queue_create_ ((unsigned int)
1477                                               max_parallel_service_connections);
1478   controller_hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1479   if (NULL == controller_hostname)
1480     controller_hostname = "127.0.0.1";
1481   msg =
1482       GNUNET_malloc (sizeof (struct GNUNET_TESTBED_InitMessage) +
1483                      strlen (controller_hostname) + 1);
1484   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_INIT);
1485   msg->header.size =
1486       htons (sizeof (struct GNUNET_TESTBED_InitMessage) +
1487              strlen (controller_hostname) + 1);
1488   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1489   msg->event_mask = GNUNET_htonll (controller->event_mask);
1490   strcpy ((char *) &msg[1], controller_hostname);
1491   GNUNET_TESTBED_queue_message_ (controller,
1492                                  (struct GNUNET_MessageHeader *) msg);
1493   return controller;
1494 }
1495
1496
1497 /**
1498  * Configure shared services at a controller.  Using this function,
1499  * you can specify that certain services (such as "resolver")
1500  * should not be run for each peer but instead be shared
1501  * across N peers on the specified host.  This function
1502  * must be called before any peers are created at the host.
1503  *
1504  * @param controller controller to configure
1505  * @param service_name name of the service to share
1506  * @param num_peers number of peers that should share one instance
1507  *        of the specified service (1 for no sharing is the default),
1508  *        use 0 to disable the service
1509  */
1510 void
1511 GNUNET_TESTBED_controller_configure_sharing (struct GNUNET_TESTBED_Controller
1512                                              *controller,
1513                                              const char *service_name,
1514                                              uint32_t num_peers)
1515 {
1516   struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1517   uint16_t service_name_size;
1518   uint16_t msg_size;
1519
1520   service_name_size = strlen (service_name) + 1;
1521   msg_size =
1522       sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage) +
1523       service_name_size;
1524   msg = GNUNET_malloc (msg_size);
1525   msg->header.size = htons (msg_size);
1526   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE);
1527   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (controller->host));
1528   msg->num_peers = htonl (num_peers);
1529   memcpy (&msg[1], service_name, service_name_size);
1530   GNUNET_TESTBED_queue_message_ (controller,
1531                                  (struct GNUNET_MessageHeader *) msg);
1532 }
1533
1534
1535 /**
1536  * disconnects from the controller.
1537  *
1538  * @param controller handle to controller to stop
1539  */
1540 void
1541 GNUNET_TESTBED_controller_disconnect (struct GNUNET_TESTBED_Controller
1542                                       *controller)
1543 {
1544   struct MessageQueue *mq_entry;
1545
1546   if (NULL != controller->th)
1547     GNUNET_CLIENT_notify_transmit_ready_cancel (controller->th);
1548   /* Clear the message queue */
1549   while (NULL != (mq_entry = controller->mq_head))
1550   {
1551     GNUNET_CONTAINER_DLL_remove (controller->mq_head, controller->mq_tail,
1552                                  mq_entry);
1553     GNUNET_free (mq_entry->msg);
1554     GNUNET_free (mq_entry);
1555   }
1556   if (NULL != controller->client)
1557     GNUNET_CLIENT_disconnect (controller->client);
1558   GNUNET_CONFIGURATION_destroy (controller->cfg);
1559   if (GNUNET_YES == controller->aux_host)
1560     GNUNET_TESTBED_host_destroy (controller->host);
1561   GNUNET_TESTBED_operation_queue_destroy_ (controller->opq_parallel_operations);
1562   GNUNET_TESTBED_operation_queue_destroy_
1563       (controller->opq_parallel_service_connections);
1564   GNUNET_TESTBED_operation_queue_destroy_
1565       (controller->opq_parallel_topology_config_operations);
1566   GNUNET_free (controller);
1567 }
1568
1569
1570 /**
1571  * Register a host with the controller
1572  *
1573  * @param controller the controller handle
1574  * @param host the host to register
1575  * @param cc the completion callback to call to inform the status of
1576  *          registration. After calling this callback the registration handle
1577  *          will be invalid. Cannot be NULL.
1578  * @param cc_cls the closure for the cc
1579  * @return handle to the host registration which can be used to cancel the
1580  *           registration
1581  */
1582 struct GNUNET_TESTBED_HostRegistrationHandle *
1583 GNUNET_TESTBED_register_host (struct GNUNET_TESTBED_Controller *controller,
1584                               struct GNUNET_TESTBED_Host *host,
1585                               GNUNET_TESTBED_HostRegistrationCompletion cc,
1586                               void *cc_cls)
1587 {
1588   struct GNUNET_TESTBED_HostRegistrationHandle *rh;
1589   struct GNUNET_TESTBED_AddHostMessage *msg;
1590   const char *username;
1591   const char *hostname;
1592   uint16_t msg_size;
1593   uint16_t user_name_length;
1594
1595   if (NULL != controller->rh)
1596     return NULL;
1597   hostname = GNUNET_TESTBED_host_get_hostname_ (host);
1598   if (GNUNET_YES == GNUNET_TESTBED_is_host_registered_ (host, controller))
1599   {
1600     LOG (GNUNET_ERROR_TYPE_WARNING, "Host hostname: %s already registered\n",
1601          (NULL == hostname) ? "localhost" : hostname);
1602     return NULL;
1603   }
1604   rh = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_HostRegistrationHandle));
1605   rh->host = host;
1606   rh->c = controller;
1607   GNUNET_assert (NULL != cc);
1608   rh->cc = cc;
1609   rh->cc_cls = cc_cls;
1610   controller->rh = rh;
1611   username = GNUNET_TESTBED_host_get_username_ (host);
1612   msg_size = (sizeof (struct GNUNET_TESTBED_AddHostMessage));
1613   user_name_length = 0;
1614   if (NULL != username)
1615   {
1616     user_name_length = strlen (username) + 1;
1617     msg_size += user_name_length;
1618   }
1619   /* FIXME: what happens when hostname is NULL? localhost */
1620   GNUNET_assert (NULL != hostname);
1621   msg_size += strlen (hostname) + 1;
1622   msg = GNUNET_malloc (msg_size);
1623   msg->header.size = htons (msg_size);
1624   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST);
1625   msg->host_id = htonl (GNUNET_TESTBED_host_get_id_ (host));
1626   msg->ssh_port = htons (GNUNET_TESTBED_host_get_ssh_port_ (host));
1627   msg->user_name_length = htons (user_name_length);
1628   if (NULL != username)
1629     memcpy (&msg[1], username, user_name_length);
1630   strcpy (((void *) &msg[1]) + user_name_length, hostname);
1631   GNUNET_TESTBED_queue_message_ (controller,
1632                                  (struct GNUNET_MessageHeader *) msg);
1633   return rh;
1634 }
1635
1636
1637 /**
1638  * Cancel the pending registration. Note that if the registration message is
1639  * already sent to the service the cancellation has only the effect that the
1640  * registration completion callback for the registration is never called.
1641  *
1642  * @param handle the registration handle to cancel
1643  */
1644 void
1645 GNUNET_TESTBED_cancel_registration (struct GNUNET_TESTBED_HostRegistrationHandle
1646                                     *handle)
1647 {
1648   if (handle != handle->c->rh)
1649   {
1650     GNUNET_break (0);
1651     return;
1652   }
1653   handle->c->rh = NULL;
1654   GNUNET_free (handle);
1655 }
1656
1657
1658 /**
1659  * Same as the GNUNET_TESTBED_controller_link, however expects configuration in
1660  * serialized and compressed
1661  *
1662  * @param master handle to the master controller who creates the association
1663  * @param delegated_host requests to which host should be delegated; cannot be NULL
1664  * @param slave_host which host is used to run the slave controller; use NULL to
1665  *          make the master controller connect to the delegated host
1666  * @param sxcfg serialized and compressed configuration
1667  * @param sxcfg_size the size scfg
1668  * @param scfg_size the size of uncompressed serialized configuration
1669  * @param is_subordinate GNUNET_YES if the controller at delegated_host should
1670  *          be started by the master controller; GNUNET_NO if we are just
1671  *          allowed to use the slave via TCP/IP
1672  */
1673 struct GNUNET_TESTBED_Operation *
1674 GNUNET_TESTBED_controller_link_2 (struct GNUNET_TESTBED_Controller *master,
1675                                   struct GNUNET_TESTBED_Host *delegated_host,
1676                                   struct GNUNET_TESTBED_Host *slave_host,
1677                                   const char *sxcfg, size_t sxcfg_size,
1678                                   size_t scfg_size, int is_subordinate)
1679 {
1680   struct OperationContext *opc;
1681   struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1682   uint16_t msg_size;
1683
1684   GNUNET_assert (GNUNET_YES ==
1685                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1686   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1687     GNUNET_assert (GNUNET_YES ==
1688                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1689   msg_size = sxcfg_size + sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1690   msg = GNUNET_malloc (msg_size);
1691   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS);
1692   msg->header.size = htons (msg_size);
1693   msg->delegated_host_id = htonl (GNUNET_TESTBED_host_get_id_ (delegated_host));
1694   msg->slave_host_id =
1695       htonl (GNUNET_TESTBED_host_get_id_
1696              ((NULL != slave_host) ? slave_host : master->host));
1697   msg->config_size = htons ((uint16_t) scfg_size);
1698   msg->is_subordinate = (GNUNET_YES == is_subordinate) ? 1 : 0;
1699   memcpy (&msg[1], sxcfg, sxcfg_size);
1700   opc = GNUNET_malloc (sizeof (struct OperationContext));
1701   opc->c = master;
1702   opc->data = msg;
1703   opc->type = OP_LINK_CONTROLLERS;
1704   opc->id = master->operation_counter++;
1705   opc->state = OPC_STATE_INIT;
1706   msg->operation_id = GNUNET_htonll (opc->id);
1707   opc->op =
1708       GNUNET_TESTBED_operation_create_ (opc, &opstart_link_controllers,
1709                                         &oprelease_link_controllers);
1710   GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1711                                           opc->op);
1712   return opc->op;
1713 }
1714
1715
1716 /**
1717  * Compresses given configuration using zlib compress
1718  *
1719  * @param config the serialized configuration
1720  * @param size the size of config
1721  * @param xconfig will be set to the compressed configuration (memory is fresly
1722  *          allocated)
1723  * @return the size of the xconfig
1724  */
1725 size_t
1726 GNUNET_TESTBED_compress_config_ (const char *config, size_t size,
1727                                  char **xconfig)
1728 {
1729   size_t xsize;
1730
1731   xsize = compressBound ((uLong) size);
1732   *xconfig = GNUNET_malloc (xsize);
1733   GNUNET_assert (Z_OK ==
1734                  compress2 ((Bytef *) * xconfig, (uLongf *) & xsize,
1735                             (const Bytef *) config, (uLongf) size,
1736                             Z_BEST_SPEED));
1737   return xsize;
1738 }
1739
1740
1741 /**
1742  * Create a link from slave controller to delegated controller. Whenever the
1743  * master controller is asked to start a peer at the delegated controller the
1744  * request will be routed towards slave controller (if a route exists). The
1745  * slave controller will then route it to the delegated controller. The
1746  * configuration of the slave controller is given and to be used to either
1747  * create the slave controller or to connect to an existing slave controller
1748  * process.  'is_subordinate' specifies if the given slave controller should be
1749  * started and managed by the master controller, or if the slave already has a
1750  * master and this is just a secondary master that is also allowed to use the
1751  * existing slave.
1752  *
1753  * @param master handle to the master controller who creates the association
1754  * @param delegated_host requests to which host should be delegated
1755  * @param slave_host which host is used to run the slave controller
1756  * @param slave_cfg configuration to use for the slave controller
1757  * @param is_subordinate GNUNET_YES if the slave should be started (and stopped)
1758  *                       by the master controller; GNUNET_NO if we are just
1759  *                       allowed to use the slave via TCP/IP
1760  * @return the operation handle
1761  */
1762 struct GNUNET_TESTBED_Operation *
1763 GNUNET_TESTBED_controller_link (struct GNUNET_TESTBED_Controller *master,
1764                                 struct GNUNET_TESTBED_Host *delegated_host,
1765                                 struct GNUNET_TESTBED_Host *slave_host,
1766                                 const struct GNUNET_CONFIGURATION_Handle
1767                                 *slave_cfg, int is_subordinate)
1768 {
1769   struct GNUNET_TESTBED_Operation *op;
1770   char *config;
1771   char *cconfig;
1772   size_t cc_size;
1773   size_t config_size;
1774
1775   GNUNET_assert (GNUNET_YES ==
1776                  GNUNET_TESTBED_is_host_registered_ (delegated_host, master));
1777   if ((NULL != slave_host) && (0 != GNUNET_TESTBED_host_get_id_ (slave_host)))
1778     GNUNET_assert (GNUNET_YES ==
1779                    GNUNET_TESTBED_is_host_registered_ (slave_host, master));
1780   config = GNUNET_CONFIGURATION_serialize (slave_cfg, &config_size);
1781   cc_size = GNUNET_TESTBED_compress_config_ (config, config_size, &cconfig);
1782   GNUNET_free (config);
1783   /* Configuration doesn't fit in 1 message */
1784   GNUNET_assert ((UINT16_MAX - 
1785                   sizeof (struct GNUNET_TESTBED_ControllerLinkMessage)) >= cc_size);
1786   op = GNUNET_TESTBED_controller_link_2 (master, delegated_host, slave_host,
1787                                          (const char *) cconfig, cc_size,
1788                                          config_size, is_subordinate);
1789   GNUNET_free (cconfig);
1790   return op;
1791 }
1792
1793
1794 /**
1795  * Function to acquire the configuration of a running slave controller. The
1796  * completion of the operation is signalled through the controller_cb from
1797  * GNUNET_TESTBED_controller_connect(). If the operation is successful the
1798  * handle to the configuration is available in the generic pointer of
1799  * operation_finished field of struct GNUNET_TESTBED_EventInformation.
1800  *
1801  * @param op_cls the closure for the operation
1802  * @param master the handle to master controller
1803  * @param slave_host the host where the slave controller is running; the handle
1804  *          to the slave_host should remain valid until this operation is
1805  *          cancelled or marked as finished
1806  * @return the operation handle; NULL if the slave_host is not registered at
1807  *           master
1808  */
1809 struct GNUNET_TESTBED_Operation *
1810 GNUNET_TESTBED_get_slave_config (void *op_cls,
1811                                  struct GNUNET_TESTBED_Controller *master,
1812                                  struct GNUNET_TESTBED_Host *slave_host)
1813 {
1814   struct OperationContext *opc;
1815   struct GetSlaveConfigData *data;
1816
1817   if (GNUNET_NO == GNUNET_TESTBED_is_host_registered_ (slave_host, master))
1818     return NULL;
1819   data = GNUNET_malloc (sizeof (struct GetSlaveConfigData));
1820   data->slave_id = GNUNET_TESTBED_host_get_id_ (slave_host);
1821   data->op_cls = op_cls;
1822   opc = GNUNET_malloc (sizeof (struct OperationContext));
1823   opc->state = OPC_STATE_INIT;
1824   opc->c = master;
1825   opc->id = master->operation_counter++;
1826   opc->type = OP_GET_SLAVE_CONFIG;
1827   opc->data = data;
1828   opc->op =
1829       GNUNET_TESTBED_operation_create_ (opc, &opstart_get_slave_config,
1830                                         &oprelease_get_slave_config);
1831   GNUNET_TESTBED_operation_queue_insert_ (master->opq_parallel_operations,
1832                                           opc->op); 
1833   return opc->op;
1834 }
1835
1836
1837 /**
1838  * Ask the testbed controller to write the current overlay topology to
1839  * a file.  Naturally, the file will only contain a snapshot as the
1840  * topology may evolve all the time.
1841  *
1842  * @param controller overlay controller to inspect
1843  * @param filename name of the file the topology should
1844  *        be written to.
1845  */
1846 void
1847 GNUNET_TESTBED_overlay_write_topology_to_file (struct GNUNET_TESTBED_Controller
1848                                                *controller,
1849                                                const char *filename)
1850 {
1851   GNUNET_break (0);
1852 }
1853
1854
1855 /**
1856  * Creates a helper initialization message. This function is here because we
1857  * want to use this in testing
1858  *
1859  * @param cname the ip address of the controlling host
1860  * @param cfg the configuration that has to used to start the testbed service
1861  *          thru helper
1862  * @return the initialization message
1863  */
1864 struct GNUNET_TESTBED_HelperInit *
1865 GNUNET_TESTBED_create_helper_init_msg_ (const char *cname,
1866                                         const struct GNUNET_CONFIGURATION_Handle
1867                                         *cfg)
1868 {
1869   struct GNUNET_TESTBED_HelperInit *msg;
1870   char *config;
1871   char *xconfig;
1872   size_t config_size;
1873   size_t xconfig_size;
1874   uint16_t cname_len;
1875   uint16_t msg_size;
1876
1877   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1878   GNUNET_assert (NULL != config);
1879   xconfig_size =
1880       GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1881   GNUNET_free (config);
1882   cname_len = strlen (cname);
1883   msg_size =
1884       xconfig_size + cname_len + 1 + sizeof (struct GNUNET_TESTBED_HelperInit);
1885   msg = GNUNET_realloc (xconfig, msg_size);
1886   (void) memmove (((void *) &msg[1]) + cname_len + 1, msg, xconfig_size);
1887   msg->header.size = htons (msg_size);
1888   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_HELPER_INIT);
1889   msg->cname_size = htons (cname_len);
1890   msg->config_size = htons (config_size);
1891   (void) strcpy ((char *) &msg[1], cname);
1892   return msg;
1893 }
1894
1895
1896 /**
1897  * Cancel a pending operation.  Releases all resources
1898  * of the operation and will ensure that no event
1899  * is generated for the operation.  Does NOT guarantee
1900  * that the operation will be fully undone (or that
1901  * nothing ever happened).
1902  *
1903  * @param operation operation to cancel
1904  */
1905 void
1906 GNUNET_TESTBED_operation_cancel (struct GNUNET_TESTBED_Operation *operation)
1907 {
1908   GNUNET_TESTBED_operation_done (operation);
1909 }
1910
1911
1912 /**
1913  * Signal that the information from an operation has been fully
1914  * processed.  This function MUST be called for each event
1915  * of type 'operation_finished' to fully remove the operation
1916  * from the operation queue.  After calling this function, the
1917  * 'op_result' becomes invalid (!).
1918  *
1919  * @param operation operation to signal completion for
1920  */
1921 void
1922 GNUNET_TESTBED_operation_done (struct GNUNET_TESTBED_Operation *operation)
1923 {
1924   switch (operation->type)
1925   {
1926   case OP_PEER_CREATE:
1927   case OP_PEER_DESTROY:
1928   case OP_PEER_START:
1929   case OP_PEER_STOP:
1930   case OP_PEER_INFO:
1931   case OP_OVERLAY_CONNECT:
1932   case OP_LINK_CONTROLLERS:
1933     GNUNET_TESTBED_operation_release_ (operation);
1934     return;
1935   default:
1936     GNUNET_assert (0);
1937     break;
1938   }
1939 }
1940
1941
1942 /**
1943  * Generates configuration by uncompressing configuration in given message. The
1944  * given message should be of the following types:
1945  * GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG,
1946  * GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG
1947  *
1948  * @param msg the message containing compressed configuration
1949  * @return handle to the parsed configuration
1950  */
1951 struct GNUNET_CONFIGURATION_Handle *
1952 GNUNET_TESTBED_extract_config_ (const struct GNUNET_MessageHeader *msg)
1953 {  
1954   struct GNUNET_CONFIGURATION_Handle *cfg;
1955   Bytef *data;
1956   const Bytef *xdata;
1957   uLong data_len;
1958   uLong xdata_len;
1959   int ret;
1960
1961   switch (ntohs (msg->type))
1962   {
1963   case GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG:
1964     {
1965       const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *imsg;
1966
1967       imsg = (const struct GNUNET_TESTBED_PeerConfigurationInformationMessage *)
1968           msg;
1969       data_len = (uLong) ntohs (imsg->config_size);
1970       xdata_len = ntohs (imsg->header.size)
1971           - sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1972       xdata = (const Bytef *) &imsg[1];
1973     }
1974     break;
1975   case GNUNET_MESSAGE_TYPE_TESTBED_SLAVECONFIG:
1976     {
1977       const struct GNUNET_TESTBED_SlaveConfiguration *imsg;
1978
1979       imsg = (const struct GNUNET_TESTBED_SlaveConfiguration *) msg;
1980       data_len = (uLong) ntohs (imsg->config_size);
1981       xdata_len =  ntohs (imsg->header.size) 
1982           - sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
1983       xdata = (const Bytef *) &imsg[1];
1984     }
1985     break;
1986   default:
1987     GNUNET_assert (0);
1988   }  
1989   data = GNUNET_malloc (data_len);
1990   if (Z_OK !=
1991       (ret =
1992        uncompress (data, &data_len, xdata, xdata_len)))
1993     GNUNET_assert (0);
1994   cfg = GNUNET_CONFIGURATION_create ();
1995   GNUNET_assert (GNUNET_OK ==
1996                  GNUNET_CONFIGURATION_deserialize (cfg, (const char *) data,
1997                                                    (size_t) data_len,
1998                                                    GNUNET_NO));
1999   GNUNET_free (data);
2000   return cfg;
2001 }
2002
2003
2004 /**
2005  * Checks the integrity of the OperationFailureEventMessage and if good returns
2006  * the error message it contains.
2007  *
2008  * @param msg the OperationFailureEventMessage
2009  * @return the error message
2010  */
2011 const char *
2012 GNUNET_TESTBED_parse_error_string_ (const struct
2013                                     GNUNET_TESTBED_OperationFailureEventMessage
2014                                     *msg)
2015 {
2016   uint16_t msize;
2017   const char *emsg;
2018   
2019   msize = ntohs (msg->header.size);
2020   if (sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage) >= msize)
2021     return NULL;
2022   msize -= sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
2023   emsg = (const char *) &msg[1];
2024   if ('\0' != emsg[msize])
2025   {
2026     GNUNET_break (0);
2027     return NULL;
2028   }
2029   return emsg;
2030 }
2031
2032 /* end of testbed_api.c */