2e1af39b9db5a855171df857810f58e86c38f8db
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
1 /*
2   This file is part of GNUnet.
3   (C) 2008--2013 Christian Grothoff (and other contributing authors)
4
5   GNUnet is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published
7   by the Free Software Foundation; either version 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 "gnunet-service-testbed.h"
28
29 #include <zlib.h>
30
31
32 /***********/
33 /* Globals */
34 /***********/
35
36 /**
37  * Our configuration
38  */
39 struct GNUNET_CONFIGURATION_Handle *our_config;
40
41 /**
42  * The master context; generated with the first INIT message
43  */
44 struct Context *GST_context;
45
46 /**
47  * A list of directly linked neighbours
48  */
49 struct Slave **GST_slave_list;
50
51 /**
52  * A list of peers we know about
53  */
54 struct Peer **GST_peer_list;
55
56 /**
57  * Array of hosts
58  */
59 struct GNUNET_TESTBED_Host **GST_host_list;
60
61 /**
62  * DLL head for forwarded operation contexts
63  */
64 struct ForwardedOperationContext *fopcq_head;
65
66 /**
67  * DLL tail for forwarded operation contexts
68  */
69 struct ForwardedOperationContext *fopcq_tail;
70
71 /**
72  * Operation queue for open file descriptors
73  */
74 struct OperationQueue *GST_opq_openfds;
75
76 /**
77  * Timeout for operations which may take some time
78  */
79 const struct GNUNET_TIME_Relative GST_timeout;
80
81 /**
82  * The size of the host list
83  */
84 unsigned int GST_host_list_size;
85
86 /**
87  * The size of directly linked neighbours list
88  */
89 unsigned int GST_slave_list_size;
90
91 /**
92  * The size of the peer list
93  */
94 unsigned int GST_peer_list_size;
95
96
97 /***********************************/
98 /* Local definitions and variables */
99 /***********************************/
100
101 /**
102  * The message queue for sending messages to clients
103  */
104 struct MessageQueue
105 {
106   /**
107    * The message to be sent
108    */
109   struct GNUNET_MessageHeader *msg;
110
111   /**
112    * The client to send the message to
113    */
114   struct GNUNET_SERVER_Client *client;
115
116   /**
117    * next pointer for DLL
118    */
119   struct MessageQueue *next;
120
121   /**
122    * prev pointer for DLL
123    */
124   struct MessageQueue *prev;
125 };
126
127 /**
128  * Our hostname; we give this to all the peers we start
129  */
130 static char *hostname;
131
132 /**
133  * Current Transmit Handle; NULL if no notify transmit exists currently
134  */
135 static struct GNUNET_SERVER_TransmitHandle *transmit_handle;
136
137 /**
138  * The head for the LCF queue
139  */
140 static struct LCFContextQueue *lcfq_head;
141
142 /**
143  * The tail for the LCF queue
144  */
145 static struct LCFContextQueue *lcfq_tail;
146
147 /**
148  * The message queue head
149  */
150 static struct MessageQueue *mq_head;
151
152 /**
153  * The message queue tail
154  */
155 static struct MessageQueue *mq_tail;
156
157 /**
158  * The hashmap of shared services
159  */
160 static struct GNUNET_CONTAINER_MultiHashMap *ss_map;
161
162 /**
163  * A list of routes
164  */
165 static struct Route **route_list;
166
167 /**
168  * The event mask for the events we listen from sub-controllers
169  */
170 static uint64_t event_mask;
171
172 /**
173  * The size of the route list
174  */
175 static unsigned int route_list_size;
176
177 /**
178  * The lcf_task handle
179  */
180 static GNUNET_SCHEDULER_TaskIdentifier lcf_proc_task_id;
181
182 /**
183  * The shutdown task handle
184  */
185 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task_id;
186
187
188 /**
189  * Function called to notify a client about the connection begin ready to queue
190  * more data.  "buf" will be NULL and "size" zero if the connection was closed
191  * for writing in the meantime.
192  *
193  * @param cls NULL
194  * @param size number of bytes available in buf
195  * @param buf where the callee should write the message
196  * @return number of bytes written to buf
197  */
198 static size_t
199 transmit_ready_notify (void *cls, size_t size, void *buf)
200 {
201   struct MessageQueue *mq_entry;
202
203   transmit_handle = NULL;
204   mq_entry = mq_head;
205   GNUNET_assert (NULL != mq_entry);
206   if (0 == size)
207     return 0;
208   GNUNET_assert (ntohs (mq_entry->msg->size) <= size);
209   size = ntohs (mq_entry->msg->size);
210   memcpy (buf, mq_entry->msg, size);
211   GNUNET_free (mq_entry->msg);
212   GNUNET_SERVER_client_drop (mq_entry->client);
213   GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
214   GNUNET_free (mq_entry);
215   mq_entry = mq_head;
216   if (NULL != mq_entry)
217     transmit_handle =
218         GNUNET_SERVER_notify_transmit_ready (mq_entry->client,
219                                              ntohs (mq_entry->msg->size),
220                                              GNUNET_TIME_UNIT_FOREVER_REL,
221                                              &transmit_ready_notify, NULL);
222   return size;
223 }
224
225
226 /**
227  * Queues a message in send queue for sending to the service
228  *
229  * @param client the client to whom the queued message has to be sent
230  * @param msg the message to queue
231  */
232 void
233 GST_queue_message (struct GNUNET_SERVER_Client *client,
234                    struct GNUNET_MessageHeader *msg)
235 {
236   struct MessageQueue *mq_entry;
237   uint16_t type;
238   uint16_t size;
239
240   type = ntohs (msg->type);
241   size = ntohs (msg->size);
242   GNUNET_assert ((GNUNET_MESSAGE_TYPE_TESTBED_INIT <= type) &&
243                  (GNUNET_MESSAGE_TYPE_TESTBED_MAX > type));
244   mq_entry = GNUNET_malloc (sizeof (struct MessageQueue));
245   mq_entry->msg = msg;
246   mq_entry->client = client;
247   GNUNET_SERVER_client_keep (client);
248   LOG_DEBUG ("Queueing message of type %u, size %u for sending\n", type,
249              ntohs (msg->size));
250   GNUNET_CONTAINER_DLL_insert_tail (mq_head, mq_tail, mq_entry);
251   if (NULL == transmit_handle)
252     transmit_handle =
253         GNUNET_SERVER_notify_transmit_ready (client, size,
254                                              GNUNET_TIME_UNIT_FOREVER_REL,
255                                              &transmit_ready_notify, NULL);
256 }
257
258
259 /**
260  * Similar to GNUNET_array_grow(); however instead of calling GNUNET_array_grow()
261  * several times we call it only once. The array is also made to grow in steps
262  * of LIST_GROW_STEP.
263  *
264  * @param ptr the array pointer to grow
265  * @param size the size of array
266  * @param accommodate_size the size which the array has to accommdate; after
267  *          this call the array will be big enough to accommdate sizes upto
268  *          accommodate_size
269  */
270 #define array_grow_large_enough(ptr, size, accommodate_size) \
271   do                                                                    \
272   {                                                                     \
273     unsigned int growth_size;                                           \
274     GNUNET_assert (size <= accommodate_size);                            \
275     growth_size = size;                                                 \
276     while (growth_size <= accommodate_size)                             \
277       growth_size += LIST_GROW_STEP;                                    \
278     GNUNET_array_grow (ptr, size, growth_size);                         \
279     GNUNET_assert (size > accommodate_size);                            \
280   } while (0)
281
282
283 /**
284  * Function to add a host to the current list of known hosts
285  *
286  * @param host the host to add
287  * @return GNUNET_OK on success; GNUNET_SYSERR on failure due to host-id
288  *           already in use
289  */
290 static int
291 host_list_add (struct GNUNET_TESTBED_Host *host)
292 {
293   uint32_t host_id;
294
295   host_id = GNUNET_TESTBED_host_get_id_ (host);
296   if (GST_host_list_size <= host_id)
297     array_grow_large_enough (GST_host_list, GST_host_list_size, host_id);
298   if (NULL != GST_host_list[host_id])
299   {
300     LOG_DEBUG ("A host with id: %u already exists\n", host_id);
301     return GNUNET_SYSERR;
302   }
303   GST_host_list[host_id] = host;
304   return GNUNET_OK;
305 }
306
307
308 /**
309  * Adds a route to the route list
310  *
311  * @param route the route to add
312  */
313 static void
314 route_list_add (struct Route *route)
315 {
316   if (route->dest >= route_list_size)
317     array_grow_large_enough (route_list, route_list_size, route->dest);
318   GNUNET_assert (NULL == route_list[route->dest]);
319   route_list[route->dest] = route;
320 }
321
322
323 /**
324  * Adds a slave to the slave array
325  *
326  * @param slave the slave controller to add
327  */
328 static void
329 slave_list_add (struct Slave *slave)
330 {
331   if (slave->host_id >= GST_slave_list_size)
332     array_grow_large_enough (GST_slave_list, GST_slave_list_size,
333                              slave->host_id);
334   GNUNET_assert (NULL == GST_slave_list[slave->host_id]);
335   GST_slave_list[slave->host_id] = slave;
336 }
337
338
339 /**
340  * Adds a peer to the peer array
341  *
342  * @param peer the peer to add
343  */
344 static void
345 peer_list_add (struct Peer *peer)
346 {
347   if (peer->id >= GST_peer_list_size)
348     array_grow_large_enough (GST_peer_list, GST_peer_list_size, peer->id);
349   GNUNET_assert (NULL == GST_peer_list[peer->id]);
350   GST_peer_list[peer->id] = peer;
351 }
352
353
354 /**
355  * Removes a the give peer from the peer array
356  *
357  * @param peer the peer to be removed
358  */
359 static void
360 peer_list_remove (struct Peer *peer)
361 {
362   unsigned int orig_size;
363   uint32_t id;
364
365   GST_peer_list[peer->id] = NULL;
366   orig_size = GST_peer_list_size;
367   while (GST_peer_list_size >= LIST_GROW_STEP)
368   {
369     for (id = GST_peer_list_size - 1;
370          (id >= GST_peer_list_size - LIST_GROW_STEP) && (id != UINT32_MAX);
371          id--)
372       if (NULL != GST_peer_list[id])
373         break;
374     if (id != ((GST_peer_list_size - LIST_GROW_STEP) - 1))
375       break;
376     GST_peer_list_size -= LIST_GROW_STEP;
377   }
378   if (orig_size == GST_peer_list_size)
379     return;
380   GST_peer_list =
381       GNUNET_realloc (GST_peer_list,
382                       sizeof (struct Peer *) * GST_peer_list_size);
383 }
384
385
386 /**
387  * Finds the route with directly connected host as destination through which
388  * the destination host can be reached
389  *
390  * @param host_id the id of the destination host
391  * @return the route with directly connected destination host; NULL if no route
392  *           is found
393  */
394 struct Route *
395 GST_find_dest_route (uint32_t host_id)
396 {
397   struct Route *route;
398
399   if (route_list_size <= host_id)
400     return NULL;
401   while (NULL != (route = route_list[host_id]))
402   {
403     if (route->thru == GST_context->host_id)
404       break;
405     host_id = route->thru;
406   }
407   return route;
408 }
409
410
411 /**
412  * Routes message to a host given its host_id
413  *
414  * @param host_id the id of the destination host
415  * @param msg the message to be routed
416  */
417 static void
418 route_message (uint32_t host_id, const struct GNUNET_MessageHeader *msg)
419 {
420   GNUNET_break (0);
421 }
422
423
424 /**
425  * Send operation failure message to client
426  *
427  * @param client the client to which the failure message has to be sent to
428  * @param operation_id the id of the failed operation
429  * @param emsg the error message; can be NULL
430  */
431 void
432 GST_send_operation_fail_msg (struct GNUNET_SERVER_Client *client,
433                              uint64_t operation_id, const char *emsg)
434 {
435   struct GNUNET_TESTBED_OperationFailureEventMessage *msg;
436   uint16_t msize;
437   uint16_t emsg_len;
438
439   msize = sizeof (struct GNUNET_TESTBED_OperationFailureEventMessage);
440   emsg_len = (NULL == emsg) ? 0 : strlen (emsg) + 1;
441   msize += emsg_len;
442   msg = GNUNET_malloc (msize);
443   msg->header.size = htons (msize);
444   msg->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_OPERATION_FAIL_EVENT);
445   msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
446   msg->operation_id = GNUNET_htonll (operation_id);
447   if (0 != emsg_len)
448     memcpy (&msg[1], emsg, emsg_len);
449   GST_queue_message (client, &msg->header);
450 }
451
452
453 /**
454  * Function to send generic operation success message to given client
455  *
456  * @param client the client to send the message to
457  * @param operation_id the id of the operation which was successful
458  */
459 static void
460 send_operation_success_msg (struct GNUNET_SERVER_Client *client,
461                             uint64_t operation_id)
462 {
463   struct GNUNET_TESTBED_GenericOperationSuccessEventMessage *msg;
464   uint16_t msize;
465
466   msize = sizeof (struct GNUNET_TESTBED_GenericOperationSuccessEventMessage);
467   msg = GNUNET_malloc (msize);
468   msg->header.size = htons (msize);
469   msg->header.type =
470       htons (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS);
471   msg->operation_id = GNUNET_htonll (operation_id);
472   msg->event_type = htonl (GNUNET_TESTBED_ET_OPERATION_FINISHED);
473   GST_queue_message (client, &msg->header);
474 }
475
476
477 /**
478  * Function to send a failure reponse for controller link operation
479  *
480  * @param client the client to send the message to
481  * @param operation_id the operation ID of the controller link request
482  * @param cfg the configuration with which the delegated controller is started.
483  *          Can be NULL if the delegated controller is not started but just
484  *          linked to.
485  * @param emsg set to an error message explaining why the controller link
486  *          failed.  Setting this to NULL signifies success.  !This should be
487  *          NULL if cfg is set!
488  */
489 static void
490 send_controller_link_response (struct GNUNET_SERVER_Client *client,
491                                uint64_t operation_id,
492                                const struct GNUNET_CONFIGURATION_Handle
493                                *cfg,
494                                const char *emsg)
495 {
496   struct GNUNET_TESTBED_ControllerLinkResponse *msg;
497   char *xconfig;
498   size_t config_size;
499   size_t xconfig_size;  
500   uint16_t msize;
501
502   GNUNET_assert ((NULL == cfg) || (NULL == emsg));
503   xconfig = NULL;
504   xconfig_size = 0;
505   config_size = 0;
506   msize = sizeof (struct GNUNET_TESTBED_ControllerLinkResponse);
507   if (NULL != cfg)
508   {
509     xconfig = GNUNET_TESTBED_compress_cfg_ (cfg,
510                                             &config_size,
511                                             &xconfig_size);
512     msize += xconfig_size;
513   }
514   if (NULL != emsg)
515     msize += strlen (emsg);
516   msg = GNUNET_malloc (msize);
517   msg->header.type = htons
518       (GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS_RESULT);
519   msg->header.size = htons (msize);
520   if (NULL == emsg)
521     msg->success = htons (GNUNET_YES);
522   msg->operation_id = GNUNET_htonll (operation_id);
523   msg->config_size = htons ((uint16_t) config_size);
524   if (NULL != xconfig)
525     memcpy (&msg[1], xconfig, xconfig_size);
526   if (NULL != emsg)
527     memcpy (&msg[1], emsg, strlen (emsg));
528   GST_queue_message (client, &msg->header);
529 }
530
531 /**
532  * Callback which will be called after a host registration succeeded or failed
533  *
534  * @param cls the handle to the slave at which the registration is completed
535  * @param emsg the error message; NULL if host registration is successful
536  */
537 static void
538 hr_completion (void *cls, const char *emsg);
539
540
541 /**
542  * Attempts to register the next host in the host registration queue
543  *
544  * @param slave the slave controller whose host registration queue is checked
545  *          for host registrations
546  */
547 static void
548 register_next_host (struct Slave *slave)
549 {
550   struct HostRegistration *hr;
551
552   hr = slave->hr_dll_head;
553   GNUNET_assert (NULL != hr);
554   GNUNET_assert (NULL == slave->rhandle);
555   LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u\n",
556        GNUNET_TESTBED_host_get_id_ (hr->host),
557        GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
558   slave->rhandle =
559       GNUNET_TESTBED_register_host (slave->controller, hr->host, hr_completion,
560                                     slave);
561 }
562
563
564 /**
565  * Callback which will be called to after a host registration succeeded or failed
566  *
567  * @param cls the handle to the slave at which the registration is completed
568  * @param emsg the error message; NULL if host registration is successful
569  */
570 static void
571 hr_completion (void *cls, const char *emsg)
572 {
573   struct Slave *slave = cls;
574   struct HostRegistration *hr;
575
576   slave->rhandle = NULL;
577   hr = slave->hr_dll_head;
578   GNUNET_assert (NULL != hr);
579   LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering host %u at %u successful\n",
580        GNUNET_TESTBED_host_get_id_ (hr->host),
581        GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
582   GNUNET_CONTAINER_DLL_remove (slave->hr_dll_head, slave->hr_dll_tail, hr);
583   if (NULL != hr->cb)
584     hr->cb (hr->cb_cls, emsg);
585   GNUNET_free (hr);
586   if (NULL != slave->hr_dll_head)
587     register_next_host (slave);
588 }
589
590
591 /**
592  * Adds a host registration's request to a slave's registration queue
593  *
594  * @param slave the slave controller at which the given host has to be
595  *          registered
596  * @param cb the host registration completion callback
597  * @param cb_cls the closure for the host registration completion callback
598  * @param host the host which has to be registered
599  */
600 void
601 GST_queue_host_registration (struct Slave *slave,
602                              GNUNET_TESTBED_HostRegistrationCompletion cb,
603                              void *cb_cls, struct GNUNET_TESTBED_Host *host)
604 {
605   struct HostRegistration *hr;
606   int call_register;
607
608   LOG (GNUNET_ERROR_TYPE_DEBUG,
609        "Queueing host registration for host %u at %u\n",
610        GNUNET_TESTBED_host_get_id_ (host),
611        GNUNET_TESTBED_host_get_id_ (GST_host_list[slave->host_id]));
612   hr = GNUNET_malloc (sizeof (struct HostRegistration));
613   hr->cb = cb;
614   hr->cb_cls = cb_cls;
615   hr->host = host;
616   call_register = (NULL == slave->hr_dll_head) ? GNUNET_YES : GNUNET_NO;
617   GNUNET_CONTAINER_DLL_insert_tail (slave->hr_dll_head, slave->hr_dll_tail, hr);
618   if (GNUNET_YES == call_register)
619     register_next_host (slave);
620 }
621
622
623 /**
624  * The  Link Controller forwarding task
625  *
626  * @param cls the LCFContext
627  * @param tc the Task context from scheduler
628  */
629 static void
630 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
631
632
633 /**
634  * Completion callback for host registrations while forwarding Link Controller messages
635  *
636  * @param cls the LCFContext
637  * @param emsg the error message; NULL if host registration is successful
638  */
639 static void
640 lcf_proc_cc (void *cls, const char *emsg)
641 {
642   struct LCFContext *lcf = cls;
643
644   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
645   switch (lcf->state)
646   {
647   case INIT:
648     if (NULL != emsg)
649       goto registration_error;
650     lcf->state = DELEGATED_HOST_REGISTERED;
651     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
652     break;
653   case DELEGATED_HOST_REGISTERED:
654     if (NULL != emsg)
655       goto registration_error;
656     lcf->state = SLAVE_HOST_REGISTERED;
657     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
658     break;
659   default:
660     GNUNET_assert (0);          /* Shouldn't reach here */
661   }
662   return;
663
664 registration_error:
665   LOG (GNUNET_ERROR_TYPE_WARNING, "Host registration failed with message: %s\n",
666        emsg);
667   lcf->state = FINISHED;
668   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
669 }
670
671
672 /**
673  * Callback to relay the reply msg of a forwarded operation back to the client
674  *
675  * @param cls ForwardedOperationContext
676  * @param msg the message to relay
677  */
678 void
679 GST_forwarded_operation_reply_relay (void *cls,
680                                      const struct GNUNET_MessageHeader *msg)
681 {
682   struct ForwardedOperationContext *fopc = cls;
683   struct GNUNET_MessageHeader *dup_msg;
684   uint16_t msize;
685
686   msize = ntohs (msg->size);
687   LOG_DEBUG ("Relaying message with type: %u, size: %u\n", ntohs (msg->type),
688              msize);
689   dup_msg = GNUNET_copy_message (msg);
690   GST_queue_message (fopc->client, dup_msg);
691   GNUNET_SERVER_client_drop (fopc->client);
692   GNUNET_SCHEDULER_cancel (fopc->timeout_task);
693   GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
694   GNUNET_free (fopc);
695 }
696
697
698 /**
699  * Task to free resources when forwarded operation has been timedout
700  *
701  * @param cls the ForwardedOperationContext
702  * @param tc the task context from scheduler
703  */
704 void
705 GST_forwarded_operation_timeout (void *cls,
706                                  const struct GNUNET_SCHEDULER_TaskContext *tc)
707 {
708   struct ForwardedOperationContext *fopc = cls;
709
710   GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
711   LOG (GNUNET_ERROR_TYPE_WARNING, "A forwarded operation has timed out\n");
712   GST_send_operation_fail_msg (fopc->client, fopc->operation_id, "Timeout");
713   GNUNET_SERVER_client_drop (fopc->client);
714   GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
715   GNUNET_free (fopc);
716 }
717
718
719 /**
720  * The  Link Controller forwarding task
721  *
722  * @param cls the LCFContext
723  * @param tc the Task context from scheduler
724  */
725 static void
726 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
727
728
729 /**
730  * Task to free resources when forwarded link controllers has been timedout
731  *
732  * @param cls the LCFContext
733  * @param tc the task context from scheduler
734  */
735 static void
736 lcf_forwarded_operation_timeout (void *cls,
737                                  const struct GNUNET_SCHEDULER_TaskContext *tc)
738 {
739   struct LCFContext *lcf = cls;
740
741   lcf->timeout_task = GNUNET_SCHEDULER_NO_TASK;
742   //  GST_forwarded_operation_timeout (lcf->fopc, tc);
743   LOG (GNUNET_ERROR_TYPE_WARNING,
744        "A forwarded controller link operation has timed out\n");
745   send_controller_link_response (lcf->client, lcf->operation_id, NULL, "Timeout");
746   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
747   lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
748 }
749
750
751 /**
752  * The  Link Controller forwarding task
753  *
754  * @param cls the LCFContext
755  * @param tc the Task context from scheduler
756  */
757 static void
758 lcf_proc_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
759 {
760   struct LCFContext *lcf = cls;
761   struct LCFContextQueue *lcfq;
762
763   lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
764   switch (lcf->state)
765   {
766   case INIT:
767     if (GNUNET_NO ==
768         GNUNET_TESTBED_is_host_registered_ (GST_host_list
769                                             [lcf->delegated_host_id],
770                                             lcf->gateway->controller))
771     {
772       GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
773                                    GST_host_list[lcf->delegated_host_id]);
774     }
775     else
776     {
777       lcf->state = DELEGATED_HOST_REGISTERED;
778       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
779     }
780     break;
781   case DELEGATED_HOST_REGISTERED:
782     if (GNUNET_NO ==
783         GNUNET_TESTBED_is_host_registered_ (GST_host_list[lcf->slave_host_id],
784                                             lcf->gateway->controller))
785     {
786       GST_queue_host_registration (lcf->gateway, lcf_proc_cc, lcf,
787                                    GST_host_list[lcf->slave_host_id]);
788     }
789     else
790     {
791       lcf->state = SLAVE_HOST_REGISTERED;
792       lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
793     }
794     break;
795   case SLAVE_HOST_REGISTERED:
796     lcf->op = GNUNET_TESTBED_controller_link (lcf,
797                                               lcf->gateway->controller,
798                                               GST_host_list[lcf->delegated_host_id],
799                                               GST_host_list[lcf->slave_host_id],
800                                               NULL,
801                                               lcf->is_subordinate);
802     lcf->timeout_task =
803         GNUNET_SCHEDULER_add_delayed (GST_timeout, &lcf_forwarded_operation_timeout,
804                                       lcf);
805     lcf->state = FINISHED;
806     break;
807   case FINISHED:
808     lcfq = lcfq_head;
809     GNUNET_assert (lcfq->lcf == lcf);
810     GNUNET_assert (NULL != lcf->cfg);
811     GNUNET_CONFIGURATION_destroy (lcf->cfg);
812     GNUNET_SERVER_client_drop (lcf->client);
813     GNUNET_TESTBED_operation_done (lcf->op);
814     GNUNET_free (lcf);
815     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
816     GNUNET_free (lcfq);
817     if (NULL != lcfq_head)
818       lcf_proc_task_id =
819           GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq_head->lcf);
820   }
821 }
822
823
824 /**
825  * Callback for event from slave controllers
826  *
827  * @param cls struct Slave *
828  * @param event information about the event
829  */
830 static void
831 slave_event_callback (void *cls,
832                       const struct GNUNET_TESTBED_EventInformation *event)
833 {
834   struct RegisteredHostContext *rhc;
835   struct LCFContext *lcf;
836   struct GNUNET_CONFIGURATION_Handle *cfg;
837   struct GNUNET_TESTBED_Operation *old_op;
838
839   /* We currently only get here when working on RegisteredHostContexts and
840      LCFContexts */
841   GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
842   rhc = event->details.operation_finished.op_cls;
843   if (CLOSURE_TYPE_RHC == rhc->type)
844   {
845     GNUNET_assert (rhc->sub_op == event->details.operation_finished.operation);
846     switch (rhc->state)
847     {
848     case RHC_GET_CFG:
849       cfg = event->details.operation_finished.generic;
850       old_op = rhc->sub_op;
851       rhc->state = RHC_LINK;
852       rhc->sub_op =
853           GNUNET_TESTBED_controller_link (rhc, rhc->gateway->controller,
854                                           rhc->reg_host, rhc->host, cfg,
855                                           GNUNET_NO);
856       GNUNET_TESTBED_operation_done (old_op);
857       break;
858     case RHC_LINK:
859       LOG_DEBUG ("OL: Linking controllers successfull\n");
860       GNUNET_TESTBED_operation_done (rhc->sub_op);
861       rhc->sub_op = NULL;
862       rhc->state = RHC_OL_CONNECT;
863       GST_process_next_focc (rhc);
864       break;
865     default:
866       GNUNET_assert (0);
867     }
868     return;
869   }
870   lcf = event->details.operation_finished.op_cls;
871   if (CLOSURE_TYPE_LCF == lcf->type)
872   {    
873     GNUNET_assert (lcf->op == event->details.operation_finished.operation);
874     GNUNET_assert (FINISHED == lcf->state);
875     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != lcf->timeout_task);
876     GNUNET_SCHEDULER_cancel (lcf->timeout_task);
877     if (NULL == event->details.operation_finished.emsg)
878       send_controller_link_response (lcf->client, lcf->operation_id,
879                                      GNUNET_TESTBED_host_get_cfg_ 
880                                      (GST_host_list[lcf->delegated_host_id]),
881                                      NULL);
882     else
883       send_controller_link_response (lcf->client, lcf->operation_id,
884                                      NULL,
885                                      event->details.operation_finished.emsg);
886     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
887     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcf);
888     return;
889   }
890   GNUNET_assert (0);
891 }
892
893
894 /**
895  * Callback to signal successfull startup of the controller process
896  *
897  * @param cls the handle to the slave whose status is to be found here
898  * @param cfg the configuration with which the controller has been started;
899  *          NULL if status is not GNUNET_OK
900  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
901  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
902  */
903 static void
904 slave_status_callback (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
905                        int status)
906 {
907   struct Slave *slave = cls;
908   struct LinkControllersContext *lcc;
909
910   lcc = slave->lcc;
911   if (GNUNET_SYSERR == status)
912   {
913     slave->controller_proc = NULL;
914     GST_slave_list[slave->host_id] = NULL;
915     GNUNET_free (slave);
916     slave = NULL;
917     LOG (GNUNET_ERROR_TYPE_WARNING, "Unexpected slave shutdown\n");
918     GNUNET_SCHEDULER_shutdown ();       /* We too shutdown */
919     goto clean_lcc;
920   }
921   slave->controller =
922       GNUNET_TESTBED_controller_connect (cfg, GST_host_list[slave->host_id],
923                                          event_mask, &slave_event_callback,
924                                          slave);
925   if (NULL != slave->controller)
926   {
927     send_controller_link_response (lcc->client, lcc->operation_id, cfg, NULL);
928   }
929   else
930   {
931     send_controller_link_response (lcc->client, lcc->operation_id, NULL,
932                                    "Could not connect to delegated controller");
933     GNUNET_TESTBED_controller_stop (slave->controller_proc);
934     GST_slave_list[slave->host_id] = NULL;
935     GNUNET_free (slave);
936     slave = NULL;
937   }
938
939 clean_lcc:
940   if (NULL != lcc)
941   {
942     if (NULL != lcc->client)
943     {
944       GNUNET_SERVER_receive_done (lcc->client, GNUNET_OK);
945       GNUNET_SERVER_client_drop (lcc->client);
946       lcc->client = NULL;
947     }
948     GNUNET_free (lcc);
949   }
950   if (NULL != slave)
951     slave->lcc = NULL;
952 }
953
954
955 /**
956  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_INIT messages
957  *
958  * @param cls NULL
959  * @param client identification of the client
960  * @param message the actual message
961  */
962 static void
963 handle_init (void *cls, struct GNUNET_SERVER_Client *client,
964              const struct GNUNET_MessageHeader *message)
965 {
966   const struct GNUNET_TESTBED_InitMessage *msg;
967   struct GNUNET_TESTBED_Host *host;
968   const char *controller_hostname;
969   uint16_t msize;
970
971   if (NULL != GST_context)
972   {
973     LOG_DEBUG ("We are being connected to laterally\n");
974     GNUNET_SERVER_receive_done (client, GNUNET_OK);
975     return;
976   }
977   msg = (const struct GNUNET_TESTBED_InitMessage *) message;
978   msize = ntohs (message->size);
979   if (msize <= sizeof (struct GNUNET_TESTBED_InitMessage))
980   {
981     GNUNET_break (0);
982     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
983     return;
984   }
985   msize -= sizeof (struct GNUNET_TESTBED_InitMessage);
986   controller_hostname = (const char *) &msg[1];
987   if ('\0' != controller_hostname[msize - 1])
988   {
989     GNUNET_break (0);
990     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
991     return;
992   }
993   GST_context = GNUNET_malloc (sizeof (struct Context));
994   GNUNET_SERVER_client_keep (client);
995   GST_context->client = client;
996   GST_context->host_id = ntohl (msg->host_id);
997   GST_context->master_ip = GNUNET_strdup (controller_hostname);
998   LOG_DEBUG ("Our IP: %s\n", GST_context->master_ip);
999   GST_context->system =
1000       GNUNET_TESTING_system_create ("testbed", GST_context->master_ip,
1001                                     hostname);
1002   host =
1003       GNUNET_TESTBED_host_create_with_id (GST_context->host_id,
1004                                           GST_context->master_ip, NULL,
1005                                           our_config, 0);
1006   host_list_add (host);
1007   LOG_DEBUG ("Created master context with host ID: %u\n", GST_context->host_id);
1008   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1009 }
1010
1011
1012 /**
1013  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1014  *
1015  * @param cls NULL
1016  * @param client identification of the client
1017  * @param message the actual message
1018  */
1019 static void
1020 handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
1021                  const struct GNUNET_MessageHeader *message)
1022 {
1023   struct GNUNET_TESTBED_Host *host;
1024   const struct GNUNET_TESTBED_AddHostMessage *msg;
1025   struct GNUNET_TESTBED_HostConfirmedMessage *reply;
1026   struct GNUNET_CONFIGURATION_Handle *host_cfg;
1027   char *username;
1028   char *hostname;
1029   char *emsg;
1030   const void *ptr;
1031   uint32_t host_id;
1032   uint16_t username_length;
1033   uint16_t hostname_length;
1034   uint16_t reply_size;
1035   uint16_t msize;
1036
1037   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
1038   msize = ntohs (msg->header.size);
1039   if (msize <= sizeof (struct GNUNET_TESTBED_AddHostMessage))
1040   {
1041     GNUNET_break_op (0);
1042     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1043     return;
1044   }
1045   username_length = ntohs (msg->username_length);
1046   hostname_length = ntohs (msg->hostname_length);
1047   /* msg must contain hostname */
1048   if ((msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) + 
1049                  username_length))
1050       || (0 == hostname_length))
1051   {
1052     GNUNET_break_op (0);
1053     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1054     return;
1055   }
1056   /* msg must contain configuration */
1057   if (msize <= (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
1058                 username_length + hostname_length))
1059   {
1060     GNUNET_break_op (0);
1061     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1062     return;
1063   }
1064   username = NULL;
1065   hostname = NULL;
1066   ptr = &msg[1];
1067   if (0 != username_length)
1068   {
1069     username = GNUNET_malloc (username_length + 1);
1070     strncpy (username, ptr, username_length);
1071     ptr += username_length;
1072   }
1073   hostname = GNUNET_malloc (hostname_length + 1);
1074   strncpy (hostname, ptr, hostname_length);
1075   ptr += hostname_length;
1076   if (NULL == (host_cfg = GNUNET_TESTBED_extract_config_ (message)))
1077   {
1078     GNUNET_free_non_null (username);
1079     GNUNET_free_non_null (hostname);
1080     GNUNET_break_op (0);
1081     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1082     return;
1083   }
1084   host_id = ntohl (msg->host_id);
1085   LOG_DEBUG ("Received ADDHOST %u message\n", host_id);
1086   LOG_DEBUG ("-------host id: %u\n", host_id);
1087   LOG_DEBUG ("-------hostname: %s\n", hostname);
1088   if (NULL != username)
1089     LOG_DEBUG ("-------username: %s\n", username);
1090   else
1091     LOG_DEBUG ("-------username: <not given>\n");
1092   LOG_DEBUG ("-------ssh port: %u\n", ntohs (msg->ssh_port));
1093   host =
1094       GNUNET_TESTBED_host_create_with_id (host_id, hostname, username,
1095                                           host_cfg, ntohs (msg->ssh_port));
1096   GNUNET_free_non_null (username);
1097   GNUNET_free (hostname);
1098   GNUNET_CONFIGURATION_destroy (host_cfg);
1099   if (NULL == host)
1100   {
1101     GNUNET_break_op (0);
1102     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1103     return;
1104   }
1105   reply_size = sizeof (struct GNUNET_TESTBED_HostConfirmedMessage);
1106   if (GNUNET_OK != host_list_add (host))
1107   {
1108     /* We are unable to add a host */
1109     emsg = "A host exists with given host-id";
1110     LOG_DEBUG ("%s: %u", emsg, host_id);
1111     GNUNET_TESTBED_host_destroy (host);
1112     reply_size += strlen (emsg) + 1;
1113     reply = GNUNET_malloc (reply_size);
1114     memcpy (&reply[1], emsg, strlen (emsg) + 1);
1115   }
1116   else
1117   {
1118     LOG_DEBUG ("Added host %u at %u\n", host_id, GST_context->host_id);
1119     reply = GNUNET_malloc (reply_size);
1120   }
1121   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST_SUCCESS);
1122   reply->header.size = htons (reply_size);
1123   reply->host_id = htonl (host_id);
1124   GST_queue_message (client, &reply->header);
1125   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1126 }
1127
1128
1129 /**
1130  * Iterator over hash map entries.
1131  *
1132  * @param cls closure
1133  * @param key current key code
1134  * @param value value in the hash map
1135  * @return GNUNET_YES if we should continue to
1136  *         iterate,
1137  *         GNUNET_NO if not.
1138  */
1139 int
1140 ss_exists_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1141 {
1142   struct SharedService *queried_ss = cls;
1143   struct SharedService *ss = value;
1144
1145   if (0 == strcmp (ss->name, queried_ss->name))
1146     return GNUNET_NO;
1147   else
1148     return GNUNET_YES;
1149 }
1150
1151
1152 /**
1153  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_ADDHOST messages
1154  *
1155  * @param cls NULL
1156  * @param client identification of the client
1157  * @param message the actual message
1158  */
1159 static void
1160 handle_configure_shared_service (void *cls, struct GNUNET_SERVER_Client *client,
1161                                  const struct GNUNET_MessageHeader *message)
1162 {
1163   const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *msg;
1164   struct SharedService *ss;
1165   char *service_name;
1166   struct GNUNET_HashCode hash;
1167   uint16_t msg_size;
1168   uint16_t service_name_size;
1169
1170   msg = (const struct GNUNET_TESTBED_ConfigureSharedServiceMessage *) message;
1171   msg_size = ntohs (message->size);
1172   if (msg_size <= sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage))
1173   {
1174     GNUNET_break (0);
1175     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1176     return;
1177   }
1178   service_name_size =
1179       msg_size - sizeof (struct GNUNET_TESTBED_ConfigureSharedServiceMessage);
1180   service_name = (char *) &msg[1];
1181   if ('\0' != service_name[service_name_size - 1])
1182   {
1183     GNUNET_break (0);
1184     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1185     return;
1186   }
1187   LOG_DEBUG ("Received service sharing request for %s, with %d peers\n",
1188              service_name, ntohl (msg->num_peers));
1189   if (ntohl (msg->host_id) != GST_context->host_id)
1190   {
1191     route_message (ntohl (msg->host_id), message);
1192     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1193     return;
1194   }
1195   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1196   ss = GNUNET_malloc (sizeof (struct SharedService));
1197   ss->name = strdup (service_name);
1198   ss->num_shared = ntohl (msg->num_peers);
1199   GNUNET_CRYPTO_hash (ss->name, service_name_size, &hash);
1200   if (GNUNET_SYSERR ==
1201       GNUNET_CONTAINER_multihashmap_get_multiple (ss_map, &hash,
1202                                                   &ss_exists_iterator, ss))
1203   {
1204     LOG (GNUNET_ERROR_TYPE_WARNING,
1205          "Service %s already configured as a shared service. "
1206          "Ignoring service sharing request \n", ss->name);
1207     GNUNET_free (ss->name);
1208     GNUNET_free (ss);
1209     return;
1210   }
1211   GNUNET_CONTAINER_multihashmap_put (ss_map, &hash, ss,
1212                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1213 }
1214
1215
1216 /**
1217  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_LCONTROLLERS message
1218  *
1219  * @param cls NULL
1220  * @param client identification of the client
1221  * @param message the actual message
1222  */
1223 static void
1224 handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
1225                          const struct GNUNET_MessageHeader *message)
1226 {
1227   const struct GNUNET_TESTBED_ControllerLinkRequest *msg;
1228   struct GNUNET_CONFIGURATION_Handle *cfg;
1229   struct LCFContextQueue *lcfq;
1230   struct Route *route;
1231   struct Route *new_route;
1232   uint32_t delegated_host_id;
1233   uint32_t slave_host_id;
1234   uint16_t msize;
1235
1236   if (NULL == GST_context)
1237   {
1238     GNUNET_break (0);
1239     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1240     return;
1241   }
1242   msize = ntohs (message->size);
1243   if (sizeof (struct GNUNET_TESTBED_ControllerLinkRequest) >= msize)
1244   {
1245     GNUNET_break (0);
1246     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1247     return;
1248   }
1249   msg = (const struct GNUNET_TESTBED_ControllerLinkRequest *) message;
1250   delegated_host_id = ntohl (msg->delegated_host_id);
1251   if (delegated_host_id == GST_context->host_id)
1252   {
1253     GNUNET_break (0);
1254     LOG (GNUNET_ERROR_TYPE_WARNING, "Trying to link ourselves\n");
1255     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1256     return;
1257   }
1258   if ((delegated_host_id >= GST_host_list_size) ||
1259       (NULL == GST_host_list[delegated_host_id]))
1260   {
1261     LOG (GNUNET_ERROR_TYPE_WARNING,
1262          "Delegated host %u not registered with us\n", delegated_host_id);
1263     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1264     return;
1265   }
1266   slave_host_id = ntohl (msg->slave_host_id);
1267   if ((slave_host_id >= GST_host_list_size) ||
1268       (NULL == GST_host_list[slave_host_id]))
1269   {
1270     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
1271          slave_host_id);
1272     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1273     return;
1274   }
1275   if (slave_host_id == delegated_host_id)
1276   {
1277     LOG (GNUNET_ERROR_TYPE_WARNING, "Slave and delegated host are same\n");
1278     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1279     return;
1280   }
1281   cfg = GNUNET_TESTBED_extract_config_ (message); /* destroy cfg here or in lcfcontext */
1282   if (NULL == cfg)
1283   {
1284     GNUNET_break (0);         /* Configuration parsing error */
1285     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1286     return;
1287   }
1288   if (slave_host_id == GST_context->host_id)    /* Link from us */
1289   {
1290     struct Slave *slave;
1291     struct LinkControllersContext *lcc;
1292
1293     if ((delegated_host_id < GST_slave_list_size) &&
1294         (NULL != GST_slave_list[delegated_host_id]))
1295     {
1296       GNUNET_break (0);
1297       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1298       return;
1299     }
1300     slave = GNUNET_malloc (sizeof (struct Slave));
1301     slave->host_id = delegated_host_id;
1302     slave->reghost_map = GNUNET_CONTAINER_multihashmap_create (100, GNUNET_NO);
1303     slave_list_add (slave);
1304     if (1 != msg->is_subordinate)
1305     {
1306       slave->controller =
1307           GNUNET_TESTBED_controller_connect (cfg, GST_host_list[slave->host_id],
1308                                              event_mask, &slave_event_callback,
1309                                              slave);
1310       if (NULL != slave->controller)
1311         send_controller_link_response (client,
1312                                        GNUNET_ntohll (msg->operation_id),
1313                                        NULL,
1314                                        NULL);
1315       else
1316         send_controller_link_response (client,
1317                                        GNUNET_ntohll (msg->operation_id),
1318                                        NULL,
1319                                        "Could not connect to delegated controller");
1320       GNUNET_SERVER_receive_done (client, GNUNET_OK);
1321       return;
1322     }
1323     lcc = GNUNET_malloc (sizeof (struct LinkControllersContext));
1324     lcc->operation_id = GNUNET_ntohll (msg->operation_id);
1325     GNUNET_SERVER_client_keep (client);
1326     lcc->client = client;
1327     slave->lcc = lcc;
1328     slave->controller_proc =
1329         GNUNET_TESTBED_controller_start (GST_context->master_ip,
1330                                          GST_host_list[slave->host_id], cfg,
1331                                          &slave_status_callback, slave);
1332     GNUNET_CONFIGURATION_destroy (cfg);
1333     new_route = GNUNET_malloc (sizeof (struct Route));
1334     new_route->dest = delegated_host_id;
1335     new_route->thru = GST_context->host_id;
1336     route_list_add (new_route);
1337     return;
1338   }
1339
1340   /* Route the request */
1341   if (slave_host_id >= route_list_size)
1342   {
1343     LOG (GNUNET_ERROR_TYPE_WARNING, "No route towards slave host");
1344     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1345     return;
1346   }
1347   lcfq = GNUNET_malloc (sizeof (struct LCFContextQueue));
1348   lcfq->lcf = GNUNET_malloc (sizeof (struct LCFContext));
1349   lcfq->lcf->type = CLOSURE_TYPE_LCF;
1350   lcfq->lcf->delegated_host_id = delegated_host_id;
1351   lcfq->lcf->slave_host_id = slave_host_id;
1352   route = GST_find_dest_route (slave_host_id);
1353   GNUNET_assert (NULL != route);        /* because we add routes carefully */
1354   GNUNET_assert (route->dest < GST_slave_list_size);
1355   GNUNET_assert (NULL != GST_slave_list[route->dest]);
1356   lcfq->lcf->cfg = cfg;
1357   lcfq->lcf->is_subordinate = msg->is_subordinate;
1358   lcfq->lcf->state = INIT;
1359   lcfq->lcf->operation_id = GNUNET_ntohll (msg->operation_id);
1360   lcfq->lcf->gateway = GST_slave_list[route->dest];
1361   GNUNET_SERVER_client_keep (client);
1362   lcfq->lcf->client = client;
1363   if (NULL == lcfq_head)
1364   {
1365     GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
1366     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1367     lcf_proc_task_id = GNUNET_SCHEDULER_add_now (&lcf_proc_task, lcfq->lcf);
1368   }
1369   else
1370     GNUNET_CONTAINER_DLL_insert_tail (lcfq_head, lcfq_tail, lcfq);
1371   /* FIXME: Adding a new route should happen after the controllers are linked
1372    * successfully */
1373   if (1 != msg->is_subordinate)
1374   {
1375     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1376     return;
1377   }
1378   if ((delegated_host_id < route_list_size) &&
1379       (NULL != route_list[delegated_host_id]))
1380   {
1381     GNUNET_break_op (0);        /* Are you trying to link delegated host twice
1382                                  * with is subordinate flag set to GNUNET_YES? */
1383     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1384     return;
1385   }
1386   new_route = GNUNET_malloc (sizeof (struct Route));
1387   new_route->dest = delegated_host_id;
1388   new_route->thru = route->dest;
1389   route_list_add (new_route);
1390   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1391 }
1392
1393
1394 /**
1395  * The task to be executed if the forwarded peer create operation has been
1396  * timed out
1397  *
1398  * @param cls the FowardedOperationContext
1399  * @param tc the TaskContext from the scheduler
1400  */
1401 static void
1402 peer_create_forward_timeout (void *cls,
1403                              const struct GNUNET_SCHEDULER_TaskContext *tc)
1404 {
1405   struct ForwardedOperationContext *fopc = cls;
1406
1407   GNUNET_free (fopc->cls);
1408   GST_forwarded_operation_timeout (fopc, tc);
1409 }
1410
1411
1412 /**
1413  * Callback to be called when forwarded peer create operation is successfull. We
1414  * have to relay the reply msg back to the client
1415  *
1416  * @param cls ForwardedOperationContext
1417  * @param msg the peer create success message
1418  */
1419 static void
1420 peer_create_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1421 {
1422   struct ForwardedOperationContext *fopc = cls;
1423   struct Peer *remote_peer;
1424
1425   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS)
1426   {
1427     GNUNET_assert (NULL != fopc->cls);
1428     remote_peer = fopc->cls;
1429     peer_list_add (remote_peer);
1430   }
1431   GST_forwarded_operation_reply_relay (fopc, msg);
1432 }
1433
1434
1435 /**
1436  * Function to destroy a peer
1437  *
1438  * @param peer the peer structure to destroy
1439  */
1440 void
1441 GST_destroy_peer (struct Peer *peer)
1442 {
1443   GNUNET_break (0 == peer->reference_cnt);
1444   if (GNUNET_YES == peer->is_remote)
1445   {
1446     peer_list_remove (peer);
1447     GNUNET_free (peer);
1448     return;
1449   }
1450   if (GNUNET_YES == peer->details.local.is_running)
1451   {
1452     GNUNET_TESTING_peer_stop (peer->details.local.peer);
1453     peer->details.local.is_running = GNUNET_NO;
1454   }
1455   GNUNET_TESTING_peer_destroy (peer->details.local.peer);
1456   GNUNET_CONFIGURATION_destroy (peer->details.local.cfg);
1457   peer_list_remove (peer);
1458   GNUNET_free (peer);
1459 }
1460
1461
1462 /**
1463  * Callback to be called when forwarded peer destroy operation is successfull. We
1464  * have to relay the reply msg back to the client
1465  *
1466  * @param cls ForwardedOperationContext
1467  * @param msg the peer create success message
1468  */
1469 static void
1470 peer_destroy_success_cb (void *cls, const struct GNUNET_MessageHeader *msg)
1471 {
1472   struct ForwardedOperationContext *fopc = cls;
1473   struct Peer *remote_peer;
1474
1475   if (GNUNET_MESSAGE_TYPE_TESTBED_GENERIC_OPERATION_SUCCESS ==
1476       ntohs (msg->type))
1477   {
1478     remote_peer = fopc->cls;
1479     GNUNET_assert (NULL != remote_peer);
1480     remote_peer->destroy_flag = GNUNET_YES;
1481     if (0 == remote_peer->reference_cnt)
1482       GST_destroy_peer (remote_peer);
1483   }
1484   GST_forwarded_operation_reply_relay (fopc, msg);
1485 }
1486
1487
1488
1489 /**
1490  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_CREATEPEER messages
1491  *
1492  * @param cls NULL
1493  * @param client identification of the client
1494  * @param message the actual message
1495  */
1496 static void
1497 handle_peer_create (void *cls, struct GNUNET_SERVER_Client *client,
1498                     const struct GNUNET_MessageHeader *message)
1499 {
1500   const struct GNUNET_TESTBED_PeerCreateMessage *msg;
1501   struct GNUNET_TESTBED_PeerCreateSuccessEventMessage *reply;
1502   struct GNUNET_CONFIGURATION_Handle *cfg;
1503   struct ForwardedOperationContext *fo_ctxt;
1504   struct Route *route;
1505   struct Peer *peer;
1506   char *config;
1507   size_t dest_size;
1508   int ret;
1509   uint32_t config_size;
1510   uint32_t host_id;
1511   uint32_t peer_id;
1512   uint16_t msize;
1513
1514
1515   msize = ntohs (message->size);
1516   if (msize <= sizeof (struct GNUNET_TESTBED_PeerCreateMessage))
1517   {
1518     GNUNET_break (0);           /* We need configuration */
1519     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1520     return;
1521   }
1522   msg = (const struct GNUNET_TESTBED_PeerCreateMessage *) message;
1523   host_id = ntohl (msg->host_id);
1524   peer_id = ntohl (msg->peer_id);
1525   if (UINT32_MAX == peer_id)
1526   {
1527     GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1528                                  "Cannot create peer with given ID");
1529     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1530     return;
1531   }
1532   if (host_id == GST_context->host_id)
1533   {
1534     char *emsg;
1535
1536     /* We are responsible for this peer */
1537     msize -= sizeof (struct GNUNET_TESTBED_PeerCreateMessage);
1538     config_size = ntohl (msg->config_size);
1539     config = GNUNET_malloc (config_size);
1540     dest_size = config_size;
1541     if (Z_OK !=
1542         (ret =
1543          uncompress ((Bytef *) config, (uLongf *) & dest_size,
1544                      (const Bytef *) &msg[1], (uLong) msize)))
1545     {
1546       GNUNET_break (0);         /* uncompression error */
1547       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1548       return;
1549     }
1550     if (config_size != dest_size)
1551     {
1552       GNUNET_break (0);         /* Uncompressed config size mismatch */
1553       GNUNET_free (config);
1554       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1555       return;
1556     }
1557     cfg = GNUNET_CONFIGURATION_create ();
1558     if (GNUNET_OK !=
1559         GNUNET_CONFIGURATION_deserialize (cfg, config, config_size, GNUNET_NO))
1560     {
1561       GNUNET_break (0);         /* Configuration parsing error */
1562       GNUNET_free (config);
1563       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1564       return;
1565     }
1566     GNUNET_free (config);
1567     GNUNET_CONFIGURATION_set_value_number (cfg, "TESTBED", "PEERID",
1568                                            (unsigned long long) peer_id);
1569     peer = GNUNET_malloc (sizeof (struct Peer));
1570     peer->is_remote = GNUNET_NO;
1571     peer->details.local.cfg = cfg;
1572     peer->id = peer_id;
1573     LOG_DEBUG ("Creating peer with id: %u\n", (unsigned int) peer->id);
1574     peer->details.local.peer =
1575         GNUNET_TESTING_peer_configure (GST_context->system,
1576                                        peer->details.local.cfg, peer->id,
1577                                        NULL /* Peer id */ ,
1578                                        &emsg);
1579     if (NULL == peer->details.local.peer)
1580     {
1581       LOG (GNUNET_ERROR_TYPE_WARNING, "Configuring peer failed: %s\n", emsg);
1582       GNUNET_free (emsg);
1583       GNUNET_free (peer);
1584       GNUNET_break (0);
1585       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1586       return;
1587     }
1588     peer->details.local.is_running = GNUNET_NO;
1589     peer_list_add (peer);
1590     reply =
1591         GNUNET_malloc (sizeof
1592                        (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1593     reply->header.size =
1594         htons (sizeof (struct GNUNET_TESTBED_PeerCreateSuccessEventMessage));
1595     reply->header.type =
1596         htons (GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER_SUCCESS);
1597     reply->peer_id = msg->peer_id;
1598     reply->operation_id = msg->operation_id;
1599     GST_queue_message (client, &reply->header);
1600     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1601     return;
1602   }
1603
1604   /* Forward peer create request */
1605   route = GST_find_dest_route (host_id);
1606   if (NULL == route)
1607   {
1608     GNUNET_break (0);
1609     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1610     return;
1611   }
1612
1613   peer = GNUNET_malloc (sizeof (struct Peer));
1614   peer->is_remote = GNUNET_YES;
1615   peer->id = peer_id;
1616   peer->details.remote.slave = GST_slave_list[route->dest];
1617   peer->details.remote.remote_host_id = host_id;
1618   fo_ctxt = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1619   GNUNET_SERVER_client_keep (client);
1620   fo_ctxt->client = client;
1621   fo_ctxt->operation_id = GNUNET_ntohll (msg->operation_id);
1622   fo_ctxt->cls = peer;          //GST_slave_list[route->dest]->controller;
1623   fo_ctxt->type = OP_PEER_CREATE;
1624   fo_ctxt->opc =
1625       GNUNET_TESTBED_forward_operation_msg_ (GST_slave_list
1626                                              [route->dest]->controller,
1627                                              fo_ctxt->operation_id,
1628                                              &msg->header,
1629                                              peer_create_success_cb, fo_ctxt);
1630   fo_ctxt->timeout_task =
1631       GNUNET_SCHEDULER_add_delayed (GST_timeout, &peer_create_forward_timeout,
1632                                     fo_ctxt);
1633   GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fo_ctxt);
1634   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1635 }
1636
1637
1638 /**
1639  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1640  *
1641  * @param cls NULL
1642  * @param client identification of the client
1643  * @param message the actual message
1644  */
1645 static void
1646 handle_peer_destroy (void *cls, struct GNUNET_SERVER_Client *client,
1647                      const struct GNUNET_MessageHeader *message)
1648 {
1649   const struct GNUNET_TESTBED_PeerDestroyMessage *msg;
1650   struct ForwardedOperationContext *fopc;
1651   struct Peer *peer;
1652   uint32_t peer_id;
1653
1654   msg = (const struct GNUNET_TESTBED_PeerDestroyMessage *) message;
1655   peer_id = ntohl (msg->peer_id);
1656   LOG_DEBUG ("Received peer destory on peer: %u and operation id: %ul\n",
1657              peer_id, GNUNET_ntohll (msg->operation_id));
1658   if ((GST_peer_list_size <= peer_id) || (NULL == GST_peer_list[peer_id]))
1659   {
1660     LOG (GNUNET_ERROR_TYPE_ERROR,
1661          "Asked to destroy a non existent peer with id: %u\n", peer_id);
1662     GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1663                                  "Peer doesn't exist");
1664     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1665     return;
1666   }
1667   peer = GST_peer_list[peer_id];
1668   if (GNUNET_YES == peer->is_remote)
1669   {
1670     /* Forward the destory message to sub controller */
1671     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1672     GNUNET_SERVER_client_keep (client);
1673     fopc->client = client;
1674     fopc->cls = peer;
1675     fopc->type = OP_PEER_DESTROY;
1676     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1677     fopc->opc =
1678         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1679                                                slave->controller,
1680                                                fopc->operation_id, &msg->header,
1681                                                &peer_destroy_success_cb, fopc);
1682     fopc->timeout_task =
1683         GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1684                                       fopc);
1685     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1686     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1687     return;
1688   }
1689   peer->destroy_flag = GNUNET_YES;
1690   if (0 == peer->reference_cnt)
1691     GST_destroy_peer (peer);
1692   else
1693     LOG (GNUNET_ERROR_TYPE_DEBUG,
1694          "Delaying peer destroy as peer is currently in use\n");
1695   send_operation_success_msg (client, GNUNET_ntohll (msg->operation_id));
1696   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1697 }
1698
1699
1700 /**
1701  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1702  *
1703  * @param cls NULL
1704  * @param client identification of the client
1705  * @param message the actual message
1706  */
1707 static void
1708 handle_peer_start (void *cls, struct GNUNET_SERVER_Client *client,
1709                    const struct GNUNET_MessageHeader *message)
1710 {
1711   const struct GNUNET_TESTBED_PeerStartMessage *msg;
1712   struct GNUNET_TESTBED_PeerEventMessage *reply;
1713   struct ForwardedOperationContext *fopc;
1714   struct Peer *peer;
1715   uint32_t peer_id;
1716
1717   msg = (const struct GNUNET_TESTBED_PeerStartMessage *) message;
1718   peer_id = ntohl (msg->peer_id);
1719   if ((peer_id >= GST_peer_list_size) || (NULL == GST_peer_list[peer_id]))
1720   {
1721     GNUNET_break (0);
1722     LOG (GNUNET_ERROR_TYPE_ERROR,
1723          "Asked to start a non existent peer with id: %u\n", peer_id);
1724     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1725     return;
1726   }
1727   peer = GST_peer_list[peer_id];
1728   if (GNUNET_YES == peer->is_remote)
1729   {
1730     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1731     GNUNET_SERVER_client_keep (client);
1732     fopc->client = client;
1733     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1734     fopc->type = OP_PEER_START;
1735     fopc->opc =
1736         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1737                                                slave->controller,
1738                                                fopc->operation_id, &msg->header,
1739                                                &GST_forwarded_operation_reply_relay,
1740                                                fopc);
1741     fopc->timeout_task =
1742         GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1743                                       fopc);
1744     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1745     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1746     return;
1747   }
1748   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer->details.local.peer))
1749   {
1750     GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1751                                  "Failed to start");
1752     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1753     return;
1754   }
1755   peer->details.local.is_running = GNUNET_YES;
1756   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1757   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT);
1758   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1759   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_START);
1760   reply->host_id = htonl (GST_context->host_id);
1761   reply->peer_id = msg->peer_id;
1762   reply->operation_id = msg->operation_id;
1763   GST_queue_message (client, &reply->header);
1764   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1765 }
1766
1767
1768 /**
1769  * Message handler for GNUNET_MESSAGE_TYPE_TESTBED_DESTROYPEER messages
1770  *
1771  * @param cls NULL
1772  * @param client identification of the client
1773  * @param message the actual message
1774  */
1775 static void
1776 handle_peer_stop (void *cls, struct GNUNET_SERVER_Client *client,
1777                   const struct GNUNET_MessageHeader *message)
1778 {
1779   const struct GNUNET_TESTBED_PeerStopMessage *msg;
1780   struct GNUNET_TESTBED_PeerEventMessage *reply;
1781   struct ForwardedOperationContext *fopc;
1782   struct Peer *peer;
1783   uint32_t peer_id;
1784
1785   msg = (const struct GNUNET_TESTBED_PeerStopMessage *) message;
1786   peer_id = ntohl (msg->peer_id);
1787   LOG (GNUNET_ERROR_TYPE_DEBUG, "Received PEER_STOP for peer %u\n", peer_id);
1788   if ((peer_id >= GST_peer_list_size) || (NULL == GST_peer_list[peer_id]))
1789   {
1790     GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1791                                  "Peer not found");
1792     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1793     return;
1794   }
1795   peer = GST_peer_list[peer_id];
1796   if (GNUNET_YES == peer->is_remote)
1797   {
1798     LOG (GNUNET_ERROR_TYPE_DEBUG, "Forwarding PEER_STOP for peer %u\n",
1799          peer_id);
1800     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1801     GNUNET_SERVER_client_keep (client);
1802     fopc->client = client;
1803     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1804     fopc->type = OP_PEER_STOP;
1805     fopc->opc =
1806         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1807                                                slave->controller,
1808                                                fopc->operation_id, &msg->header,
1809                                                &GST_forwarded_operation_reply_relay,
1810                                                fopc);
1811     fopc->timeout_task =
1812         GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1813                                       fopc);
1814     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1815     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1816     return;
1817   }
1818   if (GNUNET_OK != GNUNET_TESTING_peer_kill (peer->details.local.peer))
1819   {
1820     LOG (GNUNET_ERROR_TYPE_WARNING, "Stopping peer %u failed\n", peer_id);
1821     GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1822                                  "Peer not running");
1823     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1824     return;
1825   }
1826   LOG (GNUNET_ERROR_TYPE_DEBUG, "Peer %u successfully stopped\n", peer_id);
1827   peer->details.local.is_running = GNUNET_NO;
1828   reply = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1829   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_EVENT);
1830   reply->header.size = htons (sizeof (struct GNUNET_TESTBED_PeerEventMessage));
1831   reply->event_type = htonl (GNUNET_TESTBED_ET_PEER_STOP);
1832   reply->host_id = htonl (GST_context->host_id);
1833   reply->peer_id = msg->peer_id;
1834   reply->operation_id = msg->operation_id;
1835   GST_queue_message (client, &reply->header);
1836   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1837   GNUNET_TESTING_peer_wait (peer->details.local.peer);
1838 }
1839
1840
1841 /**
1842  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETPEERCONFIG messages
1843  *
1844  * @param cls NULL
1845  * @param client identification of the client
1846  * @param message the actual message
1847  */
1848 static void
1849 handle_peer_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1850                         const struct GNUNET_MessageHeader *message)
1851 {
1852   const struct GNUNET_TESTBED_PeerGetConfigurationMessage *msg;
1853   struct GNUNET_TESTBED_PeerConfigurationInformationMessage *reply;
1854   struct Peer *peer;
1855   char *config;
1856   char *xconfig;
1857   size_t c_size;
1858   size_t xc_size;
1859   uint32_t peer_id;
1860   uint16_t msize;
1861
1862   msg = (const struct GNUNET_TESTBED_PeerGetConfigurationMessage *) message;
1863   peer_id = ntohl (msg->peer_id);
1864   if ((peer_id >= GST_peer_list_size) || (NULL == GST_peer_list[peer_id]))
1865   {
1866     GST_send_operation_fail_msg (client, GNUNET_ntohll (msg->operation_id),
1867                                  "Peer not found");
1868     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1869     return;
1870   }
1871   peer = GST_peer_list[peer_id];
1872   if (GNUNET_YES == peer->is_remote)
1873   {
1874     struct ForwardedOperationContext *fopc;
1875
1876     LOG_DEBUG ("Forwarding PEER_GET_CONFIG for peer: %u\n", peer_id);
1877     fopc = GNUNET_malloc (sizeof (struct ForwardedOperationContext));
1878     GNUNET_SERVER_client_keep (client);
1879     fopc->client = client;
1880     fopc->operation_id = GNUNET_ntohll (msg->operation_id);
1881     fopc->type = OP_PEER_INFO;
1882     fopc->opc =
1883         GNUNET_TESTBED_forward_operation_msg_ (peer->details.remote.
1884                                                slave->controller,
1885                                                fopc->operation_id, &msg->header,
1886                                                &GST_forwarded_operation_reply_relay,
1887                                                fopc);
1888     fopc->timeout_task =
1889         GNUNET_SCHEDULER_add_delayed (GST_timeout, &GST_forwarded_operation_timeout,
1890                                       fopc);
1891     GNUNET_CONTAINER_DLL_insert_tail (fopcq_head, fopcq_tail, fopc);
1892     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1893     return;
1894   }
1895   LOG_DEBUG ("Received PEER_GET_CONFIG for peer: %u\n", peer_id);
1896   config =
1897       GNUNET_CONFIGURATION_serialize (GST_peer_list[peer_id]->details.local.cfg,
1898                                       &c_size);
1899   xc_size = GNUNET_TESTBED_compress_config_ (config, c_size, &xconfig);
1900   GNUNET_free (config);
1901   msize =
1902       xc_size +
1903       sizeof (struct GNUNET_TESTBED_PeerConfigurationInformationMessage);
1904   reply = GNUNET_realloc (xconfig, msize);
1905   (void) memmove (&reply[1], reply, xc_size);
1906   reply->header.size = htons (msize);
1907   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_PEER_CONFIGURATION);
1908   reply->peer_id = msg->peer_id;
1909   reply->operation_id = msg->operation_id;
1910   GNUNET_TESTING_peer_get_identity (GST_peer_list[peer_id]->details.local.peer,
1911                                     &reply->peer_identity);
1912   reply->config_size = htons ((uint16_t) c_size);
1913   GST_queue_message (client, &reply->header);
1914   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1915 }
1916
1917
1918 /**
1919  * Handler for GNUNET_MESSAGE_TYPE_TESTBED_GETSLAVECONFIG messages
1920  *
1921  * @param cls NULL
1922  * @param client identification of the client
1923  * @param message the actual message
1924  */
1925 static void
1926 handle_slave_get_config (void *cls, struct GNUNET_SERVER_Client *client,
1927                          const struct GNUNET_MessageHeader *message)
1928 {
1929   struct GNUNET_TESTBED_SlaveGetConfigurationMessage *msg;
1930   struct Slave *slave;
1931   struct GNUNET_TESTBED_SlaveConfiguration *reply;
1932   const struct GNUNET_CONFIGURATION_Handle *cfg;
1933   char *config;
1934   char *xconfig;
1935   size_t config_size;
1936   size_t xconfig_size;
1937   size_t reply_size;
1938   uint64_t op_id;
1939   uint32_t slave_id;
1940
1941   msg = (struct GNUNET_TESTBED_SlaveGetConfigurationMessage *) message;
1942   slave_id = ntohl (msg->slave_id);
1943   op_id = GNUNET_ntohll (msg->operation_id);
1944   if ((GST_slave_list_size <= slave_id) || (NULL == GST_slave_list[slave_id]))
1945   {
1946     /* FIXME: Add forwardings for this type of message here.. */
1947     GST_send_operation_fail_msg (client, op_id, "Slave not found");
1948     GNUNET_SERVER_receive_done (client, GNUNET_OK);
1949     return;
1950   }
1951   slave = GST_slave_list[slave_id];
1952   GNUNET_assert (NULL != (cfg = GNUNET_TESTBED_host_get_cfg_ (GST_host_list[slave->host_id])));
1953   config = GNUNET_CONFIGURATION_serialize (cfg, &config_size);
1954   xconfig_size =
1955       GNUNET_TESTBED_compress_config_ (config, config_size, &xconfig);
1956   GNUNET_free (config);
1957   reply_size = xconfig_size + sizeof (struct GNUNET_TESTBED_SlaveConfiguration);
1958   GNUNET_break (reply_size <= UINT16_MAX);
1959   GNUNET_break (config_size <= UINT16_MAX);
1960   reply = GNUNET_realloc (xconfig, reply_size);
1961   (void) memmove (&reply[1], reply, xconfig_size);
1962   reply->header.type = htons (GNUNET_MESSAGE_TYPE_TESTBED_SLAVE_CONFIGURATION);
1963   reply->header.size = htons ((uint16_t) reply_size);
1964   reply->slave_id = msg->slave_id;
1965   reply->operation_id = msg->operation_id;
1966   reply->config_size = htons ((uint16_t) config_size);
1967   GST_queue_message (client, &reply->header);
1968   GNUNET_SERVER_receive_done (client, GNUNET_OK);
1969 }
1970
1971
1972 /**
1973  * Iterator over hash map entries.
1974  *
1975  * @param cls closure
1976  * @param key current key code
1977  * @param value value in the hash map
1978  * @return GNUNET_YES if we should continue to
1979  *         iterate,
1980  *         GNUNET_NO if not.
1981  */
1982 static int
1983 ss_map_free_iterator (void *cls, const struct GNUNET_HashCode *key, void *value)
1984 {
1985   struct SharedService *ss = value;
1986
1987   GNUNET_assert (GNUNET_YES ==
1988                  GNUNET_CONTAINER_multihashmap_remove (ss_map, key, value));
1989   GNUNET_free (ss->name);
1990   GNUNET_free (ss);
1991   return GNUNET_YES;
1992 }
1993
1994
1995 /**
1996  * Iterator for freeing hash map entries in a slave's reghost_map
1997  *
1998  * @param cls handle to the slave
1999  * @param key current key code
2000  * @param value value in the hash map
2001  * @return GNUNET_YES if we should continue to
2002  *         iterate,
2003  *         GNUNET_NO if not.
2004  */
2005 static int
2006 reghost_free_iterator (void *cls, const struct GNUNET_HashCode *key,
2007                        void *value)
2008 {
2009   struct Slave *slave = cls;
2010   struct RegisteredHostContext *rhc = value;
2011   struct ForwardedOverlayConnectContext *focc;
2012
2013   GNUNET_assert (GNUNET_YES ==
2014                  GNUNET_CONTAINER_multihashmap_remove (slave->reghost_map, key,
2015                                                        value));
2016   while (NULL != (focc = rhc->focc_dll_head))
2017   {
2018     GNUNET_CONTAINER_DLL_remove (rhc->focc_dll_head, rhc->focc_dll_tail, focc);
2019     GST_cleanup_focc (focc);
2020   }
2021   if (NULL != rhc->sub_op)
2022     GNUNET_TESTBED_operation_done (rhc->sub_op);
2023   if (NULL != rhc->client)
2024     GNUNET_SERVER_client_drop (rhc->client);
2025   GNUNET_free (value);
2026   return GNUNET_YES;
2027 }
2028
2029
2030 /**
2031  * Task to clean up and shutdown nicely
2032  *
2033  * @param cls NULL
2034  * @param tc the TaskContext from scheduler
2035  */
2036 static void
2037 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2038 {
2039   struct LCFContextQueue *lcfq;
2040   struct ForwardedOperationContext *fopc;
2041   struct MessageQueue *mq_entry;
2042   uint32_t id;
2043
2044   shutdown_task_id = GNUNET_SCHEDULER_NO_TASK;
2045   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down testbed service\n");
2046   (void) GNUNET_CONTAINER_multihashmap_iterate (ss_map, &ss_map_free_iterator,
2047                                                 NULL);
2048   GNUNET_CONTAINER_multihashmap_destroy (ss_map);
2049   /* cleanup any remaining forwarded operations */
2050   while (NULL != (fopc = fopcq_head))
2051   {
2052     GNUNET_CONTAINER_DLL_remove (fopcq_head, fopcq_tail, fopc);
2053     GNUNET_TESTBED_forward_operation_msg_cancel_ (fopc->opc);
2054     if (GNUNET_SCHEDULER_NO_TASK != fopc->timeout_task)
2055       GNUNET_SCHEDULER_cancel (fopc->timeout_task);
2056     GNUNET_SERVER_client_drop (fopc->client);
2057     switch (fopc->type)
2058     {
2059     case OP_PEER_CREATE:
2060       GNUNET_free (fopc->cls);
2061       break;
2062     case OP_PEER_START:
2063     case OP_PEER_STOP:
2064     case OP_PEER_DESTROY:
2065     case OP_PEER_INFO:
2066     case OP_OVERLAY_CONNECT:
2067     case OP_LINK_CONTROLLERS:
2068     case OP_GET_SLAVE_CONFIG:
2069       break;
2070     case OP_FORWARDED:
2071       GNUNET_assert (0);
2072     };
2073     GNUNET_free (fopc);
2074   }
2075   if (NULL != lcfq_head)
2076   {
2077     if (GNUNET_SCHEDULER_NO_TASK != lcf_proc_task_id)
2078     {
2079       GNUNET_SCHEDULER_cancel (lcf_proc_task_id);
2080       lcf_proc_task_id = GNUNET_SCHEDULER_NO_TASK;
2081     }
2082   }
2083   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == lcf_proc_task_id);
2084   for (lcfq = lcfq_head; NULL != lcfq; lcfq = lcfq_head)
2085   {
2086     GNUNET_SERVER_client_drop (lcfq->lcf->client);
2087     GNUNET_assert (NULL != lcfq->lcf->cfg);
2088     GNUNET_CONFIGURATION_destroy (lcfq->lcf->cfg);
2089     GNUNET_free (lcfq->lcf);
2090     GNUNET_CONTAINER_DLL_remove (lcfq_head, lcfq_tail, lcfq);
2091     GNUNET_free (lcfq);
2092   }
2093   GST_free_occq ();
2094   GST_free_roccq ();
2095   /* Clear peer list */
2096   for (id = 0; id < GST_peer_list_size; id++)
2097     if (NULL != GST_peer_list[id])
2098     {
2099       /* If destroy flag is set it means that this peer should have been
2100        * destroyed by a context which we destroy before */
2101       GNUNET_break (GNUNET_NO == GST_peer_list[id]->destroy_flag);
2102       /* counter should be zero as we free all contexts before */
2103       GNUNET_break (0 == GST_peer_list[id]->reference_cnt);
2104       if ((GNUNET_NO == GST_peer_list[id]->is_remote) &&
2105           (GNUNET_YES == GST_peer_list[id]->details.local.is_running))
2106         GNUNET_TESTING_peer_kill (GST_peer_list[id]->details.local.peer);
2107     }
2108   for (id = 0; id < GST_peer_list_size; id++)
2109     if (NULL != GST_peer_list[id])
2110     {
2111       if (GNUNET_NO == GST_peer_list[id]->is_remote)
2112       {
2113         if (GNUNET_YES == GST_peer_list[id]->details.local.is_running)
2114           GNUNET_TESTING_peer_wait (GST_peer_list[id]->details.local.peer);
2115         GNUNET_TESTING_peer_destroy (GST_peer_list[id]->details.local.peer);
2116         GNUNET_CONFIGURATION_destroy (GST_peer_list[id]->details.local.cfg);
2117       }
2118       GNUNET_free (GST_peer_list[id]);
2119     }
2120   GNUNET_free_non_null (GST_peer_list);
2121   /* Clear host list */
2122   for (id = 0; id < GST_host_list_size; id++)
2123     if (NULL != GST_host_list[id])
2124       GNUNET_TESTBED_host_destroy (GST_host_list[id]);
2125   GNUNET_free_non_null (GST_host_list);
2126   /* Clear route list */
2127   for (id = 0; id < route_list_size; id++)
2128     if (NULL != route_list[id])
2129       GNUNET_free (route_list[id]);
2130   GNUNET_free_non_null (route_list);
2131   /* Clear GST_slave_list */
2132   for (id = 0; id < GST_slave_list_size; id++)
2133     if (NULL != GST_slave_list[id])
2134     {
2135       struct HostRegistration *hr_entry;
2136
2137       while (NULL != (hr_entry = GST_slave_list[id]->hr_dll_head))
2138       {
2139         GNUNET_CONTAINER_DLL_remove (GST_slave_list[id]->hr_dll_head,
2140                                      GST_slave_list[id]->hr_dll_tail, hr_entry);
2141         GNUNET_free (hr_entry);
2142       }
2143       if (NULL != GST_slave_list[id]->rhandle)
2144         GNUNET_TESTBED_cancel_registration (GST_slave_list[id]->rhandle);
2145       (void)
2146           GNUNET_CONTAINER_multihashmap_iterate (GST_slave_list
2147                                                  [id]->reghost_map,
2148                                                  reghost_free_iterator,
2149                                                  GST_slave_list[id]);
2150       GNUNET_CONTAINER_multihashmap_destroy (GST_slave_list[id]->reghost_map);
2151       if (NULL != GST_slave_list[id]->controller)
2152         GNUNET_TESTBED_controller_disconnect (GST_slave_list[id]->controller);
2153       if (NULL != GST_slave_list[id]->controller_proc)
2154         GNUNET_TESTBED_controller_stop (GST_slave_list[id]->controller_proc);
2155       GNUNET_free (GST_slave_list[id]);
2156     }
2157   GNUNET_free_non_null (GST_slave_list);
2158   if (NULL != GST_context)
2159   {
2160     GNUNET_free_non_null (GST_context->master_ip);
2161     if (NULL != GST_context->system)
2162       GNUNET_TESTING_system_destroy (GST_context->system, GNUNET_YES);
2163     GNUNET_SERVER_client_drop (GST_context->client);
2164     GNUNET_free (GST_context);
2165     GST_context = NULL;
2166   }
2167   if (NULL != transmit_handle)
2168     GNUNET_SERVER_notify_transmit_ready_cancel (transmit_handle);
2169   while (NULL != (mq_entry = mq_head))
2170   {
2171     GNUNET_free (mq_entry->msg);
2172     GNUNET_SERVER_client_drop (mq_entry->client);
2173     GNUNET_CONTAINER_DLL_remove (mq_head, mq_tail, mq_entry);
2174     GNUNET_free (mq_entry);
2175   }
2176   GNUNET_free_non_null (hostname);
2177   GNUNET_CONFIGURATION_destroy (our_config);
2178   /* Free hello cache */
2179   GST_cache_clear ();
2180   GNUNET_TESTBED_operation_queue_destroy_ (GST_opq_openfds);
2181   GST_opq_openfds = NULL;
2182 }
2183
2184
2185 /**
2186  * Callback for client disconnect
2187  *
2188  * @param cls NULL
2189  * @param client the client which has disconnected
2190  */
2191 static void
2192 client_disconnect_cb (void *cls, struct GNUNET_SERVER_Client *client)
2193 {
2194   if (NULL == GST_context)
2195     return;
2196   if (client == GST_context->client)
2197   {
2198     LOG (GNUNET_ERROR_TYPE_DEBUG, "Master client disconnected\n");
2199     /* should not be needed as we're terminated by failure to read
2200      * from stdin, but if stdin fails for some reason, this shouldn't
2201      * hurt for now --- might need to revise this later if we ever
2202      * decide that master connections might be temporarily down
2203      * for some reason */
2204     //GNUNET_SCHEDULER_shutdown ();
2205   }
2206 }
2207
2208
2209 /**
2210  * Testbed setup
2211  *
2212  * @param cls closure
2213  * @param server the initialized server
2214  * @param cfg configuration to use
2215  */
2216 static void
2217 testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
2218              const struct GNUNET_CONFIGURATION_Handle *cfg)
2219 {
2220   static const struct GNUNET_SERVER_MessageHandler message_handlers[] = {
2221     {&handle_init, NULL, GNUNET_MESSAGE_TYPE_TESTBED_INIT, 0},
2222     {&handle_add_host, NULL, GNUNET_MESSAGE_TYPE_TESTBED_ADD_HOST, 0},
2223     {&handle_configure_shared_service, NULL,
2224      GNUNET_MESSAGE_TYPE_TESTBED_SHARE_SERVICE, 0},
2225     {&handle_link_controllers, NULL,
2226      GNUNET_MESSAGE_TYPE_TESTBED_LINK_CONTROLLERS, 0},
2227     {&handle_peer_create, NULL, GNUNET_MESSAGE_TYPE_TESTBED_CREATE_PEER, 0},
2228     {&handle_peer_destroy, NULL, GNUNET_MESSAGE_TYPE_TESTBED_DESTROY_PEER,
2229      sizeof (struct GNUNET_TESTBED_PeerDestroyMessage)},
2230     {&handle_peer_start, NULL, GNUNET_MESSAGE_TYPE_TESTBED_START_PEER,
2231      sizeof (struct GNUNET_TESTBED_PeerStartMessage)},
2232     {&handle_peer_stop, NULL, GNUNET_MESSAGE_TYPE_TESTBED_STOP_PEER,
2233      sizeof (struct GNUNET_TESTBED_PeerStopMessage)},
2234     {&handle_peer_get_config, NULL,
2235      GNUNET_MESSAGE_TYPE_TESTBED_GET_PEER_CONFIGURATION,
2236      sizeof (struct GNUNET_TESTBED_PeerGetConfigurationMessage)},
2237     {&GST_handle_overlay_connect, NULL,
2238      GNUNET_MESSAGE_TYPE_TESTBED_OVERLAY_CONNECT,
2239      sizeof (struct GNUNET_TESTBED_OverlayConnectMessage)},
2240     {&GST_handle_remote_overlay_connect, NULL,
2241      GNUNET_MESSAGE_TYPE_TESTBED_REMOTE_OVERLAY_CONNECT, 0},
2242     {handle_slave_get_config, NULL,
2243      GNUNET_MESSAGE_TYPE_TESTBED_GET_SLAVE_CONFIGURATION,
2244      sizeof (struct GNUNET_TESTBED_SlaveGetConfigurationMessage)},
2245     {NULL}
2246   };
2247   char *logfile;
2248   unsigned long long num;
2249
2250   if (GNUNET_OK ==
2251       GNUNET_CONFIGURATION_get_value_filename (cfg, "TESTBED", "LOG_FILE",
2252                                                &logfile))
2253   {
2254     GNUNET_break (GNUNET_OK == GNUNET_log_setup ("testbed", "DEBUG", logfile));
2255     GNUNET_free (logfile);
2256   }
2257   GNUNET_assert (GNUNET_OK ==
2258                  GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
2259                                                         "CACHE_SIZE", &num));
2260   GST_cache_init ((unsigned int) num);
2261   GNUNET_assert (GNUNET_OK ==
2262                  GNUNET_CONFIGURATION_get_value_number (cfg, "TESTBED",
2263                                                         "MAX_OPEN_FDS", &num));
2264   GST_opq_openfds = GNUNET_TESTBED_operation_queue_create_ ((unsigned int) num);
2265   GNUNET_assert (GNUNET_OK ==
2266                  GNUNET_CONFIGURATION_get_value_time (cfg, "TESTBED",
2267                                                       "OPERATION_TIMEOUT",
2268                                                       (struct
2269                                                        GNUNET_TIME_Relative *)
2270                                                       &GST_timeout));
2271   GNUNET_assert (GNUNET_OK ==
2272                  GNUNET_CONFIGURATION_get_value_string (cfg, "testbed",
2273                                                         "HOSTNAME", &hostname));  
2274   our_config = GNUNET_CONFIGURATION_dup (cfg);
2275   GNUNET_SERVER_add_handlers (server, message_handlers);
2276   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
2277   ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);
2278   shutdown_task_id =
2279       GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
2280                                                   GNUNET_SCHEDULER_PRIORITY_IDLE,
2281                                                   &shutdown_task, NULL);
2282   LOG_DEBUG ("Testbed startup complete\n");
2283   event_mask = 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED;
2284 }
2285
2286
2287 /**
2288  * The starting point of execution
2289  */
2290 int
2291 main (int argc, char *const *argv)
2292 {
2293   //sleep (15);                 /* Debugging */
2294   return (GNUNET_OK ==
2295           GNUNET_SERVICE_run (argc, argv, "testbed", GNUNET_SERVICE_OPTION_NONE,
2296                               &testbed_run, NULL)) ? 0 : 1;
2297 }
2298
2299 /* end of gnunet-service-testbed.c */