hack for unix domain sockets
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
1 /*
2   This file is part of GNUnet.
3   (C) 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 2, 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/gnunet-service-testbed.c
23  * @brief implementation of the TESTBED service
24  * @author Sree Harsha Totakura
25  */
26
27 #include "platform.h"
28 #include "gnunet_service_lib.h"
29 #include "gnunet_server_lib.h"
30 #include <zlib.h>
31
32 #include "gnunet_testbed_service.h"
33 #include "testbed.h"
34 #include "testbed_api.h"
35 #include "testbed_api_hosts.h"
36 #include "gnunet_testing_lib-new.h"
37
38 /**
39  * Generic logging
40  */
41 #define LOG(kind,...)                           \
42   GNUNET_log (kind, __VA_ARGS__)
43
44 /**
45  * Debug logging
46  */
47 #define LOG_DEBUG(...)                          \
48   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
49
50
51 #define LIST_GROW_STEP 10
52
53 struct Context
54 {
55   /**
56    * The client handle associated with this context
57    */
58   struct GNUNET_SERVER_Client *client;
59
60   /**
61    * The network address of the master controller
62    */
63   char *master_ip;
64
65   /**
66    * The TESTING system handle for starting peers locally
67    */
68   struct GNUNET_TESTING_System *system;
69   
70   /**
71    * Event mask of event to be responded in this context
72    */
73   uint64_t event_mask;
74
75   /**
76    * Our host id according to this context
77    */
78   uint32_t host_id;
79 };
80
81
82 /**
83  * The message queue for sending messages to clients
84  */
85 struct MessageQueue
86 {
87   /**
88    * The message to be sent
89    */
90   struct GNUNET_MessageHeader *msg;
91
92   /**
93    * The client to send the message to
94    */
95   struct GNUNET_SERVER_Client *client;
96   
97   /**
98    * next pointer for DLL
99    */
100   struct MessageQueue *next;
101   
102   /**
103    * prev pointer for DLL
104    */
105   struct MessageQueue *prev;
106 };
107
108
109 /**
110  * The structure for identifying a shared service
111  */
112 struct SharedService
113 {
114   /**
115    * The name of the shared service
116    */
117   char *name;
118
119   /**
120    * Number of shared peers per instance of the shared service
121    */
122   uint32_t num_shared;
123
124   /**
125    * Number of peers currently sharing the service
126    */
127   uint32_t num_sharing;
128 };
129
130
131 /**
132  * A routing entry
133  */
134 struct Route
135 {
136   /**
137    * destination host
138    */
139   uint32_t dest;
140
141   /**
142    * The host destination is reachable thru
143    */
144   uint32_t thru;
145 };
146
147
148 /**
149  * Structure representing a connected(directly-linked) controller
150  */
151 struct Slave
152 {
153   /**
154    * The controller process handle if we had started the controller
155    */
156   struct GNUNET_TESTBED_ControllerProc *controller_proc;
157
158   /**
159    * The controller handle
160    */
161   struct GNUNET_TESTBED_Controller *controller;
162
163   /**
164    * The id of the host this controller is running on
165    */
166   uint32_t host_id;
167 };
168
169
170 /**
171  * Slave startup context
172  */
173 struct SlaveContext
174 {
175   /**
176    * The slave corresponding to this context
177    */
178   struct Slave *slave;
179
180   /**
181    * The configuration used as a template while startup
182    */
183   struct GNUNET_CONFIGURATION_Handle *cfg;
184 };
185
186
187 /**
188  * States of LCFContext
189  */
190 enum LCFContextState
191   {
192     /**
193      * The Context has been initialized; Nothing has been done on it
194      */
195     INIT,
196
197     /**
198      * Delegated host has been registered at the forwarding controller
199      */
200     DELEGATED_HOST_REGISTERED,
201     
202     /**
203      * The context has been finished (may have error)
204      */
205     FINISHED
206
207   };
208
209
210 /**
211  * Link controllers request forwarding context
212  */
213 struct LCFContext
214 {
215   /**
216    * The serialized and compressed configuration
217    */
218   char *sxcfg;
219
220   /**
221    * The gateway which will pass the link message to delegated host
222    */
223   struct Slave *gateway;
224
225   /**
226    * The host registration handle while registered hosts in this context
227    */
228   struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
229
230   /**
231    * The size of the compressed serialized configuration
232    */
233   size_t sxcfg_size;
234
235   /**
236    * The size of the uncompressed configuration
237    */
238   size_t scfg_size;
239
240   /**
241    * Should the delegated host be started by the slave host?
242    */
243   int is_subordinate;
244
245   /**
246    * The state of this context
247    */
248   enum LCFContextState state;
249
250   /**
251    * The delegated host
252    */
253   uint32_t delegated_host_id;
254   
255
256 };
257
258
259 /**
260  * Structure of a queue entry in LCFContext request queue
261  */
262 struct LCFContextQueue
263 {
264   /**
265    * The LCFContext
266    */
267   struct LCFContext *lcf;
268
269   /**
270    * Head prt for DLL
271    */
272   struct LCFContextQueue *next;
273
274   /**
275    * Tail ptr for DLL
276    */
277   struct LCFContextQueue *prev;
278 };
279
280
281 /**
282  * A locally started peer
283  */
284 struct Peer
285 {
286   /**
287    * The peer handle from testing API
288    */
289   struct GNUNET_TESTING_Peer *peer;
290
291   /**
292    * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
293    * configured with
294    */
295   struct GNUNET_CONFIGURATION_Handle *cfg;
296
297   /**
298    * Our local reference id for this peer
299    */
300   uint32_t id;
301
302 };
303
304
305 /**
306  * The master context; generated with the first INIT message
307  */
308 static struct Context *master_context;
309
310 /***********/
311 /* Handles */
312 /***********/
313
314 /**
315  * Wrapped stdin.
316  */
317 static struct GNUNET_DISK_FileHandle *fh;
318
319 /**
320  * Current Transmit Handle; NULL if no notify transmit exists currently
321  */
322 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
323
324 /****************/
325 /* Lists & Maps */
326 /****************/
327
328 /**
329  * The head for the LCF queue
330  */
331 static struct LCFContextQueue *lcfq_head;
332
333 /**
334  * The tail for the LCF queue
335  */
336 static struct LCFContextQueue *lcfq_tail;
337
338 /**
339  * The message queue head
340  */
341 static struct MessageQueue *mq_head;
342
343 /**
344  * The message queue tail
345  */
346 static struct MessageQueue *mq_tail;
347
348 /**
349  * Array of host list
350  */
351 static struct GNUNET_TESTBED_Host **host_list;
352
353 /**
354  * A list of routes
355  */
356 static struct Route **route_list;
357
358 /**
359  * A list of directly linked neighbours
360  */
361 static struct Slave **slave_list;
362
363 /**
364  * A list of peers we own locally
365  */
366 static struct Peer **peer_list;
367
368 /**
369  * The hashmap of shared services
370  */
371 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
372
373 /**
374  * The size of the host list
375  */
376 static uint32_t host_list_size;
377
378 /**
379  * The size of the route list
380  */
381 static uint32_t route_list_size;
382
383 /**
384  * The size of directly linked neighbours list
385  */
386 static uint32_t slave_list_size;
387
388 /**
389  * The size of the peer list
390  */
391 static uint32_t peer_list_size;
392
393 /*********/
394 /* Tasks */
395 /*********/
396
397 /**
398  * The lcf_task handle
399  */
400 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
401
402 /**
403  * The shutdown task handle
404  */
405 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
406
407 /******************/
408 /* Testing System */
409 /******************/
410
411 /**
412  * Our configuration; we also use this as template for starting other controllers
413  */
414 static struct GNUNET_CONFIGURATION_Handle *config;
415
416
417 /**
418  * Function called to notify a client about the connection begin ready to queue
419  * more data.  "buf" will be NULL and "size" zero if the connection was closed
420  * for writing in the meantime.
421  *
422  * @param cls NULL
423  * @param size number of bytes available in buf
424  * @param buf where the callee should write the message
425  * @return number of bytes written to buf
426  */
427 static size_t
428 transmit_ready_notify (void *cls, size_t size, void *buf)
429 {
430   struct MessageQueue *mq_entry;
431
432   transmit_handle = NULL;
433   mq_entry = mq_head;
434   GNUNET_assert (NULL != mq_entry);
435   if (0 == size)
436     return 0;
437   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
438   size = ntohs (mq_entry->msg->size);
439   memcpy (buf, mq_entry->msg, size);
440   GNUNET_free (mq_entry->msg);
441   GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
442   GNUNET_free (mq_entry);
443   mq_entry = mq_head;
444   if (NULL != mq_entry)
445     transmit_handle = 
446       GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
447                                            ntohs (mq_entry->msg->size),
448                                            GNUNET_TIME_UNIT_FOREVER_REL,
449                                            &transmit_ready_notify, NULL);
450   return size;
451 }
452
453
454 /**
455  * Queues a message in send queue for sending to the service
456  *
457  * @param client the client to whom the queued message has to be sent
458  * @param msg the message to queue
459  */
460 static void
461 queue_message (struct GNUNET_SERVER_Client *client,
462                struct GNUNET_MessageHeader *msg)
463 {
464   struct MessageQueue *mq_entry;
465   uint16_t type;
466   uint16_t size;
467
468   type = ntohs (msg->type);
469   size = ntohs (msg->size);
470   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
471                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
472   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
473   mq_entry->msg = msg;
474   mq_entry->client = client;
475   LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
476               ntohs (msg->size));
477   GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
478   if (NULL == transmit_handle)
479     transmit_handle = 
480       GNUNET_SERVER_notify_transmit_ready (client, size,
481                                            GNUNET_TIME_UNIT_FOREVER_REL,
482                                            &transmit_ready_notify, NULL);
483 }
484
485
486 /**
487  * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
488  *
489  * @param ptr the memory block to realloc
490  * @param size the size of ptr
491  * @param new_size the size to which ptr has to be realloc'ed
492  * @return the newly reallocated memory block
493  */
494 static void *
495 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
496 {
497   ptr = GNUNET_realloc (ptr, new_size);
498   if (new_size > size)
499     ptr = memset (ptr + size, 0, new_size - size);
500   return ptr;
501 }
502
503
504 /**
505  * Function to add a host to the current list of known hosts
506  *
507  * @param host the host to add 
508  * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
509  *           already in use
510  */
511 static int
512 host_list_add (struct GNUNET_TESTBED_Host *host)
513 {
514   uint32_t host_id;
515
516   host_id = GNUNET_TESTBED_host_get_id_ (host);
517   if (host_list_size <= host_id)
518   {
519     host_list = 
520       TESTBED_realloc (host_list, 
521                        sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
522                        sizeof (struct GNUNET_TESTBED_Host *) *
523                        (host_list_size + LIST_GROW_STEP));
524     host_list_size += LIST_GROW_STEP;
525   }
526   if (NULL != host_list[host_id])
527   {
528     LOG_DEBUG ("A host with id: %u already exists\n", host_id);
529     return GNUNET_SYSERR;
530   }
531   host_list[host_id] = host;
532   return GNUNET_OK;
533 }
534
535
536 /**
537  * Adds a route to the route list
538  *
539  * @param route the route to add
540  */
541 static void
542 route_list_add (struct Route *route)
543 {
544   if (route->dest >= route_list_size)
545   {
546     route_list = 
547       TESTBED_realloc (route_list, 
548                        sizeof (struct Route *) * route_list_size,
549                        sizeof (struct Route *) * 
550                        (route_list_size + LIST_GROW_STEP));
551     route_list_size += LIST_GROW_STEP;
552   }
553   GNUNET_assert (NULL == route_list[route->dest]);
554   route_list[route->dest] = route;
555 }
556
557
558 /**
559  * Adds a slave to the slave array
560  *
561  * @param slave the slave controller to add
562  */
563 static void
564 slave_list_add (struct Slave *slave)
565 {
566   if (slave->host_id  >= slave_list_size)
567   {
568     slave_list = TESTBED_realloc (slave_list, 
569                                   sizeof (struct Slave *) *slave_list_size,
570                                   sizeof (struct Slave *) *
571                                   (slave_list_size + LIST_GROW_STEP));
572     slave_list_size += LIST_GROW_STEP;
573   }
574   GNUNET_assert (NULL == slave_list[slave->host_id]);
575   slave_list[slave->host_id] = slave;
576 }
577
578
579 /**
580  * Adds a peer to the peer array
581  *
582  * @param peer the peer to add
583  */
584 static void
585 peer_list_add (struct Peer *peer)
586 {
587   if (peer->id  >= peer_list_size)
588   {
589     peer_list = TESTBED_realloc (peer_list, 
590                                  sizeof (struct Peer *) * peer_list_size,
591                                  sizeof (struct Peer *) *
592                                  (peer_list_size + LIST_GROW_STEP));
593     peer_list_size += LIST_GROW_STEP;
594   }
595   GNUNET_assert (NULL == peer_list[peer->id]);
596   peer_list[peer->id] = peer;
597 }
598
599
600 /**
601  * Routes message to a host given its host_id
602  *
603  * @param host_id the id of the destination host
604  * @param msg the message to be routed
605  */
606 static void
607 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
608 {
609   GNUNET_break (0);
610 }
611
612
613 /**
614  * The  Link Controller forwarding task
615  *
616  * @param cls the LCFContext
617  * @param tc the Task context from scheduler
618  */
619 static void
620 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
621
622
623 /**
624  * Completion callback for host registrations while forwarding Link Controller messages
625  *
626  * @param cls the LCFContext
627  * @param emsg the error message; NULL if host registration is successful
628  */
629 static void
630 lcf_proc_cc (void *cls, const char *emsg)
631 {
632   struct LCFContext *lcf = cls;
633
634   lcf->rhandle = NULL;
635   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
636   switch (lcf->state)
637   {
638   case INIT:
639     if (NULL != emsg)
640     {
641       LOG (GNUNET_ERROR_TYPE_WARNING, 
642            "Host registration failed with message: %s\n", emsg);
643       lcf->state = FINISHED;
644       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
645       return;
646     }
647     lcf->state = DELEGATED_HOST_REGISTERED;
648     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
649     break;
650   default:
651     GNUNET_assert (0);          /* Shouldn't reach here */
652   }  
653 }
654
655
656 /**
657  * The  Link Controller forwarding task
658  *
659  * @param cls the LCFContext
660  * @param tc the Task context from scheduler
661  */
662 static void
663 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
664 {
665   struct LCFContext *lcf = cls;
666   struct LCFContextQueue *lcfq;
667
668   lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
669   switch (lcf->state)
670   {
671   case INIT:
672     if (GNUNET_NO ==
673         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
674                                             lcf->gateway->controller))
675     {
676       lcf->rhandle =
677         GNUNET_TESTBED_register_host (lcf->gateway->controller,
678                                       host_list[lcf->delegated_host_id],
679                                       lcf_proc_cc, lcf);                                                   
680     }
681     else
682     {
683       lcf->state = DELEGATED_HOST_REGISTERED;
684       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
685     }
686     break;
687   case DELEGATED_HOST_REGISTERED:
688     GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
689                                       host_list[lcf->delegated_host_id],
690                                       host_list[lcf->gateway->host_id],
691                                       lcf->sxcfg, lcf->sxcfg_size,
692                                       lcf->scfg_size,
693                                       lcf->is_subordinate);
694     lcf->state = FINISHED;
695   case FINISHED:   
696     lcfq = lcfq_head;
697     GNUNET_assert (lcfq->lcf == lcf);
698     GNUNET_free (lcf->sxcfg);
699     GNUNET_free (lcf);
700     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
701     GNUNET_free (lcfq);
702     if (NULL != lcfq_head)
703       lcf_proc_task_id = 
704         GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
705   }
706 }
707
708
709 /**
710  * Callback for event from slave controllers
711  *
712  * @param cls struct Slave *
713  * @param event information about the event
714  */
715 static void 
716 slave_event_callback(void *cls,
717                      const struct GNUNET_TESTBED_EventInformation *event)
718 {
719   GNUNET_break (0);
720 }
721
722
723 /**
724  * Callback to signal successfull startup of the controller process
725  *
726  * @param cls the closure from GNUNET_TESTBED_controller_start()
727  * @param cfg the configuration with which the controller has been started;
728  *          NULL if status is not GNUNET_OK
729  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
730  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
731  */
732 static void 
733 slave_status_callback (void *cls, 
734                        const struct GNUNET_CONFIGURATION_Handle *cfg,
735                        int status)
736 {
737   struct SlaveContext *sc = cls;
738
739   if (GNUNET_SYSERR == status)
740   {
741     sc->slave->controller_proc = NULL;
742     LOG (GNUNET_ERROR_TYPE_WARNING,
743          "Unexpected slave shutdown\n");
744     GNUNET_SCHEDULER_shutdown ();       /* We too shutdown */
745     return;
746   }
747   GNUNET_CONFIGURATION_destroy (sc->cfg);
748   sc->slave->controller =
749     GNUNET_TESTBED_controller_connect (cfg, host_list[sc->slave->host_id],
750                                        master_context->event_mask,
751                                        &slave_event_callback, sc->slave);
752   GNUNET_free (sc);
753 }
754
755
756 /**
757  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
758  *
759  * @param cls NULL
760  * @param client identification of the client
761  * @param message the actual message
762  */
763 static void 
764 handle_init (void *cls,
765              struct GNUNET_SERVER_Client *client,
766              const struct GNUNET_MessageHeader *message)
767 {
768   const struct GNUNET_TESTBED_InitMessage *msg;
769   struct GNUNET_TESTBED_Host *host;
770   void *addr;
771   size_t addrlen;
772
773   if (NULL != master_context)
774   {
775     GNUNET_break (0);
776     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
777     return;
778   }
779   msg = (const struct GNUNET_TESTBED_InitMessage *) message;  
780   master_context = GNUNET_malloc (sizeof (struct Context));
781   master_context->client = client;
782   master_context->host_id = ntohl (msg->host_id);
783   GNUNET_assert (GNUNET_OK == 
784                  GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
785   master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
786   if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
787                         NULL, 0, NI_NUMERICHOST))
788   {
789     LOG (GNUNET_ERROR_TYPE_WARNING,
790          "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
791     GNUNET_free (addr);
792     GNUNET_free (master_context->master_ip);
793     GNUNET_assert (0);
794   }
795   GNUNET_free (addr);
796   if (0 == strcasecmp (master_context->master_ip, "localhost"))
797   {                             /* Hack for connections via unix sockets */
798     LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
799     GNUNET_free (master_context->master_ip);
800     master_context->master_ip = strdup ("127.0.0.1");
801   }
802   LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
803   master_context->system = 
804     GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
805   host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
806                                              NULL, NULL, 0);
807   host_list_add (host);
808   master_context->event_mask = GNUNET_ntohll (msg->event_mask);
809   GNUNET_SERVER_client_keep (client);
810   LOG_DEBUG ("Created master context with host ID: %u\n",
811              master_context->host_id);
812   GNUNET_SERVER_receive_done (client, GNUNET_OK);
813 }
814
815
816 /**
817  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
818  *
819  * @param cls NULL
820  * @param client identification of the client
821  * @param message the actual message
822  */
823 static void 
824 handle_add_host (void *cls,
825                  struct GNUNET_SERVER_Client *client,
826                  const struct GNUNET_MessageHeader *message)
827 {
828   struct GNUNET_TESTBED_Host *host;
829   const struct GNUNET_TESTBED_AddHostMessage *msg;
830   struct GNUNET_TESTBED_HostConfirmedMessage *reply;
831   char *username;
832   char *hostname;
833   char *emsg;
834   uint32_t host_id;
835   uint16_t username_length;
836   uint16_t hostname_length;
837   uint16_t reply_size;
838   
839   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
840   username_length = ntohs (msg->user_name_length);
841   username_length = (0 == username_length) ? 0 : username_length + 1;
842   username = (char *) &(msg[1]);
843   hostname = username + username_length;
844   if (ntohs (message->size) <=
845       (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
846   {
847     GNUNET_break (0);
848     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
849     return;
850   }
851   hostname_length = ntohs (message->size)
852     - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
853   if (strlen (hostname) != hostname_length - 1)
854   {
855     GNUNET_break (0);
856     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
857     return;
858   }
859   host_id = ntohl (msg->host_id);
860   LOG_DEBUG ("Received ADDHOST message\n");
861   LOG_DEBUG ("-------host id: %u\n", host_id);
862   if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
863   if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
864   else LOG_DEBUG ("-------username: NULL\n");
865   LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
866   host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
867                                              ntohs (msg->ssh_port));
868   GNUNET_SERVER_receive_done (client, GNUNET_OK);
869   reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
870   if (GNUNET_OK != host_list_add (host))
871   {    
872     /* We are unable to add a host */  
873     emsg = "A host exists with given host-id";
874     LOG_DEBUG ("%s: %u", emsg, host_id);
875     GNUNET_TESTBED_host_destroy (host);
876     reply_size += strlen (emsg) + 1;
877     reply = GNUNET_malloc (reply_size);
878     memcpy (&reply[1], emsg, strlen (emsg) + 1);
879   }
880   else
881     reply = GNUNET_malloc (reply_size);  
882   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
883   reply->header.size = htons (reply_size);
884   reply->host_id = htonl (host_id);  
885   queue_message (client, (struct GNUNET_MessageHeader *) reply);
886 }
887
888
889 /**
890  * Iterator over hash map entries.
891  *
892  * @param cls closure
893  * @param key current key code
894  * @param value value in the hash map
895  * @return GNUNET_YES if we should continue to
896  *         iterate,
897  *         GNUNET_NO if not.
898  */
899 int ss_exists_iterator (void *cls,
900                         const struct GNUNET_HashCode * key,
901                         void *value)
902 {
903   struct SharedService *queried_ss = cls;
904   struct SharedService *ss = value;
905
906   if (0 == strcmp (ss->name, queried_ss->name))
907     return GNUNET_NO;
908   else
909     return GNUNET_YES;
910 }
911
912
913 /**
914  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
915  *
916  * @param cls NULL
917  * @param client identification of the client
918  * @param message the actual message
919  */
920 static void 
921 handle_configure_shared_service (void *cls,
922                                  struct GNUNET_SERVER_Client *client,
923                                  const struct GNUNET_MessageHeader *message)
924 {
925   const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
926   struct SharedService *ss;
927   char *service_name;
928   struct GNUNET_HashCode hash;
929   uint16_t msg_size;
930   uint16_t service_name_size;
931     
932   msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
933   msg_size = ntohs (message->size);
934   if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
935   {
936     GNUNET_break (0);
937     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
938     return;
939   }
940   service_name_size = msg_size - 
941     sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
942   service_name = (char *) &msg[1];
943   if ('\0' != service_name[service_name_size - 1])
944   {
945     GNUNET_break (0);
946     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
947     return;
948   }
949   LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
950              service_name, ntohl (msg->num_peers));
951   if (ntohl (msg->host_id) != master_context->host_id)
952   {
953     route_message (ntohl (msg->host_id), message);
954     GNUNET_SERVER_receive_done (client, GNUNET_OK);
955     return;
956   }
957   GNUNET_SERVER_receive_done (client, GNUNET_OK);
958   ss = GNUNET_malloc (sizeof (struct SharedService));
959   ss->name = strdup (service_name);
960   ss->num_shared = ntohl (msg->num_peers);
961   GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
962   if (GNUNET_SYSERR == 
963       GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
964                                                   &ss_exists_iterator, ss))
965   {
966     LOG (GNUNET_ERROR_TYPE_WARNING,
967          "Service %s already configured as a shared service. "
968          "Ignoring service sharing request \n", ss->name);
969     GNUNET_free (ss->name);
970     GNUNET_free (ss);
971     return;
972   }
973   GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
974                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);  
975 }
976
977
978 /**
979  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
980  *
981  * @param cls NULL
982  * @param client identification of the client
983  * @param message the actual message
984  */
985 static void 
986 handle_link_controllers (void *cls,
987                          struct GNUNET_SERVER_Client *client,
988                          const struct GNUNET_MessageHeader *message)
989 {
990   const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
991   struct GNUNET_CONFIGURATION_Handle *cfg;
992   struct SlaveContext *sc;
993   struct LCFContextQueue *lcfq;
994   struct Route *route;
995   struct Route *new_route;
996   char *config;  
997   uLongf dest_size;
998   size_t config_size;
999   uint32_t delegated_host_id;
1000   uint32_t slave_host_id;
1001   uint16_t msize;
1002    
1003   if (NULL == master_context)
1004   {
1005     GNUNET_break (0);
1006     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1007     return;
1008   }
1009   msize = ntohs (message->size);
1010   if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1011   {
1012     GNUNET_break (0);
1013     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1014     return;
1015   }
1016   msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1017   delegated_host_id = ntohl (msg->delegated_host_id);
1018   if (delegated_host_id == master_context->host_id)
1019   {
1020     GNUNET_break (0);
1021     LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1022     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1023     return;
1024   }
1025   if ((delegated_host_id >= host_list_size) || 
1026       (NULL == host_list[delegated_host_id]))
1027   {
1028     LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1029     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1030     return;
1031   }
1032   slave_host_id = ntohl (msg->slave_host_id);
1033   if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1034   {
1035     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1036     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1037     return;
1038   }
1039   if (slave_host_id == delegated_host_id)
1040   {
1041     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1042     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1043     return;
1044   }
1045   msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1046   config_size = ntohs (msg->config_size);
1047   
1048   if (slave_host_id == master_context->host_id) /* Link from us */
1049   {
1050     struct Slave *slave;
1051
1052     if ((delegated_host_id < slave_list_size) && 
1053         (NULL != slave_list[delegated_host_id])) /* We have already added */
1054     {
1055       LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1056            delegated_host_id);
1057       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1058       return;
1059     }    
1060     config = GNUNET_malloc (config_size);
1061     dest_size = (uLongf) config_size;    
1062     if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1063                             (const Bytef *) &msg[1], (uLong) msize))
1064     {
1065       GNUNET_break (0);           /* Compression error */
1066       GNUNET_free (config);
1067       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1068       return;
1069     }
1070     if (config_size == dest_size)
1071     {
1072       LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1073       GNUNET_free (config);
1074       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1075     }
1076     cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1077     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1078                                                        GNUNET_NO))
1079     {
1080       GNUNET_break (0);           /* Configuration parsing error */
1081       GNUNET_free (config);
1082       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1083       return;
1084     }
1085     GNUNET_free (config);
1086     if ((delegated_host_id < slave_list_size) &&
1087         (NULL != slave_list[delegated_host_id]))
1088     {
1089       GNUNET_break (0);           /* Configuration parsing error */
1090       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1091       return;
1092     }
1093     slave = GNUNET_malloc (sizeof (struct Slave));
1094     slave->host_id = delegated_host_id;
1095     slave_list_add (slave);
1096     sc = GNUNET_malloc (sizeof (struct SlaveContext));
1097     sc->slave = slave;
1098     sc->cfg = cfg;
1099     if (1 == msg->is_subordinate)
1100     {
1101       slave->controller_proc =
1102         GNUNET_TESTBED_controller_start (master_context->master_ip,
1103                                          host_list[slave->host_id],
1104                                          cfg, &slave_status_callback,
1105                                          sc);
1106     }    
1107     new_route = GNUNET_malloc (sizeof (struct Route));
1108     new_route->dest = delegated_host_id;
1109     new_route->thru = master_context->host_id;
1110     route_list_add (new_route);
1111     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1112     return;
1113   }
1114
1115   /* Route the request */
1116   if (slave_host_id >= route_list_size)
1117   {
1118     LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1119     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1120     return;
1121   }
1122   while (NULL != (route = route_list[slave_host_id]))
1123   {
1124     if (route->thru == master_context->host_id)
1125       break;
1126     slave_host_id = route->thru;
1127   }
1128   GNUNET_assert (NULL != route); /* because we add routes carefully */
1129   GNUNET_assert (route->dest < slave_list_size);
1130   GNUNET_assert (NULL != slave_list[route->dest]);  
1131   lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1132   lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1133   lcfq->lcf->delegated_host_id = delegated_host_id;
1134   lcfq->lcf->is_subordinate =
1135     (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1136   lcfq->lcf->state = INIT;
1137   lcfq->lcf->gateway = slave_list[route->dest];
1138   lcfq->lcf->sxcfg_size = msize;
1139   lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1140   lcfq->lcf->scfg_size = config_size;
1141   (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1142   if (NULL == lcfq_head)
1143   {
1144     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1145     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1146     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1147   }
1148   else
1149     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1150   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1151   new_route = GNUNET_malloc (sizeof (struct Route));
1152   new_route->dest = delegated_host_id;
1153   new_route->thru = route->dest;
1154   route_list_add (new_route);
1155 }
1156
1157
1158 /**
1159  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1160  *
1161  * @param cls NULL
1162  * @param client identification of the client
1163  * @param message the actual message
1164  */
1165 static void 
1166 handle_peer_create (void *cls,
1167                     struct GNUNET_SERVER_Client *client,
1168                     const struct GNUNET_MessageHeader *message)
1169 {
1170   const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1171   struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1172   struct GNUNET_CONFIGURATION_Handle *cfg;
1173   char *config;
1174   size_t dest_size;
1175   int ret;
1176   uint32_t config_size;
1177   uint16_t msize;
1178   
1179
1180   msize = ntohs (message->size);
1181   if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1182   {
1183     GNUNET_break (0);           /* We need configuration */
1184     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1185     return;
1186   }
1187   msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1188   if (ntohl (msg->host_id) == master_context->host_id)
1189   {
1190     struct Peer *peer;
1191     char *emsg;
1192     
1193     /* We are responsible for this peer */
1194     msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1195     config_size = ntohl (msg->config_size);    
1196     config = GNUNET_malloc (config_size);
1197     dest_size = config_size;
1198     if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1199                                    (const Bytef *) &msg[1], (uLong) msize)))
1200     {
1201       GNUNET_break (0);           /* uncompression error */
1202       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1203       return;
1204     }
1205     if (config_size != dest_size)
1206     {
1207       GNUNET_break (0);/* Uncompressed config size mismatch */
1208       GNUNET_free (config);
1209       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1210       return;
1211     }
1212     cfg = GNUNET_CONFIGURATION_create ();
1213     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1214                                                        GNUNET_NO))
1215     {
1216       GNUNET_break (0);           /* Configuration parsing error */
1217       GNUNET_free (config);
1218       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1219       return;
1220     }
1221     GNUNET_free (config);
1222     peer = GNUNET_malloc (sizeof (struct Peer));
1223     peer->cfg = cfg;
1224     peer->id = ntohl (msg->peer_id);
1225     LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1226     peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1227                                                 peer->id,
1228                                                 NULL /* Peer id */,
1229                                                 &emsg);
1230     if (NULL == peer->peer)
1231     {
1232       LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1233       GNUNET_free (emsg);
1234       GNUNET_break (0);
1235       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1236       return;
1237     }
1238     peer_list_add (peer);
1239     reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1240     reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1241     reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1242     reply->peer_id = msg->peer_id;
1243     reply->operation_id = msg->operation_id;
1244     queue_message (client, &reply->header);
1245     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1246     return;
1247   }
1248
1249   /* FIXME: Forward the peer to other host */
1250   GNUNET_break (0);
1251   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1252 }
1253
1254
1255 /**
1256  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1257  *
1258  * @param cls NULL
1259  * @param client identification of the client
1260  * @param message the actual message
1261  */
1262 static void 
1263 handle_peer_destroy (void *cls,
1264                      struct GNUNET_SERVER_Client *client,
1265                      const struct GNUNET_MessageHeader *message)
1266 {
1267   const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1268   struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1269   uint32_t peer_id;
1270   uint32_t id;
1271   uint16_t reply_size;
1272   
1273   msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1274   peer_id = ntohl (msg->peer_id);
1275   LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1276              peer_id, GNUNET_ntohll (msg->operation_id));
1277   if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1278   {
1279     GNUNET_break (0);
1280     /* FIXME: Reply with failure event message or forward to slave controller */
1281     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1282     return;
1283   }  
1284   GNUNET_TESTING_peer_destroy (peer_list[peer_id]->peer);
1285   GNUNET_CONFIGURATION_destroy (peer_list[peer_id]->cfg);
1286   GNUNET_free (peer_list[peer_id]);
1287   peer_list[peer_id] = NULL;
1288   for (id = 0; id < LIST_GROW_STEP; id++)
1289   {
1290     if (((peer_id + id >= peer_list_size) ||
1291          (NULL != peer_list[peer_id])))
1292       break;
1293   }
1294   if (LIST_GROW_STEP == id)
1295   {
1296     peer_list_size -= LIST_GROW_STEP;
1297     peer_list = GNUNET_realloc (peer_list, peer_list_size);
1298   }
1299   reply_size = 
1300     sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1301   reply = GNUNET_malloc (reply_size);
1302   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1303   reply->header.size = htons (reply_size);
1304   reply->operation_id = msg->operation_id;
1305   reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1306   queue_message (client, &reply->header);
1307   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1308 }
1309
1310
1311 /**
1312  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1313  *
1314  * @param cls NULL
1315  * @param client identification of the client
1316  * @param message the actual message
1317  */
1318 static void 
1319 handle_peer_start (void *cls,
1320                    struct GNUNET_SERVER_Client *client,
1321                    const struct GNUNET_MessageHeader *message)
1322 {
1323   const struct GNUNET_TESTBED_PeerStartMessage *msg;
1324   struct GNUNET_TESTBED_PeerEventMessage *reply;
1325   uint32_t peer_id;
1326
1327   msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1328   peer_id = ntohl (msg->peer_id);
1329   if ((peer_id >= peer_list_size) 
1330       || (NULL == peer_list[peer_id]))
1331   {
1332     GNUNET_break (0);
1333     /* FIXME: reply with failure message or forward to slave controller */
1334     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1335     return;
1336   }
1337   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1338   {
1339     /* FIXME: return FAILURE message */
1340     GNUNET_break (0);
1341     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1342     return;
1343   }
1344   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1345   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1346   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1347   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1348   reply->host_id = htonl (master_context->host_id);
1349   reply->peer_id = msg->peer_id;
1350   reply->operation_id = msg->operation_id;
1351   queue_message (client, &reply->header);
1352   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1353 }
1354
1355
1356 /**
1357  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1358  *
1359  * @param cls NULL
1360  * @param client identification of the client
1361  * @param message the actual message
1362  */
1363 static void 
1364 handle_peer_stop (void *cls,
1365                   struct GNUNET_SERVER_Client *client,
1366                   const struct GNUNET_MessageHeader *message)
1367 {
1368   const struct GNUNET_TESTBED_PeerStopMessage *msg;
1369   struct GNUNET_TESTBED_PeerEventMessage *reply;
1370   uint32_t peer_id;
1371
1372   msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1373   peer_id = ntohl (msg->peer_id);
1374   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1375   {
1376     GNUNET_break (0);           /* FIXME: route to slave? */
1377     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1378     return;
1379   }
1380   if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1381   {
1382     /* FIXME: return FAILURE message */
1383     GNUNET_break (0);
1384     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1385     return;
1386   }
1387   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1388   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1389   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1390   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1391   reply->host_id = htonl (master_context->host_id);
1392   reply->peer_id = msg->peer_id;
1393   reply->operation_id = msg->operation_id;
1394   queue_message (client, &reply->header);
1395   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1396 }
1397
1398
1399 /**
1400  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1401  *
1402  * @param cls NULL
1403  * @param client identification of the client
1404  * @param message the actual message
1405  */
1406 static void 
1407 handle_peer_get_config (void *cls,
1408                         struct GNUNET_SERVER_Client *client,
1409                         const struct GNUNET_MessageHeader *message)
1410 {
1411   const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1412   struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1413   char *config;
1414   char *xconfig;
1415   size_t c_size;
1416   size_t xc_size;  
1417   uint32_t peer_id;
1418   uint16_t msize;
1419   
1420   msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1421   peer_id = ntohl (msg->peer_id);
1422   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1423   {
1424     /* FIXME: return FAILURE message */
1425     GNUNET_break (0);
1426     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1427   }
1428   config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1429                                            &c_size);
1430   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1431   GNUNET_free (config);
1432   msize = xc_size + sizeof (struct
1433                             GNUNET_TESTBED_PeerConfigurationInformationMessage);
1434   reply = GNUNET_realloc (xconfig, msize);
1435   (void) memmove (&reply[1], reply, xc_size);
1436   reply->header.size = htons (msize);
1437   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1438   reply->peer_id = msg->peer_id;
1439   reply->operation_id = msg->operation_id;
1440   GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1441                                     &reply->peer_identity);
1442   reply->config_size = htons ((uint16_t) c_size);
1443   queue_message (client, &reply->header);
1444   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1445 }
1446
1447
1448 /**
1449  * Iterator over hash map entries.
1450  *
1451  * @param cls closure
1452  * @param key current key code
1453  * @param value value in the hash map
1454  * @return GNUNET_YES if we should continue to
1455  *         iterate,
1456  *         GNUNET_NO if not.
1457  */
1458 static int 
1459 ss_map_free_iterator (void *cls,
1460                       const struct GNUNET_HashCode * key, void *value)
1461 {
1462   struct SharedService *ss = value;
1463
1464   GNUNET_assert (GNUNET_YES ==
1465                  GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1466   GNUNET_free (ss->name);
1467   GNUNET_free (ss);
1468   return GNUNET_YES;
1469 }
1470
1471
1472 /**
1473  * Task to clean up and shutdown nicely
1474  *
1475  * @param cls NULL
1476  * @param tc the TaskContext from scheduler
1477  */
1478 static void
1479 shutdown_task (void *cls,
1480                const struct GNUNET_SCHEDULER_TaskContext *tc)
1481 {
1482   struct LCFContextQueue *lcfq;
1483   uint32_t id;
1484
1485   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1486   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1487   (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1488                                                 NULL);
1489   GNUNET_CONTAINER_multihashmap_destroy (ss_map);  
1490   if (NULL != fh)
1491   {
1492     GNUNET_DISK_file_close (fh);
1493     fh = NULL;
1494   }
1495   if (NULL != lcfq_head)
1496   {
1497     if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1498     {
1499       GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1500       lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1501     }
1502     if (NULL != lcfq_head->lcf->rhandle)
1503       GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1504   }
1505   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1506   for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1507   {
1508     GNUNET_free (lcfq->lcf->sxcfg);
1509     GNUNET_free (lcfq->lcf);
1510     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1511     GNUNET_free (lcfq);
1512   }
1513   /* Clear peer list */
1514   for (id = 0; id < peer_list_size; id++)
1515     if (NULL != peer_list[id])
1516     {
1517       GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1518       GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1519       GNUNET_free (peer_list[id]);
1520     }
1521   GNUNET_free_non_null (peer_list);
1522   /* Clear host list */
1523   for (id = 0; id < host_list_size; id++)
1524     if (NULL != host_list[id])
1525       GNUNET_TESTBED_host_destroy (host_list[id]);
1526   GNUNET_free_non_null (host_list);
1527   /* Clear route list */
1528   for (id = 0; id < route_list_size; id++)
1529     if (NULL != route_list[id])
1530       GNUNET_free (route_list[id]);
1531   GNUNET_free_non_null (route_list);
1532   /* Clear slave_list */
1533   for (id = 0; id < slave_list_size; id++)
1534     if (NULL != slave_list[id])
1535     {
1536       GNUNET_assert (NULL != slave_list[id]->controller);
1537       GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1538       if (NULL != slave_list[id]->controller_proc)
1539         GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1540     }
1541   if (NULL != master_context)
1542   {  
1543     GNUNET_free_non_null (master_context->master_ip);
1544     if (NULL != master_context->system)
1545       GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1546   }
1547 }
1548
1549
1550 /**
1551  * Debug shutdown task in case of stdin getting closed
1552  *
1553  * @param cls NULL
1554  * @param tc the TaskContext from scheduler
1555  */
1556 static void
1557 shutdown_task_ (void *cls,
1558                 const struct GNUNET_SCHEDULER_TaskContext *tc)
1559 {
1560   LOG (GNUNET_ERROR_TYPE_DEBUG, "STDIN closed ...\n");
1561   shutdown_task (cls, tc);
1562 }
1563
1564
1565 /**
1566  * Callback for client disconnect
1567  *
1568  * @param cls NULL
1569  * @param client the client which has disconnected
1570  */
1571 static void
1572 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1573 {
1574   if (NULL == master_context)
1575     return;
1576   if (client == master_context->client)
1577   {
1578     LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1579     GNUNET_SERVER_client_drop (client);
1580     /* should not be needed as we're terminated by failure to read
1581        from stdin, but if stdin fails for some reason, this shouldn't 
1582        hurt for now --- might need to revise this later if we ever
1583        decide that master connections might be temporarily down 
1584        for some reason */
1585     GNUNET_SCHEDULER_shutdown ();
1586   }
1587 }
1588
1589
1590 /**
1591  * Testbed setup
1592  *
1593  * @param cls closure
1594  * @param server the initialized server
1595  * @param cfg configuration to use
1596  */
1597 static void 
1598 testbed_run (void *cls,
1599              struct GNUNET_SERVER_Handle *server,
1600              const struct GNUNET_CONFIGURATION_Handle *cfg)
1601 {
1602   static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1603     {
1604       {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1605        sizeof (struct GNUNET_TESTBED_InitMessage)},
1606       {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1607       {&handle_configure_shared_service, NULL,
1608        GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1609       {&handle_link_controllers, NULL,
1610        GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1611       {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
1612       {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
1613        sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
1614       {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
1615        sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
1616       {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
1617        sizeof (struct GNUNET_TESTBED_PeerStopMessage)},      
1618       {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
1619        sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
1620       {NULL}
1621     };
1622
1623   config = GNUNET_CONFIGURATION_dup (cfg);
1624   GNUNET_SERVER_add_handlers (server,
1625                               message_handlers);
1626   GNUNET_SERVER_disconnect_notify (server,
1627                                    &client_disconnect_cb,
1628                                    NULL);
1629   ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1630   fh = GNUNET_DISK_get_handle_from_native (stdin);
1631   if (NULL == fh)
1632     shutdown_task_id = 
1633       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1634                                     &shutdown_task,
1635                                     NULL);
1636   else
1637     shutdown_task_id = 
1638       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1639                                       fh,
1640                                       &shutdown_task_,
1641                                       NULL);
1642   LOG_DEBUG ("Testbed startup complete\n");
1643 }
1644
1645
1646 /**
1647  * The starting point of execution
1648  */
1649 int main (int argc, char *const *argv)
1650 {
1651   return
1652     (GNUNET_OK ==
1653      GNUNET_SERVICE_run (argc,
1654                          argv,
1655                          "testbed",
1656                          GNUNET_SERVICE_OPTION_NONE,
1657                          &testbed_run,
1658                          NULL)) ? 0 : 1;
1659 }