overlay connect operation failure reporting
[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 "gnunet_transport_service.h"
31 #include "gnunet_core_service.h"
32 #include "gnunet_hello_lib.h"
33 #include <zlib.h>
34
35 #include "gnunet_testbed_service.h"
36 #include "testbed.h"
37 #include "testbed_api.h"
38 #include "testbed_api_hosts.h"
39 #include "gnunet_testing_lib-new.h"
40
41 /**
42  * Generic logging
43  */
44 #define LOG(kind,...)                           \
45   GNUNET_log (kind, __VA_ARGS__)
46
47 /**
48  * Debug logging
49  */
50 #define LOG_DEBUG(...)                          \
51   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
52
53
54 #define LIST_GROW_STEP 10
55
56 struct Context
57 {
58   /**
59    * The client handle associated with this context
60    */
61   struct GNUNET_SERVER_Client *client;
62
63   /**
64    * The network address of the master controller
65    */
66   char *master_ip;
67
68   /**
69    * The TESTING system handle for starting peers locally
70    */
71   struct GNUNET_TESTING_System *system;
72   
73   /**
74    * Event mask of event to be responded in this context
75    */
76   uint64_t event_mask;
77
78   /**
79    * Our host id according to this context
80    */
81   uint32_t host_id;
82 };
83
84
85 /**
86  * The message queue for sending messages to clients
87  */
88 struct MessageQueue
89 {
90   /**
91    * The message to be sent
92    */
93   struct GNUNET_MessageHeader *msg;
94
95   /**
96    * The client to send the message to
97    */
98   struct GNUNET_SERVER_Client *client;
99   
100   /**
101    * next pointer for DLL
102    */
103   struct MessageQueue *next;
104   
105   /**
106    * prev pointer for DLL
107    */
108   struct MessageQueue *prev;
109 };
110
111
112 /**
113  * The structure for identifying a shared service
114  */
115 struct SharedService
116 {
117   /**
118    * The name of the shared service
119    */
120   char *name;
121
122   /**
123    * Number of shared peers per instance of the shared service
124    */
125   uint32_t num_shared;
126
127   /**
128    * Number of peers currently sharing the service
129    */
130   uint32_t num_sharing;
131 };
132
133
134 /**
135  * A routing entry
136  */
137 struct Route
138 {
139   /**
140    * destination host
141    */
142   uint32_t dest;
143
144   /**
145    * The host destination is reachable thru
146    */
147   uint32_t thru;
148 };
149
150
151 /**
152  * Structure representing a connected(directly-linked) controller
153  */
154 struct Slave
155 {
156   /**
157    * The controller process handle if we had started the controller
158    */
159   struct GNUNET_TESTBED_ControllerProc *controller_proc;
160
161   /**
162    * The controller handle
163    */
164   struct GNUNET_TESTBED_Controller *controller;
165
166   /**
167    * The id of the host this controller is running on
168    */
169   uint32_t host_id;
170 };
171
172
173 /**
174  * States of LCFContext
175  */
176 enum LCFContextState
177   {
178     /**
179      * The Context has been initialized; Nothing has been done on it
180      */
181     INIT,
182
183     /**
184      * Delegated host has been registered at the forwarding controller
185      */
186     DELEGATED_HOST_REGISTERED,
187     
188     /**
189      * The slave host has been registred at the forwarding controller
190      */
191     SLAVE_HOST_REGISTERED,
192
193     /**
194      * The context has been finished (may have error)
195      */
196     FINISHED
197
198   };
199
200
201 /**
202  * Link controllers request forwarding context
203  */
204 struct LCFContext
205 {
206   /**
207    * The serialized and compressed configuration
208    */
209   char *sxcfg;
210
211   /**
212    * The gateway which will pass the link message to delegated host
213    */
214   struct Slave *gateway;
215
216   /**
217    * The host registration handle while registered hosts in this context
218    */
219   struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
220
221   /**
222    * The size of the compressed serialized configuration
223    */
224   size_t sxcfg_size;
225
226   /**
227    * The size of the uncompressed configuration
228    */
229   size_t scfg_size;
230
231   /**
232    * Should the delegated host be started by the slave host?
233    */
234   int is_subordinate;
235
236   /**
237    * The state of this context
238    */
239   enum LCFContextState state;
240
241   /**
242    * The delegated host
243    */
244   uint32_t delegated_host_id;
245
246   /**
247    * The slave host
248    */
249   uint32_t slave_host_id;
250
251 };
252
253
254 /**
255  * Structure of a queue entry in LCFContext request queue
256  */
257 struct LCFContextQueue
258 {
259   /**
260    * The LCFContext
261    */
262   struct LCFContext *lcf;
263
264   /**
265    * Head prt for DLL
266    */
267   struct LCFContextQueue *next;
268
269   /**
270    * Tail ptr for DLL
271    */
272   struct LCFContextQueue *prev;
273 };
274
275
276 /**
277  * A locally started peer
278  */
279 struct Peer
280 {
281   /**
282    * The peer handle from testing API
283    */
284   struct GNUNET_TESTING_Peer *peer;
285
286   /**
287    * The modified (by GNUNET_TESTING_peer_configure) configuration this peer is
288    * configured with
289    */
290   struct GNUNET_CONFIGURATION_Handle *cfg;
291
292   /**
293    * Our local reference id for this peer
294    */
295   uint32_t id;
296
297 };
298
299
300 /**
301  * State information for overlay connect context
302  */
303 enum OCCState
304   {
305     /**
306      * Initial state
307      */
308     OCC_STATE_INIT,
309
310     /**
311      * Peer 1 has connected to peer0
312      */
313     OCC_STATE_PEER0_SUCCESS,
314
315     /**
316      * Peer 2 has connected to peer1
317      */
318     OCC_STATE_PEER1_SUCCESS,
319
320   };
321
322
323 /**
324  * Context information for connecting 2 peers in overlay
325  */
326 struct OverlayConnectContext
327 {
328   /**
329    * The client which has requested for overlay connection
330    */
331   struct GNUNET_SERVER_Client *client;
332
333   /**
334    * the peer which has to connect to the other peer
335    */
336   struct Peer *peer;
337
338   /**
339    * The other peer
340    */
341   struct Peer *other_peer;
342   
343   /**
344    * Transport handle of the first peer to offer second peer's HELLO
345    */
346   struct GNUNET_TRANSPORT_Handle *p1th;
347
348   /**
349    * Transport handle of other peer to get its HELLO
350    */
351   struct GNUNET_TRANSPORT_Handle *p2th;
352
353   /**
354    * Core handles of the peer which has to connect to other peer
355    */
356   struct GNUNET_CORE_Handle *ch;
357
358   /**
359    * HELLO of the other peer
360    */
361   struct GNUNET_MessageHeader *hello;
362   
363   /**
364    * Get hello handle for the other peer
365    */
366   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
367
368   /**
369    * The error message we send if this overlay connect operation has timed out
370    */
371   char *emsg;
372   
373   /**
374    * The peer identity of the first peer
375    */
376   struct GNUNET_PeerIdentity peer_identity;
377
378   /**
379    * The peer identity of the other peer
380    */
381   struct GNUNET_PeerIdentity other_peer_identity;
382
383   /**
384    * The id of the operation responsible for creating this context
385    */
386   uint64_t op_id;
387
388   /**
389    * The id of the task for sending HELLO of peer 2 to peer 1 and ask peer 1 to
390    * connect to peer 2
391    */
392   GNUNET_SCHEDULER_TaskIdentifier send_hello_task;
393   
394   /**
395    * The id of the overlay connect timeout task
396    */
397   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
398
399   /**
400    * State information for determining whose HELLOs have been successfully
401    * exchanged
402    */
403   enum OCCState state;
404
405 };
406
407
408 /**
409  * The master context; generated with the first INIT message
410  */
411 static struct Context *master_context;
412
413 /***********/
414 /* Handles */
415 /***********/
416
417 /**
418  * Current Transmit Handle; NULL if no notify transmit exists currently
419  */
420 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
421
422 /****************/
423 /* Lists & Maps */
424 /****************/
425
426 /**
427  * The head for the LCF queue
428  */
429 static struct LCFContextQueue *lcfq_head;
430
431 /**
432  * The tail for the LCF queue
433  */
434 static struct LCFContextQueue *lcfq_tail;
435
436 /**
437  * The message queue head
438  */
439 static struct MessageQueue *mq_head;
440
441 /**
442  * The message queue tail
443  */
444 static struct MessageQueue *mq_tail;
445
446 /**
447  * Array of host list
448  */
449 static struct GNUNET_TESTBED_Host **host_list;
450
451 /**
452  * A list of routes
453  */
454 static struct Route **route_list;
455
456 /**
457  * A list of directly linked neighbours
458  */
459 static struct Slave **slave_list;
460
461 /**
462  * A list of peers we own locally
463  */
464 static struct Peer **peer_list;
465
466 /**
467  * The hashmap of shared services
468  */
469 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
470
471 /**
472  * The size of the host list
473  */
474 static uint32_t host_list_size;
475
476 /**
477  * The size of the route list
478  */
479 static uint32_t route_list_size;
480
481 /**
482  * The size of directly linked neighbours list
483  */
484 static uint32_t slave_list_size;
485
486 /**
487  * The size of the peer list
488  */
489 static uint32_t peer_list_size;
490
491 /*********/
492 /* Tasks */
493 /*********/
494
495 /**
496  * The lcf_task handle
497  */
498 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
499
500 /**
501  * The shutdown task handle
502  */
503 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
504
505
506 /**
507  * Function called to notify a client about the connection begin ready to queue
508  * more data.  "buf" will be NULL and "size" zero if the connection was closed
509  * for writing in the meantime.
510  *
511  * @param cls NULL
512  * @param size number of bytes available in buf
513  * @param buf where the callee should write the message
514  * @return number of bytes written to buf
515  */
516 static size_t
517 transmit_ready_notify (void *cls, size_t size, void *buf)
518 {
519   struct MessageQueue *mq_entry;
520
521   transmit_handle = NULL;
522   mq_entry = mq_head;
523   GNUNET_assert (NULL != mq_entry);
524   if (0 == size)
525     return 0;
526   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
527   size = ntohs (mq_entry->msg->size);
528   memcpy (buf, mq_entry->msg, size);
529   GNUNET_free (mq_entry->msg);
530   GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
531   GNUNET_free (mq_entry);
532   mq_entry = mq_head;
533   if (NULL != mq_entry)
534     transmit_handle = 
535       GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
536                                            ntohs (mq_entry->msg->size),
537                                            GNUNET_TIME_UNIT_FOREVER_REL,
538                                            &transmit_ready_notify, NULL);
539   return size;
540 }
541
542
543 /**
544  * Queues a message in send queue for sending to the service
545  *
546  * @param client the client to whom the queued message has to be sent
547  * @param msg the message to queue
548  */
549 static void
550 queue_message (struct GNUNET_SERVER_Client *client,
551                struct GNUNET_MessageHeader *msg)
552 {
553   struct MessageQueue *mq_entry;
554   uint16_t type;
555   uint16_t size;
556
557   type = ntohs (msg->type);
558   size = ntohs (msg->size);
559   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
560                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
561   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
562   mq_entry->msg = msg;
563   mq_entry->client = client;
564   LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
565               ntohs (msg->size));
566   GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
567   if (NULL == transmit_handle)
568     transmit_handle = 
569       GNUNET_SERVER_notify_transmit_ready (client, size,
570                                            GNUNET_TIME_UNIT_FOREVER_REL,
571                                            &transmit_ready_notify, NULL);
572 }
573
574
575 /**
576  * Similar to GNUNET_realloc; however clears tail part of newly allocated memory
577  *
578  * @param ptr the memory block to realloc
579  * @param size the size of ptr
580  * @param new_size the size to which ptr has to be realloc'ed
581  * @return the newly reallocated memory block
582  */
583 static void *
584 TESTBED_realloc (void *ptr, size_t size, size_t new_size)
585 {
586   ptr = GNUNET_realloc (ptr, new_size);
587   if (new_size > size)
588     ptr = memset (ptr + size, 0, new_size - size);
589   return ptr;
590 }
591
592
593 /**
594  * Function to add a host to the current list of known hosts
595  *
596  * @param host the host to add 
597  * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
598  *           already in use
599  */
600 static int
601 host_list_add (struct GNUNET_TESTBED_Host *host)
602 {
603   uint32_t host_id;
604
605   host_id = GNUNET_TESTBED_host_get_id_ (host);
606   if (host_list_size <= host_id)
607   {
608     host_list = 
609       TESTBED_realloc (host_list, 
610                        sizeof (struct GNUNET_TESTBED_Host *) * host_list_size,
611                        sizeof (struct GNUNET_TESTBED_Host *) *
612                        (host_list_size + LIST_GROW_STEP));
613     host_list_size += LIST_GROW_STEP;
614   }
615   if (NULL != host_list[host_id])
616   {
617     LOG_DEBUG ("A host with id: %u already exists\n", host_id);
618     return GNUNET_SYSERR;
619   }
620   host_list[host_id] = host;
621   return GNUNET_OK;
622 }
623
624
625 /**
626  * Adds a route to the route list
627  *
628  * @param route the route to add
629  */
630 static void
631 route_list_add (struct Route *route)
632 {
633   if (route->dest >= route_list_size)
634   {
635     route_list = 
636       TESTBED_realloc (route_list, 
637                        sizeof (struct Route *) * route_list_size,
638                        sizeof (struct Route *) * 
639                        (route_list_size + LIST_GROW_STEP));
640     route_list_size += LIST_GROW_STEP;
641   }
642   GNUNET_assert (NULL == route_list[route->dest]);
643   route_list[route->dest] = route;
644 }
645
646
647 /**
648  * Adds a slave to the slave array
649  *
650  * @param slave the slave controller to add
651  */
652 static void
653 slave_list_add (struct Slave *slave)
654 {
655   if (slave->host_id  >= slave_list_size)
656   {
657     slave_list = TESTBED_realloc (slave_list, 
658                                   sizeof (struct Slave *) *slave_list_size,
659                                   sizeof (struct Slave *) *
660                                   (slave_list_size + LIST_GROW_STEP));
661     slave_list_size += LIST_GROW_STEP;
662   }
663   GNUNET_assert (NULL == slave_list[slave->host_id]);
664   slave_list[slave->host_id] = slave;
665 }
666
667
668 /**
669  * Adds a peer to the peer array
670  *
671  * @param peer the peer to add
672  */
673 static void
674 peer_list_add (struct Peer *peer)
675 {
676   if (peer->id  >= peer_list_size)
677   {
678     peer_list = TESTBED_realloc (peer_list, 
679                                  sizeof (struct Peer *) * peer_list_size,
680                                  sizeof (struct Peer *) *
681                                  (peer_list_size + LIST_GROW_STEP));
682     peer_list_size += LIST_GROW_STEP;
683   }
684   GNUNET_assert (NULL == peer_list[peer->id]);
685   peer_list[peer->id] = peer;
686 }
687
688
689 /**
690  * Removes a the give peer from the peer array
691  *
692  * @param peer the peer to be removed
693  */
694 static void
695 peer_list_remove (struct Peer *peer)
696 {
697   uint32_t id;
698
699   peer_list[peer->id] = NULL;
700   while (peer_list_size >= LIST_GROW_STEP)
701   {
702     for (id = peer_list_size - 1;
703          id > peer_list_size - LIST_GROW_STEP; id--)
704       if (NULL != peer_list[id])
705         break;
706     if (id != peer_list_size - LIST_GROW_STEP)
707       break;
708     peer_list_size -= LIST_GROW_STEP;
709   }
710   peer_list = GNUNET_realloc (peer_list, sizeof (struct GNUNET_TESTBED_Peer*)
711                               * peer_list_size);
712 }
713
714
715 /**
716  * Routes message to a host given its host_id
717  *
718  * @param host_id the id of the destination host
719  * @param msg the message to be routed
720  */
721 static void
722 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
723 {
724   GNUNET_break (0);
725 }
726
727
728 /**
729  * Send operation failure message to client
730  *
731  * @param client the client to which the failure message has to be sent to
732  * @param operation_id the id of the failed operation
733  * @param emsg the error message; can be NULL
734  */
735 static void
736 send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
737                          uint64_t operation_id,
738                          const char *emsg)
739 {
740   struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
741   uint16_t msize;
742   uint16_t emsg_len;
743   
744   msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);  
745   emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
746   msize += emsg_len;
747   msg = GNUNET_malloc (msize);
748   msg->header.size = htons (msize);
749   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATIONEVENT);
750   msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
751   msg->operation_id = GNUNET_htonll (operation_id);
752   if (0 != emsg_len)
753     memcpy (&msg[1], emsg, emsg_len);
754   queue_message (client, &msg->header);
755 }
756
757
758 /**
759  * The  Link Controller forwarding task
760  *
761  * @param cls the LCFContext
762  * @param tc the Task context from scheduler
763  */
764 static void
765 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
766
767
768 /**
769  * Completion callback for host registrations while forwarding Link Controller messages
770  *
771  * @param cls the LCFContext
772  * @param emsg the error message; NULL if host registration is successful
773  */
774 static void
775 lcf_proc_cc (void *cls, const char *emsg)
776 {
777   struct LCFContext *lcf = cls;
778
779   lcf->rhandle = NULL;
780   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
781   switch (lcf->state)
782   {
783   case INIT:
784     if (NULL != emsg)
785       goto registration_error;
786     lcf->state = DELEGATED_HOST_REGISTERED;
787     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
788     break;
789   case DELEGATED_HOST_REGISTERED:
790      if (NULL != emsg)
791       goto registration_error;
792      lcf->state = SLAVE_HOST_REGISTERED;
793      lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
794      break;
795   default:
796     GNUNET_assert (0);          /* Shouldn't reach here */
797   }  
798   return;
799
800  registration_error:
801   LOG (GNUNET_ERROR_TYPE_WARNING, 
802        "Host registration failed with message: %s\n", emsg);
803   lcf->state = FINISHED;
804   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
805 }
806
807
808 /**
809  * The  Link Controller forwarding task
810  *
811  * @param cls the LCFContext
812  * @param tc the Task context from scheduler
813  */
814 static void
815 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
816 {
817   struct LCFContext *lcf = cls;
818   struct LCFContextQueue *lcfq;
819
820   lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
821   switch (lcf->state)
822   {
823   case INIT:
824     if (GNUNET_NO ==
825         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
826                                             lcf->gateway->controller))
827     {
828       lcf->rhandle =
829         GNUNET_TESTBED_register_host (lcf->gateway->controller,
830                                       host_list[lcf->delegated_host_id],
831                                       lcf_proc_cc, lcf);
832     }
833     else
834     {
835       lcf->state = DELEGATED_HOST_REGISTERED;
836       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
837     }
838     break;
839   case DELEGATED_HOST_REGISTERED:
840     if (GNUNET_NO ==
841         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->slave_host_id],
842                                             lcf->gateway->controller))
843     {
844       lcf->rhandle =
845         GNUNET_TESTBED_register_host (lcf->gateway->controller,
846                                       host_list[lcf->slave_host_id],
847                                       lcf_proc_cc, lcf);
848     }
849     else
850     {
851       lcf->state = SLAVE_HOST_REGISTERED;
852       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
853     }
854     break;
855   case SLAVE_HOST_REGISTERED:
856     GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
857                                       host_list[lcf->delegated_host_id],
858                                       host_list[lcf->slave_host_id],
859                                       lcf->sxcfg, lcf->sxcfg_size,
860                                       lcf->scfg_size,
861                                       lcf->is_subordinate);
862     lcf->state = FINISHED;
863     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
864     break;
865   case FINISHED:
866     lcfq = lcfq_head;
867     GNUNET_assert (lcfq->lcf == lcf);
868     GNUNET_free (lcf->sxcfg);
869     GNUNET_free (lcf);
870     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
871     GNUNET_free (lcfq);
872     if (NULL != lcfq_head)
873       lcf_proc_task_id = 
874         GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
875   }
876 }
877
878
879 /**
880  * Callback for event from slave controllers
881  *
882  * @param cls struct Slave *
883  * @param event information about the event
884  */
885 static void 
886 slave_event_callback(void *cls,
887                      const struct GNUNET_TESTBED_EventInformation *event)
888 {
889   GNUNET_break (0);
890 }
891
892
893 /**
894  * Callback to signal successfull startup of the controller process
895  *
896  * @param cls the closure from GNUNET_TESTBED_controller_start()
897  * @param cfg the configuration with which the controller has been started;
898  *          NULL if status is not GNUNET_OK
899  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
900  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
901  */
902 static void 
903 slave_status_callback (void *cls, 
904                        const struct GNUNET_CONFIGURATION_Handle *cfg,
905                        int status)
906 {
907   struct Slave *slave = cls;
908
909   if (GNUNET_SYSERR == status)
910   {
911     slave->controller_proc = NULL;
912     LOG (GNUNET_ERROR_TYPE_WARNING,
913          "Unexpected slave shutdown\n");
914     GNUNET_SCHEDULER_shutdown ();       /* We too shutdown */
915     return;
916   }
917   slave->controller =
918     GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
919                                        master_context->event_mask,
920                                        &slave_event_callback, slave);
921 }
922
923
924 /**
925  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
926  *
927  * @param cls NULL
928  * @param client identification of the client
929  * @param message the actual message
930  */
931 static void 
932 handle_init (void *cls,
933              struct GNUNET_SERVER_Client *client,
934              const struct GNUNET_MessageHeader *message)
935 {
936   const struct GNUNET_TESTBED_InitMessage *msg;
937   struct GNUNET_TESTBED_Host *host;
938   void *addr;
939   size_t addrlen;
940
941   if (NULL != master_context)
942   {
943     GNUNET_break (0);
944     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
945     return;
946   }
947   msg = (const struct GNUNET_TESTBED_InitMessage *) message;  
948   master_context = GNUNET_malloc (sizeof (struct Context));
949   master_context->client = client;
950   master_context->host_id = ntohl (msg->host_id);
951   GNUNET_assert (GNUNET_OK == 
952                  GNUNET_SERVER_client_get_address (client, &addr, &addrlen));
953   master_context->master_ip = GNUNET_malloc (NI_MAXHOST);
954   if (0 != getnameinfo (addr, addrlen, master_context->master_ip, NI_MAXHOST,
955                         NULL, 0, NI_NUMERICHOST))
956   {
957     LOG (GNUNET_ERROR_TYPE_WARNING,
958          "Cannot determine the ip of master controller: %s\n", STRERROR (errno));
959     GNUNET_free (addr);
960     GNUNET_free (master_context->master_ip);
961     GNUNET_assert (0);
962   }
963   GNUNET_free (addr);
964   if (0 == strcasecmp (master_context->master_ip, "localhost"))
965   {                             /* Hack for connections via unix sockets */
966     LOG_DEBUG ("May be using local sockets - assuming loopback for master ip\n");
967     GNUNET_free (master_context->master_ip);
968     master_context->master_ip = strdup ("127.0.0.1");
969   }
970   LOG_DEBUG ("Master Controller IP: %s\n", master_context->master_ip);
971   master_context->system = 
972     GNUNET_TESTING_system_create ("testbed", master_context->master_ip);
973   host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
974                                              NULL, NULL, 0);
975   host_list_add (host);
976   master_context->event_mask = GNUNET_ntohll (msg->event_mask);
977   GNUNET_SERVER_client_keep (client);
978   LOG_DEBUG ("Created master context with host ID: %u\n",
979              master_context->host_id);
980   GNUNET_SERVER_receive_done (client, GNUNET_OK);
981 }
982
983
984 /**
985  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
986  *
987  * @param cls NULL
988  * @param client identification of the client
989  * @param message the actual message
990  */
991 static void 
992 handle_add_host (void *cls,
993                  struct GNUNET_SERVER_Client *client,
994                  const struct GNUNET_MessageHeader *message)
995 {
996   struct GNUNET_TESTBED_Host *host;
997   const struct GNUNET_TESTBED_AddHostMessage *msg;
998   struct GNUNET_TESTBED_HostConfirmedMessage *reply;
999   char *username;
1000   char *hostname;
1001   char *emsg;
1002   uint32_t host_id;
1003   uint16_t username_length;
1004   uint16_t hostname_length;
1005   uint16_t reply_size;
1006   uint16_t msize;
1007   
1008   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1009   msize = ntohs (msg->header.size);
1010   username = (char *) &(msg[1]);
1011   username_length = ntohs (msg->user_name_length);
1012   GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1013                           + username_length + 1)); /* msg must contain hostname */
1014   if (0 != username_length)
1015     GNUNET_assert ('\0' == username[username_length]);
1016   username_length = (0 == username_length) ? 0 : username_length + 1;              
1017   hostname = username + username_length;
1018   hostname_length = msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage)
1019                              + username_length);
1020   GNUNET_assert ('\0' == hostname[hostname_length - 1]);
1021   GNUNET_assert (strlen (hostname) == hostname_length - 1);
1022   host_id = ntohl (msg->host_id);
1023   LOG_DEBUG ("Received ADDHOST message\n");
1024   LOG_DEBUG ("-------host id: %u\n", host_id);
1025   if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
1026   if (0 != username_length) LOG_DEBUG ("-------username: %s\n", username);
1027   else LOG_DEBUG ("-------username: NULL\n");
1028   LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1029   host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1030                                              ntohs (msg->ssh_port));
1031   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1032   reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1033   if (GNUNET_OK != host_list_add (host))
1034   {    
1035     /* We are unable to add a host */  
1036     emsg = "A host exists with given host-id";
1037     LOG_DEBUG ("%s: %u", emsg, host_id);
1038     GNUNET_TESTBED_host_destroy (host);
1039     reply_size += strlen (emsg) + 1;
1040     reply = GNUNET_malloc (reply_size);
1041     memcpy (&reply[1], emsg, strlen (emsg) + 1);
1042   }
1043   else
1044     reply = GNUNET_malloc (reply_size);  
1045   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
1046   reply->header.size = htons (reply_size);
1047   reply->host_id = htonl (host_id);  
1048   queue_message (client, &reply->header);
1049 }
1050
1051
1052 /**
1053  * Iterator over hash map entries.
1054  *
1055  * @param cls closure
1056  * @param key current key code
1057  * @param value value in the hash map
1058  * @return GNUNET_YES if we should continue to
1059  *         iterate,
1060  *         GNUNET_NO if not.
1061  */
1062 int ss_exists_iterator (void *cls,
1063                         const struct GNUNET_HashCode * key,
1064                         void *value)
1065 {
1066   struct SharedService *queried_ss = cls;
1067   struct SharedService *ss = value;
1068
1069   if (0 == strcmp (ss->name, queried_ss->name))
1070     return GNUNET_NO;
1071   else
1072     return GNUNET_YES;
1073 }
1074
1075
1076 /**
1077  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1078  *
1079  * @param cls NULL
1080  * @param client identification of the client
1081  * @param message the actual message
1082  */
1083 static void 
1084 handle_configure_shared_service (void *cls,
1085                                  struct GNUNET_SERVER_Client *client,
1086                                  const struct GNUNET_MessageHeader *message)
1087 {
1088   const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1089   struct SharedService *ss;
1090   char *service_name;
1091   struct GNUNET_HashCode hash;
1092   uint16_t msg_size;
1093   uint16_t service_name_size;
1094     
1095   msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1096   msg_size = ntohs (message->size);
1097   if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1098   {
1099     GNUNET_break (0);
1100     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1101     return;
1102   }
1103   service_name_size = msg_size - 
1104     sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1105   service_name = (char *) &msg[1];
1106   if ('\0' != service_name[service_name_size - 1])
1107   {
1108     GNUNET_break (0);
1109     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1110     return;
1111   }
1112   LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1113              service_name, ntohl (msg->num_peers));
1114   if (ntohl (msg->host_id) != master_context->host_id)
1115   {
1116     route_message (ntohl (msg->host_id), message);
1117     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1118     return;
1119   }
1120   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1121   ss = GNUNET_malloc (sizeof (struct SharedService));
1122   ss->name = strdup (service_name);
1123   ss->num_shared = ntohl (msg->num_peers);
1124   GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1125   if (GNUNET_SYSERR == 
1126       GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1127                                                   &ss_exists_iterator, ss))
1128   {
1129     LOG (GNUNET_ERROR_TYPE_WARNING,
1130          "Service %s already configured as a shared service. "
1131          "Ignoring service sharing request \n", ss->name);
1132     GNUNET_free (ss->name);
1133     GNUNET_free (ss);
1134     return;
1135   }
1136   GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1137                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);  
1138 }
1139
1140
1141 /**
1142  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1143  *
1144  * @param cls NULL
1145  * @param client identification of the client
1146  * @param message the actual message
1147  */
1148 static void 
1149 handle_link_controllers (void *cls,
1150                          struct GNUNET_SERVER_Client *client,
1151                          const struct GNUNET_MessageHeader *message)
1152 {
1153   const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
1154   struct GNUNET_CONFIGURATION_Handle *cfg;
1155   struct LCFContextQueue *lcfq;
1156   struct Route *route;
1157   struct Route *new_route;
1158   char *config;  
1159   uLongf dest_size;
1160   size_t config_size;
1161   uint32_t delegated_host_id;
1162   uint32_t slave_host_id;
1163   uint16_t msize;
1164    
1165   if (NULL == master_context)
1166   {
1167     GNUNET_break (0);
1168     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1169     return;
1170   }
1171   msize = ntohs (message->size);
1172   if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
1173   {
1174     GNUNET_break (0);
1175     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1176     return;
1177   }
1178   msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
1179   delegated_host_id = ntohl (msg->delegated_host_id);
1180   if (delegated_host_id == master_context->host_id)
1181   {
1182     GNUNET_break (0);
1183     LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1184     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1185     return;
1186   }
1187   if ((delegated_host_id >= host_list_size) || 
1188       (NULL == host_list[delegated_host_id]))
1189   {
1190     LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
1191     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1192     return;
1193   }
1194   slave_host_id = ntohl (msg->slave_host_id);
1195   if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
1196   {
1197     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
1198     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1199     return;
1200   }
1201   if (slave_host_id == delegated_host_id)
1202   {
1203     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1204     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1205     return;
1206   }
1207   msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
1208   config_size = ntohs (msg->config_size);
1209   
1210   if (slave_host_id == master_context->host_id) /* Link from us */
1211   {
1212     struct Slave *slave;
1213
1214     if ((delegated_host_id < slave_list_size) && 
1215         (NULL != slave_list[delegated_host_id])) /* We have already added */
1216     {
1217       LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
1218            delegated_host_id);
1219       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1220       return;
1221     }    
1222     config = GNUNET_malloc (config_size);
1223     dest_size = (uLongf) config_size;    
1224     if (Z_OK != uncompress ((Bytef *) config, &dest_size,
1225                             (const Bytef *) &msg[1], (uLong) msize))
1226     {
1227       GNUNET_break (0);           /* Compression error */
1228       GNUNET_free (config);
1229       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1230       return;
1231     }
1232     if (config_size == dest_size)
1233     {
1234       LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
1235       GNUNET_free (config);
1236       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1237       return;
1238     }
1239     cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
1240     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1241                                                        GNUNET_NO))
1242     {
1243       GNUNET_break (0);           /* Configuration parsing error */
1244       GNUNET_free (config);
1245       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1246       return;
1247     }
1248     GNUNET_free (config);
1249     if ((delegated_host_id < slave_list_size) &&
1250         (NULL != slave_list[delegated_host_id]))
1251     {
1252       GNUNET_break (0);           /* Configuration parsing error */
1253       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1254       return;
1255     }
1256     slave = GNUNET_malloc (sizeof (struct Slave));
1257     slave->host_id = delegated_host_id;    
1258     slave_list_add (slave);    
1259     if (1 == msg->is_subordinate)
1260     {
1261       slave->controller_proc =
1262         GNUNET_TESTBED_controller_start (master_context->master_ip,
1263                                          host_list[slave->host_id],
1264                                          cfg, &slave_status_callback,
1265                                          slave);
1266     }
1267     else {
1268       slave->controller = 
1269         GNUNET_TESTBED_controller_connect (cfg, host_list[slave->host_id],
1270                                            master_context->event_mask,
1271                                            &slave_event_callback, slave);
1272     }
1273     GNUNET_CONFIGURATION_destroy (cfg);
1274     new_route = GNUNET_malloc (sizeof (struct Route));
1275     new_route->dest = delegated_host_id;
1276     new_route->thru = master_context->host_id;
1277     route_list_add (new_route);
1278     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1279     return;
1280   }
1281
1282   /* Route the request */
1283   if (slave_host_id >= route_list_size)
1284   {
1285     LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1286     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1287     return;
1288   }
1289   lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1290   lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1291   lcfq->lcf->delegated_host_id = delegated_host_id;
1292   lcfq->lcf->slave_host_id = slave_host_id;
1293   while (NULL != (route = route_list[slave_host_id]))
1294   {
1295     if (route->thru == master_context->host_id)
1296       break;
1297     slave_host_id = route->thru;
1298   }
1299   GNUNET_assert (NULL != route); /* because we add routes carefully */
1300   GNUNET_assert (route->dest < slave_list_size);
1301   GNUNET_assert (NULL != slave_list[route->dest]);  
1302   lcfq->lcf->is_subordinate =
1303     (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
1304   lcfq->lcf->state = INIT;
1305   lcfq->lcf->gateway = slave_list[route->dest];
1306   lcfq->lcf->sxcfg_size = msize;
1307   lcfq->lcf->sxcfg = GNUNET_malloc (msize);
1308   lcfq->lcf->scfg_size = config_size;
1309   (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
1310   if (NULL == lcfq_head)
1311   {
1312     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1313     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1314     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
1315   }
1316   else
1317     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1318   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1319   new_route = GNUNET_malloc (sizeof (struct Route));
1320   new_route->dest = delegated_host_id;
1321   new_route->thru = route->dest;
1322   route_list_add (new_route);
1323 }
1324
1325
1326 /**
1327  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1328  *
1329  * @param cls NULL
1330  * @param client identification of the client
1331  * @param message the actual message
1332  */
1333 static void 
1334 handle_peer_create (void *cls,
1335                     struct GNUNET_SERVER_Client *client,
1336                     const struct GNUNET_MessageHeader *message)
1337 {
1338   const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1339   struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1340   struct GNUNET_CONFIGURATION_Handle *cfg;
1341   char *config;
1342   size_t dest_size;
1343   int ret;
1344   uint32_t config_size;
1345   uint16_t msize;
1346   
1347
1348   msize = ntohs (message->size);
1349   if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1350   {
1351     GNUNET_break (0);           /* We need configuration */
1352     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1353     return;
1354   }
1355   msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1356   if (ntohl (msg->host_id) == master_context->host_id)
1357   {
1358     struct Peer *peer;
1359     char *emsg;
1360     
1361     /* We are responsible for this peer */
1362     msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1363     config_size = ntohl (msg->config_size);    
1364     config = GNUNET_malloc (config_size);
1365     dest_size = config_size;
1366     if (Z_OK != (ret = uncompress ((Bytef *) config, (uLongf *) &dest_size,
1367                                    (const Bytef *) &msg[1], (uLong) msize)))
1368     {
1369       GNUNET_break (0);           /* uncompression error */
1370       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1371       return;
1372     }
1373     if (config_size != dest_size)
1374     {
1375       GNUNET_break (0);/* Uncompressed config size mismatch */
1376       GNUNET_free (config);
1377       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1378       return;
1379     }
1380     cfg = GNUNET_CONFIGURATION_create ();
1381     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
1382                                                        GNUNET_NO))
1383     {
1384       GNUNET_break (0);           /* Configuration parsing error */
1385       GNUNET_free (config);
1386       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1387       return;
1388     }
1389     GNUNET_free (config);
1390     peer = GNUNET_malloc (sizeof (struct Peer));
1391     peer->cfg = cfg;
1392     peer->id = ntohl (msg->peer_id);
1393     LOG_DEBUG ("Creating peer with id: %u\n", peer->id);
1394     peer->peer = GNUNET_TESTING_peer_configure (master_context->system, peer->cfg,
1395                                                 peer->id,
1396                                                 NULL /* Peer id */,
1397                                                 &emsg);
1398     if (NULL == peer->peer)
1399     {
1400       LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1401       GNUNET_free (emsg);
1402       GNUNET_free (peer);
1403       GNUNET_break (0);
1404       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1405       return;
1406     }
1407     peer_list_add (peer);
1408     reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1409     reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1410     reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCREATESUCCESS);
1411     reply->peer_id = msg->peer_id;
1412     reply->operation_id = msg->operation_id;
1413     queue_message (client, &reply->header);
1414     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1415     return;
1416   }
1417
1418   /* FIXME: Forward the peer to other host */
1419   GNUNET_break (0);
1420   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1421 }
1422
1423
1424 /**
1425  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1426  *
1427  * @param cls NULL
1428  * @param client identification of the client
1429  * @param message the actual message
1430  */
1431 static void 
1432 handle_peer_destroy (void *cls,
1433                      struct GNUNET_SERVER_Client *client,
1434                      const struct GNUNET_MessageHeader *message)
1435 {
1436   const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1437   struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *reply;
1438   struct Peer *peer;
1439   uint32_t peer_id;
1440   uint16_t reply_size;
1441   
1442   msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1443   peer_id = ntohl (msg->peer_id);
1444   LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1445              peer_id, GNUNET_ntohll (msg->operation_id));  
1446   if ((peer_list_size <= peer_id) || (NULL == peer_list[peer_id]))
1447   {
1448     GNUNET_break (0);
1449     /* FIXME: Reply with failure event message or forward to slave controller */
1450     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1451     return;
1452   }
1453   peer = peer_list[peer_id];
1454   GNUNET_TESTING_peer_destroy (peer->peer);
1455   GNUNET_CONFIGURATION_destroy (peer->cfg);
1456   peer_list_remove (peer);
1457   GNUNET_free (peer);
1458   reply_size = 
1459     sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
1460   reply = GNUNET_malloc (reply_size);
1461   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERICOPSUCCESS);
1462   reply->header.size = htons (reply_size);
1463   reply->operation_id = msg->operation_id;
1464   reply->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
1465   queue_message (client, &reply->header);
1466   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1467 }
1468
1469
1470 /**
1471  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1472  *
1473  * @param cls NULL
1474  * @param client identification of the client
1475  * @param message the actual message
1476  */
1477 static void 
1478 handle_peer_start (void *cls,
1479                    struct GNUNET_SERVER_Client *client,
1480                    const struct GNUNET_MessageHeader *message)
1481 {
1482   const struct GNUNET_TESTBED_PeerStartMessage *msg;
1483   struct GNUNET_TESTBED_PeerEventMessage *reply;
1484   uint32_t peer_id;
1485
1486   msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1487   peer_id = ntohl (msg->peer_id);
1488   if ((peer_id >= peer_list_size) 
1489       || (NULL == peer_list[peer_id]))
1490   {
1491     GNUNET_break (0);
1492     /* FIXME: reply with failure message or forward to slave controller */
1493     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1494     return;
1495   }
1496   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer_list[peer_id]->peer))
1497   {
1498     /* FIXME: return FAILURE message */
1499     GNUNET_break (0);
1500     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1501     return;
1502   }
1503   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1504   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1505   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1506   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1507   reply->host_id = htonl (master_context->host_id);
1508   reply->peer_id = msg->peer_id;
1509   reply->operation_id = msg->operation_id;
1510   queue_message (client, &reply->header);
1511   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1512 }
1513
1514
1515 /**
1516  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1517  *
1518  * @param cls NULL
1519  * @param client identification of the client
1520  * @param message the actual message
1521  */
1522 static void 
1523 handle_peer_stop (void *cls,
1524                   struct GNUNET_SERVER_Client *client,
1525                   const struct GNUNET_MessageHeader *message)
1526 {
1527   const struct GNUNET_TESTBED_PeerStopMessage *msg;
1528   struct GNUNET_TESTBED_PeerEventMessage *reply;
1529   uint32_t peer_id;
1530
1531   msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1532   peer_id = ntohl (msg->peer_id);
1533   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1534   {
1535     GNUNET_break (0);           /* FIXME: route to slave? */
1536     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1537     return;
1538   }
1539   if (GNUNET_OK != GNUNET_TESTING_peer_stop (peer_list[peer_id]->peer))
1540   {
1541     /* FIXME: return FAILURE message */
1542     GNUNET_break (0);
1543     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1544     return;
1545   }
1546   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1547   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEEREVENT);
1548   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1549   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1550   reply->host_id = htonl (master_context->host_id);
1551   reply->peer_id = msg->peer_id;
1552   reply->operation_id = msg->operation_id;
1553   queue_message (client, &reply->header);
1554   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1555 }
1556
1557
1558 /**
1559  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1560  *
1561  * @param cls NULL
1562  * @param client identification of the client
1563  * @param message the actual message
1564  */
1565 static void 
1566 handle_peer_get_config (void *cls,
1567                         struct GNUNET_SERVER_Client *client,
1568                         const struct GNUNET_MessageHeader *message)
1569 {
1570   const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1571   struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1572   char *config;
1573   char *xconfig;
1574   size_t c_size;
1575   size_t xc_size;  
1576   uint32_t peer_id;
1577   uint16_t msize;
1578   
1579   msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1580   peer_id = ntohl (msg->peer_id);
1581   if ((peer_id >= peer_list_size) || (NULL == peer_list[peer_id]))
1582   {
1583     /* FIXME: return FAILURE message */
1584     GNUNET_break (0);
1585     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1586   }
1587   config = GNUNET_CONFIGURATION_serialize (peer_list[peer_id]->cfg,
1588                                            &c_size);
1589   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1590   GNUNET_free (config);
1591   msize = xc_size + sizeof (struct
1592                             GNUNET_TESTBED_PeerConfigurationInformationMessage);
1593   reply = GNUNET_realloc (xconfig, msize);
1594   (void) memmove (&reply[1], reply, xc_size);
1595   reply->header.size = htons (msize);
1596   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONFIG);
1597   reply->peer_id = msg->peer_id;
1598   reply->operation_id = msg->operation_id;
1599   GNUNET_TESTING_peer_get_identity (peer_list[peer_id]->peer,
1600                                     &reply->peer_identity);
1601   reply->config_size = htons ((uint16_t) c_size);
1602   queue_message (client, &reply->header);
1603   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1604 }
1605
1606
1607 /**
1608  * Task for cleaing up overlay connect context structure
1609  *
1610  * @param cls the overlay connect context
1611  * @param tc the task context
1612  */
1613 static void
1614 occ_cleanup (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1615 {
1616   struct OverlayConnectContext *occ = cls;
1617
1618   GNUNET_free_non_null (occ->emsg);
1619   GNUNET_free_non_null (occ->hello);
1620   if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1621     GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1622   if (NULL != occ->ch)
1623     GNUNET_CORE_disconnect (occ->ch);
1624   if (NULL != occ->ghh)
1625     GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1626   if (NULL != occ->p1th)
1627     GNUNET_TRANSPORT_disconnect (occ->p1th);
1628   if (NULL != occ->p2th)
1629     GNUNET_TRANSPORT_disconnect (occ->p2th);
1630   GNUNET_free (occ);
1631 }
1632
1633
1634 /**
1635  * Task which will be run when overlay connect request has been timed out
1636  *
1637  * @param 
1638  * @return 
1639  */
1640 static void
1641 timeout_overlay_connect (void *cls,
1642                          const struct GNUNET_SCHEDULER_TaskContext *tc)
1643 {
1644   struct OverlayConnectContext *occ = cls;
1645
1646   occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1647   send_operation_fail_msg (occ->client, occ->op_id, occ->emsg);
1648   GNUNET_SERVER_client_drop (occ->client);
1649   occ_cleanup (occ, tc);
1650 }
1651
1652
1653
1654 /**
1655  * Function called to notify transport users that another
1656  * peer connected to us.
1657  *
1658  * @param cls closure
1659  * @param new_peer the peer that connected
1660  * @param ats performance data
1661  * @param ats_count number of entries in ats (excluding 0-termination)
1662  */
1663 static void 
1664 overlay_connect_notify (void *cls,
1665                         const struct GNUNET_PeerIdentity * new_peer,
1666                         const struct GNUNET_ATS_Information * ats,
1667                         unsigned int ats_count)
1668 {
1669   struct OverlayConnectContext *occ = cls;
1670   struct GNUNET_TESTBED_ConnectionEventMessage *msg;
1671   char *new_peer_str;
1672   char *other_peer_str;
1673
1674   LOG_DEBUG ("Overlay connect notify\n");
1675   if (0 == memcmp (new_peer, &occ->peer_identity, 
1676                    sizeof (struct GNUNET_PeerIdentity)))
1677     return;
1678   new_peer_str = GNUNET_strdup (GNUNET_i2s (new_peer));
1679   other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1680   if (0 != memcmp (new_peer, &occ->other_peer_identity,
1681                    sizeof (struct GNUNET_PeerIdentity)))
1682   {
1683     LOG_DEBUG ("Unexpected peer %4s connected to peer %4s\n",
1684                new_peer_str, other_peer_str);
1685     GNUNET_free (new_peer_str);
1686     GNUNET_free (other_peer_str);
1687     return;
1688   }
1689   LOG_DEBUG ("Peer %4s connected to peer %4s\n", new_peer_str, other_peer_str);
1690   GNUNET_free (new_peer_str);
1691   GNUNET_free (other_peer_str);
1692   if (GNUNET_SCHEDULER_NO_TASK != occ->send_hello_task)
1693   {
1694     GNUNET_SCHEDULER_cancel (occ->send_hello_task);
1695     occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1696   }
1697   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != occ->timeout_task);
1698   GNUNET_SCHEDULER_cancel (occ->timeout_task);
1699   occ->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1700   GNUNET_free_non_null (occ->emsg);
1701   occ->emsg = NULL;
1702   GNUNET_TRANSPORT_disconnect (occ->p1th);
1703   occ->p1th = NULL;
1704   /* Peer 1 has connected connect to peer2 - now send overlay connect success message */
1705   LOG_DEBUG ("Peers connected - Sending overlay connect success\n");
1706   msg = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_ConnectionEventMessage));
1707   msg->header.size = htons (sizeof (struct
1708                                     GNUNET_TESTBED_ConnectionEventMessage));
1709   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEERCONEVENT);
1710   msg->event_type = htonl (GNUNET_TESTBED_ET_CONNECT);
1711   msg->peer1 = htonl (occ->peer->id);
1712   msg->peer2 = htonl (occ->other_peer->id);
1713   msg->operation_id = GNUNET_htonll (occ->op_id);
1714   queue_message (occ->client, &msg->header);
1715   GNUNET_SERVER_client_drop (occ->client);
1716   GNUNET_SCHEDULER_add_now (&occ_cleanup, occ);
1717 }
1718
1719
1720 static void
1721 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1722 {
1723   struct OverlayConnectContext *occ = cls;
1724   char *other_peer_str;
1725
1726   occ->send_hello_task = GNUNET_SCHEDULER_NO_TASK;
1727   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1728     return;
1729   GNUNET_assert (NULL != occ->hello);
1730   other_peer_str = GNUNET_strdup (GNUNET_i2s (&occ->other_peer_identity));
1731   LOG_DEBUG ("Offering HELLO of %s to %s\n", other_peer_str, GNUNET_i2s (&occ->peer_identity));
1732   GNUNET_free (other_peer_str);
1733   GNUNET_TRANSPORT_offer_hello (occ->p1th, occ->hello, NULL, NULL);
1734   GNUNET_TRANSPORT_try_connect (occ->p1th, &occ->other_peer_identity);
1735   occ->send_hello_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
1736                                                        &send_hello, occ);
1737 }
1738
1739 /**
1740  * Test for checking whether HELLO message is empty
1741  *
1742  * @param cls empty flag to set
1743  * @param address the HELLO
1744  * @param expiration expiration of the HELLO
1745  * @return 
1746  */
1747 static int
1748 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
1749               struct GNUNET_TIME_Absolute expiration)
1750 {
1751   int *empty = cls;
1752
1753   *empty = GNUNET_NO;
1754   return GNUNET_OK;
1755 }
1756
1757
1758 /**
1759  * Function called whenever there is an update to the
1760  * HELLO of peers in the OverlayConnectClosure
1761  *
1762  * @param cls closure
1763  * @param hello our updated HELLO
1764  */
1765 static void 
1766 hello_update_cb (void *cls, const struct GNUNET_MessageHeader *hello)
1767 {
1768   struct OverlayConnectContext *occ = cls;
1769   int empty;
1770   uint16_t msize;
1771   
1772   msize = ntohs (hello->size);
1773   if (msize < 0)
1774   {
1775     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1776                 "HELLO message of peer %s is of size 0\n",
1777                 &occ->other_peer_identity);
1778     return;
1779   }
1780   empty = GNUNET_YES;
1781   (void) 
1782     GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) hello,
1783                                     GNUNET_NO, &test_address, &empty);
1784   if (GNUNET_YES == empty)
1785   {
1786     LOG_DEBUG ("HELLO of %s is empty\n", GNUNET_i2s (&occ->other_peer_identity));
1787     return;
1788   }
1789   LOG_DEBUG ("Received HELLO of %s\n", GNUNET_i2s (&occ->other_peer_identity));
1790   occ->hello = GNUNET_malloc (msize);
1791   memcpy (occ->hello, hello, msize);
1792   GNUNET_TRANSPORT_get_hello_cancel (occ->ghh);
1793   occ->ghh = NULL;
1794   GNUNET_TRANSPORT_disconnect (occ->p2th);
1795   occ->p2th = NULL;
1796   GNUNET_free_non_null (occ->emsg);  
1797   occ->emsg = GNUNET_strdup ("Timeout while offering HELLO to other peer");
1798   occ->send_hello_task = GNUNET_SCHEDULER_add_now (&send_hello, occ);
1799 }
1800
1801
1802 /**
1803  * Function called after GNUNET_CORE_connect has succeeded (or failed
1804  * for good).  Note that the private key of the peer is intentionally
1805  * not exposed here; if you need it, your process should try to read
1806  * the private key file directly (which should work if you are
1807  * authorized...).
1808  *
1809  * @param cls closure
1810  * @param server handle to the server, NULL if we failed
1811  * @param my_identity ID of this peer, NULL if we failed
1812  */
1813 static void 
1814 core_startup_cb (void *cls, struct GNUNET_CORE_Handle * server,
1815                  const struct GNUNET_PeerIdentity *my_identity)
1816 {
1817   struct OverlayConnectContext *occ = cls;
1818
1819   GNUNET_free_non_null (occ->emsg);
1820   occ->emsg = NULL;
1821   memcpy (&occ->peer_identity, my_identity, sizeof (struct GNUNET_PeerIdentity));
1822   occ->p1th =
1823     GNUNET_TRANSPORT_connect (occ->peer->cfg, &occ->peer_identity, NULL, NULL,
1824                               NULL, NULL);
1825   /* Connect to the transport of 2nd peer and get its HELLO message */
1826   GNUNET_TESTING_peer_get_identity (occ->other_peer->peer,
1827                                     &occ->other_peer_identity);
1828   occ->p2th = 
1829     GNUNET_TRANSPORT_connect (occ->other_peer->cfg, &occ->other_peer_identity,
1830                               NULL, NULL, NULL, NULL);
1831   if ((NULL == occ->p1th) || (NULL == occ->p2th))
1832   {
1833     occ->emsg = GNUNET_strdup ("Cannot connect to TRANSPORTs of peers");
1834     goto send_failure;
1835   }
1836   LOG_DEBUG ("Acquiring HELLO of peer %s\n", GNUNET_i2s
1837              (&occ->other_peer_identity));
1838   occ->emsg = GNUNET_strdup ("Timeout while acquiring HELLO message");
1839   occ->ghh = GNUNET_TRANSPORT_get_hello (occ->p2th, &hello_update_cb, occ);
1840   return;
1841
1842  send_failure:
1843   GNUNET_SCHEDULER_cancel (occ->timeout_task);
1844   occ->timeout_task = GNUNET_SCHEDULER_add_now (&timeout_overlay_connect, occ);  
1845 }
1846
1847
1848 /**
1849  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT messages
1850  *
1851  * @param cls NULL
1852  * @param client identification of the client
1853  * @param message the actual message
1854  */
1855 static void 
1856 handle_overlay_connect (void *cls,
1857                         struct GNUNET_SERVER_Client *client,
1858                         const struct GNUNET_MessageHeader *message)
1859 {
1860   const struct GNUNET_TESTBED_OverlayConnectMessage *msg;
1861   struct OverlayConnectContext *occ;
1862   struct GNUNET_CORE_MessageHandler no_handlers[] = {
1863     {NULL, 0, 0}
1864   };
1865   uint32_t p1;
1866   uint32_t p2;
1867
1868   msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
1869   p1 = ntohl (msg->peer1);
1870   p2 = ntohl (msg->peer2);
1871   GNUNET_assert (p1 < peer_list_size);
1872   GNUNET_assert (NULL != peer_list[p1]);
1873   GNUNET_assert (p2 < peer_list_size);
1874   GNUNET_assert (NULL != peer_list[p2]);
1875   occ = GNUNET_malloc (sizeof (struct OverlayConnectContext));
1876   GNUNET_SERVER_client_keep (client);
1877   occ->client = client;
1878   occ->state = OCC_STATE_INIT;
1879   occ->peer = peer_list[p1];
1880   occ->other_peer = peer_list[p2];
1881   occ->op_id = GNUNET_ntohll (msg->operation_id);
1882   occ->timeout_task =
1883     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
1884                                   (GNUNET_TIME_UNIT_SECONDS, 30),
1885                                   &timeout_overlay_connect, occ);   
1886   /* Connect to the core of 1st peer and wait for the 2nd peer to connect */
1887   occ->emsg = GNUNET_strdup ("Timeout while connecting to CORE");
1888   occ->ch = 
1889     GNUNET_CORE_connect (occ->peer->cfg, occ, &core_startup_cb,
1890                          &overlay_connect_notify, NULL, NULL, GNUNET_NO, NULL,
1891                          GNUNET_NO, no_handlers);
1892   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1893 }
1894
1895
1896 /**
1897  * Iterator over hash map entries.
1898  *
1899  * @param cls closure
1900  * @param key current key code
1901  * @param value value in the hash map
1902  * @return GNUNET_YES if we should continue to
1903  *         iterate,
1904  *         GNUNET_NO if not.
1905  */
1906 static int 
1907 ss_map_free_iterator (void *cls,
1908                       const struct GNUNET_HashCode * key, void *value)
1909 {
1910   struct SharedService *ss = value;
1911
1912   GNUNET_assert (GNUNET_YES ==
1913                  GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1914   GNUNET_free (ss->name);
1915   GNUNET_free (ss);
1916   return GNUNET_YES;
1917 }
1918
1919
1920 /**
1921  * Task to clean up and shutdown nicely
1922  *
1923  * @param cls NULL
1924  * @param tc the TaskContext from scheduler
1925  */
1926 static void
1927 shutdown_task (void *cls,
1928                const struct GNUNET_SCHEDULER_TaskContext *tc)
1929 {
1930   struct LCFContextQueue *lcfq;
1931   uint32_t id;
1932
1933   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
1934   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1935   (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1936                                                 NULL);
1937   GNUNET_CONTAINER_multihashmap_destroy (ss_map);  
1938   if (NULL != lcfq_head)
1939   {
1940     if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1941     {
1942       GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1943       lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1944     }
1945     if (NULL != lcfq_head->lcf->rhandle)
1946       GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1947   }
1948   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1949   for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1950   {
1951     GNUNET_free (lcfq->lcf->sxcfg);
1952     GNUNET_free (lcfq->lcf);
1953     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1954     GNUNET_free (lcfq);
1955   }
1956   /* Clear peer list */
1957   for (id = 0; id < peer_list_size; id++)
1958     if (NULL != peer_list[id])
1959     {
1960       GNUNET_TESTING_peer_destroy (peer_list[id]->peer);
1961       GNUNET_CONFIGURATION_destroy (peer_list[id]->cfg);
1962       GNUNET_free (peer_list[id]);
1963     }
1964   GNUNET_free_non_null (peer_list);
1965   /* Clear host list */
1966   for (id = 0; id < host_list_size; id++)
1967     if (NULL != host_list[id])
1968       GNUNET_TESTBED_host_destroy (host_list[id]);
1969   GNUNET_free_non_null (host_list);
1970   /* Clear route list */
1971   for (id = 0; id < route_list_size; id++)
1972     if (NULL != route_list[id])
1973       GNUNET_free (route_list[id]);
1974   GNUNET_free_non_null (route_list);
1975   /* Clear slave_list */
1976   for (id = 0; id < slave_list_size; id++)
1977     if (NULL != slave_list[id])
1978     {
1979       GNUNET_assert (NULL != slave_list[id]->controller);
1980       GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1981       if (NULL != slave_list[id]->controller_proc)
1982         GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1983     }
1984   if (NULL != master_context)
1985   {  
1986     GNUNET_free_non_null (master_context->master_ip);
1987     if (NULL != master_context->system)
1988       GNUNET_TESTING_system_destroy (master_context->system, GNUNET_YES);
1989     GNUNET_free (master_context);
1990     master_context = NULL;
1991   }
1992 }
1993
1994
1995 /**
1996  * Callback for client disconnect
1997  *
1998  * @param cls NULL
1999  * @param client the client which has disconnected
2000  */
2001 static void
2002 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2003 {
2004   if (NULL == master_context)
2005     return;
2006   if (client == master_context->client)
2007   {
2008     LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2009     GNUNET_SERVER_client_drop (client);
2010     /* should not be needed as we're terminated by failure to read
2011        from stdin, but if stdin fails for some reason, this shouldn't 
2012        hurt for now --- might need to revise this later if we ever
2013        decide that master connections might be temporarily down 
2014        for some reason */
2015     //GNUNET_SCHEDULER_shutdown ();
2016   }
2017 }
2018
2019
2020 /**
2021  * Testbed setup
2022  *
2023  * @param cls closure
2024  * @param server the initialized server
2025  * @param cfg configuration to use
2026  */
2027 static void 
2028 testbed_run (void *cls,
2029              struct GNUNET_SERVER_Handle *server,
2030              const struct GNUNET_CONFIGURATION_Handle *cfg)
2031 {
2032   static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
2033     {
2034       {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
2035        sizeof (struct GNUNET_TESTBED_InitMessage)},
2036       {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
2037       {&handle_configure_shared_service, NULL,
2038        GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
2039       {&handle_link_controllers, NULL,
2040        GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
2041       {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER, 0},
2042       {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER,
2043        sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2044       {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STARTPEER,
2045        sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2046       {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOPPEER,
2047        sizeof (struct GNUNET_TESTBED_PeerStopMessage)},      
2048       {&handle_peer_get_config, NULL, GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG,
2049        sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2050       {&handle_overlay_connect, NULL, GNUNET_MESSAGE_TYPE_TESTBED_OLCONNECT,
2051        sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2052       {NULL}
2053     };
2054
2055   GNUNET_SERVER_add_handlers (server,
2056                               message_handlers);
2057   GNUNET_SERVER_disconnect_notify (server,
2058                                    &client_disconnect_cb,
2059                                    NULL);
2060   ss_map = GNUNET_CONTAINER_multihashmap_create (5);
2061   shutdown_task_id = 
2062     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
2063                                   &shutdown_task,
2064                                   NULL);
2065   LOG_DEBUG ("Testbed startup complete\n");
2066 }
2067
2068
2069 /**
2070  * The starting point of execution
2071  */
2072 int main (int argc, char *const *argv)
2073 {
2074   //sleep (30);                 /* Debugging */
2075   return
2076     (GNUNET_OK ==
2077      GNUNET_SERVICE_run (argc,
2078                          argv,
2079                          "testbed",
2080                          GNUNET_SERVICE_OPTION_NONE,
2081                          &testbed_run,
2082                          NULL)) ? 0 : 1;
2083 }