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