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