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