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