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