routing controller link messages
[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 "testbed.h"
33 #include "gnunet_testbed_service.h"
34 #include "testbed_api_hosts.h"
35
36 /**
37  * Generic logging
38  */
39 #define LOG(kind,...)                           \
40   GNUNET_log (kind, __VA_ARGS__)
41
42 /**
43  * Debug logging
44  */
45 #define LOG_DEBUG(...)                          \
46   LOG (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
47
48
49 #define LIST_GROW_STEP 10
50
51 struct Context
52 {
53   /**
54    * The client handle associated with this context
55    */
56   struct GNUNET_SERVER_Client *client;
57   
58   /**
59    * Event mask of event to be responded in this context
60    */
61   uint64_t event_mask;
62
63   /**
64    * Our host id according to this context
65    */
66   uint32_t host_id;
67 };
68
69
70 /**
71  * The message queue for sending messages to clients
72  */
73 struct MessageQueue
74 {
75   /**
76    * The message to be sent
77    */
78   struct GNUNET_MessageHeader *msg;
79
80   /**
81    * The client to send the message to
82    */
83   struct GNUNET_SERVER_Client *client;
84   
85   /**
86    * next pointer for DLL
87    */
88   struct MessageQueue *next;
89   
90   /**
91    * prev pointer for DLL
92    */
93   struct MessageQueue *prev;
94 };
95
96
97 /**
98  * The structure for identifying a shared service
99  */
100 struct SharedService
101 {
102   /**
103    * The name of the shared service
104    */
105   char *name;
106
107   /**
108    * Number of shared peers per instance of the shared service
109    */
110   uint32_t num_shared;
111
112   /**
113    * Number of peers currently sharing the service
114    */
115   uint32_t num_sharing;
116 };
117
118
119 /**
120  * A routing entry
121  */
122 struct Route
123 {
124   /**
125    * destination host
126    */
127   uint32_t dest;
128
129   /**
130    * The host destination is reachable thru
131    */
132   uint32_t thru;
133 };
134
135
136 /**
137  * Structure representing a connected(directly-linked) controller
138  */
139 struct Slave
140 {
141   /**
142    * The controller process handle if we had started the controller
143    */
144   struct GNUNET_TESTBED_ControllerProc *controller_proc;
145
146   /**
147    * The controller handle
148    */
149   struct GNUNET_TESTBED_Controller *controller;
150
151   /**
152    * The id of the host this controller is running on
153    */
154   uint32_t host_id;
155 };
156
157
158 /**
159  * States of LCFContext
160  */
161 enum LCFContextState
162   {
163     /**
164      * The Context has been initialized; Nothing has been done on it
165      */
166     INIT,
167
168     /**
169      * Delegated host has been registered at the forwarding controller
170      */
171     DELEGATED_HOST_REGISTERED,
172     
173     /**
174      * The context has been finished (may have error)
175      */
176     FINISHED
177
178   };
179
180
181 /**
182  * Link controllers request forwarding context
183  */
184 struct LCFContext
185 {
186   /**
187    * The serialized and compressed configuration
188    */
189   char *sxcfg;
190
191   /**
192    * The gateway which will pass the link message to delegated host
193    */
194   struct Slave *gateway;
195
196   /**
197    * The host registration handle while registered hosts in this context
198    */
199   struct GNUNET_TESTBED_HostRegistrationHandle *rhandle;
200
201   /**
202    * The size of the compressed serialized configuration
203    */
204   size_t sxcfg_size;
205
206   /**
207    * The size of the uncompressed configuration
208    */
209   size_t scfg_size;
210
211   /**
212    * Should the delegated host be started by the slave host?
213    */
214   int is_subordinate;
215
216   /**
217    * The state of this context
218    */
219   enum LCFContextState state;
220
221   /**
222    * The delegated host
223    */
224   uint32_t delegated_host_id;
225   
226
227 };
228
229
230 /**
231  * Structure of a queue entry in LCFContext request queue
232  */
233 struct LCFContextQueue
234 {
235   /**
236    * The LCFContext
237    */
238   struct LCFContext *lcf;
239
240   /**
241    * Head prt for DLL
242    */
243   struct LCFContextQueue *next;
244
245   /**
246    * Tail ptr for DLL
247    */
248   struct LCFContextQueue *prev;
249 };
250
251
252 /**
253  * The master context; generated with the first INIT message
254  */
255 static struct Context *master_context;
256
257 /***********/
258 /* Handles */
259 /***********/
260
261 /**
262  * Wrapped stdin.
263  */
264 static struct GNUNET_DISK_FileHandle *fh;
265
266 /**
267  * Current Transmit Handle; NULL if no notify transmit exists currently
268  */
269 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
270
271 /****************/
272 /* Lists & Maps */
273 /****************/
274
275 /**
276  * The head for the LCF queue
277  */
278 static struct LCFContextQueue *lcfq_head;
279
280 /**
281  * The tail for the LCF queue
282  */
283 static struct LCFContextQueue *lcfq_tail;
284
285 /**
286  * The message queue head
287  */
288 static struct MessageQueue *mq_head;
289
290 /**
291  * The message queue tail
292  */
293 static struct MessageQueue *mq_tail;
294
295 /**
296  * Array of host list
297  */
298 static struct GNUNET_TESTBED_Host **host_list;
299
300 /**
301  * A list of routes
302  */
303 static struct Route **route_list;
304
305 /**
306  * A list of directly linked neighbours
307  */
308 static struct Slave **slave_list;
309
310 /**
311  * The hashmap of shared services
312  */
313 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
314
315 /**
316  * The size of the host list
317  */
318 static uint32_t host_list_size;
319
320 /**
321  * The size of the route list
322  */
323 static uint32_t route_list_size;
324
325 /**
326  * The size of directly linked neighbours list
327  */
328 static uint32_t slave_list_size;
329
330 /*********/
331 /* Tasks */
332 /*********/
333
334 /**
335  * The lcf_task handle
336  */
337 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
338
339 /**
340  * The shutdown task handle
341  */
342 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
343
344
345 /**
346  * Function called to notify a client about the connection begin ready to queue
347  * more data.  "buf" will be NULL and "size" zero if the connection was closed
348  * for writing in the meantime.
349  *
350  * @param cls NULL
351  * @param size number of bytes available in buf
352  * @param buf where the callee should write the message
353  * @return number of bytes written to buf
354  */
355 static size_t
356 transmit_ready_notify (void *cls, size_t size, void *buf)
357 {
358   struct MessageQueue *mq_entry;
359
360   transmit_handle = NULL;
361   mq_entry = mq_head;
362   GNUNET_assert (NULL != mq_entry);
363   if (0 == size)
364     return 0;
365   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
366   size = ntohs (mq_entry->msg->size);
367   memcpy (buf, mq_entry->msg, size);
368   GNUNET_free (mq_entry->msg);
369   GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
370   GNUNET_free (mq_entry);
371   mq_entry = mq_head;
372   if (NULL != mq_entry)
373     transmit_handle = 
374       GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
375                                            ntohs (mq_entry->msg->size),
376                                            GNUNET_TIME_UNIT_FOREVER_REL,
377                                            &transmit_ready_notify, NULL);
378   return size;
379 }
380
381
382 /**
383  * Queues a message in send queue for sending to the service
384  *
385  * @param client the client to whom the queued message has to be sent
386  * @param msg the message to queue
387  */
388 static void
389 queue_message (struct GNUNET_SERVER_Client *client,
390                struct GNUNET_MessageHeader *msg)
391 {
392   struct MessageQueue *mq_entry;
393   uint16_t type;
394   uint16_t size;
395
396   type = ntohs (msg->type);
397   size = ntohs (msg->size);
398   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
399                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));                 
400   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
401   mq_entry->msg = msg;
402   mq_entry->client = client;
403   LOG_DEBUG ( "Queueing message of type %u, size %u for sending\n", type,
404               ntohs (msg->size));
405   GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
406   if (NULL == transmit_handle)
407     transmit_handle = 
408       GNUNET_SERVER_notify_transmit_ready (client, size,
409                                            GNUNET_TIME_UNIT_FOREVER_REL,
410                                            &transmit_ready_notify, NULL);
411 }
412
413
414 /**
415  * Function to add a host to the current list of known hosts
416  *
417  * @param host the host to add 
418  * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
419  *           already in use
420  */
421 static int
422 host_list_add (struct GNUNET_TESTBED_Host *host)
423 {
424   uint32_t host_id;
425   
426   host_id = GNUNET_TESTBED_host_get_id_ (host);
427   if (host_list_size <= host_id)
428   {
429     host_list = GNUNET_realloc (host_list, 
430                                 sizeof (struct GNUNET_TESTBED_Host *)
431                                 * (host_id + 10));
432     host_list_size += (host_id + 10);
433   }
434   if (NULL != host_list[host_id])
435   {
436     LOG_DEBUG ("A host with id: %u already exists\n", host_id);
437     return GNUNET_SYSERR;
438   }
439   host_list[host_id] = host;
440   return GNUNET_OK;
441 }
442
443
444 /**
445  * Adds a route to the route list
446  *
447  * @param route the route to add
448  */
449 static void
450 route_list_add (struct Route *route)
451 {
452   if (route->dest > route_list_size)
453   {
454     route_list_size += LIST_GROW_STEP;
455     route_list = GNUNET_realloc (route_list, sizeof (struct Route *)
456                                  * route_list_size);
457   }
458   GNUNET_assert (NULL == route_list[route->dest]);
459   route_list[route->dest] = route;
460 }
461
462
463 /**
464  * Routes message to a host given its host_id
465  *
466  * @param host_id the id of the destination host
467  * @param msg the message to be routed
468  */
469 static void
470 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
471 {
472   GNUNET_break (0);
473 }
474
475
476 /**
477  * The  Link Controller forwarding task
478  *
479  * @param cls the LCFContext
480  * @param tc the Task context from scheduler
481  */
482 static void
483 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
484
485
486 /**
487  * Completion callback for host registrations while forwarding Link Controller messages
488  *
489  * @param cls the LCFContext
490  * @param emsg the error message; NULL if host registration is successful
491  */
492 static void
493 lcf_proc_cc (void *cls, const char *emsg)
494 {
495   struct LCFContext *lcf = cls;
496
497   lcf->rhandle = NULL;
498   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
499   switch (lcf->state)
500   {
501   case INIT:
502     if (NULL != emsg)
503     {
504       LOG (GNUNET_ERROR_TYPE_WARNING, 
505            "Host registration failed with message: %s\n", emsg);
506       lcf->state = FINISHED;
507       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
508       return;
509     }
510     lcf->state = DELEGATED_HOST_REGISTERED;
511     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
512     break;
513   default:
514     GNUNET_assert (0);          /* Shouldn't reach here */
515   }  
516 }
517
518
519 /**
520  * The  Link Controller forwarding task
521  *
522  * @param cls the LCFContext
523  * @param tc the Task context from scheduler
524  */
525 static void
526 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
527 {
528   struct LCFContext *lcf = cls;
529   struct LCFContextQueue *lcfq;
530
531   lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
532   switch (lcf->state)
533   {
534   case INIT:
535     if (GNUNET_NO ==
536         GNUNET_TESTBED_is_host_registered_ (host_list[lcf->delegated_host_id],
537                                             lcf->gateway->controller))
538     {
539       lcf->rhandle =
540         GNUNET_TESTBED_register_host (lcf->gateway->controller,
541                                       host_list[lcf->delegated_host_id],
542                                       lcf_proc_cc, lcf);                                                   
543     }
544     else
545     {
546       lcf->state = DELEGATED_HOST_REGISTERED;
547       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
548     }
549     break;
550   case DELEGATED_HOST_REGISTERED:
551     GNUNET_TESTBED_controller_link_2 (lcf->gateway->controller,
552                                       host_list[lcf->delegated_host_id],
553                                       host_list[lcf->gateway->host_id],
554                                       lcf->sxcfg, lcf->sxcfg_size,
555                                       lcf->scfg_size,
556                                       lcf->is_subordinate);
557     lcf->state = FINISHED;
558   case FINISHED:   
559     lcfq = lcfq_head;
560     GNUNET_assert (lcfq->lcf == lcf);
561     GNUNET_free (lcf->sxcfg);
562     GNUNET_free (lcf);
563     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
564     GNUNET_free (lcfq);
565     if (NULL != lcfq_head)
566       lcf_proc_task_id = 
567         GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
568   }
569 }
570
571
572 /**
573  * Callback for event from slave controllers
574  *
575  * @param cls struct Slave *
576  * @param event information about the event
577  */
578 static void 
579 slave_event_callback(void *cls,
580                      const struct GNUNET_TESTBED_EventInformation *event)
581 {
582   GNUNET_break (0);
583 }
584
585
586
587 /**
588  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
589  *
590  * @param cls NULL
591  * @param client identification of the client
592  * @param message the actual message
593  */
594 static void 
595 handle_init (void *cls,
596              struct GNUNET_SERVER_Client *client,
597              const struct GNUNET_MessageHeader *message)
598 {
599   const struct GNUNET_TESTBED_InitMessage *msg;
600   struct GNUNET_TESTBED_Host *host;
601
602   if (NULL != master_context)
603   {
604     GNUNET_break (0);
605     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
606     return;
607   }
608   msg = (const struct GNUNET_TESTBED_InitMessage *) message;  
609   master_context = GNUNET_malloc (sizeof (struct Context));
610   master_context->client = client;
611   master_context->host_id = ntohl (msg->host_id);
612   host = GNUNET_TESTBED_host_create_with_id (master_context->host_id,
613                                              NULL, NULL, 0);
614   host_list_add (host);
615   master_context->event_mask = GNUNET_ntohll (msg->event_mask);
616   GNUNET_SERVER_client_keep (client);
617   LOG_DEBUG ("Created master context with host ID: %u\n",
618              master_context->host_id);
619   GNUNET_SERVER_receive_done (client, GNUNET_OK);
620 }
621
622
623 /**
624  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
625  *
626  * @param cls NULL
627  * @param client identification of the client
628  * @param message the actual message
629  */
630 static void 
631 handle_add_host (void *cls,
632                  struct GNUNET_SERVER_Client *client,
633                  const struct GNUNET_MessageHeader *message)
634 {
635   struct GNUNET_TESTBED_Host *host;
636   const struct GNUNET_TESTBED_AddHostMessage *msg;
637   struct GNUNET_TESTBED_HostConfirmedMessage *reply;
638   char *username;
639   char *hostname;
640   char *emsg;
641   uint32_t host_id;
642   uint16_t username_length;
643   uint16_t hostname_length;
644   uint16_t reply_size;
645   
646   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
647   username_length = ntohs (msg->user_name_length);
648   username_length = (0 == username_length) ? 0 : username_length + 1;
649   username = (char *) &(msg[1]);
650   hostname = username + username_length;
651   if (ntohs (message->size) <=
652       (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length))
653   {
654     GNUNET_break (0);
655     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
656     return;
657   }
658   hostname_length = ntohs (message->size)
659     - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
660   if (strlen (hostname) != hostname_length)
661   {
662     GNUNET_break (0);
663     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
664     return;
665   }
666   host_id = ntohl (msg->host_id);
667   LOG_DEBUG ("Received ADDHOST message\n");
668   LOG_DEBUG ("-------host id: %u\n", host_id);
669   if (NULL != hostname) LOG_DEBUG ("-------hostname: %s\n", hostname);
670   if (NULL != username) LOG_DEBUG ("-------username: %s\n", username);
671   LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
672   host = GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
673                                              ntohs (msg->ssh_port));
674   GNUNET_SERVER_receive_done (client, GNUNET_OK);
675   reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
676   if (GNUNET_OK != host_list_add (host))
677   {    
678     /* We are unable to add a host */  
679     emsg = "A host exists with given host-id";
680     LOG_DEBUG ("%s: %u", emsg, host_id);
681     GNUNET_TESTBED_host_destroy (host);
682     reply_size += strlen (emsg) + 1;
683     reply = GNUNET_malloc (reply_size);
684     memcpy (&reply[1], emsg, strlen (emsg) + 1);
685   }
686   else
687     reply = GNUNET_malloc (reply_size);  
688   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADDHOSTCONFIRM);
689   reply->header.size = htons (reply_size);
690   reply->host_id = htonl (host_id);  
691   queue_message (client, (struct GNUNET_MessageHeader *) reply);
692 }
693
694
695 /**
696  * Iterator over hash map entries.
697  *
698  * @param cls closure
699  * @param key current key code
700  * @param value value in the hash map
701  * @return GNUNET_YES if we should continue to
702  *         iterate,
703  *         GNUNET_NO if not.
704  */
705 int ss_exists_iterator (void *cls,
706                         const struct GNUNET_HashCode * key,
707                         void *value)
708 {
709   struct SharedService *queried_ss = cls;
710   struct SharedService *ss = value;
711
712   if (0 == strcmp (ss->name, queried_ss->name))
713     return GNUNET_NO;
714   else
715     return GNUNET_YES;
716 }
717
718 /**
719  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
720  *
721  * @param cls NULL
722  * @param client identification of the client
723  * @param message the actual message
724  */
725 static void 
726 handle_configure_shared_service (void *cls,
727                                  struct GNUNET_SERVER_Client *client,
728                                  const struct GNUNET_MessageHeader *message)
729 {
730   const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
731   struct SharedService *ss;
732   char *service_name;
733   struct GNUNET_HashCode hash;
734   uint16_t msg_size;
735   uint16_t service_name_size;
736     
737   msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
738   msg_size = ntohs (message->size);
739   if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
740   {
741     GNUNET_break (0);
742     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
743     return;
744   }
745   service_name_size = msg_size - 
746     sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
747   service_name = (char *) &msg[1];
748   if ('\0' != service_name[service_name_size - 1])
749   {
750     GNUNET_break (0);
751     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
752     return;
753   }
754   LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
755              service_name, ntohl (msg->num_peers));
756   if (ntohl (msg->host_id) != master_context->host_id)
757   {
758     route_message (ntohl (msg->host_id), message);
759     GNUNET_SERVER_receive_done (client, GNUNET_OK);
760     return;
761   }
762   GNUNET_SERVER_receive_done (client, GNUNET_OK);
763   ss = GNUNET_malloc (sizeof (struct SharedService));
764   ss->name = strdup (service_name);
765   ss->num_shared = ntohl (msg->num_peers);
766   GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
767   if (GNUNET_SYSERR == 
768       GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
769                                                   &ss_exists_iterator, ss))
770   {
771     LOG (GNUNET_ERROR_TYPE_WARNING,
772          "Service %s already configured as a shared service. "
773          "Ignoring service sharing request \n", ss->name);
774     GNUNET_free (ss->name);
775     GNUNET_free (ss);
776     return;
777   }
778   GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
779                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);  
780 }
781
782
783 /**
784  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
785  *
786  * @param cls NULL
787  * @param client identification of the client
788  * @param message the actual message
789  */
790 static void 
791 handle_link_controllers (void *cls,
792                          struct GNUNET_SERVER_Client *client,
793                          const struct GNUNET_MessageHeader *message)
794 {
795   const struct GNUNET_TESTBED_ControllerLinkMessage *msg;
796   struct GNUNET_CONFIGURATION_Handle *cfg;
797   struct LCFContextQueue *lcfq;
798   struct Route *route;
799   struct Route *new_route;
800   char *config;  
801   uLongf dest_size;
802   size_t config_size;
803   uint32_t delegated_host_id;
804   uint32_t slave_host_id;
805   uint16_t msize;
806    
807   if (NULL == master_context)
808   {
809     GNUNET_break (0);
810     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
811     return;
812   }
813   msize = ntohs (message->size);
814   if (sizeof (struct GNUNET_TESTBED_ControllerLinkMessage) >= msize)
815   {
816     GNUNET_break (0);
817     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
818     return;
819   }
820   msg = (const struct GNUNET_TESTBED_ControllerLinkMessage *) message;
821   delegated_host_id = ntohl (msg->delegated_host_id);
822   if (delegated_host_id == master_context->host_id)
823   {
824     GNUNET_break (0);
825     LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
826     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
827     return;
828   }
829   if ((delegated_host_id >= host_list_size) || 
830       (NULL == host_list[delegated_host_id]))
831   {
832     LOG (GNUNET_ERROR_TYPE_WARNING, "Delegated host not registered with us\n");
833     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
834     return;
835   }
836   slave_host_id = ntohl (msg->slave_host_id);
837   if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
838   {
839     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
840     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
841     return;
842   }
843   if (slave_host_id == delegated_host_id)
844   {
845     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
846     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
847     return;
848   }
849   msize -= sizeof (struct GNUNET_TESTBED_ControllerLinkMessage);
850   config_size = ntohs (msg->config_size);
851   
852   if (slave_host_id == master_context->host_id) /* Link from us */
853   {
854     struct Slave *slave;
855
856     if ((delegated_host_id < slave_list_size) && 
857         (NULL != slave_list[delegated_host_id])) /* We have already added */
858     {
859       LOG (GNUNET_ERROR_TYPE_WARNING, "Host %u already connected\n",
860            delegated_host_id);
861       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
862       return;
863     }    
864     config = GNUNET_malloc (config_size);
865     dest_size = (uLongf) config_size;    
866     if (Z_OK != uncompress ((Bytef *) config, &dest_size,
867                             (const Bytef *) &msg[1], (uLong) msize))
868     {
869       GNUNET_break (0);           /* Compression error */
870       GNUNET_free (config);
871       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
872       return;
873     }
874     if (config_size == dest_size)
875     {
876       LOG (GNUNET_ERROR_TYPE_WARNING, "Uncompressed config size mismatch\n");
877       GNUNET_free (config);
878       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
879     }
880     cfg = GNUNET_CONFIGURATION_create (); /* Free here or in lcfcontext */
881     if (GNUNET_OK != GNUNET_CONFIGURATION_deserialize (cfg, config, config_size,
882                                                        GNUNET_NO))
883     {
884       GNUNET_break (0);           /* Configuration parsing error */
885       GNUNET_free (config);
886       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
887       return;
888     }
889     GNUNET_free (config);
890     if (delegated_host_id >= slave_list_size)
891     {
892       slave_list_size += LIST_GROW_STEP;
893       slave_list = GNUNET_realloc (slave_list,
894                                    sizeof (struct Slave *) * slave_list_size);
895     }
896     slave = GNUNET_malloc (sizeof (struct Slave));
897     slave->host_id = delegated_host_id;
898     slave_list[delegated_host_id] = slave;
899     if (1 == msg->is_subordinate)
900     {
901       slave->controller_proc =
902         GNUNET_TESTBED_controller_start (host_list[delegated_host_id]);
903     }
904     slave->controller =
905       GNUNET_TESTBED_controller_connect (cfg, host_list[delegated_host_id],
906                                          master_context->event_mask,
907                                          &slave_event_callback, slave);
908     GNUNET_CONFIGURATION_destroy (cfg);
909     new_route = GNUNET_malloc (sizeof (struct Route));
910     new_route->dest = delegated_host_id;
911     new_route->thru = master_context->host_id;
912     route_list_add (new_route);
913     GNUNET_SERVER_receive_done (client, GNUNET_OK);
914     return;
915   }
916
917   /* Route the request */
918   if (slave_host_id >= route_list_size)
919   {
920     LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
921     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
922     return;
923   }
924   while (NULL != (route = route_list[slave_host_id]))
925   {
926     if (route->thru == master_context->host_id)
927       break;
928     slave_host_id = route->thru;
929   }
930   GNUNET_assert (NULL != route); /* because we add routes carefully */
931   GNUNET_assert (route->dest < slave_list_size);
932   GNUNET_assert (NULL != slave_list[route->dest]);  
933   lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
934   lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
935   lcfq->lcf->delegated_host_id = delegated_host_id;
936   lcfq->lcf->is_subordinate =
937     (1 == msg->is_subordinate) ? GNUNET_YES : GNUNET_NO;
938   lcfq->lcf->state = INIT;
939   lcfq->lcf->gateway = slave_list[route->dest];
940   lcfq->lcf->sxcfg_size = msize;
941   lcfq->lcf->sxcfg = GNUNET_malloc (msize);
942   lcfq->lcf->scfg_size = config_size;
943   (void) memcpy (lcfq->lcf->sxcfg, &msg[1], msize);
944   if (NULL == lcfq_head)
945   {
946     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
947     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
948     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq);
949   }
950   else
951     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
952   GNUNET_SERVER_receive_done (client, GNUNET_OK);
953   new_route = GNUNET_malloc (sizeof (struct Route));
954   new_route->dest = delegated_host_id;
955   new_route->thru = route->dest;
956   route_list_add (new_route);
957 }
958
959
960 /**
961  * Iterator over hash map entries.
962  *
963  * @param cls closure
964  * @param key current key code
965  * @param value value in the hash map
966  * @return GNUNET_YES if we should continue to
967  *         iterate,
968  *         GNUNET_NO if not.
969  */
970 static int 
971 ss_map_free_iterator (void *cls,
972                       const struct GNUNET_HashCode * key, void *value)
973 {
974   struct SharedService *ss = value;
975
976   GNUNET_assert (GNUNET_YES ==
977                  GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
978   GNUNET_free (ss->name);
979   GNUNET_free (ss);
980   return GNUNET_YES;
981 }
982
983
984 /**
985  * Task to clean up and shutdown nicely
986  *
987  * @param cls NULL
988  * @param tc the TaskContext from scheduler
989  */
990 static void
991 shutdown_task (void *cls,
992                const struct GNUNET_SCHEDULER_TaskContext *tc)
993 {
994   struct LCFContextQueue *lcfq;
995   uint32_t id;
996
997   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
998   GNUNET_SCHEDULER_shutdown ();
999   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
1000   (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
1001                                                 NULL);
1002   GNUNET_CONTAINER_multihashmap_destroy (ss_map);
1003   if (NULL != fh)
1004   {
1005     GNUNET_DISK_file_close (fh);
1006     fh = NULL;
1007   }
1008   if (NULL != lcfq_head)
1009   {
1010     if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
1011     {
1012       GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
1013       lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
1014     }
1015     if (NULL != lcfq_head->lcf->rhandle)
1016       GNUNET_TESTBED_cancel_registration (lcfq_head->lcf->rhandle);
1017   }
1018   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1019   for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
1020   {
1021     GNUNET_free (lcfq->lcf->sxcfg);
1022     GNUNET_free (lcfq->lcf);
1023     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
1024     GNUNET_free (lcfq);
1025   }
1026   /* Clear host list */
1027   for (id = 0; id < host_list_size; id++)
1028     if (NULL != host_list[id])
1029       GNUNET_TESTBED_host_destroy (host_list[id]);
1030   GNUNET_free_non_null (host_list);
1031   /* Clear route list */
1032   for (id = 0; id < route_list_size; id++)
1033     if (NULL != route_list[id])
1034       GNUNET_free (route_list[id]);
1035   GNUNET_free_non_null (route_list);
1036   /* Clear slave_list */
1037   for (id = 0; id < slave_list_size; id++)
1038     if (NULL != slave_list[id])
1039     {
1040       GNUNET_assert (NULL != slave_list[id]->controller);
1041       GNUNET_TESTBED_controller_disconnect (slave_list[id]->controller);
1042       if (NULL != slave_list[id]->controller_proc)
1043         GNUNET_TESTBED_controller_stop (slave_list[id]->controller_proc);
1044     }
1045   GNUNET_free_non_null (master_context);
1046 }
1047
1048
1049 /**
1050  * Callback for client disconnect
1051  *
1052  * @param cls NULL
1053  * @param client the client which has disconnected
1054  */
1055 static void
1056 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
1057 {
1058   if (NULL == master_context)
1059     return;
1060   if (client == master_context->client)
1061   {
1062     LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
1063     GNUNET_SERVER_client_drop (client);
1064     /* should not be needed as we're terminated by failure to read
1065        from stdin, but if stdin fails for some reason, this shouldn't 
1066        hurt for now --- might need to revise this later if we ever
1067        decide that master connections might be temporarily down 
1068        for some reason */
1069     GNUNET_SCHEDULER_shutdown ();
1070   }
1071 }
1072
1073
1074 /**
1075  * Testbed setup
1076  *
1077  * @param cls closure
1078  * @param server the initialized server
1079  * @param cfg configuration to use
1080  */
1081 static void 
1082 testbed_run (void *cls,
1083              struct GNUNET_SERVER_Handle *server,
1084              const struct GNUNET_CONFIGURATION_Handle *cfg)
1085 {
1086   static const struct GNUNET_SERVER_MessageHandler message_handlers[] =
1087     {
1088       {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT,
1089        sizeof (struct GNUNET_TESTBED_InitMessage)},
1090       {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST, 0},
1091       {&handle_configure_shared_service, NULL,
1092        GNUNET_MESSAGE_TYPE_TESTBED_SERVICESHARE, 0},
1093       {&handle_link_controllers, NULL,
1094        GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS, 0},
1095       {NULL}
1096     };
1097
1098   GNUNET_SERVER_add_handlers (server,
1099                               message_handlers);
1100   GNUNET_SERVER_disconnect_notify (server,
1101                                    &client_disconnect_cb,
1102                                    NULL);
1103   ss_map = GNUNET_CONTAINER_multihashmap_create (5);
1104   fh = GNUNET_DISK_get_handle_from_native (stdin);
1105   if (NULL == fh)
1106     shutdown_task_id = 
1107       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,                                  
1108                                     &shutdown_task,
1109                                     NULL);
1110   else
1111     shutdown_task_id = 
1112       GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
1113                                       fh,
1114                                       &shutdown_task,
1115                                       NULL);
1116 }
1117
1118
1119 /**
1120  * The starting point of execution
1121  */
1122 int main (int argc, char *const *argv)
1123 {
1124   return
1125     (GNUNET_OK ==
1126      GNUNET_SERVICE_run (argc,
1127                          argv,
1128                          "testbed",
1129                          GNUNET_SERVICE_OPTION_NONE,
1130                          &testbed_run,
1131                          NULL)) ? 0 : 1;
1132 }